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.

287 lines
8.6KB

  1. #ifndef DSJ_VALLEY_WIDGETS_HPP
  2. #define DSJ_VALLEY_WIDGETS_HPP
  3. #include "global_pre.hpp"
  4. #include "Valley.hpp"
  5. #include "window.hpp"
  6. #include <functional>
  7. // Dynamic Panel
  8. struct PanelBorderWidget : TransparentWidget {
  9. void draw(NVGcontext *vg) override;
  10. };
  11. struct DynamicPanelWidget : FramebufferWidget {
  12. int* mode;
  13. int oldMode;
  14. std::vector<std::shared_ptr<SVG>> panels;
  15. SVGWidget* visiblePanel;
  16. PanelBorderWidget* border;
  17. DynamicPanelWidget();
  18. void addPanel(std::shared_ptr<SVG> svg);
  19. void step() override;
  20. };
  21. ////////////////////////////////////////////////////////////////////////////////////////////////////
  22. // View mode
  23. enum DynamicViewMode {
  24. ACTIVE_HIGH_VIEW,
  25. ACTIVE_LOW_VIEW,
  26. ALWAYS_ACTIVE_VIEW
  27. };
  28. ////////////////////////////////////////////////////////////////////////////////////////////////////
  29. // Dynamic Switch
  30. struct DynamicSwitchWidget : virtual ParamWidget, FramebufferWidget {
  31. std::vector<std::shared_ptr<SVG>> frames;
  32. SVGWidget* sw;
  33. int* visibility;
  34. DynamicViewMode viewMode;
  35. DynamicSwitchWidget();
  36. void addFrame(std::shared_ptr<SVG> svg);
  37. void step() override;
  38. void onChange(EventChange &e) override;
  39. };
  40. template <class TDynamicSwitch>
  41. DynamicSwitchWidget* createDynamicSwitchWidget(Vec pos, Module *module, int paramId,
  42. float minValue, float maxValue, float defaultValue,
  43. int* visibilityHandle, DynamicViewMode viewMode) {
  44. DynamicSwitchWidget *dynSwitch = new TDynamicSwitch();
  45. dynSwitch->box.pos = pos;
  46. dynSwitch->module = module;
  47. dynSwitch->paramId = paramId;
  48. dynSwitch->setLimits(minValue, maxValue);
  49. dynSwitch->setDefaultValue(defaultValue);
  50. dynSwitch->visibility = visibilityHandle;
  51. dynSwitch->viewMode = viewMode;
  52. return dynSwitch;
  53. }
  54. ////////////////////////////////////////////////////////////////////////////////////////////////////
  55. // Dynamic lights
  56. struct DynamicModuleLightWidget : MultiLightWidget {
  57. Module *module = NULL;
  58. int firstLightId;
  59. int* visibility = nullptr;
  60. DynamicViewMode viewMode = ACTIVE_HIGH_VIEW;
  61. void step() override;
  62. };
  63. template<class TDynamicModuleLightWidget>
  64. DynamicModuleLightWidget *createDynamicLight(Vec pos, Module *module, int firstLightId,
  65. int* visibilityHandle, DynamicViewMode viewMode) {
  66. DynamicModuleLightWidget *light = new TDynamicModuleLightWidget();
  67. light->box.pos = pos;
  68. light->module = module;
  69. light->firstLightId = firstLightId;
  70. light->visibility = visibilityHandle;
  71. light->viewMode = viewMode;
  72. return light;
  73. }
  74. ////////////////////////////////////////////////////////////////////////////////////////////////////
  75. // Dynamic knob
  76. enum DynamicKnobMotion {
  77. SMOOTH_MOTION,
  78. SNAP_MOTION
  79. };
  80. struct DynamicKnob : virtual Knob, FramebufferWidget {
  81. /** Angles in radians */
  82. float minAngle, maxAngle;
  83. /** Not owned */
  84. TransformWidget *tw;
  85. SVGWidget *sw;
  86. CircularShadow *shadow;
  87. int* _visibility;
  88. DynamicViewMode _viewMode;
  89. DynamicKnob();
  90. void setSVG(std::shared_ptr<SVG> svg);
  91. void step() override;
  92. void onChange(EventChange &e) override;
  93. };
  94. template <class TDynamicKnob>
  95. DynamicKnob* createDynamicKnob(const Vec& pos,
  96. Module* module,
  97. int paramId,
  98. int* visibilityHandle,
  99. DynamicViewMode viewMode,
  100. float minValue,
  101. float maxValue,
  102. float defaultValue,
  103. DynamicKnobMotion motion) {
  104. DynamicKnob* knob = new TDynamicKnob;
  105. knob->module = module;
  106. knob->box.pos = pos;
  107. knob->paramId = paramId;
  108. knob->setLimits(minValue, maxValue);
  109. knob->setDefaultValue(defaultValue);
  110. knob->_visibility = visibilityHandle;
  111. knob->_viewMode = viewMode;
  112. if(motion == SNAP_MOTION) {
  113. knob->snap = true;
  114. }
  115. return knob;
  116. }
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////
  118. // Dynamic text
  119. struct DynamicText : TransparentWidget {
  120. std::shared_ptr<std::string> text;
  121. std::shared_ptr<Font> font;
  122. int size;
  123. int* visibility;
  124. DynamicViewMode viewMode;
  125. enum ColorMode {
  126. COLOR_MODE_WHITE = 0,
  127. COLOR_MODE_BLACK
  128. };
  129. int* colorHandle;
  130. NVGcolor textColor;
  131. DynamicText();
  132. virtual void draw(NVGcontext* vg) override;
  133. void step() override;
  134. };
  135. DynamicText* createDynamicText(const Vec& pos, int size, std::string text,
  136. int* visibilityHandle, DynamicViewMode viewMode);
  137. DynamicText* createDynamicText(const Vec& pos, int size, std::string text,
  138. int* visibilityHandle, int* colorHandle, DynamicViewMode viewMode);
  139. DynamicText* createDynamicText(const Vec& pos, int size, std::shared_ptr<std::string> text,
  140. int* visibilityHandle, DynamicViewMode viewMode);
  141. DynamicText* createDynamicText(const Vec& pos, int size, std::shared_ptr<std::string> text,
  142. int* visibilityHandle, int* colorHandle, DynamicViewMode viewMode);
  143. struct DynamicFrameText : DynamicText {
  144. int* itemHandle;
  145. std::vector<std::string> textItem;
  146. DynamicFrameText();
  147. void addItem(const std::string& item);
  148. void draw(NVGcontext* vg) override;
  149. };
  150. template<class T>
  151. class DynamicValueText : public TransformWidget {
  152. public:
  153. std::shared_ptr<Font> font;
  154. int size;
  155. int* visibility;
  156. DynamicViewMode viewMode;
  157. enum ColorMode {
  158. COLOR_MODE_WHITE = 0,
  159. COLOR_MODE_BLACK
  160. };
  161. int* colorHandle;
  162. NVGcolor textColor;
  163. DynamicValueText(std::shared_ptr<T> value, std::function<std::string(T)> valueToText) {
  164. font = Font::load(assetPlugin(plugin, "res/din1451alt.ttf"));
  165. size = 16;
  166. visibility = nullptr;
  167. colorHandle = nullptr;
  168. viewMode = ACTIVE_HIGH_VIEW;
  169. _value = value;
  170. _valueToText = valueToText;
  171. }
  172. void draw(NVGcontext* vg) override {
  173. nvgFontSize(vg, size);
  174. nvgFontFaceId(vg, font->handle);
  175. nvgTextLetterSpacing(vg, 0.f);
  176. Vec textPos = Vec(0.f, 0.f);
  177. if(colorHandle != nullptr) {
  178. switch((ColorMode)*colorHandle) {
  179. case COLOR_MODE_WHITE: textColor = nvgRGB(0xFF,0xFF,0xFF); break;
  180. case COLOR_MODE_BLACK: textColor = nvgRGB(0x14,0x14, 0x14); break;
  181. default: textColor = nvgRGB(0xFF,0xFF,0xFF);
  182. }
  183. }
  184. else {
  185. textColor = nvgRGB(0xFF,0xFF,0xFF);
  186. }
  187. nvgFillColor(vg, textColor);
  188. nvgTextAlign(vg, NVG_ALIGN_CENTER | NVG_ALIGN_TOP);
  189. nvgText(vg, textPos.x, textPos.y, _text.c_str(), NULL);
  190. }
  191. void step() override {
  192. _text = _valueToText(*_value);
  193. if(visibility != nullptr) {
  194. if(*visibility) {
  195. visible = true;
  196. }
  197. else {
  198. visible = false;
  199. }
  200. if(viewMode == ACTIVE_LOW_VIEW) {
  201. visible = !visible;
  202. }
  203. }
  204. }
  205. private:
  206. std::shared_ptr<T> _value;
  207. std::function<std::string(T)> _valueToText;
  208. std::string _text;
  209. };
  210. ///////////////////////////////////////////////////////////////////////////////////////////////////
  211. // Dynamic Choices
  212. struct DynamicItem : MenuItem {
  213. unsigned long _itemNumber;
  214. unsigned long* _choice;
  215. DynamicItem(unsigned long itemNumber);
  216. void onAction(EventAction &e) override;
  217. };
  218. struct DynamicChoice : ChoiceButton {
  219. unsigned long* _choice;
  220. std::vector<std::string> _items;
  221. std::shared_ptr<std::string> _text;
  222. std::shared_ptr<Font> _font;
  223. int* _visibility;
  224. int _textSize;
  225. DynamicViewMode _viewMode;
  226. DynamicChoice();
  227. void step() override;
  228. void onAction(EventAction &e) override;
  229. void draw(NVGcontext* vg) override;
  230. };
  231. DynamicChoice* createDynamicChoice(const Vec& pos,
  232. float width,
  233. const std::vector<std::string>& items,
  234. unsigned long* choiceHandle,
  235. int* visibilityHandle,
  236. DynamicViewMode viewMode);
  237. template<typename T = SVGKnob>
  238. T *createValleyKnob(Vec pos, Module *module, int paramId, float minValue, float maxValue,
  239. float defaultValue, float minAngle, float maxAngle) {
  240. T *o = Component::create<T>(pos, module);
  241. o->paramId = paramId;
  242. o->setLimits(minValue, maxValue);
  243. o->setDefaultValue(defaultValue);
  244. o->minAngle = minAngle;
  245. o->maxAngle = maxAngle;
  246. return o;
  247. }
  248. #endif