forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
714 lines (614 loc) · 17.6 KB
/
window.cpp
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#include "window.hpp"
#include "app.hpp"
#include "asset.hpp"
#include "gamepad.hpp"
#include "keyboard.hpp"
#include "util/color.hpp"
#include <map>
#include <queue>
#include <thread>
#include "osdialog.h"
#define NANOVG_GL2_IMPLEMENTATION 1
// #define NANOVG_GL3_IMPLEMENTATION 1
// #define NANOVG_GLES2_IMPLEMENTATION 1
// #define NANOVG_GLES3_IMPLEMENTATION 1
#include "nanovg_gl.h"
// Hack to get framebuffer objects working on OpenGL 2 (we blindly assume the extension is supported)
#define NANOVG_FBO_VALID 1
#include "nanovg_gl_utils.h"
#define BLENDISH_IMPLEMENTATION
#include "blendish.h"
#define NANOSVG_IMPLEMENTATION
#define NANOSVG_ALL_COLOR_KEYWORDS
#include "nanosvg.h"
#ifdef ARCH_MAC
// For CGAssociateMouseAndMouseCursorPosition
#include <ApplicationServices/ApplicationServices.h>
#endif
namespace rack {
GLFWwindow *gWindow = NULL;
NVGcontext *gVg = NULL;
NVGcontext *gFramebufferVg = NULL;
std::shared_ptr<Font> gGuiFont;
float gPixelRatio = 1.0;
float gWindowRatio = 1.0;
bool gAllowCursorLock = true;
int gGuiFrame;
Vec gMousePos;
std::string lastWindowTitle;
void windowSizeCallback(GLFWwindow* window, int width, int height) {
}
void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
#ifdef ARCH_MAC
// Ctrl-left click --> right click
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (glfwGetKey(gWindow, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS) {
button = GLFW_MOUSE_BUTTON_RIGHT;
}
}
#endif
if (action == GLFW_PRESS) {
gTempWidget = NULL;
// onMouseDown
{
EventMouseDown e;
e.pos = gMousePos;
e.button = button;
gScene->onMouseDown(e);
gTempWidget = e.target;
}
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (gTempWidget) {
// onDragStart
EventDragStart e;
gTempWidget->onDragStart(e);
}
gDraggedWidget = gTempWidget;
if (gTempWidget != gFocusedWidget) {
if (gFocusedWidget) {
// onDefocus
EventDefocus e;
gFocusedWidget->onDefocus(e);
}
gFocusedWidget = NULL;
if (gTempWidget) {
// onFocus
EventFocus e;
gTempWidget->onFocus(e);
if (e.consumed) {
gFocusedWidget = gTempWidget;
}
}
}
}
gTempWidget = NULL;
}
else if (action == GLFW_RELEASE) {
// onMouseUp
gTempWidget = NULL;
{
EventMouseUp e;
e.pos = gMousePos;
e.button = button;
gScene->onMouseUp(e);
gTempWidget = e.target;
}
if (button == GLFW_MOUSE_BUTTON_LEFT) {
if (gDraggedWidget) {
// onDragDrop
EventDragDrop e;
e.origin = gDraggedWidget;
gTempWidget->onDragDrop(e);
}
// gDraggedWidget might have been set to null in the last event, recheck here
if (gDraggedWidget) {
// onDragEnd
EventDragEnd e;
gDraggedWidget->onDragEnd(e);
}
gDraggedWidget = NULL;
gDragHoveredWidget = NULL;
}
gTempWidget = NULL;
}
}
struct MouseButtonArguments {
GLFWwindow *window;
int button;
int action;
int mods;
};
static std::queue<MouseButtonArguments> mouseButtonQueue;
void mouseButtonStickyPop() {
if (!mouseButtonQueue.empty()) {
MouseButtonArguments args = mouseButtonQueue.front();
mouseButtonQueue.pop();
mouseButtonCallback(args.window, args.button, args.action, args.mods);
}
}
void mouseButtonStickyCallback(GLFWwindow *window, int button, int action, int mods) {
// Defer multiple clicks per frame to future frames
MouseButtonArguments args = {window, button, action, mods};
mouseButtonQueue.push(args);
}
void cursorPosCallback(GLFWwindow* window, double xpos, double ypos) {
Vec mousePos = Vec(xpos, ypos).div(gPixelRatio / gWindowRatio).round();
Vec mouseRel = mousePos.minus(gMousePos);
int cursorMode = glfwGetInputMode(gWindow, GLFW_CURSOR);
(void) cursorMode;
#ifdef ARCH_MAC
// Workaround for Mac. We can't use GLFW_CURSOR_DISABLED because it's buggy, so implement it on our own.
// This is not an ideal implementation. For example, if the user drags off the screen, the new mouse position will be clamped.
if (cursorMode == GLFW_CURSOR_HIDDEN) {
// CGSetLocalEventsSuppressionInterval(0.0);
glfwSetCursorPos(gWindow, gMousePos.x, gMousePos.y);
CGAssociateMouseAndMouseCursorPosition(true);
mousePos = gMousePos;
}
// Because sometimes the cursor turns into an arrow when its position is on the boundary of the window
glfwSetCursor(gWindow, NULL);
#endif
gMousePos = mousePos;
gTempWidget = NULL;
// onMouseMove
{
EventMouseMove e;
e.pos = mousePos;
e.mouseRel = mouseRel;
gScene->onMouseMove(e);
gTempWidget = e.target;
}
if (gDraggedWidget) {
// onDragMove
EventDragMove e;
e.mouseRel = mouseRel;
gDraggedWidget->onDragMove(e);
if (gTempWidget != gDragHoveredWidget) {
if (gDragHoveredWidget) {
EventDragEnter e;
e.origin = gDraggedWidget;
gDragHoveredWidget->onDragLeave(e);
}
gDragHoveredWidget = gTempWidget;
if (gDragHoveredWidget) {
EventDragEnter e;
e.origin = gDraggedWidget;
gDragHoveredWidget->onDragEnter(e);
}
}
}
else {
if (gTempWidget != gHoveredWidget) {
if (gHoveredWidget) {
// onMouseLeave
EventMouseLeave e;
gHoveredWidget->onMouseLeave(e);
}
gHoveredWidget = gTempWidget;
if (gHoveredWidget) {
// onMouseEnter
EventMouseEnter e;
gHoveredWidget->onMouseEnter(e);
}
}
}
gTempWidget = NULL;
if (glfwGetMouseButton(gWindow, GLFW_MOUSE_BUTTON_MIDDLE) == GLFW_PRESS) {
// TODO
// Define a new global called gScrollWidget, which remembers the widget where middle-click was first pressed
EventScroll e;
e.pos = mousePos;
e.scrollRel = mouseRel;
gScene->onScroll(e);
}
}
void cursorEnterCallback(GLFWwindow* window, int entered) {
if (!entered) {
if (gHoveredWidget) {
// onMouseLeave
EventMouseLeave e;
gHoveredWidget->onMouseLeave(e);
}
gHoveredWidget = NULL;
}
}
void scrollCallback(GLFWwindow *window, double x, double y) {
Vec scrollRel = Vec(x, y);
#if ARCH_LIN || ARCH_WIN
if (windowIsShiftPressed())
scrollRel = Vec(y, x);
#endif
// onScroll
EventScroll e;
e.pos = gMousePos;
e.scrollRel = scrollRel.mult(50.0);
gScene->onScroll(e);
}
void charCallback(GLFWwindow *window, unsigned int codepoint) {
if (gFocusedWidget) {
// onText
EventText e;
e.codepoint = codepoint;
gFocusedWidget->onText(e);
}
}
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
if (gFocusedWidget) {
// onKey
EventKey e;
e.key = key;
gFocusedWidget->onKey(e);
if (e.consumed)
return;
}
// onHoverKey
EventHoverKey e;
e.pos = gMousePos;
e.key = key;
gScene->onHoverKey(e);
}
// Keyboard MIDI driver
if (!(mods & (GLFW_MOD_SHIFT | GLFW_MOD_CONTROL | GLFW_MOD_ALT | GLFW_MOD_SUPER))) {
if (action == GLFW_PRESS) {
keyboardPress(key);
}
else if (action == GLFW_RELEASE) {
keyboardRelease(key);
}
}
}
void dropCallback(GLFWwindow *window, int count, const char **paths) {
// onPathDrop
EventPathDrop e;
e.pos = gMousePos;
for (int i = 0; i < count; i++) {
e.paths.push_back(paths[i]);
}
gScene->onPathDrop(e);
}
void errorCallback(int error, const char *description) {
warn("GLFW error %d: %s", error, description);
}
void renderGui() {
int width, height;
glfwGetFramebufferSize(gWindow, &width, &height);
// Update and render
nvgBeginFrame(gVg, width, height, gPixelRatio);
nvgReset(gVg);
nvgScale(gVg, gPixelRatio, gPixelRatio);
gScene->draw(gVg);
glViewport(0, 0, width, height);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
nvgEndFrame(gVg);
glfwSwapBuffers(gWindow);
}
void windowInit() {
int err;
// Set up GLFW
glfwSetErrorCallback(errorCallback);
err = glfwInit();
if (err != GLFW_TRUE) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not initialize GLFW.");
exit(1);
}
#if defined NANOVG_GL2
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
#elif defined NANOVG_GL3
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#endif
glfwWindowHint(GLFW_MAXIMIZED, GLFW_TRUE);
glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
lastWindowTitle = "";
gWindow = glfwCreateWindow(640, 480, lastWindowTitle.c_str(), NULL, NULL);
if (!gWindow) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Cannot open window with OpenGL 2.0 renderer. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed.");
exit(1);
}
glfwMakeContextCurrent(gWindow);
glfwSwapInterval(1);
glfwSetInputMode(gWindow, GLFW_LOCK_KEY_MODS, 1);
glfwSetWindowSizeCallback(gWindow, windowSizeCallback);
glfwSetMouseButtonCallback(gWindow, mouseButtonStickyCallback);
// Call this ourselves, but on every frame instead of only when the mouse moves
// glfwSetCursorPosCallback(gWindow, cursorPosCallback);
glfwSetCursorEnterCallback(gWindow, cursorEnterCallback);
glfwSetScrollCallback(gWindow, scrollCallback);
glfwSetCharCallback(gWindow, charCallback);
glfwSetKeyCallback(gWindow, keyCallback);
glfwSetDropCallback(gWindow, dropCallback);
// Set up GLEW
glewExperimental = GL_TRUE;
err = glewInit();
if (err != GLEW_OK) {
osdialog_message(OSDIALOG_ERROR, OSDIALOG_OK, "Could not initialize GLEW. Does your graphics card support OpenGL 2.0 or greater? If so, make sure you have the latest graphics drivers installed.");
exit(1);
}
// GLEW generates GL error because it calls glGetString(GL_EXTENSIONS), we'll consume it here.
glGetError();
glfwSetWindowSizeLimits(gWindow, 640, 480, GLFW_DONT_CARE, GLFW_DONT_CARE);
// Set up NanoVG
int nvgFlags = NVG_ANTIALIAS;
#if defined NANOVG_GL2
gVg = nvgCreateGL2(nvgFlags);
#elif defined NANOVG_GL3
gVg = nvgCreateGL3(nvgFlags);
#elif defined NANOVG_GLES2
gVg = nvgCreateGLES2(nvgFlags);
#endif
assert(gVg);
#if defined NANOVG_GL2
gFramebufferVg = nvgCreateGL2(nvgFlags);
#elif defined NANOVG_GL3
gFramebufferVg = nvgCreateGL3(nvgFlags);
#elif defined NANOVG_GLES2
gFramebufferVg = nvgCreateGLES2(nvgFlags);
#endif
assert(gFramebufferVg);
// Set up Blendish
gGuiFont = Font::load(assetGlobal("res/fonts/DejaVuSans.ttf"));
bndSetFont(gGuiFont->handle);
windowSetTheme(nvgRGB(0x33, 0x33, 0x33), nvgRGB(0xf0, 0xf0, 0xf0));
}
void windowDestroy() {
gGuiFont.reset();
#if defined NANOVG_GL2
nvgDeleteGL2(gVg);
#elif defined NANOVG_GL3
nvgDeleteGL3(gVg);
#elif defined NANOVG_GLES2
nvgDeleteGLES2(gVg);
#endif
#if defined NANOVG_GL2
nvgDeleteGL2(gFramebufferVg);
#elif defined NANOVG_GL3
nvgDeleteGL3(gFramebufferVg);
#elif defined NANOVG_GLES2
nvgDeleteGLES2(gFramebufferVg);
#endif
glfwDestroyWindow(gWindow);
glfwTerminate();
}
void windowRun() {
assert(gWindow);
gGuiFrame = 0;
while(!glfwWindowShouldClose(gWindow)) {
double startTime = glfwGetTime();
gGuiFrame++;
// Poll events
glfwPollEvents();
{
double xpos, ypos;
glfwGetCursorPos(gWindow, &xpos, &ypos);
cursorPosCallback(gWindow, xpos, ypos);
}
mouseButtonStickyPop();
gamepadStep();
// Set window title
std::string windowTitle;
windowTitle = gApplicationName;
windowTitle += " ";
windowTitle += gApplicationVersion;
if (!gRackWidget->lastPath.empty()) {
windowTitle += " - ";
windowTitle += stringFilename(gRackWidget->lastPath);
}
if (windowTitle != lastWindowTitle) {
glfwSetWindowTitle(gWindow, windowTitle.c_str());
lastWindowTitle = windowTitle;
}
// Get desired scaling
float pixelRatio;
glfwGetWindowContentScale(gWindow, &pixelRatio, NULL);
pixelRatio = roundf(pixelRatio);
if (pixelRatio != gPixelRatio) {
EventZoom eZoom;
gScene->onZoom(eZoom);
gPixelRatio = pixelRatio;
}
// Get framebuffer/window ratio
int width, height;
glfwGetFramebufferSize(gWindow, &width, &height);
int windowWidth, windowHeight;
glfwGetWindowSize(gWindow, &windowWidth, &windowHeight);
gWindowRatio = (float)width / windowWidth;
gScene->box.size = Vec(width, height).div(gPixelRatio);
// Step scene
gScene->step();
// Render
bool visible = glfwGetWindowAttrib(gWindow, GLFW_VISIBLE) && !glfwGetWindowAttrib(gWindow, GLFW_ICONIFIED);
if (visible) {
renderGui();
}
// Limit framerate manually if vsync isn't working
double endTime = glfwGetTime();
double frameTime = endTime - startTime;
double minTime = 1.0 / 90.0;
if (frameTime < minTime) {
std::this_thread::sleep_for(std::chrono::duration<double>(minTime - frameTime));
}
endTime = glfwGetTime();
// info("%lf fps", 1.0 / (endTime - startTime));
}
}
void windowClose() {
glfwSetWindowShouldClose(gWindow, GLFW_TRUE);
}
void windowCursorLock() {
if (gAllowCursorLock) {
#ifdef ARCH_MAC
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
#else
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
#endif
}
}
void windowCursorUnlock() {
if (gAllowCursorLock) {
glfwSetInputMode(gWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
bool windowIsModPressed() {
#ifdef ARCH_MAC
return glfwGetKey(gWindow, GLFW_KEY_LEFT_SUPER) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_SUPER) == GLFW_PRESS;
#else
return glfwGetKey(gWindow, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_CONTROL) == GLFW_PRESS;
#endif
}
bool windowIsShiftPressed() {
return glfwGetKey(gWindow, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS || glfwGetKey(gWindow, GLFW_KEY_RIGHT_SHIFT) == GLFW_PRESS;
}
Vec windowGetWindowSize() {
int width, height;
glfwGetWindowSize(gWindow, &width, &height);
return Vec(width, height);
}
void windowSetWindowSize(Vec size) {
int width = size.x;
int height = size.y;
glfwSetWindowSize(gWindow, width, height);
}
Vec windowGetWindowPos() {
int x, y;
glfwGetWindowPos(gWindow, &x, &y);
return Vec(x, y);
}
void windowSetWindowPos(Vec pos) {
int x = pos.x;
int y = pos.y;
glfwSetWindowPos(gWindow, x, y);
}
bool windowIsMaximized() {
return glfwGetWindowAttrib(gWindow, GLFW_MAXIMIZED);
}
void windowSetTheme(NVGcolor bg, NVGcolor fg) {
// Assume dark background and light foreground
BNDwidgetTheme w;
w.outlineColor = bg;
w.itemColor = fg;
w.innerColor = bg;
w.innerSelectedColor = colorPlus(bg, nvgRGB(0x30, 0x30, 0x30));
w.textColor = fg;
w.textSelectedColor = fg;
w.shadeTop = 0;
w.shadeDown = 0;
BNDtheme t;
t.backgroundColor = colorPlus(bg, nvgRGB(0x30, 0x30, 0x30));
t.regularTheme = w;
t.toolTheme = w;
t.radioTheme = w;
t.textFieldTheme = w;
t.optionTheme = w;
t.choiceTheme = w;
t.numberFieldTheme = w;
t.sliderTheme = w;
t.scrollBarTheme = w;
t.tooltipTheme = w;
t.menuTheme = w;
t.menuItemTheme = w;
t.sliderTheme.itemColor = bg;
t.sliderTheme.innerColor = colorPlus(bg, nvgRGB(0x50, 0x50, 0x50));
t.sliderTheme.innerSelectedColor = colorPlus(bg, nvgRGB(0x60, 0x60, 0x60));
t.textFieldTheme = t.sliderTheme;
t.textFieldTheme.textColor = colorMinus(bg, nvgRGB(0x20, 0x20, 0x20));
t.textFieldTheme.textSelectedColor = t.textFieldTheme.textColor;
t.scrollBarTheme.itemColor = colorPlus(bg, nvgRGB(0x50, 0x50, 0x50));
t.scrollBarTheme.innerColor = bg;
t.menuTheme.innerColor = colorMinus(bg, nvgRGB(0x10, 0x10, 0x10));
t.menuTheme.textColor = colorMinus(fg, nvgRGB(0x50, 0x50, 0x50));
t.menuTheme.textSelectedColor = t.menuTheme.textColor;
bndSetTheme(t);
}
static int windowX = 0;
static int windowY = 0;
static int windowWidth = 0;
static int windowHeight = 0;
void windowSetFullScreen(bool fullScreen) {
if (windowGetFullScreen()) {
glfwSetWindowMonitor(gWindow, NULL, windowX, windowY, windowWidth, windowHeight, GLFW_DONT_CARE);
}
else {
glfwGetWindowPos(gWindow, &windowX, &windowY);
glfwGetWindowSize(gWindow, &windowWidth, &windowHeight);
GLFWmonitor *monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
glfwSetWindowMonitor(gWindow, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
}
}
bool windowGetFullScreen() {
GLFWmonitor *monitor = glfwGetWindowMonitor(gWindow);
return monitor != NULL;
}
////////////////////
// resources
////////////////////
Font::Font(const std::string &filename) {
handle = nvgCreateFont(gVg, filename.c_str(), filename.c_str());
if (handle >= 0) {
info("Loaded font %s", filename.c_str());
}
else {
warn("Failed to load font %s", filename.c_str());
}
}
Font::~Font() {
// There is no NanoVG deleteFont() function yet, so do nothing
}
std::shared_ptr<Font> Font::load(const std::string &filename) {
static std::map<std::string, std::weak_ptr<Font>> cache;
auto sp = cache[filename].lock();
if (!sp)
cache[filename] = sp = std::make_shared<Font>(filename);
return sp;
}
////////////////////
// Image
////////////////////
Image::Image(const std::string &filename) {
handle = nvgCreateImage(gVg, filename.c_str(), NVG_IMAGE_REPEATX | NVG_IMAGE_REPEATY);
if (handle > 0) {
info("Loaded image %s", filename.c_str());
}
else {
warn("Failed to load image %s", filename.c_str());
}
}
Image::~Image() {
// TODO What if handle is invalid?
nvgDeleteImage(gVg, handle);
}
std::shared_ptr<Image> Image::load(const std::string &filename) {
static std::map<std::string, std::weak_ptr<Image>> cache;
auto sp = cache[filename].lock();
if (!sp)
cache[filename] = sp = std::make_shared<Image>(filename);
return sp;
}
////////////////////
// SVG
////////////////////
SVG::SVG(const std::string &filename) {
handle = nsvgParseFromFile(filename.c_str(), "px", SVG_DPI);
if (handle) {
info("Loaded SVG %s", filename.c_str());
}
else {
warn("Failed to load SVG %s", filename.c_str());
}
}
SVG::~SVG() {
nsvgDelete(handle);
}
std::shared_ptr<SVG> SVG::load(const std::string &filename) {
static std::map<std::string, std::weak_ptr<SVG>> cache;
auto sp = cache[filename].lock();
if (!sp)
cache[filename] = sp = std::make_shared<SVG>(filename);
return sp;
}
} // namespace rack