The Battle for Wesnoth  1.15.0-dev
tristate_button.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2018 by David White <dave@whitevine.net>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #pragma once
16 
17 #include "widgets/widget.hpp"
18 
19 #include "exceptions.hpp"
21 
22 namespace gui {
23 
24 //This button has 4 states, why it is called tristate?
25 class tristate_button : public widget
26 {
27 
28 public:
29 
30  struct error : public game::error {
32  : game::error("GUI1 tristate button error")
33  {}
34  };
35 
37 
40  std::string button_image="",
41  const bool auto_join=true);
42 
43  /** Default implementation, but defined out-of-line for efficiency reasons. */
44  virtual ~tristate_button();
45 
46  void set_pressed(PRESSED_STATE new_pressed_state);
47 
48  // void set_active(bool active);
49 
50  bool pressed();
52 
53  bool hit(int x, int y) const;
54  virtual void enable(bool new_val=true) override;
55  void release();
56 
58  const surface& image)
59  {
60  itemImage_ = image;
61  }
62 
63  void set_item_id(const std::string& id) {
64  item_id_ = id;
65  }
66 
67  void draw() override {
68  widget::draw();
69  }
70 
71 protected:
72 
73  virtual void handle_event(const SDL_Event& event) override;
74  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
75  virtual void mouse_down(const SDL_MouseButtonEvent& event);
76  virtual void mouse_up(const SDL_MouseButtonEvent& event);
77 
78  virtual void draw_contents() override;
79 
80 private:
81 
82  void calculate_size();
83 
85  itemImage_,
86  // normalImage_, activeImage_,
90  // disabledImage_, pressedDownDisabledImage_, pressedUpDisabledImage_, pressedBothDisabledImage_;
91 
92  SDL_Rect textRect_;
93 
94  enum STATE { UNINIT,
99  };
100 
101 
103 
104  bool pressed_;
105 
107 
109 
110  std::string item_id_;
111 
112 }; //end class button
113 
114 }
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:83
virtual ~tristate_button()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: video.hpp:31
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:230
virtual void draw_contents() override
virtual void draw()
Definition: widget.cpp:277
CVideo & video() const
Definition: widget.hpp:84
void draw() override
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
this module manages the cache of images.
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