blob: 1c22356f98ccb3255439d3724df4959d026b6743 [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
54 child = node->first_node("placement");
55 if(child)
56 LoadPlacement(child, &mRenderX, &mRenderY, &mRenderW, &mRenderH);
57
58 child = node->first_node("background");
59 if(child)
60 {
61 attr = child->first_attribute("color");
62 if(attr)
63 ConvertStrToColor(attr->value(), &m_color);
64
65 attr = child->first_attribute("resource");
66 if(attr)
67 {
68 m_image = PageManager::FindResource(attr->value());
69 if(m_image)
70 {
71 mRenderW = gr_get_width(m_image->GetResource());
72 mRenderH = gr_get_height(m_image->GetResource());
73 }
74 }
75 }
76
77 child = node->first_node("speed");
78 if(child)
79 {
80 attr = child->first_attribute("multiplier");
81 if(attr)
82 m_speedMultiplier = atof(attr->value());
83 }
84}
85
86int MouseCursor::Render(void)
87{
88 if(!m_present)
89 return 0;
90
91 if(m_image)
92 {
93 gr_blit(m_image->GetResource(), 0, 0, mRenderW, mRenderH, mRenderX, mRenderY);
94 }
95 else
96 {
97 gr_color(m_color.red, m_color.green, m_color.blue, m_color.alpha);
98 gr_fill(mRenderX, mRenderY, mRenderW, mRenderH);
99 }
100 return 0;
101}
102
103int MouseCursor::Update(void)
104{
105 if(m_present != ev_has_mouse())
106 {
107 m_present = ev_has_mouse();
108 if(m_present)
109 SetRenderPos(m_resX/2, m_resY/2);
110 return 2;
111 }
112
113 if(m_present && m_moved)
114 {
115 m_moved = false;
116 return 2;
117 }
118 return 0;
119}
120
121int MouseCursor::SetRenderPos(int x, int y, int w, int h)
122{
123 if(x == mRenderX && y == mRenderY)
124 m_moved = true;
125
126 return RenderObject::SetRenderPos(x, y, w, h);
127}
128
129void MouseCursor::Move(int deltaX, int deltaY)
130{
131 if(deltaX != 0)
132 {
133 mRenderX += deltaX*m_speedMultiplier;
134 mRenderX = (std::min)(mRenderX, m_resX);
135 mRenderX = (std::max)(mRenderX, 0);
136
137 m_moved = true;
138 }
139
140 if(deltaY != 0)
141 {
142 mRenderY += deltaY*m_speedMultiplier;
143 mRenderY = (std::min)(mRenderY, m_resY);
144 mRenderY = (std::max)(mRenderY, 0);
145
146 m_moved = true;
147 }
148}
149
150void MouseCursor::GetPos(int& x, int& y)
151{
152 x = mRenderX;
153 y = mRenderY;
154}