The Battle for Wesnoth  1.17.17+dev
repeating_button.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2023
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/core/log.hpp"
21 #include "gui/core/timer.hpp"
23 #include "gui/widgets/settings.hpp"
24 #include "gui/widgets/window.hpp"
25 #include "sound.hpp"
26 #include "wml_exception.hpp"
27 #include "gettext.hpp"
28 
29 #include <functional>
30 
31 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
32 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
33 
34 namespace gui2
35 {
36 
37 // ------------ WIDGET -----------{
38 
39 REGISTER_WIDGET(repeating_button)
40 
41 repeating_button::repeating_button(const implementation::builder_repeating_button& builder)
42  : styled_widget(builder, type())
43  , clickable_item()
44  , state_(ENABLED)
45  , repeat_timer_(0)
46 {
47  connect_signal<event::MOUSE_ENTER>(std::bind(
48  &repeating_button::signal_handler_mouse_enter, this, std::placeholders::_2, std::placeholders::_3));
49  connect_signal<event::MOUSE_LEAVE>(std::bind(
50  &repeating_button::signal_handler_mouse_leave, this, std::placeholders::_2, std::placeholders::_3));
51 
52  connect_signal<event::LEFT_BUTTON_DOWN>(std::bind(
53  &repeating_button::signal_handler_left_button_down, this, std::placeholders::_2, std::placeholders::_3));
54  connect_signal<event::LEFT_BUTTON_UP>(std::bind(
55  &repeating_button::signal_handler_left_button_up, this, std::placeholders::_2, std::placeholders::_3));
56 }
57 
59 {
60  if(repeat_timer_) {
62  }
63 }
64 
66  const event::signal& signal)
67 {
68  connect_signal<event::LEFT_BUTTON_DOWN>(signal);
69 }
70 
72  const event::signal& signal)
73 {
74  disconnect_signal<event::LEFT_BUTTON_DOWN>(signal);
75 }
76 
77 void repeating_button::set_active(const bool active)
78 {
79  if(get_active() != active) {
80  set_state(active ? ENABLED : DISABLED);
81  }
82 }
83 
85 {
86  return state_ != DISABLED;
87 }
88 
90 {
91  return state_;
92 }
93 
95 {
96  if(state != state_) {
97  state_ = state;
98  queue_redraw();
99 
100  if(state_ == DISABLED && repeat_timer_) {
102  repeat_timer_ = 0;
103  }
104  }
105 }
106 
108  bool& handled)
109 {
110  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
111 
113  handled = true;
114 }
115 
117  bool& handled)
118 {
119  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
120 
122  handled = true;
123 }
124 
125 void
127  bool& handled)
128 {
129  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
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  window* window = get_window();
138  if(window) {
140  [this, window](unsigned int) {
142  },true);
143 
145  }
146 
148  }
149 
150  handled = true;
151 }
152 
154  bool& handled)
155 {
156  DBG_GUI_E << LOG_HEADER << ' ' << event << ".";
157 
158  if(repeat_timer_) {
160  repeat_timer_ = 0;
161  }
162 
163  if(get_active()) {
165  }
166  handled = true;
167 }
168 
169 // }---------- DEFINITION ---------{
170 
173 {
174  DBG_GUI_P << "Parsing repeating button " << id;
175 
176  load_resolutions<resolution>(cfg);
177 }
178 
180  : resolution_definition(cfg)
181 {
182  // Note the order should be the same as the enum state_t in
183  // repeating_button.hpp.
184  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_enabled", _("Missing required state for repeating button")));
185  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_disabled", _("Missing required state for repeating button")));
186  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_pressed", _("Missing required state for repeating button")));
187  state.emplace_back(VALIDATE_WML_CHILD(cfg, "state_focused", _("Missing required state for repeating button")));
188 }
189 
190 // }---------- BUILDER -----------{
191 
192 namespace implementation
193 {
194 
195 builder_repeating_button::builder_repeating_button(const config& cfg)
196  : builder_styled_widget(cfg)
197 {
198 }
199 
200 std::unique_ptr<widget> builder_repeating_button::build() const
201 {
202  auto widget = std::make_unique<repeating_button>(*this);
203 
204  DBG_GUI_G << "Window builder: placed repeating button '" << id
205  << "' with definition '" << definition << "'.";
206 
207  return widget;
208 }
209 
210 } // namespace implementation
211 
212 // }------------ END --------------
213 
214 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:161
Small concept class.
bool fire(const ui_event event, widget &target)
Fires an event which has no extra parameters.
Definition: dispatcher.cpp:76
A repeating_button is a control that can be pushed down and repeat a certain action.
void set_state(const state_t state)
virtual unsigned get_state() const override
See styled_widget::get_state.
state_t state_
Current state of the widget.
std::size_t repeat_timer_
The timer for the repeating events.
virtual void set_active(const bool active) override
See styled_widget::set_active.
state_t
Possible states of the widget.
void signal_handler_left_button_down(const event::ui_event event, bool &handled)
void connect_signal_mouse_left_down(const event::signal &signal)
Connects a signal handler for a left mouse button down.
virtual bool get_active() const override
See styled_widget::get_active.
void disconnect_signal_mouse_left_down(const event::signal &signal)
Disconnects a signal handler for a left mouse button down.
void signal_handler_mouse_leave(const event::ui_event event, bool &handled)
void signal_handler_left_button_up(const event::ui_event event, bool &handled)
void signal_handler_mouse_enter(const event::ui_event event, bool &handled)
Base class for all visible items.
Base class for all widgets.
Definition: widget.hpp:54
void queue_redraw()
Indicates that this widget should be redrawn.
Definition: widget.cpp:442
window * get_window()
Get the parent window.
Definition: widget.cpp:118
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
void mouse_capture(const bool capture=true)
Definition: window.cpp:1124
static std::string _(const char *str)
Definition: gettext.hpp:93
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
#define DBG_GUI_P
Definition: log.hpp:66
#define DBG_GUI_E
Definition: log.hpp:35
This file contains the window object, this object is a top level container which has the event manage...
ui_event
The event sent to the dispatcher.
Definition: handler.hpp:115
@ LEFT_BUTTON_DOWN
Definition: handler.hpp:120
dispatcher_callback<> signal
Used for events in event_category::general.
Definition: dispatcher.hpp:58
std::string sound_button_click
Definition: settings.cpp:48
unsigned repeat_button_repeat_time
Definition: settings.cpp:46
Generic file dialog.
std::size_t add_timer(const uint32_t interval, const std::function< void(std::size_t id)> &callback, const bool repeat)
Adds a new timer.
Definition: timer.cpp:127
bool remove_timer(const std::size_t id)
Removes a timer.
Definition: timer.cpp:168
Contains the implementation details for lexical_cast and shouldn't be used directly.
void play_UI_sound(const std::string &files)
Definition: sound.cpp:1066
#define REGISTER_WIDGET(id)
Wrapper for REGISTER_WIDGET3.
#define LOG_HEADER
This file contains the settings handling of the widget library.
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.
repeating_button_definition(const config &cfg)
Base class of a resolution, contains the common keys for a resolution.
std::vector< state_definition > state
Contains the gui2 timer routines.
Add a special kind of assert to validate whether the input from WML doesn't contain any problems that...
#define VALIDATE_WML_CHILD(cfg, key, message)