MFMv2.0.10
Movable Feast Machine Simulator 2.0.10
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Panel.h
Go to the documentation of this file.
1 /* -*- mode:C++ -*-
2  Panel.h Hierarchical rendering system
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 
27 #ifndef PANEL_H
28 #define PANEL_H
29 
30 #include "itype.h"
31 #include "Point.h"
32 #include "Rect.h"
33 #include "Drawing.h"
34 #include "Keyboard.h"
35 #include "ByteSink.h"
36 #include "EditingTool.h"
38 
39 namespace MFM
40 {
41 
42  typedef enum
43  {
44  ANCHOR_NORTH,
45  ANCHOR_EAST,
46  ANCHOR_SOUTH,
47  ANCHOR_WEST
48  }GUIAnchor;
49 
50  class Panel; // FORWARD
51 
52  struct MouseEvent
53  {
54  MouseEvent(const Keyboard & keyboard, SDL_Event & event,
55  const EditingTool selectedTool) :
56  m_keyboard(keyboard),
57  m_event(event),
58  m_selectedTool(selectedTool)
59  { }
60 
61  const Keyboard & m_keyboard;
62  SDL_Event & m_event;
63  const EditingTool m_selectedTool;
64 
65  virtual ~MouseEvent()
66  { }
67 
68  // Double dispatch support
69  virtual bool Handle(Panel & panel) = 0;
70 
71  virtual SPoint GetAt() const = 0;
72  };
73 
74  struct MouseButtonEvent : public MouseEvent
75  {
76  MouseButtonEvent(const Keyboard & keyboard,
77  SDL_Event & event, const EditingTool selectedTool) :
78  MouseEvent(keyboard, event, selectedTool)
79  { }
80 
81  virtual bool Handle(Panel & panel) ;
82 
83  virtual SPoint GetAt() const
84  {
85  return SPoint(m_event.button.x, m_event.button.y);
86  }
87  };
88 
89  typedef SPoint ButtonPositionArray[SDL_BUTTON_X2+1];
90 
91  struct MouseMotionEvent : public MouseEvent
92  {
93  const u32 m_buttonMask;
94  const ButtonPositionArray & m_buttonPositionArray;
95 
96  MouseMotionEvent(const Keyboard & keyboard, SDL_Event & event, u32 buttonMask,
97  ButtonPositionArray & bpa, const EditingTool selectedTool) :
98  MouseEvent(keyboard, event, selectedTool),
99  m_buttonMask(buttonMask),
100  m_buttonPositionArray(bpa)
101  { }
102 
103  virtual bool Handle(Panel & panel) ;
104 
105  virtual SPoint GetAt() const
106  {
107  return SPoint(m_event.motion.x, m_event.motion.y);
108  }
109  };
110 
115  class Panel
116  {
117  protected:
118  OString16 m_name;
119 
120  Rect m_rect;
121  u32 m_bdColor; // Default border color of this panel
122  u32 m_bgColor; // Default background color of this panel
123  u32 m_fgColor; // Default foreground color of this panel
124 
128  TTF_Font* m_font;
129 
134  SPoint m_desiredLocation;
135 
136  // My parent Panel, if any
137  Panel * m_parent;
138 
139  // The top of the stack of panels I am parent to. This is
140  // null when I have no kids
141  Panel * m_top;
142 
143  // My position in my parent's doubly-linked circle of panels.
144  // These are null when I have no parent.
145  Panel * m_forward; // Pointer to next guy above me in rendering order
146  Panel * m_backward; // Pointer to next guy below me in rendering order
147 
148  Panel* m_focusedChild;
149 
154  bool m_visible;
155 
156  // A helper function to indent a line, that totally belongs elsewhere
157  static void Indent(ByteSink& sink, u32 count) ;
158 
159  public:
160  Panel(u32 width=0, u32 height=0);
161 
162  virtual ~Panel();
163 
164  void Insert(Panel* child, Panel* afterOrNull) ;
165 
166  Panel* Pop() ;
167 
168  void Remove(Panel* child) ;
169 
170  void SetVisibility(bool value){ m_visible = value; }
171 
172  void ToggleVisibility(){ m_visible = !m_visible; }
173 
174  bool IsVisible()
175  {
176  return m_visible;
177  }
178 
179  u32 GetWidth() const {return m_rect.GetWidth();}
180 
181  u32 GetHeight() const {return m_rect.GetHeight();}
182 
183  void SetDimensions(u32 width, u32 height);
184 
185  void SetDesiredSize(u32 width, u32 height);
186 
187  const UPoint & GetDimensions() const ;
188 
189  const UPoint & GetDesiredSize() const ;
190 
191  void SetRenderPoint(const SPoint & renderPt);
192 
193  const SPoint & GetRenderPoint() const ;
194 
195  const char * GetName() const { return m_name.GetBuffer(); }
196 
197  void SetName(const char * name)
198  {
199  m_name.Reset();
200  if (name)
201  {
202  m_name.Print(name);
203  }
204  m_name.GetZString(); // Ensure null terminated to use GetBuffer()
205  }
206 
207  void Print(ByteSink & sink, u32 indent = 0) const;
208 
212  u32 GetBackground() const
213  {
214  return m_bgColor;
215  }
216 
220  u32 SetBackground(const u32 color)
221  {
222  u32 old = m_bgColor;
223  m_bgColor = color;
224  return old;
225  }
226 
230  u32 GetBorder() const
231  {
232  return m_bdColor;
233  }
234 
238  u32 SetBorder(const u32 color)
239  {
240  u32 old = m_bdColor;
241  m_bdColor = color;
242  return old;
243  }
244 
248  u32 GetForeground() const
249  {
250  return m_fgColor;
251  }
252 
256  u32 SetForeground(const u32 color)
257  {
258  u32 old = m_fgColor;
259  m_fgColor = color;
260  return old;
261  }
262 
268  TTF_Font* GetFont() const ;
269 
274  TTF_Font* SetFont(TTF_Font * newFont) ;
275 
284 
285 
286  virtual void Paint(Drawing & config);
287 
303  virtual void PaintUpdateVisibility(Drawing & config);
304 
312  virtual void PaintComponent(Drawing & config);
313 
318  virtual void PaintBorder(Drawing & config);
319 
326  virtual void PaintChildren(Drawing & config);
327 
335  virtual bool Dispatch(MouseEvent & event, const Rect & rect);
336 
345  virtual bool Handle(MouseButtonEvent & event) ;
346 
352  virtual bool Handle(MouseMotionEvent & event) ;
353 
359  virtual void OnMouseExit();
360 
364  virtual void HandleResize(const UPoint& parentSize);
365 
373  void SetAnchor(const GUIAnchor anchor);
374 
389  SPoint GetTextSize(TTF_Font * font, const char * text);
390 
391  };
392 } /* namespace MFM */
393 #endif /*PANEL_H*/
Definition: Rect.h:40
virtual void OnMouseExit()
Definition: Panel.cpp:307
TTF_Font * GetFont() const
Definition: Panel.cpp:131
u32 GetBackground() const
Definition: Panel.h:212
Definition: Panel.h:115
UPoint m_desiredSize
Definition: Panel.h:133
u32 SetBorder(const u32 color)
Definition: Panel.h:238
TTF_Font * m_font
Definition: Panel.h:128
Definition: Keyboard.h:36
Definition: Panel.h:91
bool m_visible
Definition: Panel.h:154
u32 GetWidth() const
Definition: Rect.h:209
Definition: Panel.h:74
void Reset()
Definition: OverflowableCharBufferByteSink.h:192
u32 SetForeground(const u32 color)
Definition: Panel.h:256
virtual void HandleResize(const UPoint &parentSize)
Definition: Panel.cpp:265
const char * GetZString()
Definition: OverflowableCharBufferByteSink.h:143
void SetAnchor(const GUIAnchor anchor)
Definition: Panel.cpp:243
SPoint GetAbsoluteLocation()
Definition: Panel.cpp:173
u32 GetBorder() const
Definition: Panel.h:230
virtual void PaintComponent(Drawing &config)
Definition: Panel.cpp:230
Definition: Panel.h:52
Definition: ByteSink.h:47
u32 GetForeground() const
Definition: Panel.h:248
void Print(const char *str, s32 fieldWidth=-1, u8 padChar= ' ')
Definition: ByteSink.cpp:31
const char * GetBuffer() const
Definition: OverflowableCharBufferByteSink.h:158
virtual void PaintChildren(Drawing &config)
Definition: Panel.cpp:215
virtual void PaintBorder(Drawing &config)
Definition: Panel.cpp:237
virtual bool Handle(MouseButtonEvent &event)
Definition: Panel.cpp:16
virtual bool Dispatch(MouseEvent &event, const Rect &rect)
Definition: Panel.cpp:310
SPoint GetTextSize(TTF_Font *font, const char *text)
Definition: Panel.cpp:364
u32 GetHeight() const
Definition: Rect.h:219
virtual void PaintUpdateVisibility(Drawing &config)
Definition: Panel.cpp:179
TTF_Font * SetFont(TTF_Font *newFont)
Definition: Panel.cpp:135
Definition: Drawing.h:44
u32 SetBackground(const u32 color)
Definition: Panel.h:220