-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.cpp
More file actions
393 lines (319 loc) · 11.7 KB
/
main.cpp
File metadata and controls
393 lines (319 loc) · 11.7 KB
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
/*
==============================================================================
This file is part of the YUP library.
Copyright (c) 2024 - kunitoki@gmail.com
YUP is an open source library subject to open-source licensing.
The code included in this file is provided under the terms of the ISC license
http://www.isc.org/downloads/software-support-policy/isc-license. Permission
to use, copy, modify, and/or distribute this software for any purpose with or
without fee is hereby granted provided that the above copyright notice and
this permission notice appear in all copies.
YUP IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#include <yup_core/yup_core.h>
#include <yup_audio_devices/yup_audio_devices.h>
#include <yup_events/yup_events.h>
#include <yup_graphics/yup_graphics.h>
#include <yup_gui/yup_gui.h>
#include <yup_audio_gui/yup_audio_gui.h>
#if YUP_MODULE_AVAILABLE_yup_python
#include <yup_python/yup_python.h>
#endif
#include <memory>
#include <cmath> // For sine wave generation
#if YUP_ANDROID
#include <BinaryData.h>
#endif
#include "examples/Artboard.h"
#include "examples/Audio.h"
#include "examples/AudioFileDemo.h"
#include "examples/ColorLab.h"
#include "examples/ConvolutionDemo.h"
#include "examples/CrossoverDemo.h"
#include "examples/FileChooser.h"
#include "examples/FilterDemo.h"
#include "examples/LayoutFonts.h"
#include "examples/OpaqueDemo.h"
#include "examples/Paths.h"
#include "examples/PopupMenu.h"
#include "examples/ScrollBarDemo.h"
#include "examples/SliderDemo.h"
#include "examples/SpectrumAnalyzer.h"
#include "examples/Svg.h"
#include "examples/TextEditor.h"
#include "examples/VariableFonts.h"
#include "examples/Widgets.h"
#include "examples/PaintProfilerDemo.h"
#if YUP_MODULE_AVAILABLE_yup_python
#include "examples/Python.h"
#endif
//==============================================================================
class DemoListModel : public yup::ListBoxModel
{
public:
DemoListModel (yup::Array<yup::String> names)
: demoNames (std::move (names))
{
}
int getNumRows() override
{
return demoNames.size();
}
yup::String getRowText (int rowIndex) override
{
if (rowIndex >= 0 && rowIndex < demoNames.size())
return demoNames[rowIndex];
return {};
}
void selectedRowsChanged (const yup::Array<int>& selectedRows) override
{
if (onSelectionChanged && ! selectedRows.isEmpty())
onSelectionChanged (selectedRows[0]);
}
std::function<void (int)> onSelectionChanged;
private:
yup::Array<yup::String> demoNames;
};
//==============================================================================
class CustomWindow
: public yup::DocumentWindow
, public yup::Timer
{
public:
CustomWindow()
: yup::DocumentWindow (yup::ComponentNative::Options()
.withAllowedHighDensityDisplay (true),
yup::Color (0xff404040))
{
setTitle ("main");
#if YUP_WASM
auto baseFilePath = yup::File ("/data");
#else
auto baseFilePath = yup::File (__FILE__).getParentDirectory().getSiblingFile ("data");
#endif
/*
// Load an image
{
yup::MemoryBlock mb;
auto imageFile = baseFilePath.getChildFile ("logo.png");
if (imageFile.loadFileAsData (mb))
{
auto loadedImage = yup::Image::loadFromData (mb.asBytes());
if (loadedImage.wasOk())
image = std::move (loadedImage.getReference());
}
else
{
yup::Logger::outputDebugString ("Unable to load requested image");
}
}
*/
#if YUP_WASM
yup::File dataPath = yup::File ("/data");
#else
yup::File dataPath = yup::File (__FILE__).getParentDirectory().getSiblingFile ("data");
#endif
int counter = 0;
registerDemo<AudioExample> ("Audio", counter++);
registerDemo<AudioFileDemo> ("Audio File", counter++);
registerDemo<ColorLabDemo> ("Color Lab", counter++);
registerDemo<ConvolutionDemo> ("Convolution Demo", counter++);
registerDemo<CrossoverDemo> ("Crossover Demo", counter++);
registerDemo<FileChooserDemo> ("File Chooser", counter++);
registerDemo<FilterDemo> ("Filter Demo", counter++);
registerDemo<LayoutFontsExample> ("Layout Fonts", counter++);
registerDemo<OpaqueDemo> ("Opaque Demo", counter++);
registerDemo<PathsExample> ("Paths", counter++);
registerDemo<PopupMenuDemo> ("Popup Menu", counter++);
registerDemo<ScrollBarDemo> ("ScrollBar", counter++);
registerDemo<SliderDemo> ("Sliders", counter++);
registerDemo<SpectrumAnalyzerDemo> ("FFT Analyzer", counter++);
registerDemo<TextEditorDemo> ("Text Editor", counter++);
registerDemo<VariableFontsExample> ("Variable Fonts", counter++);
registerDemo<WidgetsDemo> ("Widgets", counter++);
registerDemo<ArtboardDemo> ("Artboard", counter++, [] (auto& artboard)
{
jassert (artboard.loadArtboard());
});
registerDemo<SvgDemo> ("SVG", counter++);
registerDemo<PaintProfilerDemo> ("Paint Profiler", counter++);
#if YUP_MODULE_AVAILABLE_yup_python
registerDemo<PythonDemo> ("Python", counter++);
#endif
// Create the ListBox with the demo names
listModel = std::make_unique<DemoListModel> (demoNames);
listModel->onSelectionChanged = [this] (int index)
{
selectComponent (index);
};
listBox = std::make_unique<yup::ListBox>();
listBox->setModel (listModel.get());
listBox->setRowHeight (30);
listBox->setRowWidth (200);
listBox->selectRow (0, false, yup::dontSendNotification);
addAndMakeVisible (listBox.get());
selectComponent (0);
startTimerHz (10);
}
~CustomWindow() override
{
}
void resized() override
{
constexpr auto margin = 5;
constexpr auto listBoxWidth = 200;
constexpr auto listBoxHeight = 40;
auto bounds = getLocalBounds().reduced (margin);
auto width = bounds.getWidth();
auto height = bounds.getHeight();
// Landscape orientation (width > height): vertical ListBox on the left
if (width > height)
{
listBox->setOrientation (yup::ListBox::Orientation::vertical);
listBox->setRowHeight (30);
listBox->setVerticalScrollBarVisibility (yup::ScrollBar::VisibilityMode::autoHide);
listBox->setHorizontalScrollBarVisibility (yup::ScrollBar::VisibilityMode::alwaysHidden);
auto listBoxBounds = bounds.removeFromLeft (listBoxWidth);
listBox->setBounds (listBoxBounds);
// Add margin between ListBox and demo components
bounds.removeFromLeft (margin);
}
// Portrait orientation (width <= height): horizontal ListBox on top
else
{
listBox->setOrientation (yup::ListBox::Orientation::horizontal);
listBox->setRowWidth (80);
listBox->setRowHeight (listBoxHeight);
listBox->setVerticalScrollBarVisibility (yup::ScrollBar::VisibilityMode::alwaysHidden);
listBox->setHorizontalScrollBarVisibility (yup::ScrollBar::VisibilityMode::autoHide);
auto listBoxBounds = bounds.removeFromTop (listBoxHeight);
listBox->setBounds (listBoxBounds);
// Add margin between ListBox and demo components
bounds.removeFromTop (margin);
}
// Demo components take the remaining space
for (auto& component : components)
component->setBounds (bounds);
}
void paint (yup::Graphics& g) override
{
yup::DocumentWindow::paint (g);
//g.drawImageAt (image, getLocalBounds().getCenter());
}
/*
void paintOverChildren (yup::Graphics& g) override
{
if (! image.isValid())
return;
g.setBlendMode (yup::BlendMode::ColorDodge);
g.setOpacity (1.0f);
g.drawImageAt (image, getLocalBounds().getCenter());
}
*/
void keyDown (const yup::KeyPress& keys, const yup::Point<float>& position) override
{
switch (keys.getKey())
{
case yup::KeyPress::escapeKey:
userTriedToCloseWindow();
break;
case yup::KeyPress::textAKey:
getNativeComponent()->enableAtomicMode (! getNativeComponent()->isAtomicModeEnabled());
break;
case yup::KeyPress::textWKey:
getNativeComponent()->enableWireframe (! getNativeComponent()->isWireframeEnabled());
break;
case yup::KeyPress::textZKey:
setFullScreen (! isFullScreen());
break;
}
}
void timerCallback() override
{
updateWindowTitle();
}
void userTriedToCloseWindow() override
{
yup::YUPApplication::getInstance()->systemRequestedQuit();
}
void selectComponent (int index)
{
for (auto& component : components)
component->setVisible (false);
components[index]->setVisible (true);
}
private:
template <class Demo>
void registerDemo (const yup::String& name, int counter, std::function<void (Demo&)> setup = nullptr)
{
demoNames.add (name);
auto demo = std::make_unique<Demo>();
auto& demoInstance = *demo.get();
components.add (std::move (demo));
addChildComponent (components.getLast());
if (setup)
setup (demoInstance);
}
void updateWindowTitle()
{
yup::String title;
auto currentFps = getNativeComponent()->getCurrentFrameRate();
title << "[" << yup::String (currentFps, 1) << " FPS]";
title << " | YUP On Rive Renderer";
if (getNativeComponent()->isAtomicModeEnabled())
title << " (atomic)";
auto [width, height] = getNativeComponent()->getContentSize();
title << " | " << width << " x " << height;
setTitle (title);
}
yup::Array<yup::String> demoNames;
std::unique_ptr<DemoListModel> listModel;
std::unique_ptr<yup::ListBox> listBox;
yup::OwnedArray<yup::Component> components;
yup::Font font;
yup::Image image;
};
//==============================================================================
struct Application : yup::YUPApplication
{
Application() = default;
yup::String getApplicationName() override
{
return "yup! graphics";
}
yup::String getApplicationVersion() override
{
return "1.0";
}
void initialise (const yup::String& commandLineParameters) override
{
YUP_PROFILE_START();
yup::Logger::outputDebugString ("Starting app " + commandLineParameters);
yup::MessageManager::callAsync ([this]
{
yup::Process::makeForegroundProcess();
window = std::make_unique<CustomWindow>();
#if YUP_IOS
window->centreWithSize ({ 320, 480 });
#elif YUP_ANDROID
window->centreWithSize ({ 1080, 2400 });
// window->setFullScreen(true);
#else
window->centreWithSize ({ 1024, 768 });
#endif
window->setVisible (true);
});
}
void shutdown() override
{
yup::Logger::outputDebugString ("Shutting down");
window.reset();
YUP_PROFILE_STOP();
}
private:
std::unique_ptr<CustomWindow> window;
};
START_YUP_APPLICATION (Application)