4
Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
6
This program is free software; you can redistribute it and/or modify it
7
under the terms of the GNU General Public License as published by the
8
Free Software Foundation; either version 2 of the License, or (at your
9
option) any later version.
11
This program is distributed in the hope that it will be useful, but WITHOUT
12
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
You should have received a copy of the GNU General Public License along
17
with this program; if not, write to the Free Software Foundation, Inc.,
18
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
contact: plee-the-bear@gamned.org
22
Please add the tag [Bear] in the subject of your mails.
25
* \file gui/button.cpp
26
* \brief Implementation of the gui::button class.
27
* \author Julien Jorge
29
#include "gui/button.hpp"
31
#include "gui/static_text.hpp"
32
#include "input/keyboard.hpp"
34
/*----------------------------------------------------------------------------*/
37
* \param owner The control owning this one.
38
* \param f The font used to display the text.
39
* \param label The text displayed in the button.
40
* \param c The function called when the button is activated.
42
bear::gui::button::button
43
( visual_component* owner, const font_type& f, const std::string& label,
45
: visual_component(owner), m_text(NULL), m_click_callback(c), m_margin(0)
49
m_text->set_text(label);
53
/*----------------------------------------------------------------------------*/
55
* \brief Set the font of the text.
56
* \param f The new font.
58
void bear::gui::button::set_font( font_type f )
63
} // button::set_font()
65
/*----------------------------------------------------------------------------*/
67
* \brief Set the margin around the text of the control.
68
* \param m The margin.
70
void bear::gui::button::set_margin( size_type m )
75
} // button::set_margin()
77
/*----------------------------------------------------------------------------*/
79
* \brief Set the text of the control.
80
* \param text The new text.
82
void bear::gui::button::set_text( const std::string& text )
85
m_text->set_text(text);
87
} // button::set_text()
89
/*----------------------------------------------------------------------------*/
91
* \brief Get the text of the control.
93
const std::string& bear::gui::button::get_text() const
95
return m_text->get_text();
96
} // button::get_text()
98
/*----------------------------------------------------------------------------*/
100
* \brief Set the callback called when the button is clicked.
101
* \param c The callback.
103
void bear::gui::button::set_callback( const callback& c )
105
m_click_callback = c;
106
} // button::set_callback()
108
/*----------------------------------------------------------------------------*/
110
* \brief Initialize the control.
112
void bear::gui::button::create()
116
m_text = new static_text(this);
117
m_text->set_auto_size(true);
118
} // button::create()
120
/*----------------------------------------------------------------------------*/
122
* \brief Tell that a key has been pressed.
123
* \param key The code of the key.
125
bool bear::gui::button::on_key_press( const bear::input::key_info& key )
129
if ( key.is_enter() || (key.get_code() == input::keyboard::kc_space) )
130
m_click_callback.execute();
135
} // button::on_key_press()
137
/*----------------------------------------------------------------------------*/
139
* \brief Tell that a joystick button has been pressed.
140
* \param joy_button The code of the button.
141
* \param joy_index The index of the joytick.
143
bool bear::gui::button::on_button_press
144
( bear::input::joystick::joy_code joy_button, unsigned int joy_index )
150
case bear::input::joystick::jc_button_1:
151
case bear::input::joystick::jc_button_2:
152
case bear::input::joystick::jc_button_3:
153
case bear::input::joystick::jc_button_4:
154
case bear::input::joystick::jc_button_5:
155
case bear::input::joystick::jc_button_6:
156
case bear::input::joystick::jc_button_7:
157
case bear::input::joystick::jc_button_8:
158
case bear::input::joystick::jc_button_9:
159
case bear::input::joystick::jc_button_10:
160
case bear::input::joystick::jc_button_11:
161
case bear::input::joystick::jc_button_12:
162
case bear::input::joystick::jc_button_13:
163
case bear::input::joystick::jc_button_14:
164
case bear::input::joystick::jc_button_15:
165
case bear::input::joystick::jc_button_16:
166
m_click_callback.execute();
173
} // button::on_button_press()
175
/*----------------------------------------------------------------------------*/
177
* \brief Tell that a mouse button has been pressed.
178
* \param key The code of the button.
179
* \param pos The position of the mouse.
181
bool bear::gui::button::on_mouse_press
182
( input::mouse::mouse_code key,
183
const claw::math::coordinate_2d<unsigned int>& pos )
185
m_click_callback.execute();
187
} // button::on_mouse_press()