blob: 692676561c35042c5633a1a5578e2424a9f09d06 [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;
36 sSlider = NULL;
37 sSliderUsed = NULL;
38 sTouch = NULL;
39 sTouchW = 20;
Dees_Troy51a0e822012-09-05 15:24:24 -040040
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020041 if (!node)
42 {
43 LOGERR("GUISlider created without XML node\n");
44 return;
45 }
Dees_Troy51a0e822012-09-05 15:24:24 -040046
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020047 child = node->first_node("resource");
48 if (child)
49 {
50 attr = child->first_attribute("base");
51 if (attr)
52 sSlider = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -040053
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020054 attr = child->first_attribute("used");
55 if (attr)
56 sSliderUsed = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -040057
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020058 attr = child->first_attribute("touch");
59 if (attr)
60 sTouch = PageManager::FindResource(attr->value());
61 }
Dees_Troy51a0e822012-09-05 15:24:24 -040062
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020063 // Load the placement
Dees_Troy51a0e822012-09-05 15:24:24 -040064 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY);
65
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 if (sSlider && sSlider->GetResource())
67 {
Dees_Troy51a0e822012-09-05 15:24:24 -040068 mRenderW = gr_get_width(sSlider->GetResource());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020069 mRenderH = gr_get_height(sSlider->GetResource());
70 }
71 if (sTouch && sTouch->GetResource())
72 {
73 sTouchW = gr_get_width(sTouch->GetResource()); // Width of the "touch image" that follows the touch (arrow)
74 sTouchH = gr_get_height(sTouch->GetResource()); // Height of the "touch image" that follows the touch (arrow)
75 }
Dees_Troy51a0e822012-09-05 15:24:24 -040076
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020077 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040078 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020079 mActionY = mRenderY;
80 mActionW = mRenderW;
81 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040082
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020083 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040084
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020085 sCurTouchX = mRenderX;
86 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040087}
88
89GUISlider::~GUISlider()
90{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 delete sAction;
Dees_Troy51a0e822012-09-05 15:24:24 -040092}
93
94int GUISlider::Render(void)
95{
Vojtech Bocekede51c52014-02-07 23:58:09 +010096 if(!isConditionTrue())
97 return 0;
98
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 if (!sSlider || !sSlider->GetResource())
100 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 // Draw the slider
103 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 // Draw the used
106 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
107 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 // Draw the touch icon
110 if (sTouch && sTouch->GetResource())
111 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400112
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200113 sUpdate = 0;
114 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400115}
116
117int GUISlider::Update(void)
118{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100119 if(!isConditionTrue())
120 return 0;
121
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 if (sUpdate)
123 return 2;
124 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400125}
126
127int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
128{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100129 if(!isConditionTrue())
130 return -1;
131
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200132 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400133
134 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200135 {
136 case TOUCH_START:
137 if (x >= mRenderX && x <= mRenderX + sTouchW &&
138 y >= mRenderY && y <= mRenderY + mRenderH)
139 {
140 sCurTouchX = x - (sTouchW / 2);
141 if (sCurTouchX < mRenderX)
142 sCurTouchX = mRenderX;
143 dragging = true;
144 }
145 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400146
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200147 case TOUCH_DRAG:
148 if (!dragging)
149 return 0;
150 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
151 {
152 sCurTouchX = mRenderX;
153 dragging = false;
154 sUpdate = 1;
155 break;
156 }
157 sCurTouchX = x - (sTouchW / 2);
158 if (sCurTouchX < mRenderX)
159 sCurTouchX = mRenderX;
160 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
161 sCurTouchX = mRenderX + mRenderW - sTouchW;
162 sUpdate = 1;
163 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400164
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200165 case TOUCH_RELEASE:
166 if (!dragging)
167 return 0;
168
Ethan Yonker03db3262014-02-05 08:02:06 -0600169 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
170 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200171 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600172 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200173
174 sCurTouchX = mRenderX;
175 dragging = false;
176 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400177 case TOUCH_REPEAT:
178 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200179 break;
180 }
181 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400182}