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