You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.6KB

  1. #include "TSParamTextField.hpp"
  2. #include <string>
  3. //-----------------------------------------------------------------------------------------------
  4. // TSParamTextField()
  5. // @textType: (IN) Text type for this field (for validation). Should be a value type.
  6. // @maxLength: (IN) Max length for this field.
  7. // @paramCtl: (IN) The widget for control.
  8. // @formatStr: (IN) The format string.
  9. //-----------------------------------------------------------------------------------------------
  10. TSParamTextField::TSParamTextField(TextType textType, int maxLength, ParamWidget* paramCtl, const char* formatStr) : TSTextField(textType, maxLength)
  11. {
  12. this->control = paramCtl;
  13. FramebufferWidget* fbw = dynamic_cast<FramebufferWidget*>(paramCtl);
  14. if (fbw != NULL)
  15. {
  16. isDirty = &(fbw->dirty);
  17. isBufferedCtrl = true;
  18. }
  19. this->formatString = formatStr;
  20. return;
  21. }
  22. //-----------------------------------------------------------------------------------------------
  23. // saveValue()
  24. // Validate input and save value (valid values only).
  25. //-----------------------------------------------------------------------------------------------
  26. void TSParamTextField::saveValue()
  27. {
  28. isEditing = 2; // Wait 2 cycles before setting knob -> text again in step()
  29. char buffer[50] = { 0 };
  30. if (control != NULL)
  31. {
  32. float controlVal = control->value;
  33. if (isValid()) {
  34. // Set the value on the control:
  35. float val = (text.length() > 0) ? std::stof(text.c_str()) : 0.0f;
  36. if (text2KnobVal != NULL)
  37. controlVal = text2KnobVal(val);
  38. else
  39. controlVal = val;
  40. if (controlVal < control->minValue)
  41. {
  42. val = (knob2TextVal == NULL) ? control->minValue : knob2TextVal(control->minValue);
  43. controlVal = control->minValue;
  44. }
  45. else if (controlVal > control->maxValue)
  46. {
  47. val = (knob2TextVal == NULL) ? control->maxValue : knob2TextVal(control->maxValue);
  48. controlVal = control->maxValue;
  49. }
  50. control->setValue(controlVal);
  51. if (isBufferedCtrl && isDirty != NULL)
  52. {
  53. *isDirty = true; // Set dirty flag to redraw
  54. }
  55. }
  56. lastControlVal = controlVal;
  57. if (knob2TextVal != NULL)
  58. sprintf(buffer, formatString, knob2TextVal(controlVal));
  59. else
  60. sprintf(buffer, formatString, controlVal);
  61. text = buffer;
  62. }
  63. return;
  64. }
  65. //-----------------------------------------------------------------------------------------------
  66. // onAction()
  67. // Save value if valid.
  68. //-----------------------------------------------------------------------------------------------
  69. void TSParamTextField::onAction(EventAction &e)
  70. {
  71. //debug("onAction() - visible = %d!", visible);
  72. if (visible)
  73. {
  74. saveValue();
  75. e.consumed = true;
  76. }
  77. return;
  78. }
  79. //-----------------------------------------------------------------------------------------------
  80. // onDefocus()
  81. // Validate input, set control value to match, format the text field number.
  82. //-----------------------------------------------------------------------------------------------
  83. void TSParamTextField::onDefocus(EventDefocus &e)
  84. {
  85. saveValue();
  86. if (autoHideMode == AutoHideMode::AutoHideOnDefocus) {
  87. visible = false;
  88. }
  89. isEditing = 2; // Wait one cycle before setting knob -> text again in step()
  90. e.consumed = true;
  91. return;
  92. } // end onDefocus()
  93. //-----------------------------------------------------------------------------------------------
  94. // step()
  95. // Set value to match the control.
  96. //-----------------------------------------------------------------------------------------------
  97. void TSParamTextField::step()
  98. {
  99. if (control != NULL && !isEditing)
  100. {
  101. if (control->value != lastControlVal)
  102. {
  103. char buffer[50] = { 0 };
  104. if (knob2TextVal != NULL)
  105. sprintf(buffer, formatString, knob2TextVal(control->value));
  106. else
  107. sprintf(buffer, formatString, control->value);
  108. text = buffer;
  109. lastControlVal = control->value;
  110. }
  111. }
  112. else if (isEditing < 3 && isEditing > 0)
  113. isEditing--;
  114. return;
  115. }
  116. //-----------------------------------------------------------------------------------------------
  117. // setText()
  118. // @val : (IN) Float value to set the text.
  119. // Uses the format string.
  120. //-----------------------------------------------------------------------------------------------
  121. void TSParamTextField::setText(float val)
  122. {
  123. char buffer[50] = { 0 };
  124. float controlVal = val;
  125. if (control != NULL)
  126. {
  127. if (text2KnobVal != NULL)
  128. controlVal = text2KnobVal(val);
  129. else
  130. controlVal = val;
  131. if (controlVal < control->minValue)
  132. val = (knob2TextVal == NULL) ? control->minValue : knob2TextVal(control->minValue);
  133. else if (controlVal > control->maxValue)
  134. val = (knob2TextVal == NULL) ? control->maxValue : knob2TextVal(control->maxValue);
  135. }
  136. // Format the text
  137. sprintf(buffer, formatString, val);
  138. text = buffer;
  139. return;
  140. }