The Battle for Wesnoth  1.17.0-dev
tristate_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 "widgets/widget.hpp"
19 
20 #include "exceptions.hpp"
22 
23 namespace gui {
24 
25 //This button has 4 states, why it is called tristate?
26 class tristate_button : public widget
27 {
28 
29 public:
30 
31  struct error : public game::error {
33  : game::error("GUI1 tristate button error")
34  {}
35  };
36 
38 
41  std::string button_image="",
42  const bool auto_join=true);
43 
44  /** Default implementation, but defined out-of-line for efficiency reasons. */
45  virtual ~tristate_button();
46 
47  void set_pressed(PRESSED_STATE new_pressed_state);
48 
49  // void set_active(bool active);
50 
51  bool pressed();
53 
54  bool hit(int x, int y) const;
55  virtual void enable(bool new_val=true) override;
56  void release();
57 
59  const surface& image)
60  {
61  itemImage_ = image;
62  }
63 
64  void set_item_id(const std::string& id) {
65  item_id_ = id;
66  }
67 
68  void draw() override {
69  widget::draw();
70  }
71 
72 protected:
73 
74  virtual void handle_event(const SDL_Event& event) override;
75  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
76  virtual void mouse_down(const SDL_MouseButtonEvent& event);
77  virtual void mouse_up(const SDL_MouseButtonEvent& event);
78 
79  virtual void draw_contents() override;
80 
81 private:
82 
83  void calculate_size();
84 
86  itemImage_,
87  // normalImage_, activeImage_,
91  // disabledImage_, pressedDownDisabledImage_, pressedUpDisabledImage_, pressedBothDisabledImage_;
92 
93  SDL_Rect textRect_;
94 
95  enum STATE { UNINIT,
100  };
101 
102 
104 
105  bool pressed_;
106 
108 
110 
111  std::string item_id_;
112 
113 }; //end class button
114 
115 }
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:84
virtual ~tristate_button()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: video.hpp:32
void set_item_id(const std::string &id)
General purpose widgets.
editor::tristate_palette * palette_
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:222
virtual void draw_contents() override
virtual void draw()
Definition: widget.cpp:269
CVideo & video() const
Definition: widget.hpp:84
void draw() override
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:28
Functions to load and save images from/to disk.
PRESSED_STATE pressed_state() const
virtual void enable(bool new_val=true) override
tristate_button(CVideo &video, editor::tristate_palette *palette, std::string button_image="", const bool auto_join=true)
void set_item_image(const surface &image)
bool hit(int x, int y) const