blob: a3884474b27be4f91efd61ca3fc8749f8a2ee2d1 [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#include "../minuitwrp/minui.h"
Dees_Troy51a0e822012-09-05 15:24:24 -040025}
26
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);
83 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 if (sTouch && sTouch->GetResource())
85 {
thatf6ed8fc2015-02-14 20:23:16 +010086 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
87 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 }
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040091 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 mActionY = mRenderY;
93 mActionW = mRenderW;
94 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040095
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020096 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 sCurTouchX = mRenderX;
99 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100}
101
102GUISlider::~GUISlider()
103{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200104 delete sAction;
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600105 delete sSliderLabel;
Dees_Troy51a0e822012-09-05 15:24:24 -0400106}
107
108int GUISlider::Render(void)
109{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100110 if(!isConditionTrue())
111 return 0;
112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 if (!sSlider || !sSlider->GetResource())
114 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 // Draw the slider
117 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400118
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200119 // Draw the used
120 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
121 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 // Draw the touch icon
124 if (sTouch && sTouch->GetResource())
125 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400126
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600127 if (sSliderLabel) {
128 int ret = sSliderLabel->Render();
129 if (ret < 0) return ret;
130 }
131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 sUpdate = 0;
133 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400134}
135
136int GUISlider::Update(void)
137{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100138 if(!isConditionTrue())
139 return 0;
140
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200141 if (sUpdate)
142 return 2;
143 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400144}
145
146int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
147{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100148 if(!isConditionTrue())
149 return -1;
150
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400152
153 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 {
155 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500156 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200157 y >= mRenderY && y <= mRenderY + mRenderH)
158 {
159 sCurTouchX = x - (sTouchW / 2);
160 if (sCurTouchX < mRenderX)
161 sCurTouchX = mRenderX;
162 dragging = true;
163 }
164 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400165
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200166 case TOUCH_DRAG:
167 if (!dragging)
168 return 0;
169 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
170 {
171 sCurTouchX = mRenderX;
172 dragging = false;
173 sUpdate = 1;
174 break;
175 }
176 sCurTouchX = x - (sTouchW / 2);
177 if (sCurTouchX < mRenderX)
178 sCurTouchX = mRenderX;
179 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
180 sCurTouchX = mRenderX + mRenderW - sTouchW;
181 sUpdate = 1;
182 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400183
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 case TOUCH_RELEASE:
185 if (!dragging)
186 return 0;
187
Ethan Yonker03db3262014-02-05 08:02:06 -0600188 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
189 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200190 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600191 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200192
193 sCurTouchX = mRenderX;
194 dragging = false;
195 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400196 case TOUCH_REPEAT:
197 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200198 break;
199 }
200 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400201}