Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // 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) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 18 | #include "../data.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 23 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | #include "rapidxml.hpp" |
| 28 | #include "objects.hpp" |
| 29 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 30 | GUISlider::GUISlider(xml_node<>* node) : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 31 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 32 | xml_attribute<>* attr; |
| 33 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | sAction = NULL; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 36 | sSliderLabel = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 37 | sSlider = NULL; |
| 38 | sSliderUsed = NULL; |
| 39 | sTouch = NULL; |
| 40 | sTouchW = 20; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 41 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 42 | if (!node) |
| 43 | { |
| 44 | LOGERR("GUISlider created without XML node\n"); |
| 45 | return; |
| 46 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 47 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 48 | // Load the resources |
| 49 | child = FindNode(node, "resource"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | if (child) |
| 51 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 52 | sSlider = LoadAttrImage(child, "base"); |
| 53 | sSliderUsed = LoadAttrImage(child, "used"); |
| 54 | sTouch = LoadAttrImage(child, "touch"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 55 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 56 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 57 | // Load the text label |
| 58 | sSliderLabel = new GUIText(node); |
| 59 | if (sSliderLabel->Render() < 0) |
| 60 | { |
| 61 | delete sSliderLabel; |
| 62 | sSliderLabel = NULL; |
| 63 | } |
| 64 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 65 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 66 | Placement TextPlacement = CENTER; |
| 67 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &TextPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 69 | mRenderW = sSlider->GetWidth(); |
| 70 | mRenderH = sSlider->GetHeight(); |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 71 | if (TextPlacement == CENTER || TextPlacement == CENTER_X_ONLY) { |
| 72 | mRenderX = mRenderX - (mRenderW / 2); |
| 73 | if (TextPlacement == CENTER) { |
| 74 | mRenderY = mRenderY - (mRenderH / 2); |
| 75 | } |
| 76 | } |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 77 | if (sSliderLabel) { |
| 78 | int sTextX = mRenderX + (mRenderW / 2); |
| 79 | int w, h; |
| 80 | sSliderLabel->GetCurrentBounds(w, h); |
| 81 | int sTextY = mRenderY + ((mRenderH - h) / 2); |
| 82 | sSliderLabel->SetRenderPos(sTextX, sTextY); |
| 83 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 84 | if (sTouch && sTouch->GetResource()) |
| 85 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 86 | sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow) |
| 87 | sTouchH = sTouch->GetHeight(); // Height of the "touch image" that follows the touch (arrow) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 88 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 89 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 90 | //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 91 | mActionX = mRenderX; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 92 | mActionY = mRenderY; |
| 93 | mActionW = mRenderW; |
| 94 | mActionH = mRenderH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 95 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | sAction = new GUIAction(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 97 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 98 | sCurTouchX = mRenderX; |
| 99 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | GUISlider::~GUISlider() |
| 103 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 104 | delete sAction; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 105 | delete sSliderLabel; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int GUISlider::Render(void) |
| 109 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 110 | if(!isConditionTrue()) |
| 111 | return 0; |
| 112 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 113 | if (!sSlider || !sSlider->GetResource()) |
| 114 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 115 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | // Draw the slider |
| 117 | gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 118 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 119 | // Draw the used |
| 120 | if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX) |
| 121 | gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | // Draw the touch icon |
| 124 | if (sTouch && sTouch->GetResource()) |
| 125 | gr_blit(sTouch->GetResource(), 0, 0, sTouchW, sTouchH, sCurTouchX, (mRenderY + ((mRenderH - sTouchH) / 2))); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 126 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 127 | if (sSliderLabel) { |
| 128 | int ret = sSliderLabel->Render(); |
| 129 | if (ret < 0) return ret; |
| 130 | } |
| 131 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 132 | sUpdate = 0; |
| 133 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int GUISlider::Update(void) |
| 137 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 138 | if(!isConditionTrue()) |
| 139 | return 0; |
| 140 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | if (sUpdate) |
| 142 | return 2; |
| 143 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 147 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 148 | if(!isConditionTrue()) |
| 149 | return -1; |
| 150 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 151 | static bool dragging = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 152 | |
| 153 | switch (state) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | { |
| 155 | case TOUCH_START: |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 156 | if (x >= mRenderX && x <= mRenderX + sTouchW && |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 157 | y >= mRenderY && y <= mRenderY + mRenderH) |
| 158 | { |
| 159 | sCurTouchX = x - (sTouchW / 2); |
| 160 | if (sCurTouchX < mRenderX) |
| 161 | sCurTouchX = mRenderX; |
| 162 | dragging = true; |
| 163 | } |
| 164 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 165 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 166 | case TOUCH_DRAG: |
| 167 | if (!dragging) |
| 168 | return 0; |
| 169 | if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2)) |
| 170 | { |
| 171 | sCurTouchX = mRenderX; |
| 172 | dragging = false; |
| 173 | sUpdate = 1; |
| 174 | break; |
| 175 | } |
| 176 | sCurTouchX = x - (sTouchW / 2); |
| 177 | if (sCurTouchX < mRenderX) |
| 178 | sCurTouchX = mRenderX; |
| 179 | if (sCurTouchX > mRenderX + mRenderW - sTouchW) |
| 180 | sCurTouchX = mRenderX + mRenderW - sTouchW; |
| 181 | sUpdate = 1; |
| 182 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 183 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 184 | case TOUCH_RELEASE: |
| 185 | if (!dragging) |
| 186 | return 0; |
| 187 | |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 188 | if (sCurTouchX >= mRenderX + mRenderW - sTouchW) { |
| 189 | DataManager::Vibrate("tw_button_vibrate"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 190 | sAction->doActions(); |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 191 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 192 | |
| 193 | sCurTouchX = mRenderX; |
| 194 | dragging = false; |
| 195 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 196 | case TOUCH_REPEAT: |
| 197 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 198 | break; |
| 199 | } |
| 200 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 201 | } |