Phoenard
PHNWidget.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 This file is part of the Phoenard Arduino library
5 Copyright (c) 2014 Phoenard
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
30 #ifndef _PHN_WIDGET_H_
31 #define _PHN_WIDGET_H_
32 
33 #include "PHNDisplayHardware.h"
34 #include "PHNPalette.h"
35 #include "PHNTextContainer.h"
36 
41 #define BACKGROUND 0
42 #define FRAME 1
43 #define FOREGROUND 2
44 #define CONTENT 3
45 #define HIGHLIGHT 4
46 #define ACTIVATED 5
47 
48 
49 // (background, frame, foreground, content, highlight, activated)
51 #define WIDGET_DEFAULT_COLORS PALETTE(BLACK, RED, WHITE, BLACK, YELLOW, GRAY_LIGHT)
52 
53 // Forward-declare the Widget for in the container
54 class PHN_Widget;
55 
75  public:
77  PHN_WidgetContainer(void);
78 
85  void updateWidgets(bool update, bool draw, bool forceRedraw);
95  void removeWidget(PHN_Widget &widget);
100  void clearWidgets();
102  PHN_Widget *widget(int index) { return widget_values[index]; }
104  const int widgetCount() { return widget_count; }
105  protected:
112  void clearSilent();
118  void setWidgetCapacity(int capacity);
119 
122  private:
123  PHN_Widget **widget_values;
124  int widget_count;
125 };
126 
146  friend class PHN_WidgetContainer;
147 
148  public:
150  PHN_Widget();
152  virtual ~PHN_Widget();
154  const int getX(void) { return x; }
156  const int getY(void) { return y; }
158  const int getWidth(void) { return width; }
160  const int getHeight(void) { return height; }
162  void setBounds(int x, int y, int width, int height);
164  void setSize(int width, int height);
171  virtual void setColor(int colorId, color_t color);
178  const color_t color(int colorId) { return colors.get(colorId); }
179 
181  bool isTouched();
183  bool isTouchEnter();
185  bool isTouchLeave();
187  bool isTouchChange();
189  bool isClicked();
190 
192  void setVisible(bool visible);
194  bool isVisible(void);
195 
197  bool isDrawn(void);
198 
200  void setDrawingEnabled(bool drawing);
202  bool isDrawingEnabled(void);
203 
205  void invalidate(void);
207  bool isInvalidated(void);
209  void draw_validate();
211  virtual void undraw(void);
213  virtual void draw(void) = 0;
215  virtual void update(void) = 0;
216 protected:
218 
219  int x, y, width, height;
224  unsigned char visible;
227 };
228 
229 #endif
virtual void setColor(int colorId, color_t color)
Sets a style color for this widget.
Definition: PHNWidget.cpp:95
PHN_WidgetContainer(void)
Constructor for a new widget container.
Definition: PHNWidget.cpp:140
bool invalidated
Invalidated state of the widget.
Definition: PHNWidget.h:226
A color Palette that can store up to 256 colors.
Definition: PHNPalette.h:43
void removeWidget(PHN_Widget &widget)
Removes a single widget.
Definition: PHNWidget.cpp:197
void clearWidgets()
Removes all contained widgets.
Definition: PHNWidget.cpp:223
const int getWidth(void)
Gets the width of the widget.
Definition: PHNWidget.h:158
void updateWidgets(bool update, bool draw, bool forceRedraw)
Updates all the widgets contained.
Definition: PHNWidget.cpp:161
void addWidget(PHN_Widget &widget)
Adds a single widget.
Definition: PHNWidget.cpp:192
PHN_Widget * widget(int index)
Gets the widget stored at an index.
Definition: PHNWidget.h:102
virtual void update(void)=0
Update function routine, where widget logic must be performed.
bool isTouchEnter()
Gets whether the user's touch input entered the widget.
Definition: PHNWidget.cpp:106
bool isInvalidated(void)
Checks whether the widget is invalidated and needs to be redrawn.
Definition: PHNWidget.cpp:123
bool isVisible(void)
Gets whether the widget is displayed and updated.
Definition: PHNWidget.cpp:87
PHN_Widget()
Constructor for a new widget, initializing all fields to the default values.
Definition: PHNWidget.cpp:29
color_t get(int index) const
Gets a single color set at the index, BLACK if index out of range.
Definition: PHNPalette.cpp:36
void invalidate(void)
Invalidated the widget, causing it to be re-drawn at a later time.
Definition: PHNWidget.cpp:119
void setVisible(bool visible)
Sets whether the widget is displayed and updated.
Definition: PHNWidget.cpp:64
void setBounds(int x, int y, int width, int height)
Sets the new bounds (x, y, width, height) of the widget.
Definition: PHNWidget.cpp:46
void draw_validate()
Draws the widget if invalidated, clearing the invalidated state.
Definition: PHNWidget.cpp:127
bool isDrawingEnabled(void)
Gets whether the widget is drawn and undrawn.
Definition: PHNWidget.cpp:83
virtual void draw(void)=0
Draw function routine, where widgets must perform drawing.
void setDrawingEnabled(bool drawing)
Sets whether the widget is drawn and undrawn.
Definition: PHNWidget.cpp:75
Low-level utility functions for working with the display.
const color_t color(int colorId)
Gets a style color set for this widget.
Definition: PHNWidget.h:178
const int getHeight(void)
Gets the height of the widget.
Definition: PHNWidget.h:160
uint16_t color_t
Definition of the 16-bit 565 color type.
Definition: PHNDisplayHardware.h:97
bool isTouchChange()
Gets whether the user clicked, entered or left the widget.
Definition: PHNWidget.cpp:115
A single widget which covers a rectangular area of the screen.
Definition: PHNWidget.h:145
virtual ~PHN_Widget()
Destructor for a widget, clearing any child widgets and freeing memory.
Definition: PHNWidget.cpp:39
const int getY(void)
Gets the y-coordinate of the widget.
Definition: PHNWidget.h:156
void clearSilent()
Removes all child widgets from this container without handling proper redrawing.
Definition: PHNWidget.cpp:214
virtual void undraw(void)
Clear draw function routine, where widgets perform un-drawing (to background)
Definition: PHNWidget.cpp:60
PHN_Palette colors
A palette of colors set for the widget.
Definition: PHNWidget.h:222
Holds the PHN_TextContainer class used to dynamically access text using various datatypes.
bool isClicked()
Gets whether the user clicked (pressed and released) the widget.
Definition: PHNWidget.cpp:112
void setSize(int width, int height)
Sets the size (width, height) of the widget.
Definition: PHNWidget.cpp:54
const int widgetCount()
Gets how many widgets are stored.
Definition: PHNWidget.h:104
void setWidgetCapacity(int capacity)
Resizes the internal array of widgets to a new capacity.
Definition: PHNWidget.cpp:227
A container for storing multiple PHN_Widget instances.
Definition: PHNWidget.h:74
bool isDrawn(void)
Gets whether the widget has been drawn.
Definition: PHNWidget.cpp:91
Contains the PHN_Palette, a color container, and several palette macros.
unsigned char visible
Draw state flags of the widget (0=invisible, 0x1=visible, 0x2=drawn, 0x4=draw_disabled) ...
Definition: PHNWidget.h:224
const int getX(void)
Gets the x-coordinate of the widget.
Definition: PHNWidget.h:154
bool isTouched()
Gets whether the widget is touched down by the user.
Definition: PHNWidget.cpp:103
bool isTouchLeave()
Gets whether the user's touch input left the widget.
Definition: PHNWidget.cpp:109
bool deleteAddedWidgets
Sets whether added widgets are deleted (were added with new)
Definition: PHNWidget.h:121