Drawing.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. #include "Drawing.h"
  2. #include "AlphaField.h"
  3. #include "Border.h"
  4. #include "Font.h"
  5. #include "Globals.h"
  6. #include "Image.h"
  7. #include "KeyboardEvent.h"
  8. #include "MouseEvent.h"
  9. #include "Screen.h"
  10. #include "Scroll.h"
  11. #include "Text.h"
  12. #include "TextField.h"
  13. #include "ToolTip.h"
  14. #include "UIInitialization.h"
  15. #ifdef WIN32
  16. # include <Windows.h>
  17. #endif
  18. using namespace Framework;
  19. // Contents of the Drawable class from Drawing.h
  20. // Constructor
  21. Drawable::Drawable()
  22. : ReferenceCounter(),
  23. pos(0, 0),
  24. gr(0, 0),
  25. makParam(0),
  26. takParam(0),
  27. mak(0),
  28. tak(0),
  29. nmakParam(0),
  30. ntakParam(0),
  31. nMak(0),
  32. nTak(0),
  33. mouseIn(0),
  34. toolTip(0),
  35. style(0),
  36. rend(0),
  37. onNeedToolTip(0),
  38. toolTipRequested(0)
  39. {}
  40. // Destructor
  41. Drawable::~Drawable()
  42. {
  43. if (toolTip) toolTip->release();
  44. }
  45. void Drawable::doMouseEvent(MouseEvent& me, bool userRet)
  46. {
  47. me.processed = userRet;
  48. }
  49. // non-constant
  50. void Drawable::setRender()
  51. {
  52. rend = 1;
  53. }
  54. void Drawable::setToolTipText(const char* txt, Screen* zScreen, Font* zFont)
  55. {
  56. if (!txt)
  57. toolTip = (ToolTip*)toolTip->release();
  58. else
  59. {
  60. if (!toolTip)
  61. {
  62. toolTip = new ToolTip(zScreen);
  63. toolTip->addStyle(DrawableBackground::Style::Background
  64. | DrawableBackground::Style::BAlpha
  65. | DrawableBackground::Style::Border
  66. | DrawableBackground::Style::Visible);
  67. toolTip->setBackgroundColor(0xA0000000);
  68. toolTip->setBorderColor(0xFFFFFFFF);
  69. toolTip->setBorderWidth(1);
  70. if (mouseIn && toolTip) toolTip->setMouseIn(1);
  71. }
  72. UIInit init = defaultUI(zFont, zScreen);
  73. TextField* t = init.createTextField(init.initParam);
  74. t->setText(txt);
  75. t->setSize(t->zTextRenderer()->getTextWidth(txt),
  76. t->zTextRenderer()->getTextHeight(txt));
  77. toolTip->addMember(t);
  78. }
  79. toolTipRequested = 0;
  80. }
  81. // sets a function that creates a tooltip on first use
  82. // if one does not exist yet
  83. // initToolTip: the function
  84. void Drawable::setNeedToolTipEvent(
  85. std::function<bool(Drawable*, Point localPos)> onNeedToolTip)
  86. {
  87. this->onNeedToolTip = onNeedToolTip;
  88. }
  89. // sets the tooltip
  90. // tt: the tooltip
  91. void Drawable::setToolTipZ(ToolTip* tt)
  92. {
  93. if (toolTip) toolTip->release();
  94. toolTip = tt;
  95. if (mouseIn && toolTip) toolTip->setMouseIn(1);
  96. toolTipRequested = 0;
  97. }
  98. Lock& Drawable::readLock()
  99. {
  100. return rwLock.getReadLock();
  101. }
  102. Lock& Drawable::writeLock()
  103. {
  104. return rwLock.getWriteLock();
  105. }
  106. void Drawable::setMouseEventParameter(void* p) // sets the mouse event parameter
  107. {
  108. makParam = p;
  109. }
  110. void Drawable::setKeyboardEventParameter(
  111. void* p) // sets the keyboard event parameter
  112. {
  113. takParam = p;
  114. }
  115. void Drawable::setMouseEvent(MouseAction ak) // sets the mouse event
  116. {
  117. mak = ak;
  118. }
  119. void Framework::Drawable::addMouseEvent(MouseAction ak)
  120. {
  121. if (!mak)
  122. {
  123. mak = ak;
  124. }
  125. else
  126. {
  127. MouseAction old = mak;
  128. mak = [old, ak](void* p, void* o, MouseEvent me) {
  129. return old(p, o, me) && ak(p, o, me);
  130. };
  131. }
  132. }
  133. void Drawable::setKeyboardEvent(KeyboardAction ak) // sets the keyboard event
  134. {
  135. tak = ak;
  136. }
  137. void Framework::Drawable::addKeyboardEvent(KeyboardAction ak)
  138. {
  139. if (!tak)
  140. {
  141. tak = ak;
  142. }
  143. else
  144. {
  145. KeyboardAction old = tak;
  146. tak = [old, ak](void* p, void* o, KeyboardEvent te) {
  147. return old(p, o, te) && ak(p, o, te);
  148. };
  149. }
  150. }
  151. void Drawable::setPostMouseEventParameter(
  152. void* p) // sets the mouse event parameter
  153. {
  154. nmakParam = p;
  155. }
  156. void Drawable::setPostKeyboardEventParameter(
  157. void* p) // sets the keyboard event parameter
  158. {
  159. ntakParam = p;
  160. }
  161. void Drawable::setPostMouseEvent(MouseAction ak) // sets the mouse event
  162. {
  163. nMak = ak;
  164. }
  165. void Drawable::setPostKeyboardEvent(
  166. KeyboardAction ak) // sets the keyboard event
  167. {
  168. nTak = ak;
  169. }
  170. void Drawable::doPublicMouseEvent(MouseEvent& me) // calls Mak
  171. {
  172. rwLock.lockRead();
  173. bool inside = isPointInside(me.mx, me.my);
  174. if (!me.insideParent || me.processed || !inside)
  175. {
  176. if (mouseIn)
  177. {
  178. mouseIn = 0;
  179. if (toolTip) toolTip->setMouseIn(0);
  180. MouseEvent me2;
  181. me2.id = ME_Leaves;
  182. me2.mx = me.mx - pos.x;
  183. me2.my = me.my - pos.y;
  184. me2.processed = 0;
  185. me2.insideParent = me.insideParent;
  186. bool userRet = mak && mak(makParam, this, me2);
  187. doMouseEvent(me2, userRet);
  188. }
  189. }
  190. if (!inside && me.id == ME_PLeft) removeStyle(Style::Focus);
  191. if ((me.processed && hasStyleNot(Style::MEIgnoreProcessed))
  192. || (!inside && hasStyleNot(Style::MEIgnoreInside))
  193. || (hasStyleNot(Style::Visible) && hasStyleNot(Style::MEIgnoreVisible)))
  194. {
  195. rwLock.unlockRead();
  196. return;
  197. }
  198. if (inside && me.id == ME_PLeft) addStyle(Style::Focus);
  199. if (me.insideParent && !mouseIn && me.id != ME_Leaves && inside
  200. && !me.processed && hasStyle(Style::Visible))
  201. {
  202. mouseIn = 1;
  203. if (toolTip)
  204. toolTip->setMouseIn(1);
  205. else if (onNeedToolTip && !toolTipRequested)
  206. {
  207. toolTipRequested
  208. = onNeedToolTip(this, Point(me.mx - pos.x, me.my - pos.y));
  209. }
  210. MouseEvent me2;
  211. me2.id = ME_Enter;
  212. me2.mx = me.mx - pos.x;
  213. me2.my = me.my - pos.y;
  214. me2.processed = 0;
  215. me2.insideParent = 1;
  216. bool userRet = mak && mak(makParam, this, me2);
  217. doMouseEvent(me2, userRet);
  218. }
  219. else if (me.insideParent && mouseIn && me.id != ME_Leaves && inside
  220. && !me.processed && hasStyle(Style::Visible) && !toolTip
  221. && onNeedToolTip && !toolTipRequested)
  222. {
  223. toolTipRequested
  224. = onNeedToolTip(this, Point(me.mx - pos.x, me.my - pos.y));
  225. }
  226. Point old = Point(me.mx, me.my);
  227. me.mx -= pos.x, me.my -= pos.y;
  228. if (me.insideParent || hasStyle(Style::MEIgnoreParentInside))
  229. {
  230. bool userRet = hasStyle(Style::Allowed)
  231. && (me.processed || !me.insideParent
  232. || (inside && mak && mak(makParam, this, me)));
  233. bool vera = me.processed;
  234. doMouseEvent(me, userRet);
  235. if (nMak && me.processed && !vera && hasStyle(Style::Allowed)
  236. && me.insideParent && inside)
  237. me.processed = nMak(nmakParam, this, me);
  238. }
  239. me.mx = old.x, me.my = old.y;
  240. rwLock.unlockRead();
  241. }
  242. void Drawable::doKeyboardEvent(KeyboardEvent& te) // calls Tak
  243. {
  244. if (te.processed) return;
  245. if (tak) te.processed |= tak(takParam, this, te);
  246. if (nTak && te.processed) te.processed = nTak(ntakParam, this, te);
  247. }
  248. void Drawable::setPosition(const Point& pos) // sets the position
  249. {
  250. rwLock.lockWrite();
  251. if (this->pos != pos) rend = 1;
  252. this->pos = pos;
  253. rwLock.unlockWrite();
  254. }
  255. void Drawable::setX(int xPos)
  256. {
  257. rwLock.lockWrite();
  258. if (pos.x != xPos)
  259. {
  260. rend = 1;
  261. pos.x = xPos;
  262. }
  263. rwLock.unlockWrite();
  264. }
  265. void Drawable::setY(int yPos)
  266. {
  267. rwLock.lockWrite();
  268. if (pos.y != yPos)
  269. {
  270. rend = 1;
  271. pos.y = yPos;
  272. }
  273. rwLock.unlockWrite();
  274. }
  275. void Drawable::setSize(const Point& gr) // sets the size
  276. {
  277. rwLock.lockWrite();
  278. if (this->gr != gr) rend = 1;
  279. this->gr = gr;
  280. rwLock.unlockWrite();
  281. }
  282. void Drawable::setPosition(int x, int y) // sets the position
  283. {
  284. setPosition(Point(x, y));
  285. }
  286. void Drawable::setSize(int x, int y) // sets the size
  287. {
  288. setSize(Point(x, y));
  289. }
  290. void Drawable::setWidth(int width)
  291. {
  292. rwLock.lockWrite();
  293. if (this->gr.x != width) rend = 1;
  294. gr.x = width;
  295. rwLock.unlockWrite();
  296. }
  297. void Drawable::setHeight(int height)
  298. {
  299. rwLock.lockWrite();
  300. if (this->gr.y != height) rend = 1;
  301. gr.y = height;
  302. rwLock.unlockWrite();
  303. }
  304. bool Drawable::tick(double tickval)
  305. {
  306. bool r = rend;
  307. rend = 0;
  308. return r;
  309. }
  310. void Drawable::setStyle(__int64 style) // sets the style of the text field
  311. {
  312. if (this->style != style)
  313. {
  314. this->style = style;
  315. rend = 1;
  316. }
  317. }
  318. void Drawable::setStyle(__int64 style, bool add_remove)
  319. {
  320. if (add_remove && (this->style | style) != this->style)
  321. {
  322. this->style |= style;
  323. rend = 1;
  324. }
  325. else if (!add_remove && (this->style & ~style) != this->style)
  326. {
  327. if (toolTip && (style | Style::Visible) == style)
  328. toolTip->setMouseIn(0);
  329. this->style &= ~style;
  330. rend = 1;
  331. }
  332. }
  333. void Drawable::addStyle(__int64 style)
  334. {
  335. if ((this->style | style) != this->style)
  336. {
  337. this->style |= style;
  338. rend = 1;
  339. }
  340. }
  341. void Drawable::removeStyle(__int64 style)
  342. {
  343. if ((this->style & ~style) != this->style)
  344. {
  345. if (toolTip && (style | Style::Visible) == style)
  346. toolTip->setMouseIn(0);
  347. this->style &= ~style;
  348. rend = 1;
  349. }
  350. }
  351. void Drawable::render(Image& zRObj)
  352. {
  353. if (toolTip && (style | Style::Visible) == style) toolTip->setDrawing();
  354. }
  355. // constant
  356. bool Drawable::hasMouseEvent() const // checks if Mak is set
  357. {
  358. return mak != 0;
  359. }
  360. bool Drawable::hasKeyboardEvent() const // checks if Tak is set
  361. {
  362. return tak != 0;
  363. }
  364. const Point& Drawable::getPosition() const // returns the position
  365. {
  366. return pos;
  367. }
  368. const Point& Drawable::getSize() const // returns the size
  369. {
  370. return gr;
  371. }
  372. int Drawable::getWidth() const // returns the width
  373. {
  374. return gr.x;
  375. }
  376. int Drawable::getHeight() const // returns the height
  377. {
  378. return gr.y;
  379. }
  380. // Returns the width of the interior area of the drawing in pixels
  381. int Drawable::getInnerWidth() const
  382. {
  383. return gr.x;
  384. }
  385. // Returns the height of the interior area of the drawing in pixels
  386. int Drawable::getInnerHeight() const
  387. {
  388. return gr.y;
  389. }
  390. int Drawable::getX() const // returns X
  391. {
  392. return pos.x;
  393. }
  394. int Drawable::getY() const // returns Y
  395. {
  396. return pos.y;
  397. }
  398. // Checks if a point is inside this object
  399. // p: the point
  400. // return: 1 if the point is inside, 0 otherwise
  401. bool Drawable::isPointInside(Point p) const
  402. {
  403. return isPointInside(p.x, p.y);
  404. }
  405. // Checks if a point is inside this object
  406. // x: the x coordinate of the point
  407. // y: the y coordinate of the point
  408. // return: 1 if the point is inside, 0 otherwise
  409. bool Drawable::isPointInside(int x, int y) const
  410. {
  411. return x >= pos.x && x <= pos.x + gr.x && y >= pos.y && y <= pos.y + gr.y;
  412. }
  413. ToolTip* Drawable::getToolTip() const // returns the ToolTip text
  414. {
  415. return dynamic_cast<ToolTip*>(toolTip->getThis());
  416. }
  417. ToolTip* Drawable::zToolTip() const
  418. {
  419. return toolTip;
  420. }
  421. bool Drawable::hasStyle(__int64 style) const // checks if style is present
  422. {
  423. return (this->style | style) == this->style;
  424. }
  425. bool Drawable::hasStyleNot(
  426. __int64 style) const // checks if style is not present
  427. {
  428. return (this->style | style) != this->style;
  429. }
  430. __int64 Framework::Drawable::getStyles() const
  431. {
  432. return style;
  433. }
  434. Drawable* Drawable::duplicate() const // Creates a copy of the drawing
  435. {
  436. Drawable* obj = new Drawable();
  437. obj->setPosition(pos);
  438. obj->setSize(gr);
  439. obj->setMouseEventParameter(makParam);
  440. obj->setKeyboardEventParameter(takParam);
  441. obj->setMouseEvent(mak);
  442. obj->setKeyboardEvent(tak);
  443. if (toolTip) obj->setToolTipZ((ToolTip*)toolTip->duplicate());
  444. return obj;
  445. }
  446. // Contents of the DrawableBackground class from Drawing.h
  447. // Constructor
  448. DrawableBackground::DrawableBackground()
  449. : Drawable()
  450. {
  451. backgroundColor = 0xFF000000;
  452. border = 0;
  453. backgroundImage = 0;
  454. backgroundFeld = 0;
  455. horizontalScrollBar = 0;
  456. vertikalScrollBar = 0;
  457. innenPosition.x = 0;
  458. innenPosition.y = 0;
  459. innenSize.x = 0;
  460. innenSize.y = 0;
  461. }
  462. // Destructor
  463. DrawableBackground::~DrawableBackground()
  464. {
  465. if (border) border->release();
  466. if (backgroundImage) backgroundImage->release();
  467. if (backgroundFeld) backgroundFeld->release();
  468. if (horizontalScrollBar) horizontalScrollBar->release();
  469. if (vertikalScrollBar) vertikalScrollBar->release();
  470. }
  471. void DrawableBackground::doMouseEvent(MouseEvent& me, bool userRet)
  472. {
  473. if (userRet)
  474. {
  475. int rbr = 0;
  476. if (hasStyle(Style::Border) && border) rbr = border->getRWidth();
  477. bool vs = hasStyle(Style::VScroll) && vertikalScrollBar;
  478. bool hs = hasStyle(Style::HScroll) && horizontalScrollBar;
  479. if (vs)
  480. {
  481. if (hs)
  482. horizontalScrollBar->doMouseMessage(
  483. rbr, gr.y - rbr - 15, gr.x - rbr * 2 - 15, 15, me);
  484. vertikalScrollBar->doMouseMessage(
  485. gr.x - rbr - 15, rbr, 15, gr.y - rbr * 2, me);
  486. }
  487. else if (hs)
  488. horizontalScrollBar->doMouseMessage(
  489. rbr, gr.y - rbr - 15, gr.x - rbr * 2, 15, me);
  490. }
  491. me.processed = userRet;
  492. }
  493. void DrawableBackground::setBackgroundImage(
  494. Image* bild) // sets the background image
  495. {
  496. if (!backgroundImage) backgroundImage = new Image();
  497. backgroundImage->newImage(bild->getWidth(), bild->getHeight(), 0);
  498. int* buff1 = backgroundImage->getBuffer();
  499. int* buff2 = bild->getBuffer();
  500. for (int i = 0; i < bild->getWidth() * bild->getHeight(); ++i)
  501. buff1[i] = buff2[i];
  502. bild->release();
  503. rend = 1;
  504. }
  505. void DrawableBackground::setBackgroundImageZ(
  506. Image* bild) // sets a pointer to the background image
  507. {
  508. if (backgroundImage != bild)
  509. {
  510. if (backgroundImage) backgroundImage->release();
  511. backgroundImage = bild;
  512. rend = 1;
  513. }
  514. }
  515. void DrawableBackground::setBackgroundColor(int fc) // sets the background color
  516. {
  517. if (backgroundColor != fc)
  518. {
  519. backgroundColor = fc;
  520. rend = 1;
  521. }
  522. }
  523. void DrawableBackground::setAlphaFieldZ(
  524. AlphaField* buff) // sets a pointer to the background buffer
  525. {
  526. if (backgroundFeld != buff)
  527. {
  528. if (backgroundFeld) backgroundFeld->release();
  529. backgroundFeld = buff;
  530. rend = 1;
  531. }
  532. }
  533. void DrawableBackground::setAlphaFieldStrength(
  534. int st) // sets the strength of the background buffer
  535. {
  536. if (!backgroundFeld)
  537. {
  538. backgroundFeld = new AlphaField();
  539. rend = 1;
  540. }
  541. if (backgroundFeld->getStrength() != st)
  542. {
  543. backgroundFeld->setStrength(st);
  544. rend = 1;
  545. }
  546. }
  547. void DrawableBackground::setAlphaFieldColor(
  548. int fc) // sets the color of the background buffer
  549. {
  550. if (!backgroundFeld)
  551. {
  552. backgroundFeld = new AlphaField();
  553. rend = 1;
  554. }
  555. if (backgroundFeld->getColor() != fc)
  556. {
  557. backgroundFeld->setColor(fc);
  558. rend = 1;
  559. }
  560. }
  561. void DrawableBackground::setBorderZ(Border* ram) // sets a pointer to the border
  562. {
  563. if (border != ram)
  564. {
  565. if (border) border->release();
  566. border = ram;
  567. rend = 1;
  568. }
  569. }
  570. void DrawableBackground::setBorderWidth(int br) // sets the border width
  571. {
  572. if (!border)
  573. {
  574. border = new LBorder();
  575. rend = 1;
  576. }
  577. if (border->getRWidth() != br)
  578. {
  579. border->setBorderWidth(br);
  580. rend = 1;
  581. }
  582. }
  583. void DrawableBackground::setBorderColor(int fc) // sets the border color
  584. {
  585. if (!border)
  586. {
  587. border = new LBorder();
  588. rend = 1;
  589. }
  590. if (border->getColor() != fc)
  591. {
  592. border->setColor(fc);
  593. rend = 1;
  594. }
  595. }
  596. void DrawableBackground::setVerticalClickScroll(
  597. int ks) // sets the vertical scroll speed
  598. {
  599. if (!vertikalScrollBar)
  600. {
  601. vertikalScrollBar = new VScrollBar();
  602. rend = 1;
  603. }
  604. if (vertikalScrollBar->getClickScroll() != ks)
  605. {
  606. vertikalScrollBar->setClickScroll(ks);
  607. rend = 1;
  608. }
  609. }
  610. void DrawableBackground::setVerticalScrollPos(
  611. int pos) // sets the vertical scroll position
  612. {
  613. if (!vertikalScrollBar)
  614. {
  615. vertikalScrollBar = new VScrollBar();
  616. rend = 1;
  617. }
  618. if (vertikalScrollBar && vertikalScrollBar->getScroll() != pos)
  619. {
  620. vertikalScrollBar->scroll(pos);
  621. rend = 1;
  622. }
  623. }
  624. void DrawableBackground::setVerticalScrollColor(
  625. int f, int bgF) // sets the scroll color
  626. {
  627. if (!vertikalScrollBar)
  628. {
  629. vertikalScrollBar = new VScrollBar();
  630. rend = 1;
  631. }
  632. if (vertikalScrollBar
  633. && (vertikalScrollBar->getColor() != f
  634. || vertikalScrollBar->getBgColor() != bgF))
  635. {
  636. vertikalScrollBar->setColor(f);
  637. vertikalScrollBar->setBgColor(bgF, bgF != 0);
  638. rend = 1;
  639. }
  640. }
  641. void DrawableBackground::setHorizontalClickScroll(
  642. int ks) // sets the horizontal scroll speed
  643. {
  644. if (!horizontalScrollBar)
  645. {
  646. horizontalScrollBar = new HScrollBar();
  647. rend = 1;
  648. }
  649. if (horizontalScrollBar && horizontalScrollBar->getClickScroll() != ks)
  650. {
  651. horizontalScrollBar->setClickScroll(ks);
  652. rend = 1;
  653. }
  654. }
  655. void DrawableBackground::setHorizontalScrollPos(
  656. int pos) // sets the horizontal scroll position
  657. {
  658. if (!horizontalScrollBar)
  659. {
  660. horizontalScrollBar = new HScrollBar();
  661. rend = 1;
  662. }
  663. if (horizontalScrollBar && horizontalScrollBar->getScroll() != pos)
  664. {
  665. horizontalScrollBar->scroll(pos);
  666. rend = 1;
  667. }
  668. }
  669. void DrawableBackground::setHorizontalScrollColor(
  670. int f, int bgF) // sets the scroll color
  671. {
  672. if (!horizontalScrollBar)
  673. {
  674. horizontalScrollBar = new HScrollBar();
  675. rend = 1;
  676. }
  677. if (horizontalScrollBar
  678. && (horizontalScrollBar->getColor() != f
  679. || horizontalScrollBar->getBgColor() != bgF))
  680. {
  681. horizontalScrollBar->setColor(f);
  682. horizontalScrollBar->setBgColor(bgF, bgF != 0);
  683. rend = 1;
  684. }
  685. }
  686. bool DrawableBackground::tick(double tickVal)
  687. {
  688. if (vertikalScrollBar && hasStyle(Style::VScroll))
  689. rend |= vertikalScrollBar->getNeedRender();
  690. if (horizontalScrollBar && hasStyle(Style::HScroll))
  691. rend |= horizontalScrollBar->getNeedRender();
  692. return Drawable::tick(tickVal);
  693. }
  694. void DrawableBackground::render(Image& rObj)
  695. {
  696. innenPosition.x = pos.x;
  697. innenPosition.y = pos.y;
  698. innenSize.x = gr.x;
  699. innenSize.y = gr.y;
  700. if (hasStyleNot(Style::Visible)) return;
  701. rwLock.lockRead();
  702. if (!rObj.setDrawOptions(pos.x, pos.y, gr.x, gr.y))
  703. {
  704. rwLock.unlockRead();
  705. return;
  706. }
  707. Drawable::render(rObj);
  708. int rbr = 0;
  709. if (hasStyle(Style::Border) && border)
  710. {
  711. border->setSize(gr);
  712. border->render(rObj);
  713. rbr = border->getRWidth();
  714. }
  715. innenPosition.x += rbr;
  716. innenPosition.y += rbr;
  717. innenSize.x -= rbr * 2;
  718. innenSize.y -= rbr * 2;
  719. if (!rObj.setDrawOptions(rbr, rbr, gr.x - rbr * 2, gr.y - rbr * 2))
  720. {
  721. rObj.releaseDrawOptions();
  722. rwLock.unlockRead();
  723. return;
  724. }
  725. bool vs = vertikalScrollBar && hasStyle(Style::VScroll);
  726. bool hs = horizontalScrollBar && hasStyle(Style::HScroll);
  727. if (vs)
  728. {
  729. vertikalScrollBar->render(
  730. gr.x - rbr * 2 - 15, 0, 15, gr.y - rbr * 2, rObj);
  731. innenSize.x -= 15;
  732. if (hs)
  733. {
  734. horizontalScrollBar->render(
  735. 0, gr.y - rbr * 2 - 15, gr.x - rbr * 2 - 15, 15, rObj);
  736. innenSize.y -= 15;
  737. if (!rObj.setDrawOptions(
  738. 0, 0, gr.x - rbr * 2 - 15, gr.y - rbr * 2 - 15))
  739. {
  740. rObj.releaseDrawOptions();
  741. rObj.releaseDrawOptions();
  742. rwLock.unlockRead();
  743. return;
  744. }
  745. horizontalScrollBar->update(
  746. horizontalScrollBar->getScrollData()->max, innenSize.x);
  747. }
  748. else
  749. {
  750. if (!rObj.setDrawOptions(0, 0, gr.x - rbr * 2 - 15, gr.y - rbr * 2))
  751. {
  752. rObj.releaseDrawOptions();
  753. rObj.releaseDrawOptions();
  754. rwLock.unlockRead();
  755. return;
  756. }
  757. }
  758. vertikalScrollBar->update(
  759. vertikalScrollBar->getScrollData()->max, innenSize.y);
  760. }
  761. else if (hs)
  762. {
  763. horizontalScrollBar->render(
  764. rbr, gr.y - rbr * 2 - 15, gr.x - rbr * 2, 15, rObj);
  765. innenSize.y -= 15;
  766. if (!rObj.setDrawOptions(0, 0, gr.x - rbr * 2, gr.y - rbr * 2 - 15))
  767. {
  768. rObj.releaseDrawOptions();
  769. rObj.releaseDrawOptions();
  770. rwLock.unlockRead();
  771. return;
  772. }
  773. }
  774. if (hasStyle(Style::Background))
  775. {
  776. if (hasStyle(Style::BAlpha))
  777. rObj.alphaRegion(
  778. 0, 0, gr.x - rbr * 2, gr.y - rbr * 2, backgroundColor);
  779. else
  780. rObj.fillRegion(
  781. 0, 0, gr.x - rbr * 2, gr.y - rbr * 2, backgroundColor);
  782. if (hasStyle(Style::BImage) && backgroundImage)
  783. {
  784. if (hasStyle(Style::BImageScale))
  785. {
  786. if (hasStyle(Style::BAlpha))
  787. rObj.alphaImageScaled(
  788. 0, 0, gr.x - rbr * 2, gr.y - rbr * 2, *backgroundImage);
  789. else
  790. rObj.drawImageScaled(
  791. 0, 0, gr.x - rbr * 2, gr.y - rbr * 2, *backgroundImage);
  792. }
  793. else
  794. {
  795. int x = (gr.x - rbr * 2 - backgroundImage->getSize().x) / 2;
  796. int y = (gr.y - rbr * 2 - backgroundImage->getSize().y) / 2;
  797. if (hasStyle(Style::BAlpha))
  798. rObj.alphaImage(
  799. x, y, gr.x - rbr * 2, gr.y - rbr * 2, *backgroundImage);
  800. else
  801. rObj.drawImage(
  802. x, y, gr.x - rbr * 2, gr.y - rbr * 2, *backgroundImage);
  803. }
  804. }
  805. }
  806. if (hasStyle(Style::Buffered) && backgroundFeld)
  807. {
  808. backgroundFeld->setSize(gr.x - rbr * 2, gr.y - rbr * 2);
  809. backgroundFeld->render(rObj);
  810. }
  811. if (vs || hs) rObj.releaseDrawOptions();
  812. rObj.releaseDrawOptions();
  813. rObj.releaseDrawOptions();
  814. rwLock.unlockRead();
  815. }
  816. // Returns the width of the interior area of the drawing in pixels
  817. int DrawableBackground::getInnerWidth() const
  818. {
  819. return getWidth() - 2 * getBorderWidth();
  820. }
  821. // Returns the height of the interior area of the drawing in pixels
  822. int DrawableBackground::getInnerHeight() const
  823. {
  824. return getHeight() - 2 * getBorderWidth();
  825. }
  826. Image*
  827. DrawableBackground::getBackgroundImage() const // returns the background image
  828. {
  829. if (!backgroundImage) return 0;
  830. return dynamic_cast<Image*>(backgroundImage->getThis());
  831. }
  832. Image* DrawableBackground::zBackgroundImage()
  833. const // returns the background image without increased reference counter
  834. {
  835. return backgroundImage;
  836. }
  837. int DrawableBackground::getBackgroundColor()
  838. const // returns the background color
  839. {
  840. return backgroundColor;
  841. }
  842. AlphaField*
  843. DrawableBackground::getAlphaField() const // returns the background buffer
  844. {
  845. if (!backgroundFeld) return 0;
  846. return dynamic_cast<AlphaField*>(backgroundFeld->getThis());
  847. }
  848. AlphaField* DrawableBackground::zAlphaField()
  849. const // returns the background buffer without increased reference counter
  850. {
  851. return backgroundFeld;
  852. }
  853. int DrawableBackground::getAlphaFieldStrength()
  854. const // returns the strength of the background buffer
  855. {
  856. if (!backgroundFeld) return 0;
  857. return backgroundFeld->getStrength();
  858. }
  859. int DrawableBackground::getAlphaFieldColor()
  860. const // returns the color of the background buffer
  861. {
  862. return backgroundFeld->getColor();
  863. }
  864. Border* DrawableBackground::getBorder() const // returns the border
  865. {
  866. if (!border) return 0;
  867. return dynamic_cast<Border*>(border->getThis());
  868. }
  869. Border* DrawableBackground::zBorder()
  870. const // returns the border without increased reference counter
  871. {
  872. return border;
  873. }
  874. int DrawableBackground::getBorderWidth() const // returns the border width
  875. {
  876. if (!border || hasStyleNot(Style::Border)) return 0;
  877. return border->getRWidth();
  878. }
  879. int DrawableBackground::getBorderColor() const // returns the border color
  880. {
  881. return border->getColor();
  882. }
  883. int DrawableBackground::getVerticalClickScroll() const
  884. {
  885. return vertikalScrollBar ? vertikalScrollBar->getClickScroll() : 0;
  886. }
  887. int DrawableBackground::getVerticalScrollPos() const
  888. {
  889. return vertikalScrollBar ? vertikalScrollBar->getScroll() : 0;
  890. }
  891. int DrawableBackground::getVerticalScrollColor() const
  892. {
  893. return vertikalScrollBar ? vertikalScrollBar->getColor() : 0;
  894. }
  895. int DrawableBackground::getVerticalScrollBackground() const
  896. {
  897. return vertikalScrollBar ? vertikalScrollBar->getBgColor() : 0;
  898. }
  899. int DrawableBackground::getHorizontalClickScroll() const
  900. {
  901. return horizontalScrollBar ? horizontalScrollBar->getClickScroll() : 0;
  902. }
  903. int DrawableBackground::getHorizontalScrollPos() const
  904. {
  905. return horizontalScrollBar ? horizontalScrollBar->getScroll() : 0;
  906. }
  907. int DrawableBackground::getHorizontalScrollColor() const
  908. {
  909. return horizontalScrollBar ? horizontalScrollBar->getColor() : 0;
  910. }
  911. int DrawableBackground::getHorizontalScrollBackground() const
  912. {
  913. return horizontalScrollBar ? horizontalScrollBar->getBgColor() : 0;
  914. }
  915. Drawable* DrawableBackground::duplicate() const // Creates a copy of the drawing
  916. {
  917. DrawableBackground* obj = new DrawableBackground();
  918. obj->setPosition(pos);
  919. obj->setSize(gr);
  920. obj->setMouseEventParameter(makParam);
  921. obj->setKeyboardEventParameter(takParam);
  922. obj->setMouseEvent(mak);
  923. obj->setKeyboardEvent(tak);
  924. if (toolTip) obj->setToolTipZ((ToolTip*)toolTip->duplicate());
  925. obj->setStyle(style);
  926. obj->setBackgroundColor(backgroundColor);
  927. if (backgroundFeld)
  928. obj->setAlphaFieldZ((AlphaField*)backgroundFeld->duplicate());
  929. if (border) obj->setBorderZ((Border*)border->duplicate());
  930. if (backgroundImage)
  931. obj->setBackgroundImage(
  932. dynamic_cast<Image*>(backgroundImage->getThis()));
  933. if (vertikalScrollBar)
  934. {
  935. obj->setVerticalClickScroll(vertikalScrollBar->getClickScroll());
  936. obj->setVerticalScrollPos(vertikalScrollBar->getScroll());
  937. obj->setVerticalScrollColor(
  938. vertikalScrollBar->getColor(), vertikalScrollBar->getBgColor());
  939. }
  940. if (horizontalScrollBar)
  941. {
  942. obj->setHorizontalClickScroll(horizontalScrollBar->getClickScroll());
  943. obj->setHorizontalScrollPos(horizontalScrollBar->getScroll());
  944. obj->setHorizontalScrollColor(
  945. horizontalScrollBar->getColor(), horizontalScrollBar->getBgColor());
  946. }
  947. return obj;
  948. }