blob: c53dabc28ed02ee13184fe75eeb2eb272ed6dca6 [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 {
thatf6ed8fc2015-02-14 20:23:16 +010050 sSlider = LoadAttrImage(child, "base");
51 sSliderUsed = LoadAttrImage(child, "used");
52 sTouch = LoadAttrImage(child, "touch");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 }
Dees_Troy51a0e822012-09-05 15:24:24 -040054
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020055 // Load the placement
Dees_Troy51a0e822012-09-05 15:24:24 -040056 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY);
57
thatf6ed8fc2015-02-14 20:23:16 +010058 mRenderW = sSlider->GetWidth();
59 mRenderH = sSlider->GetHeight();
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020060 if (sTouch && sTouch->GetResource())
61 {
thatf6ed8fc2015-02-14 20:23:16 +010062 sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow)
63 sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020064 }
Dees_Troy51a0e822012-09-05 15:24:24 -040065
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020066 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040067 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 mActionY = mRenderY;
69 mActionW = mRenderW;
70 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040071
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020072 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040073
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020074 sCurTouchX = mRenderX;
75 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040076}
77
78GUISlider::~GUISlider()
79{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020080 delete sAction;
Dees_Troy51a0e822012-09-05 15:24:24 -040081}
82
83int GUISlider::Render(void)
84{
Vojtech Bocekede51c52014-02-07 23:58:09 +010085 if(!isConditionTrue())
86 return 0;
87
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020088 if (!sSlider || !sSlider->GetResource())
89 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040090
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020091 // Draw the slider
92 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040093
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020094 // Draw the used
95 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
96 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 // Draw the touch icon
99 if (sTouch && sTouch->GetResource())
100 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 sUpdate = 0;
103 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400104}
105
106int GUISlider::Update(void)
107{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100108 if(!isConditionTrue())
109 return 0;
110
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200111 if (sUpdate)
112 return 2;
113 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400114}
115
116int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
117{
Vojtech Bocekede51c52014-02-07 23:58:09 +0100118 if(!isConditionTrue())
119 return -1;
120
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200121 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400122
123 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200124 {
125 case TOUCH_START:
Matt Mowerfb1c4ff2014-04-16 13:43:36 -0500126 if (x >= mRenderX && x <= mRenderX + sTouchW &&
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200127 y >= mRenderY && y <= mRenderY + mRenderH)
128 {
129 sCurTouchX = x - (sTouchW / 2);
130 if (sCurTouchX < mRenderX)
131 sCurTouchX = mRenderX;
132 dragging = true;
133 }
134 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400135
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200136 case TOUCH_DRAG:
137 if (!dragging)
138 return 0;
139 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
140 {
141 sCurTouchX = mRenderX;
142 dragging = false;
143 sUpdate = 1;
144 break;
145 }
146 sCurTouchX = x - (sTouchW / 2);
147 if (sCurTouchX < mRenderX)
148 sCurTouchX = mRenderX;
149 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
150 sCurTouchX = mRenderX + mRenderW - sTouchW;
151 sUpdate = 1;
152 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400153
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200154 case TOUCH_RELEASE:
155 if (!dragging)
156 return 0;
157
Ethan Yonker03db3262014-02-05 08:02:06 -0600158 if (sCurTouchX >= mRenderX + mRenderW - sTouchW) {
159 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200160 sAction->doActions();
Ethan Yonker03db3262014-02-05 08:02:06 -0600161 }
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200162
163 sCurTouchX = mRenderX;
164 dragging = false;
165 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400166 case TOUCH_REPEAT:
167 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200168 break;
169 }
170 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400171}