Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 1 | // slider.cpp - GUISlider object |
| 2 | // Pulled & ported from https://raw.github.com/agrabren/RecoverWin/master/gui/slider.cpp |
| 3 | |
| 4 | #include <stdarg.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <fcntl.h> |
| 9 | #include <sys/reboot.h> |
| 10 | #include <sys/stat.h> |
| 11 | #include <sys/time.h> |
| 12 | #include <sys/mman.h> |
| 13 | #include <sys/types.h> |
| 14 | #include <sys/ioctl.h> |
| 15 | #include <time.h> |
| 16 | #include <unistd.h> |
| 17 | #include <stdlib.h> |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame^] | 18 | #include "../data.hpp" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 19 | |
| 20 | #include <string> |
| 21 | |
| 22 | extern "C" { |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 23 | #include "../twcommon.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 24 | #include "../minuitwrp/minui.h" |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | #include "rapidxml.hpp" |
| 28 | #include "objects.hpp" |
| 29 | |
| 30 | GUISlider::GUISlider(xml_node<>* node) |
| 31 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 32 | xml_attribute<>* attr; |
| 33 | xml_node<>* child; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 34 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 35 | sAction = NULL; |
| 36 | sSlider = NULL; |
| 37 | sSliderUsed = NULL; |
| 38 | sTouch = NULL; |
| 39 | sTouchW = 20; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 40 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 41 | if (!node) |
| 42 | { |
| 43 | LOGERR("GUISlider created without XML node\n"); |
| 44 | return; |
| 45 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 46 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 47 | child = node->first_node("resource"); |
| 48 | if (child) |
| 49 | { |
| 50 | attr = child->first_attribute("base"); |
| 51 | if (attr) |
| 52 | sSlider = PageManager::FindResource(attr->value()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 53 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 54 | attr = child->first_attribute("used"); |
| 55 | if (attr) |
| 56 | sSliderUsed = PageManager::FindResource(attr->value()); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 57 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 58 | attr = child->first_attribute("touch"); |
| 59 | if (attr) |
| 60 | sTouch = PageManager::FindResource(attr->value()); |
| 61 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 62 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 63 | // Load the placement |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 64 | LoadPlacement(node->first_node("placement"), &mRenderX, &mRenderY); |
| 65 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 66 | if (sSlider && sSlider->GetResource()) |
| 67 | { |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 68 | mRenderW = gr_get_width(sSlider->GetResource()); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 69 | mRenderH = gr_get_height(sSlider->GetResource()); |
| 70 | } |
| 71 | if (sTouch && sTouch->GetResource()) |
| 72 | { |
| 73 | sTouchW = gr_get_width(sTouch->GetResource()); // Width of the "touch image" that follows the touch (arrow) |
| 74 | sTouchH = gr_get_height(sTouch->GetResource()); // Height of the "touch image" that follows the touch (arrow) |
| 75 | } |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 76 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 77 | //LOGINFO("mRenderW: %i mTouchW: %i\n", mRenderW, mTouchW); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 78 | mActionX = mRenderX; |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 79 | mActionY = mRenderY; |
| 80 | mActionW = mRenderW; |
| 81 | mActionH = mRenderH; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 82 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 83 | sAction = new GUIAction(node); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 84 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 85 | sCurTouchX = mRenderX; |
| 86 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | GUISlider::~GUISlider() |
| 90 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 91 | delete sAction; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | int GUISlider::Render(void) |
| 95 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 96 | if (!sSlider || !sSlider->GetResource()) |
| 97 | return -1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 98 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 99 | // Draw the slider |
| 100 | gr_blit(sSlider->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 101 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 102 | // Draw the used |
| 103 | if (sSliderUsed && sSliderUsed->GetResource() && sCurTouchX > mRenderX) |
| 104 | gr_blit(sSliderUsed->GetResource(), 0, 0, sCurTouchX - mRenderX, mRenderH, mRenderX, mRenderY); |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 105 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 106 | // Draw the touch icon |
| 107 | if (sTouch && sTouch->GetResource()) |
| 108 | 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] | 109 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 110 | sUpdate = 0; |
| 111 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | int GUISlider::Update(void) |
| 115 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 116 | if (sUpdate) |
| 117 | return 2; |
| 118 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int GUISlider::NotifyTouch(TOUCH_STATE state, int x, int y) |
| 122 | { |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 123 | static bool dragging = false; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 124 | |
| 125 | switch (state) |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 126 | { |
| 127 | case TOUCH_START: |
| 128 | if (x >= mRenderX && x <= mRenderX + sTouchW && |
| 129 | y >= mRenderY && y <= mRenderY + mRenderH) |
| 130 | { |
| 131 | sCurTouchX = x - (sTouchW / 2); |
| 132 | if (sCurTouchX < mRenderX) |
| 133 | sCurTouchX = mRenderX; |
| 134 | dragging = true; |
| 135 | } |
| 136 | break; |
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 | case TOUCH_DRAG: |
| 139 | if (!dragging) |
| 140 | return 0; |
| 141 | if (y < mRenderY - sTouchH || y > mRenderY + (sTouchH * 2)) |
| 142 | { |
| 143 | sCurTouchX = mRenderX; |
| 144 | dragging = false; |
| 145 | sUpdate = 1; |
| 146 | break; |
| 147 | } |
| 148 | sCurTouchX = x - (sTouchW / 2); |
| 149 | if (sCurTouchX < mRenderX) |
| 150 | sCurTouchX = mRenderX; |
| 151 | if (sCurTouchX > mRenderX + mRenderW - sTouchW) |
| 152 | sCurTouchX = mRenderX + mRenderW - sTouchW; |
| 153 | sUpdate = 1; |
| 154 | break; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 155 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 156 | case TOUCH_RELEASE: |
Samer Diab (S.a.M.e.R_d) | 71e9b04 | 2014-01-07 20:18:47 +0000 | [diff] [blame^] | 157 | DataManager::Vibrate("tw_button_vibrate"); |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 158 | if (!dragging) |
| 159 | return 0; |
| 160 | |
| 161 | if (sCurTouchX >= mRenderX + mRenderW - sTouchW) |
| 162 | sAction->doActions(); |
| 163 | |
| 164 | sCurTouchX = mRenderX; |
| 165 | dragging = false; |
| 166 | sUpdate = 1; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 167 | case TOUCH_REPEAT: |
| 168 | case TOUCH_HOLD: |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | return 0; |
Dees_Troy | 51a0e82 | 2012-09-05 15:24:24 -0400 | [diff] [blame] | 172 | } |