Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // console.cpp - GUIConsole object |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include <string> |
| 37 | |
| 38 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 39 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame^] | 41 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | |
| 43 | #include "rapidxml.hpp" |
| 44 | #include "objects.hpp" |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 45 | #include "gui.hpp" |
| 46 | #include "twmsg.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 48 | #define GUI_CONSOLE_BUFFER_SIZE 512 |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 50 | size_t last_message_count = 0; |
| 51 | std::vector<Message> gMessages; |
| 52 | |
| 53 | std::vector<std::string> gConsole; |
| 54 | std::vector<std::string> gConsoleColor; |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 55 | static FILE* ors_file; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 57 | extern "C" void __gui_print(const char *color, char *buf) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 59 | char *start, *next; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 60 | |
| 61 | if (buf[0] == '\n' && strlen(buf) < 2) { |
| 62 | // This prevents the double lines bug seen in the console during zip installs |
| 63 | return; |
| 64 | } |
| 65 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 66 | for (start = next = buf; *next != '\0';) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | { |
| 68 | if (*next == '\n') |
| 69 | { |
| 70 | *next = '\0'; |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 71 | gConsole.push_back(start); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 72 | gConsoleColor.push_back(color); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 74 | start = ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 75 | } |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 76 | else |
| 77 | ++next; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 78 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 79 | |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 80 | // 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] | 81 | if(*start) { |
Vojtech Bocek | d5b26d6 | 2014-03-06 22:56:13 +0100 | [diff] [blame] | 82 | gConsole.push_back(start); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 83 | gConsoleColor.push_back(color); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | extern "C" void gui_print(const char *fmt, ...) |
| 88 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 89 | char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 90 | |
| 91 | va_list ap; |
| 92 | va_start(ap, fmt); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 93 | vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 94 | va_end(ap); |
| 95 | |
| 96 | fputs(buf, stdout); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 97 | if (ors_file) { |
| 98 | fprintf(ors_file, "%s", buf); |
| 99 | fflush(ors_file); |
| 100 | } |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 101 | |
| 102 | __gui_print("normal", buf); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | extern "C" void gui_print_color(const char *color, const char *fmt, ...) |
| 107 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 108 | char buf[GUI_CONSOLE_BUFFER_SIZE]; // We're going to limit a single request to 512 bytes |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 109 | |
| 110 | va_list ap; |
| 111 | va_start(ap, fmt); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 112 | vsnprintf(buf, GUI_CONSOLE_BUFFER_SIZE, fmt, ap); |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 113 | va_end(ap); |
| 114 | |
| 115 | fputs(buf, stdout); |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 116 | if (ors_file) { |
| 117 | fprintf(ors_file, "%s", buf); |
| 118 | fflush(ors_file); |
| 119 | } |
Ethan Yonker | bf2cb1c | 2014-07-02 10:15:54 -0500 | [diff] [blame] | 120 | |
| 121 | __gui_print(color, buf); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 122 | return; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Ethan Yonker | 03a42f6 | 2014-08-08 11:03:51 -0500 | [diff] [blame] | 125 | extern "C" void gui_set_FILE(FILE* f) |
| 126 | { |
| 127 | ors_file = f; |
| 128 | } |
| 129 | |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 130 | void gui_msg(const char* text) |
| 131 | { |
| 132 | if (text) { |
| 133 | Message msg = Msg(text); |
| 134 | gui_msg(msg); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | void gui_warn(const char* text) |
| 139 | { |
| 140 | if (text) { |
| 141 | Message msg = Msg(msg::kWarning, text); |
| 142 | gui_msg(msg); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | void gui_err(const char* text) |
| 147 | { |
| 148 | if (text) { |
| 149 | Message msg = Msg(msg::kError, text); |
| 150 | gui_msg(msg); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void gui_highlight(const char* text) |
| 155 | { |
| 156 | if (text) { |
| 157 | Message msg = Msg(msg::kHighlight, text); |
| 158 | gui_msg(msg); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | void gui_msg(Message msg) |
| 163 | { |
| 164 | std::string output = msg; |
| 165 | output += "\n"; |
| 166 | fputs(output.c_str(), stdout); |
| 167 | if (ors_file) { |
| 168 | fprintf(ors_file, "%s", output.c_str()); |
| 169 | fflush(ors_file); |
| 170 | } |
| 171 | gMessages.push_back(msg); |
| 172 | } |
| 173 | |
| 174 | void GUIConsole::Translate_Now() { |
| 175 | size_t message_count = gMessages.size(); |
| 176 | if (message_count <= last_message_count) |
| 177 | return; |
| 178 | |
| 179 | for (size_t m = last_message_count; m < message_count; m++) { |
| 180 | std::string message = gMessages[m]; |
| 181 | std::string color = "normal"; |
| 182 | if (gMessages[m].GetKind() == msg::kError) |
| 183 | color = "error"; |
| 184 | else if (gMessages[m].GetKind() == msg::kHighlight) |
| 185 | color = "highlight"; |
| 186 | else if (gMessages[m].GetKind() == msg::kWarning) |
| 187 | color = "warning"; |
| 188 | gConsole.push_back(message); |
| 189 | gConsoleColor.push_back(color); |
| 190 | } |
| 191 | last_message_count = message_count; |
| 192 | } |
| 193 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 194 | GUIConsole::GUIConsole(xml_node<>* node) : GUIScrollList(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 195 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 196 | xml_node<>* child; |
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 | mLastCount = 0; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 199 | scrollToEnd = true; |
| 200 | mSlideoutX = mSlideoutY = mSlideoutW = mSlideoutH = 0; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 201 | mSlideout = 0; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 202 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 203 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 204 | allowSelection = false; // console doesn't support list item selections |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 206 | if (!node) |
| 207 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 208 | mRenderX = 0; mRenderY = 0; mRenderW = gr_fb_width(); mRenderH = gr_fb_height(); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 209 | } |
| 210 | else |
| 211 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 212 | child = FindNode(node, "color"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 213 | if (child) |
| 214 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 215 | mFontColor = LoadAttrColor(child, "foreground", mFontColor); |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 216 | mBackgroundColor = LoadAttrColor(child, "background", mBackgroundColor); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 217 | //mScrollColor = LoadAttrColor(child, "scroll", mScrollColor); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 219 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 220 | child = FindNode(node, "slideout"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 221 | if (child) |
| 222 | { |
| 223 | mSlideout = 1; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 224 | mSlideoutState = hidden; |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 225 | LoadPlacement(child, &mSlideoutX, &mSlideoutY, &mSlideoutW, &mSlideoutH, &mPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 226 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 227 | mSlideoutImage = LoadAttrImage(child, "resource"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 228 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 229 | if (mSlideoutImage && mSlideoutImage->GetResource()) |
| 230 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 231 | mSlideoutW = mSlideoutImage->GetWidth(); |
| 232 | mSlideoutH = mSlideoutImage->GetHeight(); |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 233 | if (mPlacement == CENTER || mPlacement == CENTER_X_ONLY) { |
| 234 | mSlideoutX = mSlideoutX - (mSlideoutW / 2); |
| 235 | if (mPlacement == CENTER) { |
| 236 | mSlideoutY = mSlideoutY - (mSlideoutH / 2); |
| 237 | } |
| 238 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int GUIConsole::RenderSlideout(void) |
| 245 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 246 | if (!mSlideoutImage || !mSlideoutImage->GetResource()) |
| 247 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 248 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 249 | gr_blit(mSlideoutImage->GetResource(), 0, 0, mSlideoutW, mSlideoutH, mSlideoutX, mSlideoutY); |
| 250 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 251 | } |
| 252 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 253 | int GUIConsole::RenderConsole(void) |
| 254 | { |
Ethan Yonker | 74db157 | 2015-10-28 12:44:49 -0500 | [diff] [blame] | 255 | Translate_Now(); |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 256 | AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 257 | GUIScrollList::Render(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 258 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 259 | // if last line is fully visible, keep tracking the last line when new lines are added |
| 260 | int bottom_offset = GetDisplayRemainder() - actualItemHeight; |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 261 | bool isAtBottom = firstDisplayedItem == (int)GetItemCount() - GetDisplayItemCount() - (bottom_offset != 0) && y_offset == bottom_offset; |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 262 | if (isAtBottom) |
| 263 | scrollToEnd = true; |
| 264 | #if 0 |
| 265 | // debug - show if we are tracking the last line |
| 266 | if (scrollToEnd) { |
| 267 | gr_color(0,255,0,255); |
| 268 | gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5); |
| 269 | } else { |
| 270 | gr_color(255,0,0,255); |
| 271 | gr_fill(mRenderX+mRenderW-5, mRenderY+mRenderH-5, 5, 5); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 272 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 273 | #endif |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 274 | return (mSlideout ? RenderSlideout() : 0); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int GUIConsole::Render(void) |
| 278 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 279 | if(!isConditionTrue()) |
| 280 | return 0; |
| 281 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 282 | if (mSlideout && mSlideoutState == hidden) |
| 283 | return RenderSlideout(); |
| 284 | |
| 285 | return RenderConsole(); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int GUIConsole::Update(void) |
| 289 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 290 | if (mSlideout && mSlideoutState != visible) |
| 291 | { |
| 292 | if (mSlideoutState == hidden) |
| 293 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 294 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 295 | if (mSlideoutState == request_hide) |
| 296 | mSlideoutState = hidden; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 297 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 298 | if (mSlideoutState == request_show) |
| 299 | mSlideoutState = visible; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 300 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 301 | // Any time we activate the console, we reset the position |
| 302 | SetVisibleListLocation(rConsole.size() - 1); |
| 303 | mUpdate = 1; |
| 304 | scrollToEnd = true; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 305 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 306 | |
Ethan Yonker | 44925ad | 2015-07-22 12:33:59 -0500 | [diff] [blame] | 307 | if (AddLines(&gConsole, &gConsoleColor, &mLastCount, &rConsole, &rConsoleColor)) { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 308 | // someone added new text |
| 309 | // at least the scrollbar must be updated, even if the new lines are currently not visible |
| 310 | mUpdate = 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 311 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 312 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 313 | if (scrollToEnd) { |
| 314 | // keep the last line in view |
| 315 | SetVisibleListLocation(rConsole.size() - 1); |
| 316 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 317 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 318 | GUIScrollList::Update(); |
| 319 | |
| 320 | if (mUpdate) { |
| 321 | mUpdate = 0; |
| 322 | if (Render() == 0) |
| 323 | return 2; |
| 324 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 325 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // IsInRegion - Checks if the request is handled by this object |
that | f8194e2 | 2015-01-29 01:05:01 +0100 | [diff] [blame] | 329 | // Return 1 if this object handles the request, 0 if not |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 330 | int GUIConsole::IsInRegion(int x, int y) |
| 331 | { |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 332 | if (mSlideout) { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 333 | // Check if they tapped the slideout button |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 334 | if (x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 335 | return 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 336 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 337 | // If we're only rendering the slideout, bail now |
| 338 | if (mSlideoutState == hidden) |
| 339 | return 0; |
| 340 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 341 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 342 | return GUIScrollList::IsInRegion(x, y); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | // NotifyTouch - Notify of a touch event |
| 346 | // Return 0 on success, >0 to ignore remainder of touch, and <0 on error |
| 347 | int GUIConsole::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 348 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 349 | if(!isConditionTrue()) |
| 350 | return -1; |
| 351 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 352 | if (mSlideout && x >= mSlideoutX && x < mSlideoutX + mSlideoutW && y >= mSlideoutY && y < mSlideoutY + mSlideoutH) { |
| 353 | if (state == TOUCH_START) { |
| 354 | if (mSlideoutState == hidden) |
| 355 | mSlideoutState = request_show; |
| 356 | else if (mSlideoutState == visible) |
| 357 | mSlideoutState = request_hide; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 358 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 359 | return 1; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 360 | } |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 361 | scrollToEnd = false; |
| 362 | return GUIScrollList::NotifyTouch(state, x, y); |
| 363 | } |
| 364 | |
| 365 | size_t GUIConsole::GetItemCount() |
| 366 | { |
| 367 | return rConsole.size(); |
| 368 | } |
| 369 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 370 | void GUIConsole::RenderItem(size_t itemindex, int yPos, bool selected __unused) |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 371 | { |
| 372 | // Set the color for the font |
| 373 | if (rConsoleColor[itemindex] == "normal") { |
| 374 | gr_color(mFontColor.red, mFontColor.green, mFontColor.blue, mFontColor.alpha); |
| 375 | } else { |
| 376 | COLOR FontColor; |
| 377 | std::string color = rConsoleColor[itemindex]; |
| 378 | ConvertStrToColor(color, &FontColor); |
| 379 | FontColor.alpha = 255; |
| 380 | gr_color(FontColor.red, FontColor.green, FontColor.blue, FontColor.alpha); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 381 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 382 | |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 383 | // render text |
| 384 | const char* text = rConsole[itemindex].c_str(); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 385 | gr_textEx_scaleW(mRenderX, yPos, text, mFont->GetResource(), mRenderW, TOP_LEFT, 0); |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 386 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 387 | |
Ethan Yonker | d0514ba | 2015-10-22 14:17:47 -0500 | [diff] [blame] | 388 | void GUIConsole::NotifySelect(size_t item_selected __unused) |
that | 8d46c09 | 2015-02-26 01:30:04 +0100 | [diff] [blame] | 389 | { |
| 390 | // do nothing - console ignores selections |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 391 | } |