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