The Battle for Wesnoth  1.19.5+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 class button : public widget
27 {
28 public:
30  TYPE get_type() const { return type_; }
31 
33 
34  button(const std::string& label, TYPE type=TYPE_PRESS,
35  std::string button_image="", SPACE_CONSUMPTION spacing=DEFAULT_SPACE,
36  const bool auto_join=true, std::string overlay_image="", int font_size = -1);
37 
38 
39  /** Default implementation, but defined out-of-line for efficiency reasons. */
40  virtual ~button();
41  void set_check(bool check);
42  void set_active(bool active);
43  bool checked() const;
44 
45  void set_label(const std::string& val);
46  void set_image(const std::string& image_file_base);
47  void set_overlay(const std::string& image_file_base);
48  void set_image_path_suffix(const std::string& suffix) { button_image_path_suffix_ = suffix; load_images(); }
49 
50  bool pressed();
51  bool hit(int x, int y) const;
52  virtual void enable(bool new_val=true);
53  void release();
54 
55 protected:
56  virtual void handle_event(const SDL_Event& event);
57  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
58  virtual void mouse_down(const SDL_MouseButtonEvent& event);
59  virtual void mouse_up(const SDL_MouseButtonEvent& event);
60  virtual void draw_contents();
61 
63 
64 private:
65 
66  void load_images();
67 
68  void calculate_size();
69 
70  std::string label_text_;
71 
76  SDL_Rect textRect_;
77 
80 
81  bool pressed_;
82 
84 
86 
87  std::string button_image_name_;
90 
95 
96 }; //end class button
97 
98 }
int checkbox_horizontal_padding_
Definition: button.hpp:93
void set_overlay(const std::string &image_file_base)
Definition: button.cpp:394
texture image_
Definition: button.hpp:72
int base_width_
Definition: button.hpp:85
virtual void handle_event(const SDL_Event &event)
Definition: button.cpp:536
void load_images()
Definition: button.cpp:84
texture disabledImage_
Definition: button.hpp:73
void set_image_path_suffix(const std::string &suffix)
Definition: button.hpp:48
bool hit(int x, int y) const
Definition: button.cpp:376
texture touchedImage_
Definition: button.hpp:73
bool pressed()
Definition: button.cpp:567
virtual void enable(bool new_val=true)
Definition: button.cpp:272
int font_size_
Definition: button.hpp:91
TYPE type_
Definition: button.hpp:62
int base_height_
Definition: button.hpp:85
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:30
std::string button_image_path_suffix_
Definition: button.hpp:89
void calculate_size()
Definition: button.cpp:201
texture overlayPressedImage_
Definition: button.hpp:74
virtual void draw_contents()
Definition: button.cpp:286
bool pressed_
Definition: button.hpp:81
void release()
Definition: button.cpp:480
STATE state_
Definition: button.hpp:79
SPACE_CONSUMPTION spacing_
Definition: button.hpp:83
std::string button_image_name_
Definition: button.hpp:87
void set_active(bool active)
Definition: button.cpp:256
SPACE_CONSUMPTION
Definition: button.hpp:32
@ MINIMUM_SPACE
Definition: button.hpp:32
@ DEFAULT_SPACE
Definition: button.hpp:32
texture overlayActiveImage_
Definition: button.hpp:75
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: button.cpp:485
texture activeImage_
Definition: button.hpp:72
std::string label_text_
Definition: button.hpp:70
texture pressedActiveImage_
Definition: button.hpp:72
@ TYPE_TURBO
Definition: button.hpp:29
@ TYPE_PRESS
Definition: button.hpp:29
@ TYPE_IMAGE
Definition: button.hpp:29
@ TYPE_CHECK
Definition: button.hpp:29
@ TYPE_RADIO
Definition: button.hpp:29
texture overlayDisabledImage_
Definition: button.hpp:74
int horizontal_padding_
Definition: button.hpp:92
texture overlayImage_
Definition: button.hpp:74
texture pressedDisabledImage_
Definition: button.hpp:73
void set_image(const std::string &image_file_base)
Definition: button.cpp:383
int vertical_padding_
Definition: button.hpp:94
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:78
@ TOUCHED_PRESSED
Definition: button.hpp:78
@ PRESSED_ACTIVE
Definition: button.hpp:78
texture pressedImage_
Definition: button.hpp:72
SDL_Rect textRect_
Definition: button.hpp:76
texture overlayPressedDisabledImage_
Definition: button.hpp:74
std::string button_overlay_image_name_
Definition: button.hpp:88
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:200
General purpose widgets.