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