Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 21 | #include "../data.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | |
| 23 | #include <string> |
| 24 | |
| 25 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 26 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 27 | #include "../minuitwrp/minui.h" |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 28 | #include "gui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | #include "rapidxml.hpp" |
| 32 | #include "objects.hpp" |
| 33 | |
| 34 | GUIKeyboard::GUIKeyboard(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 35 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 36 | { |
| 37 | int layoutindex, rowindex, keyindex, Xindex, Yindex, keyHeight = 0, keyWidth = 0; |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 38 | rowY = colX = -1; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 39 | highlightRenderCount = 0; |
| 40 | hasHighlight = hasCapsHighlight = false; |
Dees_Troy | 3a16cb5 | 2013-01-10 15:16:02 +0000 | [diff] [blame] | 41 | char resource[10], layout[8], row[5], key[6], longpress[7]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | xml_attribute<>* attr; |
| 43 | xml_node<>* child; |
| 44 | xml_node<>* keylayout; |
| 45 | xml_node<>* keyrow; |
| 46 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 47 | for (layoutindex=0; layoutindex<MAX_KEYBOARD_LAYOUTS; layoutindex++) { |
| 48 | layouts[layoutindex].keyboardImg = NULL; |
| 49 | memset(layouts[layoutindex].keys, 0, sizeof(Layout::keys)); |
| 50 | memset(layouts[layoutindex].row_end_y, 0, sizeof(Layout::row_end_y)); |
| 51 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 52 | |
| 53 | mRendered = false; |
| 54 | currentLayout = 1; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 55 | CapsLockOn = false; |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 56 | KeyboardHeight = KeyboardWidth = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
| 58 | if (!node) return; |
| 59 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 60 | mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlight); |
| 61 | mCapsHighlightColor = LoadAttrColor(FindNode(node, "capshighlight"), "color", &hasCapsHighlight); |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 62 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 63 | // compatibility ugliness: resources should be specified in the layouts themselves instead |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | // Load the images for the different layouts |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 65 | child = FindNode(node, "layout"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | if (child) |
| 67 | { |
| 68 | layoutindex = 1; |
| 69 | strcpy(resource, "resource1"); |
| 70 | attr = child->first_attribute(resource); |
| 71 | while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 72 | layouts[layoutindex - 1].keyboardImg = LoadAttrImage(child, resource); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
| 74 | layoutindex++; |
| 75 | resource[8] = (char)(layoutindex + 48); |
| 76 | attr = child->first_attribute(resource); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Check the first image to get height and width |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 81 | if (layouts[0].keyboardImg && layouts[0].keyboardImg->GetResource()) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 83 | KeyboardWidth = layouts[0].keyboardImg->GetWidth(); |
| 84 | KeyboardHeight = layouts[0].keyboardImg->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | // Load all of the layout maps |
| 88 | layoutindex = 1; |
| 89 | strcpy(layout, "layout1"); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 90 | keylayout = FindNode(node, layout); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | while (keylayout) |
| 92 | { |
| 93 | if (layoutindex > MAX_KEYBOARD_LAYOUTS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 94 | LOGERR("Too many layouts defined in keyboard.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | return; |
| 96 | } |
| 97 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 98 | Layout& lay = layouts[layoutindex - 1]; |
| 99 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | child = keylayout->first_node("keysize"); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 101 | keyHeight = LoadAttrIntScaleY(child, "height", 0); |
| 102 | keyWidth = LoadAttrIntScaleX(child, "width", 0); |
| 103 | // compatibility ugliness: capslock="0" means that this is the caps layout. Also it has nothing to do with keysize. |
| 104 | lay.is_caps = (LoadAttrInt(child, "capslock", 1) == 0); |
| 105 | // compatibility ugliness: revert_layout has nothing to do with keysize. |
| 106 | lay.revert_layout = LoadAttrInt(child, "revert_layout", -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | |
| 108 | rowindex = 1; |
| 109 | Yindex = 0; |
| 110 | strcpy(row, "row1"); |
| 111 | keyrow = keylayout->first_node(row); |
| 112 | while (keyrow) |
| 113 | { |
| 114 | if (rowindex > MAX_KEYBOARD_ROWS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 115 | LOGERR("Too many rows defined in keyboard.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | |
| 119 | Yindex += keyHeight; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 120 | lay.row_end_y[rowindex - 1] = Yindex; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 121 | |
| 122 | keyindex = 1; |
| 123 | Xindex = 0; |
| 124 | strcpy(key, "key01"); |
| 125 | attr = keyrow->first_attribute(key); |
| 126 | |
| 127 | while (attr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | if (keyindex > MAX_KEYBOARD_KEYS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 129 | LOGERR("Too many keys defined in a keyboard row.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 133 | const char* keyinfo = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | |
| 135 | if (strlen(keyinfo) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 136 | LOGERR("No key info on layout%i, row%i, key%dd.\n", layoutindex, rowindex, keyindex); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | return; |
| 138 | } |
| 139 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 140 | if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, false)) |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 141 | LOGERR("Invalid key info on layout%i, row%i, key%02i.\n", layoutindex, rowindex, keyindex); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 143 | |
| 144 | // PROCESS LONG PRESS INFO IF EXISTS |
| 145 | sprintf(longpress, "long%02i", keyindex); |
| 146 | attr = keyrow->first_attribute(longpress); |
| 147 | if (attr) { |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 148 | const char* keyinfo = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 149 | |
| 150 | if (strlen(keyinfo) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 151 | LOGERR("No long press info on layout%i, row%i, long%dd.\n", layoutindex, rowindex, keyindex); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 155 | if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, true)) |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 156 | LOGERR("Invalid long press key info on layout%i, row%i, long%02i.\n", layoutindex, rowindex, keyindex); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 157 | } |
| 158 | keyindex++; |
| 159 | sprintf(key, "key%02i", keyindex); |
| 160 | attr = keyrow->first_attribute(key); |
| 161 | } |
| 162 | rowindex++; |
| 163 | row[3] = (char)(rowindex + 48); |
| 164 | keyrow = keylayout->first_node(row); |
| 165 | } |
| 166 | layoutindex++; |
| 167 | layout[6] = (char)(layoutindex + 48); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 168 | keylayout = FindNode(node, layout); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 169 | } |
| 170 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 171 | int x, y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | // Load the placement |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 173 | LoadPlacement(FindNode(node, "placement"), &x, &y); |
| 174 | SetRenderPos(x, y, KeyboardWidth, KeyboardHeight); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 175 | return; |
| 176 | } |
| 177 | |
| 178 | GUIKeyboard::~GUIKeyboard() |
| 179 | { |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 180 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 181 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 182 | int GUIKeyboard::ParseKey(const char* keyinfo, Key& key, int& Xindex, int keyWidth, bool longpress) |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 183 | { |
| 184 | int keychar = 0; |
| 185 | if (strlen(keyinfo) == 1) { |
| 186 | // This is a single key, simple definition |
| 187 | keychar = keyinfo[0]; |
| 188 | } else { |
| 189 | // This key has extra data: {keywidth}:{what_the_key_does} |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 190 | keyWidth = scale_theme_x(atoi(keyinfo)); |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 191 | |
| 192 | const char* ptr = keyinfo; |
| 193 | while (*ptr > 32 && *ptr != ':') |
| 194 | ptr++; |
| 195 | if (*ptr != ':') |
| 196 | return -1; // no colon is an error |
| 197 | ptr++; |
| 198 | |
| 199 | if (*ptr == 0) { // This is an empty area |
| 200 | keychar = 0; |
| 201 | } else if (strlen(ptr) == 1) { // This is the character that this key uses |
| 202 | keychar = *ptr; |
| 203 | } else if (*ptr == 'c') { // This is an ASCII character code: "c:{number}" |
| 204 | keychar = atoi(ptr + 2); |
| 205 | } else if (*ptr == 'l') { // This is a different layout: "layout{number}" |
| 206 | keychar = KEYBOARD_LAYOUT; |
| 207 | key.layout = atoi(ptr + 6); |
| 208 | } else if (*ptr == 'a') { // This is an action: "action" |
| 209 | keychar = KEYBOARD_ACTION; |
| 210 | } else |
| 211 | return -1; |
| 212 | } |
| 213 | |
| 214 | if (longpress) { |
| 215 | key.longpresskey = keychar; |
| 216 | } else { |
| 217 | key.key = keychar; |
| 218 | Xindex += keyWidth; |
| 219 | key.end_x = Xindex - 1; |
| 220 | } |
| 221 | |
| 222 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | int GUIKeyboard::Render(void) |
| 226 | { |
| 227 | if (!isConditionTrue()) |
| 228 | { |
| 229 | mRendered = false; |
| 230 | return 0; |
| 231 | } |
| 232 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 233 | Layout& lay = layouts[currentLayout - 1]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 234 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 235 | if (lay.keyboardImg && lay.keyboardImg->GetResource()) |
| 236 | gr_blit(lay.keyboardImg->GetResource(), 0, 0, KeyboardWidth, KeyboardHeight, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 237 | |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 238 | // Draw highlight for capslock |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 239 | if (hasCapsHighlight && lay.is_caps && CapsLockOn) { |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 240 | gr_color(mCapsHighlightColor.red, mCapsHighlightColor.green, mCapsHighlightColor.blue, mCapsHighlightColor.alpha); |
| 241 | for (int indexy=0; indexy<MAX_KEYBOARD_ROWS; indexy++) { |
| 242 | for (int indexx=0; indexx<MAX_KEYBOARD_KEYS; indexx++) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 243 | if ((int)lay.keys[indexy][indexx].key == KEYBOARD_LAYOUT && (int)lay.keys[indexy][indexx].layout == lay.revert_layout) { |
| 244 | int boxheight, boxwidth, x; |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 245 | if (indexy == 0) |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 246 | boxheight = lay.row_end_y[indexy]; |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 247 | else |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 248 | boxheight = lay.row_end_y[indexy] - lay.row_end_y[indexy - 1]; |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 249 | if (indexx == 0) { |
| 250 | x = mRenderX; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 251 | boxwidth = lay.keys[indexy][indexx].end_x; |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 252 | } else { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 253 | x = mRenderX + lay.keys[indexy][indexx - 1].end_x; |
| 254 | boxwidth = lay.keys[indexy][indexx].end_x - lay.keys[indexy][indexx - 1].end_x; |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 255 | } |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 256 | gr_fill(x, mRenderY + lay.row_end_y[indexy - 1], boxwidth, boxheight); |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 262 | if (hasHighlight && highlightRenderCount != 0) { |
| 263 | int boxheight, boxwidth, x; |
| 264 | if (rowY == 0) |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 265 | boxheight = lay.row_end_y[rowY]; |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 266 | else |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 267 | boxheight = lay.row_end_y[rowY] - lay.row_end_y[rowY - 1]; |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 268 | if (colX == 0) { |
| 269 | x = mRenderX; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 270 | boxwidth = lay.keys[rowY][colX].end_x; |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 271 | } else { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 272 | x = mRenderX + lay.keys[rowY][colX - 1].end_x; |
| 273 | boxwidth = lay.keys[rowY][colX].end_x - lay.keys[rowY][colX - 1].end_x; |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 274 | } |
| 275 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 276 | gr_fill(x, mRenderY + lay.row_end_y[rowY - 1], boxwidth, boxheight); |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 277 | if (highlightRenderCount > 0) |
| 278 | highlightRenderCount--; |
| 279 | } else |
| 280 | mRendered = true; |
| 281 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 282 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | int GUIKeyboard::Update(void) |
| 286 | { |
| 287 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 288 | if (!mRendered) return 2; |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | int GUIKeyboard::SetRenderPos(int x, int y, int w, int h) |
| 294 | { |
| 295 | mRenderX = x; |
| 296 | mRenderY = y; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 297 | mRenderW = KeyboardWidth; |
| 298 | mRenderH = KeyboardHeight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 299 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 300 | return 0; |
| 301 | } |
| 302 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 303 | GUIKeyboard::Key* GUIKeyboard::HitTestKey(int x, int y) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 304 | { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 305 | if (!IsInRegion(x, y)) |
| 306 | return NULL; |
| 307 | |
| 308 | int rely = y - mRenderY; |
| 309 | int relx = x - mRenderX; |
| 310 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 311 | Layout& lay = layouts[currentLayout - 1]; |
| 312 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 313 | // Find the correct row |
| 314 | int row; |
| 315 | for (row = 0; row < MAX_KEYBOARD_ROWS; ++row) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 316 | if (lay.row_end_y[row] > rely) |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 317 | break; |
| 318 | } |
| 319 | if (row == MAX_KEYBOARD_ROWS) |
| 320 | return NULL; |
| 321 | |
| 322 | // Find the correct key (column) |
| 323 | int col; |
| 324 | int x1 = 0; |
| 325 | for (col = 0; col < MAX_KEYBOARD_KEYS; ++col) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 326 | Key& key = lay.keys[row][col]; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 327 | if (x1 <= relx && relx < key.end_x && key.key != 0) { |
| 328 | // This is the key that was pressed! |
| 329 | rowY = row; |
| 330 | colX = col; |
| 331 | return &key; |
| 332 | } |
| 333 | x1 = key.end_x; |
| 334 | } |
| 335 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 339 | { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 340 | static int was_held = 0, startX = 0; |
| 341 | static Key* initial_key = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 342 | |
| 343 | if (!isConditionTrue()) return -1; |
| 344 | |
| 345 | switch (state) |
| 346 | { |
| 347 | case TOUCH_START: |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 348 | was_held = 0; |
| 349 | startX = x; |
| 350 | initial_key = HitTestKey(x, y); |
| 351 | if (initial_key) |
| 352 | highlightRenderCount = -1; |
| 353 | else |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 354 | highlightRenderCount = 0; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 355 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 356 | break; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 357 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 358 | case TOUCH_DRAG: |
| 359 | break; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 360 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 361 | case TOUCH_RELEASE: |
Dees_Troy | 0cb64e5 | 2012-10-15 15:56:10 -0400 | [diff] [blame] | 362 | if (x < startX - (KeyboardWidth * 0.5)) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 363 | if (highlightRenderCount != 0) { |
| 364 | highlightRenderCount = 0; |
| 365 | mRendered = false; |
| 366 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT); |
| 368 | return 0; |
Dees_Troy | 0cb64e5 | 2012-10-15 15:56:10 -0400 | [diff] [blame] | 369 | } else if (x > startX + (KeyboardWidth * 0.5)) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 370 | if (highlightRenderCount != 0) { |
| 371 | highlightRenderCount = 0; |
| 372 | mRendered = false; |
| 373 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 374 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT); |
| 375 | return 0; |
| 376 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 377 | // fall through |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 378 | case TOUCH_HOLD: |
| 379 | case TOUCH_REPEAT: |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 380 | if (!initial_key) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 381 | if (highlightRenderCount != 0) { |
| 382 | highlightRenderCount = 0; |
| 383 | mRendered = false; |
| 384 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 385 | return 0; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | if (highlightRenderCount != 0) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 389 | if (state == TOUCH_RELEASE) |
| 390 | highlightRenderCount = 2; |
| 391 | else |
| 392 | highlightRenderCount = -1; |
| 393 | mRendered = false; |
| 394 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 395 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 396 | if (HitTestKey(x, y) != initial_key) { |
| 397 | // We dragged off of the starting key |
| 398 | if (highlightRenderCount != 0) { |
| 399 | highlightRenderCount = 0; |
| 400 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 401 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 402 | return 0; |
| 403 | } else { |
| 404 | Key& key = *initial_key; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 405 | Layout& lay = layouts[currentLayout - 1]; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 406 | if (state == TOUCH_RELEASE && was_held == 0) { |
| 407 | DataManager::Vibrate("tw_keyboard_vibrate"); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 408 | if ((int)key.key == KEYBOARD_LAYOUT) { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 409 | // Switch layouts |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 410 | if (lay.is_caps && key.layout == lay.revert_layout && !CapsLockOn) { |
| 411 | CapsLockOn = true; // Set the caps lock |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 412 | } else { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 413 | CapsLockOn = false; // Unset the caps lock and change layouts |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 414 | currentLayout = key.layout; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 415 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 416 | mRendered = false; |
| 417 | } else if ((int)key.key == KEYBOARD_ACTION) { |
| 418 | // Action |
| 419 | highlightRenderCount = 0; |
| 420 | // Send action notification |
| 421 | PageManager::NotifyKeyboard(key.key); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 422 | } else if ((int)key.key < KEYBOARD_SPECIAL_KEYS && (int)key.key > 0) { |
| 423 | // Regular key |
| 424 | PageManager::NotifyKeyboard(key.key); |
| 425 | if (!CapsLockOn && lay.is_caps) { |
| 426 | // caps lock was not set, change layouts |
| 427 | currentLayout = lay.revert_layout; |
| 428 | mRendered = false; |
| 429 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 430 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 431 | } else if (state == TOUCH_HOLD) { |
| 432 | was_held = 1; |
| 433 | if ((int)key.key == KEYBOARD_BACKSPACE) { |
| 434 | // Repeat backspace |
| 435 | PageManager::NotifyKeyboard(key.key); |
| 436 | } else if ((int)key.longpresskey < KEYBOARD_SPECIAL_KEYS && (int)key.longpresskey > 0) { |
| 437 | // Long Press Key |
| 438 | DataManager::Vibrate("tw_keyboard_vibrate"); |
| 439 | PageManager::NotifyKeyboard(key.longpresskey); |
| 440 | } |
| 441 | } else if (state == TOUCH_REPEAT) { |
| 442 | was_held = 1; |
| 443 | if ((int)key.key == KEYBOARD_BACKSPACE) { |
| 444 | // Repeat backspace |
| 445 | PageManager::NotifyKeyboard(key.key); |
| 446 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 447 | } |
| 448 | } |
| 449 | break; |
| 450 | } |
| 451 | |
| 452 | return 0; |
| 453 | } |