-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSingularityController.cpp
More file actions
72 lines (62 loc) · 2 KB
/
Copy pathSingularityController.cpp
File metadata and controls
72 lines (62 loc) · 2 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
#include "SingularityController.h"
#include <iostream>
SingularityController::SingularityController(IParameterProvider ¶meterProvider, std::string_view resourcePath)
: parameterProvider_(parameterProvider)
{
renderer_ = IRenderer::createRenderer(resourcePath);
jsEngine_ = IJSEngine::createJSEngine(parameterProvider_);
#ifndef NDEBUG
fileWatcher_ = IFileWatcher::createFileWatcher(UI_DIR);
widgetsWatcher_ = IFileWatcher::createFileWatcher(SINGULARITY_WIDGETS_DIR);
fprintf(stderr, "[singularity] Watching: %s\n", UI_DIR);
#endif
}
void SingularityController::setLogger(IJSEngine::LogCallback cb)
{
jsEngine_->setLogger(std::move(cb));
}
void SingularityController::initialize()
{
#ifndef NDEBUG
fileWatcher_->setCallback([this](const std::string& filePath) {
std::cout << "File changed" << std::endl;
reloadPending_ = true;
});
widgetsWatcher_->setCallback([this](const std::string& filePath) {
std::cout << "Widget changed: " << filePath << std::endl;
reloadPending_ = true;
});
#endif
jsEngine_->load(UI_MAIN, renderer_.get());
}
void SingularityController::tick()
{
#ifndef NDEBUG
if (reloadPending_.exchange(false)) {
reload();
dirty_ = true;
}
#endif
// Periodic redraw to pick up host-automated parameter changes.
// Every ~20 frames (~3Hz at 60fps) we force a redraw.
if (++frameCounter_ >= 20) {
frameCounter_ = 0;
dirty_ = true;
}
bool shouldDraw = dirty_.exchange(false) || jsEngine_->wantsAnimatedRedraw();
if (!shouldDraw) return;
renderer_->beginFrame();
if (!renderer_->currentCanvas()) return;
jsEngine_->draw();
renderer_->present();
}
void SingularityController::reload()
{
std::cout << "Reload called" << std::endl;
renderer_->clearImageCache();
jsEngine_->load(UI_MAIN, renderer_.get());
}
void SingularityController::registerImage(const std::string& name, const uint8_t* data, int size)
{
renderer_->registerImage(name, data, size);
}