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