Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: f0e449a78ae502a34fd4691a9e34d39535120285 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
   client/messagebox.cc
   This file is part of the Osirion project and is distributed under
   the terms and conditions of the GNU General Public License version 2
*/

#include "audio/audio.h"
#include "client/messagebox.h"
#include "core/commandbuffer.h"
#include "ui/ui.h"

namespace client
{

Messagebox::Messagebox(ui::Widget * parent) :
	ui::Window(parent)
{
	set_border(false);
	set_background(false);
	set_label("messagebox");
	
	messagebox_frame = new ui::Window(this);
	messagebox_frame->set_border(true);
	messagebox_frame->set_background(true);
	
	messagebox_label = new ui::Label(messagebox_frame);
	messagebox_label->set_border(false);
	messagebox_label->set_background(false);
	messagebox_label->set_alignment(ui::AlignTop | ui::AlignHCenter);
	
	messagebox_button1 = new ui::Button(messagebox_frame);
	messagebox_button2 = new ui::Button(messagebox_frame);
}

Messagebox::~Messagebox()
{
}

void Messagebox::set_text(const std::string &text)
{
	messagebox_label->set_text(text);
}
	
void Messagebox::set_buttons(const std::string &text1, const std::string &command1, const std::string &text2, const std::string &command2)
{
	if (text1.size()) {
		messagebox_button1->set_text(text1);
	} else {
		messagebox_button1->set_text("Close");
	}	
	if (command1.size()) {
		messagebox_button1->set_command("remote " + command1);
	} else {
		messagebox_button1->set_command("");
	}
	
	messagebox_button2->set_text(text2);
	if (command2.size()) {
		messagebox_button2->set_command("remote " + command2);
	} else {
		messagebox_button2->set_command("");
	}
	
	if (text2.size()) {
		messagebox_button2->show();
	} else {
		messagebox_button2->hide();
	}	
}

void Messagebox::resize()
{	
	const float padding =  ui::root()->font_large()->height();
	
	set_size(parent()->size());
	
	messagebox_frame->set_size(
		ui::UI::elementsize.width() * 3.0f, 
		ui::UI::elementsize.width() * 1.5f
       	);	
	messagebox_frame->set_location(
		(width() - messagebox_frame->width()) * 0.5f,
		(height() - messagebox_frame->height()) * 0.5f
	);

	messagebox_label->set_size(messagebox_frame->width() - padding * 2.0f, messagebox_frame->height() - padding * 2.0f);
	messagebox_label->set_location(padding, padding);
	
	messagebox_button1->set_size(ui::UI::elementsize);
	messagebox_button2->set_size(ui::UI::elementsize);
	
	if (messagebox_button2->visible()) {
		const float l = (messagebox_frame->width() - messagebox_button1->width() - messagebox_button2->width() - padding * 2.0f) * 0.5f;
		messagebox_button1->set_location(l, messagebox_frame->height() - messagebox_button1->height() - padding);
		messagebox_button2->set_location(messagebox_button1->right() + padding, messagebox_frame->height() - messagebox_button2->height() - padding);
	} else {
		messagebox_button1->set_location((messagebox_frame->width() - messagebox_button1->width()) * 0.5f, messagebox_frame->height() - messagebox_button1->height() - padding);
	}
}

bool Messagebox::on_emit(Widget *sender, const Event event, void *data)
{
	if ((sender == messagebox_button1) || (sender == messagebox_button2)) {
		if (event == Widget::EventButtonClicked)  {
			hide();	
		}
		return true;
	}
	
	return false;
}

bool Messagebox::on_keypress(const int key, const unsigned int modifier)
{
	std::string command;
	
	switch (key) {
		case SDLK_RETURN:
		case SDLK_KP_ENTER:
			command.assign(messagebox_button1->command());
			if (command.size()) {
				core::cmd() << command << std::endl;
			}
			audio::play("ui/clicked");
			hide();
			return true;
			break;
			
		case SDLK_ESCAPE:
			if (messagebox_button2->visible()) {
				command.assign(messagebox_button2->command());
			} else {
				command.assign(messagebox_button1->command());
			}
			if (command.size()) {
				core::cmd() << command << std::endl;
			}
			audio::play("ui/clicked");
			hide();
			return true;
			break;
			
		default:
			break;
	}

	return Window::on_keypress(key, modifier);
}

} // namespace client