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