summaryrefslogtreecommitdiff
path: root/src/game/ToolButton.cpp
blob: 72beed7a74075f78a089e7de1cb708a5468e44f2 (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
/*
 * ToolButton.cpp
 *
 *  Created on: Jan 30, 2012
 *      Author: Simon
 */

#include "ToolButton.h"
#include "interface/Keys.h"

ToolButton::ToolButton(ui::Point position, ui::Point size, std::string text_):
	ui::Button(position, size, text_)
{
	SetSelectionState(-1);
	activeBorder = ui::Colour(255, 0, 0);
}

void ToolButton::OnMouseClick(int x, int y, unsigned int button)
{
    isButtonDown = true;
}

void ToolButton::OnMouseUp(int x, int y, unsigned int button)
{
	if(isButtonDown)
	{
		if(button == BUTTON_LEFT)
			SetSelectionState(0);
		if(button == BUTTON_RIGHT)
			SetSelectionState(1);
		if(button == BUTTON_MIDDLE)
			SetSelectionState(2);
		DoAction();
	}
	isButtonDown = false;
}

void ToolButton::Draw(const ui::Point& screenPos)
{
	Graphics * g = ui::Engine::Ref().g;
	int totalColour = background.Red + (3*background.Green) + (2*background.Blue);

	g->fillrect(screenPos.X+2, screenPos.Y+2, Size.X-4, Size.Y-4, background.Red, background.Green, background.Blue, background.Alpha);

	if(isMouseInside && currentSelection == -1)
	{
		g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, activeBorder.Red, activeBorder.Green, activeBorder.Blue, activeBorder.Alpha);
	}
	else
	{
		g->drawrect(screenPos.X, screenPos.Y, Size.X, Size.Y, border.Red, border.Green, border.Blue, border.Alpha);
	}

	if (totalColour<544)
	{
		g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 255, 255, 255, 255);
	}
	else
	{
		g->drawtext(screenPos.X+textPosition.X, screenPos.Y+textPosition.Y, buttonDisplayText.c_str(), 0, 0, 0, 255);
	}
}

void ToolButton::SetSelectionState(int state)
{
	currentSelection = state;
	switch(state)
	{
	case 0:
		border = ui::Colour(255, 0, 0);
		break;
	case 1:
		border = ui::Colour(0, 0, 255);
		break;
	case 2:
		border = ui::Colour(0, 255, 0);
		break;
	default:
		border = ui::Colour(0, 0, 0);
		break;
	}
}

int ToolButton::GetSelectionState()
{
	return currentSelection;
}

ToolButton::~ToolButton() {
	// TODO Auto-generated destructor stub
}