Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 1 | /* |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 2 | Copyright 2012 to 2020 TeamWin |
Matt Mower | e04eee7 | 2016-12-31 00:38:57 -0600 | [diff] [blame] | 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 | |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 19 | // slidervalue.cpp - GUISliderValue object |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <sys/reboot.h> |
| 27 | #include <sys/stat.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <sys/ioctl.h> |
| 32 | #include <time.h> |
| 33 | #include <unistd.h> |
| 34 | #include <stdlib.h> |
| 35 | |
| 36 | #include <string> |
| 37 | |
| 38 | extern "C" { |
| 39 | #include "../twcommon.h" |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 40 | } |
bigbiff | d81833a | 2021-01-17 11:06:57 -0500 | [diff] [blame^] | 41 | #include "minuitwrp/minui.h" |
| 42 | #include "minuitwrp/truetype.hpp" |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 43 | |
| 44 | #include "rapidxml.hpp" |
| 45 | #include "objects.hpp" |
| 46 | |
Vojtech Bocek | ede51c5 | 2014-02-07 23:58:09 +0100 | [diff] [blame] | 47 | GUISliderValue::GUISliderValue(xml_node<>* node) : GUIObject(node) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 48 | { |
| 49 | xml_attribute<>* attr; |
| 50 | xml_node<>* child; |
| 51 | |
| 52 | mMin = 0; |
| 53 | mMax = 100; |
| 54 | mValue = 0; |
| 55 | mLineH = 2; |
| 56 | mLinePadding = 10; |
| 57 | mSliderW = 5; |
| 58 | mSliderH = 30; |
| 59 | mLineX = 0; |
| 60 | mLineY = 0; |
| 61 | mValueStr = NULL; |
| 62 | mAction = NULL; |
| 63 | mShowCurr = true; |
| 64 | mShowRange = false; |
| 65 | mChangeOnDrag = false; |
| 66 | mRendered = false; |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 67 | mBackgroundImage = NULL; |
| 68 | mHandleImage = NULL; |
| 69 | mHandleHoverImage = NULL; |
| 70 | mDragging = false; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 71 | |
| 72 | mLabel = NULL; |
| 73 | ConvertStrToColor("white", &mTextColor); |
| 74 | ConvertStrToColor("white", &mLineColor); |
| 75 | ConvertStrToColor("blue", &mSliderColor); |
| 76 | |
| 77 | if (!node) |
| 78 | { |
| 79 | LOGERR("GUISliderValue created without XML node\n"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | mLabel = new GUIText(node); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 84 | if (mLabel->Render() < 0) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 85 | { |
| 86 | delete mLabel; |
| 87 | mLabel = NULL; |
| 88 | } |
| 89 | |
| 90 | mAction = new GUIAction(node); |
| 91 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 92 | child = FindNode(node, "font"); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 93 | if (child) |
| 94 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 95 | mFont = LoadAttrFont(child, "resource"); |
| 96 | mTextColor = LoadAttrColor(child, "color", mTextColor); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // Load the placement |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 100 | LoadPlacement(FindNode(node, "placement"), &mRenderX, &mRenderY, &mRenderW); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 101 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 102 | child = FindNode(node, "colors"); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 103 | if (child) |
| 104 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 105 | mLineColor = LoadAttrColor(child, "line"); |
| 106 | mSliderColor = LoadAttrColor(child, "slider"); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 109 | child = FindNode(node, "resource"); |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 110 | if (child) |
| 111 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 112 | mBackgroundImage = LoadAttrImage(child, "background"); |
| 113 | mHandleImage = LoadAttrImage(child, "handle"); |
| 114 | mHandleHoverImage = LoadAttrImage(child, "handlehover"); |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 117 | child = FindNode(node, "data"); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 118 | if (child) |
| 119 | { |
| 120 | attr = child->first_attribute("variable"); |
| 121 | if (attr) |
| 122 | mVariable = attr->value(); |
| 123 | |
| 124 | attr = child->first_attribute("min"); |
| 125 | if (attr) |
| 126 | { |
| 127 | mMinStr = gui_parse_text(attr->value()); |
| 128 | mMin = atoi(mMinStr.c_str()); |
| 129 | } |
| 130 | |
| 131 | attr = child->first_attribute("max"); |
| 132 | if (attr) |
| 133 | { |
| 134 | mMaxStr = gui_parse_text(attr->value()); |
| 135 | mMax = atoi(mMaxStr.c_str()); |
| 136 | } |
| 137 | |
| 138 | if (mMin > mMax) |
| 139 | mMin = mMax; |
| 140 | |
| 141 | attr = child->first_attribute("default"); |
| 142 | if (attr) |
| 143 | { |
| 144 | string parsevalue = gui_parse_text(attr->value()); |
| 145 | int def = atoi(parsevalue.c_str()); |
| 146 | |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 147 | if (def < mMin) |
| 148 | def = mMin; |
| 149 | else if (def > mMax) |
| 150 | def = mMax; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 151 | DataManager::SetValue(mVariable, def); |
| 152 | } |
| 153 | |
| 154 | attr = child->first_attribute("showrange"); |
| 155 | if (attr) |
| 156 | mShowRange = atoi(attr->value()); |
| 157 | |
| 158 | attr = child->first_attribute("showcurr"); |
| 159 | if (attr) |
| 160 | mShowCurr = atoi(attr->value()); |
| 161 | |
| 162 | attr = child->first_attribute("changeondrag"); |
| 163 | if (attr) |
| 164 | mChangeOnDrag = atoi(attr->value()); |
| 165 | } |
| 166 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 167 | child = FindNode(node, "dimensions"); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 168 | if (child) |
| 169 | { |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 170 | mLineH = LoadAttrIntScaleY(child, "lineh", mLineH); |
| 171 | mLinePadding = LoadAttrIntScaleX(child, "linepadding", mLinePadding); |
| 172 | mSliderW = LoadAttrIntScaleX(child, "sliderw", mSliderW); |
| 173 | mSliderH = LoadAttrIntScaleY(child, "sliderh", mSliderH); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Ethan Yonker | 58f2132 | 2018-08-24 11:17:36 -0500 | [diff] [blame] | 176 | if (mFont && mFont->GetResource()) |
| 177 | mFontHeight = mFont->GetHeight(); |
| 178 | else |
| 179 | mFontHeight = 0; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 180 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 181 | if (mShowCurr) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 182 | { |
| 183 | int maxLen = std::max(strlen(mMinStr.c_str()), strlen(mMaxStr.c_str())); |
| 184 | mValueStr = new char[maxLen+1]; |
| 185 | } |
| 186 | |
| 187 | loadValue(true); |
| 188 | |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 189 | if (mShowRange) |
| 190 | { |
| 191 | int textW = std::max(measureText(mMaxStr), measureText(mMinStr)); |
| 192 | mLinePadding += textW; |
| 193 | } |
| 194 | |
| 195 | SetRenderPos(mRenderX, mRenderY, mRenderW); |
| 196 | } |
| 197 | |
| 198 | GUISliderValue::~GUISliderValue() |
| 199 | { |
| 200 | delete mLabel; |
| 201 | delete mAction; |
| 202 | delete[] mValueStr; |
| 203 | } |
| 204 | |
| 205 | void GUISliderValue::loadValue(bool force) |
| 206 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 207 | if (!mVariable.empty()) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 208 | { |
| 209 | int value = DataManager::GetIntValue(mVariable); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 210 | if (mValue == value && !force) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 211 | return; |
| 212 | |
| 213 | mValue = value; |
| 214 | } |
| 215 | |
| 216 | mValue = std::max(mValue, mMin); |
| 217 | mValue = std::min(mValue, mMax); |
| 218 | mValuePct = pctFromValue(mValue); |
| 219 | mRendered = false; |
| 220 | } |
| 221 | |
| 222 | int GUISliderValue::SetRenderPos(int x, int y, int w, int h) |
| 223 | { |
| 224 | mRenderX = x; |
| 225 | mRenderY = y; |
| 226 | if (w || h) |
| 227 | { |
| 228 | mRenderW = w; |
| 229 | mRenderH = h; |
| 230 | } |
| 231 | |
| 232 | mRenderH = mSliderH; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 233 | if (mShowCurr) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 234 | mRenderH += mFontHeight; |
| 235 | |
| 236 | if (mLabel) |
| 237 | { |
| 238 | int lw, lh; |
| 239 | mLabel->GetCurrentBounds(lw, lh); |
| 240 | int textX = mRenderX + (mRenderW/2 - lw/2); |
| 241 | |
| 242 | mLabel->SetRenderPos(textX, mRenderY); |
| 243 | |
| 244 | y += lh; |
| 245 | mRenderH += lh; |
| 246 | } |
| 247 | |
| 248 | mSliderY = y; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 249 | |
| 250 | mActionX = mRenderX; |
| 251 | mActionY = mRenderY; |
| 252 | mActionW = mRenderW; |
| 253 | mActionH = mRenderH; |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 254 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 255 | if (mBackgroundImage && mBackgroundImage->GetResource()) |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 256 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 257 | mLineW = mBackgroundImage->GetWidth(); |
| 258 | mLineH = mBackgroundImage->GetHeight(); |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 259 | } |
| 260 | else |
| 261 | mLineW = mRenderW - (mLinePadding * 2); |
| 262 | |
| 263 | mLineY = y + (mSliderH/2 - mLineH/2); |
| 264 | mLineX = mRenderX + (mRenderW/2 - mLineW/2); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | int GUISliderValue::measureText(const std::string& str) |
| 270 | { |
| 271 | void* fontResource = NULL; |
| 272 | if (mFont) fontResource = mFont->GetResource(); |
| 273 | |
bigbiff | d58ba18 | 2020-03-23 10:02:29 -0400 | [diff] [blame] | 274 | return twrpTruetype::gr_ttf_measureEx(str.c_str(), fontResource); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | int GUISliderValue::Render(void) |
| 278 | { |
| 279 | if (!isConditionTrue()) |
| 280 | { |
| 281 | mRendered = false; |
| 282 | return 0; |
| 283 | } |
| 284 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 285 | if (mLabel) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 286 | { |
| 287 | int w, h; |
| 288 | mLabel->GetCurrentBounds(w, h); |
| 289 | if (w != mLabelW) { |
| 290 | mLabelW = w; |
| 291 | int textX = mRenderX + (mRenderW/2 - mLabelW/2); |
| 292 | mLabel->SetRenderPos(textX, mRenderY); |
| 293 | } |
| 294 | int res = mLabel->Render(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 295 | if (res < 0) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 296 | return res; |
| 297 | } |
| 298 | |
| 299 | // line |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 300 | if (mBackgroundImage && mBackgroundImage->GetResource()) |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 301 | { |
| 302 | gr_blit(mBackgroundImage->GetResource(), 0, 0, mLineW, mLineH, mLineX, mLineY); |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | gr_color(mLineColor.red, mLineColor.green, mLineColor.blue, mLineColor.alpha); |
| 307 | gr_fill(mLineX, mLineY, mLineW, mLineH); |
| 308 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 309 | |
| 310 | // slider |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 311 | uint32_t sliderX = mLineX + (mValuePct*(mLineW - mSliderW))/100; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 312 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 313 | if (mHandleImage && mHandleImage->GetResource()) |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 314 | { |
| 315 | gr_surface s = mHandleImage->GetResource(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 316 | if (mDragging && mHandleHoverImage && mHandleHoverImage->GetResource()) |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 317 | s = mHandleHoverImage->GetResource(); |
| 318 | gr_blit(s, 0, 0, mSliderW, mSliderH, sliderX, mLineY + (mLineH/2 - mSliderH/2)); |
| 319 | } |
| 320 | else |
| 321 | { |
| 322 | gr_color(mSliderColor.red, mSliderColor.green, mSliderColor.blue, mSliderColor.alpha); |
| 323 | gr_fill(sliderX, mSliderY, mSliderW, mSliderH); |
| 324 | } |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 325 | |
| 326 | void *fontResource = NULL; |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 327 | if (mFont) fontResource = mFont->GetResource(); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 328 | gr_color(mTextColor.red, mTextColor.green, mTextColor.blue, mTextColor.alpha); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 329 | if (mShowRange) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 330 | { |
| 331 | int rangeY = (mLineY - mLineH/2) - mFontHeight/2; |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 332 | gr_textEx_scaleW(mRenderX + mPadding/2, rangeY, mMinStr.c_str(), fontResource, mRenderW, TOP_LEFT, 0); |
| 333 | gr_textEx_scaleW(mLineX + mLineW + mPadding/2, rangeY, mMaxStr.c_str(), fontResource, mRenderW, TOP_LEFT, 0); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 336 | if (mValueStr && mShowCurr) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 337 | { |
| 338 | sprintf(mValueStr, "%d", mValue); |
| 339 | int textW = measureText(mValueStr); |
Ethan Yonker | b7a54a3 | 2015-10-05 10:16:27 -0500 | [diff] [blame] | 340 | gr_textEx_scaleW(mRenderX + (mRenderW/2 - textW/2), mSliderY+mSliderH, mValueStr, fontResource, mRenderW, TOP_LEFT, 0); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | mRendered = true; |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | int GUISliderValue::Update(void) |
| 348 | { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 349 | if (!isConditionTrue()) |
| 350 | return mRendered ? 2 : 0; |
| 351 | if (!mRendered) |
| 352 | return 2; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 353 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 354 | if (mLabel) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 355 | return mLabel->Update(); |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int GUISliderValue::valueFromPct(float pct) |
| 360 | { |
| 361 | int range = abs(mMax - mMin); |
| 362 | return mMin + (pct * range) / 100; |
| 363 | } |
| 364 | |
| 365 | float GUISliderValue::pctFromValue(int value) |
| 366 | { |
| 367 | return float((value - mMin) * 100) / abs(mMax - mMin); |
| 368 | } |
| 369 | |
| 370 | int GUISliderValue::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 371 | { |
Matt Mower | fb1c4ff | 2014-04-16 13:43:36 -0500 | [diff] [blame] | 372 | if (!isConditionTrue()) |
| 373 | return -1; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 374 | |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 375 | switch (state) |
| 376 | { |
| 377 | case TOUCH_START: |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 378 | if (x >= mLineX && x <= mLineX + mLineW && |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 379 | y >= mRenderY && y <= mRenderY + mRenderH) |
| 380 | { |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 381 | mDragging = true; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 382 | } |
| 383 | // no break |
| 384 | case TOUCH_DRAG: |
| 385 | { |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 386 | if (!mDragging) return 0; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 387 | |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 388 | x = std::max(mLineX + mSliderW/2, x); |
| 389 | x = std::min(mLineX + mLineW - mSliderW/2, x); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 390 | |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 391 | mValuePct = float(((x - (mLineX + mSliderW/2)) * 100) / (mLineW - mSliderW)); |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 392 | int newVal = valueFromPct(mValuePct); |
| 393 | if (newVal != mValue) { |
| 394 | mRendered = false; |
| 395 | mValue = newVal; |
| 396 | if (mChangeOnDrag) { |
| 397 | if (!mVariable.empty()) |
| 398 | DataManager::SetValue(mVariable, mValue); |
| 399 | if (mAction) |
| 400 | mAction->doActions(); |
| 401 | } |
| 402 | } |
| 403 | break; |
| 404 | } |
| 405 | case TOUCH_RELEASE: |
| 406 | { |
Vojtech Bocek | 18d7c98 | 2014-08-04 17:19:28 +0200 | [diff] [blame] | 407 | if (!mDragging) return 0; |
| 408 | mDragging = false; |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 409 | |
| 410 | if (!mVariable.empty()) |
| 411 | DataManager::SetValue(mVariable, mValue); |
| 412 | if (mAction) |
| 413 | mAction->doActions(); |
| 414 | break; |
| 415 | } |
| 416 | case TOUCH_REPEAT: |
| 417 | case TOUCH_HOLD: |
| 418 | break; |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 423 | int GUISliderValue::NotifyVarChange(const std::string& varName, const std::string& value) |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 424 | { |
Vojtech Bocek | 0722056 | 2014-02-08 02:05:33 +0100 | [diff] [blame] | 425 | GUIObject::NotifyVarChange(varName, value); |
| 426 | |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 427 | if (mLabel) |
| 428 | mLabel->NotifyVarChange(varName, value); |
| 429 | if (varName == mVariable) { |
| 430 | int newVal = atoi(value.c_str()); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 431 | if (newVal != mValue) { |
Vojtech Bocek | 8593234 | 2013-04-01 22:11:33 +0200 | [diff] [blame] | 432 | mValue = newVal; |
| 433 | mValuePct = pctFromValue(mValue); |
| 434 | mRendered = false; |
| 435 | } |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | void GUISliderValue::SetPageFocus(int inFocus) |
| 441 | { |
| 442 | if (inFocus) |
| 443 | loadValue(); |
| 444 | } |