Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // console.cpp - GUIConsole object |
| 2 | |
| 3 | #include <stdarg.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <fcntl.h> |
| 8 | #include <sys/reboot.h> |
| 9 | #include <sys/stat.h> |
| 10 | #include <sys/time.h> |
| 11 | #include <sys/mman.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <sys/ioctl.h> |
| 14 | #include <time.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
| 17 | |
| 18 | #include <string> |
| 19 | |
| 20 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | |
| 29 | static std::vector<std::string> gConsole; |
| 30 | |
| 31 | extern "C" void gui_print(const char *fmt, ...) |
| 32 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 33 | char buf[512]; // We're going to limit a single request to 512 bytes |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | va_list ap; |
| 36 | va_start(ap, fmt); |
| 37 | vsnprintf(buf, 512, fmt, ap); |
| 38 | va_end(ap); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 39 | |
Dees_Troy | 32c8eb8 | 2012-09-11 15:28:06 -0400 | [diff] [blame] | 40 | fputs(buf, stdout); |
| 41 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 42 | char *start, *next; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 43 | |
| 44 | if (buf[0] == '\n' && strlen(buf) < 2) { |
| 45 | // This prevents the double lines bug seen in the console during zip installs |
| 46 | return; |
| 47 | } |
| 48 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 49 | for (start = next = buf; *next != '\0';) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | { |
| 51 | if (*next == '\n') |
| 52 | { |
| 53 | *next = '\0'; |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 54 | gConsole.push_back(start); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 55 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 56 | start = ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 57 | } |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 58 | else |
| 59 | ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 60 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 61 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 62 | // The text after last \n (or whole string if there is no \n) |
| 63 | if(*start) |
| 64 | gConsole.push_back(start); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 68 | GUIConsole::GUIConsole(xml_node<>* node) : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 69 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 70 | xml_attribute<>* attr; |
| 71 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 72 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 73 | mFont = NULL; |
| 74 | mCurrentLine = -1; |
| 75 | memset(&mForegroundColor, 255, sizeof(COLOR)); |
| 76 | memset(&mBackgroundColor, 0, sizeof(COLOR)); |
| 77 | mBackgroundColor.alpha = 255; |
| 78 | memset(&mScrollColor, 0x08, sizeof(COLOR)); |
| 79 | mScrollColor.alpha = 255; |
| 80 | mLastCount = 0; |
| 81 | mSlideout = 0; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 82 | RenderCount = 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 83 | mSlideoutState = hidden; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 84 | mRender = true; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 85 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 86 | mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | if (!node) |
| 89 | { |
| 90 | mSlideoutX = 0; mSlideoutY = 0; mSlideoutW = 0; mSlideoutH = 0; |
| 91 | mConsoleX = 0; mConsoleY = 0; mConsoleW = gr_fb_width(); mConsoleH = gr_fb_height(); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | child = node->first_node("font"); |
| 96 | if (child) |
| 97 | { |
| 98 | attr = child->first_attribute("resource"); |
| 99 | if (attr) |
| 100 | mFont = PageManager::FindResource(attr->value()); |
| 101 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 102 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 103 | child = node->first_node("color"); |
| 104 | if (child) |
| 105 | { |
| 106 | attr = child->first_attribute("foreground"); |
| 107 | if (attr) |
| 108 | { |
| 109 | std::string color = attr->value(); |
| 110 | ConvertStrToColor(color, &mForegroundColor); |
| 111 | } |
| 112 | attr = child->first_attribute("background"); |
| 113 | if (attr) |
| 114 | { |
| 115 | std::string color = attr->value(); |
| 116 | ConvertStrToColor(color, &mBackgroundColor); |
| 117 | } |
| 118 | attr = child->first_attribute("scroll"); |
| 119 | if (attr) |
| 120 | { |
| 121 | std::string color = attr->value(); |
| 122 | ConvertStrToColor(color, &mScrollColor); |
| 123 | } |
| 124 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 125 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 126 | // Load the placement |
| 127 | LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 129 | child = node->first_node("slideout"); |
| 130 | if (child) |
| 131 | { |
| 132 | mSlideout = 1; |
| 133 | LoadPlacement(child, &mSlideoutX, &mSlideoutY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 135 | attr = child->first_attribute("resource"); |
| 136 | if (attr) mSlideoutImage = PageManager::FindResource(attr->value()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | if (mSlideoutImage && mSlideoutImage->GetResource()) |
| 139 | { |
| 140 | mSlideoutW = gr_get_width(mSlideoutImage->GetResource()); |
| 141 | mSlideoutH = gr_get_height(mSlideoutImage->GetResource()); |
| 142 | } |
| 143 | } |
| 144 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 146 | gr_getFontDetails(mFont, &mFontHeight, NULL); |
| 147 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 148 | SetRenderPos(mConsoleX, mConsoleY); |
| 149 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | int GUIConsole::RenderSlideout(void) |
| 153 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | if (!mSlideoutImage || !mSlideoutImage->GetResource()) |
| 155 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 157 | gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY); |
| 158 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int GUIConsole::RenderConsole(void) |
| 162 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | void* fontResource = NULL; |
| 164 | if (mFont) |
| 165 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 167 | // We fill the background |
| 168 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 169 | gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 170 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 171 | gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha); |
| 172 | gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 173 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 174 | // Render the lines |
| 175 | gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 176 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 177 | // Don't try to continue to render without data |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 178 | int prevCount = mLastCount; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | mLastCount = gConsole.size(); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 180 | mRender = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 181 | if (mLastCount == 0) |
| 182 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 183 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 184 | // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped |
| 185 | // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping |
| 186 | // may different in different console windows |
| 187 | for (int i = prevCount; i < mLastCount; i++) { |
| 188 | string curr_line = gConsole[i]; |
| 189 | int line_char_width; |
| 190 | for(;;) { |
| 191 | line_char_width = gr_maxExW(curr_line.c_str(), fontResource, mConsoleW); |
| 192 | if (line_char_width < curr_line.size()) { |
| 193 | rConsole.push_back(curr_line.substr(0, line_char_width)); |
Ethan Yonker | 6b4276f | 2014-03-07 06:48:45 -0600 | [diff] [blame] | 194 | curr_line = curr_line.substr(line_char_width); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 195 | } else { |
| 196 | rConsole.push_back(curr_line); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | RenderCount = rConsole.size(); |
| 202 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 203 | // Find the start point |
| 204 | int start; |
| 205 | int curLine = mCurrentLine; // Thread-safing (Another thread updates this value) |
| 206 | if (curLine == -1) |
| 207 | { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 208 | start = RenderCount - mMaxRows; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 209 | } |
| 210 | else |
| 211 | { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 212 | if (curLine > (int) RenderCount) |
| 213 | curLine = (int) RenderCount; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 214 | if ((int) mMaxRows > curLine) |
| 215 | curLine = (int) mMaxRows; |
| 216 | start = curLine - mMaxRows; |
| 217 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 218 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 219 | unsigned int line; |
| 220 | for (line = 0; line < mMaxRows; line++) |
| 221 | { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 222 | if ((start + (int) line) >= 0 && (start + (int) line) < (int) RenderCount) |
| 223 | gr_textExW(mConsoleX, mStartY + (line * mFontHeight), rConsole[start + line].c_str(), fontResource, mConsoleW + mConsoleX); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 224 | } |
| 225 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | int GUIConsole::Render(void) |
| 229 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 230 | if(!isConditionTrue()) |
| 231 | return 0; |
| 232 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 233 | if (mSlideout && mSlideoutState == hidden) |
| 234 | return RenderSlideout(); |
| 235 | |
| 236 | return RenderConsole(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | int GUIConsole::Update(void) |
| 240 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 241 | if(!isConditionTrue()) |
| 242 | return 0; |
| 243 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 244 | if (mSlideout && mSlideoutState != visible) |
| 245 | { |
| 246 | if (mSlideoutState == hidden) |
| 247 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 248 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 249 | if (mSlideoutState == request_hide) |
| 250 | mSlideoutState = hidden; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 252 | if (mSlideoutState == request_show) |
| 253 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 254 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 255 | // Any time we activate the slider, we reset the position |
| 256 | mCurrentLine = -1; |
| 257 | return 2; |
| 258 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 259 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 260 | if (mCurrentLine == -1 && mLastCount != gConsole.size()) |
| 261 | { |
| 262 | // We can use Render, and return for just a flip |
| 263 | Render(); |
| 264 | return 2; |
| 265 | } |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 266 | else if (mRender) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 267 | { |
| 268 | // They're still touching, so re-render |
| 269 | Render(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 270 | return 2; |
| 271 | } |
| 272 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | int GUIConsole::SetRenderPos(int x, int y, int w, int h) |
| 276 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 277 | // Adjust the stub position accordingly |
| 278 | mSlideoutX += (x - mConsoleX); |
| 279 | mSlideoutY += (y - mConsoleY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 280 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 281 | mConsoleX = x; |
| 282 | mConsoleY = y; |
| 283 | if (w || h) |
| 284 | { |
| 285 | mConsoleW = w; |
| 286 | mConsoleH = h; |
| 287 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 288 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 289 | // Calculate the max rows |
| 290 | mMaxRows = mConsoleH / mFontHeight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 291 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 292 | // Adjust so we always fit to bottom |
| 293 | mStartY = mConsoleY + (mConsoleH % mFontHeight); |
| 294 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | // IsInRegion - Checks if the request is handled by this object |
| 298 | // Return 0 if this object handles the request, 1 if not |
| 299 | int GUIConsole::IsInRegion(int x, int y) |
| 300 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 301 | if (mSlideout) |
| 302 | { |
| 303 | // Check if they tapped the slideout button |
| 304 | if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) |
| 305 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 306 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 307 | // If we're only rendering the slideout, bail now |
| 308 | if (mSlideoutState == hidden) |
| 309 | return 0; |
| 310 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 311 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 312 | return (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) ? 0 : 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | // NotifyTouch - Notify of a touch event |
| 316 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 317 | int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 318 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 319 | if(!isConditionTrue()) |
| 320 | return -1; |
| 321 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 322 | if (mSlideout && mSlideoutState == hidden) |
| 323 | { |
| 324 | if (state == TOUCH_START) |
| 325 | { |
| 326 | mSlideoutState = request_show; |
| 327 | return 1; |
| 328 | } |
| 329 | } |
| 330 | else if (mSlideout && mSlideoutState == visible) |
| 331 | { |
| 332 | // Are we sliding it back in? |
| 333 | if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH)) |
| 334 | { |
| 335 | mSlideoutState = request_hide; |
| 336 | return 1; |
| 337 | } |
| 338 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 339 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 340 | // If we don't have enough lines to scroll, throw this away. |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 341 | if (RenderCount < mMaxRows) return 1; |
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 | // We are scrolling!!! |
| 344 | switch (state) |
| 345 | { |
| 346 | case TOUCH_START: |
| 347 | mLastTouchX = x; |
| 348 | mLastTouchY = y; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 349 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 350 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 351 | case TOUCH_DRAG: |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 352 | if (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) |
| 353 | break; // touch is outside of the console area -- do nothing |
| 354 | if (y > mLastTouchY + mFontHeight) { |
| 355 | while (y > mLastTouchY + mFontHeight) { |
| 356 | if (mCurrentLine == -1) |
| 357 | mCurrentLine = RenderCount - 1; |
| 358 | else if (mCurrentLine > mMaxRows) |
| 359 | mCurrentLine--; |
| 360 | mLastTouchY += mFontHeight; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 361 | } |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 362 | mRender = true; |
| 363 | } else if (y < mLastTouchY - mFontHeight) { |
| 364 | while (y < mLastTouchY - mFontHeight) { |
| 365 | if (mCurrentLine >= 0) |
| 366 | mCurrentLine++; |
| 367 | mLastTouchY -= mFontHeight; |
| 368 | } |
| 369 | if (mCurrentLine >= (int) RenderCount) |
| 370 | mCurrentLine = -1; |
| 371 | mRender = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 372 | } |
| 373 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 374 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 375 | case TOUCH_RELEASE: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 376 | mLastTouchY = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 377 | case TOUCH_REPEAT: |
| 378 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 382 | } |