blob: b46d156f6f8032fa82c453a2b12fb346ff0cdeb3 [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{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020031 xml_attribute<>* attr;
32 xml_node<>* child;
Dees_Troy51a0e822012-09-05 15:24:24 -040033
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020034 sAction = NULL;
35 sSlider = NULL;
36 sSliderUsed = NULL;
37 sTouch = NULL;
38 sTouchW = 20;
Dees_Troy51a0e822012-09-05 15:24:24 -040039
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020040 if (!node)
41 {
42 LOGERR("GUISlider created without XML node\n");
43 return;
44 }
Dees_Troy51a0e822012-09-05 15:24:24 -040045
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020046 child = node->first_node("resource");
47 if (child)
48 {
49 attr = child->first_attribute("base");
50 if (attr)
51 sSlider = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -040052
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020053 attr = child->first_attribute("used");
54 if (attr)
55 sSliderUsed = PageManager::FindResource(attr->value());
Dees_Troy51a0e822012-09-05 15:24:24 -040056
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020057 attr = child->first_attribute("touch");
58 if (attr)
59 sTouch = PageManager::FindResource(attr->value());
60 }
Dees_Troy51a0e822012-09-05 15:24:24 -040061
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020062 // Load the placement
Dees_Troy51a0e822012-09-05 15:24:24 -040063 LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY);
64
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020065 if (sSlider && sSlider->GetResource())
66 {
Dees_Troy51a0e822012-09-05 15:24:24 -040067 mRenderW = gr_get_width(sSlider->GetResource());
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020068 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 }
Dees_Troy51a0e822012-09-05 15:24:24 -040075
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020076 //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW);
Dees_Troy51a0e822012-09-05 15:24:24 -040077 mActionX = mRenderX;
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020078 mActionY = mRenderY;
79 mActionW = mRenderW;
80 mActionH = mRenderH;
Dees_Troy51a0e822012-09-05 15:24:24 -040081
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020082 sAction = new GUIAction(node);
Dees_Troy51a0e822012-09-05 15:24:24 -040083
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020084 sCurTouchX = mRenderX;
85 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -040086}
87
88GUISlider::~GUISlider()
89{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020090 delete sAction;
Dees_Troy51a0e822012-09-05 15:24:24 -040091}
92
93int GUISlider::Render(void)
94{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020095 if (!sSlider || !sSlider->GetResource())
96 return -1;
Dees_Troy51a0e822012-09-05 15:24:24 -040097
Vojtech Bocekfafb0c52013-07-25 22:53:02 +020098 // Draw the slider
99 gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400100
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200101 // Draw the used
102 if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX)
103 gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY);
Dees_Troy51a0e822012-09-05 15:24:24 -0400104
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200105 // Draw the touch icon
106 if (sTouch && sTouch->GetResource())
107 gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2)));
Dees_Troy51a0e822012-09-05 15:24:24 -0400108
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200109 sUpdate = 0;
110 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400111}
112
113int GUISlider::Update(void)
114{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200115 if (sUpdate)
116 return 2;
117 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400118}
119
120int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y)
121{
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200122 static bool dragging = false;
Dees_Troy51a0e822012-09-05 15:24:24 -0400123
124 switch (state)
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200125 {
126 case TOUCH_START:
127 if (x >= mRenderX && x <= mRenderX + sTouchW &&
128 y >= mRenderY && y <= mRenderY + mRenderH)
129 {
130 sCurTouchX = x - (sTouchW / 2);
131 if (sCurTouchX < mRenderX)
132 sCurTouchX = mRenderX;
133 dragging = true;
134 }
135 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400136
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200137 case TOUCH_DRAG:
138 if (!dragging)
139 return 0;
140 if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2))
141 {
142 sCurTouchX = mRenderX;
143 dragging = false;
144 sUpdate = 1;
145 break;
146 }
147 sCurTouchX = x - (sTouchW / 2);
148 if (sCurTouchX < mRenderX)
149 sCurTouchX = mRenderX;
150 if (sCurTouchX > mRenderX + mRenderW - sTouchW)
151 sCurTouchX = mRenderX + mRenderW - sTouchW;
152 sUpdate = 1;
153 break;
Dees_Troy51a0e822012-09-05 15:24:24 -0400154
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200155 case TOUCH_RELEASE:
156 if (!dragging)
157 return 0;
158
159 if (sCurTouchX >= mRenderX + mRenderW - sTouchW)
160 sAction->doActions();
161
162 sCurTouchX = mRenderX;
163 dragging = false;
164 sUpdate = 1;
Dees_Troy51a0e822012-09-05 15:24:24 -0400165 case TOUCH_REPEAT:
166 case TOUCH_HOLD:
Vojtech Bocekfafb0c52013-07-25 22:53:02 +0200167 break;
168 }
169 return 0;
Dees_Troy51a0e822012-09-05 15:24:24 -0400170}