blob: af7d07b8f60fbeaef10b2a62ea94f745409db127 [file] [log] [blame]
Dees_Troya13d74f2013-03-24 08:54:55 -05001/*
2 Copyright 2012 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*/
Dees_Troy51a0e822012-09-05 15:24:24 -040018
19#include <stdarg.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <fcntl.h>
24#include <sys/reboot.h>
25#include <sys/stat.h>
26#include <sys/time.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29#include <sys/ioctl.h>
30#include <time.h>
31#include <unistd.h>
32#include <stdlib.h>
33
34#include <string>
35
36extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000037#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040038#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039}
40
41#include "rapidxml.hpp"
42#include "objects.hpp"
43
44GUIButton::GUIButton(xml_node<>* node)
45 : Conditional(node)
46{
47 xml_attribute<>* attr;
48 xml_node<>* child;
49
50 mButtonImg = NULL;
51 mButtonIcon = NULL;
52 mButtonLabel = NULL;
53 mAction = NULL;
54 mRendered = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -060055 hasHighlightColor = false;
56 renderHighlight = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050057 hasFill = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
59 if (!node) return;
60
61 // Three of the four can be loaded directly from the node
62 mButtonImg = new GUIImage(node);
63 mButtonLabel = new GUIText(node);
64 mAction = new GUIAction(node);
65
66 if (mButtonImg->Render() < 0)
67 {
Dees_Troy51a0e822012-09-05 15:24:24 -040068 delete mButtonImg;
69 mButtonImg = NULL;
70 }
71 if (mButtonLabel->Render() < 0)
72 {
73 delete mButtonLabel;
74 mButtonLabel = NULL;
75 }
Dees_Troya13d74f2013-03-24 08:54:55 -050076 // Load fill if it exists
77 memset(&mFillColor, 0, sizeof(COLOR));
78 child = node->first_node("fill");
79 if (child)
80 {
81 attr = child->first_attribute("color");
82 if (attr) {
83 hasFill = true;
84 std::string color = attr->value();
85 ConvertStrToColor(color, &mFillColor);
86 }
87 }
88 if (!hasFill && mButtonImg == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000089 LOGERR("No image resource or fill specified for button.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050090 }
Dees_Troy51a0e822012-09-05 15:24:24 -040091
92 // The icon is a special case
93 child = node->first_node("icon");
94 if (child)
95 {
96 attr = child->first_attribute("resource");
97 if (attr)
98 mButtonIcon = PageManager::FindResource(attr->value());
99 }
100
Dees_Troy1a7a6672013-02-15 09:39:07 -0600101 memset(&mHighlightColor, 0, sizeof(COLOR));
102 child = node->first_node("highlight");
103 if (child) {
104 attr = child->first_attribute("color");
105 if (attr) {
106 hasHighlightColor = true;
107 std::string color = attr->value();
108 ConvertStrToColor(color, &mHighlightColor);
109 }
110 }
111
Dees_Troy51a0e822012-09-05 15:24:24 -0400112 int x, y, w, h;
Dees_Troya13d74f2013-03-24 08:54:55 -0500113 if (mButtonImg) {
114 mButtonImg->GetRenderPos(x, y, w, h);
115 } else if (hasFill) {
116 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
117 }
118 SetRenderPos(x, y, w, h);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119 return;
120}
121
122GUIButton::~GUIButton()
123{
124 if (mButtonImg) delete mButtonImg;
125 if (mButtonLabel) delete mButtonLabel;
126 if (mAction) delete mAction;
127 if (mButtonIcon) delete mButtonIcon;
128}
129
130int GUIButton::Render(void)
131{
132 if (!isConditionTrue())
133 {
134 mRendered = false;
135 return 0;
136 }
137
138 int ret = 0;
139
140 if (mButtonImg) ret = mButtonImg->Render();
141 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500142 if (hasFill) {
143 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
144 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
145 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400146 if (mButtonIcon && mButtonIcon->GetResource())
147 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
Dees_Troya13d74f2013-03-24 08:54:55 -0500148 if (mButtonLabel) {
149 int w, h;
150 mButtonLabel->GetCurrentBounds(w, h);
151 if (w != mTextW) {
152 mTextW = w;
153 // As a special case, we'll allow large text which automatically moves it to the right.
154 if (mTextW > mRenderW)
155 {
156 mTextX = mRenderW + mRenderX + 5;
157 mRenderW += mTextW + 5;
158 }
159 else
160 {
161 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
162 }
163 mButtonLabel->SetRenderPos(mTextX, mTextY);
164 }
165 ret = mButtonLabel->Render();
166 if (ret < 0) return ret;
167 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600168 if (renderHighlight && hasHighlightColor) {
169 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
170 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
171 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400172 mRendered = true;
173 return ret;
174}
175
176int GUIButton::Update(void)
177{
178 if (!isConditionTrue()) return (mRendered ? 2 : 0);
179 if (!mRendered) return 2;
180
181 int ret = 0, ret2 = 0;
182
Dees_Troy4d12f962012-10-19 13:13:15 -0400183 if (mButtonImg) ret = mButtonImg->Update();
Dees_Troy51a0e822012-09-05 15:24:24 -0400184 if (ret < 0) return ret;
185
186 if (ret == 0)
187 {
Dees_Troya13d74f2013-03-24 08:54:55 -0500188 if (mButtonLabel) {
189 ret2 = mButtonLabel->Update();
190 if (ret2 < 0) return ret2;
191 if (ret2 > ret) ret = ret2;
192 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400193 }
194 else if (ret == 1)
195 {
196 // The button re-rendered, so everyone else is a render
197 if (mButtonIcon && mButtonIcon->GetResource())
198 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
199 if (mButtonLabel) ret = mButtonLabel->Render();
200 if (ret < 0) return ret;
201 ret = 1;
202 }
203 else
204 {
205 // Aparently, the button needs a background update
206 ret = 2;
207 }
208 return ret;
209}
210
211int GUIButton::SetRenderPos(int x, int y, int w, int h)
212{
213 mRenderX = x;
214 mRenderY = y;
215 if (w || h)
216 {
217 mRenderW = w;
218 mRenderH = h;
219 }
220
221 mIconW = 0; mIconH = 0;
222 if (mButtonIcon && mButtonIcon->GetResource())
223 {
224 mIconW = gr_get_width(mButtonIcon->GetResource());
225 mIconH = gr_get_height(mButtonIcon->GetResource());
226 }
227
228 mTextH = 0;
229 mTextW = 0;
230 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
231 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
232 if (mTextW)
233 {
234 // As a special case, we'll allow large text which automatically moves it to the right.
235 if (mTextW > mRenderW)
236 {
237 mTextX = mRenderW + mRenderX + 5;
238 mRenderW += mTextW + 5;
239 }
240 else
241 {
242 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
243 }
244 }
245
246 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
247 {
248 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
249 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
250 }
251 else
252 {
253 int divisor = mRenderH - (mIconH + mTextH);
254 mIconY = mRenderY + (divisor / 3);
255 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
256 }
257
258 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
259 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
260 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
261 return 0;
262}
263
264int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
265{
Dees_Troy4d12f962012-10-19 13:13:15 -0400266 static int last_state = 0;
267
268 if (!isConditionTrue()) return -1;
269 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
270 if (last_state == 1) {
271 last_state = 0;
272 if (mButtonLabel != NULL)
273 mButtonLabel->isHighlighted = false;
274 if (mButtonImg != NULL)
275 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600276 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400277 mRendered = false;
278 }
279 } else {
280 if (last_state == 0) {
281 last_state = 1;
282 if (mButtonLabel != NULL)
283 mButtonLabel->isHighlighted = true;
284 if (mButtonImg != NULL)
285 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600286 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400287 mRendered = false;
288 }
289 }
290 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
291 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400292 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
293}
294