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