blob: 2fd114d88f6fd97304596e03c47e958ef59adedd [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 Yonker21ff02a2015-02-18 14:35:00 -060071 if (sSliderLabel) {
72 int sTextX = mRenderX + (mRenderW / 2);
73 int w, h;
74 sSliderLabel->GetCurrentBounds(w, h);
75 int sTextY = mRenderY + ((mRenderH - h) / 2);
76 sSliderLabel->SetRenderPos(sTextX, sTextY);
77 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 if (sTouch && sTouch->GetResource())
79 {
thatf6ed8fc2015-02-14 20:23:16 +010080 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
81 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020082 }
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040085 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020086 mActionY = mRenderY;
87 mActionW = mRenderW;
88 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040089
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040091
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020092 sCurTouchX = mRenderX;
93 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040094}
95
96GUISlider::~GUISlider()
97{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 delete sAction;
Ethan Yonker21ff02a2015-02-18 14:35:00 -060099 delete sSliderLabel;
Dees_Troy51a0e822012-09-05 15:24:24 -0400100}
101
102int GUISlider::Render(void)
103{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100104 if(!isConditionTrue())
105 return 0;
106
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200107 if (!sSlider || !sSlider->GetResource())
108 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 // Draw the slider
111 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 // Draw the used
114 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
115 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400116
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200117 // Draw the touch icon
118 if (sTouch && sTouch->GetResource())
119 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400120
Ethan Yonker21ff02a2015-02-18 14:35:00 -0600121 if (sSliderLabel) {
122 int ret = sSliderLabel->Render();
123 if (ret < 0) return ret;
124 }
125
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 sUpdate = 0;
127 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400128}
129
130int GUISlider::Update(void)
131{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100132 if(!isConditionTrue())
133 return 0;
134
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 if (sUpdate)
136 return 2;
137 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400138}
139
140int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
141{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100142 if(!isConditionTrue())
143 return -1;
144
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200145 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
147 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200148 {
149 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500150 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200151 y >= mRenderY && y <= mRenderY + mRenderH)
152 {
153 sCurTouchX = x - (sTouchW / 2);
154 if (sCurTouchX < mRenderX)
155 sCurTouchX = mRenderX;
156 dragging = true;
157 }
158 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400159
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 case TOUCH_DRAG:
161 if (!dragging)
162 return 0;
163 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
164 {
165 sCurTouchX = mRenderX;
166 dragging = false;
167 sUpdate = 1;
168 break;
169 }
170 sCurTouchX = x - (sTouchW / 2);
171 if (sCurTouchX < mRenderX)
172 sCurTouchX = mRenderX;
173 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
174 sCurTouchX = mRenderX + mRenderW - sTouchW;
175 sUpdate = 1;
176 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400177
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200178 case TOUCH_RELEASE:
179 if (!dragging)
180 return 0;
181
Ethan Yonker03db3262014-02-05 08:02:06 -0600182 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
183 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200184 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600185 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200186
187 sCurTouchX = mRenderX;
188 dragging = false;
189 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400190 case TOUCH_REPEAT:
191 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200192 break;
193 }
194 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400195}