The Battle for Wesnoth  1.17.10+dev
tristate_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2022
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 "widgets/widget.hpp"
19 
20 #include "exceptions.hpp"
22 #include "sdl/texture.hpp"
23 
24 namespace gui {
25 
26 //This button has 4 states, why it is called tristate?
27 class tristate_button : public widget
28 {
29 
30 public:
31 
32  struct error : public game::error {
34  : game::error("GUI1 tristate button error")
35  {}
36  };
37 
39 
42  std::string button_image="",
43  const bool auto_join=true);
44 
45  /** Default implementation, but defined out-of-line for efficiency reasons. */
46  virtual ~tristate_button();
47 
48  void set_pressed(PRESSED_STATE new_pressed_state);
49 
50  // void set_active(bool active);
51 
52  bool pressed();
54 
55  bool hit(int x, int y) const;
56  virtual void enable(bool new_val=true) override;
57  void release();
58 
59  void set_item_image(const texture& base, const texture& over = texture())
60  {
61  itemBaseImage_ = base;
62  itemOverlayImage_ = over;
63  }
64 
65  void set_item_id(const std::string& id) {
66  item_id_ = id;
67  }
68 
69 protected:
70 
71  virtual void handle_event(const SDL_Event& event) override;
72  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
73  virtual void mouse_down(const SDL_MouseButtonEvent& event);
74  virtual void mouse_up(const SDL_MouseButtonEvent& event);
75 
76  virtual void draw_contents() override;
77 
78 private:
79 
80  void calculate_size();
81 
84  // normalImage_, activeImage_,
88  // disabledImage_, pressedDownDisabledImage_, pressedUpDisabledImage_, pressedBothDisabledImage_;
89 
90  SDL_Rect textRect_;
91 
92  enum STATE { UNINIT,
97  };
98 
99 
101 
102  bool pressed_;
103 
105 
107 
108  std::string item_id_;
109 
110 }; //end class button
111 
112 }
virtual void mouse_up(const SDL_MouseButtonEvent &event)
virtual void mouse_down(const SDL_MouseButtonEvent &event)
std::vector< color_t > palette(const color_range &cr)
Creates a reference color palette from a color range.
Definition: color_range.cpp:85
virtual ~tristate_button()
Default implementation, but defined out-of-line for efficiency reasons.
void set_item_id(const std::string &id)
General purpose widgets.
tristate_button(editor::tristate_palette *palette, std::string button_image="", const bool auto_join=true)
editor::tristate_palette * palette_
Wrapper class to encapsulate creation and management of an SDL_Texture.
Definition: texture.hpp:32
void set_pressed(PRESSED_STATE new_pressed_state)
virtual void handle_event(const SDL_Event &event) override
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
const std::string & id() const
Definition: widget.cpp:199
virtual void draw_contents() override
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:28
void set_item_image(const texture &base, const texture &over=texture())
PRESSED_STATE pressed_state() const
virtual void enable(bool new_val=true) override
bool hit(int x, int y) const