blob: fab5a2ea0c25d6492e751912d0c24dea34b4e5ae [file] [log] [blame]
Matt Mowere04eee72016-12-31 00:38:57 -06001/*
2 Copyright 2017 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*/
18
Dees_Troy51a0e822012-09-05 15:24:24 -040019// slider.cpp - GUISlider object
20// Pulled & ported from https://raw.github.com/agrabren/RecoverWin/master/gui/slider.cpp
21
22#include <stdarg.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26#include <fcntl.h>
27#include <sys/reboot.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/mman.h>
31#include <sys/types.h>
32#include <sys/ioctl.h>
33#include <time.h>
34#include <unistd.h>
35#include <stdlib.h>
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000036#include "../data.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040037
38#include <string>
39
40extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000041#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040042}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010043#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040044
45#include "rapidxml.hpp"
46#include "objects.hpp"
47
Vojtech Bocekede51c52014-02-07 23:58:09 +010048GUISlider::GUISlider(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040049{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040051
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020052 sAction = NULL;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060053 sSliderLabel = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 sSlider = NULL;
55 sSliderUsed = NULL;
56 sTouch = NULL;
57 sTouchW = 20;
Dees_Troy51a0e822012-09-05 15:24:24 -040058
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020059 if (!node)
60 {
61 LOGERR("GUISlider created without XML node\n");
62 return;
63 }
Dees_Troy51a0e822012-09-05 15:24:24 -040064
Ethan Yonker21ff02a2015-02-18 14:35:00 -060065 // Load the resources
66 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020067 if (child)
68 {
thatf6ed8fc2015-02-14 20:23:16 +010069 sSlider = LoadAttrImage(child, "base");
70 sSliderUsed = LoadAttrImage(child, "used");
71 sTouch = LoadAttrImage(child, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 }
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Ethan Yonker21ff02a2015-02-18 14:35:00 -060074 // Load the text label
75 sSliderLabel = new GUIText(node);
76 if (sSliderLabel->Render() < 0)
77 {
78 delete sSliderLabel;
79 sSliderLabel = NULL;
80 }
81
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020082 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060083 Placement TextPlacement = CENTER;
84 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &TextPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040085
Ethan Yonker58f21322018-08-24 11:17:36 -050086 if (sSlider && sSlider->GetResource()) {
87 mRenderW = sSlider->GetWidth();
88 mRenderH = sSlider->GetHeight();
89 } else {
90 mRenderW = mRenderH = 0;
91 }
Ethan Yonker591b9202015-03-11 11:17:15 -050092 if (TextPlacement == CENTER || TextPlacement == CENTER_X_ONLY) {
93 mRenderX = mRenderX - (mRenderW / 2);
94 if (TextPlacement == CENTER) {
95 mRenderY = mRenderY - (mRenderH / 2);
96 }
97 }
Ethan Yonker21ff02a2015-02-18 14:35:00 -060098 if (sSliderLabel) {
99 int sTextX = mRenderX + (mRenderW / 2);
100 int w, h;
101 sSliderLabel->GetCurrentBounds(w, h);
102 int sTextY = mRenderY + ((mRenderH - h) / 2);
103 sSliderLabel->SetRenderPos(sTextX, sTextY);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500104 sSliderLabel->SetMaxWidth(mRenderW);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600105 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 if (sTouch && sTouch->GetResource())
107 {
thatf6ed8fc2015-02-14 20:23:16 +0100108 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
109 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400111
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200112 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -0400113 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 mActionY = mRenderY;
115 mActionW = mRenderW;
116 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -0400117
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200118 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 sCurTouchX = mRenderX;
121 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122}
123
124GUISlider::~GUISlider()
125{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 delete sAction;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600127 delete sSliderLabel;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUISlider::Render(void)
131{
Matt Mowera8a89d12016-12-30 18:10:37 -0600132 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100133 return 0;
134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (!sSlider || !sSlider->GetResource())
136 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 // Draw the slider
139 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 // Draw the used
142 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
143 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 // Draw the touch icon
146 if (sTouch && sTouch->GetResource())
147 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400148
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600149 if (sSliderLabel) {
150 int ret = sSliderLabel->Render();
151 if (ret < 0) return ret;
152 }
153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 sUpdate = 0;
155 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400156}
157
158int GUISlider::Update(void)
159{
Matt Mowera8a89d12016-12-30 18:10:37 -0600160 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100161 return 0;
162
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200163 if (sUpdate)
164 return 2;
165 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166}
167
168int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
169{
Matt Mowera8a89d12016-12-30 18:10:37 -0600170 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100171 return -1;
172
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400174
175 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 {
177 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500178 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200179 y >= mRenderY && y <= mRenderY + mRenderH)
180 {
181 sCurTouchX = x - (sTouchW / 2);
182 if (sCurTouchX < mRenderX)
183 sCurTouchX = mRenderX;
184 dragging = true;
185 }
186 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400187
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200188 case TOUCH_DRAG:
189 if (!dragging)
190 return 0;
191 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
192 {
193 sCurTouchX = mRenderX;
194 dragging = false;
195 sUpdate = 1;
196 break;
197 }
198 sCurTouchX = x - (sTouchW / 2);
199 if (sCurTouchX < mRenderX)
200 sCurTouchX = mRenderX;
201 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
202 sCurTouchX = mRenderX + mRenderW - sTouchW;
203 sUpdate = 1;
204 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400205
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200206 case TOUCH_RELEASE:
207 if (!dragging)
208 return 0;
209
Ethan Yonker03db3262014-02-05 08:02:06 -0600210 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
211 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200212 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600213 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200214
215 sCurTouchX = mRenderX;
216 dragging = false;
217 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400218 case TOUCH_REPEAT:
219 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200220 break;
221 }
222 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400223}