~ubuntu-branches/ubuntu/trusty/plee-the-bear/trusty-proposed

« back to all changes in this revision

Viewing changes to bear-engine/core/src/gui/code/button.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Julien Jorge, Julien Jorge
  • Date: 2010-11-17 20:13:34 UTC
  • mfrom: (6.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101117201334-o4dp7uq437to7oxb
Tags: 0.5.1-1
[ Julien Jorge ]
* New upstream release (Closes: #565062, #546514).
* Add armhf architecture (Closes: #604689).
* Remove patches integrated upstream: rpath-editors.diff, rpath-game.diff,
  editors-menu-section.diff.
* Bump the Standard-Version, no changes.
* Update my email address.
* Set build dependency of libclaw to 1.6.0.
* Add the missing ${misc:Depends} in debian/control.
* List gettext translations in bear-factory.install and plee-the-bear.install.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Bear Engine
 
3
 
 
4
  Copyright (C) 2005-2010 Julien Jorge, Sebastien Angibaud
 
5
 
 
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.
 
10
 
 
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
 
14
  more details.
 
15
 
 
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
 
19
 
 
20
  contact: plee-the-bear@gamned.org
 
21
 
 
22
  Please add the tag [Bear] in the subject of your mails.
 
23
*/
 
24
/**
 
25
 * \file gui/button.cpp
 
26
 * \brief Implementation of the gui::button class.
 
27
 * \author Julien Jorge
 
28
 */
 
29
#include "gui/button.hpp"
 
30
 
 
31
#include "gui/static_text.hpp"
 
32
#include "input/keyboard.hpp"
 
33
 
 
34
/*----------------------------------------------------------------------------*/
 
35
/**
 
36
 * \brief Constructor.
 
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.
 
41
 */
 
42
bear::gui::button::button
 
43
( visual_component* owner, const font_type& f, const std::string& label,
 
44
  const callback& c )
 
45
  : visual_component(owner), m_text(NULL), m_click_callback(c), m_margin(0)
 
46
{
 
47
  create();
 
48
  m_text->set_font(f);
 
49
  m_text->set_text(label);
 
50
  fit(m_margin);
 
51
} // button::button()
 
52
 
 
53
/*----------------------------------------------------------------------------*/
 
54
/**
 
55
 * \brief Set the font of the text.
 
56
 * \param f The new font.
 
57
 */
 
58
void bear::gui::button::set_font( font_type f )
 
59
{
 
60
  set_size_maximum();
 
61
  m_text->set_font(f);
 
62
  fit(m_margin);
 
63
} // button::set_font()
 
64
 
 
65
/*----------------------------------------------------------------------------*/
 
66
/**
 
67
 * \brief Set the margin around the text of the control.
 
68
 * \param m The margin.
 
69
 */
 
70
void bear::gui::button::set_margin( size_type m )
 
71
{
 
72
  m_margin = m;
 
73
  set_size_maximum();
 
74
  fit(m_margin);
 
75
} // button::set_margin()
 
76
 
 
77
/*----------------------------------------------------------------------------*/
 
78
/**
 
79
 * \brief Set the text of the control.
 
80
 * \param text The new text.
 
81
 */
 
82
void bear::gui::button::set_text( const std::string& text )
 
83
{
 
84
  set_size_maximum();
 
85
  m_text->set_text(text);
 
86
  fit(m_margin);
 
87
} // button::set_text()
 
88
 
 
89
/*----------------------------------------------------------------------------*/
 
90
/**
 
91
 * \brief Get the text of the control.
 
92
 */
 
93
const std::string& bear::gui::button::get_text() const
 
94
{
 
95
  return m_text->get_text();
 
96
} // button::get_text()
 
97
 
 
98
/*----------------------------------------------------------------------------*/
 
99
/**
 
100
 * \brief Set the callback called when the button is clicked.
 
101
 * \param c The callback.
 
102
 */
 
103
void bear::gui::button::set_callback( const callback& c )
 
104
{
 
105
  m_click_callback = c;
 
106
} // button::set_callback()
 
107
 
 
108
/*----------------------------------------------------------------------------*/
 
109
/**
 
110
 * \brief Initialize the control.
 
111
 */
 
112
void bear::gui::button::create()
 
113
{
 
114
  set_size_maximum();
 
115
 
 
116
  m_text = new static_text(this);
 
117
  m_text->set_auto_size(true);
 
118
} // button::create()
 
119
 
 
120
/*----------------------------------------------------------------------------*/
 
121
/**
 
122
 * \brief Tell that a key has been pressed.
 
123
 * \param key The code of the key.
 
124
 */
 
125
bool bear::gui::button::on_key_press( const bear::input::key_info& key )
 
126
{
 
127
  bool result = true;
 
128
 
 
129
  if ( key.is_enter() || (key.get_code() == input::keyboard::kc_space) )
 
130
    m_click_callback.execute();
 
131
  else
 
132
    result = false;
 
133
 
 
134
  return result;
 
135
} // button::on_key_press()
 
136
 
 
137
/*----------------------------------------------------------------------------*/
 
138
/**
 
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.
 
142
 */
 
143
bool bear::gui::button::on_button_press
 
144
( bear::input::joystick::joy_code joy_button, unsigned int joy_index )
 
145
{
 
146
  bool result = true;
 
147
 
 
148
  switch( joy_button )
 
149
    {
 
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();
 
167
      break;
 
168
    default:
 
169
      result = false;
 
170
    }
 
171
 
 
172
  return result;
 
173
} // button::on_button_press()
 
174
 
 
175
/*----------------------------------------------------------------------------*/
 
176
/**
 
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.
 
180
 */
 
181
bool bear::gui::button::on_mouse_press
 
182
( input::mouse::mouse_code key,
 
183
  const claw::math::coordinate_2d<unsigned int>& pos )
 
184
{
 
185
  m_click_callback.execute();
 
186
  return true;
 
187
} // button::on_mouse_press()