1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
/*
* Button.cpp
*
* Created on: Jan 8, 2012
* Author: Simon
*/
#include <iostream>
#include "interface/Button.h"
#include "Graphics.h"
namespace ui {
Button::Button(State* parent_state, std::string buttonText):
Component(parent_state),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false)
{
}
Button::Button(Point position, Point size, std::string buttonText):
Component(position, size),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false)
{
}
Button::Button(std::string buttonText):
Component(),
ButtonText(buttonText),
isMouseInside(false),
isButtonDown(false)
{
}
void Button::Draw(const Point& screenPos)
{
Graphics * g = ui::Engine::Ref().g;
// = reinterpret_cast<Graphics*>(userdata);
//TODO: Cache text location, that way we don't have the text alignment code here
if(isButtonDown)
{
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 0, 0, 0, 255);
}
else
{
if(isMouseInside)
g->fillrect(Position.X, Position.Y, Size.X, Size.Y, 20, 20, 20, 255);
g->drawrect(Position.X, Position.Y, Size.X, Size.Y, 255, 255, 255, 255);
g->drawtext(Position.X+(Size.X-Graphics::textwidth((char *)ButtonText.c_str()))/2, Position.Y+(Size.Y-10)/2, ButtonText, 255, 255, 255, 255);
}
/*sf::RenderWindow* rw = reinterpret_cast<sf::RenderWindow*>(userdata); //it better be a RenderWindow or so help your god
//Draw component here
sf::Text textGraphic(ButtonText);
textGraphic.SetCharacterSize(11);
if(isButtonDown)
textGraphic.SetColor(sf::Color::Black);
else
textGraphic.SetColor(sf::Color::White);
sf::FloatRect tempRect = textGraphic.GetRect();
textGraphic.SetPosition(ceil(X + Width/2 - tempRect.Width/2), ceil(Y + Height/2 - tempRect.Height/2));
if(isMouseInside)
{
if(isButtonDown)
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
else
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::Black, 2.f, sf::Color::White));
}
else
{
if(isButtonDown)
rw->Draw(sf::Shape::Rectangle(X+2, Y+2, Width-4, Width-4, sf::Color::White, 2.f, sf::Color::Black));
else
rw->Draw(sf::Shape::Rectangle(X+1, Y+1, Width-2, Width-2, sf::Color::Black, 1.f, sf::Color::White));
}
rw->Draw(textGraphic);*/
}
void Button::OnMouseUnclick(int x, int y, unsigned int button)
{
std::cout << "Unclick!" << std::endl;
if(button != 1)
{
return; //left click only!
}
if(isButtonDown)
{
DoAction();
}
isButtonDown = false;
}
//void Button::OnMouseUp(int x, int y, unsigned int button) //mouse unclick is called before this
//{
// if(button != 1) return; //left click only!
// isButtonDown = false;
//}
void Button::OnMouseClick(int x, int y, unsigned int button)
{
std::cout << "Click!" << std::endl;
if(button != 1) return; //left click only!
isButtonDown = true;
}
void Button::OnMouseEnter(int x, int y)
{
std::cout << "Enter!"<<std::endl;
isMouseInside = true;
}
void Button::OnMouseLeave(int x, int y)
{
std::cout << "Leave!"<<std::endl;
isMouseInside = false;
}
void Button::DoAction()
{
std::cout << "Do action!"<<std::endl;
}
Button::~Button()
{
}
} /* namespace ui */
|