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