-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathmain.cpy
281 lines (226 loc) · 7.92 KB
/
main.cpy
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
#include "../build/rmkit.h"
#include "../shared/string.h"
#include "shapes.h"
#include <sys/types.h>
#include <sys/wait.h>
pid_t _pid
class AppBackground: public ui::Widget:
public:
int byte_size
framebuffer::VirtualFB *vfb = NULL
AppBackground(int x, y, w, h): ui::Widget(x, y, w, h):
self.byte_size = w*h*sizeof(remarkable_color)
fw, fh := fb->get_display_size()
vfb = new framebuffer::VirtualFB(fw, fh)
vfb->clear_screen()
vfb->fbmem = (remarkable_color*) memcpy(vfb->fbmem, fb->fbmem, self.byte_size)
void render():
if rm2fb::IN_RM2FB_SHIM:
fb->waveform_mode = WAVEFORM_MODE_GC16
else:
fb->waveform_mode = WAVEFORM_MODE_AUTO
memcpy(fb->fbmem, vfb->fbmem, self.byte_size)
fb->perform_redraw(true)
fb->dirty = 1
class SettingsDialog: public ui::InfoDialog:
public:
ui::ToggleButton* snap_enabled
ui::ToggleButton* rows_header_enabled
ui::ToggleButton* columns_header_enabled
ui::RangeInput* snap_range
ui::RangeInput* rows_range
ui::RangeInput* columns_range
SettingsDialog(int x, y, w, h): ui::InfoDialog(x, y, w, h):
self.set_title(string("Settings"))
self.contentWidget = new ui::Text(0, 0, 20, 50, "")
void save_settings():
debug "SAVING SETTINGS"
shape::Shape::set_snapping(self.snap_range->get_value())
shape::Shape::snap_enabled = self.snap_enabled->toggled
shape::Shape::rows_header_enabled = self.rows_header_enabled->toggled
shape::Shape::columns_header_enabled = self.columns_header_enabled->toggled
shape::Shape::rows = self.rows_range->get_value()
shape::Shape::columns = self.columns_range->get_value()
void on_button_selected(string text):
self.hide()
self.save_settings()
// this function actually builds the dialog scene and necessary widgets /
// and packings for the modal overlay
void build_dialog():
ui::InfoDialog::build_dialog()
self.on_hide = self.scene->on_hide
self.scene->on_hide += PLS_LAMBDA(auto &visible) {
self.hide()
self.save_settings()
}
a_layout := ui::VerticalLayout(self.x, self.y+50, self.w, self.h-100, self.scene)
left := 20
snap_section_label := new ui::Text(left, 0, self.w - 2*left, 50, "Snapping")
snap_section_label->set_style(ui::Stylesheet()
.valign_bottom()
.justify_center()
.underline())
self.snap_enabled = new ui::ToggleButton(left, 0, self.w - 2*left, 50, "Enabled")
self.snap_enabled->set_style(ui::Stylesheet()
.valign_middle()
.justify_left())
snap_range_label := new ui::Text(left, 0, self.w - 2*left, 50, "Snap grid (mm)")
snap_range_label->set_style(ui::Stylesheet()
.valign_bottom()
.justify_left())
self.snap_range = new ui::RangeInput(left, 0, self.w - 2*left, 50)
snap_range->set_range(1, 10)
table_label := new ui::Text(left, 0, self.w - 2*left, 50, "Table")
table_label->set_style(ui::Stylesheet()
.valign_bottom()
.justify_center()
.underline())
rows_label := new ui::Text(left, 0, self.w - 2*left, 50, "Rows")
rows_label->set_style(ui::Stylesheet()
.valign_bottom()
.justify_left())
self.rows_range = new ui::RangeInput(left, 0, self.w - 2*left, 50)
rows_range->set_range(1, 10)
self.rows_header_enabled = new ui::ToggleButton(left, 0, self.w - 2*left, 50, "Header Enabled")
self.rows_header_enabled->set_style(ui::Stylesheet()
.valign_middle()
.justify_left())
columns_label := new ui::Text(left, 0, self.w - 2*left, 50, "Columns")
columns_label->set_style(ui::Stylesheet()
.valign_bottom()
.justify_left())
self.columns_range = new ui::RangeInput(left, 0, self.w - 2*left, 50)
columns_range->set_range(1, 10)
self.columns_header_enabled = new ui::ToggleButton(left, 0, self.w - 2*left, 50, "Header Enabled")
self.columns_header_enabled->set_style(ui::Stylesheet()
.valign_middle()
.justify_left())
a_layout.pack_start(snap_section_label)
a_layout.pack_start(snap_enabled)
a_layout.pack_start(snap_range_label)
a_layout.pack_start(snap_range)
a_layout.pack_start(table_label)
a_layout.pack_start(rows_label)
a_layout.pack_start(rows_header_enabled)
a_layout.pack_start(rows_range)
a_layout.pack_start(columns_label)
a_layout.pack_start(columns_header_enabled)
a_layout.pack_start(columns_range)
class App:
public:
AppBackground* app_bg
SettingsDialog *settings
shared_ptr<framebuffer::FB> fb
App():
fb = framebuffer::get()
w, h = fb->get_display_size()
fb->dither = framebuffer::DITHER::BAYER_2
fb->waveform_mode = WAVEFORM_MODE_DU
scene := ui::make_scene()
ui::MainLoop::set_scene(scene)
app_bg = new AppBackground(0, 0, w, h)
scene->add(app_bg)
settings = new SettingsDialog(0, 0, 400, 650)
style := ui::Stylesheet() \
.valign(ui::Style::VALIGN::MIDDLE) \
.justify(ui::Style::JUSTIFY::CENTER)
h_layout := ui::HorizontalLayout(0, h-60, w, 50, scene)
no_button := new ui::Button(0, 0, 200, 50, "cancel")
ok_button := new ui::Button(0, 0, 200, 50, "ok")
settings_btn := new ui::Button(0, 0, 200, 50, "settings")
h_layout.pack_start(settings_btn)
h_layout.pack_end(ok_button)
h_layout.pack_end(no_button)
shape_dropdown := new ui::TextDropdown(0, 0, 250, 50, "add")
shape_dropdown->dir = ui::DropdownButton::DIRECTION::UP
// TODO: pull these from the list of available shapes
shape_dropdown->add_section("add shape")->add_options(%{
"line",
"v. line",
"h. line",
"rect",
"circle",
"bezier",
"table"})
h_layout.pack_center(shape_dropdown)
no_button->set_style(style)
ok_button->set_style(style)
no_button->mouse.click += PLS_LAMBDA(auto &ev) {
self.cleanup()
exit(0)
}
settings_btn->mouse.click += PLS_LAMBDA(auto &ev) {
settings->show();
}
ok_button->mouse.click += PLS_LAMBDA(auto &ev) {
self.cleanup()
ui::MainLoop::in.touch.lock()
shape_strs := vector<string>{}
for auto sh : shape::to_draw:
str := sh->to_lamp()
cmd := "echo '"+ str + "' | /opt/bin/lamp"
debug "RUNNING", cmd
_ := system("sleep 0.1")
_ = system(cmd.c_str())
_ := system("sleep 0.5")
ui::MainLoop::in.ungrab()
exit(0)
}
// TODO: this should use Shape to get the shape to use
shape_dropdown->events.selected += PLS_LAMBDA(int i) {
val := shape_dropdown->options[i]->name
r := 100
if val == "circle":
s := new shape::Circle(w/2-r/2, h/2-r/2, r, r, scene)
if val == "h. line":
s := new shape::HLine(w/2-r/2, h/2-r/2, r, r, scene)
if val == "v. line":
s := new shape::VLine(w/2-r/2, h/2-r/2, r, r, scene)
if val == "line":
s := new shape::Line(w/2-r/2, h/2-r/2, r, r, scene)
if val == "rect":
s := new shape::Rectangle(w/2-r/2, h/2-r/2, r, r, scene)
if val == "bezier":
r = 300
s := new shape::Bezier(w/2-r/2, h/2-r/2, r, r, scene)
if val == "table":
s := new shape::Table(w/2-r/2, h/2-r/2, r, r, scene)
}
void redraw(bool skip_shape=false):
app_bg->render()
void redraw(input::SynMotionEvent &ev):
redraw(false)
void cleanup():
debug "CLEANING UP", _pid
ui::MainLoop::in.ungrab()
app_bg->render()
void run():
ui::MainLoop::in.grab()
ui::MainLoop::refresh()
// just to kick off the app, we do a full redraw
ui::MainLoop::redraw()
while true:
ui::MainLoop::main()
ui::MainLoop::redraw()
ui::MainLoop::read_input()
App app
did_clean := false
void cleanup():
if did_clean:
return
did_clean = true
app.cleanup()
exit(0)
void catch_sigint(int):
debug "CAUGHT SIGINT", _pid
cleanup()
def main():
signal(SIGINT, catch_sigint);
// we fork just to be a little bit extra safe with
// unlocking the input file descriptors
_pid = fork()
if _pid:
wait(NULL)
ui::MainLoop::in.ungrab()
else:
app.run()