blob: 45614e461d3a7cac2602a71407ccfdec7918c126 [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>
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000033#include "../data.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040034
35#include <string>
36
37extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000038#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040039}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010040#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040041
42#include "rapidxml.hpp"
43#include "objects.hpp"
44
45GUIButton::GUIButton(xml_node<>* node)
Vojtech Bocekede51c52014-02-07 23:58:09 +010046 : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040047{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020048 xml_attribute<>* attr;
49 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040050
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020051 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
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 if (!node) return;
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Dees Troy4168a072013-11-29 05:01:51 +000062 // These can be loaded directly from the node
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063 mButtonLabel = new GUIText(node);
64 mAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Ethan Yonker21ff02a2015-02-18 14:35:00 -060066 mButtonImg = new GUIImage(node);
67 if (mButtonImg->Render() < 0)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 {
Ethan Yonker21ff02a2015-02-18 14:35:00 -060069 delete mButtonImg;
70 mButtonImg = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020071 }
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
Ethan Yonker21ff02a2015-02-18 14:35:00 -060078 mFillColor = LoadAttrColor(FindNode(node, "fill"), "color", &hasFill);
Dees_Troya13d74f2013-03-24 08:54:55 -050079 if (!hasFill && mButtonImg == NULL) {
Dees_Troy2673cec2013-04-02 20:22:16 +000080 LOGERR("No image resource or fill specified for button.\n");
Dees_Troya13d74f2013-03-24 08:54:55 -050081 }
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 // The icon is a special case
Ethan Yonker21ff02a2015-02-18 14:35:00 -060084 mButtonIcon = LoadAttrImage(FindNode(node, "icon"), "resource");
Dees_Troy51a0e822012-09-05 15:24:24 -040085
Ethan Yonker21ff02a2015-02-18 14:35:00 -060086 mHighlightColor = LoadAttrColor(FindNode(node, "highlight"), "color", &hasHighlightColor);
Dees_Troy1a7a6672013-02-15 09:39:07 -060087
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 int x, y, w, h;
Dees Troyb21cc642013-09-10 17:36:41 +000089 TextPlacement = TOP_LEFT;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 if (mButtonImg) {
Dees_Troya13d74f2013-03-24 08:54:55 -050091 mButtonImg->GetRenderPos(x, y, w, h);
92 } else if (hasFill) {
Ethan Yonker21ff02a2015-02-18 14:35:00 -060093 LoadPlacement(FindNode(node, "placement"), &x, &y, &w, &h, &TextPlacement);
Dees_Troya13d74f2013-03-24 08:54:55 -050094 }
95 SetRenderPos(x, y, w, h);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050096 if (mButtonLabel) {
97 TextPlacement = (Placement)LoadAttrInt(FindNode(node, "placement"), "textplacement", TOP_LEFT);
98 if (TextPlacement != TEXT_ONLY_RIGHT) {
99 mButtonLabel->scaleWidth = 1;
100 mButtonLabel->SetMaxWidth(w);
101 mButtonLabel->SetPlacement(CENTER);
102 mTextX = ((mRenderW / 2) + mRenderX);
103 mTextY = mRenderY + (mRenderH / 2);
104 mButtonLabel->SetRenderPos(mTextX, mTextY);
105 } else {
106 mTextX = mRenderW + mRenderX + 5;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500107 mButtonLabel->GetCurrentBounds(mTextW, mTextH);
Ethan Yonkerfd52b982015-11-06 15:24:26 -0600108 mRenderW += mTextW + 5;
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500109 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
110 mButtonLabel->SetRenderPos(mTextX, mTextY);
Ethan Yonkerfd52b982015-11-06 15:24:26 -0600111 if (mAction)
112 mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
113 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500114 }
115 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400116}
117
118GUIButton::~GUIButton()
119{
thatf6ed8fc2015-02-14 20:23:16 +0100120 delete mButtonImg;
121 delete mButtonLabel;
122 delete mAction;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123}
124
125int GUIButton::Render(void)
126{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 if (!isConditionTrue())
128 {
129 mRendered = false;
130 return 0;
131 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400132
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (mButtonImg) ret = mButtonImg->Render();
136 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500137 if (hasFill) {
138 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
139 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
140 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (mButtonIcon && mButtonIcon->GetResource())
142 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
143 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500144 int w, h;
145 mButtonLabel->GetCurrentBounds(w, h);
146 if (w != mTextW) {
147 mTextW = w;
Dees_Troya13d74f2013-03-24 08:54:55 -0500148 }
149 ret = mButtonLabel->Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200150 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500151 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600152 if (renderHighlight && hasHighlightColor) {
153 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
154 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
155 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 mRendered = true;
157 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158}
159
160int GUIButton::Update(void)
161{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162 if (!isConditionTrue()) return (mRendered ? 2 : 0);
163 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 int ret = 0, ret2 = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 if (mButtonImg) ret = mButtonImg->Update();
168 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400169
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200170 if (ret == 0)
171 {
172 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500173 ret2 = mButtonLabel->Update();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200174 if (ret2 < 0) return ret2;
175 if (ret2 > ret) ret = ret2;
Dees_Troya13d74f2013-03-24 08:54:55 -0500176 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200177 }
178 else if (ret == 1)
179 {
180 // The button re-rendered, so everyone else is a render
181 if (mButtonIcon && mButtonIcon->GetResource())
182 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
183 if (mButtonLabel) ret = mButtonLabel->Render();
184 if (ret < 0) return ret;
185 ret = 1;
186 }
187 else
188 {
189 // Aparently, the button needs a background update
190 ret = 2;
191 }
192 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400193}
194
195int GUIButton::SetRenderPos(int x, int y, int w, int h)
196{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 mRenderX = x;
198 mRenderY = y;
199 if (w || h)
200 {
201 mRenderW = w;
202 mRenderH = h;
203 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400204
thatf6ed8fc2015-02-14 20:23:16 +0100205 mIconW = mButtonIcon->GetWidth();
206 mIconH = mButtonIcon->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400207
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200208 mTextH = 0;
209 mTextW = 0;
210 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
211 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500212 if (mTextW && TextPlacement == TEXT_ONLY_RIGHT)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 {
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500214 mRenderW += mTextW + 5;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200215 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400216
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
218 {
219 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 }
221 else
222 {
223 int divisor = mRenderH - (mIconH + mTextH);
224 mIconY = mRenderY + (divisor / 3);
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400226
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200227 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
228 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
229 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
230 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400231}
232
233int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
234{
Dees_Troy4d12f962012-10-19 13:13:15 -0400235 static int last_state = 0;
236
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200237 if (!isConditionTrue()) return -1;
Dees_Troy4d12f962012-10-19 13:13:15 -0400238 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
239 if (last_state == 1) {
240 last_state = 0;
241 if (mButtonLabel != NULL)
242 mButtonLabel->isHighlighted = false;
243 if (mButtonImg != NULL)
244 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600245 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400246 mRendered = false;
247 }
248 } else {
249 if (last_state == 0) {
250 last_state = 1;
Ethan Yonker03db3262014-02-05 08:02:06 -0600251 DataManager::Vibrate("tw_button_vibrate");
Dees_Troy4d12f962012-10-19 13:13:15 -0400252 if (mButtonLabel != NULL)
253 mButtonLabel->isHighlighted = true;
254 if (mButtonImg != NULL)
255 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600256 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400257 mRendered = false;
258 }
259 }
260 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
261 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200262 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400263}