blob: 59b295bd06f34ba55b95e265bba78239e108312d [file] [log] [blame]
Dees_Troy51a0e822012-09-05 15:24:24 -04001// slider.cpp - GUISlider object
2// Pulled & ported from https://raw.github.com/agrabren/RecoverWin/master/gui/slider.cpp
3
4#include <stdarg.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <fcntl.h>
9#include <sys/reboot.h>
10#include <sys/stat.h>
11#include <sys/time.h>
12#include <sys/mman.h>
13#include <sys/types.h>
14#include <sys/ioctl.h>
15#include <time.h>
16#include <unistd.h>
17#include <stdlib.h>
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +000018#include "../data.hpp"
Dees_Troy51a0e822012-09-05 15:24:24 -040019
20#include <string>
21
22extern "C" {
Dees_Troy2673cec2013-04-02 20:22:16 +000023#include "../twcommon.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040024}
Ethan Yonkerfbb43532015-12-28 21:54:50 +010025#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040026
27#include "rapidxml.hpp"
28#include "objects.hpp"
29
Vojtech Bocekede51c52014-02-07 23:58:09 +010030GUISlider::GUISlider(xml_node<>* node) : GUIObject(node)
Dees_Troy51a0e822012-09-05 15:24:24 -040031{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020032 xml_attribute<>* attr;
33 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040034
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020035 sAction = NULL;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060036 sSliderLabel = NULL;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020037 sSlider = NULL;
38 sSliderUsed = NULL;
39 sTouch = NULL;
40 sTouchW = 20;
Dees_Troy51a0e822012-09-05 15:24:24 -040041
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020042 if (!node)
43 {
44 LOGERR("GUISlider created without XML node\n");
45 return;
46 }
Dees_Troy51a0e822012-09-05 15:24:24 -040047
Ethan Yonker21ff02a2015-02-18 14:35:00 -060048 // Load the resources
49 child = FindNode(node, "resource");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020050 if (child)
51 {
thatf6ed8fc2015-02-14 20:23:16 +010052 sSlider = LoadAttrImage(child, "base");
53 sSliderUsed = LoadAttrImage(child, "used");
54 sTouch = LoadAttrImage(child, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 }
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Ethan Yonker21ff02a2015-02-18 14:35:00 -060057 // Load the text label
58 sSliderLabel = new GUIText(node);
59 if (sSliderLabel->Render() < 0)
60 {
61 delete sSliderLabel;
62 sSliderLabel = NULL;
63 }
64
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 // Load the placement
Ethan Yonker21ff02a2015-02-18 14:35:00 -060066 Placement TextPlacement = CENTER;
67 LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &TextPlacement);
Dees_Troy51a0e822012-09-05 15:24:24 -040068
thatf6ed8fc2015-02-14 20:23:16 +010069 mRenderW = sSlider->GetWidth();
70 mRenderH = sSlider->GetHeight();
Ethan Yonker591b9202015-03-11 11:17:15 -050071 if (TextPlacement == CENTER || TextPlacement == CENTER_X_ONLY) {
72 mRenderX = mRenderX - (mRenderW / 2);
73 if (TextPlacement == CENTER) {
74 mRenderY = mRenderY - (mRenderH / 2);
75 }
76 }
Ethan Yonker21ff02a2015-02-18 14:35:00 -060077 if (sSliderLabel) {
78 int sTextX = mRenderX + (mRenderW / 2);
79 int w, h;
80 sSliderLabel->GetCurrentBounds(w, h);
81 int sTextY = mRenderY + ((mRenderH - h) / 2);
82 sSliderLabel->SetRenderPos(sTextX, sTextY);
Ethan Yonkerb7a54a32015-10-05 10:16:27 -050083 sSliderLabel->SetMaxWidth(mRenderW);
Ethan Yonker21ff02a2015-02-18 14:35:00 -060084 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 if (sTouch && sTouch->GetResource())
86 {
thatf6ed8fc2015-02-14 20:23:16 +010087 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
88 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020089 }
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040092 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020093 mActionY = mRenderY;
94 mActionW = mRenderW;
95 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040096
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020097 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 sCurTouchX = mRenderX;
100 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101}
102
103GUISlider::~GUISlider()
104{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 delete sAction;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600106 delete sSliderLabel;
Dees_Troy51a0e822012-09-05 15:24:24 -0400107}
108
109int GUISlider::Render(void)
110{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100111 if(!isConditionTrue())
112 return 0;
113
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200114 if (!sSlider || !sSlider->GetResource())
115 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 // Draw the slider
118 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400119
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200120 // Draw the used
121 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
122 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 // Draw the touch icon
125 if (sTouch && sTouch->GetResource())
126 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400127
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600128 if (sSliderLabel) {
129 int ret = sSliderLabel->Render();
130 if (ret < 0) return ret;
131 }
132
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200133 sUpdate = 0;
134 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135}
136
137int GUISlider::Update(void)
138{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100139 if(!isConditionTrue())
140 return 0;
141
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200142 if (sUpdate)
143 return 2;
144 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400145}
146
147int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
148{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100149 if(!isConditionTrue())
150 return -1;
151
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200152 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
154 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 {
156 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500157 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 y >= mRenderY && y <= mRenderY + mRenderH)
159 {
160 sCurTouchX = x - (sTouchW / 2);
161 if (sCurTouchX < mRenderX)
162 sCurTouchX = mRenderX;
163 dragging = true;
164 }
165 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 case TOUCH_DRAG:
168 if (!dragging)
169 return 0;
170 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
171 {
172 sCurTouchX = mRenderX;
173 dragging = false;
174 sUpdate = 1;
175 break;
176 }
177 sCurTouchX = x - (sTouchW / 2);
178 if (sCurTouchX < mRenderX)
179 sCurTouchX = mRenderX;
180 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
181 sCurTouchX = mRenderX + mRenderW - sTouchW;
182 sUpdate = 1;
183 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400184
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200185 case TOUCH_RELEASE:
186 if (!dragging)
187 return 0;
188
Ethan Yonker03db3262014-02-05 08:02:06 -0600189 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
190 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200191 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600192 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200193
194 sCurTouchX = mRenderX;
195 dragging = false;
196 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400197 case TOUCH_REPEAT:
198 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200199 break;
200 }
201 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400202}