The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
repeating_button.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "gui/core/log.hpp"
20 #include "gui/core/timer.hpp"
22 #include "gui/widgets/settings.hpp"
23 #include "gui/widgets/window.hpp"
24 #include "sound.hpp"
25 
26 #include "utils/functional.hpp"
27 
28 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
29 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
36 REGISTER_WIDGET(repeating_button)
37 
39  : tcontrol(COUNT), tclickable_(), state_(ENABLED), repeat_timer_(0)
40 {
41  connect_signal<event::MOUSE_ENTER>(std::bind(
43  connect_signal<event::MOUSE_LEAVE>(std::bind(
45 
46  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
48  connect_signal<event::LEFT_BUTTON_UP>(std::bind(
50 }
51 
53 {
54  if(repeat_timer_) {
56  }
57 }
58 
60  const event::tsignal_function& signal)
61 {
62  connect_signal<event::LEFT_BUTTON_DOWN>(signal);
63 }
64 
66  const event::tsignal_function& signal)
67 {
68  disconnect_signal<event::LEFT_BUTTON_DOWN>(signal);
69 }
70 
71 void trepeating_button::set_active(const bool active)
72 {
73  if(get_active() != active) {
74  set_state(active ? ENABLED : DISABLED);
75  }
76 }
77 
79 {
80  return state_ != DISABLED;
81 }
82 
84 {
85  return state_;
86 }
87 
89 {
90  if(state != state_) {
91  state_ = state;
92  set_is_dirty(true);
93 
94  if(state_ == DISABLED && repeat_timer_) {
96  repeat_timer_ = 0;
97  }
98  }
99 }
100 
102 {
103  static const std::string type = "repeating_button";
104  return type;
105 }
106 
108  bool& handled)
109 {
110  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
111 
113  handled = true;
114 }
115 
117  bool& handled)
118 {
119  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
120 
122  handled = true;
123 }
124 
125 void
127  bool& handled)
128 {
129  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
130 
131  // If the timer isn't set it's the initial down event.
132  if(!repeat_timer_) {
133 
134  // mimic the old gui and only play the sound once.
136 
137  twindow* window = get_window();
138  if(window) {
140  [this, window](unsigned int) {
141  window->fire(event::LEFT_BUTTON_DOWN, *this);
142  },true);
143 
144  window->mouse_capture();
145  }
146 
148  }
149 
150  handled = true;
151 }
152 
154  bool& handled)
155 {
156  DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n";
157 
158  if(repeat_timer_) {
160  repeat_timer_ = 0;
161  }
162 
163  if(get_active()) {
165  }
166  handled = true;
167 }
168 
169 // }---------- DEFINITION ---------{
170 
172  : tcontrol_definition(cfg)
173 {
174  DBG_GUI_P << "Parsing repeating button " << id << '\n';
175 
176  load_resolutions<tresolution>(cfg);
177 }
178 
179 /*WIKI
180  * @page = GUIWidgetDefinitionWML
181  * @order = 1_repeating_button
182  *
183  * == Repeating button ==
184  *
185  * @macro = repeating_button_description
186  *
187  * The following states exist:
188  * * state_enabled, the repeating_button is enabled.
189  * * state_disabled, the repeating_button is disabled.
190  * * state_pressed, the left mouse repeating_button is down.
191  * * state_focused, the mouse is over the repeating_button.
192  * @begin{parent}{name="gui/"}
193  * @begin{tag}{name="repeating_button_definition"}{min=0}{max=-1}{super="generic/widget_definition"}
194  * @begin{tag}{name="resolution"}{min=0}{max=-1}{super="generic/widget_definition/resolution"}
195  * @begin{tag}{name="state_enabled"}{min=0}{max=1}{super="generic/state"}
196  * @end{tag}{name="state_enabled"}
197  * @begin{tag}{name="state_disabled"}{min=0}{max=1}{super="generic/state"}
198  * @end{tag}{name="state_disabled"}
199  * @begin{tag}{name="state_pressed"}{min=0}{max=1}{super="generic/state"}
200  * @end{tag}{name="state_pressed"}
201  * @begin{tag}{name="state_focused"}{min=0}{max=1}{super="generic/state"}
202  * @end{tag}{name="state_focused"}
203  * @end{tag}{name="resolution"}
204  * @end{tag}{name="repeating_button_definition"}
205  * @end{parent}{name="gui/"}
206  */
209 {
210  // Note the order should be the same as the enum tstate in
211  // repeating_button.hpp.
212  state.push_back(tstate_definition(cfg.child("state_enabled")));
213  state.push_back(tstate_definition(cfg.child("state_disabled")));
214  state.push_back(tstate_definition(cfg.child("state_pressed")));
215  state.push_back(tstate_definition(cfg.child("state_focused")));
216 }
217 
218 // }---------- BUILDER -----------{
219 
220 /*WIKI_MACRO
221  * @begin{macro}{repeating_button_description}
222  *
223  * A repeating_button is a control that can be pushed down and repeat a
224  * certain action. Once the button is down every x milliseconds it is
225  * down a new down event is triggered.
226  * @end{macro}
227  */
228 
229 /*WIKI
230  * @page = GUIWidgetInstanceWML
231  * @order = 2_repeating_button
232  *
233  * == Repeating button ==
234  *
235  * @macro = repeating_button_description
236  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
237  * @begin{tag}{name="repeating_button"}{min=0}{max=-1}{super="gui/window/resolution/grid/row/column/button"}
238  * @end{tag}{name="repeating_button"}
239  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
240  */
241 
242 namespace implementation
243 {
244 
245 tbuilder_repeating_button::tbuilder_repeating_button(const config& cfg)
246  : tbuilder_control(cfg)
247 {
248 }
249 
251 {
252  trepeating_button* widget = new trepeating_button();
253 
254  init_control(widget);
255 
256  DBG_GUI_G << "Window builder: placed repeating button '" << id
257  << "' with definition '" << definition << "'.\n";
258 
259  return widget;
260 }
261 
262 } // namespace implementation
263 
264 // }------------ END --------------
265 
266 } // namespace gui2
Define the common log macros for the gui toolkit.
#define DBG_GUI_P
Definition: log.hpp:69
void signal_handler_left_button_down(const event::tevent event, bool &handled)
void signal_handler_mouse_enter(const event::tevent event, bool &handled)
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
This file contains the window object, this object is a top level container which has the event manage...
virtual unsigned get_state() const override
See tcontrol::get_state.
std::string sound_button_click
Definition: settings.cpp:58
bool remove_timer(const size_t id)
Removes a timer.
Definition: timer.cpp:144
A left mouse button down event for a widget.
Definition: handler.hpp:76
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
virtual bool get_active() const override
See tcontrol::get_active.
Base class of a resolution, contains the common keys for a resolution.
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
unsigned repeat_button_repeat_time
Definition: settings.cpp:56
void disconnect_signal_mouse_left_down(const event::tsignal_function &signal)
Disconnects a signal handler for a left mouse button down.
#define LOG_HEADER
This file contains the settings handling of the widget library.
tstate
Possible states of the widget.
bool fire(const tevent event, twidget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:144
void connect_signal_mouse_left_down(const event::tsignal_function &signal)
Connects a signal handler for a left mouse button down.
tstate state_
Current state of the widget.
std::function< void(tdispatcher &dispatcher, const tevent event, bool &handled, bool &halt)> tsignal_function
Callback function signature.
Definition: dispatcher.hpp:40
tevent
The event send to the dispatcher.
Definition: handler.hpp:54
Contains the state info for a resolution.
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
Small concept class.
Definition: clickable.hpp:39
#define DBG_GUI_E
Definition: log.hpp:35
Contains the gui2 timer routines.
void signal_handler_mouse_leave(const event::tevent event, bool &handled)
void signal_handler_left_button_up(const event::tevent event, bool &handled)
Base class for all visible items.
Definition: control.hpp:34
virtual void set_active(const bool active) override
See tcontrol::set_active.
std::vector< tstate_definition > state
cl_event event
Definition: glew.h:3070
config & child(const std::string &key, int n=0)
Returns the nth child with the given key, or a reference to an invalid config if there is none...
Definition: config.cpp:658
Base class for all widgets.
Definition: widget.hpp:49
void play_UI_sound(const std::string &files)
Definition: sound.cpp:842
void mouse_capture(const bool capture=true)
Definition: window.cpp:1388
void set_state(const tstate state)
trepeating_button_definition(const config &cfg)
twindow * get_window()
Get the parent window.
Definition: widget.cpp:116
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
size_t add_timer(const Uint32 interval, const std::function< void(size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:112
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
size_t repeat_timer_
The timer for the repeating events.