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