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