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