Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 1 | #include <stdarg.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <sys/reboot.h> |
| 7 | #include <sys/stat.h> |
| 8 | #include <sys/time.h> |
| 9 | #include <sys/mman.h> |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/ioctl.h> |
| 12 | #include <time.h> |
| 13 | #include <unistd.h> |
| 14 | #include <stdlib.h> |
| 15 | |
| 16 | #include <string> |
| 17 | |
| 18 | extern "C" { |
| 19 | #include "../twcommon.h" |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 20 | } |
Ethan Yonker | fbb4353 | 2015-12-28 21:54:50 +0100 | [diff] [blame] | 21 | #include "../minuitwrp/minui.h" |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 22 | |
| 23 | #include "rapidxml.hpp" |
| 24 | #include "objects.hpp" |
| 25 | |
| 26 | MouseCursor::MouseCursor(int resX, int resY) |
| 27 | { |
| 28 | ResetData(resX, resY); |
| 29 | } |
| 30 | |
| 31 | MouseCursor::~MouseCursor() |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void MouseCursor::ResetData(int resX, int resY) |
| 36 | { |
| 37 | m_resX = resX; |
| 38 | m_resY = resY; |
| 39 | m_moved = false; |
| 40 | m_speedMultiplier = 2.5f; |
| 41 | m_image = NULL; |
| 42 | m_present = false; |
| 43 | |
| 44 | ConvertStrToColor("red", &m_color); |
| 45 | |
| 46 | SetRenderPos(resX/2, resY/2, 10, 10); |
| 47 | } |
| 48 | |
| 49 | void MouseCursor::LoadData(xml_node<>* node) |
| 50 | { |
| 51 | xml_attribute<>* attr; |
| 52 | xml_node<>* child; |
| 53 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 54 | child = FindNode(node, "placement"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 55 | if (child) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 56 | LoadPlacement(child, &mRenderX, &mRenderY, &mRenderW, &mRenderH); |
| 57 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 58 | child = FindNode(node, "background"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 59 | if (child) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 60 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 61 | m_color = LoadAttrColor(child, "color", m_color); |
| 62 | m_image = LoadAttrImage(child, "resource"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 63 | if (m_image) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 64 | { |
that | f6ed8fc | 2015-02-14 20:23:16 +0100 | [diff] [blame] | 65 | mRenderW = m_image->GetWidth(); |
| 66 | mRenderH = m_image->GetHeight(); |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
Ethan Yonker | 21ff02a | 2015-02-18 14:35:00 -0600 | [diff] [blame] | 70 | child = FindNode(node, "speed"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 71 | if (child) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 72 | { |
| 73 | attr = child->first_attribute("multiplier"); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 74 | if (attr) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 75 | m_speedMultiplier = atof(attr->value()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int MouseCursor::Render(void) |
| 80 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 81 | if (!m_present) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 82 | return 0; |
| 83 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 84 | if (m_image) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 85 | { |
| 86 | gr_blit(m_image->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY); |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | gr_color(m_color.red, m_color.green, m_color.blue, m_color.alpha); |
| 91 | gr_fill(mRenderX, mRenderY, mRenderW, mRenderH); |
| 92 | } |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int MouseCursor::Update(void) |
| 97 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 98 | if (m_present != ev_has_mouse()) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 99 | { |
| 100 | m_present = ev_has_mouse(); |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 101 | if (m_present) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 102 | SetRenderPos(m_resX/2, m_resY/2); |
| 103 | return 2; |
| 104 | } |
| 105 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 106 | if (m_present && m_moved) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 107 | { |
| 108 | m_moved = false; |
| 109 | return 2; |
| 110 | } |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | int MouseCursor::SetRenderPos(int x, int y, int w, int h) |
| 115 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 116 | if (x == mRenderX && y == mRenderY) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 117 | m_moved = true; |
| 118 | |
| 119 | return RenderObject::SetRenderPos(x, y, w, h); |
| 120 | } |
| 121 | |
| 122 | void MouseCursor::Move(int deltaX, int deltaY) |
| 123 | { |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 124 | if (deltaX != 0) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 125 | { |
| 126 | mRenderX += deltaX*m_speedMultiplier; |
| 127 | mRenderX = (std::min)(mRenderX, m_resX); |
| 128 | mRenderX = (std::max)(mRenderX, 0); |
| 129 | |
| 130 | m_moved = true; |
| 131 | } |
| 132 | |
Matt Mower | a8a89d1 | 2016-12-30 18:10:37 -0600 | [diff] [blame] | 133 | if (deltaY != 0) |
Vojtech Bocek | 1fc30fc | 2014-01-29 18:37:19 +0100 | [diff] [blame] | 134 | { |
| 135 | mRenderY += deltaY*m_speedMultiplier; |
| 136 | mRenderY = (std::min)(mRenderY, m_resY); |
| 137 | mRenderY = (std::max)(mRenderY, 0); |
| 138 | |
| 139 | m_moved = true; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | void MouseCursor::GetPos(int& x, int& y) |
| 144 | { |
| 145 | x = mRenderX; |
| 146 | y = mRenderY; |
| 147 | } |