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 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 132 | mFont = LoadAttrFont(child, "resource"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 133 | } |
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 | child = node->first_node("color"); |
| 136 | if (child) |
| 137 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 138 | mForegroundColor = LoadAttrColor(child, "foreground", mForegroundColor); |
| 139 | mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor); |
| 140 | mScrollColor = LoadAttrColor(child, "scroll", mScrollColor); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 142 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 143 | // Load the placement |
| 144 | LoadPlacement(node->first_node("placement"), &mConsoleX, &mConsoleY, &mConsoleW, &mConsoleH); |
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 | child = node->first_node("slideout"); |
| 147 | if (child) |
| 148 | { |
| 149 | mSlideout = 1; |
| 150 | LoadPlacement(child, &mSlideoutX, &mSlideoutY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 151 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 152 | mSlideoutImage = LoadAttrImage(child, "resource"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | if (mSlideoutImage && mSlideoutImage->GetResource()) |
| 155 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 156 | mSlideoutW = mSlideoutImage->GetWidth(); |
| 157 | mSlideoutH = mSlideoutImage->GetHeight(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 161 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 162 | mFontHeight = mFont->GetHeight(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH); |
| 164 | SetRenderPos(mConsoleX, mConsoleY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int GUIConsole::RenderSlideout(void) |
| 168 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 169 | if (!mSlideoutImage || !mSlideoutImage->GetResource()) |
| 170 | return -1; |
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 | gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY); |
| 173 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int GUIConsole::RenderConsole(void) |
| 177 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 178 | void* fontResource = NULL; |
| 179 | if (mFont) |
| 180 | fontResource = mFont->GetResource(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 181 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 182 | // We fill the background |
| 183 | gr_color(mBackgroundColor.red, mBackgroundColor.green, mBackgroundColor.blue, 255); |
| 184 | gr_fill(mConsoleX, mConsoleY, mConsoleW, mConsoleH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 185 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 186 | gr_color(mScrollColor.red, mScrollColor.green, mScrollColor.blue, mScrollColor.alpha); |
| 187 | gr_fill(mConsoleX + (mConsoleW * 9 / 10), mConsoleY, (mConsoleW / 10), mConsoleH); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 188 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 189 | // Don't try to continue to render without data |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 190 | size_t prevCount = mLastCount; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 191 | mLastCount = gConsole.size(); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 192 | mRender = false; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 193 | if (mLastCount == 0) |
| 194 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 196 | // Due to word wrap, figure out what / how the newly added text needs to be added to the render vector that is word wrapped |
| 197 | // Note, that multiple consoles on different GUI pages may be different widths or use different fonts, so the word wrapping |
| 198 | // may different in different console windows |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 199 | for (size_t i = prevCount; i < mLastCount; i++) { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 200 | string curr_line = gConsole[i]; |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 201 | string curr_color = gConsoleColor[i]; |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 202 | for(;;) { |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 203 | size_t line_char_width = gr_maxExW(curr_line.c_str(), fontResource, mConsoleW); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 204 | if (line_char_width < curr_line.size()) { |
| 205 | rConsole.push_back(curr_line.substr(0, line_char_width)); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 206 | rConsoleColor.push_back(curr_color); |
Ethan Yonker | 6b4276f | 2014-03-07 06:48:45 -0600 | [diff] [blame] | 207 | curr_line = curr_line.substr(line_char_width); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 208 | } else { |
| 209 | rConsole.push_back(curr_line); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 210 | rConsoleColor.push_back(curr_color); |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | RenderCount = rConsole.size(); |
| 216 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 217 | // Find the start point |
| 218 | int start; |
| 219 | int curLine = mCurrentLine; // Thread-safing (Another thread updates this value) |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 220 | if (curLine == -1) // follow tail |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 221 | { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 222 | start = RenderCount - mMaxRows; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 223 | } |
| 224 | else |
| 225 | { |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 226 | if (curLine > (int) RenderCount) |
| 227 | curLine = (int) RenderCount; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 228 | if ((int) mMaxRows > curLine) |
| 229 | curLine = (int) mMaxRows; |
| 230 | start = curLine - mMaxRows; |
| 231 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 232 | |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 233 | // note: start can be negative here |
| 234 | for (int line = 0; line < mMaxRows; line++) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 235 | { |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 236 | int index = start + line; |
| 237 | if (index >= 0 && index < (int) RenderCount) { |
| 238 | if (rConsoleColor[index] == "normal") { |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 239 | gr_color(mForegroundColor.red, mForegroundColor.green, mForegroundColor.blue, mForegroundColor.alpha); |
| 240 | } else { |
| 241 | COLOR mFontColor; |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 242 | std::string color = rConsoleColor[index]; |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 243 | ConvertStrToColor(color, &mFontColor); |
| 244 | mFontColor.alpha = 255; |
| 245 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 246 | } |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 247 | gr_textExW(mConsoleX, mStartY + (line * mFontHeight), rConsole[index].c_str(), fontResource, mConsoleW + mConsoleX); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 248 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 249 | } |
| 250 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | int GUIConsole::Render(void) |
| 254 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 255 | if(!isConditionTrue()) |
| 256 | return 0; |
| 257 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 258 | if (mSlideout && mSlideoutState == hidden) |
| 259 | return RenderSlideout(); |
| 260 | |
| 261 | return RenderConsole(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int GUIConsole::Update(void) |
| 265 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 266 | if(!isConditionTrue()) |
| 267 | return 0; |
| 268 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 269 | if (mSlideout && mSlideoutState != visible) |
| 270 | { |
| 271 | if (mSlideoutState == hidden) |
| 272 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 273 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 274 | if (mSlideoutState == request_hide) |
| 275 | mSlideoutState = hidden; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 276 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 277 | if (mSlideoutState == request_show) |
| 278 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 279 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 280 | // Any time we activate the slider, we reset the position |
| 281 | mCurrentLine = -1; |
| 282 | return 2; |
| 283 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 284 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 285 | if (mCurrentLine == -1 && mLastCount != gConsole.size()) |
| 286 | { |
| 287 | // We can use Render, and return for just a flip |
| 288 | Render(); |
| 289 | return 2; |
| 290 | } |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 291 | else if (mRender) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 292 | { |
| 293 | // They're still touching, so re-render |
| 294 | Render(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 295 | return 2; |
| 296 | } |
| 297 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | int GUIConsole::SetRenderPos(int x, int y, int w, int h) |
| 301 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 302 | // Adjust the stub position accordingly |
| 303 | mSlideoutX += (x - mConsoleX); |
| 304 | mSlideoutY += (y - mConsoleY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 305 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 306 | mConsoleX = x; |
| 307 | mConsoleY = y; |
| 308 | if (w || h) |
| 309 | { |
| 310 | mConsoleW = w; |
| 311 | mConsoleH = h; |
| 312 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 313 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 314 | // Calculate the max rows |
| 315 | mMaxRows = mConsoleH / mFontHeight; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 316 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 317 | // Adjust so we always fit to bottom |
| 318 | mStartY = mConsoleY + (mConsoleH % mFontHeight); |
| 319 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | // IsInRegion - Checks if the request is handled by this object |
that | f8194e2 | 2015-01-29 01:05:01 +0100 | [diff] [blame] | 323 | // Return 1 if this object handles the request, 0 if not |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 324 | int GUIConsole::IsInRegion(int x, int y) |
| 325 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 326 | if (mSlideout) |
| 327 | { |
| 328 | // Check if they tapped the slideout button |
| 329 | if (x >= mSlideoutX && x <= mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) |
| 330 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 331 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 332 | // If we're only rendering the slideout, bail now |
| 333 | if (mSlideoutState == hidden) |
| 334 | return 0; |
| 335 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | |
that | f8194e2 | 2015-01-29 01:05:01 +0100 | [diff] [blame] | 337 | 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] | 338 | } |
| 339 | |
| 340 | // NotifyTouch - Notify of a touch event |
| 341 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 342 | int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 343 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 344 | if(!isConditionTrue()) |
| 345 | return -1; |
| 346 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 347 | if (mSlideout && mSlideoutState == hidden) |
| 348 | { |
| 349 | if (state == TOUCH_START) |
| 350 | { |
| 351 | mSlideoutState = request_show; |
| 352 | return 1; |
| 353 | } |
| 354 | } |
| 355 | else if (mSlideout && mSlideoutState == visible) |
| 356 | { |
| 357 | // Are we sliding it back in? |
| 358 | if (state == TOUCH_START && x > mSlideoutX && x < (mSlideoutX + mSlideoutW) && y > mSlideoutY && y < (mSlideoutY + mSlideoutH)) |
| 359 | { |
| 360 | mSlideoutState = request_hide; |
| 361 | return 1; |
| 362 | } |
| 363 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 364 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 365 | // If we don't have enough lines to scroll, throw this away. |
that | 05360ba | 2015-02-06 00:58:16 +0100 | [diff] [blame] | 366 | if ((int)RenderCount < mMaxRows) return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 367 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 368 | // We are scrolling!!! |
| 369 | switch (state) |
| 370 | { |
| 371 | case TOUCH_START: |
| 372 | mLastTouchX = x; |
| 373 | mLastTouchY = y; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 374 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 375 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 376 | case TOUCH_DRAG: |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 377 | if (x < mConsoleX || x > mConsoleX + mConsoleW || y < mConsoleY || y > mConsoleY + mConsoleH) |
| 378 | break; // touch is outside of the console area -- do nothing |
| 379 | if (y > mLastTouchY + mFontHeight) { |
| 380 | while (y > mLastTouchY + mFontHeight) { |
| 381 | if (mCurrentLine == -1) |
| 382 | mCurrentLine = RenderCount - 1; |
| 383 | else if (mCurrentLine > mMaxRows) |
| 384 | mCurrentLine--; |
| 385 | mLastTouchY += mFontHeight; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 386 | } |
Dees Troy | 31218ec | 2014-02-25 20:35:56 +0000 | [diff] [blame] | 387 | mRender = true; |
| 388 | } else if (y < mLastTouchY - mFontHeight) { |
| 389 | while (y < mLastTouchY - mFontHeight) { |
| 390 | if (mCurrentLine >= 0) |
| 391 | mCurrentLine++; |
| 392 | mLastTouchY -= mFontHeight; |
| 393 | } |
| 394 | if (mCurrentLine >= (int) RenderCount) |
| 395 | mCurrentLine = -1; |
| 396 | mRender = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 397 | } |
| 398 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 399 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 400 | case TOUCH_RELEASE: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 401 | mLastTouchY = -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 402 | case TOUCH_REPEAT: |
| 403 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 404 | break; |
| 405 | } |
| 406 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 407 | } |