The Battle for Wesnoth  1.17.12+dev
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
3  by David White <>
4  Part of the Battle for Wesnoth Project
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,
13  See the COPYING file for more details.
14 */
16 #pragma once
18 #include "widget.hpp"
20 #include "exceptions.hpp"
22 #include "sdl/texture.hpp"
24 namespace gui {
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  };
61  TYPE get_type() const { return type_; }
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);
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;
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(); }
81  bool pressed();
82  bool hit(int x, int y) const;
83  virtual void enable(bool new_val=true);
84  void release();
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();
95 private:
97  void load_images();
99  void calculate_size();
101  std::string label_text_;
107  SDL_Rect textRect_;
112  bool pressed_;
118  std::string button_image_name_;
127 }; //end class button
129 }
std::string button_image_name_
Definition: button.hpp:118
void set_check(bool check)
Definition: button.cpp:239
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: button.cpp:486
std::string button_image_path_suffix_
Definition: button.hpp:120
bool pressed_
Definition: button.hpp:112
int base_height_
Definition: button.hpp:116
void set_label(const std::string &val)
Definition: button.cpp:407
bool hit(int x, int y) const
Definition: button.cpp:377
void set_image(const std::string &image_file_base)
Definition: button.cpp:384
texture pressedActiveImage_
Definition: button.hpp:103
virtual void enable(bool new_val=true)
Definition: button.cpp:273
texture overlayDisabledImage_
Definition: button.hpp:103
General purpose widgets.
texture overlayImage_
Definition: button.hpp:103
void set_image_path_suffix(const std::string &suffix)
Definition: button.hpp:79
int checkbox_horizontal_padding_
Definition: button.hpp:124
texture image_
Definition: button.hpp:103
virtual void mouse_down(const SDL_MouseButtonEvent &event)
Definition: button.cpp:458
void load_images()
Definition: button.cpp:85
virtual void draw_contents()
Definition: button.cpp:287
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
int base_width_
Definition: button.hpp:116
void release()
Definition: button.cpp:481
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:42
std::string button_overlay_image_name_
Definition: button.hpp:119
std::string label
What to show in the filter&#39;s drop-down list.
Definition: manager.cpp:217
texture overlayPressedDisabledImage_
Definition: button.hpp:103
TYPE get_type() const
Definition: button.hpp:61
void calculate_size()
Definition: button.cpp:202
Definition: button.hpp:114
TYPE type_
Definition: button.hpp:93
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
Definition: button.cpp:424
Definition: button.hpp:63
void set_overlay(const std::string &image_file_base)
Definition: button.cpp:395
bool pressed()
Definition: button.cpp:568
texture overlayPressedImage_
Definition: button.hpp:103
bool checked() const
Definition: button.cpp:268
texture pressedImage_
Definition: button.hpp:103
texture touchedImage_
Definition: button.hpp:103
A button is a control that can be pushed to start an action or close a dialog.
Definition: button.hpp:51
virtual void handle_event(const SDL_Event &event)
Definition: button.cpp:537
int font_size_
Definition: button.hpp:122
std::string label_text_
Definition: button.hpp:101
SDL_Rect textRect_
Definition: button.hpp:107
virtual ~button()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: button.cpp:198
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:28
int horizontal_padding_
Definition: button.hpp:123
STATE state_
Definition: button.hpp:110
int vertical_padding_
Definition: button.hpp:125
texture overlayActiveImage_
Definition: button.hpp:103
texture activeImage_
Definition: button.hpp:103
texture pressedDisabledImage_
Definition: button.hpp:103
void set_active(bool active)
Definition: button.cpp:257
texture disabledImage_
Definition: button.hpp:103