blob: 18b5560c4dea6fd4e1eb5d4102ac18bea5666ef9 [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#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040040}
41
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);
Dees_Troy51a0e822012-09-05 15:24:24 -040096}
97
98GUIButton::~GUIButton()
99{
thatf6ed8fc2015-02-14 20:23:16 +0100100 delete mButtonImg;
101 delete mButtonLabel;
102 delete mAction;
Dees_Troy51a0e822012-09-05 15:24:24 -0400103}
104
105int GUIButton::Render(void)
106{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 if (!isConditionTrue())
108 {
109 mRendered = false;
110 return 0;
111 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 int ret = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 if (mButtonImg) ret = mButtonImg->Render();
116 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500117 if (hasFill) {
118 gr_color(mFillColor.red, mFillColor.green, mFillColor.blue, mFillColor.alpha);
119 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
120 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 if (mButtonIcon && mButtonIcon->GetResource())
122 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
123 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500124 int w, h;
125 mButtonLabel->GetCurrentBounds(w, h);
126 if (w != mTextW) {
127 mTextW = w;
Dees Troyb21cc642013-09-10 17:36:41 +0000128 if (TextPlacement == CENTER_X_ONLY) {
129 mTextX = ((mRenderW - mRenderX) / 2);
130 } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right.
Dees_Troya13d74f2013-03-24 08:54:55 -0500131 mTextX = mRenderW + mRenderX + 5;
132 mRenderW += mTextW + 5;
Dees Troyb21cc642013-09-10 17:36:41 +0000133 } else {
Dees_Troya13d74f2013-03-24 08:54:55 -0500134 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
135 }
136 mButtonLabel->SetRenderPos(mTextX, mTextY);
137 }
138 ret = mButtonLabel->Render();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200139 if (ret < 0) return ret;
Dees_Troya13d74f2013-03-24 08:54:55 -0500140 }
Dees_Troy1a7a6672013-02-15 09:39:07 -0600141 if (renderHighlight && hasHighlightColor) {
142 gr_color(mHighlightColor.red, mHighlightColor.green, mHighlightColor.blue, mHighlightColor.alpha);
143 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
144 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 mRendered = true;
146 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400147}
148
149int GUIButton::Update(void)
150{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 if (!isConditionTrue()) return (mRendered ? 2 : 0);
152 if (!mRendered) return 2;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 int ret = 0, ret2 = 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 if (mButtonImg) ret = mButtonImg->Update();
157 if (ret < 0) return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400158
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200159 if (ret == 0)
160 {
161 if (mButtonLabel) {
Dees_Troya13d74f2013-03-24 08:54:55 -0500162 ret2 = mButtonLabel->Update();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 if (ret2 < 0) return ret2;
164 if (ret2 > ret) ret = ret2;
Dees_Troya13d74f2013-03-24 08:54:55 -0500165 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 }
167 else if (ret == 1)
168 {
169 // The button re-rendered, so everyone else is a render
170 if (mButtonIcon && mButtonIcon->GetResource())
171 gr_blit(mButtonIcon->GetResource(), 0, 0, mIconW, mIconH, mIconX, mIconY);
172 if (mButtonLabel) ret = mButtonLabel->Render();
173 if (ret < 0) return ret;
174 ret = 1;
175 }
176 else
177 {
178 // Aparently, the button needs a background update
179 ret = 2;
180 }
181 return ret;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182}
183
184int GUIButton::SetRenderPos(int x, int y, int w, int h)
185{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186 mRenderX = x;
187 mRenderY = y;
188 if (w || h)
189 {
190 mRenderW = w;
191 mRenderH = h;
192 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400193
thatf6ed8fc2015-02-14 20:23:16 +0100194 mIconW = mButtonIcon->GetWidth();
195 mIconH = mButtonIcon->GetHeight();
Dees_Troy51a0e822012-09-05 15:24:24 -0400196
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200197 mTextH = 0;
198 mTextW = 0;
199 mIconX = mRenderX + ((mRenderW - mIconW) / 2);
200 if (mButtonLabel) mButtonLabel->GetCurrentBounds(mTextW, mTextH);
201 if (mTextW)
202 {
Dees Troyb21cc642013-09-10 17:36:41 +0000203 if (TextPlacement == CENTER_X_ONLY) {
204 mTextX = ((mRenderW - mRenderX) / 2);
205 } else if (mTextW > mRenderW) { // As a special case, we'll allow large text which automatically moves it to the right.
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 mTextX = mRenderW + mRenderX + 5;
207 mRenderW += mTextW + 5;
Dees Troyb21cc642013-09-10 17:36:41 +0000208 } else {
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 mTextX = mRenderX + ((mRenderW - mTextW) / 2);
210 }
211 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400212
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200213 if (mIconH == 0 || mTextH == 0 || mIconH + mTextH > mRenderH)
214 {
215 mIconY = mRenderY + (mRenderH / 2) - (mIconH / 2);
216 mTextY = mRenderY + (mRenderH / 2) - (mTextH / 2);
217 }
218 else
219 {
220 int divisor = mRenderH - (mIconH + mTextH);
221 mIconY = mRenderY + (divisor / 3);
222 mTextY = mRenderY + (divisor * 2 / 3) + mIconH;
223 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400224
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200225 if (mButtonLabel) mButtonLabel->SetRenderPos(mTextX, mTextY);
226 if (mAction) mAction->SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
227 SetActionPos(mRenderX, mRenderY, mRenderW, mRenderH);
228 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400229}
230
231int GUIButton::NotifyTouch(TOUCH_STATE state, int x, int y)
232{
Dees_Troy4d12f962012-10-19 13:13:15 -0400233 static int last_state = 0;
234
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200235 if (!isConditionTrue()) return -1;
Dees_Troy4d12f962012-10-19 13:13:15 -0400236 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH || state == TOUCH_RELEASE) {
237 if (last_state == 1) {
238 last_state = 0;
239 if (mButtonLabel != NULL)
240 mButtonLabel->isHighlighted = false;
241 if (mButtonImg != NULL)
242 mButtonImg->isHighlighted = false;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600243 renderHighlight = false;
Dees_Troy4d12f962012-10-19 13:13:15 -0400244 mRendered = false;
245 }
246 } else {
247 if (last_state == 0) {
248 last_state = 1;
Ethan Yonker03db3262014-02-05 08:02:06 -0600249 DataManager::Vibrate("tw_button_vibrate");
Dees_Troy4d12f962012-10-19 13:13:15 -0400250 if (mButtonLabel != NULL)
251 mButtonLabel->isHighlighted = true;
252 if (mButtonImg != NULL)
253 mButtonImg->isHighlighted = true;
Dees_Troy1a7a6672013-02-15 09:39:07 -0600254 renderHighlight = true;
Dees_Troy4d12f962012-10-19 13:13:15 -0400255 mRendered = false;
256 }
257 }
258 if (x < mRenderX || x - mRenderX > mRenderW || y < mRenderY || y - mRenderY > mRenderH)
259 return 0;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200260 return (mAction ? mAction->NotifyTouch(state, x, y) : 1);
Dees_Troy51a0e822012-09-05 15:24:24 -0400261}