blob: 3908b82ab2fae3f392d176fa18b02d9215f16c68 [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
30GUISlider::GUISlider(xml_node<>* node)
31{
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 Bocekfafb0c52013-07-25 22:53:02 +020096 if (!sSlider || !sSlider->GetResource())
97 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040098
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020099 // Draw the slider
100 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400101
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200102 // Draw the used
103 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
104 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400105
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200106 // Draw the touch icon
107 if (sTouch && sTouch->GetResource())
108 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400109
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200110 sUpdate = 0;
111 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400112}
113
114int GUISlider::Update(void)
115{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200116 if (sUpdate)
117 return 2;
118 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400119}
120
121int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
122{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200123 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400124
125 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200126 {
127 case TOUCH_START:
128 if (x >= mRenderX && x <= mRenderX + sTouchW &&
129 y >= mRenderY && y <= mRenderY + mRenderH)
130 {
131 sCurTouchX = x - (sTouchW / 2);
132 if (sCurTouchX < mRenderX)
133 sCurTouchX = mRenderX;
134 dragging = true;
135 }
136 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400137
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200138 case TOUCH_DRAG:
139 if (!dragging)
140 return 0;
141 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
142 {
143 sCurTouchX = mRenderX;
144 dragging = false;
145 sUpdate = 1;
146 break;
147 }
148 sCurTouchX = x - (sTouchW / 2);
149 if (sCurTouchX < mRenderX)
150 sCurTouchX = mRenderX;
151 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
152 sCurTouchX = mRenderX + mRenderW - sTouchW;
153 sUpdate = 1;
154 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400155
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200156 case TOUCH_RELEASE:
Samer Diab (S.a.M.e.R_d)71e9b042014-01-07 20:18:47 +0000157 DataManager::Vibrate("tw_button_vibrate");
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200158 if (!dragging)
159 return 0;
160
161 if (sCurTouchX >= mRenderX + mRenderW - sTouchW)
162 sAction->doActions();
163
164 sCurTouchX = mRenderX;
165 dragging = false;
166 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400167 case TOUCH_REPEAT:
168 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200169 break;
170 }
171 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400172}