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