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); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 83 | sSliderLabel->SetMaxWidth(mRenderW); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 84 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | if (sTouch && sTouch->GetResource()) |
| 86 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 87 | sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow) |
| 88 | 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] | 89 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 90 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 91 | //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 92 | mActionX = mRenderX; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 93 | mActionY = mRenderY; |
| 94 | mActionW = mRenderW; |
| 95 | mActionH = mRenderH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 96 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 97 | sAction = new GUIAction(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | sCurTouchX = mRenderX; |
| 100 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | GUISlider::~GUISlider() |
| 104 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 105 | delete sAction; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 106 | delete sSliderLabel; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | int GUISlider::Render(void) |
| 110 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 111 | if(!isConditionTrue()) |
| 112 | return 0; |
| 113 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 114 | if (!sSlider || !sSlider->GetResource()) |
| 115 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 116 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 117 | // Draw the slider |
| 118 | gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 120 | // Draw the used |
| 121 | if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX) |
| 122 | gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 123 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 124 | // Draw the touch icon |
| 125 | if (sTouch && sTouch->GetResource()) |
| 126 | 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] | 127 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 128 | if (sSliderLabel) { |
| 129 | int ret = sSliderLabel->Render(); |
| 130 | if (ret < 0) return ret; |
| 131 | } |
| 132 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 133 | sUpdate = 0; |
| 134 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | int GUISlider::Update(void) |
| 138 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 139 | if(!isConditionTrue()) |
| 140 | return 0; |
| 141 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 142 | if (sUpdate) |
| 143 | return 2; |
| 144 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 148 | { |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 149 | if(!isConditionTrue()) |
| 150 | return -1; |
| 151 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 152 | static bool dragging = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 153 | |
| 154 | switch (state) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 155 | { |
| 156 | case TOUCH_START: |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 157 | if (x >= mRenderX && x <= mRenderX + sTouchW && |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | y >= mRenderY && y <= mRenderY + mRenderH) |
| 159 | { |
| 160 | sCurTouchX = x - (sTouchW / 2); |
| 161 | if (sCurTouchX < mRenderX) |
| 162 | sCurTouchX = mRenderX; |
| 163 | dragging = true; |
| 164 | } |
| 165 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 167 | case TOUCH_DRAG: |
| 168 | if (!dragging) |
| 169 | return 0; |
| 170 | if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2)) |
| 171 | { |
| 172 | sCurTouchX = mRenderX; |
| 173 | dragging = false; |
| 174 | sUpdate = 1; |
| 175 | break; |
| 176 | } |
| 177 | sCurTouchX = x - (sTouchW / 2); |
| 178 | if (sCurTouchX < mRenderX) |
| 179 | sCurTouchX = mRenderX; |
| 180 | if (sCurTouchX > mRenderX + mRenderW - sTouchW) |
| 181 | sCurTouchX = mRenderX + mRenderW - sTouchW; |
| 182 | sUpdate = 1; |
| 183 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 184 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 185 | case TOUCH_RELEASE: |
| 186 | if (!dragging) |
| 187 | return 0; |
| 188 | |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 189 | if (sCurTouchX >= mRenderX + mRenderW - sTouchW) { |
| 190 | DataManager::Vibrate("tw_button_vibrate"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 191 | sAction->doActions(); |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 192 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 193 | |
| 194 | sCurTouchX = mRenderX; |
| 195 | dragging = false; |
| 196 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 197 | case TOUCH_REPEAT: |
| 198 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
| 201 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 202 | } |