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
00011 namespace WoopsiUI {
00012
00013 class GraphicsPort;
00014 class FontBase;
00015 class RectCache;
00016
00021 class Gadget {
00022 public:
00023
00027 enum OutlineType {
00028 OUTLINE_CLICK_DEPENDENT = 0,
00029 OUTLINE_OUT = 1,
00030 OUTLINE_IN = 2,
00031 OUTLINE_OUT_IN = 3
00032 };
00033
00037 enum CloseType {
00038 CLOSE_TYPE_CLOSE = 0,
00039 CLOSE_TYPE_HIDE = 1,
00040 CLOSE_TYPE_SHELVE = 2
00041 };
00042
00046 enum GadgetFlagType {
00047 GADGET_BORDERLESS = 0x0001,
00048 GADGET_DRAGGABLE = 0x0002,
00049 GADGET_PERMEABLE = 0x0004,
00050 GADGET_DOUBLE_CLICKABLE = 0x0008,
00051 GADGET_NO_SHIFT_CLICK_CHILDREN = 0x0010,
00052 GADGET_NO_RAISE_EVENTS = 0x0020,
00053 GADGET_DECORATION = 0x0040
00054 };
00055
00059 typedef struct {
00060 u8 clicked : 1;
00061 u8 hasFocus : 1;
00062 u8 dragging : 1;
00063 u8 deleted : 1;
00064 u8 shelved : 1;
00065 u8 borderless : 1;
00066 u8 draggable : 1;
00067 u8 drawingEnabled : 1;
00068 u8 enabled : 1;
00069 u8 decoration : 1;
00070 u8 permeable : 1;
00071 u8 raisesEvents : 1;
00072 u8 erased : 1;
00073 u8 shiftClickChildren : 1;
00074 u8 visibleRegionCacheInvalid : 1;
00075 u8 hidden : 1;
00076 u8 doubleClickable : 1;
00077 u8 modal : 1;
00078 } Flags;
00079
00083 typedef struct {
00084 u32 value;
00085 char* name;
00086 } NameValuePair;
00087
00099 Gadget(s16 x, s16 y, u16 width, u16 height, u32 flags, FontBase* font = NULL);
00100
00105 const s16 getX() const;
00106
00111 const s16 getY() const;
00112
00117 inline const u32 getRefcon() const { return _refcon; };
00118
00124 inline const CloseType getCloseType() { return _closeType; };
00125
00132 inline const bool hasFocus() const { return _flags.hasFocus; };
00133
00142 const bool isDeleted() const;
00143
00150 const bool isDrawingEnabled() const;
00151
00158 const bool isHidden() const;
00159
00166 const bool isEnabled() const;
00167
00174 inline const bool isDecoration() const { return _flags.decoration; };
00175
00182 inline const bool isPermeable() const { return _flags.permeable; };
00183
00188 inline const bool isDoubleClickable() const { return _flags.doubleClickable; };
00189
00194 inline const bool isBorderless() const { return _flags.borderless; };
00195
00200 inline const bool isClicked() const { return _flags.clicked; };
00201
00206 inline const bool isShelved() const { return _flags.shelved; };
00207
00212 const bool isModal() const;
00213
00218 inline const bool getShiftClickChildren() const { return _flags.shiftClickChildren; };
00219
00224 inline void setShiftClickChildren(const u32 shiftClickChildren) { _flags.shiftClickChildren = shiftClickChildren; };
00225
00230 inline u16 getWidth() const { return _width; };
00231
00236 inline u16 getHeight() const { return _height; };
00237
00242 inline Gadget* getParent() const { return _parent; };
00243
00248 inline Gadget* getFocusedGadget() { return _focusedGadget; };
00249
00254 inline const u8 getPhysicalScreenNumber() const { return calculatePhysicalScreenNumber(getY()); };
00255
00260 inline const bool raisesEvents() const { return _flags.raisesEvents & !isDeleted(); };
00261
00268 virtual void getPreferredDimensions(Rect& rect) const;
00269
00276 virtual void getClientRect(Rect& rect) const;
00277
00283 void getRectClippedToHierarchy(Rect& rect) const;
00284
00294 virtual GraphicsPort* newGraphicsPort(bool isForeground);
00295
00308 virtual GraphicsPort* newGraphicsPort(Rect clipRect);
00309
00315 WoopsiArray<Rect>* getForegroundRegions();
00316
00322 WoopsiArray<Rect>* getBackgroundRegions();
00323
00328 FontBase* getFont() const;
00329
00334 inline const u16 getBackColour() const { return _backColour; };
00335
00340 inline const u16 getShineColour() const { return _shineColour; };
00341
00346 inline const u16 getHighlightColour() const { return _highlightColour; };
00347
00352 inline const u16 getShadowColour() const { return _shadowColour; };
00353
00358 inline const u16 getFillColour() const { return _fillColour; };
00359
00364 inline const u16 getDarkColour() const { return _darkColour; };
00365
00370 inline const OutlineType getOutlineType() const { return _outline; };
00371
00377 u32 setRefcon(u32 refcon);
00378
00383 void setBorderless(bool isBorderless);
00384
00389 inline void setDraggable(const bool isDraggable) { _flags.draggable = isDraggable; };
00390
00395 inline void setPermeable(const bool isPermeable) { _flags.permeable = isPermeable; };
00396
00401 inline void setDoubleClickable(const bool isDoubleClickable) { _flags.doubleClickable = isDoubleClickable; };
00402
00407 inline void setOutlineType(const OutlineType outline) { _outline = outline; };
00408
00414 inline void addGadgetEventHandler(GadgetEventHandler* eventHandler) { _gadgetEventHandlers.push_back(eventHandler); };
00415
00420 void removeGadgetEventHandler(GadgetEventHandler* eventHandler);
00421
00426 inline void setRaisesEvents(const bool raisesEvents) { _flags.raisesEvents = raisesEvents; };
00427
00432 inline void disableDrawing() { _flags.drawingEnabled = false; };
00433
00437 inline void enableDrawing() { _flags.drawingEnabled = true; };
00438
00443 inline void setBackColour(const u16 colour) { _backColour = colour; };
00444
00449 inline void setShineColour(const u16 colour) { _shineColour = colour; };
00450
00455 inline void setHighlightColour(const u16 colour) { _highlightColour = colour; };
00456
00461 inline void setShadowColour(const u16 colour) { _shadowColour = colour; };
00462
00467 inline void setFillColour(const u16 colour) { _fillColour = colour; };
00468
00473 inline void setDarkColour(const u16 colour) { _darkColour = colour; };
00474
00479 inline void setCloseType(const CloseType closeType) { _closeType = closeType; };
00480
00485 virtual void setFont(FontBase* font);
00486
00490 virtual void redraw();
00491
00496 virtual void erase();
00497
00502 virtual bool enable();
00503
00508 virtual bool disable();
00509
00515 virtual void close();
00516
00524 virtual bool shelve();
00525
00532 virtual bool unshelve();
00533
00540 virtual bool show();
00541
00548 virtual bool hide();
00549
00556 virtual bool click(s16 x, s16 y);
00557
00564 virtual bool doubleClick(s16 x, s16 y);
00565
00572 virtual bool shiftClick(s16 x, s16 y);
00573
00580 virtual bool release(s16 x, s16 y);
00581
00590 virtual bool drag(s16 x, s16 y, s16 vX, s16 vY);
00591
00597 virtual bool keyPress(KeyCode keyCode);
00598
00604 virtual bool keyRelease(KeyCode keyCode);
00605
00610 virtual void lidClose();
00611
00616 virtual void lidOpen();
00617
00622 virtual bool focus();
00623
00628 virtual bool blur();
00629
00637 virtual bool moveTo(s16 x, s16 y);
00638
00645 virtual bool resize(u16 width, u16 height);
00646
00657 virtual bool changeDimensions(s16 x, s16 y, u16 width, u16 height);
00658
00663 virtual bool raiseToTop();
00664
00669 virtual bool lowerToBottom();
00670
00677 virtual bool raiseGadgetToTop(Gadget* gadget);
00678
00685 virtual bool lowerGadgetToBottom(Gadget* gadget);
00686
00692 void moveChildToDeleteQueue(Gadget* gadget);
00693
00702 bool moveChildToShelvedList(Gadget* gadget);
00703
00713 bool moveShelvedToChildList(Gadget* gadget);
00714
00721 virtual void setFocusedGadget(Gadget* gadget);
00722
00729 bool checkCollision(s16 x, s16 y) const;
00730
00739 bool checkCollision(s16 x, s16 y, u16 width, u16 height) const;
00740
00746 bool checkCollision(Gadget* gadget) const;
00747
00754 void invalidateLowerGadgetsVisibleRectCache(Gadget* gadget);
00755
00760 void unregisterChildrenFromVBL();
00761
00769 void addGadget(Gadget* gadget);
00770
00778 void insertGadget(Gadget* gadget);
00779
00785 inline void setParent(Gadget* parent) { _parent = parent; };
00786
00792 virtual void setDragging(u16 x, u16 y);
00793
00797 void cacheVisibleRects();
00798
00803 void invalidateVisibleRectCache();
00804
00811 virtual inline void draw(Rect clipRect) { };
00812
00817 virtual void eraseGadget(Gadget* gadget);
00818
00824 virtual void redrawDirty(WoopsiArray<Rect>* invalidRects, Gadget* sender);
00825
00830 void clipRectToHierarchy(Rect& rect) const;
00831
00837 virtual bool swapGadgetDepth(Gadget* gadget);
00838
00843 bool swapDepth();
00844
00849 inline void destroy() { delete this; };
00850
00858 bool remove();
00859
00868 bool removeChild(Gadget* gadget);
00869
00875 void addContextMenuItem(const char* name, u32 value);
00876
00882 virtual void showContextMenu(s16 x, s16 y);
00883
00888 virtual bool handleContextMenuSelection(const ContextMenuItem* item);
00889
00893 void goModal();
00894
00898 inline void stopModal() { _flags.modal = false; };
00899
00905 const s32 getGadgetIndex(const Gadget* gadget) const;
00906
00912 const Gadget* getChild(u32 index) const;
00913
00918 const s32 getChildCount() const { return _gadgets.size(); };
00919
00924 const inline s32 getDecorationCount() const { return _decorationCount; };
00925
00930 inline RectCache* getRectCache() const { return _rectCache; };
00931
00932 protected:
00933 s16 _x;
00934 s16 _y;
00935 u16 _width;
00936 u16 _height;
00937 u32 _refcon;
00939
00940 s16 _grabPointX;
00941 s16 _grabPointY;
00942 s16 _newX;
00943 s16 _newY;
00945
00946 u16 _backColour;
00947 u16 _shineColour;
00948 u16 _highlightColour;
00949 u16 _shadowColour;
00950 u16 _fillColour;
00951 u16 _darkColour;
00953
00954 Flags _flags;
00956
00957 WoopsiArray<GadgetEventHandler*> _gadgetEventHandlers;
00959
00960 u32 _lastClickTime;
00961 s16 _lastClickX;
00962 s16 _lastClickY;
00963 s16 _doubleClickBounds;
00964 s16 _doubleClickTime;
00966
00967 Gadget* _parent;
00968 Gadget* _focusedGadget;
00969 WoopsiArray<Gadget*> _gadgets;
00970 WoopsiArray<Gadget*> _shelvedGadgets;
00972
00973 u8 _decorationCount;
00975
00976 RectCache* _rectCache;
00978 OutlineType _outline;
00979 CloseType _closeType;
00981 FontBase* _font;
00983
00984 WoopsiArray<NameValuePair> _contextMenuItems;
00989 virtual ~Gadget();
00990
01005 const s16 calculatePhysicalScreenY(s16 y) const;
01006
01016 const u8 calculatePhysicalScreenNumber(s16 y) const;
01017
01022 void clear(Rect clipRect);
01023
01028 void clear();
01029
01033 void drawChildren();
01034
01041 virtual void closeChild(Gadget* gadget);
01042
01049 virtual void shelveChild(Gadget* gadget);
01050
01057 virtual void redrawDirtyChildren(WoopsiArray<Rect>* invalidRects, Gadget* sender);
01058
01070 virtual GraphicsPort* newInternalGraphicsPort(bool isForeground);
01071
01079 virtual GraphicsPort* newInternalGraphicsPort(Rect clipRect);
01080
01086 void raiseClickEvent(s16 x, s16 y);
01087
01093 void raiseDoubleClickEvent(s16 x, s16 y);
01094
01100 void raiseShiftClickEvent(s16 x, s16 y);
01101
01107 void raiseReleaseEvent(s16 x, s16 y);
01108
01114 void raiseReleaseOutsideEvent(s16 x, s16 y);
01115
01123 void raiseDragEvent(s16 x, s16 y, s16 vX, s16 vY);
01124
01128 void raiseMoveForwardEvent();
01129
01133 void raiseMoveBackwardEvent();
01134
01139 void raiseKeyPressEvent(KeyCode keyCode);
01140
01145 void raiseKeyReleaseEvent(KeyCode keyCode);
01146
01150 void raiseLidCloseEvent();
01151
01155 void raiseLidOpenEvent();
01156
01160 void raiseFocusEvent();
01161
01165 void raiseBlurEvent();
01166
01170 void raiseCloseEvent();
01171
01175 void raiseHideEvent();
01176
01180 void raiseShowEvent();
01181
01185 void raiseShelveEvent();
01186
01190 void raiseUnshelveEvent();
01191
01195 void raiseEnableEvent();
01196
01200 void raiseDisableEvent();
01201
01205 void raiseValueChangeEvent();
01206
01212 void raiseResizeEvent(u16 width, u16 height);
01213
01221 void raiseMoveEvent(s16 x, s16 y, s16 vX, s16 vY);
01222
01227 void raiseContextMenuSelectionEvent(const ContextMenuItem* contextMenuItem);
01228
01244 void raiseActionEvent(s16 x, s16 y, s16 vX, s16 vY, KeyCode keyCode);
01245
01251 const s32 getHigherVisibleGadget(const s32 startIndex) const;
01252
01258 const s32 getLowerVisibleGadget(const s32 startIndex) const;
01259
01263 inline Gadget(const Gadget& gadget) { };
01264 };
01265 }
01266
01267 #endif