ScummVM API documentation
gump.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef ULTIMA8_GUMPS_GUMP_H
23 #define ULTIMA8_GUMPS_GUMP_H
24 
25 #include "ultima/ultima8/kernel/object.h"
26 #include "ultima/ultima8/misc/rect.h"
27 #include "ultima/ultima8/gfx/frame_id.h"
28 #include "ultima/shared/std/containers.h"
29 #include "ultima/ultima8/misc/classtype.h"
30 
31 namespace Ultima {
32 namespace Ultima8 {
33 
34 class RenderSurface;
35 class Shape;
36 class Item;
37 class GumpNotifyProcess;
38 
39 class Gump;
40 typedef bool (*FindGumpPredicate)(const Gump *g);
41 template<class T> inline bool IsOfType(const Gump *g) { return dynamic_cast<const T*>(g) != nullptr; }
42 
47 class Gump : public Object {
48 protected:
49  uint16 _owner; // Owner item
50  Gump *_parent; // Parent gump
51  int32 _x, _y; // Gump's position in parent.
52  // Always the upper left corner!
53 
54  Rect _dims; // The dimensions/coord space of the gump
55  uint32 _flags; // Gump flags
56  int32 _layer; // gump ordering layer
57 
58  int32 _index; // 'Index'
59 
60  const Shape *_shape; // The gumps shape (always painted at 0,0)
61  uint32 _frameNum;
62 
65  Std::list<Gump *> _children; // List of all gumps
66  Gump *_focusChild; // The child that has focus
67 
68  uint16 _notifier; // Process to notify when we're closing
69  uint32 _processResult; // Result for the notifier process
70 
71 public:
72  ENABLE_RUNTIME_CLASSTYPE()
73  Gump();
74  Gump(int x, int y, int width, int height, uint16 owner = 0,
75  uint32 flags = 0, int32 layer = LAYER_NORMAL);
76  ~Gump() override;
77 
78 public:
79 
80  virtual void CreateNotifier();
81  void SetNotifyProcess(GumpNotifyProcess *proc);
82  GumpNotifyProcess *GetNotifyProcess();
83  inline uint32 GetResult() {
84  return _processResult;
85  }
86  void SetResult(uint32 res) {
87  _processResult = res;
88  }
89 
91  inline void SetShape(const Shape *shape, uint32 frameNum) {
92  _shape = shape;
93  _frameNum = frameNum;
94  }
95 
96  void SetShape(FrameID frame, bool adjustsize = false);
97 
99  void UpdateDimsFromShape();
100 
102  inline void Set_frameNum(uint32 frameNum) {
103  _frameNum = frameNum;
104  }
105 
110  virtual void InitGump(Gump *newparent, bool take_focus = true);
111 
116  virtual Gump *FindGump(FindGumpPredicate predicate, bool recursive = true);
117 
122  template<class T> Gump *FindGump(bool recursive = true) {
123  return FindGump(&IsOfType<T>, recursive);
124  }
125 
127  template<int T> static bool FindByIndex(const Gump *g) { return g->GetIndex() == T; }
128 
131  virtual Gump *FindGump(int mx, int my);
132 
137  virtual bool GetMouseCursor(int32 mx, int32 my, Shape &shape, int32 &frame);
138 
139  // Notify gumps the render surface changed.
140  virtual void RenderSurfaceChanged();
141 
143  virtual void run();
144 
148  virtual void CloseItemDependents();
149 
152  // \param surf The RenderSurface to paint to
153  // \param lerp_factor The lerp_factor to paint at (0-256)
154  // \param scaled Set if the gump is being drawn scaled.
155  virtual void Paint(RenderSurface *surf, int32 lerp_factor, bool scaled);
156 
159  // \param surf The RenderSurface to paint to
160  // \param lerp_factor The lerp_factor to paint at (0-256)
161  // \param scalex Fixed point scaling factor for x coord
162  // \param scaley Fixed point scaling factor for y coord
163  virtual void PaintCompositing(RenderSurface *surf, int32 lerp_factor, int32 scalex, int32 scaley);
164 
165 protected:
166 
168  // \param surf The RenderSurface to paint to
169  // \param lerp_factor The lerp_factor to paint at (0-256)
170  // \param scaled Set if the gump is being drawn scaled.
171  virtual void PaintThis(RenderSurface *surf, int32 lerp_factor, bool scaled);
172 
174  // \param surf The RenderSurface to paint to
175  // \param lerp_factor The lerp_factor to paint at (0-256)
176  // \param scaled Set if the gump is being drawn scaled.
177  virtual void PaintChildren(RenderSurface *surf, int32 lerp_factor, bool scaled);
178 
180  // \param surf The RenderSurface to paint to
181  // \param lerp_factor The lerp_factor to paint at (0-256)
182  // \param scalex Fixed point scaling factor for x coord
183  // \param scaley Fixed point scaling factor for y coord
184  virtual void PaintComposited(RenderSurface *surf, int32 lerp_factor, int32 scalex, int32 scaley);
185 
186  static inline int32 ScaleCoord(int32 c, int32 factor) {
187  return ((c * factor) + (1 << 15)) >> 16;
188  }
189  static inline int32 UnscaleCoord(int32 c, int32 factor) {
190  return (c << 16) / factor;
191  }
192 
193 public:
194 
197  virtual void Close(bool no_del = false);
198 
200  bool IsClosing() const {
201  return (_flags & FLAG_CLOSING) != 0;
202  }
203 
205  virtual void Move(int32 x, int32 y) {
206  _x = x;
207  _y = y;
208  }
209 
211  virtual void MoveRelative(int x, int y) {
212  _x += x;
213  _y += y;
214  }
215 
216  void getLocation(int32 &x, int32 &y) const {
217  x = _x;
218  y = _y;
219  }
220 
221  enum Position {
222  CENTER = 1,
223  TOP_LEFT = 2,
224  TOP_RIGHT = 3,
225  BOTTOM_LEFT = 4,
226  BOTTOM_RIGHT = 5,
227  TOP_CENTER = 6,
228  BOTTOM_CENTER = 7,
229  LEFT_CENTER = 8,
230  RIGHT_CENTER = 9
231  };
232 
234  // \param pos the postition on the parent gump
235  // \param xoffset an offset from the position on the x-axis
236  // \param yoffset an offset from the position on the y-axis
237  virtual void setRelativePosition(Position pos, int xoffset = 0, int yoffset = 0);
238 
239  //
240  // Points and Coords
241  //
242 
244  virtual void GetDims(Rect &d) const {
245  d = _dims;
246  }
247 
249  virtual void SetDims(const Rect &d) {
250  _dims = d;
251  }
252 
254  virtual bool PointOnGump(int mx, int my);
255 
256  enum PointRoundDir {
257  ROUND_TOPLEFT = 0,
258  ROUND_BOTTOMRIGHT = 1
259  };
260  enum RectRoundDir {
261  ROUND_INSIDE,
262  ROUND_OUTSIDE
263  };
264 
266  virtual void ScreenSpaceToGump(int32 &sx, int32 &sy,
267  PointRoundDir r = ROUND_TOPLEFT);
268 
270  virtual void GumpToScreenSpace(int32 &gx, int32 &gy,
271  PointRoundDir r = ROUND_TOPLEFT);
272 
274  virtual void ParentToGump(int32 &px, int32 &py,
275  PointRoundDir r = ROUND_TOPLEFT);
276 
278  virtual void GumpToParent(int32 &gx, int32 &gy,
279  PointRoundDir r = ROUND_TOPLEFT);
280 
282  virtual void GumpRectToScreenSpace(Rect &gr, RectRoundDir r = ROUND_OUTSIDE);
283 
285  virtual void ScreenSpaceToGumpRect(Rect &sr, RectRoundDir r = ROUND_OUTSIDE);
286 
288  virtual uint16 TraceObjId(int32 mx, int32 my);
289 
292  virtual bool GetLocationOfItem(uint16 itemid, int32 &gx, int32 &gy,
293  int32 lerp_factor = 256);
294 
295 
296  //
297  // Some event handlers. In theory they 'should' be able to be mapped to
298  // Usecode classes.
299  //
300  // mx and my are relative to parents position
301  //
302  // onMouseDown returns the Gump that handled the Input, if it was handled.
303  // The MouseUp,MouseDouble events will be sent to the same gump.
304  //
305  // onMouseMotion works like onMouseDown,
306  // but independently of the other methods.
307  //
308  // Unhandled input will be passed down to the next lower gump.
309  //
310  // A mouse click on a gump will make it focus, IF it wants it.
311  //
312  // It is often preferrable to handle both click and double events
313  // rather than only the up event to avoid unintended clicks after
314  // performing intended action.
315 
316  // Return Gump that handled event
317  virtual Gump *onMouseDown(int button, int32 mx, int32 my);
318  virtual void onMouseUp(int button, int32 mx, int32 my) { }
319  virtual void onMouseClick(int button, int32 mx, int32 my) { }
320  virtual void onMouseDouble(int button, int32 mx, int32 my) { }
321  virtual Gump *onMouseMotion(int32 mx, int32 my);
322 
323  // onMouseOver is only call when the mouse first passes over the gump
324  // onMouseLeft is call as the mouse leaves the gump.
325  virtual void onMouseOver() { };
326  virtual void onMouseLeft() { };
327 
328  // Keyboard input gets sent to the FocusGump. Or if there isn't one, it
329  // will instead get sent to the default key handler. TextInput requires
330  // that text mode be enabled. Return true if handled, false if not.
331  // Default, returns false, unless handled by focus child
332  virtual bool OnKeyDown(int key, int mod);
333  virtual bool OnKeyUp(int key);
334  virtual bool OnTextInput(int unicode);
335 
336  // This is for detecting focus changes for keyboard input. Gets called true
337  // when the this gump is being set as the focus focus gump. It is called
338  // false when focus is being taken away.
339  virtual void OnFocus(bool /*gain*/) { }
340 
341  // Makes this gump the focus
342  virtual void MakeFocus();
343 
344  // Is this gump the focus?
345  inline bool IsFocus() {
346  return _parent ? _parent->_focusChild == this : false;
347  }
348 
349  // Get the child in focus
350  inline Gump *GetFocusChild() {
351  return _focusChild;
352  }
353 
354  // Find a new Child to be the focus
355  void FindNewFocusChild();
356 
357 
358  //
359  // Child gump related
360  //
361 
363  virtual void AddChild(Gump *, bool take_focus = true);
364 
366  virtual void RemoveChild(Gump *);
367 
369  virtual void MoveChildToFront(Gump *);
370 
372  inline Gump *GetParent() {
373  return _parent;
374  }
375 
377  Gump *GetRootGump();
378 
381  virtual void ChildNotify(Gump *child, uint32 message) { }
382  void SetIndex(int32 i) {
383  _index = i;
384  }
385  int32 GetIndex() const {
386  return _index;
387  }
388 
391  virtual bool onDragStart(int32 mx, int32 my);
392  virtual void onDragStop(int32 mx, int32 my);
393  virtual void onDrag(int32 mx, int32 my);
394 
397  virtual bool StartDraggingItem(Item *item, int mx, int my) {
398  return false;
399  }
400 
404  virtual bool DraggingItem(Item *item, int mx, int my) {
405  return false;
406  }
407 
409  virtual void DraggingItemLeftGump(Item *item) { }
410 
415  virtual void StopDraggingItem(Item *item, bool moved) { }
416 
422  virtual void DropItem(Item *item, int mx, int my) { }
423 
424 public:
425 
426  //
427  // Gump Flags
428  //
429  enum GumpFlags {
430  FLAG_DRAGGABLE = 0x0001, // When set, the gump can be dragged
431  FLAG_HIDDEN = 0x0002, // When set, the gump will not be drawn
432  FLAG_CLOSING = 0x0004, // When set, the gump is closing
433  FLAG_CLOSE_AND_DEL = 0x0008, // When set, the gump is closing and will be deleted
434  FLAG_ITEM_DEPENDENT = 0x0010, // When set, the gump will be deleted on MapChange
435  FLAG_DONT_SAVE = 0x0020, // When set, don't save this gump. Be very careful with this one!
436  FLAG_CORE_GUMP = 0x0040, // core gump (only children are saved)
437  FLAG_KEEP_VISIBLE = 0x0080, // Keep this gump on-screen. (only for ItemRelativeGumps)
438  FLAG_PREVENT_SAVE = 0x0100 // When set, prevent game from saving
439  };
440 
442  inline bool hasFlags(uint flags) const {
443  return (_flags & flags) != 0;
444  }
445 
446  inline bool IsHidden() const {
447  return (_flags & FLAG_HIDDEN) || (_parent && _parent->IsHidden());
448  }
449  bool IsDraggable() const {
450  return _flags & FLAG_DRAGGABLE;
451  }
452  virtual void HideGump() {
453  _flags |= FLAG_HIDDEN;
454  }
455  virtual void UnhideGump() {
456  _flags &= ~FLAG_HIDDEN;
457  }
458  void SetVisibility(bool visible) {
459  if (visible)
460  UnhideGump();
461  else
462  HideGump();
463  }
464 
465  bool mustSave(bool toplevel) const;
466 
467  //
468  // Gump Layers
469  //
470  enum GumpLayers {
471  LAYER_DESKTOP = -16, // Layer for Desktop 'bottom most'
472  LAYER_GAMEMAP = -8, // Layer for the World Gump
473  LAYER_NORMAL = 0, // Layer for Normal gumps
474  LAYER_ABOVE_NORMAL = 8, // Layer for Always on top Gumps
475  LAYER_MODAL = 12, // Layer for Modal Gumps
476  LAYER_CONSOLE = 16 // Layer for the console
477  };
478 
479  enum Message {
480  GUMP_CLOSING = 0x100
481  };
482 
483  bool loadData(Common::ReadStream *rs, uint32 version);
484  void saveData(Common::WriteStream *ws) override;
485 };
486 
487 } // End of namespace Ultima8
488 } // End of namespace Ultima
489 
490 #endif
virtual void setRelativePosition(Position pos, int xoffset=0, int yoffset=0)
Moves this gump to a relative location on the parent gump.
virtual void Move(int32 x, int32 y)
Move this gump.
Definition: gump.h:205
virtual bool StartDraggingItem(Item *item, int mx, int my)
Definition: gump.h:397
virtual void DraggingItemLeftGump(Item *item)
Called when an item that was being dragged over the gump left the gump.
Definition: gump.h:409
Definition: gump_notify_process.h:33
virtual void InitGump(Gump *newparent, bool take_focus=true)
virtual void SetDims(const Rect &d)
Set the _dims.
Definition: gump.h:249
Definition: stream.h:77
Definition: rect.h:32
Definition: item.h:42
virtual void GumpRectToScreenSpace(Rect &gr, RectRoundDir r=ROUND_OUTSIDE)
Transform a rectangle to screenspace from gumpspace.
virtual void GumpToParent(int32 &gx, int32 &gy, PointRoundDir r=ROUND_TOPLEFT)
Convert a gump point to parent relative point.
virtual void PaintChildren(RenderSurface *surf, int32 lerp_factor, bool scaled)
Paint the Gumps Children (RenderSurface is relative to this)
virtual void PaintCompositing(RenderSurface *surf, int32 lerp_factor, int32 scalex, int32 scaley)
virtual void RemoveChild(Gump *)
Remove a gump from the child list.
Definition: render_surface.h:40
virtual void PaintThis(RenderSurface *surf, int32 lerp_factor, bool scaled)
Overloadable method to Paint just this Gump (RenderSurface is relative to this)
Definition: gump.h:47
virtual void run()
Run the gump.
Definition: detection.h:27
Gump * GetParent()
Get the parent.
Definition: gump.h:372
Definition: object.h:39
virtual void ScreenSpaceToGumpRect(Rect &sr, RectRoundDir r=ROUND_OUTSIDE)
Transform a rectangle to gumpspace from screenspace.
void UpdateDimsFromShape()
Update the width/height to match the gump&#39;s current shape frame.
virtual void ParentToGump(int32 &px, int32 &py, PointRoundDir r=ROUND_TOPLEFT)
Convert a parent relative point to a gump point.
virtual void StopDraggingItem(Item *item, bool moved)
Definition: gump.h:415
virtual void AddChild(Gump *, bool take_focus=true)
Add a gump to the child list.
virtual void PaintComposited(RenderSurface *surf, int32 lerp_factor, int32 scalex, int32 scaley)
Overloadable method to Paint just this gumps unscaled components that require compositing (RenderSurf...
Gump * GetRootGump()
Get the root gump (or self)
Gump * FindGump(bool recursive=true)
Definition: gump.h:122
static bool FindByIndex(const Gump *g)
A predicate to find a ui element by its index.
Definition: gump.h:127
virtual void ChildNotify(Gump *child, uint32 message)
Definition: gump.h:381
virtual void ScreenSpaceToGump(int32 &sx, int32 &sy, PointRoundDir r=ROUND_TOPLEFT)
Convert a screen space point to a gump point.
virtual bool DraggingItem(Item *item, int mx, int my)
Definition: gump.h:404
void SetShape(const Shape *shape, uint32 frameNum)
Set the Gump&#39;s shape/frame.
Definition: gump.h:91
virtual bool onDragStart(int32 mx, int32 my)
virtual uint16 TraceObjId(int32 mx, int32 my)
Trace a click, and return ObjId.
bool hasFlags(uint flags) const
Does this gump have any of the given flags mask set.
Definition: gump.h:442
virtual bool GetLocationOfItem(uint16 itemid, int32 &gx, int32 &gy, int32 lerp_factor=256)
virtual void CloseItemDependents()
Definition: frame_id.h:30
virtual void MoveRelative(int x, int y)
Move this gump relative to its current position.
Definition: gump.h:211
Definition: containers.h:200
Definition: stream.h:385
virtual void GumpToScreenSpace(int32 &gx, int32 &gy, PointRoundDir r=ROUND_TOPLEFT)
Convert a gump point to a screen space point.
Definition: shape.h:38
virtual bool GetMouseCursor(int32 mx, int32 my, Shape &shape, int32 &frame)
virtual void DropItem(Item *item, int mx, int my)
Definition: gump.h:422
virtual void Paint(RenderSurface *surf, int32 lerp_factor, bool scaled)
virtual Gump * FindGump(FindGumpPredicate predicate, bool recursive=true)
virtual void MoveChildToFront(Gump *)
Move child to front (within its layer)
virtual void GetDims(Rect &d) const
Get the _dims.
Definition: gump.h:244
Std::list< Gump * > _children
Definition: gump.h:65
bool IsClosing() const
Check to see if a Gump is Closing.
Definition: gump.h:200
virtual bool PointOnGump(int mx, int my)
Detect if a point is on the gump.
void Set_frameNum(uint32 frameNum)
Set the Gump&#39;s frame.
Definition: gump.h:102
virtual void Close(bool no_del=false)