Gump.h

Go to the documentation of this file.
00001 /*
00002  *  Copyright (C) 2003-2007  The Pentagram Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 #ifndef GUMP_H_INCLUDED
00020 #define GUMP_H_INCLUDED
00021 
00022 #include "Object.h"
00023 #include "Rect.h"
00024 #include "FrameID.h"
00025 #include <list>
00026 
00027 class RenderSurface;
00028 class Shape;
00029 class Item;
00030 class GumpNotifyProcess;
00031 
00032 //
00033 // Class Gump
00034 //
00035 // Desc: Base Gump Class that all other Gumps inherit from
00036 //
00037 
00038 class Gump : public Object
00039 {
00040 protected:
00041 
00042         friend class GumpList;
00043 
00044         uint16                          owner;                  // Owner item
00045         Gump *                          parent;                 // Parent gump
00046         sint32                          x, y;                   // Gump's position in parent.
00047                                                                                 // Always the upper left corner!
00048 
00049         Pentagram::Rect         dims;                   // The dimensions/coord space of the gump
00050         uint32                          flags;                  // Gump flags
00051         sint32                          layer;                  // gump ordering layer
00052 
00053         sint32                          index;                  // 'Index'
00054 
00055         Shape                           *shape;                 // The gumps shape (always painted at 0,0)
00056         uint32                          framenum;
00057 
00060         std::list<Gump*>        children;               // List of all gumps
00061         Gump *                          focus_child;    // The child that has focus
00062 
00063         uint16                          notifier;               // Process to notify when we're closing
00064         uint32                          process_result; // Result for the notifier process
00065 
00066 public:
00067         ENABLE_RUNTIME_CLASSTYPE();
00068         Gump();
00069         Gump(int x, int y, int width, int height, uint16 owner = 0,
00070                  uint32 _Flags = 0, sint32 layer = LAYER_NORMAL);
00071         virtual ~Gump();
00072 
00073 public:
00074 
00075         virtual void                            CreateNotifier();
00076         void                                            SetNotifyProcess(GumpNotifyProcess* proc);
00077         GumpNotifyProcess*                      GetNotifyProcess();
00078         inline uint32                           GetResult() { return process_result; }
00079 
00081         inline void                                     SetShape(Shape *_shape, uint32 _framenum)
00082                 { shape = _shape; framenum = _framenum; }
00083 
00084         void                                            SetShape(FrameID frame, bool adjustsize=false);
00085 
00087         inline void                                     SetFramenum(uint32 _framenum)
00088                 { framenum = _framenum; }
00089 
00094         virtual void                            InitGump(Gump* newparent,bool take_focus=true);
00095 
00101         virtual Gump *                          FindGump(const RunTimeClassType& t,
00102                                                                                  bool recursive=true,
00103                                                                                  bool no_inheritance=false);
00104 
00110         template<class T> Gump *        FindGump(bool recursive=true,
00111                                                                                  bool no_inheritance=false)
00112                 { return FindGump(T::ClassType, recursive, no_inheritance); }
00113 
00116         virtual Gump *          FindGump(int mx, int my);
00117 
00122         virtual bool            GetMouseCursor(int mx, int my, Shape &shape,
00123                                                                            sint32 &frame);
00124 
00125         // Notify gumps the render surface changed.
00126         virtual void            RenderSurfaceChanged();
00127 
00130         virtual bool            Run(const uint32 framenum);
00131 
00135         virtual void            CloseItemDependents(void);
00136 
00139         // \param surf The RenderSurface to paint to
00140         // \param lerp_factor The lerp_factor to paint at (0-256)
00141         // \param scaled Set if the gump is being drawn scaled. 
00142         virtual void            Paint(RenderSurface* surf, sint32 lerp_factor, bool scaled);
00143 
00146         // \param surf The RenderSurface to paint to
00147         // \param lerp_factor The lerp_factor to paint at (0-256)
00148         // \param scalex Fixed point scaling factor for x coord
00149         // \param scaley Fixed point scaling factor for y coord
00150         virtual void            PaintCompositing(RenderSurface* surf, sint32 lerp_factor, sint32 scalex, sint32 scaley);
00151 
00152 protected:
00153 
00155         // \param surf The RenderSurface to paint to
00156         // \param lerp_factor The lerp_factor to paint at (0-256)
00157         // \param scaled Set if the gump is being drawn scaled. 
00158         virtual void            PaintThis(RenderSurface* surf, sint32 lerp_factor, bool scaled);
00159 
00161         // \param surf The RenderSurface to paint to
00162         // \param lerp_factor The lerp_factor to paint at (0-256)
00163         // \param scaled Set if the gump is being drawn scaled. 
00164         virtual void            PaintChildren(RenderSurface* surf, sint32 lerp_factor, bool scaled);
00165 
00167         // \param surf The RenderSurface to paint to
00168         // \param lerp_factor The lerp_factor to paint at (0-256)
00169         // \param scalex Fixed point scaling factor for x coord
00170         // \param scaley Fixed point scaling factor for y coord
00171         virtual void            PaintComposited(RenderSurface* surf, sint32 lerp_factor, sint32 scalex, sint32 scaley);
00172 
00173         static inline sint32 ScaleCoord(sint32 c, sint32 factor) { return ((c*factor)+(1<<15))>>16; }
00174         static inline sint32 UnscaleCoord(sint32 c, sint32 factor) { return (c<<16)/factor; }
00175 
00176 public:
00177 
00180         virtual void            Close(bool no_del = false);
00181 
00183         bool                            IsClosing() { return (flags&FLAG_CLOSING)!=0; }
00184 
00186         virtual void            Move(int x_, int y_) { x = x_; y = y_; }
00187 
00189         virtual void            MoveRelative(int x_, int y_) { x += x_; y += y_; }
00190 
00191         enum Position {
00192                 CENTER = 1,
00193                 TOP_LEFT = 2,
00194                 TOP_RIGHT = 3,
00195                 BOTTOM_LEFT = 4,
00196                 BOTTOM_RIGHT = 5,
00197                 TOP_CENTER = 6,
00198                 BOTTOM_CENTER = 7
00199         };
00200 
00202         // \param pos the postition on the parent gump
00203         // \param xoffset an offset from the position on the x-axis
00204         // \param yoffset an offset from the position on the y-axis
00205         virtual void            setRelativePosition(Position pos, int xoffset=0, int yoffset=0);
00206 
00207         //
00208         // Points and Coords
00209         //
00210 
00212         virtual void            GetDims(Pentagram::Rect &d) { d = dims; }
00213 
00215         virtual bool            PointOnGump(int mx, int my);
00216 
00217         enum PointRoundDir {
00218                 ROUND_TOPLEFT = 0,
00219                 ROUND_BOTTOMRIGHT = 1
00220         };
00221         enum RectRoundDir {
00222                 ROUND_INSIDE,
00223                 ROUND_OUTSIDE
00224         };
00225 
00227         virtual void            ScreenSpaceToGump(int &sx, int &sy,
00228                                                                                   PointRoundDir r = ROUND_TOPLEFT);
00229 
00231         virtual void            GumpToScreenSpace(int &gx, int &gy,
00232                                                                                   PointRoundDir r = ROUND_TOPLEFT);
00233 
00235         virtual void            ParentToGump(int &px, int &py,
00236                                                                          PointRoundDir r = ROUND_TOPLEFT);
00237 
00239         virtual void            GumpToParent(int &gx, int &gy,
00240                                                                          PointRoundDir r = ROUND_TOPLEFT);
00241 
00243         virtual void            GumpRectToScreenSpace(int &gx,int &gy, int &gw,int &gh,
00244                                                                                           RectRoundDir r = ROUND_OUTSIDE);
00245 
00247         virtual void            ScreenSpaceToGumpRect(int &sx,int &sy, int &sw,int &sh,
00248                                                                                          RectRoundDir r = ROUND_OUTSIDE);
00249 
00251         virtual uint16          TraceObjId(int mx, int my);
00252 
00255         virtual bool            GetLocationOfItem(uint16 itemid, int &gx, int &gy,
00256                                                                                   sint32 lerp_factor = 256);
00257 
00258 
00259         //
00260         // Some event handlers. In theory they 'should' be able to be mapped to
00261         // Usecode classes.
00262         //
00263         // mx and my are relative to parents position
00264         //
00265         // OnMouseDown returns the Gump that handled the Input, if it was handled.
00266         // The MouseUp,MouseDouble events will be sent to the same gump.
00267         //
00268         // OnMouseMotion works like OnMouseDown,
00269         // but independently of the other methods.
00270         //
00271         // Unhandled input will be passed down to the next lower gump.
00272         //
00273         // A mouse click on a gump will make it focus, IF it wants it.
00274         //
00275         
00276         // Return Gump that handled event
00277         virtual Gump *          OnMouseDown(int button, int mx, int my);
00278         virtual void            OnMouseUp(int  button, int mx, int my) { }
00279         virtual void            OnMouseClick(int button, int mx, int my) { }
00280         virtual void            OnMouseDouble(int button, int mx, int my) { }
00281         virtual Gump *          OnMouseMotion(int mx, int my);
00282 
00283         // OnMouseOver is only call when the mouse first passes over the gump
00284         // OnMouseLeft is call as the mouse leaves the gump.
00285         virtual void            OnMouseOver() { };
00286         virtual void            OnMouseLeft() { };
00287 
00288         // Keyboard input gets sent to the FocusGump. Or if there isn't one, it
00289         // will instead get sent to the default key handler. TextInput requires
00290         // that text mode be enabled. Return true if handled, false if not.
00291         // Default, returns false, unless handled by focus child
00292         virtual bool            OnKeyDown(int key, int mod);
00293         virtual bool            OnKeyUp(int key);
00294         virtual bool            OnTextInput(int unicode);
00295 
00296         // This is for detecting focus changes for keyboard input. Gets called true
00297         // when the this gump is being set as the focus focus gump. It is called
00298         // false when focus is being taken away.
00299         virtual void            OnFocus(bool /*gain*/) { }
00300         
00301         // Makes this gump the focus
00302         virtual void            MakeFocus();
00303 
00304         // Is this gump the focus?
00305         inline bool                     IsFocus()
00306                 { return parent?parent->focus_child==this:false; }
00307 
00308         // Get the child in focus
00309         inline Gump *           GetFocusChild() { return focus_child; }
00310 
00311         // Find a new Child to be the focus
00312         void                            FindNewFocusChild();
00313 
00314 
00315         //
00316         // Child gump related
00317         //
00318 
00320         virtual void            AddChild(Gump *, bool take_focus = true);
00321 
00323         virtual void            RemoveChild(Gump *);
00324 
00326         virtual void            MoveChildToFront(Gump *);
00327 
00329         inline Gump *           GetParent() { return parent; }
00330 
00332         Gump *                          GetRootGump();
00333 
00336         virtual void            ChildNotify(Gump *child, uint32 message) { }
00337         void                            SetIndex(sint32 i) { index = i; }
00338         sint32                          GetIndex() { return index; }
00339 
00340         // Dragging
00343         virtual bool            StartDraggingChild(Gump* gump, int mx, int my);
00344         virtual void            DraggingChild(Gump* gump, int mx, int my);
00345         virtual void            StopDraggingChild(Gump* gump);
00346 
00349         virtual bool            StartDraggingItem(Item* item, int mx, int my)
00350                 { return false; }
00351 
00355         virtual bool            DraggingItem(Item* item, int mx, int my)
00356                 { return false; }
00357 
00359         virtual void            DraggingItemLeftGump(Item* item) { }
00360 
00365         virtual void            StopDraggingItem(Item* item, bool moved) { }
00366 
00372         virtual void            DropItem(Item* item, int mx, int my) { }
00373 
00374 public:
00375 
00376         //
00377         // Gump Flags
00378         //
00379         enum GumpFlags {
00380                 FLAG_DRAGGABLE          = 0x01,         // When set, the gump can be dragged
00381                 FLAG_HIDDEN                     = 0x02,         // When set, the gump will not be drawn
00382                 FLAG_CLOSING            = 0x04,         // When set, the gump is closing
00383                 FLAG_CLOSE_AND_DEL      = 0x08,         // When set, the gump is closing and will be deleted
00384                 FLAG_ITEM_DEPENDENT     = 0x10,         // When set, the gump will be deleted on MapChange
00385                 FLAG_DONT_SAVE      = 0x20,             // When set, don't save this gump.
00386                                                         // Be very careful with this one!
00387                 FLAG_CORE_GUMP      = 0x40,             // core gump (only children are saved)
00388                 FLAG_KEEP_VISIBLE   = 0x80              // Keep this gump on-screen.
00389                                                                                 // (only for ItemRelativeGumps)
00390         };
00391 
00392         inline bool                     IsHidden()
00393                 { return (flags&FLAG_HIDDEN) || (parent && parent->IsHidden()); }
00394         bool                            IsDraggable() { return flags&FLAG_DRAGGABLE; }
00395         virtual void            HideGump() { flags |= FLAG_HIDDEN; }
00396         virtual void            UnhideGump() { flags &= ~FLAG_HIDDEN; }
00397 
00398         bool mustSave(bool toplevel);
00399 
00400         //
00401         // Gump Layers
00402         //
00403         enum GumpLayers {
00404                 LAYER_DESKTOP           = -16,          // Layer for Desktop 'bottom most'
00405                 LAYER_GAMEMAP           = -8,           // Layer for the World Gump
00406                 LAYER_NORMAL            = 0,            // Layer for Normal gumps
00407                 LAYER_ABOVE_NORMAL      = 8,            // Layer for Always on top Gumps
00408                 LAYER_MODAL         = 12,               // Layer for Modal Gumps
00409                 LAYER_CONSOLE           = 16            // Layer for the console
00410         };
00411 
00412         bool loadData(IDataSource* ids, uint32 version);
00413 protected:
00414         virtual void saveData(ODataSource* ods);
00415 };
00416 
00417 #endif //GUMP_H_INCLUDED

Generated on Fri Jul 27 22:27:17 2007 for pentagram by  doxygen 1.4.7