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; |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 38 | currentKey = NULL; |
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; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
| 57 | if (!node) return; |
| 58 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 59 | mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlight); |
| 60 | mCapsHighlightColor = LoadAttrColor(FindNode(node, "capshighlight"), "color", &hasCapsHighlight); |
Ethan Yonker | c3120d4 | 2014-02-17 07:55:00 -0600 | [diff] [blame] | 61 | |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 62 | child = FindNode(node, "keymargin"); |
| 63 | mKeyMarginX = LoadAttrIntScaleX(child, "x", 0); |
| 64 | mKeyMarginY = LoadAttrIntScaleY(child, "y", 0); |
| 65 | |
| 66 | child = FindNode(node, "background"); |
| 67 | mBackgroundColor = LoadAttrColor(child, "color", COLOR(32,32,32,255)); |
| 68 | |
| 69 | child = FindNode(node, "key-alphanumeric"); |
| 70 | mFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel")); |
| 71 | mFontColor = LoadAttrColor(child, "textcolor", COLOR(255,255,255,255)); |
| 72 | mKeyColorAlphanumeric = LoadAttrColor(child, "color", COLOR(0,0,0,0)); |
| 73 | |
| 74 | child = FindNode(node, "key-other"); |
| 75 | mSmallFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-small")); |
| 76 | mFontColorSmall = LoadAttrColor(child, "textcolor", COLOR(192,192,192,255)); |
| 77 | mKeyColorOther = LoadAttrColor(child, "color", COLOR(0,0,0,0)); |
| 78 | |
| 79 | child = FindNode(node, "longpress"); |
| 80 | mLongpressFont = PageManager::GetResources()->FindFont(LoadAttrString(child, "font", "keylabel-longpress")); |
| 81 | mLongpressFontColor = LoadAttrColor(child, "textcolor", COLOR(128,128,128,255)); |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 82 | LoadPlacement(child, &longpressOffsetX, &longpressOffsetY); |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 83 | |
| 84 | LoadKeyLabels(node, 0); // load global key labels |
| 85 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 86 | // compatibility ugliness: resources should be specified in the layouts themselves instead |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | // Load the images for the different layouts |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 88 | child = FindNode(node, "layout"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | if (child) |
| 90 | { |
| 91 | layoutindex = 1; |
| 92 | strcpy(resource, "resource1"); |
| 93 | attr = child->first_attribute(resource); |
| 94 | while (attr && layoutindex < (MAX_KEYBOARD_LAYOUTS + 1)) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 95 | layouts[layoutindex - 1].keyboardImg = LoadAttrImage(child, resource); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | |
| 97 | layoutindex++; |
| 98 | resource[8] = (char)(layoutindex + 48); |
| 99 | attr = child->first_attribute(resource); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // Check the first image to get height and width |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 104 | if (layouts[0].keyboardImg && layouts[0].keyboardImg->GetResource()) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | { |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 106 | mRenderW = layouts[0].keyboardImg->GetWidth(); |
| 107 | mRenderH = layouts[0].keyboardImg->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // Load all of the layout maps |
| 111 | layoutindex = 1; |
| 112 | strcpy(layout, "layout1"); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 113 | keylayout = FindNode(node, layout); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 114 | while (keylayout) |
| 115 | { |
| 116 | if (layoutindex > MAX_KEYBOARD_LAYOUTS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 117 | LOGERR("Too many layouts defined in keyboard.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 118 | return; |
| 119 | } |
| 120 | |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 121 | LoadKeyLabels(keylayout, layoutindex); // load per-layout key labels |
| 122 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 123 | Layout& lay = layouts[layoutindex - 1]; |
| 124 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | child = keylayout->first_node("keysize"); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 126 | keyHeight = LoadAttrIntScaleY(child, "height", 0); |
| 127 | keyWidth = LoadAttrIntScaleX(child, "width", 0); |
| 128 | // compatibility ugliness: capslock="0" means that this is the caps layout. Also it has nothing to do with keysize. |
| 129 | lay.is_caps = (LoadAttrInt(child, "capslock", 1) == 0); |
| 130 | // compatibility ugliness: revert_layout has nothing to do with keysize. |
| 131 | lay.revert_layout = LoadAttrInt(child, "revert_layout", -1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 132 | |
| 133 | rowindex = 1; |
| 134 | Yindex = 0; |
| 135 | strcpy(row, "row1"); |
| 136 | keyrow = keylayout->first_node(row); |
| 137 | while (keyrow) |
| 138 | { |
| 139 | if (rowindex > MAX_KEYBOARD_ROWS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 140 | LOGERR("Too many rows defined in keyboard.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | Yindex += keyHeight; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 145 | lay.row_end_y[rowindex - 1] = Yindex; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 146 | |
| 147 | keyindex = 1; |
| 148 | Xindex = 0; |
| 149 | strcpy(key, "key01"); |
| 150 | attr = keyrow->first_attribute(key); |
| 151 | |
| 152 | while (attr) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | if (keyindex > MAX_KEYBOARD_KEYS) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 154 | LOGERR("Too many keys defined in a keyboard row.\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | return; |
| 156 | } |
| 157 | |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 158 | const char* keyinfo = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | |
| 160 | if (strlen(keyinfo) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 161 | 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] | 162 | return; |
| 163 | } |
| 164 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 165 | if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, false)) |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 166 | 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] | 167 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 168 | |
| 169 | // PROCESS LONG PRESS INFO IF EXISTS |
| 170 | sprintf(longpress, "long%02i", keyindex); |
| 171 | attr = keyrow->first_attribute(longpress); |
| 172 | if (attr) { |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 173 | const char* keyinfo = attr->value(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 174 | |
| 175 | if (strlen(keyinfo) == 0) { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 176 | 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] | 177 | return; |
| 178 | } |
| 179 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 180 | if (ParseKey(keyinfo, lay.keys[rowindex - 1][keyindex - 1], Xindex, keyWidth, true)) |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 181 | 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] | 182 | } |
| 183 | keyindex++; |
| 184 | sprintf(key, "key%02i", keyindex); |
| 185 | attr = keyrow->first_attribute(key); |
| 186 | } |
| 187 | rowindex++; |
| 188 | row[3] = (char)(rowindex + 48); |
| 189 | keyrow = keylayout->first_node(row); |
| 190 | } |
| 191 | layoutindex++; |
| 192 | layout[6] = (char)(layoutindex + 48); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 193 | keylayout = FindNode(node, layout); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 194 | } |
| 195 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 196 | int x, y; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 197 | // Load the placement |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 198 | LoadPlacement(FindNode(node, "placement"), &x, &y, &mRenderW, &mRenderH); |
| 199 | SetRenderPos(x, y, mRenderW, mRenderH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 200 | return; |
| 201 | } |
| 202 | |
| 203 | GUIKeyboard::~GUIKeyboard() |
| 204 | { |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 205 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 207 | 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] | 208 | { |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 209 | key.layout = 0; |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 210 | int keychar = 0; |
| 211 | if (strlen(keyinfo) == 1) { |
| 212 | // This is a single key, simple definition |
| 213 | keychar = keyinfo[0]; |
| 214 | } else { |
| 215 | // This key has extra data: {keywidth}:{what_the_key_does} |
Ethan Yonker | 63e414f | 2015-02-06 15:44:39 -0600 | [diff] [blame] | 216 | keyWidth = scale_theme_x(atoi(keyinfo)); |
that | 1a7ba97 | 2015-02-01 19:48:19 +0100 | [diff] [blame] | 217 | |
| 218 | const char* ptr = keyinfo; |
| 219 | while (*ptr > 32 && *ptr != ':') |
| 220 | ptr++; |
| 221 | if (*ptr != ':') |
| 222 | return -1; // no colon is an error |
| 223 | ptr++; |
| 224 | |
| 225 | if (*ptr == 0) { // This is an empty area |
| 226 | keychar = 0; |
| 227 | } else if (strlen(ptr) == 1) { // This is the character that this key uses |
| 228 | keychar = *ptr; |
| 229 | } else if (*ptr == 'c') { // This is an ASCII character code: "c:{number}" |
| 230 | keychar = atoi(ptr + 2); |
| 231 | } else if (*ptr == 'l') { // This is a different layout: "layout{number}" |
| 232 | keychar = KEYBOARD_LAYOUT; |
| 233 | key.layout = atoi(ptr + 6); |
| 234 | } else if (*ptr == 'a') { // This is an action: "action" |
| 235 | keychar = KEYBOARD_ACTION; |
| 236 | } else |
| 237 | return -1; |
| 238 | } |
| 239 | |
| 240 | if (longpress) { |
| 241 | key.longpresskey = keychar; |
| 242 | } else { |
| 243 | key.key = keychar; |
| 244 | Xindex += keyWidth; |
| 245 | key.end_x = Xindex - 1; |
| 246 | } |
| 247 | |
| 248 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 249 | } |
| 250 | |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 251 | void GUIKeyboard::LoadKeyLabels(xml_node<>* parent, int layout) |
| 252 | { |
| 253 | for (xml_node<>* child = parent->first_node(); child; child = child->next_sibling()) { |
| 254 | std::string name = child->name(); |
| 255 | if (name == "keylabel") { |
| 256 | std::string keydef = LoadAttrString(child, "key", ""); |
| 257 | Key tempkey; |
| 258 | int dummyX; |
| 259 | if (ParseKey(keydef.c_str(), tempkey, dummyX, 0, false) == 0) { |
| 260 | KeyLabel keylabel; |
| 261 | keylabel.key = tempkey.key; |
| 262 | keylabel.layout_from = layout; |
| 263 | keylabel.layout_to = tempkey.layout; |
| 264 | keylabel.text = LoadAttrString(child, "text", ""); |
| 265 | keylabel.image = LoadAttrImage(child, "resource"); |
| 266 | mKeyLabels.push_back(keylabel); |
| 267 | } else { |
| 268 | LOGERR("Ignoring invalid keylabel in layout %d: '%s'.\n", layout, keydef.c_str()); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | } |
| 273 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 274 | void GUIKeyboard::DrawKey(Key& key, int keyX, int keyY, int keyW, int keyH) |
| 275 | { |
| 276 | unsigned char keychar = key.key; |
| 277 | if (!keychar) |
| 278 | return; |
| 279 | |
| 280 | // key background |
| 281 | COLOR& c = (keychar >= 32 && keychar < 127) ? mKeyColorAlphanumeric : mKeyColorOther; |
| 282 | gr_color(c.red, c.green, c.blue, c.alpha); |
| 283 | keyX += mKeyMarginX; |
| 284 | keyY += mKeyMarginY; |
| 285 | keyW -= mKeyMarginX * 2; |
| 286 | keyH -= mKeyMarginY * 2; |
| 287 | gr_fill(keyX, keyY, keyW, keyH); |
| 288 | |
| 289 | // key label |
| 290 | FontResource* labelFont = mFont; |
| 291 | string labelText; |
| 292 | ImageResource* labelImage = NULL; |
| 293 | if (keychar > 32 && keychar < 127) { |
| 294 | labelText = (char) keychar; |
| 295 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 296 | } |
| 297 | else { |
| 298 | // search for a special key label |
| 299 | for (std::vector<KeyLabel>::iterator it = mKeyLabels.begin(); it != mKeyLabels.end(); ++it) { |
| 300 | if (it->layout_from > 0 && it->layout_from != currentLayout) |
| 301 | continue; // this label is for another layout |
| 302 | if (it->key == key.key && it->layout_to == key.layout) |
| 303 | { |
| 304 | // found a label |
| 305 | labelText = it->text; |
| 306 | labelImage = it->image; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | labelFont = mSmallFont; |
| 311 | gr_color(mFontColorSmall.red, mFontColorSmall.green, mFontColorSmall.blue, mFontColorSmall.alpha); |
| 312 | } |
| 313 | |
| 314 | if (labelImage) |
| 315 | { |
| 316 | int w = labelImage->GetWidth(); |
| 317 | int h = labelImage->GetHeight(); |
| 318 | int x = keyX + (keyW - w) / 2; |
| 319 | int y = keyY + (keyH - h) / 2; |
| 320 | gr_blit(labelImage->GetResource(), 0, 0, w, h, x, y); |
| 321 | } |
| 322 | else if (!labelText.empty()) |
| 323 | { |
| 324 | void* fontResource = labelFont->GetResource(); |
| 325 | int textW = gr_measureEx(labelText.c_str(), fontResource); |
| 326 | int textH = labelFont->GetHeight(); |
| 327 | int textX = keyX + (keyW - textW) / 2; |
| 328 | int textY = keyY + (keyH - textH) / 2; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 329 | gr_textEx_scaleW(textX, textY, labelText.c_str(), fontResource, keyW, TOP_LEFT, 0); |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // longpress key label (only if font is defined) |
| 333 | keychar = key.longpresskey; |
| 334 | if (keychar > 32 && keychar < 127 && mLongpressFont->GetResource()) { |
| 335 | void* fontResource = mLongpressFont->GetResource(); |
| 336 | gr_color(mLongpressFontColor.red, mLongpressFontColor.green, mLongpressFontColor.blue, mLongpressFontColor.alpha); |
| 337 | string text(1, keychar); |
| 338 | int textH = mLongpressFont->GetHeight(); |
| 339 | int textW = gr_measureEx(text.c_str(), fontResource); |
| 340 | int textX = keyX + keyW - longpressOffsetX - textW; |
| 341 | int textY = keyY + longpressOffsetY; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 342 | gr_textEx_scaleW(textX, textY, text.c_str(), fontResource, keyW, TOP_LEFT, 0); |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 346 | int GUIKeyboard::Render(void) |
| 347 | { |
| 348 | if (!isConditionTrue()) |
| 349 | { |
| 350 | mRendered = false; |
| 351 | return 0; |
| 352 | } |
| 353 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 354 | Layout& lay = layouts[currentLayout - 1]; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 355 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 356 | bool drawKeys = false; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 357 | if (lay.keyboardImg && lay.keyboardImg->GetResource()) |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 358 | // keyboard is image based |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 359 | gr_blit(lay.keyboardImg->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 360 | else { |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 361 | // keyboard is software drawn |
| 362 | // fill background |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 363 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, mBackgroundColor.alpha); |
| 364 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 365 | drawKeys = true; |
| 366 | } |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 367 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 368 | // draw keys |
| 369 | int y1 = 0; |
| 370 | for (int row = 0; row < MAX_KEYBOARD_ROWS; ++row) { |
| 371 | int rowY = mRenderY + y1; |
| 372 | int rowH = lay.row_end_y[row] - y1; |
| 373 | y1 = lay.row_end_y[row]; |
| 374 | int x1 = 0; |
| 375 | for (int col = 0; col < MAX_KEYBOARD_KEYS; ++col) { |
| 376 | Key& key = lay.keys[row][col]; |
| 377 | int keyY = rowY; |
| 378 | int keyH = rowH; |
| 379 | int keyX = mRenderX + x1; |
| 380 | int keyW = key.end_x - x1; |
| 381 | x1 = key.end_x; |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 382 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 383 | // Draw key for software drawn keyboard |
| 384 | if (drawKeys) |
| 385 | DrawKey(key, keyX, keyY, keyW, keyH); |
| 386 | |
| 387 | // Draw highlight for capslock |
| 388 | if (hasCapsHighlight && lay.is_caps && CapsLockOn && (int)key.key == KEYBOARD_LAYOUT && key.layout == lay.revert_layout) { |
| 389 | gr_color(mCapsHighlightColor.red, mCapsHighlightColor.green, mCapsHighlightColor.blue, mCapsHighlightColor.alpha); |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 390 | gr_fill(keyX, keyY, keyW, keyH); |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 391 | } |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 392 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 393 | // Highlight current key |
| 394 | if (hasHighlight && &key == currentKey && highlightRenderCount != 0) { |
| 395 | gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha); |
| 396 | gr_fill(keyX, keyY, keyW, keyH); |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 400 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 401 | if (!hasHighlight || highlightRenderCount == 0) |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 402 | mRendered = true; |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 403 | else if (highlightRenderCount > 0) |
| 404 | highlightRenderCount--; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 405 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | int GUIKeyboard::Update(void) |
| 409 | { |
| 410 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 411 | if (!mRendered) return 2; |
| 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int GUIKeyboard::SetRenderPos(int x, int y, int w, int h) |
| 417 | { |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 418 | RenderObject::SetRenderPos(x, y, w, h); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 419 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 420 | return 0; |
| 421 | } |
| 422 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 423 | GUIKeyboard::Key* GUIKeyboard::HitTestKey(int x, int y) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 424 | { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 425 | if (!IsInRegion(x, y)) |
| 426 | return NULL; |
| 427 | |
| 428 | int rely = y - mRenderY; |
| 429 | int relx = x - mRenderX; |
| 430 | |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 431 | Layout& lay = layouts[currentLayout - 1]; |
| 432 | |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 433 | // Find the correct row |
| 434 | int row; |
| 435 | for (row = 0; row < MAX_KEYBOARD_ROWS; ++row) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 436 | if (lay.row_end_y[row] > rely) |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | if (row == MAX_KEYBOARD_ROWS) |
| 440 | return NULL; |
| 441 | |
| 442 | // Find the correct key (column) |
| 443 | int col; |
| 444 | int x1 = 0; |
| 445 | for (col = 0; col < MAX_KEYBOARD_KEYS; ++col) { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 446 | Key& key = lay.keys[row][col]; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 447 | if (x1 <= relx && relx < key.end_x && key.key != 0) { |
| 448 | // This is the key that was pressed! |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 449 | return &key; |
| 450 | } |
| 451 | x1 = key.end_x; |
| 452 | } |
| 453 | return NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | int GUIKeyboard::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 457 | { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 458 | static int was_held = 0, startX = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 459 | |
| 460 | if (!isConditionTrue()) return -1; |
| 461 | |
| 462 | switch (state) |
| 463 | { |
| 464 | case TOUCH_START: |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 465 | was_held = 0; |
| 466 | startX = x; |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 467 | currentKey = HitTestKey(x, y); |
| 468 | if (currentKey) |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 469 | highlightRenderCount = -1; |
| 470 | else |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 471 | highlightRenderCount = 0; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 472 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 473 | break; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 474 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 475 | case TOUCH_DRAG: |
| 476 | break; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 477 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 478 | case TOUCH_RELEASE: |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 479 | if (x < startX - (mRenderW * 0.5)) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 480 | if (highlightRenderCount != 0) { |
| 481 | highlightRenderCount = 0; |
| 482 | mRendered = false; |
| 483 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 484 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_LEFT); |
| 485 | return 0; |
that | f256b72 | 2015-05-23 20:44:12 +0200 | [diff] [blame] | 486 | } else if (x > startX + (mRenderW * 0.5)) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 487 | if (highlightRenderCount != 0) { |
| 488 | highlightRenderCount = 0; |
| 489 | mRendered = false; |
| 490 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 491 | PageManager::NotifyKeyboard(KEYBOARD_SWIPE_RIGHT); |
| 492 | return 0; |
| 493 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 494 | // fall through |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 495 | case TOUCH_HOLD: |
| 496 | case TOUCH_REPEAT: |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 497 | if (!currentKey) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 498 | if (highlightRenderCount != 0) { |
| 499 | highlightRenderCount = 0; |
| 500 | mRendered = false; |
| 501 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 502 | return 0; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | if (highlightRenderCount != 0) { |
Dees_Troy | 30b962e | 2012-10-19 20:48:59 -0400 | [diff] [blame] | 506 | if (state == TOUCH_RELEASE) |
| 507 | highlightRenderCount = 2; |
| 508 | else |
| 509 | highlightRenderCount = -1; |
| 510 | mRendered = false; |
| 511 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 512 | |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 513 | if (HitTestKey(x, y) != currentKey) { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 514 | // We dragged off of the starting key |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 515 | currentKey = NULL; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 516 | if (highlightRenderCount != 0) { |
| 517 | highlightRenderCount = 0; |
| 518 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 519 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 520 | return 0; |
| 521 | } else { |
that | f6b2066 | 2015-06-25 21:51:37 +0200 | [diff] [blame] | 522 | Key& key = *currentKey; |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 523 | Layout& lay = layouts[currentLayout - 1]; |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 524 | if (state == TOUCH_RELEASE && was_held == 0) { |
| 525 | DataManager::Vibrate("tw_keyboard_vibrate"); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 526 | if ((int)key.key == KEYBOARD_LAYOUT) { |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 527 | // Switch layouts |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 528 | if (lay.is_caps && key.layout == lay.revert_layout && !CapsLockOn) { |
| 529 | CapsLockOn = true; // Set the caps lock |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 530 | } else { |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 531 | CapsLockOn = false; // Unset the caps lock and change layouts |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 532 | currentLayout = key.layout; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 533 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 534 | mRendered = false; |
| 535 | } else if ((int)key.key == KEYBOARD_ACTION) { |
| 536 | // Action |
| 537 | highlightRenderCount = 0; |
| 538 | // Send action notification |
| 539 | PageManager::NotifyKeyboard(key.key); |
that | d86f49d | 2015-03-15 00:56:57 +0100 | [diff] [blame] | 540 | } else if ((int)key.key < KEYBOARD_SPECIAL_KEYS && (int)key.key > 0) { |
| 541 | // Regular key |
| 542 | PageManager::NotifyKeyboard(key.key); |
| 543 | if (!CapsLockOn && lay.is_caps) { |
| 544 | // caps lock was not set, change layouts |
| 545 | currentLayout = lay.revert_layout; |
| 546 | mRendered = false; |
| 547 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 548 | } |
that | e79878b | 2015-03-14 23:07:23 +0100 | [diff] [blame] | 549 | } else if (state == TOUCH_HOLD) { |
| 550 | was_held = 1; |
| 551 | if ((int)key.key == KEYBOARD_BACKSPACE) { |
| 552 | // Repeat backspace |
| 553 | PageManager::NotifyKeyboard(key.key); |
| 554 | } else if ((int)key.longpresskey < KEYBOARD_SPECIAL_KEYS && (int)key.longpresskey > 0) { |
| 555 | // Long Press Key |
| 556 | DataManager::Vibrate("tw_keyboard_vibrate"); |
| 557 | PageManager::NotifyKeyboard(key.longpresskey); |
| 558 | } |
| 559 | } else if (state == TOUCH_REPEAT) { |
| 560 | was_held = 1; |
| 561 | if ((int)key.key == KEYBOARD_BACKSPACE) { |
| 562 | // Repeat backspace |
| 563 | PageManager::NotifyKeyboard(key.key); |
| 564 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 565 | } |
| 566 | } |
| 567 | break; |
| 568 | } |
| 569 | |
| 570 | return 0; |
| 571 | } |