Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // progressbar.cpp - GUIProgressBar 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" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 21 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 22 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | #include "rapidxml.hpp" |
| 26 | #include "objects.hpp" |
| 27 | |
| 28 | GUIProgressBar::GUIProgressBar(xml_node<>* node) |
| 29 | { |
| 30 | xml_attribute<>* attr; |
| 31 | xml_node<>* child; |
| 32 | |
| 33 | mEmptyBar = NULL; |
| 34 | mFullBar = NULL; |
| 35 | mLastPos = 0; |
| 36 | mSlide = 0.0; |
| 37 | mSlideInc = 0.0; |
| 38 | |
| 39 | if (!node) |
| 40 | { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 41 | LOGERR("GUIProgressBar created without XML node\n"); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
| 45 | child = node->first_node("resource"); |
| 46 | if (child) |
| 47 | { |
| 48 | attr = child->first_attribute("empty"); |
| 49 | if (attr) |
| 50 | mEmptyBar = PageManager::FindResource(attr->value()); |
| 51 | |
| 52 | attr = child->first_attribute("full"); |
| 53 | if (attr) |
| 54 | mFullBar = PageManager::FindResource(attr->value()); |
| 55 | } |
| 56 | |
| 57 | // Load the placement |
| 58 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY); |
| 59 | |
| 60 | // Load the data |
| 61 | child = node->first_node("data"); |
| 62 | if (child) |
| 63 | { |
| 64 | attr = child->first_attribute("min"); |
| 65 | if (attr) mMinValVar = attr->value(); |
| 66 | |
| 67 | attr = child->first_attribute("max"); |
| 68 | if (attr) mMaxValVar = attr->value(); |
| 69 | |
| 70 | attr = child->first_attribute("name"); |
| 71 | if (attr) mCurValVar = attr->value(); |
| 72 | } |
| 73 | |
| 74 | if (mEmptyBar && mEmptyBar->GetResource()) |
| 75 | { |
| 76 | mRenderW = gr_get_width(mEmptyBar->GetResource()); |
| 77 | mRenderH = gr_get_height(mEmptyBar->GetResource()); |
| 78 | } |
| 79 | |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | int GUIProgressBar::Render(void) |
| 84 | { |
| 85 | // This handles making sure timing updates occur |
| 86 | Update(); |
| 87 | return RenderInternal(); |
| 88 | } |
| 89 | |
| 90 | int GUIProgressBar::RenderInternal(void) |
| 91 | { |
| 92 | if (!mEmptyBar || !mEmptyBar->GetResource()) return -1; |
| 93 | if (!mFullBar || !mFullBar->GetResource()) return -1; |
| 94 | |
| 95 | gr_blit(mEmptyBar->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 96 | gr_blit(mFullBar->GetResource(), 0, 0, mLastPos, mRenderH, mRenderX, mRenderY); |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | int GUIProgressBar::Update(void) |
| 101 | { |
| 102 | std::string str; |
| 103 | int min, max, cur, pos; |
| 104 | |
| 105 | if (mMinValVar.empty()) min = 0; |
| 106 | else |
| 107 | { |
| 108 | str.clear(); |
| 109 | if (atoi(mMinValVar.c_str()) != 0) str = mMinValVar; |
| 110 | else DataManager::GetValue(mMinValVar, str); |
| 111 | min = atoi(str.c_str()); |
| 112 | } |
| 113 | |
| 114 | if (mMaxValVar.empty()) max = 100; |
| 115 | else |
| 116 | { |
| 117 | str.clear(); |
| 118 | if (atoi(mMaxValVar.c_str()) != 0) str = mMaxValVar; |
| 119 | else DataManager::GetValue(mMaxValVar, str); |
| 120 | max = atoi(str.c_str()); |
| 121 | } |
| 122 | |
| 123 | str.clear(); |
| 124 | DataManager::GetValue(mCurValVar, str); |
| 125 | cur = atoi(str.c_str()); |
| 126 | |
| 127 | // Do slide, if needed |
| 128 | if (mSlideFrames) |
| 129 | { |
| 130 | mSlide += mSlideInc; |
| 131 | mSlideFrames--; |
| 132 | if (cur != (int) mSlide) |
| 133 | { |
| 134 | cur = (int) mSlide; |
| 135 | DataManager::SetValue(mCurValVar, cur); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // Normalize to 0 |
| 140 | max -= min; |
| 141 | cur -= min; |
| 142 | min = 0; |
| 143 | |
| 144 | if (cur < min) cur = min; |
| 145 | if (cur > max) cur = max; |
| 146 | |
| 147 | if (max == 0) pos = 0; |
| 148 | else pos = (cur * mRenderW) / max; |
| 149 | |
| 150 | if (pos == mLastPos) return 0; |
| 151 | mLastPos = pos; |
| 152 | if (RenderInternal() != 0) return -1; |
| 153 | return 2; |
| 154 | } |
| 155 | |
| 156 | int GUIProgressBar::NotifyVarChange(std::string varName, std::string value) |
| 157 | { |
| 158 | static int nextPush = 0; |
| 159 | |
| 160 | if (varName.empty()) |
| 161 | { |
| 162 | nextPush = 0; |
| 163 | mLastPos = 0; |
| 164 | mSlide = 0.0; |
| 165 | mSlideInc = 0.0; |
| 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | if (varName == "ui_progress_portion" || varName == "ui_progress_frames") |
| 170 | { |
| 171 | std::string str; |
| 172 | int cur; |
| 173 | |
| 174 | if (mSlideFrames) |
| 175 | { |
| 176 | mSlide += (mSlideInc * mSlideFrames); |
| 177 | cur = (int) mSlide; |
| 178 | DataManager::SetValue(mCurValVar, cur); |
| 179 | mSlideFrames = 0; |
| 180 | } |
| 181 | |
| 182 | if (nextPush) |
| 183 | { |
| 184 | mSlide += nextPush; |
| 185 | cur = (int) mSlide; |
| 186 | DataManager::SetValue(mCurValVar, cur); |
| 187 | nextPush = 0; |
| 188 | } |
| 189 | |
| 190 | if (varName == "ui_progress_portion") mSlide = atof(value.c_str()); |
| 191 | else |
| 192 | { |
| 193 | mSlideFrames = atol(value.c_str()); |
| 194 | if (mSlideFrames == 0) |
| 195 | { |
| 196 | // We're just holding this progress until the next push |
| 197 | nextPush = mSlide; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if (mSlide > 0 && mSlideFrames > 0) |
| 202 | { |
| 203 | // Get the current position |
| 204 | str.clear(); |
| 205 | DataManager::GetValue(mCurValVar, str); |
| 206 | cur = atoi(str.c_str()); |
| 207 | |
| 208 | mSlideInc = (float) mSlide / (float) mSlideFrames; |
| 209 | mSlide = cur; |
| 210 | } |
| 211 | } |
| 212 | return 0; |
| 213 | } |
| 214 | |