KDirStat – a graphical disk usage utility
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
5.8KB

  1. /*
  2. * File name: kpacman.h
  3. * Summary: PacMan animation inside widgets
  4. * License: LGPL - See file COPYING.LIB for details.
  5. * Author: Stefan Hundhammer <sh@suse.de>
  6. *
  7. * Updated: 2004-03-29
  8. */
  9. #ifndef KPacMan_h
  10. #define KPacMan_h
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <tqwidget.h>
  15. #include <tqpainter.h>
  16. #include <tqcolor.h>
  17. #include <tqdatetime.h>
  18. #ifndef NOT_USED
  19. # define NOT_USED(PARAM) ( (void) (PARAM) )
  20. #endif
  21. class TQTimer;
  22. /**
  23. * Helper class to display a PacMan animation inside a widget.
  24. * Note that this is not a widget itself, it needs to be placed inside a widget
  25. * - which fact makes it suitable for use inside non-widget objects such as
  26. * @ref TQListViewItem.
  27. *
  28. * If you are looking for a widget that can do all that self-contained, see
  29. * @ref KPacMan.
  30. *
  31. * @short PacMan animation
  32. **/
  33. class KPacManAnimation
  34. {
  35. public:
  36. /**
  37. * Constructor.
  38. *
  39. * Create a PacMan sprite in 'widget' of 'size' pixels diameter. Start at
  40. * a random position and move in random direction if 'randomStart' is true.
  41. **/
  42. KPacManAnimation( TQWidget * widget,
  43. int size,
  44. bool randomStart );
  45. /**
  46. * Destructor.
  47. **/
  48. virtual ~KPacManAnimation();
  49. /**
  50. * Animate PacMan inside this rectangle.
  51. * Call this frequently enough (e.g. by a timer) to get fluid motion.
  52. * Set up the painter prior to calling this; the entire rectangle will be
  53. * cleared with the current brush, and PacMan's outline will be drawn with
  54. * the current pen.
  55. *
  56. * PacMan moves from the left side of this rectangle to the right, turning
  57. * around when it (he?) reaches the right edge. It (he?) is centered
  58. * vertically.
  59. *
  60. * My, what is the sex of that thing? ;-)
  61. **/
  62. void animate( TQPainter * painter,
  63. TQRect rect );
  64. /**
  65. * Restart - reset to initial position and direction.
  66. **/
  67. void restart();
  68. /**
  69. * Return the rectangle where the last PacMan was painted.
  70. **/
  71. TQRect lastPacMan() { return _pacManRect; }
  72. /**
  73. * Set the animation interval in milliseconds.
  74. **/
  75. void setInterval( int intervalMilliSec ) { _interval = intervalMilliSec; }
  76. int interval() const { return _interval; }
  77. /**
  78. * Number of pixels to move for each phase.
  79. **/
  80. int speed() const { return _speed; }
  81. void setSpeed( int speed ) { _speed = speed; }
  82. /**
  83. * Brush to draw PacMan's inside. Bright yellow by default.
  84. **/
  85. TQBrush brush() const { return _brush; }
  86. void setBrush( const TQBrush & brush ) { _brush = brush; }
  87. /**
  88. * Number of degrees PacMan's mouth opens or closes for each animation.
  89. **/
  90. int mouthOpenInc() const { return _mouthInc; }
  91. void setMouthOpenInc( int deg ) { _mouthInc = deg; }
  92. /**
  93. * Minimum angle in degrees that PacMan's mouth opens.
  94. **/
  95. int minMouthOpenAngle() const { return _minMouth; }
  96. void setMinMouthOpenAngle( int deg ) { _minMouth = deg; }
  97. /**
  98. * Maximum angle in degrees that PacMan's mouth opens.
  99. **/
  100. int maxMouthOpenAngle() const { return _maxMouth; }
  101. void setMaxMouthOpenAngle( int deg ) { _maxMouth = deg; }
  102. protected:
  103. TQWidget * _widget;
  104. TQBrush _brush;
  105. TQTime _time;
  106. TQRect _pacManRect;
  107. int _size;
  108. bool _randomStart;
  109. int _speed;
  110. int _minMouth;
  111. int _maxMouth;
  112. int _mouthInc;
  113. int _interval; // milliseconds
  114. // Current values
  115. int _pos;
  116. int _mouth;
  117. bool _justStarted;
  118. bool _goingRight;
  119. };
  120. /**
  121. * Widget that displays a PacMan animation.
  122. *
  123. * @short PacMan widget
  124. **/
  125. class KPacMan: public TQWidget
  126. {
  127. Q_OBJECT
  128. public:
  129. /**
  130. * Constructor.
  131. *
  132. * @param pacManSize size of the PacMan sprite
  133. * @param randomStart random start position and direction if true
  134. **/
  135. KPacMan( TQWidget * parent = 0,
  136. int pacManSize = 16,
  137. bool randomStart = false,
  138. const char * widgetName = 0 );
  139. /**
  140. * Destructor.
  141. **/
  142. virtual ~KPacMan();
  143. /**
  144. * Access to the internal @ref PacManAnimation to avoid duplicating all its
  145. * methods.
  146. **/
  147. KPacManAnimation * pacMan() { return _pacMan; }
  148. /**
  149. * Access to the internal @ref TQPainter to avoid duplicating all its
  150. * methods. Change this painter in order to change the visual appearance of
  151. * the PacMan sprite.
  152. **/
  153. TQPainter * painter() { return _painter; }
  154. /**
  155. * Returns the animation interval in milliseconds.
  156. **/
  157. int interval() const { return _interval; }
  158. /**
  159. * Set the animation interval in milliseconds.
  160. **/
  161. void setInterval( int intervalMilliSec );
  162. /**
  163. * Return the (left and right) margin.
  164. **/
  165. int margin() { return _margin; }
  166. /**
  167. * Set the (left and right) margin - a place PacMan never goes.
  168. **/
  169. void setMargin( int margin ) { _margin = margin; }
  170. /**
  171. * Returns the widget's preferred size.
  172. *
  173. * Reimplemented from @ref TQWidget.
  174. **/
  175. virtual TQSize sizeHint() const;
  176. public slots:
  177. /**
  178. * Start the animation.
  179. **/
  180. void start();
  181. /**
  182. * Stop the animation and clear the widget.
  183. **/
  184. void stop();
  185. /**
  186. * Do one animation. Triggered by timer.
  187. **/
  188. void animate();
  189. protected:
  190. /**
  191. * Actually do the painting.
  192. *
  193. * Reimplemented from @ref TQWidget.
  194. **/
  195. virtual void paintEvent( TQPaintEvent *ev );
  196. /**
  197. * Stop animation on mouse click.
  198. *
  199. * Reimplemented from @ref TQWidget.
  200. **/
  201. virtual void mouseReleaseEvent ( TQMouseEvent *ev );
  202. protected:
  203. KPacManAnimation * _pacMan;
  204. TQPainter * _painter;
  205. TQTimer * _timer;
  206. int _interval; // millisec
  207. bool _active;
  208. int _margin;
  209. int _pacManSize;
  210. };
  211. #endif // KPacMan_h
  212. // EOF