|
| 1 | +using namespace ui |
| 2 | + |
| 3 | +BLANK := WHITE |
| 4 | +WRONG_PLACE := color::gray32(25) |
| 5 | +RIGHT_PLACE := color::gray32(15) |
| 6 | +WRONG_LETTER := WHITE - 1 |
| 7 | + |
| 8 | +namespace wordle: |
| 9 | + namespace keyboard: |
| 10 | + Stylesheet BTN_STYLE = Stylesheet().font_size(48).valign_middle() |
| 11 | + using namespace ui; |
| 12 | + |
| 13 | + class KeyButton: public ui::Button: |
| 14 | + public: |
| 15 | + remarkable_color color = WHITE |
| 16 | + KeyButton(int x, y, w, h, string t): ui::Button(x, y, w, h, t): |
| 17 | + pass |
| 18 | + |
| 19 | + void before_render(): |
| 20 | + ui::Button::before_render() |
| 21 | + self.mouse_inside = self.mouse_down && self.mouse_inside |
| 22 | + |
| 23 | + void render(): |
| 24 | + fb->draw_rect(self.x, self.y, self.w, self.h, WHITE, true) |
| 25 | + if self.color == WRONG_LETTER: |
| 26 | + fb->draw_line(self.x, self.y, self.x+self.w, self.y+self.h, 2, BLACK) |
| 27 | + fb->draw_line(self.x+self.w, self.y, self.x, self.y+self.h, 2, BLACK) |
| 28 | + |
| 29 | + else: |
| 30 | + fb->draw_rect(self.x, self.y, self.w, self.h, self.color, true) |
| 31 | + |
| 32 | + self.textWidget->render() |
| 33 | + |
| 34 | + color := WHITE |
| 35 | + if self.mouse_inside: |
| 36 | + color = BLACK |
| 37 | + |
| 38 | + fill := false |
| 39 | + if self.mouse_down: |
| 40 | + fill = true |
| 41 | + fb->draw_rect(self.x, self.y, self.w, self.h, color, fill) |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | + class Row: public Widget: |
| 46 | + public: |
| 47 | + HorizontalLayout *layout = NULL |
| 48 | + Scene scene |
| 49 | + |
| 50 | + Row(int x, y, w, h, Scene s): Widget(x,y,w,h): |
| 51 | + self.scene = s |
| 52 | + |
| 53 | + void add_key(KeyButton *key): |
| 54 | + if self.layout == NULL: |
| 55 | + debug "RENDERING ROW", self.x, self.y, self.w, self.h |
| 56 | + self.layout = new HorizontalLayout(self.x, self.y, self.w, self.h, self.scene) |
| 57 | + self.layout->pack_start(key) |
| 58 | + |
| 59 | + void render(): |
| 60 | + pass // if a component is in scene, it gets rendered |
| 61 | + |
| 62 | + class Keyboard: public Widget: |
| 63 | + class KEYBOARD_EVENTS: |
| 64 | + public: |
| 65 | + KEYBOARD_EVENT changed |
| 66 | + KEYBOARD_EVENT done |
| 67 | + |
| 68 | + public: |
| 69 | + vector<Row*> rows |
| 70 | + map<char, KeyButton*> keys |
| 71 | + Scene scene |
| 72 | + string text = "" |
| 73 | + int btn_width |
| 74 | + int btn_height |
| 75 | + |
| 76 | + KEYBOARD_EVENTS events |
| 77 | + |
| 78 | + Keyboard(Scene s): Widget(0, 0, 0, 0) |
| 79 | + self.scene = s |
| 80 | + w, full_h = self.fb->get_display_size() |
| 81 | + h = full_h / 4 |
| 82 | + self.w = w |
| 83 | + self.h = h |
| 84 | + self.upper_layout() |
| 85 | + |
| 86 | + void upper_layout(): |
| 87 | + self.set_layout( |
| 88 | + "QWERTYUOIP", |
| 89 | + "ASDFGHJKL", |
| 90 | + "ZXCVBNM" |
| 91 | + ) |
| 92 | + |
| 93 | + void set_layout(string row1chars, row2chars, row3chars): |
| 94 | + self.btn_width = w / row1chars.size() |
| 95 | + self.btn_height = 100 |
| 96 | + indent := row1chars.size() > row2chars.size() ? h/8 : 0 |
| 97 | + row1 := new Row(0,0,w,100, self.scene) |
| 98 | + row2 := new Row(indent,0,w,100, self.scene) |
| 99 | + row3 := new Row(indent,0,w,100, self.scene) |
| 100 | + |
| 101 | + fw, fh = self.fb->get_display_size() |
| 102 | + v_layout := ui::VerticalLayout(0, 0, fw, fh, self.scene) |
| 103 | + |
| 104 | + v_layout.pack_end(row3) |
| 105 | + v_layout.pack_end(row2) |
| 106 | + v_layout.pack_end(row1) |
| 107 | + |
| 108 | + for (auto c: row1chars): |
| 109 | + row1->add_key(self.make_char_button(c)) |
| 110 | + |
| 111 | + for (auto c: row2chars): |
| 112 | + row2->add_key(self.make_char_button(c)) |
| 113 | + |
| 114 | + backspace_key := new KeyButton(0,0,self.btn_width,btn_height,"back") |
| 115 | + backspace_key->set_style(BTN_STYLE) |
| 116 | + backspace_key->mouse.click += PLS_LAMBDA(auto &ev): |
| 117 | + if self.text.size() > 0: |
| 118 | + self.text.pop_back() |
| 119 | + kev := KeyboardEvent {text:self.text} |
| 120 | + self.events.changed(kev) |
| 121 | + self.dirty = 1 |
| 122 | + ; |
| 123 | + row3->add_key(backspace_key) |
| 124 | + for (auto c: row3chars): |
| 125 | + row3->add_key(self.make_char_button(c)) |
| 126 | + |
| 127 | + enter_key := new KeyButton(0,0,self.btn_width,btn_height,"enter") |
| 128 | + enter_key->set_style(BTN_STYLE) |
| 129 | + enter_key->mouse.click += PLS_LAMBDA(auto &ev): |
| 130 | + kev := KeyboardEvent {text:self.text} |
| 131 | + if self.text.length() == 5: |
| 132 | + self.events.done(kev) |
| 133 | + ; |
| 134 | + |
| 135 | + row3->add_key(enter_key) |
| 136 | + |
| 137 | + void mark_color(char c, remarkable_color color): |
| 138 | + key := self.keys.find(c) |
| 139 | + if key != self.keys.end(): |
| 140 | + if key->second->color > color: |
| 141 | + key->second->color = color |
| 142 | + key->second->dirty = 1 |
| 143 | + |
| 144 | + void clear_colors(remarkable_color color): |
| 145 | + for auto key : self.keys: |
| 146 | + key.second->color = color |
| 147 | + |
| 148 | + KeyButton* make_char_button(char c): |
| 149 | + string s(1, c) |
| 150 | + key := new KeyButton(0,0,self.btn_width,btn_height,s) |
| 151 | + key->set_style(BTN_STYLE) |
| 152 | + key->mouse.click += PLS_LAMBDA(auto &ev): |
| 153 | + self.dirty = 1 |
| 154 | + if c == ' ': |
| 155 | + return |
| 156 | + |
| 157 | + if self.text.length() < 5: |
| 158 | + self.text.push_back(c) |
| 159 | + kev := KeyboardEvent {text:self.text} |
| 160 | + self.events.changed(kev) |
| 161 | + ; |
| 162 | + |
| 163 | + self.keys[c] = key |
| 164 | + return key |
| 165 | + |
| 166 | + KeyButton* make_icon_button(icons::Icon icon, int w): |
| 167 | + key := new KeyButton(0,0,self.btn_width,btn_height,"") |
| 168 | + key->icon = icon |
| 169 | + return key |
| 170 | + |
| 171 | + void render(): |
| 172 | + pass |
| 173 | + ; |
0 commit comments