blob: 5ba8182a1563aaa245aec8184c23894ff1918f9e [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" {
37#include "../common.h"
38#include "../minuitwrp/minui.h"
39#include "../recovery_ui.h"
40}
41
42#include "rapidxml.hpp"
43#include "objects.hpp"
44
45GUIButton::GUIButton(xml_node<>* node)
46 : Conditional(node)
47{
48 xml_attribute<>* attr;
49 xml_node<>* child;
50
51 mButtonImg = NULL;
52 mButtonIcon = NULL;
53 mButtonLabel = NULL;
54 mAction = NULL;
55 mRendered = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -060056 hasHighlightColor = false;
57 renderHighlight = false;
Dees_Troya13d74f2013-03-24 08:54:55 -050058 hasFill = false;
Dees_Troy51a0e822012-09-05 15:24:24 -040059
60 if (!node) return;
61
62 // Three of the four can be loaded directly from the node
63 mButtonImg = new GUIImage(node);
64 mButtonLabel = new GUIText(node);
65 mAction = new GUIAction(node);
66
67 if (mButtonImg->Render() < 0)
68 {
Dees_Troy51a0e822012-09-05 15:24:24 -040069 delete mButtonImg;
70 mButtonImg = NULL;
71 }
72 if (mButtonLabel->Render() < 0)
73 {
74 delete mButtonLabel;
75 mButtonLabel = NULL;
76 }
Dees_Troya13d74f2013-03-24 08:54:55 -050077 // Load fill if it exists
78 memset(&mFillColor, 0, sizeof(COLOR));
79 child = node->first_node("fill");
80 if (child)
81 {
82 attr = child->first_attribute("color");
83 if (attr) {
84 hasFill = true;
85 std::string color = attr->value();
86 ConvertStrToColor(color, &mFillColor);
87 }
88 }
89 if (!hasFill && mButtonImg == NULL) {
90 LOGE("No image resource or fill specified for button.\n");
91 }
Dees_Troy51a0e822012-09-05 15:24:24 -040092
93 // The icon is a special case
94 child = node->first_node("icon");
95 if (child)
96 {
97 attr = child->first_attribute("resource");
98 if (attr)
99 mButtonIcon = PageManager::FindResource(attr->value());
100 }
101
Dees_Troy1a7a6672013-02-15 09:39:07 -0600102 memset(&mHighlightColor, 0, sizeof(COLOR));
103 child = node->first_node("highlight");
104 if (child) {
105 attr = child->first_attribute("color");
106 if (attr) {
107 hasHighlightColor = true;
108 std::string color = attr->value();
109 ConvertStrToColor(color, &mHighlightColor);
110 }
111 }
112
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 int x, y, w, h;
Dees_Troya13d74f2013-03-24 08:54:55 -0500114 if (mButtonImg) {
115 mButtonImg->GetRenderPos(x, y, w, h);
116 } else if (hasFill) {
117 LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
118 }
119 SetRenderPos(x, y, w, h);
Dees_Troy51a0e822012-09-05 15:24:24 -0400120 return;
121}
122
123GUIButton::~GUIButton()
124{
125 if (mButtonImg) delete mButtonImg;
126 if (mButtonLabel) delete mButtonLabel;
127 if (mAction) delete mAction;
128 if (mButtonIcon) delete mButtonIcon;
129}
130
131int GUIButton::Render(void)
132{
133 if (!isConditionTrue())
134 {
135 mRendered = false;
136 return 0;
137 }
138
139 int ret = 0;
140
141 if (mButtonImg) ret = mButtonImg->Render();
142 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500143 if (hasFill) {
144 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
145 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
146 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400147 if (mButtonIcon && mButtonIcon->GetResource())
148 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
Dees_Troya13d74f2013-03-24 08:54:55 -0500149 if (mButtonLabel) {
150 int w, h;
151 mButtonLabel->GetCurrentBounds(w, h);
152 if (w != mTextW) {
153 mTextW = w;
154 // As a special case, we'll allow large text which automatically moves it to the right.
155 if (mTextW > mRenderW)
156 {
157 mTextX = mRenderW + mRenderX + 5;
158 mRenderW += mTextW + 5;
159 }
160 else
161 {
162 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
163 }
164 mButtonLabel->SetRenderPos(mTextX, mTextY);
165 }
166 ret = mButtonLabel->Render();
167 if (ret < 0) return ret;
168 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600169 if (renderHighlight && hasHighlightColor) {
170 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
171 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
172 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400173 mRendered = true;
174 return ret;
175}
176
177int GUIButton::Update(void)
178{
179 if (!isConditionTrue()) return (mRendered ? 2 : 0);
180 if (!mRendered) return 2;
181
182 int ret = 0, ret2 = 0;
183
Dees_Troy4d12f962012-10-19 13:13:15 -0400184 if (mButtonImg) ret = mButtonImg->Update();
Dees_Troy51a0e822012-09-05 15:24:24 -0400185 if (ret < 0) return ret;
186
187 if (ret == 0)
188 {
Dees_Troya13d74f2013-03-24 08:54:55 -0500189 if (mButtonLabel) {
190 ret2 = mButtonLabel->Update();
191 if (ret2 < 0) return ret2;
192 if (ret2 > ret) ret = ret2;
193 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400194 }
195 else if (ret == 1)
196 {
197 // The button re-rendered, so everyone else is a render
198 if (mButtonIcon && mButtonIcon->GetResource())
199 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
200 if (mButtonLabel) ret = mButtonLabel->Render();
201 if (ret < 0) return ret;
202 ret = 1;
203 }
204 else
205 {
206 // Aparently, the button needs a background update
207 ret = 2;
208 }
209 return ret;
210}
211
212int GUIButton::SetRenderPos(int x, int y, int w, int h)
213{
214 mRenderX = x;
215 mRenderY = y;
216 if (w || h)
217 {
218 mRenderW = w;
219 mRenderH = h;
220 }
221
222 mIconW = 0; mIconH = 0;
223 if (mButtonIcon && mButtonIcon->GetResource())
224 {
225 mIconW = gr_get_width(mButtonIcon->GetResource());
226 mIconH = gr_get_height(mButtonIcon->GetResource());
227 }
228
229 mTextH = 0;
230 mTextW = 0;
231 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
232 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
233 if (mTextW)
234 {
235 // As a special case, we'll allow large text which automatically moves it to the right.
236 if (mTextW > mRenderW)
237 {
238 mTextX = mRenderW + mRenderX + 5;
239 mRenderW += mTextW + 5;
240 }
241 else
242 {
243 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
244 }
245 }
246
247 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
248 {
249 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
250 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
251 }
252 else
253 {
254 int divisor = mRenderH - (mIconH + mTextH);
255 mIconY = mRenderY + (divisor / 3);
256 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
257 }
258
259 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
260 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
261 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
262 return 0;
263}
264
265int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
266{
Dees_Troy4d12f962012-10-19 13:13:15 -0400267 static int last_state = 0;
268
269 if (!isConditionTrue()) return -1;
270 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
271 if (last_state == 1) {
272 last_state = 0;
273 if (mButtonLabel != NULL)
274 mButtonLabel->isHighlighted = false;
275 if (mButtonImg != NULL)
276 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600277 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400278 mRendered = false;
279 }
280 } else {
281 if (last_state == 0) {
282 last_state = 1;
283 if (mButtonLabel != NULL)
284 mButtonLabel->isHighlighted = true;
285 if (mButtonImg != NULL)
286 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600287 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400288 mRendered = false;
289 }
290 }
291 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
292 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400293 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
294}
295