The Battle for Wesnoth  1.19.0-dev
button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
3  by David White <dave@whitevine.net>
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 #pragma once
17 
18 #include "widget.hpp"
19 
20 #include "exceptions.hpp"
21 
22 #include "sdl/texture.hpp"
23 
24 namespace gui {
25 
26 /**
27  * @ingroup GUIWidgetWML
28  *
29  * A button is a control that can be pushed to start an action or close a dialog.
30  *
31  * When a button has a return value it sets the return value for the window.
32  * Normally this closes the window and returns this value to the caller.
33  * The return value can either be defined by the user or determined from the id of the button.
34  * The return value has a higher precedence as the one defined by the id.
35  * (Of course it's weird to give a button an id and then override its return value.)
36  *
37  * When the button doesn't have a standard id, but you still want to use the return value of that id, use return_value_id instead.
38  * This has a higher precedence as return_value.
39  *
40  * List with the button specific variables:
41  * Key |Type |Default|Description
42  * ---------------|--------------------------------|-------|-----------
43  * return_value_id| @ref guivartype_string "string"|"" |The return value id.
44  * return_value | @ref guivartype_int "int" |0 |The return value.
45  * The following states exist:
46  * * state_enabled - the button is enabled.
47  * * state_disabled - the button is disabled.
48  * * state_pressed - the left mouse button is down.
49  * * state_focussed - the mouse is over the button.
50  */
51 class button : public widget
52 {
53 public:
54  struct error : public game::error {
56  : game::error("GUI1 button error")
57  {}
58  };
59 
61  TYPE get_type() const { return type_; }
62 
64 
65  button(const std::string& label, TYPE type=TYPE_PRESS,
66  std::string button_image="", SPACE_CONSUMPTION spacing=DEFAULT_SPACE,
67  const bool auto_join=true, std::string overlay_image="", int font_size = -1);
68 
69 
70  /** Default implementation, but defined out-of-line for efficiency reasons. */
71  virtual ~button();
72  void set_check(bool check);
73  void set_active(bool active);
74  bool checked() const;
75 
76  void set_label(const std::string& val);
77  void set_image(const std::string& image_file_base);
78  void set_overlay(const std::string& image_file_base);
79  void set_image_path_suffix(const std::string& suffix) { button_image_path_suffix_ = suffix; load_images(); }
80 
81  bool pressed();
82  bool hit(int x, int y) const;
83  virtual void enable(bool new_val=true);
84  void release();
85 
86 protected:
87  virtual void handle_event(const SDL_Event& event);
88  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
89  virtual void mouse_down(const SDL_MouseButtonEvent& event);
90  virtual void mouse_up(const SDL_MouseButtonEvent& event);
91  virtual void draw_contents();
92 
94 
95 private:
96 
97  void load_images();
98 
99  void calculate_size();
100 
101  std::string label_text_;
102 
107  SDL_Rect textRect_;
108 
111 
112  bool pressed_;
113 
115 
117 
118  std::string button_image_name_;
121 
126 
127 }; //end class button
128 
129 }
A button is a control that can be pushed to start an action or close a dialog.
Definition: button.hpp:52
int checkbox_horizontal_padding_
Definition: button.hpp:124
void set_overlay(const std::string &image_file_base)
Definition: button.cpp:394
texture image_
Definition: button.hpp:103
int base_width_
Definition: button.hpp:116
virtual void handle_event(const SDL_Event &event)
Definition: button.cpp:536
void load_images()
Definition: button.cpp:84
texture disabledImage_
Definition: button.hpp:104
void set_image_path_suffix(const std::string &suffix)
Definition: button.hpp:79
bool hit(int x, int y) const
Definition: button.cpp:376
texture touchedImage_
Definition: button.hpp:104
bool pressed()
Definition: button.cpp:567
virtual void enable(bool new_val=true)
Definition: button.cpp:272
int font_size_
Definition: button.hpp:122
TYPE type_
Definition: button.hpp:93
int base_height_
Definition: button.hpp:116
button(const std::string &label, TYPE type=TYPE_PRESS, std::string button_image="", SPACE_CONSUMPTION spacing=DEFAULT_SPACE, const bool auto_join=true, std::string overlay_image="", int font_size=-1)
Definition: button.cpp:41
TYPE get_type() const
Definition: button.hpp:61
std::string button_image_path_suffix_
Definition: button.hpp:120
void calculate_size()
Definition: button.cpp:201
texture overlayPressedImage_
Definition: button.hpp:105
virtual void draw_contents()
Definition: button.cpp:286
bool pressed_
Definition: button.hpp:112
void release()
Definition: button.cpp:480
STATE state_
Definition: button.hpp:110
SPACE_CONSUMPTION spacing_
Definition: button.hpp:114
std::string button_image_name_
Definition: button.hpp:118
void set_active(bool active)
Definition: button.cpp:256
SPACE_CONSUMPTION
Definition: button.hpp:63
@ MINIMUM_SPACE
Definition: button.hpp:63
@ DEFAULT_SPACE
Definition: button.hpp:63
texture overlayActiveImage_
Definition: button.hpp:106
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: button.cpp:485
texture activeImage_
Definition: button.hpp:103
std::string label_text_
Definition: button.hpp:101
texture pressedActiveImage_
Definition: button.hpp:103
@ TYPE_TURBO
Definition: button.hpp:60
@ TYPE_PRESS
Definition: button.hpp:60
@ TYPE_IMAGE
Definition: button.hpp:60
@ TYPE_CHECK
Definition: button.hpp:60
@ TYPE_RADIO
Definition: button.hpp:60
texture overlayDisabledImage_
Definition: button.hpp:105
int horizontal_padding_
Definition: button.hpp:123
texture overlayImage_
Definition: button.hpp:105
texture pressedDisabledImage_
Definition: button.hpp:104
void set_image(const std::string &image_file_base)
Definition: button.cpp:383
int vertical_padding_
Definition: button.hpp:125
void set_label(const std::string &val)
Definition: button.cpp:406
virtual void mouse_down(const SDL_MouseButtonEvent &event)
Definition: button.cpp:457
bool checked() const
Definition: button.cpp:267
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
Definition: button.cpp:423
@ TOUCHED_NORMAL
Definition: button.hpp:109
@ TOUCHED_PRESSED
Definition: button.hpp:109
@ PRESSED_ACTIVE
Definition: button.hpp:109
texture pressedImage_
Definition: button.hpp:103
SDL_Rect textRect_
Definition: button.hpp:107
texture overlayPressedDisabledImage_
Definition: button.hpp:105
std::string button_overlay_image_name_
Definition: button.hpp:119
void set_check(bool check)
Definition: button.cpp:238
virtual ~button()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: button.cpp:197
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:33
std::string label
What to show in the filter's drop-down list.
Definition: manager.cpp:209
General purpose widgets.
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29