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