Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2017 TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | // slider.cpp - GUISlider object |
| 20 | // Pulled & ported from https://raw.github.com/agrabren/RecoverWin/master/gui/slider.cpp |
| 21 | |
| 22 | #include <stdarg.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <sys/reboot.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/types.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <time.h> |
| 34 | #include <unistd.h> |
| 35 | #include <stdlib.h> |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame] | 36 | #include "../data.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 37 | |
| 38 | #include <string> |
| 39 | |
| 40 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame^] | 43 | #include "minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 44 | |
| 45 | #include "rapidxml.hpp" |
| 46 | #include "objects.hpp" |
| 47 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 48 | GUISlider::GUISlider(xml_node<>* node) : GUIObject(node) |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 49 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 50 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 51 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 52 | sAction = NULL; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 53 | sSliderLabel = NULL; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 54 | sSlider = NULL; |
| 55 | sSliderUsed = NULL; |
| 56 | sTouch = NULL; |
| 57 | sTouchW = 20; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 58 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 59 | if (!node) |
| 60 | { |
| 61 | LOGERR("GUISlider created without XML node\n"); |
| 62 | return; |
| 63 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 65 | // Load the resources |
| 66 | child = FindNode(node, "resource"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 67 | if (child) |
| 68 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 69 | sSlider = LoadAttrImage(child, "base"); |
| 70 | sSliderUsed = LoadAttrImage(child, "used"); |
| 71 | sTouch = LoadAttrImage(child, "touch"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 72 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 73 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 74 | // Load the text label |
| 75 | sSliderLabel = new GUIText(node); |
| 76 | if (sSliderLabel->Render() < 0) |
| 77 | { |
| 78 | delete sSliderLabel; |
| 79 | sSliderLabel = NULL; |
| 80 | } |
| 81 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 82 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 83 | Placement TextPlacement = CENTER; |
| 84 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW, &mRenderH, &TextPlacement); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 85 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 86 | if (sSlider && sSlider->GetResource()) { |
| 87 | mRenderW = sSlider->GetWidth(); |
| 88 | mRenderH = sSlider->GetHeight(); |
| 89 | } else { |
| 90 | mRenderW = mRenderH = 0; |
| 91 | } |
Ethan Yonker | 591b920 | 2015-03-11 11:17:15 -0500 | [diff] [blame] | 92 | if (TextPlacement == CENTER || TextPlacement == CENTER_X_ONLY) { |
| 93 | mRenderX = mRenderX - (mRenderW / 2); |
| 94 | if (TextPlacement == CENTER) { |
| 95 | mRenderY = mRenderY - (mRenderH / 2); |
| 96 | } |
| 97 | } |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 98 | if (sSliderLabel) { |
| 99 | int sTextX = mRenderX + (mRenderW / 2); |
| 100 | int w, h; |
| 101 | sSliderLabel->GetCurrentBounds(w, h); |
| 102 | int sTextY = mRenderY + ((mRenderH - h) / 2); |
| 103 | sSliderLabel->SetRenderPos(sTextX, sTextY); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 104 | sSliderLabel->SetMaxWidth(mRenderW); |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 105 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | if (sTouch && sTouch->GetResource()) |
| 107 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 108 | sTouchW = sTouch->GetWidth(); // Width of the "touch image" that follows the touch (arrow) |
| 109 | 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] | 110 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 111 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 112 | //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 113 | mActionX = mRenderX; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 114 | mActionY = mRenderY; |
| 115 | mActionW = mRenderW; |
| 116 | mActionH = mRenderH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 117 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 118 | sAction = new GUIAction(node); |
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 | sCurTouchX = mRenderX; |
| 121 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | GUISlider::~GUISlider() |
| 125 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 126 | delete sAction; |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 127 | delete sSliderLabel; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int GUISlider::Render(void) |
| 131 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 132 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 133 | return 0; |
| 134 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 135 | if (!sSlider || !sSlider->GetResource()) |
| 136 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 137 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 138 | // Draw the slider |
| 139 | gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 140 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 141 | // Draw the used |
| 142 | if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX) |
| 143 | gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 144 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 145 | // Draw the touch icon |
| 146 | if (sTouch && sTouch->GetResource()) |
| 147 | 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] | 148 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 149 | if (sSliderLabel) { |
| 150 | int ret = sSliderLabel->Render(); |
| 151 | if (ret < 0) return ret; |
| 152 | } |
| 153 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 154 | sUpdate = 0; |
| 155 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int GUISlider::Update(void) |
| 159 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 160 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 161 | return 0; |
| 162 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 163 | if (sUpdate) |
| 164 | return 2; |
| 165 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 169 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 170 | if (!isConditionTrue()) |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 171 | return -1; |
| 172 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 173 | static bool dragging = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 174 | |
| 175 | switch (state) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 176 | { |
| 177 | case TOUCH_START: |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 178 | if (x >= mRenderX && x <= mRenderX + sTouchW && |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 179 | y >= mRenderY && y <= mRenderY + mRenderH) |
| 180 | { |
| 181 | sCurTouchX = x - (sTouchW / 2); |
| 182 | if (sCurTouchX < mRenderX) |
| 183 | sCurTouchX = mRenderX; |
| 184 | dragging = true; |
| 185 | } |
| 186 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 187 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 188 | case TOUCH_DRAG: |
| 189 | if (!dragging) |
| 190 | return 0; |
| 191 | if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2)) |
| 192 | { |
| 193 | sCurTouchX = mRenderX; |
| 194 | dragging = false; |
| 195 | sUpdate = 1; |
| 196 | break; |
| 197 | } |
| 198 | sCurTouchX = x - (sTouchW / 2); |
| 199 | if (sCurTouchX < mRenderX) |
| 200 | sCurTouchX = mRenderX; |
| 201 | if (sCurTouchX > mRenderX + mRenderW - sTouchW) |
| 202 | sCurTouchX = mRenderX + mRenderW - sTouchW; |
| 203 | sUpdate = 1; |
| 204 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 205 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 206 | case TOUCH_RELEASE: |
| 207 | if (!dragging) |
| 208 | return 0; |
| 209 | |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 210 | if (sCurTouchX >= mRenderX + mRenderW - sTouchW) { |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 211 | |
| 212 | #ifndef TW_NO_HAPTICS |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 213 | DataManager::Vibrate("tw_button_vibrate"); |
bigbiff bigbiff | 3ed778a | 2019-03-12 19:28:31 -0400 | [diff] [blame] | 214 | #endif |
| 215 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 216 | sAction->doActions(); |
Ethan Yonker | 03db326 | 2014-02-05 08:02:06 -0600 | [diff] [blame] | 217 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 218 | |
| 219 | sCurTouchX = mRenderX; |
| 220 | dragging = false; |
| 221 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 222 | case TOUCH_REPEAT: |
| 223 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 224 | break; |
| 225 | } |
| 226 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 227 | } |