2
0

PolyScreenEntity.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyScreenEntity.h"
  20. #include "PolyInputEvent.h"
  21. #include "PolyRectangle.h"
  22. #include "PolyPolygon.h"
  23. #include "PolyVertex.h"
  24. #include "PolyRenderer.h"
  25. #include "PolyCoreServices.h"
  26. inline double round(double x) { return floor(x + 0.5); }
  27. using namespace Polycode;
  28. ScreenEntity::ScreenEntity() : Entity(), EventDispatcher() {
  29. color = Color(1.0f,1.0f,1.0f,1.0f);
  30. width = 1;
  31. height = 1;
  32. setHitbox(1, 1);
  33. backfaceCulled = false;
  34. positionMode = POSITION_TOPLEFT;
  35. mouseOver = false;
  36. isDragged = false;
  37. dragOffsetX = 0;
  38. dragOffsetY = 0;
  39. parentEntity = NULL;
  40. zindex = 0;
  41. depthWrite = false;
  42. depthTest = false;
  43. focusable = false;
  44. hasFocus = false;
  45. focusChildren = false;
  46. blockMouseInput = false;
  47. snapToPixels = false;
  48. lastClickTicks = 0;
  49. dragLimits = NULL;
  50. xmouse = 0;
  51. ymouse = 0;
  52. processInputEvents = false;
  53. }
  54. void ScreenEntity::addEntity(Entity *newChild) {
  55. ((ScreenEntity*)newChild)->setDefaultScreenOptions(snapToPixels);
  56. Entity::addEntity(newChild);
  57. }
  58. void ScreenEntity::setDefaultScreenOptions(bool snapToPixels) {
  59. this->snapToPixels = snapToPixels;
  60. for(int i=0; i < children.size(); i++) {
  61. ((ScreenEntity*)children[i])->setDefaultScreenOptions(snapToPixels);
  62. }
  63. }
  64. void ScreenEntity::focusNextChild() {
  65. int j = 0;
  66. bool hasFocusedChild = false;
  67. if(CoreServices::getInstance()->focusedChild) {
  68. for(int i=0; i < children.size(); i++) {
  69. if(children[i] == CoreServices::getInstance()->focusedChild) {
  70. j = i;
  71. hasFocusedChild = true;
  72. }
  73. }
  74. }
  75. if(!hasFocusedChild)
  76. return;
  77. for(int i=0; i < children.size(); i++) {
  78. if(((ScreenEntity*)children[j])->isFocusable() && children[j] != CoreServices::getInstance()->focusedChild) {
  79. focusChild(((ScreenEntity*)children[j]));
  80. return;
  81. }
  82. j++;
  83. if(j == children.size())
  84. j = 0;
  85. }
  86. }
  87. Number ScreenEntity::getRotation() const {
  88. return this->getRoll();
  89. }
  90. void ScreenEntity::focusChild(ScreenEntity *child) {
  91. if(CoreServices::getInstance()->focusedChild != NULL) {
  92. ((ScreenEntity*)CoreServices::getInstance()->focusedChild)->onLoseFocus();
  93. ((ScreenEntity*)CoreServices::getInstance()->focusedChild)->hasFocus = false;
  94. }
  95. CoreServices::getInstance()->focusedChild = child;
  96. ((ScreenEntity*)CoreServices::getInstance()->focusedChild)->hasFocus = true;
  97. ((ScreenEntity*)CoreServices::getInstance()->focusedChild)->onGainFocus();
  98. }
  99. bool ScreenEntity::isFocusable() const {
  100. return focusable;
  101. }
  102. void ScreenEntity::startDrag(Number xOffset, Number yOffset) {
  103. isDragged = true;
  104. dragOffsetX = xOffset;
  105. dragOffsetY = yOffset;
  106. }
  107. void ScreenEntity::stopDrag() {
  108. isDragged = false;
  109. }
  110. ScreenEntity::~ScreenEntity() {
  111. if(CoreServices::getInstance()->focusedChild == this) {
  112. CoreServices::getInstance()->focusedChild = NULL;
  113. }
  114. }
  115. void ScreenEntity::setBlendingMode(int newBlendingMode) {
  116. blendingMode = newBlendingMode;
  117. }
  118. void ScreenEntity::setPosition(Number x, Number y) {
  119. position.x = x;
  120. position.y = y;
  121. matrixDirty = true;
  122. }
  123. void ScreenEntity::setPosition(const Vector2 &v) {
  124. position.x = v.x;
  125. position.y = v.y;
  126. matrixDirty = true;
  127. }
  128. void ScreenEntity::setScale(Number x, Number y) {
  129. scale.x = x;
  130. scale.y = y;
  131. matrixDirty = true;
  132. }
  133. void ScreenEntity::setScale(const Vector2 &v) {
  134. scale.x = v.x;
  135. scale.y = v.y;
  136. matrixDirty = true;
  137. }
  138. Number ScreenEntity::getWidth() const {
  139. return width;
  140. }
  141. Number ScreenEntity::getHeight() const {
  142. return height;
  143. }
  144. bool isPointInsidePolygon2D(Polycode::Polygon *poly, const Vector2 &p) {
  145. Number angle = 0.0;
  146. Vector2 p1,p2;
  147. /*
  148. printf("PIP: %f,%f in [%f,%f], [%f,%f], [%f,%f], [%f,%f]\n", p.x, p.y,
  149. poly->getVertex(0)->x, poly->getVertex(0)->y,
  150. poly->getVertex(1)->x, poly->getVertex(1)->y,
  151. poly->getVertex(2)->x, poly->getVertex(2)->y,
  152. poly->getVertex(3)->x, poly->getVertex(3)->y);
  153. */
  154. for (int i=0; i < poly->getVertexCount(); i++) {
  155. p1.x = poly->getVertex(i)->x - p.x;
  156. p1.y = poly->getVertex(i)->y - p.y;
  157. p2.x = poly->getVertex((i+1)%poly->getVertexCount())->x - p.x;
  158. p2.y = poly->getVertex((i+1)%poly->getVertexCount())->y - p.y;
  159. Vector2 vec(p1.x,p1.y);
  160. angle += vec.angle(Vector2(p2.x,p2.y));
  161. }
  162. if (fabs(angle) < PI)
  163. return false;
  164. else
  165. return true;
  166. }
  167. bool ScreenEntity::hitTest(const Number x, const Number y) {
  168. Vector3 v;
  169. Polygon testPoly;
  170. Matrix4 transformMatrix = getConcatenatedMatrix();
  171. v = Vector3(hit.x, hit.y, 0);
  172. v = transformMatrix * v;
  173. testPoly.addVertex(v.x, v.y, 0.0);
  174. v = Vector3(hit.x+hit.w, hit.y, 0);
  175. v = transformMatrix * v;
  176. testPoly.addVertex(v.x, v.y, 0.0);
  177. v = Vector3(hit.x+hit.w, hit.y+hit.h, 0);
  178. v = transformMatrix * v;
  179. testPoly.addVertex(v.x, v.y, 0.0);
  180. v = Vector3(hit.x,hit.y+hit.h, 0);
  181. v = transformMatrix * v;
  182. testPoly.addVertex(v.x, v.y, 0.0);
  183. return isPointInsidePolygon2D(&testPoly, Vector2(x,y));
  184. }
  185. void ScreenEntity::setPositionMode(int newPositionMode) {
  186. positionMode = newPositionMode;
  187. }
  188. void ScreenEntity::_onKeyDown(PolyKEY key, wchar_t charCode) {
  189. onKeyDown(key, charCode);
  190. for(int i=0;i<children.size();i++) {
  191. ((ScreenEntity*)children[i])->_onKeyDown(key, charCode);
  192. }
  193. }
  194. void ScreenEntity::_onKeyUp(PolyKEY key, wchar_t charCode) {
  195. onKeyUp(key, charCode);
  196. for(int i=0;i<children.size();i++) {
  197. ((ScreenEntity*)children[i])->_onKeyUp(key, charCode);
  198. }
  199. }
  200. int ScreenEntity::getPositionMode() {
  201. return positionMode;
  202. }
  203. void ScreenEntity::setDragLimits(Polycode::Rectangle rect) {
  204. if(!dragLimits)
  205. dragLimits = new Polycode::Rectangle();
  206. dragLimits->x = rect.x;
  207. dragLimits->y = rect.y;
  208. dragLimits->w = rect.w;
  209. dragLimits->h = rect.h;
  210. }
  211. void ScreenEntity::clearDragLimits() {
  212. delete dragLimits;
  213. dragLimits = NULL;
  214. }
  215. Rectangle ScreenEntity::getHitbox() {
  216. return hit;
  217. }
  218. void ScreenEntity::setHitbox(Number width, Number height) {
  219. hit.w = width;
  220. hit.h = height;
  221. hit.x = -width/2;
  222. hit.y = -height/2;
  223. }
  224. void ScreenEntity::setHitbox(Number width, Number height, Number left, Number top) {
  225. hit.w = width;
  226. hit.h = height;
  227. hit.x = left;
  228. hit.y = top;
  229. }
  230. Matrix4 ScreenEntity::getScreenConcatenatedMatrix() {
  231. Matrix4 retMatrix = transformMatrix;
  232. if(positionMode == POSITION_TOPLEFT) {
  233. Vector3 pos = retMatrix.getPosition();
  234. retMatrix.setPosition(pos.x + width/2.0, pos.y + height/2.0, 0);
  235. }
  236. if(parentEntity) {
  237. return retMatrix * ((ScreenEntity*)parentEntity)->getScreenConcatenatedMatrix();
  238. } else {
  239. return retMatrix;
  240. }
  241. }
  242. void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp, Vector2 parentAdjust) {
  243. if(isDragged) {
  244. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  245. if(parentEntity) {
  246. Matrix4 inverse = ((ScreenEntity*)parentEntity)->getScreenConcatenatedMatrix().inverse();
  247. localCoordinate = inverse * localCoordinate;
  248. }
  249. setPosition(localCoordinate.x-dragOffsetX,localCoordinate.y-dragOffsetY);
  250. if(dragLimits) {
  251. if(position.x < dragLimits->x)
  252. position.x = dragLimits->x;
  253. if(position.x > dragLimits->x + dragLimits->w)
  254. position.x = dragLimits->x + dragLimits->w;
  255. if(position.y < dragLimits->y)
  256. position.y = dragLimits->y;
  257. if(position.y > dragLimits->y + dragLimits->h)
  258. position.y = dragLimits->y + dragLimits->h;
  259. }
  260. }
  261. bool doTest = true;
  262. // if(hasMask) {
  263. // if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  264. // doTest = false;
  265. // }
  266. // }
  267. if(doTest) {
  268. if(processInputEvents && enabled) {
  269. if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  270. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  271. Matrix4 inverse = getConcatenatedMatrix().inverse();
  272. localCoordinate = inverse * localCoordinate;
  273. if(positionMode == POSITION_TOPLEFT)
  274. localCoordinate.x += hit.w/2.0;
  275. if(positionMode == POSITION_TOPLEFT)
  276. localCoordinate.y += hit.h/2.0;
  277. onMouseMove(localCoordinate.x,localCoordinate.y);
  278. xmouse = localCoordinate.x;
  279. ymouse = localCoordinate.y;
  280. dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEMOVE);
  281. if(!mouseOver) {
  282. dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEOVER);
  283. mouseOver = true;
  284. }
  285. } else {
  286. if(mouseOver) {
  287. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  288. Matrix4 inverse = getConcatenatedMatrix().inverse();
  289. localCoordinate = inverse * localCoordinate;
  290. if(positionMode == POSITION_TOPLEFT)
  291. localCoordinate.x += hit.w/2.0;
  292. if(positionMode == POSITION_TOPLEFT)
  293. localCoordinate.y += hit.h/2.0;
  294. dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEOUT);
  295. mouseOver = false;
  296. }
  297. }
  298. }
  299. if(enabled) {
  300. for(int i=children.size()-1;i>=0;i--) {
  301. Vector2 adjust = parentAdjust;
  302. if(positionMode == POSITION_TOPLEFT)
  303. adjust += Vector2(width/2.0, height/2.0);
  304. ((ScreenEntity*)children[i])->_onMouseMove(x,y, timestamp, adjust);
  305. if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
  306. if(((ScreenEntity*)children[i])->hitTest(x,y))
  307. break;
  308. }
  309. }
  310. }
  311. }
  312. }
  313. bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust) {
  314. bool retVal = false;
  315. bool doTest = true;
  316. // if(hasMask) {
  317. // if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  318. // doTest = false;
  319. // }
  320. // }
  321. if(doTest) {
  322. if(processInputEvents && enabled) {
  323. if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  324. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  325. Matrix4 inverse = getConcatenatedMatrix().inverse();
  326. localCoordinate = inverse * localCoordinate;
  327. if(positionMode == POSITION_TOPLEFT)
  328. localCoordinate.x += hit.w/2.0;
  329. if(positionMode == POSITION_TOPLEFT)
  330. localCoordinate.y += hit.h/2.0;
  331. onMouseUp(localCoordinate.x,localCoordinate.y);
  332. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);
  333. inputEvent->mouseButton = mouseButton;
  334. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP);
  335. retVal = true;
  336. } else {
  337. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  338. Matrix4 inverse = getConcatenatedMatrix().inverse();
  339. localCoordinate = inverse * localCoordinate;
  340. if(positionMode == POSITION_TOPLEFT)
  341. localCoordinate.x += hit.w/2.0;
  342. if(positionMode == POSITION_TOPLEFT)
  343. localCoordinate.y += hit.h/2.0;
  344. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);
  345. inputEvent->mouseButton = mouseButton;
  346. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  347. }
  348. }
  349. if(enabled) {
  350. for(int i=children.size()-1;i>=0;i--) {
  351. Vector2 adjust = parentAdjust;
  352. if(positionMode == POSITION_TOPLEFT)
  353. adjust += Vector2(width/2.0, height/2.0);
  354. ((ScreenEntity*)children[i])->_onMouseUp(x,y, mouseButton, timestamp, adjust);;
  355. if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
  356. if(((ScreenEntity*)children[i])->hitTest(x,y))
  357. break;
  358. }
  359. }
  360. }
  361. }
  362. return retVal;
  363. }
  364. void ScreenEntity::_onMouseWheelUp(Number x, Number y, int timestamp, Vector2 parentAdjust) {
  365. bool retVal = false;
  366. bool doTest = true;
  367. // if(hasMask) {
  368. // if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  369. // doTest = false;
  370. // }
  371. // }
  372. if(doTest) {
  373. if(processInputEvents && enabled) {
  374. if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  375. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  376. Matrix4 inverse = getConcatenatedMatrix().inverse();
  377. localCoordinate = inverse * localCoordinate;
  378. if(positionMode == POSITION_TOPLEFT)
  379. localCoordinate.x += hit.w/2.0;
  380. if(positionMode == POSITION_TOPLEFT)
  381. localCoordinate.y += hit.h/2.0;
  382. onMouseWheelUp(localCoordinate.x,localCoordinate.y);
  383. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);
  384. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_UP);
  385. retVal = true;
  386. }
  387. }
  388. if(enabled) {
  389. for(int i=children.size()-1;i>=0;i--) {
  390. Vector2 adjust = parentAdjust;
  391. if(positionMode == POSITION_TOPLEFT)
  392. adjust += Vector2(width/2.0, height/2.0);
  393. ((ScreenEntity*)children[i])->_onMouseWheelUp(x,y, timestamp, adjust);;
  394. if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
  395. if(((ScreenEntity*)children[i])->hitTest(x,y))
  396. break;
  397. }
  398. }
  399. }
  400. }
  401. }
  402. void ScreenEntity::_onMouseWheelDown(Number x, Number y, int timestamp, Vector2 parentAdjust) {
  403. bool retVal = false;
  404. bool doTest = true;
  405. // if(hasMask) {
  406. // if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  407. // doTest = false;
  408. // }
  409. // }
  410. if(doTest) {
  411. if(processInputEvents && enabled) {
  412. if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  413. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  414. Matrix4 inverse = getConcatenatedMatrix().inverse();
  415. localCoordinate = inverse * localCoordinate;
  416. if(positionMode == POSITION_TOPLEFT)
  417. localCoordinate.x += hit.w/2.0;
  418. if(positionMode == POSITION_TOPLEFT)
  419. localCoordinate.y += hit.h/2.0;
  420. onMouseWheelDown(localCoordinate.x,localCoordinate.y);
  421. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);
  422. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_DOWN);
  423. retVal = true;
  424. }
  425. }
  426. if(enabled) {
  427. for(int i=children.size()-1;i>=0;i--) {
  428. Vector2 adjust = parentAdjust;
  429. if(positionMode == POSITION_TOPLEFT)
  430. adjust += Vector2(width/2.0, height/2.0);
  431. ((ScreenEntity*)children[i])->_onMouseWheelDown(x,y, timestamp, adjust);;
  432. if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
  433. if(((ScreenEntity*)children[i])->hitTest(x,y))
  434. break;
  435. }
  436. }
  437. }
  438. }
  439. }
  440. bool ScreenEntity::_onMouseDown(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust) {
  441. bool retVal = false;
  442. bool doTest = true;
  443. // if(hasMask) {
  444. // if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  445. // doTest = false;
  446. // }
  447. // }
  448. if(doTest) {
  449. if(processInputEvents && enabled) {
  450. if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
  451. Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
  452. Matrix4 inverse = getConcatenatedMatrix().inverse();
  453. localCoordinate = inverse * localCoordinate;
  454. if(positionMode == POSITION_TOPLEFT)
  455. localCoordinate.x += hit.w/2.0;
  456. if(positionMode == POSITION_TOPLEFT)
  457. localCoordinate.y += hit.h/2.0;
  458. onMouseDown(localCoordinate.x,localCoordinate.y);
  459. InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);
  460. inputEvent->mouseButton = mouseButton;
  461. dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEDOWN);
  462. if(timestamp - lastClickTicks < 400) {
  463. InputEvent *inputEvent = new InputEvent(Vector2(x,y), timestamp);
  464. inputEvent->mouseButton = mouseButton;
  465. dispatchEvent(inputEvent, InputEvent::EVENT_DOUBLECLICK);
  466. }
  467. lastClickTicks = timestamp;
  468. retVal = true;
  469. }
  470. }
  471. if(enabled) {
  472. for(int i=children.size()-1;i>=0;i--) {
  473. Vector2 adjust = parentAdjust;
  474. if(positionMode == POSITION_TOPLEFT)
  475. adjust += Vector2(width/2.0, height/2.0);
  476. ((ScreenEntity*)children[i])->_onMouseDown(x,y, mouseButton, timestamp, adjust);;
  477. if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
  478. if(((ScreenEntity*)children[i])->hitTest(x,y))
  479. break;
  480. }
  481. }
  482. }
  483. }
  484. return retVal;
  485. }
  486. Vector2 ScreenEntity::getScreenPosition() const {
  487. Vector2 ret = getPosition2D();
  488. if(parentEntity) {
  489. return ret + ((ScreenEntity*)parentEntity)->getScreenPosition();
  490. } else {
  491. return ret;
  492. }
  493. }
  494. void ScreenEntity::setRotation(Number rotation) {
  495. setRoll(rotation);
  496. }
  497. Vector2 ScreenEntity::getPosition2D() const {
  498. return Vector2(position.x, position.y);
  499. }
  500. Matrix4 ScreenEntity::buildPositionMatrix() {
  501. Matrix4 posMatrix;
  502. switch(positionMode) {
  503. case POSITION_TOPLEFT:
  504. posMatrix.m[3][0] = (position.x+floor(width/2.0f)*scale.x)*matrixAdj;
  505. posMatrix.m[3][1] = (position.y+floor(height/2.0f)*scale.y)*matrixAdj;
  506. posMatrix.m[3][2] = position.z*matrixAdj;
  507. break;
  508. case POSITION_CENTER:
  509. posMatrix.m[3][0] = position.x*matrixAdj;
  510. posMatrix.m[3][1] = position.y*matrixAdj;
  511. posMatrix.m[3][2] = position.z*matrixAdj;
  512. break;
  513. }
  514. if(snapToPixels) {
  515. posMatrix.m[3][0] = round(posMatrix.m[3][0]);
  516. posMatrix.m[3][1] = round(posMatrix.m[3][1]);
  517. posMatrix.m[3][2] = round(posMatrix.m[3][2]);
  518. }
  519. return posMatrix;
  520. }
  521. void ScreenEntity::adjustMatrixForChildren() {
  522. if(positionMode == POSITION_TOPLEFT) {
  523. if(snapToPixels) {
  524. renderer->translate2D(-floor(width/2.0f), -floor(height/2.0f));
  525. } else {
  526. renderer->translate2D(-width/2.0f, -height/2.0f);
  527. }
  528. }
  529. }