1 /* B.Oops
2  * Glitch effect sequencer LV2 plugin
3  *
4  * Copyright (C) 2020 by Sven Jähnichen
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef OPTIONTAPESPEED_HPP_
21 #define OPTIONTAPESPEED_HPP_
22 
23 #include <new>
24 #include "OptionWidget.hpp"
25 #include "BWidgets/Label.hpp"
26 #include "DialRange.hpp"
27 
28 class OptionTapeSpeed : public OptionWidget
29 {
30 public:
OptionTapeSpeed()31 	OptionTapeSpeed () : OptionTapeSpeed (0.0, 0.0, 0.0, 0.0, "widget") {}
OptionTapeSpeed(const double x,const double y,const double width,const double height,const std::string & name)32 	OptionTapeSpeed (const double x, const double y, const double width, const double height, const std::string& name) :
33 		OptionWidget (x, y, width, height, name),
34 		tapeSpeedLabel (10, 90, 60, 20, "ctlabel", BOOPS_LABEL_SPEED)
35 	{
36 		try
37 		{
38 			options[0] = new DialRange (10, 20, 60, 60, "pad0", 0.5, 0.0, 1.0, 0.0, BIDIRECTIONAL, "%1.2f");
39 			options[1] = new BWidgets::ValueWidget (0, 0, 0, 0, "widget", 0.0);
40 		}
41 		catch (std::bad_alloc& ba) {throw ba;}
42 
43 		options[0]->setCallbackFunction (BEvents::VALUE_CHANGED_EVENT, valueChangedCallback);
44 		((DialRange*)options[0])->range.setCallbackFunction (BEvents::VALUE_CHANGED_EVENT, rangeChangedCallback);
45 		options[1]->setCallbackFunction (BEvents::VALUE_CHANGED_EVENT, valueChangedCallback);
46 
47 		add (tapeSpeedLabel);
48 		add (*options[0]);
49 		add (*options[1]);
50 	}
51 
OptionTapeSpeed(const OptionTapeSpeed & that)52 	OptionTapeSpeed (const OptionTapeSpeed& that) : OptionWidget (that), tapeSpeedLabel (that.tapeSpeedLabel)
53 	{
54 		add (tapeSpeedLabel);
55 	}
56 
operator =(const OptionTapeSpeed & that)57 	OptionTapeSpeed& operator= (const OptionTapeSpeed& that)
58 	{
59 		release (&tapeSpeedLabel);
60 		OptionWidget::operator= (that);
61 		tapeSpeedLabel = that.tapeSpeedLabel;
62 		add (tapeSpeedLabel);
63 
64 		return *this;
65 	}
66 
clone() const67 	virtual Widget* clone () const override {return new OptionTapeSpeed (*this);}
68 
applyTheme(BStyles::Theme & theme)69 	virtual void applyTheme (BStyles::Theme& theme) override {applyTheme (theme, name_);}
70 
applyTheme(BStyles::Theme & theme,const std::string & name)71 	virtual void applyTheme (BStyles::Theme& theme, const std::string& name) override
72 	{
73 		OptionWidget::applyTheme (theme, name);
74 		tapeSpeedLabel.applyTheme (theme);
75 	}
76 
valueChangedCallback(BEvents::Event * event)77 	static void valueChangedCallback(BEvents::Event* event)
78 	{
79 		if (!event) return;
80 		BWidgets::Widget* widget = event->getWidget ();
81 		if (!widget) return;
82 		OptionWidget* p = (OptionWidget*) widget->getParent();
83 		if (!p) return;
84 		BOopsGUI* ui = (BOopsGUI*) widget->getMainWindow();
85 		if (!ui) return;
86 
87 		// options[1] changed ? Send to range
88 		if (widget == p->getWidget(1)) ((DialRange*)p->getWidget(0))->range.setValue (((BWidgets::ValueWidget*)widget)->getValue());
89 
90 		// Forward all changed options to ui
91 		ui->optionChangedCallback (event);
92 	}
93 
rangeChangedCallback(BEvents::Event * event)94 	static void rangeChangedCallback(BEvents::Event* event)
95 	{
96 		if (!event) return;
97 		BWidgets::Widget* widget = event->getWidget ();
98 		if (!widget) return;
99 		DialRange* p = (DialRange*) widget->getParent();
100 		if (!p) return;
101 		OptionWidget* pp = (OptionWidget*) p->getParent();
102 		if (!pp) return;
103 
104 		// Send changed range to options[1]
105 		if ((p == (DialRange*)pp->getWidget(0)) && (widget == (BWidgets::Widget*)&p->range))
106 		{
107 			p->update();
108 			((BWidgets::ValueWidget*)pp->getWidget(1))->setValue (p->range.getValue ());
109 		}
110 	}
111 
112 protected:
113 	BWidgets::Label tapeSpeedLabel;
114 
115 
116 };
117 
118 #endif /* OPTIONTAPESPEED_HPP_ */
119