Drawing.h 22 KB

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