blob: ed5c615ceb561e6774d6f991a6916bcd671cf26c [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_attribute<>* attr;
51 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 sAction = NULL;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060054 sSliderLabel = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 sSlider = NULL;
56 sSliderUsed = NULL;
57 sTouch = NULL;
58 sTouchW = 20;
Dees_Troy51a0e822012-09-05 15:24:24 -040059
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 if (!node)
61 {
62 LOGERR("GUISlider created without XML node\n");
63 return;
64 }
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Ethan Yonker21ff02a2015-02-18 14:35:00 -060066 // Load the resources
67 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 if (child)
69 {
thatf6ed8fc2015-02-14 20:23:16 +010070 sSlider = LoadAttrImage(child, "base");
71 sSliderUsed = LoadAttrImage(child, "used");
72 sTouch = LoadAttrImage(child, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020073 }
Dees_Troy51a0e822012-09-05 15:24:24 -040074
Ethan Yonker21ff02a2015-02-18 14:35:00 -060075 // Load the text label
76 sSliderLabel = new GUIText(node);
77 if (sSliderLabel->Render() < 0)
78 {
79 delete sSliderLabel;
80 sSliderLabel = NULL;
81 }
82
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060084 Placement TextPlacement = CENTER;
85 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &TextPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040086
thatf6ed8fc2015-02-14 20:23:16 +010087 mRenderW = sSlider->GetWidth();
88 mRenderH = sSlider->GetHeight();
Ethan Yonker591b9202015-03-11 11:17:15 -050089 if (TextPlacement == CENTER || TextPlacement == CENTER_X_ONLY) {
90 mRenderX = mRenderX - (mRenderW / 2);
91 if (TextPlacement == CENTER) {
92 mRenderY = mRenderY - (mRenderH / 2);
93 }
94 }
Ethan Yonker21ff02a2015-02-18 14:35:00 -060095 if (sSliderLabel) {
96 int sTextX = mRenderX + (mRenderW / 2);
97 int w, h;
98 sSliderLabel->GetCurrentBounds(w, h);
99 int sTextY = mRenderY + ((mRenderH - h) / 2);
100 sSliderLabel->SetRenderPos(sTextX, sTextY);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -0500101 sSliderLabel->SetMaxWidth(mRenderW);
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600102 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200103 if (sTouch && sTouch->GetResource())
104 {
thatf6ed8fc2015-02-14 20:23:16 +0100105 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
106 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 }
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -0400110 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 mActionY = mRenderY;
112 mActionW = mRenderW;
113 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 sCurTouchX = mRenderX;
118 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119}
120
121GUISlider::~GUISlider()
122{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 delete sAction;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600124 delete sSliderLabel;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125}
126
127int GUISlider::Render(void)
128{
Matt Mowera8a89d12016-12-30 18:10:37 -0600129 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100130 return 0;
131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 if (!sSlider || !sSlider->GetResource())
133 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 // Draw the slider
136 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 // Draw the used
139 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
140 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 // Draw the touch icon
143 if (sTouch && sTouch->GetResource())
144 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400145
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600146 if (sSliderLabel) {
147 int ret = sSliderLabel->Render();
148 if (ret < 0) return ret;
149 }
150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 sUpdate = 0;
152 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153}
154
155int GUISlider::Update(void)
156{
Matt Mowera8a89d12016-12-30 18:10:37 -0600157 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100158 return 0;
159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 if (sUpdate)
161 return 2;
162 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400163}
164
165int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
166{
Matt Mowera8a89d12016-12-30 18:10:37 -0600167 if (!isConditionTrue())
Vojtech Bocekede51c52014-02-07 23:58:09 +0100168 return -1;
169
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200170 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171
172 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173 {
174 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500175 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200176 y >= mRenderY && y <= mRenderY + mRenderH)
177 {
178 sCurTouchX = x - (sTouchW / 2);
179 if (sCurTouchX < mRenderX)
180 sCurTouchX = mRenderX;
181 dragging = true;
182 }
183 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400184
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 case TOUCH_DRAG:
186 if (!dragging)
187 return 0;
188 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
189 {
190 sCurTouchX = mRenderX;
191 dragging = false;
192 sUpdate = 1;
193 break;
194 }
195 sCurTouchX = x - (sTouchW / 2);
196 if (sCurTouchX < mRenderX)
197 sCurTouchX = mRenderX;
198 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
199 sCurTouchX = mRenderX + mRenderW - sTouchW;
200 sUpdate = 1;
201 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200203 case TOUCH_RELEASE:
204 if (!dragging)
205 return 0;
206
Ethan Yonker03db3262014-02-05 08:02:06 -0600207 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
208 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200209 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600210 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200211
212 sCurTouchX = mRenderX;
213 dragging = false;
214 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400215 case TOUCH_REPEAT:
216 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200217 break;
218 }
219 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400220}