KDEGames

kgamecanvas.h
1 /* Originally created for KBoard
2  Copyright 2006 Maurizio Monge <[email protected]>
3 
4 BSD License
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions
7 are met:
8 
9 1. Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in the
13  documentation and/or other materials provided with the distribution.
14 
15 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef __KGAMECANVAS_H__
28 #define __KGAMECANVAS_H__
29 
30 /*
31  * Author: Maurizio Monge <[email protected]>
32  */
33 
34 #include <QList>
35 #include <QPoint>
36 #include <QPicture>
37 #include <QPixmap>
38 #include <QPainter>
39 #include <QRect>
40 #include <QRegion>
41 #include <QWidget>
42 #include "libkdegamesprivate_export.h"
43 #include <KGameRendererClient>
44 
45 class KGameCanvasItem;
46 
55 class KDEGAMESPRIVATE_EXPORT KGameCanvasAbstract
56 {
57 protected:
58  friend class KGameCanvasItem;
59 
61  QList<KGameCanvasItem*> m_animated_items;
62 
63 public:
66 
67  virtual ~KGameCanvasAbstract();
68 
70  const QList<KGameCanvasItem*>* items() const { return &m_items; }
71 
73  KGameCanvasItem* itemAt(const QPoint &pos) const;
74 
76  KGameCanvasItem* itemAt(int x, int y) const { return itemAt(QPoint(x,y)); }
77 
80  QList<KGameCanvasItem*> itemsAt(const QPoint &pos) const;
81 
83  QList<KGameCanvasItem*> itemsAt(int x, int y) const { return itemsAt(QPoint(x,y)); }
84 
86  virtual void ensureAnimating() = 0;
87 
89  virtual void ensurePendingUpdate() = 0;
90 
92  virtual void invalidate(const QRect& r, bool translate = true) = 0;
93 
95  virtual void invalidate(const QRegion& r, bool translate = true) = 0;
96 
98  virtual class KGameCanvasWidget* topLevelCanvas() = 0;
99 
101  virtual QPoint canvasPosition() const = 0;
102 };
103 
104 
114 class KDEGAMESPRIVATE_EXPORT KGameCanvasItem
115 {
116 private:
117  friend class KGameCanvasAbstract;
118  friend class KGameCanvasWidget;
119  friend class KGameCanvasGroup;
120  friend class KGameCanvasAdapter;
121 
122  bool m_visible;
123  bool m_animated;
124  int m_opacity;
125  QPoint m_pos;
126  KGameCanvasAbstract *m_canvas;
127 
128  bool m_changed;
129  QRect m_last_rect;
130 
131  static QPixmap* transparence_pixmap_cache;
132  static QPixmap* getTransparenceCache(const QSize &s);
133  virtual void paintInternal(QPainter* p, const QRect& prect, const QRegion& preg,
134  const QPoint &delta, double cumulative_opacity);
135 
136  void updateAfterRestack(int from, int to);
137 
138  /* function to update pending changes, called from parent */
139  virtual void updateChanges();
140 
141 public:
148  explicit KGameCanvasItem(KGameCanvasAbstract* canvas = nullptr);
149 
150  virtual ~KGameCanvasItem();
151 
153  virtual void changed();
154 
156  bool visible() const { return m_visible; }
157 
159  void setVisible(bool v);
160 
162  bool animated() const { return m_animated; }
163 
165  void setAnimated(bool a);
166 
168  int opacity() const { return m_opacity; }
169 
171  void setOpacity(int o);
172 
174  void hide(){ setVisible(false); }
175 
177  void show(){ setVisible(true); }
178 
180  void raise();
181 
183  void lower();
184 
186  void stackOver(KGameCanvasItem* ref);
187 
189  void stackUnder(KGameCanvasItem* ref);
190 
192  KGameCanvasAbstract *canvas() const { return m_canvas; }
193 
195  KGameCanvasWidget *topLevelCanvas() const { return m_canvas ? m_canvas->topLevelCanvas() : nullptr; }
196 
199  void putInCanvas(KGameCanvasAbstract *canvas);
200 
202  QPoint pos() const { return m_pos; }
203 
205  QPoint absolutePosition() const;
206 
210  void moveTo(const QPoint &newpos);
211 
213  void moveTo(int x, int y) { moveTo( QPoint(x,y)); }
214 
216  virtual void paint(QPainter* p) = 0;
217 
219  virtual QRect rect() const = 0;
220 
225  virtual bool layered() const;
226 
230  virtual void advance(int msecs);
231 };
232 
233 
244 class KDEGAMESPRIVATE_EXPORT KGameCanvasDummy : public KGameCanvasItem
245 {
246 public:
248  explicit KGameCanvasDummy(KGameCanvasAbstract* canvas = nullptr);
249 
250  virtual ~KGameCanvasDummy();
251 
253  void paint(QPainter* p) override;
254 
256  QRect rect() const override;
257 };
258 
259 
269 class KDEGAMESPRIVATE_EXPORT KGameCanvasGroup : public KGameCanvasItem, public KGameCanvasAbstract
270 {
271 private:
272  mutable bool m_child_rect_changed;
273  mutable QRect m_last_child_rect;
274 
275  void paintInternal(QPainter* p, const QRect& prect, const QRegion& preg,
276  const QPoint& delta, double cumulative_opacity) override;
277 
278  void ensureAnimating() override;
279  void ensurePendingUpdate() override;
280  void invalidate(const QRect& r, bool translate = true) override;
281  void invalidate(const QRegion& r, bool translate = true) override;
282  void updateChanges() override;
283  void changed() override;
284 
285 public:
287  explicit KGameCanvasGroup(KGameCanvasAbstract* canvas = nullptr);
288 
289  virtual ~KGameCanvasGroup();
290 
292  void paint(QPainter* p) override;
293 
295  QRect rect() const override;
296 
298  void advance(int msecs) override;
299 
301  KGameCanvasWidget* topLevelCanvas() override;
302 
303  QPoint canvasPosition() const override;
304 };
305 
314 class KDEGAMESPRIVATE_EXPORT KGameCanvasPicture : public KGameCanvasItem
315 {
316 private:
317  QPicture m_picture;
318 
319 public:
321  explicit KGameCanvasPicture(const QPicture& picture, KGameCanvasAbstract* canvas = nullptr);
322 
324  explicit KGameCanvasPicture(KGameCanvasAbstract* canvas = nullptr);
325 
326  virtual ~KGameCanvasPicture();
327 
329  QPicture picture() const { return m_picture; }
330 
332  void setPicture(const QPicture& picture);
333 
334  void paint(QPainter* p) override;
335  QRect rect() const override;
336 };
337 
346 class KDEGAMESPRIVATE_EXPORT KGameCanvasPixmap : public KGameCanvasItem
347 {
348 private:
349  QPixmap m_pixmap;
350 
351 public:
353  explicit KGameCanvasPixmap(const QPixmap& pixmap, KGameCanvasAbstract* canvas = nullptr);
354 
356  explicit KGameCanvasPixmap(KGameCanvasAbstract* canvas = nullptr);
357 
358  virtual ~KGameCanvasPixmap();
359 
361  QPixmap pixmap() const { return m_pixmap; }
362 
364  void setPixmap(const QPixmap& pixmap);
365 
366  void paint(QPainter* p) override;
367  QRect rect() const override;
368  bool layered() const override { return false; }
369 };
370 
381 class KDEGAMESPRIVATE_EXPORT KGameCanvasRenderedPixmap : public KGameCanvasPixmap, public KGameRendererClient
382 {
383 public:
384  KGameCanvasRenderedPixmap(KGameRenderer* renderer, const QString& spriteKey, KGameCanvasAbstract* canvas = nullptr);
385 protected:
386  void receivePixmap(const QPixmap& pixmap) override;
387 };
388 
397 class KDEGAMESPRIVATE_EXPORT KGameCanvasTiledPixmap : public KGameCanvasItem
398 {
399 private:
400  QPixmap m_pixmap;
401  QSize m_size;
402  QPoint m_origin;
403  bool m_move_orig;
404 
405 public:
407  KGameCanvasTiledPixmap(const QPixmap& pixmap, const QSize &size, const QPoint &origin,
408  bool move_orig, KGameCanvasAbstract* canvas = nullptr);
409 
411  explicit KGameCanvasTiledPixmap(KGameCanvasAbstract* canvas = nullptr);
412 
413  virtual ~KGameCanvasTiledPixmap();
414 
416  QPixmap pixmap() const { return m_pixmap; }
417 
419  void setPixmap(const QPixmap& pixmap);
420 
422  void setSize(const QSize &size);
423 
425  QPoint origin() const { return m_move_orig ? m_origin + pos() : m_origin; }
426 
428  void setOrigin(const QPoint &size);
429 
431  bool moveOrigin(){ return m_move_orig; }
432 
434  void setMoveOrigin(bool move_orig);
435 
436  void paint(QPainter* p) override;
437  QRect rect() const override;
438  bool layered() const override { return false; }
439 };
440 
441 
450 class KDEGAMESPRIVATE_EXPORT KGameCanvasRectangle : public KGameCanvasItem
451 {
452 private:
453  QColor m_color;
454  QSize m_size;
455 
456 public:
458  KGameCanvasRectangle(const QColor& color, const QSize &size, KGameCanvasAbstract* canvas = nullptr);
459 
461  explicit KGameCanvasRectangle(KGameCanvasAbstract* canvas = nullptr);
462 
463  virtual ~KGameCanvasRectangle();
464 
466  QColor color() const { return m_color; }
467 
469  void setColor(const QColor& color);
470 
472  void setSize(const QSize &size);
473 
474  void paint(QPainter* p) override;
475  QRect rect() const override;
476  bool layered() const override { return false; }
477 };
478 
487 class KDEGAMESPRIVATE_EXPORT KGameCanvasText : public KGameCanvasItem
488 {
489 public:
493  enum HPos {
494  HStart,
495  HLeft,
496  HRight,
497  HCenter
498  };
499 
503  enum VPos {
504  VBaseline,
505  VTop,
506  VBottom,
507  VCenter
508  };
509 
510 private:
511  QString m_text;
512  QColor m_color;
513  QFont m_font;
514  HPos m_hpos;
515  VPos m_vpos;
516  QRect m_bounding_rect;
517 
518  QPoint offsetToDrawPos() const;
519  void calcBoundingRect();
520 
521 public:
523  KGameCanvasText(const QString& text, const QColor& color,
524  const QFont& font, HPos hp, VPos vp,
525  KGameCanvasAbstract* canvas = nullptr);
526 
528  explicit KGameCanvasText(KGameCanvasAbstract* canvas = nullptr);
529 
530  virtual ~KGameCanvasText();
531 
533  QString text() const { return m_text; }
534 
536  void setText(const QString& text);
537 
539  QColor color() const { return m_color; }
540 
542  void setColor(const QColor& color);
543 
545  QFont font() const { return m_font; }
546 
548  void setFont(const QFont& font);
549 
551  HPos hPositioning() const { return m_hpos; }
552 
554  VPos vPositioning() const { return m_vpos; }
555 
557  void setPositioning(HPos hp, VPos vp);
558 
559  void paint(QPainter* p) override;
560  QRect rect() const override;
561  bool layered() const override { return false; }
562 };
563 
573 class KDEGAMESPRIVATE_EXPORT KGameCanvasWidget : public QWidget, public KGameCanvasAbstract
574 {
575 Q_OBJECT
576 private:
577  friend class KGameCanvasItem;
578  friend class AnimationNotifier;
579 
580  class KGameCanvasWidgetPrivate *priv;
581 
582  void ensureAnimating() override;
583  void ensurePendingUpdate() override;
584  void invalidate(const QRect& r, bool translate = true) override;
585  void invalidate(const QRegion& r, bool translate = true) override;
586 
587  void paintEvent(QPaintEvent *event) override;
588 
589 private Q_SLOTS:
590  void processAnimations();
591  void updateChanges();
592 
593 public:
595  explicit KGameCanvasWidget(QWidget* parent = nullptr);
596 
597  virtual ~KGameCanvasWidget();
598 
600  void setAnimationDelay(int d);
601 
604  int mSecs();
605 
607  KGameCanvasWidget* topLevelCanvas() override;
608 
610  QPoint canvasPosition() const override;
611 };
612 
636 class KDEGAMESPRIVATE_EXPORT KGameCanvasAdapter : public KGameCanvasAbstract
637 {
638  QRect m_child_rect;
639  bool m_child_rect_valid;
640  QRect m_invalidated_rect;
641 
642  void ensureAnimating() override { }
643  void ensurePendingUpdate() override;
644  void invalidate(const QRect& r, bool translate = true) override;
645  void invalidate(const QRegion& r, bool translate = true) override;
646 
647  QRect childRect();
648 public:
651 
656  class KGameCanvasWidget* topLevelCanvas() override { return nullptr; }
657 
667  QPoint canvasPosition() const override { return QPoint(0, 0); }
668 
673  virtual void render(QPainter* p);
674 
681  virtual void updateParent(const QRect& rect) = 0;
682 };
683 
684 #endif //__KGRGAMECANVAS_H__
bool layered() const override
Override this function to specify if the painting operations will paint over each other...
Definition: kgamecanvas.h:438
QFont font() const
Returns the font.
Definition: kgamecanvas.h:545
QPicture picture() const
Returns the picture.
Definition: kgamecanvas.h:329
An abstract item.
Definition: kgamecanvas.h:114
VPos
Specifies the meaning of the y coordinate of the item.
Definition: kgamecanvas.h:503
Container widget.
Definition: kgamecanvas.h:573
Container class.
Definition: kgamecanvas.h:55
A sprite pixmap from KGameRenderer.
Definition: kgamecanvas.h:381
virtual void ensurePendingUpdate()=0
Virtual function to ensure an update is pending, called from children.
VPos vPositioning() const
Returns the vertical positioning style.
Definition: kgamecanvas.h:554
QColor color() const
Returns the color.
Definition: kgamecanvas.h:539
QPixmap pixmap() const
Returns the pixmap.
Definition: kgamecanvas.h:416
bool layered() const override
Override this function to specify if the painting operations will paint over each other...
Definition: kgamecanvas.h:561
virtual void paint(QPainter *p)=0
Override this function to draw the item with the painter.
virtual QPoint canvasPosition() const =0
KGameCanvasItem * itemAt(int x, int y) const
Overload, same as above.
Definition: kgamecanvas.h:76
KGameCanvasAbstract * canvas() const
Returns the canvas that is actually "owning" the item.
Definition: kgamecanvas.h:192
HPos hPositioning() const
Returns the horizontal positioning style.
Definition: kgamecanvas.h:551
virtual void paintEvent(QPaintEvent *event)
void moveTo(int x, int y)
Overload, same as above.
Definition: kgamecanvas.h:213
virtual void receivePixmap(const QPixmap &pixmap)=0
This method is called when the KGameRenderer has provided a new pixmap for this client (esp...
A generic adapter for KGameCanvas.
Definition: kgamecanvas.h:636
bool layered() const override
Override this function to specify if the painting operations will paint over each other...
Definition: kgamecanvas.h:368
Cache-enabled rendering of SVG themes.
Definition: kgamerenderer.h:94
QString text() const
Returns the text.
Definition: kgamecanvas.h:533
virtual void changed()
schedule an update if the item
KGameCanvasWidget * topLevelCanvas() override
returns &#39;this&#39;
An item containing other items.
Definition: kgamecanvas.h:269
class KGameCanvasWidget * topLevelCanvas() override
An adapter is not associated to any canvas, so this function simply returns 0.
Definition: kgamecanvas.h:656
virtual QRect rect() const =0
Override this function to return the rect the item will be drawn into.
KGameCanvasText.
Definition: kgamecanvas.h:487
QRect rect() const const
A pixmap (sprite).
Definition: kgamecanvas.h:346
virtual void invalidate(const QRect &r, bool translate=true)=0
Virtual function to update a rect.
A solid rectangle.
Definition: kgamecanvas.h:450
QColor color() const
Returns the color.
Definition: kgamecanvas.h:466
An object that receives pixmaps from a KGameRenderer.
A dummy (empty) item.
Definition: kgamecanvas.h:244
QPoint canvasPosition() const override
The absolute position of the rendered content is not well defined for KGameCanvasAdapter.
Definition: kgamecanvas.h:667
QPixmap pixmap() const
Returns the pixmap.
Definition: kgamecanvas.h:361
void show()
Shows the item.
Definition: kgamecanvas.h:177
virtual void advance(int msecs)
Override this function to handle animations, the default function does nothing.
QPoint pos() const
Returns the position of the item.
Definition: kgamecanvas.h:202
bool animated() const
Returns true if the item is animated.
Definition: kgamecanvas.h:162
int opacity() const
Returns the opacity of the item.
Definition: kgamecanvas.h:168
HPos
Specifies the meaning of the x coordinate of the item.
Definition: kgamecanvas.h:493
void hide()
Hides the item.
Definition: kgamecanvas.h:174
bool visible() const
Returns true if the item is visible.
Definition: kgamecanvas.h:156
KGameCanvasWidget * topLevelCanvas() const
Returns the toplevel canvas widget, or NULL.
Definition: kgamecanvas.h:195
virtual void ensureAnimating()=0
Virtual function to let know the canvas that it has animated items in it.
bool layered() const override
Override this function to specify if the painting operations will paint over each other...
Definition: kgamecanvas.h:476
const QList< KGameCanvasItem * > * items() const
Returns a const pointer to the list holding all the items in the canvas.
Definition: kgamecanvas.h:70
A tiled pixmap (brush).
Definition: kgamecanvas.h:397
QList< KGameCanvasItem * > itemsAt(int x, int y) const
Overload, same as above.
Definition: kgamecanvas.h:83
bool moveOrigin()
If the origin is moved.
Definition: kgamecanvas.h:431
A picture, ie a collection of paint operations.
Definition: kgamecanvas.h:314
virtual class KGameCanvasWidget * topLevelCanvas()=0
Returns the toplevel non-group KGameCanvasWidget object.
void render(QPaintDevice *target, const QPoint &targetOffset, const QRegion &sourceRegion, QWidget::RenderFlags renderFlags)
QPoint origin() const
The origin.
Definition: kgamecanvas.h:425
QPoint canvasPosition() const override
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.