The Battle for Wesnoth  1.15.0+dev
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 http://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 "widget.hpp"
18 
19 #include "exceptions.hpp"
20 
21 
22 namespace gui {
23 
24 class button : public widget
25 {
26 public:
27  struct error : public game::error {
29  : game::error("GUI1 button error")
30  {}
31  };
32 
34  TYPE get_type() const { return type_; }
35 
37 
38  button(CVideo& video, const std::string& label, TYPE type=TYPE_PRESS,
39  std::string button_image="", SPACE_CONSUMPTION spacing=DEFAULT_SPACE,
40  const bool auto_join=true, std::string overlay_image="");
41 
42 
43  /** Default implementation, but defined out-of-line for efficiency reasons. */
44  virtual ~button();
45  void set_check(bool check);
46  void set_active(bool active);
47  bool checked() const;
48 
49  void set_label(const std::string& val);
50  void set_image(const std::string& image_file_base);
51  void set_overlay(const std::string& image_file_base);
52  void set_image_path_suffix(const std::string& suffix) { button_image_path_suffix_ = suffix; load_images(); }
53 
54  bool pressed();
55  bool hit(int x, int y) const;
56  virtual void enable(bool new_val=true);
57  void release();
58 
59 protected:
60  virtual void handle_event(const SDL_Event& event);
61  virtual void mouse_motion(const SDL_MouseMotionEvent& event);
62  virtual void mouse_down(const SDL_MouseButtonEvent& event);
63  virtual void mouse_up(const SDL_MouseButtonEvent& event);
64  virtual void draw_contents();
65 
67 
68 private:
69 
70  void load_images();
71 
72  void calculate_size();
73 
74  std::string label_text_;
75 
80  SDL_Rect textRect_;
81 
84 
85  bool pressed_;
86 
88 
90 
91  std::string button_image_name_;
94 
95 }; //end class button
96 
97 }
std::string button_image_name_
Definition: button.hpp:91
void set_check(bool check)
Definition: button.cpp:257
virtual void mouse_up(const SDL_MouseButtonEvent &event)
Definition: button.cpp:499
button(CVideo &video, 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="")
Definition: button.cpp:45
std::string button_image_path_suffix_
Definition: button.hpp:93
static void check(LexState *ls, int c)
Definition: lparser.cpp:106
bool pressed_
Definition: button.hpp:85
int base_height_
Definition: button.hpp:89
void set_label(const std::string &val)
Definition: button.cpp:419
surface pressedActiveImage_
Definition: button.hpp:76
bool hit(int x, int y) const
Definition: button.cpp:389
void set_image(const std::string &image_file_base)
Definition: button.cpp:396
surface pressedImage_
Definition: button.hpp:76
Definition: video.hpp:31
virtual void enable(bool new_val=true)
Definition: button.cpp:291
surface overlayPressedImage_
Definition: button.hpp:76
surface activeImage_
Definition: button.hpp:76
General purpose widgets.
void set_image_path_suffix(const std::string &suffix)
Definition: button.hpp:52
surface disabledImage_
Definition: button.hpp:76
virtual void mouse_down(const SDL_MouseButtonEvent &event)
Definition: button.cpp:471
void load_images()
Definition: button.cpp:83
virtual void draw_contents()
Definition: button.cpp:304
int base_width_
Definition: button.hpp:89
void release()
Definition: button.cpp:494
surface overlayImage_
Definition: button.hpp:76
std::string button_overlay_image_name_
Definition: button.hpp:92
TYPE get_type() const
Definition: button.hpp:34
void calculate_size()
Definition: button.cpp:201
SPACE_CONSUMPTION spacing_
Definition: button.hpp:87
TYPE type_
Definition: button.hpp:66
surface touchedImage_
Definition: button.hpp:76
virtual void mouse_motion(const SDL_MouseMotionEvent &event)
Definition: button.cpp:437
SPACE_CONSUMPTION
Definition: button.hpp:36
void set_overlay(const std::string &image_file_base)
Definition: button.cpp:407
bool pressed()
Definition: button.cpp:580
bool checked() const
Definition: button.cpp:286
surface image_
Definition: button.hpp:76
virtual void handle_event(const SDL_Event &event)
Definition: button.cpp:550
surface overlayPressedDisabledImage_
Definition: button.hpp:76
CVideo & video() const
Definition: widget.hpp:83
surface overlayActiveImage_
Definition: button.hpp:76
std::string label_text_
Definition: button.hpp:74
SDL_Rect textRect_
Definition: button.hpp:80
virtual ~button()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: button.cpp:197
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:29
surface pressedDisabledImage_
Definition: button.hpp:76
STATE state_
Definition: button.hpp:83
surface overlayDisabledImage_
Definition: button.hpp:76
void set_active(bool active)
Definition: button.cpp:275