This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathatom_view.cpp
331 lines (265 loc) · 9.38 KB
/
atom_view.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
#include "atom_view.h"
#include <assert.h>
#include <string.h>
#include <poincare/integer.h>
#include <poincare/number.h>
#include <escher/palette.h>
#include <apps/i18n.h>
#include "atom_defs.h"
#define ATOM_VIEW_VARS_NUM 4
#define MODE_TABLE 0
#define MODE_ATOM 1
#ifndef ATOM_APP_USE_PALETTE
class AtomPalette {
public:
constexpr static KDColor Unknown = KDColor::RGB24(0xeeeeee);
constexpr static KDColor AlkaliMetal = KDColor::RGB24(0xffaa00);
constexpr static KDColor AlkaliEarthMetal = KDColor::RGB24(0xf6f200);
constexpr static KDColor Lanthanide = KDColor::RGB24(0xffaa8b);
constexpr static KDColor Actinide = KDColor::RGB24(0xdeaacd);
constexpr static KDColor TransitionMetal = KDColor::RGB24(0xde999c);
constexpr static KDColor PostTransitionMetal = KDColor::RGB24(0x9cbaac);
constexpr static KDColor Metalloid = KDColor::RGB24(0x52ce8b);
constexpr static KDColor Halogen = KDColor::RGB24(0x00debd);
constexpr static KDColor ReactiveNonmetal = KDColor::RGB24(0x00ee00);
constexpr static KDColor NobleGas = KDColor::RGB24(0x8baaff);
constexpr static KDColor TableLines = KDColor::RGB24(0x323532);
constexpr static KDColor AtomColor[] = {
Unknown, AlkaliMetal, AlkaliEarthMetal, Lanthanide, Actinide, TransitionMetal,
PostTransitionMetal, Metalloid, Halogen, ReactiveNonmetal, NobleGas
};
};
constexpr KDColor AtomPalette::AtomColor[];
constexpr KDColor AtomPalette::TableLines;
#endif
namespace Atom {
AtomView::AtomView() :
View()
{
}
void AtomView::drawAtom(KDContext * ctx, uint8_t id) const {
KDColor fill = AtomPalette::AtomColor[(uint8_t)atomsdefs[id].type];
if (atomsdefs[id].y >= 7) {
ctx->fillRect(KDRect(6 + atomsdefs[id].x * 17, 15 + atomsdefs[id].y * 17, 18, 18), fill);
ctx->strokeRect(KDRect(6 + atomsdefs[id].x * 17, 15 + atomsdefs[id].y * 17, 18, 18), AtomPalette::TableLines);
ctx->drawString(atomsdefs[id].symbol, KDPoint(8 + atomsdefs[id].x * 17, 17 + atomsdefs[id].y * 17), KDFont::SmallFont, KDColorBlack, fill);
} else {
ctx->fillRect(KDRect(6 + atomsdefs[id].x * 17, 6 + atomsdefs[id].y * 17, 18, 18), fill);
ctx->strokeRect(KDRect(6 + atomsdefs[id].x * 17, 6 + atomsdefs[id].y * 17, 18, 18), AtomPalette::TableLines);
ctx->drawString(atomsdefs[id].symbol, KDPoint(8 + atomsdefs[id].x * 17, 8 + atomsdefs[id].y * 17), KDFont::SmallFont, KDColorBlack, fill);
}
}
uint8_t cursor_pos = 2;
bool partial_draw = false;
bool copy_mode = false;
uint8_t copy_cursor_pos = 0;
uint8_t view_mode = MODE_TABLE;
void AtomView::handleLeft() {
if (cursor_pos > 0) {
cursor_pos--;
partial_draw = true;
markRectAsDirty(bounds());
}
}
void AtomView::handleRight() {
if (cursor_pos < ATOM_NUMS - 1) {
cursor_pos++;
partial_draw = true;
markRectAsDirty(bounds());
}
}
void AtomView::handleUp() {
if (copy_mode) {
if (copy_cursor_pos > 0)
copy_cursor_pos--;
partial_draw = true;
markRectAsDirty(bounds());
return;
}
if (view_mode == MODE_ATOM) {
return;
}
uint8_t curr_x = atomsdefs[cursor_pos].x;
uint8_t curr_y = atomsdefs[cursor_pos].y;
bool updated = false;
if (curr_y > 0 && curr_y <= 9) {
for(uint8_t i = 0; i < ATOM_NUMS; i++) {
if (atomsdefs[i].x == curr_x && atomsdefs[i].y == curr_y - 1) {
cursor_pos = i;
updated = true;
break;
}
}
}
if (updated) {
partial_draw = true;
markRectAsDirty(bounds());
}
}
void AtomView::handleDown() {
if (copy_mode) {
if (copy_cursor_pos < ATOM_VIEW_VARS_NUM-1)
copy_cursor_pos++;
partial_draw = true;
markRectAsDirty(bounds());
return;
}
if (view_mode == MODE_ATOM) {
return;
}
uint8_t curr_x = atomsdefs[cursor_pos].x;
uint8_t curr_y = atomsdefs[cursor_pos].y;
bool updated = false;
if (curr_y >= 0 && curr_y < 9) {
for(uint8_t i = 0; i < ATOM_NUMS; i++) {
if (atomsdefs[i].x == curr_x && atomsdefs[i].y == curr_y + 1) {
cursor_pos = i;
updated = true;
break;
}
}
}
if (updated) {
partial_draw = true;
markRectAsDirty(bounds());
}
}
bool AtomView::handleOK() {
if (view_mode == MODE_TABLE) {
if (copy_mode) {
copy_mode = false;
char buffer[12];
switch(copy_cursor_pos) {
case 0:
Poincare::Integer(atomsdefs[cursor_pos].num).serialize(buffer, 4);
break;
case 1:
Poincare::Integer(atomsdefs[cursor_pos].neutrons + atomsdefs[cursor_pos].num).serialize(buffer, 4);
break;
case 2:
Poincare::Number::FloatNumber(atomsdefs[cursor_pos].mass).serialize(buffer, 11);
break;
case 3:
Poincare::Number::FloatNumber(atomsdefs[cursor_pos].electroneg).serialize(buffer, 11);
break;
default:
break;
}
Clipboard::sharedClipboard()->store(buffer);
partial_draw = true;
markRectAsDirty(bounds());
return true;
} else {
view_mode = MODE_ATOM;
copy_mode = false;
partial_draw = false;
markRectAsDirty(bounds());
return true;
}
}
return false;
}
void AtomView::handleCopy() {
copy_mode = true;
copy_cursor_pos = 0;
partial_draw = true;
markRectAsDirty(bounds());
}
bool AtomView::handleBack() {
if (view_mode == MODE_ATOM) {
view_mode = MODE_TABLE;
copy_mode = false;
partial_draw = false;
markRectAsDirty(bounds());
return true;
} else if (view_mode == MODE_TABLE) {
if (copy_mode) {
copy_mode = false;
partial_draw = true;
markRectAsDirty(bounds());
return true;
}
}
return false;
}
void AtomView::drawRect(KDContext * ctx, KDRect rect) const {
if (view_mode == MODE_TABLE) {
if (partial_draw) {
partial_draw = false;
ctx->fillRect(KDRect(50, 0, 169, 57), KDColorWhite);
ctx->fillRect(KDRect( 8, 170, 305, 35), KDColorWhite);
} else {
ctx->fillRect(bounds(), KDColorWhite);
}
for(uint8_t i = 0; i < ATOM_NUMS; i++) {
AtomView::drawAtom(ctx, i);
}
if (!copy_mode) {
if (atomsdefs[cursor_pos].y >= 7) {
ctx->strokeRect(KDRect(6 + atomsdefs[cursor_pos].x * 17, 15 + atomsdefs[cursor_pos].y * 17, 18, 18), KDColorBlack);
ctx->strokeRect(KDRect(7 + atomsdefs[cursor_pos].x * 17, 16 + atomsdefs[cursor_pos].y * 17, 16, 16), KDColorBlack);
} else {
ctx->strokeRect(KDRect(6 + atomsdefs[cursor_pos].x * 17, 6 + atomsdefs[cursor_pos].y * 17, 18, 18), KDColorBlack);
ctx->strokeRect(KDRect(7 + atomsdefs[cursor_pos].x * 17, 7 + atomsdefs[cursor_pos].y * 17, 16, 16), KDColorBlack);
}
}
ctx->fillRect(KDRect(48, 99, 2, 61), AtomPalette::TableLines);
ctx->fillRect(KDRect(48, 141, 9, 2), AtomPalette::TableLines);
ctx->fillRect(KDRect(48, 158, 9, 2), AtomPalette::TableLines);
char protons[4];
char nucleons[4];
Poincare::Integer(atomsdefs[cursor_pos].num).serialize(protons, 4);
Poincare::Integer(atomsdefs[cursor_pos].neutrons + atomsdefs[cursor_pos].num).serialize(nucleons, 4);
ctx->drawString(atomsdefs[cursor_pos].symbol, KDPoint(73, 23), KDFont::LargeFont);
ctx->drawString(I18n::translate(atomsdefs[cursor_pos].name), KDPoint(110, 27), KDFont::SmallFont);
if (copy_mode && copy_cursor_pos == 0)
ctx->drawString(nucleons, KDPoint(50, 17), KDFont::SmallFont, KDColorBlack, Palette::SelectDark);
else
ctx->drawString(nucleons, KDPoint(50, 17), KDFont::SmallFont);
if (copy_mode && copy_cursor_pos == 1)
ctx->drawString(protons, KDPoint(50, 31), KDFont::SmallFont, KDColorBlack, Palette::SelectDark);
else
ctx->drawString(protons, KDPoint(50, 31), KDFont::SmallFont);
char buffer[20];
memset(buffer, ' ', 19);
buffer[19] = 0;
buffer[0] = 'M';
buffer[1] = ':';
int num = Poincare::Number::FloatNumber(atomsdefs[cursor_pos].mass).serialize(buffer + 5, 13);
buffer[5 + num] = 0;
if (copy_mode && copy_cursor_pos == 2)
ctx->drawString(buffer, KDPoint(8, 174), KDFont::SmallFont, KDColorBlack, Palette::SelectDark);
else
ctx->drawString(buffer, KDPoint(8, 174), KDFont::SmallFont);
memset(buffer, ' ', 19);
buffer[19] = 0;
buffer[0] = 'K';
buffer[1] = 'h';
buffer[2] = 'i';
buffer[3] = ':';
if (atomsdefs[cursor_pos].electroneg == -1) {
buffer[5] = 'N';
buffer[6] = '/';
buffer[7] = 'A';
buffer[8] = 0;
} else {
num = Poincare::Number::FloatNumber(atomsdefs[cursor_pos].electroneg).serialize(buffer + 5, 13);
buffer[5 + num] = 0;
}
if (copy_mode && copy_cursor_pos == 3)
ctx->drawString(buffer, KDPoint(8, 188), KDFont::SmallFont, KDColorBlack, Palette::SelectDark);
else
ctx->drawString(buffer, KDPoint(8, 188), KDFont::SmallFont);
} else if (view_mode == MODE_ATOM) {
ctx->fillRect(bounds(), KDColorWhite);
char protons[4];
char nucleons[4];
Poincare::Integer(atomsdefs[cursor_pos].num).serialize(protons, 4);
Poincare::Integer(atomsdefs[cursor_pos].neutrons + atomsdefs[cursor_pos].num).serialize(nucleons, 4);
ctx->drawString(atomsdefs[cursor_pos].symbol, KDPoint(29, 12), KDFont::LargeFont);
ctx->drawString(I18n::translate(atomsdefs[cursor_pos].name), KDPoint(66, 16), KDFont::SmallFont);
ctx->drawString(nucleons, KDPoint(6, 6), KDFont::SmallFont);
ctx->drawString(protons, KDPoint(6, 20), KDFont::SmallFont);
}
}
}