Woopsi 1.0
GUI Framework for Nintendo DS Homebrew

gadget.h

00001 #ifndef _GADGET_H_
00002 #define _GADGET_H_
00003 
00004 #include <nds.h>
00005 #include "defines.h"
00006 #include "gadgeteventhandler.h"
00007 #include "woopsiarray.h"
00008 #include "glyphs.h"
00009 #include "rect.h"
00010 #include "fontbase.h"
00011 #include "gadgetstyle.h"
00012 #include "gadgeteventhandlerlist.h"
00013 #include "woopsistring.h"
00014 
00015 namespace WoopsiUI {
00016 
00017         class GraphicsPort;
00018         class FontBase;
00019         class RectCache;
00020 
00025         class Gadget {
00026         public:
00027 
00032                 enum CloseType {
00033                         CLOSE_TYPE_CLOSE = 0,                           
00034                         CLOSE_TYPE_HIDE = 1,                            
00035                         CLOSE_TYPE_SHELVE = 2                           
00036                 };
00037 
00041                 enum GadgetFlagType {
00042                         GADGET_BORDERLESS = 0x0001,                                     
00043                         GADGET_DRAGGABLE = 0x0002,                                      
00044                         GADGET_PERMEABLE = 0x0004,                                      
00045                         GADGET_DOUBLE_CLICKABLE = 0x0008,                       
00046                         GADGET_NO_RAISE_EVENTS = 0x0010,                        
00047                         GADGET_DECORATION = 0x0020                                      
00048                 };
00049 
00053                 typedef struct {
00054                         u8 clicked : 1;                                         
00055                         u8 hasFocus : 1;                                        
00056                         u8 dragging : 1;                                        
00057                         u8 deleted : 1;                                         
00058                         u8 shelved : 1;                                         
00059                         u8 borderless : 1;                                      
00060                         u8 draggable : 1;                                       
00061                         u8 enabled : 1;                                         
00062                         u8 decoration : 1;                                      
00063                         u8 permeable : 1;                                       
00064                         u8 erased : 1;                                          
00065                         u8 hidden : 1;                                          
00066                         u8 doubleClickable : 1;                         
00067                         u8 modal : 1;                                           
00068                         u8 canReceiveFocus : 1;                         
00069                 } Flags;
00070 
00074                 typedef struct {
00075                         u32 value;                                                      
00076                         WoopsiString name;                                      
00077                 } NameValuePair;
00078 
00082                 typedef struct {
00083                         u8 top;                                                         
00084                         u8 right;                                                       
00085                         u8 bottom;                                                      
00086                         u8 left;                                                        
00087                 } GadgetBorderSize;
00088 
00103                 Gadget(s16 x, s16 y, u16 width, u16 height, u32 flags, GadgetStyle* style = NULL);
00104                 
00109                 const s16 getX() const;
00110 
00115                 const s16 getY() const;
00116 
00121                 const s16 getRelativeX() const;
00122 
00127                 const s16 getRelativeY() const;
00128 
00133                 inline const u32 getRefcon() const { return _refcon; };
00134 
00140                 inline const CloseType getCloseType() const { return _closeType; };
00141 
00148                 inline const bool hasFocus() const { return _flags.hasFocus; };
00149 
00158                 const bool isDeleted() const;
00159 
00166                 const bool isDrawingEnabled() const;
00167 
00174                 const bool isHidden() const;
00175 
00182                 const bool isEnabled() const;
00183 
00192                 inline const bool isDecoration() const { return _flags.decoration; };
00193 
00203                 inline const bool isPermeable() const { return _flags.permeable; };
00204 
00210                 inline const bool isDoubleClickable() const { return _flags.doubleClickable; };
00211 
00216                 inline const bool isBorderless() const { return _flags.borderless; };
00217 
00222                 inline const bool isClicked() const { return _flags.clicked; };
00223 
00228                 inline const bool isBeingDragged() const { return _flags.dragging; };
00229 
00234                 inline const bool isShelved() const { return _flags.shelved; };
00235 
00241                 const bool isModal() const;
00242                 
00247                 const bool canReceiveFocus() const;
00248 
00253                 inline u16 getWidth() const { return _rect.getWidth(); };
00254 
00259                 inline u16 getHeight() const { return _rect.getHeight(); };
00260 
00265                 inline Gadget* getParent() const { return _parent; };
00266 
00271                 inline Gadget* getFocusedGadget() { return _focusedGadget; };
00272 
00278                 virtual const u8 getPhysicalScreenNumber() const;
00279 
00284                 inline const bool raisesEvents() const { return _gadgetEventHandlers->isEnabled(); };
00285 
00292                 virtual void getPreferredDimensions(Rect& rect) const;
00293 
00300                 void getClientRect(Rect& rect) const;
00301 
00307                 void getRectClippedToHierarchy(Rect& rect) const;
00308 
00318                 GraphicsPort* newGraphicsPort(bool isForeground);
00319 
00333                 GraphicsPort* newGraphicsPort(Rect clipRect);
00334 
00340                 WoopsiArray<Rect>* getForegroundRegions();
00341 
00346                 FontBase* getFont() const;
00347                 
00352                 FontBase* getGlyphFont() const;
00353 
00358                 inline const u16 getBackColour() const { return _style.colours.back; };
00359 
00364                 inline const u16 getShineColour() const { return _style.colours.shine; };
00365 
00370                 inline const u16 getHighlightColour() const { return _style.colours.highlight; };
00371 
00376                 inline const u16 getShadowColour() const { return _style.colours.shadow; };
00377 
00382                 inline const u16 getFillColour() const { return _style.colours.fill; };
00383 
00388                 inline const u16 getDarkColour() const { return _style.colours.dark; };
00389 
00395                 u32 setRefcon(u32 refcon);
00396 
00401                 void setBorderless(bool isBorderless);
00402 
00407                 inline void setDraggable(const bool isDraggable) { _flags.draggable = isDraggable; };
00408 
00414                 inline void setPermeable(const bool isPermeable) { _flags.permeable = isPermeable; };
00415 
00420                 inline void setDoubleClickable(const bool isDoubleClickable) { _flags.doubleClickable = isDoubleClickable; };
00421 
00427                 inline void addGadgetEventHandler(GadgetEventHandler* eventHandler) { _gadgetEventHandlers->addGadgetEventHandler(eventHandler); };
00428 
00433                 inline void removeGadgetEventHandler(GadgetEventHandler* eventHandler) { _gadgetEventHandlers->removeGadgetEventHandler(eventHandler); };
00434 
00439                 inline void setRaisesEvents(const bool raisesEvents) { 
00440                         raisesEvents ? _gadgetEventHandlers->enable() : _gadgetEventHandlers->disable();
00441                 };
00442 
00447                 inline void setBackColour(const u16 colour) { _style.colours.back = colour; };
00448 
00453                 inline void setShineColour(const u16 colour) { _style.colours.shine = colour; };
00454 
00459                 inline void setHighlightColour(const u16 colour) { _style.colours.highlight = colour; };
00460 
00465                 inline void setShadowColour(const u16 colour) { _style.colours.shadow = colour; };
00466 
00471                 inline void setFillColour(const u16 colour) { _style.colours.fill = colour; };
00472 
00477                 inline void setDarkColour(const u16 colour) { _style.colours.dark = colour; };
00478 
00483                 inline void setCloseType(const CloseType closeType) { _closeType = closeType; };
00484 
00489                 virtual void setFont(FontBase* font);
00490                 
00495                 virtual void setGlyphFont(FontBase* font);
00496 
00504                 void redraw(const Rect& rect);
00505 
00511                 bool enable();
00512 
00518                 bool disable();
00519 
00525                 void close();
00526 
00534                 bool shelve();
00535 
00543                 bool unshelve();
00544 
00551                 bool show();
00552 
00559                 bool hide();
00560 
00567                 bool click(s16 x, s16 y);
00568 
00575                 virtual bool isDoubleClick(s16 x, s16 y);
00576 
00583                 bool doubleClick(s16 x, s16 y);
00584 
00591                 bool shiftClick(s16 x, s16 y);
00592 
00599                 bool release(s16 x, s16 y);
00600 
00609                 bool drag(s16 x, s16 y, s16 vX, s16 vY);
00610 
00616                 bool keyPress(KeyCode keyCode);
00617                 
00623                 bool keyRepeat(KeyCode keyCode);
00624 
00630                 bool keyRelease(KeyCode keyCode);
00631 
00636                 void lidClose();
00637 
00642                 void lidOpen();
00643 
00649                 bool focus();
00650 
00656                 bool blur();
00657 
00665                 bool moveTo(s16 x, s16 y);
00666 
00673                 bool resize(u16 width, u16 height);
00674 
00683                 bool changeDimensions(s16 x, s16 y, u16 width, u16 height);
00684 
00689                 bool raiseToTop();
00690 
00695                 bool lowerToBottom();
00696 
00703                 bool raiseGadgetToTop(Gadget* child);
00704 
00711                 bool lowerGadgetToBottom(Gadget* gadget);
00712 
00718                 void moveChildToDeleteQueue(Gadget* gadget);
00719 
00728                 bool moveChildToShelvedList(Gadget* gadget);
00729 
00739                 bool moveShelvedToChildList(Gadget* gadget);
00740 
00747                 void setFocusedGadget(Gadget* gadget);
00748 
00756                 bool checkCollision(s16 x, s16 y) const;
00757 
00767                 bool checkCollision(s16 x, s16 y, u16 width, u16 height) const;
00768 
00775                 bool checkCollision(Gadget* gadget) const;
00776 
00783                 bool checkCollision(const Rect& rect) const;
00784 
00795                 void invalidateLowerGadgetsVisibleRectCache(Gadget* gadget);
00796 
00815                 void addGadget(Gadget* gadget);
00816 
00830                 void insertGadget(Gadget* gadget);
00831 
00838                 inline void setParent(Gadget* parent) { _parent = parent; };
00839 
00844                 void cacheVisibleRects() const;
00845 
00850                 void invalidateVisibleRectCache();
00851 
00857                 void clipRectToHierarchy(Rect& rect) const;
00858 
00868                 virtual bool swapGadgetDepth(Gadget* gadget);
00869 
00877                 bool swapDepth();
00878 
00883                 inline void destroy() { delete this; };
00884 
00892                 bool remove();
00893 
00902                 bool removeChild(Gadget* gadget);
00903 
00909                 void addContextMenuItem(const WoopsiString& name, u32 value);
00910 
00918                 void showContextMenu(s16 x, s16 y);
00919 
00925                 virtual bool handleContextMenuSelection(const ListDataItem* item);
00926 
00932                 void goModal();
00933 
00937                 inline void stopModal() { _flags.modal = false; };
00938 
00944                 const s32 getGadgetIndex(const Gadget* gadget) const;
00945 
00951                 Gadget* getChild(u32 index) const;
00952 
00960                 const s32 getChildCount() const { return _gadgets.size(); };
00961 
00967                 const inline s32 getDecorationCount() const { return _decorationCount; };
00968 
00973                 inline RectCache* getRectCache() const { return _rectCache; };
00974 
00980                 void setBorderSize(const GadgetBorderSize& borderSize);
00981 
00986                 void markRectsDamaged();
00987                 
00997                 void markRectDamaged(const Rect& rect);
00998 
00999         protected:
01000                 Rect _rect;                                                             
01001                 u32 _refcon;                                                    
01003                 // Dragging variables
01004                 s16 _grabPointX;                                                
01005                 s16 _grabPointY;                                                
01006                 s16 _newX;                                                              
01007                 s16 _newY;                                                              
01009                 // Style
01010                 GadgetStyle _style;                                             
01012                 // Status
01013                 Flags _flags;                                                   
01015                 // Event handling
01016                 GadgetEventHandlerList* _gadgetEventHandlers;           
01018                 // Double-clicking
01019                 u32 _lastClickTime;                                             
01020                 s16 _lastClickX;                                                
01021                 s16 _lastClickY;                                                
01022                 s16 _doubleClickBounds;                                 
01024                 // Hierarchy control
01025                 Gadget* _parent;                                                
01026                 Gadget* _focusedGadget;                                 
01027                 WoopsiArray<Gadget*> _gadgets;                  
01028                 WoopsiArray<Gadget*> _shelvedGadgets;   
01030                 // Decorations
01031                 u8 _decorationCount;                                    
01033                 // Visible regions
01034                 RectCache* _rectCache;                                  
01036                 CloseType _closeType;                                   
01038                 GadgetBorderSize _borderSize;                   
01040                 // Context menu item definitions
01041                 WoopsiArray<NameValuePair> _contextMenuItems;   
01046                 virtual ~Gadget();
01047 
01052                 virtual inline void drawContents(GraphicsPort* port) { };
01053 
01058                 virtual void drawBorder(GraphicsPort* port) { };
01059 
01069                 bool checkCollisionWithForegroundRects(s16 x, s16 y) const;
01070 
01084                 const s16 calculatePhysicalScreenY(s16 y) const;
01085 
01096                 const u8 calculatePhysicalScreenNumber(s16 y) const;
01097 
01104                 void closeChild(Gadget* gadget);
01105 
01115                 void shelveChild(Gadget* gadget);
01116 
01127                 GraphicsPort* newInternalGraphicsPort(Rect clipRect);
01128 
01134                 const s32 getHigherVisibleGadget(const s32 startIndex) const;
01135 
01141                 const s32 getLowerVisibleGadget(const s32 startIndex) const;
01142                 
01150                 void startDragging(s16 x, s16 y);
01151                 
01157                 void stopDragging(s16 x, s16 y);
01158 
01162                 inline Gadget(const Gadget& gadget) { };
01163 
01171                 virtual inline void onClick(s16 x, s16 y) { };
01172                 
01182                 virtual inline void onDoubleClick(s16 x, s16 y) { };
01183                 
01191                 virtual inline void onShiftClick(s16 x, s16 y) { };
01192                 
01200                 virtual inline void onRelease(s16 x, s16 y) { };
01201                 
01209                 virtual inline void onReleaseOutside(s16 x, s16 y) { };
01210                 
01220                 virtual inline void onDrag(s16 x, s16 y, s16 vX, s16 vY) { };
01221                 
01227                 virtual inline void onDragStart() { };
01228                 
01234                 virtual inline void onDragStop() { };
01235                 
01242                 virtual inline void onKeyPress(KeyCode keyCode) { };
01243                 
01250                 virtual inline void onKeyRelease(KeyCode keyCode) { };
01251                 
01258                 virtual inline void onKeyRepeat(KeyCode keyCode) { };
01259                 
01265                 virtual inline void onFocus() { };
01266                 
01272                 virtual inline void onBlur() { };
01273                 
01279                 virtual inline void onLidOpen() { };
01280                 
01286                 virtual inline void onLidClose() { };
01287                 
01293                 virtual inline void onEnable() { };
01294                 
01300                 virtual inline void onDisable() { };
01301                 
01309                 virtual inline void onResize(u16 width, u16 height) { };
01310         };
01311 }
01312 
01313 #endif