MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AbstractButton.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  AbstractButton.h Clickable Panel
3  Copyright (C) 2014 The Regents of the University of New Mexico. All rights reserved.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License as published by the Free Software Foundation; either
8  version 2.1 of the License, or (at your option) any later version.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this library; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
18  USA
19 */
20 
28 #ifndef ABSTRACTBUTTON_H
29 #define ABSTRACTBUTTON_H
30 
31 #include "itype.h"
32 #include "SDL.h"
33 #include "SDL_ttf.h"
34 #include "Point.h"
35 #include "Panel.h"
36 #include "Drawing.h"
37 
38 namespace MFM
39 {
44  class AbstractButton : public Panel
45  {
46  private:
47 
51  static const u32 BUTTON_BORDER_COLOR = Drawing::GREY10;
52 
56  static const u32 BUTTON_BACKGROUND_COLOR = Drawing::GREY20;
57 
61  static const u32 BUTTON_COLOR = Drawing::GREY90;
62 
67  SPoint m_dimensions;
68 
73  SPoint m_location;
74 
78  const char* m_text;
79 
84  SDL_Surface* m_icon;
85 
90  bool m_enabled;
91 
92  bool m_justClicked;
93 
97  void Init() ;
98 
99  public:
100 
105  AbstractButton();
106 
114  AbstractButton(const char* text);
115 
123  AbstractButton(SDL_Surface* icon);
124 
136  AbstractButton(const char* text, SDL_Surface* icon);
137 
141  ~AbstractButton();
142 
151  void SetLocation(const SPoint& location)
152  {
153  m_location = location;
154  }
155 
163  void SetIcon(SDL_Surface* icon)
164  {
165  m_icon = icon;
166  }
167 
175  SDL_Surface* GetIcon()
176  {
177  return m_icon;
178  }
179 
186  void SetDimensions(const SPoint& dimensions)
187  {
188  m_dimensions = dimensions;
189  Panel::SetDimensions(dimensions.GetX(), dimensions.GetY());
190  }
191 
200  void SetEnabled(bool isEnabled)
201  {
202  m_enabled = isEnabled;
203  }
204 
212  void SetText(const char* text)
213  {
214  m_text = text;
215  }
216 
224  const char* GetText()
225  {
226  return m_text;
227  }
228 
236  bool IsEnabled() const
237  {
238  return m_enabled;
239  }
240 
250  bool Contains(SPoint& pt);
251 
254 
255  virtual bool Handle(MouseButtonEvent & event)
256  {
257  if (IsEnabled())
258  {
259  if(event.m_event.button.button == SDL_BUTTON_WHEELUP ||
260  event.m_event.button.button == SDL_BUTTON_WHEELDOWN)
261  {
262  return false; /* Don't take wheel events. */
263  }
264  if (event.m_event.type == SDL_MOUSEBUTTONUP) // Execute on up
265  {
266  m_justClicked = true;
267  OnClick(event.m_event.button.button);
268  return true; // We took it
269  }
270  if (event.m_event.type == SDL_MOUSEBUTTONDOWN) // But eat down too
271  {
272  OnPress(event.m_event.button.button);
273  return true;
274  }
275  }
276  return false;
277  }
278 
279  virtual void HandleResize(const UPoint& parentSize)
280  {
281  /* As a button, do nothing. */
282  }
283 
284  bool PaintClickHighlight(Drawing& d)
285  {
286  if(m_justClicked)
287  {
288  m_justClicked = false;
289  d.SetForeground(Drawing::YELLOW);
290  d.FillRect(0, 0,
291  this->Panel::GetDimensions().GetX(), this->Panel::GetDimensions().GetY());
292  return true;
293  }
294  return false;
295  }
296 
297  virtual void PaintComponentNonClick(Drawing& d);
298 
299  virtual void PaintComponent(Drawing & d);
300 
311  virtual void OnClick(u8 button) = 0;
312 
323  virtual void OnPress(u8 button)
324  { }
325  };
326 }
327 
328 #endif /* ABSTRACTBUTTON_H */
void SetText(const char *text)
Definition: AbstractButton.h:212
void SetLocation(const SPoint &location)
Definition: AbstractButton.h:151
void SetDimensions(const SPoint &dimensions)
Definition: AbstractButton.h:186
Definition: Panel.h:115
virtual void HandleResize(const UPoint &parentSize)
Definition: AbstractButton.h:279
~AbstractButton()
Definition: AbstractButton.cpp:47
u32 SetForeground(const u32 color)
Definition: Drawing.h:188
Definition: Panel.h:74
Definition: AbstractButton.h:44
AbstractButton()
Definition: AbstractButton.cpp:14
void SetEnabled(bool isEnabled)
Definition: AbstractButton.h:200
T GetY() const
Definition: Point.tcc:40
const char * GetText()
Definition: AbstractButton.h:224
virtual void OnPress(u8 button)
Definition: AbstractButton.h:323
bool IsEnabled() const
Definition: AbstractButton.h:236
bool Contains(SPoint &pt)
Definition: AbstractButton.cpp:80
virtual void PaintComponent(Drawing &d)
Definition: AbstractButton.cpp:50
virtual bool Handle(MouseButtonEvent &event)
Definition: AbstractButton.h:255
void FillRect(int x, int y, int w, int h) const
Definition: Drawing.cpp:115
virtual void OnClick(u8 button)=0
void SetIcon(SDL_Surface *icon)
Definition: AbstractButton.h:163
SDL_Surface * GetIcon()
Definition: AbstractButton.h:175
Definition: Drawing.h:44
T GetX() const
Definition: Point.tcc:34