blob: 84e6322a4dc46c6c3adee4d264f4664c81a57ebd [file] [log] [blame]
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +01001#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
18extern "C" {
19#include "../twcommon.h"
20#include "../minuitwrp/minui.h"
21}
22
23#include "rapidxml.hpp"
24#include "objects.hpp"
25
26MouseCursor::MouseCursor(int resX, int resY)
27{
28 ResetData(resX, resY);
29}
30
31MouseCursor::~MouseCursor()
32{
33}
34
35void 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
49void MouseCursor::LoadData(xml_node<>* node)
50{
51 xml_attribute<>* attr;
52 xml_node<>* child;
53
Ethan Yonker21ff02a2015-02-18 14:35:00 -060054 child = FindNode(node, "placement");
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010055 if(child)
56 LoadPlacement(child, &mRenderX, &mRenderY, &mRenderW, &mRenderH);
57
Ethan Yonker21ff02a2015-02-18 14:35:00 -060058 child = FindNode(node, "background");
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010059 if(child)
60 {
thatf6ed8fc2015-02-14 20:23:16 +010061 m_color = LoadAttrColor(child, "color", m_color);
62 m_image = LoadAttrImage(child, "resource");
63 if(m_image)
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010064 {
thatf6ed8fc2015-02-14 20:23:16 +010065 mRenderW = m_image->GetWidth();
66 mRenderH = m_image->GetHeight();
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010067 }
68 }
69
Ethan Yonker21ff02a2015-02-18 14:35:00 -060070 child = FindNode(node, "speed");
Vojtech Bocek1fc30fc2014-01-29 18:37:19 +010071 if(child)
72 {
73 attr = child->first_attribute("multiplier");
74 if(attr)
75 m_speedMultiplier = atof(attr->value());
76 }
77}
78
79int MouseCursor::Render(void)
80{
81 if(!m_present)
82 return 0;
83
84 if(m_image)
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
96int MouseCursor::Update(void)
97{
98 if(m_present != ev_has_mouse())
99 {
100 m_present = ev_has_mouse();
101 if(m_present)
102 SetRenderPos(m_resX/2, m_resY/2);
103 return 2;
104 }
105
106 if(m_present && m_moved)
107 {
108 m_moved = false;
109 return 2;
110 }
111 return 0;
112}
113
114int MouseCursor::SetRenderPos(int x, int y, int w, int h)
115{
116 if(x == mRenderX && y == mRenderY)
117 m_moved = true;
118
119 return RenderObject::SetRenderPos(x, y, w, h);
120}
121
122void MouseCursor::Move(int deltaX, int deltaY)
123{
124 if(deltaX != 0)
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
133 if(deltaY != 0)
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
143void MouseCursor::GetPos(int& x, int& y)
144{
145 x = mRenderX;
146 y = mRenderY;
147}