blob: 95962c09b5c17bbb4d85d92133a5cb38a91ab7e4 [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>
18
19#include <string>
20
21extern "C" {
22#include "../common.h"
23#include "../minuitwrp/minui.h"
24#include "../recovery_ui.h"
25}
26
27#include "rapidxml.hpp"
28#include "objects.hpp"
29
30GUISlider::GUISlider(xml_node<>* node)
31{
32 xml_attribute<>* attr;
33 xml_node<>* child;
34
35 sAction = NULL;
36 sSlider = NULL;
37 sSliderUsed = NULL;
38 sTouch = NULL;
39 sTouchW = 20;
40
41 if (!node)
42 {
43 LOGE("GUISlider created without XML node\n");
44 return;
45 }
46
47 child = node->first_node("resource");
48 if (child)
49 {
50 attr = child->first_attribute("base");
51 if (attr)
52 sSlider = PageManager::FindResource(attr->value());
53
54 attr = child->first_attribute("used");
55 if (attr)
56 sSliderUsed = PageManager::FindResource(attr->value());
57
58 attr = child->first_attribute("touch");
59 if (attr)
60 sTouch = PageManager::FindResource(attr->value());
61 }
62
63 // Load the placement
64 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY);
65
66 if (sSlider && sSlider->GetResource())
67 {
68 mRenderW = gr_get_width(sSlider->GetResource());
69 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 }
76
77 //LOGI("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
78 mActionX = mRenderX;
79 mActionY = mRenderY;
80 mActionW = mRenderW;
81 mActionH = mRenderH;
82
83 sAction = new GUIAction(node);
84
85 sCurTouchX = mRenderX;
86 sUpdate = 1;
87
88 return;
89}
90
91GUISlider::~GUISlider()
92{
93 delete sAction;
94}
95
96int GUISlider::Render(void)
97{
98 if (!sSlider || !sSlider->GetResource()) return -1;
99
100 // Draw the slider
101 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
102
103 // Draw the used
104 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
105 {
106 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
107 }
108
109 // Draw the touch icon
110 if (sTouch && sTouch->GetResource())
111 {
112 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
113 }
114
115 sUpdate = 0;
116 return 0;
117}
118
119int GUISlider::Update(void)
120{
121 if (sUpdate) return 2;
122 return 0;
123}
124
125int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
126{
127 static bool dragging = false;
128
129 switch (state)
130 {
131 case TOUCH_START:
132 if (x >= mRenderX && x <= mRenderX + sTouchW &&
133 y >= mRenderY && y <= mRenderY + mRenderH)
134 {
135 sCurTouchX = x - (sTouchW / 2);
136 if (sCurTouchX < mRenderX) sCurTouchX = mRenderX;
137 dragging = true;
138 }
139 break;
140
141 case TOUCH_DRAG:
142 if (!dragging) return 0;
143 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
144 {
145 sCurTouchX = mRenderX;
146 dragging = false;
147 sUpdate = 1;
148 break;
149 }
150 sCurTouchX = x - (sTouchW / 2);
151 if (sCurTouchX < mRenderX) sCurTouchX = mRenderX;
152 if (sCurTouchX > mRenderX + mRenderW - sTouchW) sCurTouchX = mRenderX + mRenderW - sTouchW;
153 sUpdate = 1;
154 break;
155
156 case TOUCH_RELEASE:
157 if (!dragging) return 0;
158 if (sCurTouchX >= mRenderX + mRenderW - sTouchW)
159 {
160 sAction->doActions();
161 }
162 sCurTouchX = mRenderX;
163 dragging = false;
164 sUpdate = 1;
165 case TOUCH_REPEAT:
166 case TOUCH_HOLD:
167 break;
168 }
169 return 0;
170}
171