Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 1 | /* |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 2 | Copyright 2012 to 2016 bigbiff/Dees_Troy TeamWin |
Dees Troy | 3be70a8 | 2013-10-22 14:25:12 +0000 | [diff] [blame] | 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 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // input.cpp - GUIInput object |
| 20 | |
| 21 | #include <linux/input.h> |
| 22 | #include <pthread.h> |
| 23 | #include <stdarg.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <sys/reboot.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/time.h> |
| 31 | #include <sys/mman.h> |
| 32 | #include <sys/types.h> |
| 33 | #include <sys/ioctl.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
| 36 | #include <stdlib.h> |
| 37 | |
| 38 | #include <string> |
| 39 | |
| 40 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 43 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | |
| 45 | #include "rapidxml.hpp" |
| 46 | #include "objects.hpp" |
| 47 | #include "../data.hpp" |
| 48 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 49 | #define TW_INPUT_NO_UPDATE -1000 // Magic value for HandleTextLocation when no change in scrolling has occurred |
| 50 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | GUIInput::GUIInput(xml_node<>* node) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 52 | : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 54 | xml_attribute<>* attr; |
| 55 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | mInputText = NULL; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | mAction = NULL; |
| 59 | mBackground = NULL; |
| 60 | mCursor = NULL; |
| 61 | mFont = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 62 | mRendered = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 63 | HasMask = false; |
| 64 | DrawCursor = false; |
| 65 | isLocalChange = true; |
| 66 | HasAllowed = false; |
| 67 | HasDisabled = false; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 68 | cursorX = textWidth = scrollingX = mFontHeight = mFontY = lastX = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | mBackgroundX = mBackgroundY = mBackgroundW = mBackgroundH = MinLen = MaxLen = 0; |
| 70 | mCursorLocation = -1; // -1 is always the end of the string |
| 71 | CursorWidth = 3; |
| 72 | ConvertStrToColor("black", &mBackgroundColor); |
| 73 | ConvertStrToColor("white", &mCursorColor); |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 74 | mValue = ""; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 75 | displayValue = ""; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | if (!node) |
| 78 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 80 | // Load text directly from the node |
| 81 | mInputText = new GUIText(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | // Load action directly from the node |
| 83 | mAction = new GUIAction(node); |
| 84 | |
| 85 | if (mInputText->Render() < 0) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 86 | { |
| 87 | delete mInputText; |
| 88 | mInputText = NULL; |
| 89 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | |
| 91 | // Load the background |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 92 | child = FindNode(node, "background"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 93 | if (child) |
| 94 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 95 | mBackground = LoadAttrImage(child, "resource"); |
| 96 | mBackgroundColor = LoadAttrColor(child, "color", mBackgroundColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | } |
| 98 | if (mBackground && mBackground->GetResource()) |
| 99 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 100 | mBackgroundW = mBackground->GetWidth(); |
| 101 | mBackgroundH = mBackground->GetHeight(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Load the cursor color |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 105 | child = FindNode(node, "cursor"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | if (child) |
| 107 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 108 | mCursor = LoadAttrImage(child, "resource"); |
| 109 | mCursorColor = LoadAttrColor(child, "color", mCursorColor); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 110 | attr = child->first_attribute("hasfocus"); |
| 111 | if (attr) |
| 112 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 113 | std::string focus = attr->value(); |
| 114 | SetInputFocus(atoi(focus.c_str())); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | } |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 116 | CursorWidth = LoadAttrIntScaleX(child, "width", CursorWidth); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | } |
| 118 | DrawCursor = HasInputFocus; |
| 119 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 120 | // Load the font |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 121 | child = FindNode(node, "font"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 122 | if (child) |
| 123 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 124 | mFont = LoadAttrFont(child, "resource"); |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 125 | if (mFont && mFont->GetResource()) |
| 126 | mFontHeight = mFont->GetHeight(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 127 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 129 | child = FindNode(node, "data"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 130 | if (child) |
| 131 | { |
| 132 | attr = child->first_attribute("name"); |
| 133 | if (attr) |
| 134 | mVariable = attr->value(); |
| 135 | attr = child->first_attribute("default"); |
| 136 | if (attr) |
| 137 | DataManager::SetValue(mVariable, attr->value()); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 138 | mMask = LoadAttrString(child, "mask"); |
| 139 | HasMask = !mMask.empty(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Load input restrictions |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 143 | child = FindNode(node, "restrict"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | if (child) |
| 145 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 146 | MinLen = LoadAttrInt(child, "minlen", MinLen); |
| 147 | MaxLen = LoadAttrInt(child, "maxlen", MaxLen); |
| 148 | AllowedList = LoadAttrString(child, "allow"); |
| 149 | HasAllowed = !AllowedList.empty(); |
| 150 | DisabledList = LoadAttrString(child, "disable"); |
| 151 | HasDisabled = !DisabledList.empty(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | } |
| 153 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 155 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 157 | |
| 158 | if (mInputText && mFontHeight && mFontHeight < (unsigned)mRenderH) { |
| 159 | mFontY = ((mRenderH - mFontHeight) / 2) + mRenderY; |
| 160 | mInputText->SetRenderPos(mRenderX, mFontY); |
| 161 | } else |
| 162 | mFontY = mRenderY; |
| 163 | |
| 164 | if (mInputText) |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 165 | mInputText->SetMaxWidth(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | GUIInput::~GUIInput() |
| 169 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 170 | delete mInputText; |
| 171 | delete mAction; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | } |
| 173 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 174 | void GUIInput::HandleTextLocation(int x) { |
| 175 | mRendered = false; |
| 176 | if (textWidth <= mRenderW) { |
| 177 | if (x != TW_INPUT_NO_UPDATE) |
| 178 | lastX = x; |
| 179 | scrollingX = 0; |
| 180 | return; |
| 181 | } |
| 182 | if (scrollingX + textWidth < mRenderW) { |
| 183 | scrollingX = mRenderW - textWidth; |
| 184 | } |
| 185 | |
| 186 | if (x == TW_INPUT_NO_UPDATE) |
| 187 | return; |
| 188 | |
| 189 | scrollingX += x - lastX; |
| 190 | if (scrollingX > 0) |
| 191 | scrollingX = 0; |
| 192 | else if (scrollingX + textWidth < mRenderW) |
| 193 | scrollingX = mRenderW - textWidth; |
| 194 | lastX = x; |
| 195 | } |
| 196 | |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 197 | void GUIInput::UpdateDisplayText() { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 198 | void* fontResource = NULL; |
| 199 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 200 | if (mFont) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 201 | fontResource = mFont->GetResource(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 202 | } else { |
| 203 | textWidth = 0; |
| 204 | return; |
| 205 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 206 | |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 207 | DataManager::GetValue(mVariable, mValue); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 208 | if (HasMask) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 209 | int index, string_size = mValue.size(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 210 | string maskedValue; |
| 211 | for (index=0; index<string_size; index++) |
| 212 | maskedValue += mMask; |
| 213 | displayValue = maskedValue; |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 214 | } else { |
| 215 | displayValue = mValue; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 216 | } |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 217 | |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 218 | textWidth = gr_ttf_measureEx(displayValue.c_str(), fontResource); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void GUIInput::HandleCursorByTouch(int x) { |
| 222 | // Uses x to find mCursorLocation and cursorX |
| 223 | if (displayValue.size() == 0) { |
| 224 | mCursorLocation = -1; |
| 225 | cursorX = mRenderX; |
| 226 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 227 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 229 | void* fontResource = NULL; |
| 230 | if (mFont) { |
| 231 | fontResource = mFont->GetResource(); |
| 232 | } else { |
| 233 | return; |
| 234 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 235 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 236 | string cursorString; |
| 237 | unsigned index = 0, displaySize = displayValue.size(); |
| 238 | int prevX = mRenderX + scrollingX; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 239 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 240 | for (index = 0; index <= displaySize; index++) { |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 241 | cursorString = displayValue.substr(0, index); |
| 242 | cursorX = gr_ttf_measureEx(cursorString.c_str(), fontResource) + mRenderX + scrollingX; |
| 243 | if (cursorX > x) { |
| 244 | if (index > 0 && x <= cursorX - ((x - prevX) / 2) && prevX >= mRenderX) { |
| 245 | // This helps make sure that we can place the cursor before the very first char if the first char is |
| 246 | // is fully visible while also still letting us place the cursor after the last char if fully visible |
| 247 | mCursorLocation = index - 1; |
| 248 | cursorX = prevX; |
| 249 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 250 | } |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 251 | mCursorLocation = index; |
| 252 | if (cursorX > mRenderX + mRenderW) { |
| 253 | cursorX = prevX; // This makes sure that the cursor doesn't get placed after the end of the input box |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 254 | mCursorLocation--; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 255 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 256 | } |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 257 | if (cursorX >= mRenderX) { |
| 258 | return; // This makes sure that the cursor doesn't get placed before the beginning of the input box |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | } |
| 260 | } |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 261 | prevX = cursorX; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 262 | } |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 263 | mCursorLocation = -1; // x is at or past the end of the string |
| 264 | } |
| 265 | |
| 266 | void GUIInput::HandleCursorByText() { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 267 | // Uses mCursorLocation to find cursorX |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 268 | if (!DrawCursor) |
| 269 | return; |
| 270 | |
| 271 | void* fontResource = NULL; |
| 272 | if (mFont) { |
| 273 | fontResource = mFont->GetResource(); |
| 274 | } else { |
| 275 | return; |
| 276 | } |
| 277 | |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 278 | int cursorTextWidth = textWidth; // width of text to the left of the cursor |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 279 | |
| 280 | if (mCursorLocation != -1) { |
| 281 | string cursorDisplay = displayValue; |
| 282 | cursorDisplay.resize(mCursorLocation); |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 283 | cursorTextWidth = gr_ttf_measureEx(cursorDisplay.c_str(), fontResource); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 284 | } |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 285 | cursorX = mRenderX + cursorTextWidth + scrollingX; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 286 | if (cursorX >= mRenderX + mRenderW) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 287 | scrollingX = mRenderW - cursorTextWidth; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 288 | cursorX = mRenderX + mRenderW - CursorWidth; |
| 289 | } else if (cursorX < mRenderX) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 290 | scrollingX = cursorTextWidth * -1; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 291 | cursorX = mRenderX; |
| 292 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | int GUIInput::Render(void) |
| 296 | { |
| 297 | if (!isConditionTrue()) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 298 | { |
| 299 | mRendered = false; |
| 300 | return 0; |
| 301 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 302 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 303 | // First step, fill background |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 304 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 305 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 306 | |
| 307 | // Next, render the background resource (if it exists) |
| 308 | if (mBackground && mBackground->GetResource()) |
| 309 | { |
| 310 | mBackgroundX = mRenderX + ((mRenderW - mBackgroundW) / 2); |
| 311 | mBackgroundY = mRenderY + ((mRenderH - mBackgroundH) / 2); |
| 312 | gr_blit(mBackground->GetResource(), 0, 0, mBackgroundW, mBackgroundH, mBackgroundX, mBackgroundY); |
| 313 | } |
| 314 | |
| 315 | int ret = 0; |
| 316 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 317 | // Render the text |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 318 | if (mInputText) { |
| 319 | mInputText->SetRenderPos(mRenderX + scrollingX, mFontY); |
| 320 | mInputText->SetText(displayValue); |
| 321 | gr_clip(mRenderX, mRenderY, mRenderW, mRenderH); |
| 322 | ret = mInputText->Render(); |
| 323 | gr_noclip(); |
| 324 | } |
| 325 | if (ret < 0) |
| 326 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 327 | |
| 328 | if (HasInputFocus && DrawCursor) { |
| 329 | // Render the cursor |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 330 | gr_color(mCursorColor.red, mCursorColor.green, mCursorColor.blue, 255); |
| 331 | gr_fill(cursorX, mFontY, CursorWidth, mFontHeight); |
| 332 | } |
| 333 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 334 | mRendered = true; |
| 335 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | int GUIInput::Update(void) |
| 339 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 340 | if (!isConditionTrue()) return (mRendered ? 2 : 0); |
| 341 | if (!mRendered) return 2; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 342 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 343 | int ret = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 344 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 345 | if (mInputText) ret = mInputText->Update(); |
| 346 | if (ret < 0) return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 347 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 348 | return ret; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | int GUIInput::GetSelection(int x, int y) |
| 352 | { |
| 353 | if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH) return -1; |
| 354 | return (x - mRenderX); |
| 355 | } |
| 356 | |
| 357 | int GUIInput::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 358 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 359 | static int startSelection = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 360 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 361 | if (!isConditionTrue()) |
| 362 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 363 | |
| 364 | if (!HasInputFocus) { |
| 365 | if (state != TOUCH_RELEASE) |
| 366 | return 0; // Only change focus if touch releases within the input box |
| 367 | if (GetSelection(x, y) >= 0) { |
| 368 | // When changing focus, we don't scroll or change the cursor location |
| 369 | PageManager::SetKeyBoardFocus(0); |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 370 | PageManager::NotifyCharInput(0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 371 | SetInputFocus(1); |
| 372 | DrawCursor = true; |
| 373 | mRendered = false; |
| 374 | } |
| 375 | } else { |
| 376 | switch (state) { |
| 377 | case TOUCH_HOLD: |
| 378 | case TOUCH_REPEAT: |
| 379 | break; |
| 380 | case TOUCH_START: |
| 381 | startSelection = GetSelection(x,y); |
| 382 | lastX = x; |
| 383 | DrawCursor = false; |
| 384 | mRendered = false; |
| 385 | break; |
| 386 | |
| 387 | case TOUCH_DRAG: |
| 388 | // Check if we dragged out of the selection window |
| 389 | if (GetSelection(x, y) == -1) { |
| 390 | lastX = 0; |
| 391 | break; |
| 392 | } |
| 393 | |
| 394 | DrawCursor = false; |
| 395 | |
| 396 | // Provide some debounce on initial touches |
| 397 | if (startSelection != -1 && abs(x - lastX) < 6) { |
| 398 | break; |
| 399 | } |
| 400 | |
| 401 | startSelection = -1; |
| 402 | if (lastX != x) |
| 403 | HandleTextLocation(x); |
| 404 | break; |
| 405 | |
| 406 | case TOUCH_RELEASE: |
| 407 | // We've moved the cursor location |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 408 | mRendered = false; |
| 409 | DrawCursor = true; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 410 | HandleCursorByTouch(x); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 411 | break; |
| 412 | } |
| 413 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 414 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 415 | } |
| 416 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 417 | int GUIInput::NotifyVarChange(const std::string& varName, const std::string& value) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 418 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 419 | GUIObject::NotifyVarChange(varName, value); |
| 420 | |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 421 | if (varName == mVariable) { |
| 422 | if (!isLocalChange) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 423 | UpdateDisplayText(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 424 | HandleTextLocation(TW_INPUT_NO_UPDATE); |
| 425 | } else |
| 426 | isLocalChange = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 427 | return 0; |
| 428 | } |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 429 | if (varName.empty()) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 430 | UpdateDisplayText(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 431 | HandleTextLocation(TW_INPUT_NO_UPDATE); |
| 432 | HandleCursorByText(); |
| 433 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 434 | return 0; |
| 435 | } |
| 436 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 437 | int GUIInput::NotifyKey(int key, bool down) |
| 438 | { |
| 439 | if (!HasInputFocus || !down) |
| 440 | return 1; |
| 441 | |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 442 | switch (key) |
| 443 | { |
| 444 | case KEY_LEFT: |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 445 | if (mCursorLocation == 0) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 446 | return 0; // we're already at the beginning |
| 447 | if (mCursorLocation == -1) { |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 448 | if (displayValue.size() == 0) { |
| 449 | cursorX = mRenderX; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 450 | return 0; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 451 | } |
| 452 | mCursorLocation = displayValue.size() - 1; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 453 | } else { |
| 454 | mCursorLocation--; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 455 | } |
| 456 | mRendered = false; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 457 | HandleCursorByText(); |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 458 | return 0; |
| 459 | |
| 460 | case KEY_RIGHT: |
| 461 | if (mCursorLocation == -1) |
| 462 | return 0; // we're already at the end |
| 463 | mCursorLocation++; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 464 | if ((int)displayValue.size() <= mCursorLocation) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 465 | mCursorLocation = -1; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 466 | HandleCursorByText(); |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 467 | mRendered = false; |
| 468 | return 0; |
| 469 | |
| 470 | case KEY_HOME: |
| 471 | case KEY_UP: |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 472 | if (displayValue.size() == 0) |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 473 | return 0; |
| 474 | mCursorLocation = 0; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 475 | mRendered = false; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 476 | cursorX = mRenderX; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 477 | return 0; |
| 478 | |
| 479 | case KEY_END: |
| 480 | case KEY_DOWN: |
| 481 | mCursorLocation = -1; |
| 482 | mRendered = false; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 483 | HandleCursorByText(); |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | return 1; |
| 488 | } |
| 489 | |
| 490 | int GUIInput::NotifyCharInput(int key) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 491 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 492 | if (HasInputFocus) { |
| 493 | if (key == KEYBOARD_BACKSPACE) { |
| 494 | //Backspace |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 495 | if (mValue.size() > 0 && mCursorLocation != 0) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 496 | if (mCursorLocation == -1) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 497 | mValue.resize(mValue.size() - 1); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 498 | } else { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 499 | mValue.erase(mCursorLocation - 1, 1); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 500 | mCursorLocation--; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 501 | } |
| 502 | isLocalChange = true; |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 503 | DataManager::SetValue(mVariable, mValue); |
| 504 | UpdateDisplayText(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 505 | HandleTextLocation(TW_INPUT_NO_UPDATE); |
| 506 | HandleCursorByText(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 507 | } |
| 508 | } else if (key == KEYBOARD_SWIPE_LEFT) { |
| 509 | // Delete all |
| 510 | isLocalChange = true; |
| 511 | if (mCursorLocation == -1) { |
| 512 | DataManager::SetValue (mVariable, ""); |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 513 | mValue = ""; |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 514 | textWidth = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 515 | mCursorLocation = -1; |
| 516 | } else { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 517 | mValue.erase(0, mCursorLocation); |
| 518 | DataManager::SetValue(mVariable, mValue); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 519 | mCursorLocation = 0; |
| 520 | } |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 521 | UpdateDisplayText(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 522 | cursorX = mRenderX; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 523 | scrollingX = 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 524 | mRendered = false; |
| 525 | return 0; |
that | 8834a0f | 2016-01-05 23:29:30 +0100 | [diff] [blame] | 526 | } else if (key >= 32) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 527 | // Regular key |
| 528 | if (HasAllowed && AllowedList.find((char)key) == string::npos) { |
| 529 | return 0; |
| 530 | } |
| 531 | if (HasDisabled && DisabledList.find((char)key) != string::npos) { |
| 532 | return 0; |
| 533 | } |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 534 | if (MaxLen != 0 && mValue.size() >= MaxLen) { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 535 | return 0; |
| 536 | } |
| 537 | if (mCursorLocation == -1) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 538 | mValue += key; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 539 | } else { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 540 | mValue.insert(mCursorLocation, 1, key); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 541 | mCursorLocation++; |
| 542 | } |
| 543 | isLocalChange = true; |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 544 | DataManager::SetValue(mVariable, mValue); |
| 545 | UpdateDisplayText(); |
Ethan Yonker | a5db712 | 2016-03-14 15:47:09 -0500 | [diff] [blame] | 546 | HandleTextLocation(TW_INPUT_NO_UPDATE); |
| 547 | HandleCursorByText(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 548 | } else if (key == KEYBOARD_ACTION) { |
| 549 | // Action |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 550 | if (mAction) { |
Sultan Qasim Khan | 14138d9 | 2016-04-04 04:11:25 -0400 | [diff] [blame] | 551 | unsigned inputLen = mValue.length(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 552 | if (inputLen < MinLen) |
| 553 | return 0; |
| 554 | else if (MaxLen != 0 && inputLen > MaxLen) |
| 555 | return 0; |
| 556 | else |
| 557 | return (mAction ? mAction->NotifyTouch(TOUCH_RELEASE, mRenderX, mRenderY) : 1); |
| 558 | } |
| 559 | } |
| 560 | return 0; |
| 561 | } else { |
| 562 | if (key == 0) { |
| 563 | // Somewhat ugly hack-ish way to tell the box to redraw after losing focus to remove the cursor |
| 564 | mRendered = false; |
| 565 | return 1; |
| 566 | } |
| 567 | } |
| 568 | return 1; |
| 569 | } |