1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
Copyright 2012 bigbiff/Dees_Troy TeamWin
This file is part of TWRP/TeamWin Recovery Project.
TWRP is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
TWRP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TWRP. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/reboot.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string>
extern "C" {
#include "../common.h"
#include "../minuitwrp/minui.h"
#include "../recovery_ui.h"
}
#include "rapidxml.hpp"
#include "objects.hpp"
GUIButton::GUIButton(xml_node<>* node)
: Conditional(node)
{
xml_attribute<>* attr;
xml_node<>* child;
mButtonImg = NULL;
mButtonIcon = NULL;
mButtonLabel = NULL;
mAction = NULL;
mRendered = false;
hasHighlightColor = false;
renderHighlight = false;
hasFill = false;
if (!node) return;
// Three of the four can be loaded directly from the node
mButtonImg = new GUIImage(node);
mButtonLabel = new GUIText(node);
mAction = new GUIAction(node);
if (mButtonImg->Render() < 0)
{
delete mButtonImg;
mButtonImg = NULL;
}
if (mButtonLabel->Render() < 0)
{
delete mButtonLabel;
mButtonLabel = NULL;
}
// Load fill if it exists
memset(&mFillColor, 0, sizeof(COLOR));
child = node->first_node("fill");
if (child)
{
attr = child->first_attribute("color");
if (attr) {
hasFill = true;
std::string color = attr->value();
ConvertStrToColor(color, &mFillColor);
}
}
if (!hasFill && mButtonImg == NULL) {
LOGE("No image resource or fill specified for button.\n");
}
// The icon is a special case
child = node->first_node("icon");
if (child)
{
attr = child->first_attribute("resource");
if (attr)
mButtonIcon = PageManager::FindResource(attr->value());
}
memset(&mHighlightColor, 0, sizeof(COLOR));
child = node->first_node("highlight");
if (child) {
attr = child->first_attribute("color");
if (attr) {
hasHighlightColor = true;
std::string color = attr->value();
ConvertStrToColor(color, &mHighlightColor);
}
}
int x, y, w, h;
if (mButtonImg) {
mButtonImg->GetRenderPos(x, y, w, h);
} else if (hasFill) {
LoadPlacement(node->first_node("placement"), &x, &y, &w, &h);
}
SetRenderPos(x, y, w, h);
return;
}
GUIButton::~GUIButton()
{
if (mButtonImg) delete mButtonImg;
if (mButtonLabel) delete mButtonLabel;
if (mAction) delete mAction;
if (mButtonIcon) delete mButtonIcon;
}
int GUIButton::Render(void)
{
if (!isConditionTrue())
{
mRendered = false;
return 0;
}
int ret = 0;
if (mButtonImg) ret = mButtonImg->Render();
if (ret < 0) return ret;
if (hasFill) {
gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
}
if (mButtonIcon && mButtonIcon->GetResource())
gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
if (mButtonLabel) {
int w, h;
mButtonLabel->GetCurrentBounds(w, h);
if (w != mTextW) {
mTextW = w;
// As a special case, we'll allow large text which automatically moves it to the right.
if (mTextW > mRenderW)
{
mTextX = mRenderW + mRenderX + 5;
mRenderW += mTextW + 5;
}
else
{
mTextX = mRenderX + ((mRenderW - mTextW) / 2);
}
mButtonLabel->SetRenderPos(mTextX, mTextY);
}
ret = mButtonLabel->Render();
if (ret < 0) return ret;
}
if (renderHighlight && hasHighlightColor) {
gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
}
mRendered = true;
return ret;
}
int GUIButton::Update(void)
{
if (!isConditionTrue()) return (mRendered ? 2 : 0);
if (!mRendered) return 2;
int ret = 0, ret2 = 0;
if (mButtonImg) ret = mButtonImg->Update();
if (ret < 0) return ret;
if (ret == 0)
{
if (mButtonLabel) {
ret2 = mButtonLabel->Update();
if (ret2 < 0) return ret2;
if (ret2 > ret) ret = ret2;
}
}
else if (ret == 1)
{
// The button re-rendered, so everyone else is a render
if (mButtonIcon && mButtonIcon->GetResource())
gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
if (mButtonLabel) ret = mButtonLabel->Render();
if (ret < 0) return ret;
ret = 1;
}
else
{
// Aparently, the button needs a background update
ret = 2;
}
return ret;
}
int GUIButton::SetRenderPos(int x, int y, int w, int h)
{
mRenderX = x;
mRenderY = y;
if (w || h)
{
mRenderW = w;
mRenderH = h;
}
mIconW = 0; mIconH = 0;
if (mButtonIcon && mButtonIcon->GetResource())
{
mIconW = gr_get_width(mButtonIcon->GetResource());
mIconH = gr_get_height(mButtonIcon->GetResource());
}
mTextH = 0;
mTextW = 0;
mIconX = mRenderX + ((mRenderW - mIconW) / 2);
if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
if (mTextW)
{
// As a special case, we'll allow large text which automatically moves it to the right.
if (mTextW > mRenderW)
{
mTextX = mRenderW + mRenderX + 5;
mRenderW += mTextW + 5;
}
else
{
mTextX = mRenderX + ((mRenderW - mTextW) / 2);
}
}
if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
{
mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
}
else
{
int divisor = mRenderH - (mIconH + mTextH);
mIconY = mRenderY + (divisor / 3);
mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
}
if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
return 0;
}
int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
{
static int last_state = 0;
if (!isConditionTrue()) return -1;
if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
if (last_state == 1) {
last_state = 0;
if (mButtonLabel != NULL)
mButtonLabel->isHighlighted = false;
if (mButtonImg != NULL)
mButtonImg->isHighlighted = false;
renderHighlight = false;
mRendered = false;
}
} else {
if (last_state == 0) {
last_state = 1;
if (mButtonLabel != NULL)
mButtonLabel->isHighlighted = true;
if (mButtonImg != NULL)
mButtonImg->isHighlighted = true;
renderHighlight = true;
mRendered = false;
}
}
if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
return 0;
return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
}
|