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