Woopsi 1.3
Nintendo DS Gui Framework
libwoopsi/include/gadget.h
00001 #ifndef _GADGET_H_
00002 #define _GADGET_H_
00003 
00004 #include <nds.h>
00005 #include "defines.h"
00006 #include "fontbase.h"
00007 #include "gadgeteventhandler.h"
00008 #include "gadgeteventhandlerlist.h"
00009 #include "gadgetstyle.h"
00010 #include "glyphs.h"
00011 #include "rect.h"
00012 #include "woopsiarray.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 
00031         typedef struct {
00032             u8 clicked : 1;                     
00033             u8 hasFocus : 1;                    
00034             u8 dragging : 1;                    
00035             u8 deleted : 1;                     
00036             u8 shelved : 1;                     
00037             u8 borderless : 1;                  
00038             u8 draggable : 1;                   
00039             u8 enabled : 1;                     
00040             u8 decoration : 1;                  
00041             u8 permeable : 1;                   
00042             u8 erased : 1;                      
00043             u8 hidden : 1;                      
00044             u8 doubleClickable : 1;             
00045             u8 modal : 1;                       
00046             u8 canReceiveFocus : 1;             
00047         } Flags;
00048 
00052         typedef struct {
00053             u32 value;                          
00054             WoopsiString name;                  
00055         } NameValuePair;
00056 
00060         typedef struct {
00061             u8 top;                             
00062             u8 right;                           
00063             u8 bottom;                          
00064             u8 left;                            
00065         } GadgetBorderSize;
00066 
00080         Gadget(s16 x, s16 y, u16 width, u16 height, GadgetStyle* style = NULL);
00081         
00086         const s16 getX() const;
00087 
00092         const s16 getY() const;
00093 
00098         const s16 getRelativeX() const;
00099 
00104         const s16 getRelativeY() const;
00105 
00110         inline const u32 getRefcon() const { return _refcon; };
00111 
00118         inline const bool hasFocus() const { return _flags.hasFocus; };
00119 
00128         const bool isDeleted() const;
00129 
00136         const bool isDrawingEnabled() const;
00137 
00144         const bool isHidden() const;
00145 
00152         const bool isEnabled() const;
00153 
00162         inline const bool isDecoration() const { return _flags.decoration; };
00163 
00173         inline const bool isPermeable() const { return _flags.permeable; };
00174 
00180         inline const bool isDoubleClickable() const { return _flags.doubleClickable; };
00181 
00186         inline const bool isBorderless() const { return _flags.borderless; };
00187 
00192         inline const bool isClicked() const { return _flags.clicked; };
00193 
00198         inline const bool isBeingDragged() const { return _flags.dragging; };
00199 
00204         inline const bool isShelved() const { return _flags.shelved; };
00205 
00211         const bool isModal() const;
00212         
00217         const bool canReceiveFocus() const;
00218 
00223         inline u16 getWidth() const { return _rect.getWidth(); };
00224 
00229         inline u16 getHeight() const { return _rect.getHeight(); };
00230 
00235         inline Gadget* getParent() const { return _parent; };
00236 
00241         inline Gadget* getFocusedGadget() { return _focusedGadget; };
00242 
00248         virtual const u8 getPhysicalScreenNumber() const;
00249 
00254         inline const bool raisesEvents() const { return _gadgetEventHandlers->isEnabled(); };
00255 
00262         virtual void getPreferredDimensions(Rect& rect) const;
00263 
00270         void getClientRect(Rect& rect) const;
00271 
00277         void getRectClippedToHierarchy(Rect& rect) const;
00278 
00288         GraphicsPort* newGraphicsPort(bool isForeground);
00289 
00303         GraphicsPort* newGraphicsPort(Rect clipRect);
00304 
00310         WoopsiArray<Rect>* getForegroundRegions();
00311 
00316         FontBase* getFont() const;
00317         
00322         FontBase* getGlyphFont() const;
00323 
00328         inline const u16 getBackColour() const { return _style.colours.back; };
00329 
00334         inline const u16 getShineColour() const { return _style.colours.shine; };
00335 
00340         inline const u16 getHighlightColour() const { return _style.colours.highlight; };
00341 
00346         inline const u16 getShadowColour() const { return _style.colours.shadow; };
00347 
00352         inline const u16 getFillColour() const { return _style.colours.fill; };
00353 
00358         inline const u16 getDarkColour() const { return _style.colours.dark; };
00359 
00364         inline const u16 getTextColour() const { return _style.colours.text; };
00365 
00371         u32 setRefcon(u32 refcon);
00372 
00377         virtual void setBorderless(bool isBorderless);
00378 
00383         void setDecoration(bool isDecoration);
00384 
00389         inline void setDraggable(const bool isDraggable) { _flags.draggable = isDraggable; };
00390 
00396         inline void setPermeable(const bool isPermeable) { _flags.permeable = isPermeable; };
00397 
00402         inline void setDoubleClickable(const bool isDoubleClickable) { _flags.doubleClickable = isDoubleClickable; };
00403 
00409         inline void addGadgetEventHandler(GadgetEventHandler* eventHandler) { _gadgetEventHandlers->addGadgetEventHandler(eventHandler); };
00410 
00415         inline void removeGadgetEventHandler(GadgetEventHandler* eventHandler) { _gadgetEventHandlers->removeGadgetEventHandler(eventHandler); };
00416 
00421         inline void setRaisesEvents(const bool raisesEvents) { 
00422             raisesEvents ? _gadgetEventHandlers->enable() : _gadgetEventHandlers->disable();
00423         };
00424 
00429         inline void setBackColour(const u16 colour) { _style.colours.back = colour; };
00430 
00435         inline void setShineColour(const u16 colour) { _style.colours.shine = colour; };
00436 
00441         inline void setHighlightColour(const u16 colour) { _style.colours.highlight = colour; };
00442 
00447         inline void setShadowColour(const u16 colour) { _style.colours.shadow = colour; };
00448 
00453         inline void setFillColour(const u16 colour) { _style.colours.fill = colour; };
00454 
00459         inline void setDarkColour(const u16 colour) { _style.colours.dark = colour; };
00460 
00465         inline void setTextColour(const u16 colour) { _style.colours.text = colour; };
00466 
00471         virtual void setFont(FontBase* font);
00472         
00477         virtual void setGlyphFont(FontBase* font);
00478 
00486         void redraw(const Rect& rect);
00487 
00493         bool enable();
00494 
00500         bool disable();
00501 
00507         void close();
00508 
00516         bool shelve();
00517 
00525         bool unshelve();
00526 
00533         bool show();
00534 
00541         bool hide();
00542 
00549         bool click(s16 x, s16 y);
00550 
00557         virtual bool isDoubleClick(s16 x, s16 y);
00558 
00565         bool doubleClick(s16 x, s16 y);
00566 
00573         bool shiftClick(s16 x, s16 y);
00574 
00581         bool release(s16 x, s16 y);
00582 
00591         bool drag(s16 x, s16 y, s16 vX, s16 vY);
00592 
00598         bool keyPress(KeyCode keyCode);
00599         
00605         bool keyRepeat(KeyCode keyCode);
00606 
00612         bool keyRelease(KeyCode keyCode);
00613 
00618         void lidClose();
00619 
00624         void lidOpen();
00625 
00631         bool focus();
00632 
00638         bool blur();
00639 
00647         bool moveTo(s16 x, s16 y);
00648 
00655         bool resize(u16 width, u16 height);
00656 
00665         bool changeDimensions(s16 x, s16 y, u16 width, u16 height);
00666 
00671         bool raiseToTop();
00672 
00677         bool lowerToBottom();
00678 
00685         bool raiseGadgetToTop(Gadget* gadget);
00686 
00694         bool lowerGadgetToBottom(Gadget* gadget);
00695 
00701         void moveGadgetToDeleteQueue(Gadget* gadget);
00702 
00711         bool moveGadgetToShelvedList(Gadget* gadget);
00712 
00722         bool moveShelvedToGadgetList(Gadget* gadget);
00723 
00730         void setFocusedGadget(Gadget* gadget);
00731 
00739         bool checkCollision(s16 x, s16 y) const;
00740 
00750         bool checkCollision(s16 x, s16 y, u16 width, u16 height) const;
00751 
00758         bool checkCollision(const Gadget* gadget) const;
00759 
00766         bool checkCollision(const Rect& rect) const;
00767 
00778         void invalidateLowerGadgetsVisibleRectCache(Gadget* gadget);
00779 
00798         void addGadget(Gadget* gadget);
00799 
00813         void insertGadget(Gadget* gadget);
00814 
00821         inline void setParent(Gadget* parent) { _parent = parent; };
00822 
00827         void cacheVisibleRects() const;
00828 
00833         void invalidateVisibleRectCache();
00834 
00840         void clipRectToHierarchy(Rect& rect) const;
00841 
00849         bool changeGadgetDepth(s32 sourceIndex, s32 destinationIndex);
00850 
00858         s32 getLowestCollidingGadgetIndex(const Gadget* gadget) const;
00859 
00867         s32 getHighestCollidingGadgetIndex(const Gadget* gadget) const;
00868 
00878         virtual bool swapDepth();
00879 
00884         inline void destroy() { delete this; };
00885 
00893         bool remove();
00894 
00903         bool removeGadget(Gadget* gadget);
00904 
00910         void addContextMenuItem(const WoopsiString& name, u32 value);
00911 
00919         void showContextMenu(s16 x, s16 y);
00920 
00926         virtual bool handleContextMenuSelection(const ListDataItem* item);
00927 
00933         void goModal();
00934 
00938         inline void stopModal() { _flags.modal = false; };
00939 
00945         const s32 getGadgetIndex(const Gadget* gadget) const;
00946 
00952         Gadget* getGadget(u32 index) const;
00953 
00961         const s32 getGadgetCount() const { return _gadgets.size(); };
00962 
00968         const inline s32 getDecorationCount() const { return _decorationCount; };
00969 
00974         inline RectCache* getRectCache() const { return _rectCache; };
00975 
00981         void setBorderSize(const GadgetBorderSize& borderSize);
00982 
00987         void markRectsDamaged();
00988         
00998         void markRectDamaged(const Rect& rect);
00999 
01000     protected:
01001         Rect _rect;                             
01002         u32 _refcon;                            
01004         // Dragging variables
01005         s16 _grabPointX;                        
01006         s16 _grabPointY;                        
01007         s16 _newX;                              
01008         s16 _newY;                              
01010         // Style
01011         GadgetStyle _style;                     
01013         // Status
01014         Flags _flags;                           
01016         // Event handling
01017         GadgetEventHandlerList* _gadgetEventHandlers;       
01019         // Double-clicking
01020         u32 _lastClickTime;                     
01021         s16 _lastClickX;                        
01022         s16 _lastClickY;                        
01023         s16 _doubleClickBounds;                 
01025         // Hierarchy control
01026         Gadget* _parent;                        
01027         Gadget* _focusedGadget;                 
01028         WoopsiArray<Gadget*> _gadgets;          
01029         WoopsiArray<Gadget*> _shelvedGadgets;   
01031         // Decorations
01032         s32 _decorationCount;                   
01034         // Visible regions
01035         RectCache* _rectCache;                  
01037         GadgetBorderSize _borderSize;           
01039         // Context menu item definitions
01040         WoopsiArray<NameValuePair> _contextMenuItems;   
01045         virtual ~Gadget();
01046 
01051         virtual inline void drawContents(GraphicsPort* port) { };
01052 
01057         virtual void drawBorder(GraphicsPort* port) { };
01058 
01068         bool checkCollisionWithForegroundRects(s16 x, s16 y) const;
01069 
01083         const s16 calculatePhysicalScreenY(s16 y) const;
01084 
01095         const u8 calculatePhysicalScreenNumber(s16 y) const;
01096 
01103         void closeGadget(Gadget* gadget);
01104 
01114         void shelveGadget(Gadget* gadget);
01115 
01126         GraphicsPort* newInternalGraphicsPort(Rect clipRect);
01127 
01133         const s32 getHigherVisibleGadget(const s32 startIndex) const;
01134 
01140         const s32 getLowerVisibleGadget(const s32 startIndex) const;
01141         
01149         void startDragging(s16 x, s16 y);
01150         
01156         void stopDragging(s16 x, s16 y);
01157 
01161         inline Gadget(const Gadget& gadget) { };
01162 
01170         virtual inline void onClick(s16 x, s16 y) { };
01171         
01181         virtual inline void onDoubleClick(s16 x, s16 y) { };
01182         
01190         virtual inline void onShiftClick(s16 x, s16 y) { };
01191         
01199         virtual inline void onRelease(s16 x, s16 y) { };
01200         
01208         virtual inline void onReleaseOutside(s16 x, s16 y) { };
01209         
01219         virtual inline void onDrag(s16 x, s16 y, s16 vX, s16 vY) { };
01220         
01226         virtual inline void onDragStart() { };
01227         
01233         virtual inline void onDragStop() { };
01234         
01241         virtual inline void onKeyPress(KeyCode keyCode) { };
01242         
01249         virtual inline void onKeyRelease(KeyCode keyCode) { };
01250         
01257         virtual inline void onKeyRepeat(KeyCode keyCode) { };
01258         
01264         virtual inline void onFocus() { };
01265         
01271         virtual inline void onBlur() { };
01272         
01278         virtual inline void onLidOpen() { };
01279         
01285         virtual inline void onLidClose() { };
01286         
01292         virtual inline void onEnable() { };
01293         
01299         virtual inline void onDisable() { };
01300         
01308         virtual inline void onResize(u16 width, u16 height) { };
01309     };
01310 }
01311 
01312 #endif
 All Classes Functions Variables Enumerations Enumerator