summaryrefslogtreecommitdiff
path: root/src/interface.old/Button.cpp
blob: a357c363dded0b5c49620795ce9666247fc265b3 (plain)
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
/*
 * Button.cpp
 *
 *  Created on: Jan 8, 2012
 *      Author: Simon
 */

#include <iostream>

#include "interface/Button.h"
#include "Graphics.h"

namespace ui {

Button::Button(int x, int y, int width, int height, const std::string& buttonText):
		Component(x, y, width, height),
		Toggleable(false),
		ButtonText(buttonText),
		isMouseInside(false),
		isButtonDown(false),
		state(false)
{

}

void Button::Draw(void* userdata)
{
	Graphics * g = reinterpret_cast<Graphics*>(userdata);
	//TODO: Cache text location, that way we don't have the text alignment code here
	if(isButtonDown)
	{
		g->fillrect(X, Y, Width, Height, 255, 255, 255, 255);
		g->drawtext(X+(Width-Graphics::textwidth((char *)ButtonText.c_str()))/2, Y+(Height-10)/2, ButtonText, 0, 0, 0, 255);
	}
	else
	{
		if(isMouseInside)
			g->fillrect(X, Y, Width, Height, 20, 20, 20, 255);
		g->drawrect(X, Y, Width, Height, 255, 255, 255, 255);
		g->drawtext(X+(Width-Graphics::textwidth((char *)ButtonText.c_str()))/2, Y+(Height-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)
{
    if(button != 1)
    {
        return; //left click only!
    }

    if(isButtonDown)
    {
        if(state)
        {
            state = false;
        }
        else
        {
            if(Toggleable)
            {
                state = true;
            }
            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)
{
    if(button != 1) return; //left click only!

    isButtonDown = true;
}

void Button::OnMouseEnter(int x, int y, int dx, int dy)
{
    isMouseInside = true;
}

void Button::OnMouseLeave(int x, int y, int dx, int dy)
{
    isMouseInside = false;
}

void Button::DoAction()
{
    std::cout << "Do action!"<<std::endl;
}

} /* namespace ui */