forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.hpp
401 lines (333 loc) · 9.85 KB
/
app.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#pragma once
#include <vector>
#include <jansson.h>
#include "widgets.hpp"
namespace rack {
struct Model;
struct Module;
struct Wire;
struct RackWidget;
struct ParamWidget;
struct Port;
struct SVGPanel;
////////////////////
// module
////////////////////
// A 1U module should be 15x380. Thus the width of a module should be a factor of 15.
#define RACK_GRID_WIDTH 15
#define RACK_GRID_HEIGHT 380
static const Vec RACK_GRID_SIZE = Vec(15, 380);
struct ModuleWidget : OpaqueWidget {
Model *model = NULL;
/** Owns the module pointer */
Module *module = NULL;
SVGPanel *panel = NULL;
std::vector<Port*> inputs;
std::vector<Port*> outputs;
std::vector<ParamWidget*> params;
~ModuleWidget();
void setModule(Module *module);
/** Convenience functions for adding special widgets (calls addChild()) */
void addInput(Port *input);
void addOutput(Port *output);
void addParam(ParamWidget *param);
void setPanel(std::shared_ptr<SVG> svg);
virtual json_t *toJson();
virtual void fromJson(json_t *rootJ);
/** Disconnects cables from all ports
Called when the user clicks Disconnect Cables in the context menu.
*/
virtual void disconnect();
/** Resets the parameters of the module and calls the Module's randomize().
Called when the user clicks Initialize in the context menu.
*/
virtual void reset();
/** Deprecated */
virtual void initialize() final {}
/** Randomizes the parameters of the module and calls the Module's randomize().
Called when the user clicks Randomize in the context menu.
*/
virtual void randomize();
virtual Menu *createContextMenu();
void draw(NVGcontext *vg) override;
Vec dragPos;
void onMouseDown(EventMouseDown &e) override;
void onMouseMove(EventMouseMove &e) override;
void onHoverKey(EventHoverKey &e) override;
void onDragStart(EventDragStart &e) override;
void onDragEnd(EventDragEnd &e) override;
void onDragMove(EventDragMove &e) override;
};
struct ValueLight;
struct WireWidget : OpaqueWidget {
Port *outputPort = NULL;
Port *inputPort = NULL;
Port *hoveredOutputPort = NULL;
Port *hoveredInputPort = NULL;
ValueLight *inputLight;
ValueLight *outputLight;
Wire *wire = NULL;
NVGcolor color;
WireWidget();
~WireWidget();
/** Synchronizes the plugged state of the widget to the owned wire */
void updateWire();
Vec getOutputPos();
Vec getInputPos();
void draw(NVGcontext *vg) override;
void drawPlugs(NVGcontext *vg);
};
struct WireContainer : TransparentWidget {
WireWidget *activeWire = NULL;
/** Takes ownership of `w` and adds it as a child if it isn't already */
void setActiveWire(WireWidget *w);
/** "Drops" the wire onto the port, making an engine connection if successful */
void commitActiveWire();
void removeTopWire(Port *port);
void removeAllWires(Port *port);
/** Returns the most recently added wire connected to the given Port, i.e. the top of the stack */
WireWidget *getTopWire(Port *port);
void draw(NVGcontext *vg) override;
};
struct RackWidget : OpaqueWidget {
FramebufferWidget *rails;
// Only put ModuleWidgets in here
Widget *moduleContainer;
// Only put WireWidgets in here
WireContainer *wireContainer;
std::string lastPath;
Vec lastMousePos;
RackWidget();
~RackWidget();
/** Completely clear the rack's modules and wires */
void clear();
/** Clears the rack and loads the template patch */
void reset();
void openDialog();
void saveDialog();
void saveAsDialog();
void savePatch(std::string filename);
void loadPatch(std::string filename);
json_t *toJson();
void fromJson(json_t *rootJ);
void addModule(ModuleWidget *m);
/** Transfers ownership to the caller so they must `delete` it if that is the intension */
void deleteModule(ModuleWidget *m);
void cloneModule(ModuleWidget *m);
/** Sets a module's box if non-colliding. Returns true if set */
bool requestModuleBox(ModuleWidget *m, Rect box);
/** Moves a module to the closest non-colliding position */
bool requestModuleBoxNearest(ModuleWidget *m, Rect box);
void step() override;
void draw(NVGcontext *vg) override;
void onMouseMove(EventMouseMove &e) override;
void onMouseDown(EventMouseDown &e) override;
void onZoom(EventZoom &e) override;
};
struct RackRail : TransparentWidget {
void draw(NVGcontext *vg) override;
};
struct AddModuleWindow : Window {
Vec modulePos;
AddModuleWindow();
void step() override;
};
struct Panel : TransparentWidget {
NVGcolor backgroundColor;
std::shared_ptr<Image> backgroundImage;
void draw(NVGcontext *vg) override;
};
struct SVGPanel : FramebufferWidget {
void step() override;
void setBackground(std::shared_ptr<SVG> svg);
};
////////////////////
// params
////////////////////
struct CircularShadow : TransparentWidget {
float blur = 0.0;
void draw(NVGcontext *vg) override;
};
struct ParamWidget : OpaqueWidget, QuantityWidget {
Module *module = NULL;
int paramId;
/** Used to momentarily disable value randomization
To permanently disable or change randomization behavior, override the randomize() method instead of changing this.
*/
bool randomizable = true;
json_t *toJson();
void fromJson(json_t *rootJ);
virtual void randomize();
void onMouseDown(EventMouseDown &e) override;
void onChange(EventChange &e) override;
};
/** Implements vertical dragging behavior for ParamWidgets */
struct Knob : ParamWidget {
/** Snap to nearest integer while dragging */
bool snap = false;
float dragValue;
void onDragStart(EventDragStart &e) override;
void onDragMove(EventDragMove &e) override;
void onDragEnd(EventDragEnd &e) override;
/** Tell engine to smoothly vary this parameter */
void onChange(EventChange &e) override;
};
struct SpriteKnob : virtual Knob, SpriteWidget {
int minIndex, maxIndex, spriteCount;
void step() override;
};
/** A knob which rotates an SVG and caches it in a framebuffer */
struct SVGKnob : virtual Knob, FramebufferWidget {
/** Angles in radians */
float minAngle, maxAngle;
/** Not owned */
TransformWidget *tw;
SVGWidget *sw;
SVGKnob();
void setSVG(std::shared_ptr<SVG> svg);
void step() override;
void onChange(EventChange &e) override;
};
struct SVGSlider : Knob, FramebufferWidget {
/** Intermediate positions will be interpolated between these positions */
Vec minHandlePos, maxHandlePos;
/** Not owned */
SVGWidget *background;
SVGWidget *handle;
SVGSlider();
void step() override;
void onChange(EventChange &e) override;
};
struct Switch : ParamWidget {
};
struct SVGSwitch : virtual Switch, FramebufferWidget {
std::vector<std::shared_ptr<SVG>> frames;
/** Not owned */
SVGWidget *sw;
SVGSwitch();
/** Adds an SVG file to represent the next switch position */
void addFrame(std::shared_ptr<SVG> svg);
void step() override;
void onChange(EventChange &e) override;
};
/** A switch that cycles through each mechanical position */
struct ToggleSwitch : virtual Switch {
void onDragStart(EventDragStart &e) override {
// Cycle through values
// e.g. a range of [0.0, 3.0] would have modes 0, 1, 2, and 3.
if (value >= maxValue)
setValue(minValue);
else
setValue(value + 1.0);
}
};
/** A switch that is turned on when held */
struct MomentarySwitch : virtual Switch {
/** Don't randomize state */
void randomize() override {}
void onDragStart(EventDragStart &e) override {
setValue(maxValue);
}
void onDragEnd(EventDragEnd &e) override {
setValue(minValue);
}
};
////////////////////
// lights
////////////////////
struct LightWidget : TransparentWidget {
NVGcolor bgColor = nvgRGBf(0, 0, 0);
NVGcolor color = nvgRGBf(1, 1, 1);
void draw(NVGcontext *vg) override;
};
/** Mixes a list of colors based on a list of brightness values */
struct MultiLightWidget : LightWidget {
std::vector<NVGcolor> baseColors;
void addBaseColor(NVGcolor baseColor);
/** Sets the color to a linear combination of the baseColors with the given weights */
void setValues(const std::vector<float> &values);
};
/** A MultiLightWidget that points to a module's Light or a range of lights
Will access firstLightId, firstLightId + 1, etc. for each added color
*/
struct ModuleLightWidget : MultiLightWidget {
Module *module = NULL;
int firstLightId;
void step() override;
};
////////////////////
// ports
////////////////////
struct Port : OpaqueWidget {
enum PortType {
INPUT,
OUTPUT
};
Module *module = NULL;
PortType type = INPUT;
int portId;
MultiLightWidget *plugLight;
Port();
~Port();
void step() override;
void draw(NVGcontext *vg) override;
void onMouseDown(EventMouseDown &e) override;
void onDragStart(EventDragStart &e) override;
void onDragEnd(EventDragEnd &e) override;
void onDragDrop(EventDragDrop &e) override;
void onDragEnter(EventDragEnter &e) override;
void onDragLeave(EventDragEnter &e) override;
};
struct SVGPort : Port, FramebufferWidget {
SVGWidget *background;
SVGPort();
void draw(NVGcontext *vg) override;
};
/** If you don't add these to your ModuleWidget, they will fall out of the rack... */
struct SVGScrew : FramebufferWidget {
SVGWidget *sw;
SVGScrew();
};
////////////////////
// scene
////////////////////
struct Toolbar : OpaqueWidget {
Slider *wireOpacitySlider;
Slider *wireTensionSlider;
Slider *zoomSlider;
RadioButton *cpuUsageButton;
Toolbar();
void draw(NVGcontext *vg) override;
};
struct PluginManagerWidget : Widget {
Widget *loginWidget;
Widget *manageWidget;
Widget *downloadWidget;
PluginManagerWidget();
void step() override;
};
struct RackScrollWidget : ScrollWidget {
void step() override;
};
struct RackScene : Scene {
ScrollWidget *scrollWidget;
ZoomWidget *zoomWidget;
RackScene();
void step() override;
void draw(NVGcontext *vg) override;
void onHoverKey(EventHoverKey &e) override;
void onPathDrop(EventPathDrop &e) override;
};
////////////////////
// globals
////////////////////
extern std::string gApplicationName;
extern std::string gApplicationVersion;
extern std::string gApiHost;
// Easy access to "singleton" widgets
extern RackScene *gRackScene;
extern RackWidget *gRackWidget;
extern Toolbar *gToolbar;
void sceneInit();
void sceneDestroy();
} // namespace rack