Drawing.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #ifndef Drawable_H
  2. #define Drawable_H
  3. #include <functional>
  4. #include "Critical.h"
  5. #include "KeyboardEvent.h"
  6. #include "MouseEvent.h"
  7. #include "Point.h"
  8. #include "ReferenceCounter.h"
  9. namespace Framework
  10. {
  11. struct MouseEvent; //! MouseEvent.h
  12. struct KeyboardEvent; //! KeyboardEvent.h
  13. class Image; //! Image.h
  14. class Drawable; //! From this file
  15. class ToolTip; //! ToolTip.h
  16. class Screen; //! Screen.h
  17. class Border; //! Border.h
  18. class AlphaField; //! AlphaField.h
  19. class VScrollBar; //! Scroll.h
  20. class HScrollBar; //! Scroll.h
  21. class Font;
  22. //! A drawing element for the 2D GUI Framework
  23. class Drawable : public virtual ReferenceCounter
  24. {
  25. public:
  26. class Style
  27. {
  28. public:
  29. //! If this style is set, the drawing is displayed when rendered
  30. static const __int64 Visible = 0x00001;
  31. //! If this style is set, the user can interact with the drawing
  32. static const __int64 Allowed = 0x00002;
  33. //! If this style is set, keyboard events are processed by the
  34. //! drawing
  35. static const __int64 Focus = 0x00004;
  36. //! If this style is set, mouse events are processed even when
  37. //! the object is not visible
  38. static const __int64 MEIgnoreVisible = 0x0800000000000000;
  39. //! If this style is set, mouse events are processed even when
  40. //! they are outside the drawing
  41. static const __int64 MEIgnoreInside = 0x2000000000000000;
  42. //! If this style is set, mouse events are processed even when
  43. //! they have already been processed by another drawing
  44. static const __int64 MEIgnoreProcessed = 0x4000000000000000;
  45. //! If this style is set, mouse events are processed even when
  46. //! they are outside the drawing to which this drawing belongs
  47. static const __int64 MEIgnoreParentInside = 0x8000000000000000;
  48. };
  49. protected:
  50. Point pos;
  51. Point gr;
  52. void* makParam;
  53. void* takParam;
  54. MouseAction mak;
  55. KeyboardAction tak;
  56. void* nmakParam;
  57. void* ntakParam;
  58. MouseAction nMak;
  59. KeyboardAction nTak;
  60. bool mouseIn;
  61. ReadWriteLock rwLock;
  62. ToolTip* toolTip;
  63. __int64 style;
  64. bool rend;
  65. std::function<bool(Drawable*, Point localPos)> onNeedToolTip;
  66. bool toolTipRequested;
  67. //! Processes a mouse event. Called automatically by the framework.
  68. //! \param me The mouse event \param userRet true if
  69. //! MouseEvent::processed should be set to true
  70. DLLEXPORT virtual void doMouseEvent(MouseEvent& me, bool userRet);
  71. public:
  72. //! Constructor
  73. DLLEXPORT Drawable();
  74. //! Destructor
  75. DLLEXPORT virtual ~Drawable();
  76. //! Specifies whether the drawing has changed since the last frame
  77. //! and needs to be redrawn
  78. DLLEXPORT void setRender();
  79. //! Sets the text that appears when the user hovers the mouse over
  80. //! the drawing for a longer time \param txt The text to display
  81. //! \param zScreen A pointer to the screen object without increased
  82. //! reference counter \param zFont A pointer to the font to use
  83. //! without increased reference counter
  84. DLLEXPORT void setToolTipText(
  85. const char* txt, Screen* zScreen, Font* zFont);
  86. //! Sets a function called when a tooltip is needed and none has
  87. //! been set yet \param initToolTip The function
  88. DLLEXPORT void setNeedToolTipEvent(
  89. std::function<bool(Drawable*, Point localPos)> onNeedTooltip);
  90. //! Sets the tooltip
  91. //! \param tt The tooltip
  92. DLLEXPORT void setToolTipZ(ToolTip* tt);
  93. //! returns a read lock for the drawing.
  94. DLLEXPORT Lock& readLock();
  95. //! returns a write lock for the drawing.
  96. DLLEXPORT Lock& writeLock();
  97. //! Sets the parameter passed to the callback function on a mouse event
  98. //! \param p The parameter
  99. DLLEXPORT void setMouseEventParameter(void* p);
  100. //! Sets the parameter passed to the callback function on a keyboard
  101. //! event
  102. //! \param p The parameter
  103. DLLEXPORT void setKeyboardEventParameter(void* p);
  104. //! Sets the callback function to be called on a mouse event.
  105. //! If the callback returns 0, or was not set, the mouse event
  106. //! is not further processed by the drawing. The standard function
  107. //! __ret1ME can be used, which is defined in MouseEvent.h and always
  108. //! returns 1 \param ak A pointer to the callback function
  109. DLLEXPORT void setMouseEvent(MouseAction ak);
  110. //! Adds a callback function to be called on a mouse event.
  111. //! If any callback returns 0, or none was set, the mouse event
  112. //! is not further processed by the drawing. The standard function
  113. //! __ret1ME can be used, which is defined in MouseEvent.h and always
  114. //! returns 1 \param ak A pointer to the callback function
  115. DLLEXPORT void addMouseEvent(MouseAction ak);
  116. //! Sets the callback function to be called on a keyboard event.
  117. //! If the callback returns 0, or was not set, the keyboard event
  118. //! is not further processed by the drawing. The standard function
  119. //! __ret1TE can be used, which is defined in KeyboardEvent.h and
  120. //! always returns 1. Other standard functions are _onlyNumbersTE and
  121. //! _onlyHexTE also from KeyboardEvent.h \param ak A pointer to
  122. //! the callback function
  123. DLLEXPORT void setKeyboardEvent(KeyboardAction ak);
  124. //! Adds a callback function to be called on a keyboard event.
  125. //! If any callback returns 0, or none was set, the keyboard event
  126. //! is not further processed by the drawing. The standard function
  127. //! __ret1TE can be used, which is defined in KeyboardEvent.h and
  128. //! always returns 1. Other standard functions are _onlyNumbersTE and
  129. //! _onlyHexTE also from KeyboardEvent.h \param ak A pointer to
  130. //! the callback function
  131. DLLEXPORT void addKeyboardEvent(KeyboardAction ak);
  132. //! Sets the parameter passed to the callback function on a mouse event
  133. //! that is called after the event was processed by the drawing
  134. //! \param p The parameter
  135. DLLEXPORT void setPostMouseEventParameter(void* p);
  136. //! Sets the parameter passed to the callback function on a keyboard
  137. //! event that is called after the event was processed by the drawing
  138. //! \param p The parameter
  139. DLLEXPORT void setPostKeyboardEventParameter(void* p);
  140. //! Sets the callback function to be called on a mouse event after
  141. //! the event has already been processed by the drawing. If the callback
  142. //! returns 1, or was not set, the mouse event will not be processed
  143. //! by any other drawing, such as ones behind this drawing. The standard
  144. //! function __ret1ME can be used, which is defined in MouseEvent.h
  145. //! and always returns 1 \param ak A pointer to the callback function
  146. DLLEXPORT void setPostMouseEvent(MouseAction ak);
  147. //! Sets the callback function to be called on a keyboard event after
  148. //! the event has already been processed by the drawing. If the callback
  149. //! returns 1, or was not set, the keyboard event will not be processed
  150. //! by any other drawing. The standard function __ret1TE can be used,
  151. //! which is defined in KeyboardEvent.h and always returns 1. Other
  152. //! standard functions are _onlyNumbersTE and _onlyHexTE also from
  153. //! KeyboardEvent.h \param ak A pointer to the callback function
  154. DLLEXPORT void setPostKeyboardEvent(KeyboardAction ak);
  155. //! Processes a mouse event. Called automatically by the framework.
  156. //! \param me The event
  157. DLLEXPORT virtual void doPublicMouseEvent(MouseEvent& me);
  158. //! Processes a keyboard event. Called automatically by the framework
  159. //! \param te The event
  160. DLLEXPORT virtual void doKeyboardEvent(KeyboardEvent& te);
  161. //! Processes the time elapsed since the last call of this function
  162. //! \param tickVal The elapsed time in seconds
  163. DLLEXPORT virtual bool tick(double tickval);
  164. //! Sets the position of the drawing
  165. //! \param pos The position in pixels
  166. DLLEXPORT void setPosition(const Point& pos);
  167. //! Sets the X position of the drawing
  168. //! \param xPos The position in pixels
  169. DLLEXPORT void setX(int xPos);
  170. //! Sets the Y position of the drawing
  171. //! \param yPos The position in pixels
  172. DLLEXPORT void setY(int yPos);
  173. //! Sets the size of the drawing
  174. //! \param gr A point with x as width and y as height in pixels
  175. DLLEXPORT void setSize(const Point& gr);
  176. //! Sets the position of the drawing
  177. //! \param x The X position in pixels
  178. //! \param y The Y position in pixels
  179. DLLEXPORT void setPosition(int x, int y);
  180. //! Sets the size of the drawing
  181. //! \param br The width in pixels
  182. //! \param height The height in pixels
  183. DLLEXPORT void setSize(int br, int height);
  184. //! Set the width in pixel
  185. //! \param width the width
  186. DLLEXPORT void setWidth(int width);
  187. //! Set the height in pixel
  188. //! \param width the height
  189. DLLEXPORT void setHeight(int height);
  190. //! Sets the style of the drawing
  191. //! \param style The new style consisting of flags from the
  192. //! corresponding Style class
  193. DLLEXPORT void setStyle(__int64 style);
  194. //! Sets the style of the drawing
  195. //! \param style All style flags to be modified
  196. //! \param add_remove 1 if the style should be added. 0 if the style
  197. //! should be removed
  198. DLLEXPORT void setStyle(__int64 style, bool add_remove);
  199. //! Adds style flags
  200. //! \param style The style to add
  201. DLLEXPORT void addStyle(__int64 style);
  202. //! Removes style flags
  203. //! \param style The style to remove
  204. DLLEXPORT void removeStyle(__int64 style);
  205. //! Renders the drawing into a specific image
  206. //! \param zRObj The image to draw into
  207. DLLEXPORT virtual void render(Image& zRObj);
  208. //! Returns whether a callback function for mouse events was set
  209. DLLEXPORT bool hasMouseEvent() const;
  210. //! Returns whether a callback function for keyboard events was set
  211. DLLEXPORT bool hasKeyboardEvent() const;
  212. //! Returns the position of the drawing in pixels
  213. DLLEXPORT const Point& getPosition() const;
  214. //! Returns the size of the drawing in pixels. x for width and y
  215. //! for height
  216. DLLEXPORT const Point& getSize() const;
  217. //! Returns the width of the drawing in pixels
  218. DLLEXPORT int getWidth() const;
  219. //! Returns the height of the drawing in pixels
  220. DLLEXPORT int getHeight() const;
  221. //! Returns the inner width of the drawing in pixels
  222. DLLEXPORT virtual int getInnerWidth() const;
  223. //! Returns the inner height of the drawing in pixels
  224. DLLEXPORT virtual int getInnerHeight() const;
  225. //! Returns the X position of the drawing in pixels
  226. DLLEXPORT int getX() const;
  227. //! Returns the Y position of the drawing in pixels
  228. DLLEXPORT int getY() const;
  229. //! Checks whether a point is inside this object
  230. //! \param p The point
  231. //! \return 1 if the point is inside, 0 otherwise
  232. DLLEXPORT virtual bool isPointInside(Point p) const;
  233. //! Checks whether a point is inside this object
  234. //! \param x The x coordinate of the point
  235. //! \param y The y coordinate of the point
  236. //! \return 1 if the point is inside, 0 otherwise
  237. DLLEXPORT virtual bool isPointInside(int x, int y) const;
  238. //! Returns a pointer to the tooltip object, if it is used
  239. DLLEXPORT ToolTip* getToolTip() const;
  240. //! Returns a pointer to the tooltip object without increased
  241. //! reference counter, if it is used
  242. DLLEXPORT ToolTip* zToolTip() const;
  243. //! Returns whether specific styles are set
  244. //! \param style The styles to check
  245. //! \return 1 if all styles in style are set
  246. DLLEXPORT bool hasStyle(__int64 style) const;
  247. //! Returns whether specific styles are not set
  248. //! \param style The styles to check
  249. //! \return 1 if all styles in style are not set
  250. DLLEXPORT bool hasStyleNot(__int64 style) const;
  251. //! returns all currently active styles
  252. DLLEXPORT __int64 getStyles() const;
  253. //! Copies the complete drawing so it can be modified without
  254. //! affecting the original
  255. DLLEXPORT virtual Drawable* duplicate() const;
  256. };
  257. //! A drawing element for the 2D GUI Framework with background, border
  258. //! and scroll bars
  259. class DrawableBackground : public Drawable
  260. {
  261. public:
  262. class Style : public Drawable::Style
  263. {
  264. public:
  265. //! If this flag is set, the drawing gets a border
  266. static const __int64 Border = 0x00010;
  267. //! If this flag is set, the drawing gets a background
  268. static const __int64 Background = 0x00020;
  269. //! If this flag is set, the background is transparent.
  270. //! Requires flag Background
  271. static const __int64 BAlpha = 0x00040;
  272. //! If this flag is set, an image is used as background.
  273. //! Requires flag Background
  274. static const __int64 BImage = 0x00080;
  275. //! If this flag is set, a color gradient appears as border
  276. static const __int64 Buffered = 0x00100;
  277. //! If this flag is set, a scrollbar appears at the right edge
  278. static const __int64 VScroll = 0x00200;
  279. //! If this flag is set, a scrollbar appears at the bottom edge
  280. static const __int64 HScroll = 0x00400;
  281. //! If this flag is set, the image is scaled
  282. static const __int64 BImageScale = 0x00800;
  283. };
  284. protected:
  285. int backgroundColor;
  286. Border* border;
  287. Image* backgroundImage;
  288. AlphaField* backgroundFeld;
  289. VScrollBar* vertikalScrollBar;
  290. HScrollBar* horizontalScrollBar;
  291. Point innenPosition;
  292. Point innenSize;
  293. protected:
  294. //! Processes mouse messages
  295. //! \param me The event triggered by the mouse input
  296. DLLEXPORT void doMouseEvent(MouseEvent& me, bool userRet) override;
  297. public:
  298. //! Constructor
  299. DLLEXPORT DrawableBackground();
  300. //! Destructor
  301. DLLEXPORT virtual ~DrawableBackground();
  302. //! Sets the background image (requires drawing flag:
  303. //! Style::Background, HImage) \param bild The image is copied and used
  304. //! as background image
  305. DLLEXPORT void setBackgroundImage(Image* bild);
  306. //! Sets a pointer to the background image (requires drawing flag:
  307. //! Style::Background) \param bild The image is used without copying
  308. DLLEXPORT void setBackgroundImageZ(Image* bild);
  309. //! Sets the background color (requires drawing flag:
  310. //! Style::Background) \param fc The background color in A8R8G8B8 format
  311. DLLEXPORT void setBackgroundColor(int fc);
  312. //! Sets a pointer to the AlphaField (requires drawing flag:
  313. //! Style::Buffered) \param buff The AlphaField to draw over the
  314. //! background
  315. DLLEXPORT void setAlphaFieldZ(AlphaField* buff);
  316. //! Sets the strength of the AlphaField (requires drawing flag:
  317. //! Style::Buffered) \param st The strength of the AlphaField drawn
  318. //! over the background
  319. DLLEXPORT void setAlphaFieldStrength(int st);
  320. //! Sets the color of the AlphaField (requires drawing flag:
  321. //! Style::Buffered) \param fc The color of the AlphaField drawn
  322. //! over the background
  323. DLLEXPORT void setAlphaFieldColor(int fc);
  324. //! Sets a pointer to the line border drawn around the text field
  325. //! (requires drawing flag: Style::Border)
  326. //! \param ram The border
  327. DLLEXPORT void setBorderZ(Border* ram);
  328. //! Sets the width of the line border (requires drawing flag:
  329. //! Style::Border) \param br The width in pixels
  330. DLLEXPORT void setBorderWidth(int br);
  331. //! Sets the color of the line border (requires drawing flag:
  332. //! Style::Border) \param fc The color in A8R8G8B8 format
  333. DLLEXPORT void setBorderColor(int fc);
  334. //! Sets the scroll speed of the vertical scroll bar (requires drawing
  335. //! flag: Style::VScroll) \param ks The scroll speed in pixels per mouse
  336. //! click
  337. DLLEXPORT void setVerticalClickScroll(int ks);
  338. //! Scrolls to a specific position on the vertical scroll bar
  339. //! (requires drawing flag: Style::VScroll) \param pos The scroll offset
  340. //! in pixels
  341. DLLEXPORT void setVerticalScrollPos(int pos);
  342. //! Sets the color of the vertical scroll bar (requires drawing flag:
  343. //! Style::VScroll) \param f The foreground color in A8R8G8B8 format
  344. //! \param bgF The background color in A8R8G8B8 format
  345. DLLEXPORT void setVerticalScrollColor(int f, int bgF);
  346. //! Sets the scroll speed of the horizontal scroll bar (requires drawing
  347. //! flag: Style::HScroll) \param ks The scroll speed in pixels per mouse
  348. //! click
  349. DLLEXPORT void setHorizontalClickScroll(int ks);
  350. //! Scrolls to a specific position on the horizontal scroll bar
  351. //! (requires drawing flag: Style::HScroll) \param pos The scroll offset
  352. //! in pixels
  353. DLLEXPORT void setHorizontalScrollPos(int pos);
  354. //! Sets the color of the horizontal scroll bar (requires drawing flag:
  355. //! Style::HScroll) \param f The foreground color in A8R8G8B8 format
  356. //! \param bgF The background color in A8R8G8B8 format
  357. DLLEXPORT void setHorizontalScrollColor(int f, int bgF);
  358. //! Updates the drawing background
  359. //! \param tickVal The elapsed time in seconds since the last call
  360. //! of this function \return 1 if the image needs to be redrawn. 0
  361. //! otherwise
  362. DLLEXPORT bool tick(double tickVal) override;
  363. //! Renders the background of a drawing to rObj
  364. DLLEXPORT void render(Image& rObj) override;
  365. //! Returns the inner width of the drawing in pixels
  366. DLLEXPORT virtual int getInnerWidth() const override;
  367. //! Returns the inner height of the drawing in pixels
  368. DLLEXPORT virtual int getInnerHeight() const override;
  369. //! Returns the background image.
  370. //! \return 0 if no background image is used
  371. DLLEXPORT Image* getBackgroundImage() const;
  372. //! Returns the background image without increased reference counter.
  373. //! \return 0 if no background image is used
  374. DLLEXPORT Image* zBackgroundImage() const;
  375. //! Returns the background color in A8R8G8B8 format
  376. DLLEXPORT int getBackgroundColor() const;
  377. //! Returns the AlphaField drawn over the background.
  378. //! \return 0 if the AlphaField was not defined
  379. DLLEXPORT AlphaField* getAlphaField() const;
  380. //! Returns the AlphaField without increased reference counter drawn
  381. //! over the background. \return 0 if the AlphaField was not defined
  382. DLLEXPORT AlphaField* zAlphaField() const;
  383. //! Returns the strength of the AlphaField
  384. DLLEXPORT int getAlphaFieldStrength() const;
  385. //! Returns the color of the AlphaField in A8R8G8B8 format
  386. DLLEXPORT int getAlphaFieldColor() const;
  387. //! Returns the border
  388. //! \return 0 if no border was defined
  389. DLLEXPORT Border* getBorder() const;
  390. //! Returns the border without increased reference counter
  391. //! \return 0 if no border was defined
  392. DLLEXPORT Border* zBorder() const;
  393. //! Returns the width of the border in pixels
  394. DLLEXPORT int getBorderWidth() const;
  395. //! Returns the color of the border in A8R8G8B8 format
  396. DLLEXPORT int getBorderColor() const;
  397. //! Returns the scroll speed of the vertical scroll bar
  398. DLLEXPORT int getVerticalClickScroll() const;
  399. //! Returns the scroll position of the vertical scroll bar
  400. DLLEXPORT int getVerticalScrollPos() const;
  401. //! Returns the color of the vertical scroll bar in A8R8G8B8 format
  402. DLLEXPORT int getVerticalScrollColor() const;
  403. //! Returns the background color of the vertical scroll bar in A8R8G8B8
  404. //! format
  405. DLLEXPORT int getVerticalScrollBackground() const;
  406. //! Returns the scroll speed of the horizontal scroll bar
  407. DLLEXPORT int getHorizontalClickScroll() const;
  408. //! Returns the scroll position of the horizontal scroll bar
  409. DLLEXPORT int getHorizontalScrollPos() const;
  410. //! Returns the color of the horizontal scroll bar in A8R8G8B8 format
  411. DLLEXPORT int getHorizontalScrollColor() const;
  412. //! Returns the background color of the horizontal scroll bar in
  413. //! A8R8G8B8 format
  414. DLLEXPORT int getHorizontalScrollBackground() const;
  415. //! Creates a copy of the drawing that can be modified without
  416. //! affecting the original
  417. DLLEXPORT virtual Drawable* duplicate() const;
  418. };
  419. } // namespace Framework
  420. #endif