Form.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #include "Base.h"
  2. #include "Form.h"
  3. #include "AbsoluteLayout.h"
  4. #include "FlowLayout.h"
  5. #include "VerticalLayout.h"
  6. #include "Game.h"
  7. #include "Theme.h"
  8. #include "Label.h"
  9. #include "Button.h"
  10. #include "CheckBox.h"
  11. #include "Scene.h"
  12. // Default form shaders
  13. #define FORM_VSH "res/shaders/form.vert"
  14. #define FORM_FSH "res/shaders/form.frag"
  15. namespace gameplay
  16. {
  17. static Effect* __formEffect = NULL;
  18. static std::vector<Form*> __forms;
  19. Form::Form() : _theme(NULL), _frameBuffer(NULL), _spriteBatch(NULL), _node(NULL),
  20. _nodeQuad(NULL), _nodeMaterial(NULL) , _u2(0), _v1(0), _isGamepad(false)
  21. {
  22. }
  23. Form::~Form()
  24. {
  25. SAFE_DELETE(_spriteBatch);
  26. SAFE_RELEASE(_frameBuffer);
  27. SAFE_RELEASE(_theme);
  28. if (__formEffect)
  29. {
  30. if (__formEffect->getRefCount() == 1)
  31. {
  32. __formEffect->release();
  33. __formEffect = NULL;
  34. }
  35. }
  36. // Remove this Form from the global list.
  37. std::vector<Form*>::iterator it = std::find(__forms.begin(), __forms.end(), this);
  38. if (it != __forms.end())
  39. {
  40. __forms.erase(it);
  41. }
  42. }
  43. Form* Form::create(const char* id, Theme::Style* style, Layout::Type layoutType)
  44. {
  45. GP_ASSERT(style);
  46. Layout* layout;
  47. switch (layoutType)
  48. {
  49. case Layout::LAYOUT_ABSOLUTE:
  50. layout = AbsoluteLayout::create();
  51. break;
  52. case Layout::LAYOUT_FLOW:
  53. layout = FlowLayout::create();
  54. break;
  55. case Layout::LAYOUT_VERTICAL:
  56. layout = VerticalLayout::create();
  57. break;
  58. default:
  59. GP_ERROR("Unsupported layout type '%d'.", layoutType);
  60. break;
  61. }
  62. Form* form = new Form();
  63. if (id)
  64. form->_id = id;
  65. form->_style = style;
  66. form->_layout = layout;
  67. form->_theme = style->getTheme();
  68. form->_theme->addRef();
  69. // Get default projection matrix.
  70. Game* game = Game::getInstance();
  71. Matrix::createOrthographicOffCenter(0, game->getWidth(), game->getHeight(), 0, 0, 1, &form->_defaultProjectionMatrix);
  72. form->updateBounds();
  73. __forms.push_back(form);
  74. return form;
  75. }
  76. Form* Form::create(const char* url)
  77. {
  78. // Load Form from .form file.
  79. Properties* properties = Properties::create(url);
  80. if (properties == NULL)
  81. {
  82. GP_ASSERT(properties);
  83. return NULL;
  84. }
  85. // Check if the Properties is valid and has a valid namespace.
  86. Properties* formProperties = (strlen(properties->getNamespace()) > 0) ? properties : properties->getNextNamespace();
  87. assert(formProperties);
  88. if (!formProperties || !(strcmp(formProperties->getNamespace(), "form") == 0))
  89. {
  90. GP_ASSERT(formProperties);
  91. SAFE_DELETE(properties);
  92. return NULL;
  93. }
  94. // Create new form with given ID, theme and layout.
  95. const char* themeFile = formProperties->getString("theme");
  96. const char* layoutString = formProperties->getString("layout");
  97. Layout* layout;
  98. switch (getLayoutType(layoutString))
  99. {
  100. case Layout::LAYOUT_ABSOLUTE:
  101. layout = AbsoluteLayout::create();
  102. break;
  103. case Layout::LAYOUT_FLOW:
  104. layout = FlowLayout::create();
  105. break;
  106. case Layout::LAYOUT_VERTICAL:
  107. layout = VerticalLayout::create();
  108. break;
  109. default:
  110. GP_ERROR("Unsupported layout type '%d'.", getLayoutType(layoutString));
  111. break;
  112. }
  113. Theme* theme = Theme::create(themeFile);
  114. GP_ASSERT(theme);
  115. Form* form = new Form();
  116. form->_layout = layout;
  117. form->_theme = theme;
  118. // Get default projection matrix.
  119. Game* game = Game::getInstance();
  120. Matrix::createOrthographicOffCenter(0, game->getWidth(), game->getHeight(), 0, 0, 1, &form->_defaultProjectionMatrix);
  121. Theme::Style* style = NULL;
  122. const char* styleName = formProperties->getString("style");
  123. if (styleName)
  124. {
  125. style = theme->getStyle(styleName);
  126. }
  127. else
  128. {
  129. style = theme->getEmptyStyle();
  130. }
  131. form->initialize(style, formProperties);
  132. form->_consumeInputEvents = formProperties->getBool("consumeInputEvents", false);
  133. // Alignment
  134. if ((form->_alignment & Control::ALIGN_BOTTOM) == Control::ALIGN_BOTTOM)
  135. {
  136. form->_bounds.y = Game::getInstance()->getHeight() - form->_bounds.height;
  137. }
  138. else if ((form->_alignment & Control::ALIGN_VCENTER) == Control::ALIGN_VCENTER)
  139. {
  140. form->_bounds.y = Game::getInstance()->getHeight() * 0.5f - form->_bounds.height * 0.5f;
  141. }
  142. if ((form->_alignment & Control::ALIGN_RIGHT) == Control::ALIGN_RIGHT)
  143. {
  144. form->_bounds.x = Game::getInstance()->getWidth() - form->_bounds.width;
  145. }
  146. else if ((form->_alignment & Control::ALIGN_HCENTER) == Control::ALIGN_HCENTER)
  147. {
  148. form->_bounds.x = Game::getInstance()->getWidth() * 0.5f - form->_bounds.width * 0.5f;
  149. }
  150. form->_scroll = getScroll(formProperties->getString("scroll"));
  151. form->_scrollBarsAutoHide = formProperties->getBool("scrollBarsAutoHide");
  152. if (form->_scrollBarsAutoHide)
  153. {
  154. form->_scrollBarOpacity = 0.0f;
  155. }
  156. // Add all the controls to the form.
  157. form->addControls(theme, formProperties);
  158. SAFE_DELETE(properties);
  159. form->updateBounds();
  160. __forms.push_back(form);
  161. return form;
  162. }
  163. Form* Form::getForm(const char* id)
  164. {
  165. std::vector<Form*>::const_iterator it;
  166. for (it = __forms.begin(); it < __forms.end(); ++it)
  167. {
  168. Form* f = *it;
  169. GP_ASSERT(f);
  170. if (strcmp(id, f->getId()) == 0)
  171. {
  172. return f;
  173. }
  174. }
  175. return NULL;
  176. }
  177. Theme* Form::getTheme() const
  178. {
  179. return _theme;
  180. }
  181. void Form::setSize(float width, float height)
  182. {
  183. if (_autoWidth)
  184. {
  185. width = Game::getInstance()->getWidth();
  186. }
  187. if (_autoHeight)
  188. {
  189. height = Game::getInstance()->getHeight();
  190. }
  191. if (width != 0.0f && height != 0.0f &&
  192. (width != _bounds.width || height != _bounds.height))
  193. {
  194. // Width and height must be powers of two to create a texture.
  195. unsigned int w = nextPowerOfTwo(width);
  196. unsigned int h = nextPowerOfTwo(height);
  197. _u2 = width / (float)w;
  198. _v1 = height / (float)h;
  199. // Create framebuffer if necessary. TODO: Use pool to cache.
  200. if (_frameBuffer)
  201. SAFE_RELEASE(_frameBuffer)
  202. _frameBuffer = FrameBuffer::create(_id.c_str(), w, h);
  203. GP_ASSERT(_frameBuffer);
  204. // Re-create projection matrix.
  205. Matrix::createOrthographicOffCenter(0, width, height, 0, 0, 1, &_projectionMatrix);
  206. // Re-create sprite batch.
  207. SAFE_DELETE(_spriteBatch);
  208. _spriteBatch = SpriteBatch::create(_frameBuffer->getRenderTarget()->getTexture());
  209. GP_ASSERT(_spriteBatch);
  210. // Clear the framebuffer black
  211. Game* game = Game::getInstance();
  212. FrameBuffer* previousFrameBuffer = _frameBuffer->bind();
  213. Rectangle previousViewport = game->getViewport();
  214. game->setViewport(Rectangle(0, 0, width, height));
  215. _theme->setProjectionMatrix(_projectionMatrix);
  216. game->clear(Game::CLEAR_COLOR, Vector4::zero(), 1.0, 0);
  217. _theme->setProjectionMatrix(_defaultProjectionMatrix);
  218. previousFrameBuffer->bind();
  219. game->setViewport(previousViewport);
  220. }
  221. _bounds.width = width;
  222. _bounds.height = height;
  223. _dirty = true;
  224. }
  225. void Form::setBounds(const Rectangle& bounds)
  226. {
  227. setPosition(bounds.x, bounds.y);
  228. setSize(bounds.width, bounds.height);
  229. }
  230. void Form::setWidth(float width)
  231. {
  232. setSize(width, _bounds.height);
  233. }
  234. void Form::setHeight(float height)
  235. {
  236. setSize(_bounds.width, height);
  237. }
  238. void Form::setAutoWidth(bool autoWidth)
  239. {
  240. if (_autoWidth != autoWidth)
  241. {
  242. _autoWidth = autoWidth;
  243. _dirty = true;
  244. if (_autoWidth)
  245. {
  246. setSize(_bounds.width, Game::getInstance()->getWidth());
  247. }
  248. }
  249. }
  250. void Form::setAutoHeight(bool autoHeight)
  251. {
  252. if (_autoHeight != autoHeight)
  253. {
  254. _autoHeight = autoHeight;
  255. _dirty = true;
  256. if (_autoHeight)
  257. {
  258. setSize(_bounds.width, Game::getInstance()->getHeight());
  259. }
  260. }
  261. }
  262. static Effect* createEffect()
  263. {
  264. Effect* effect = NULL;
  265. if (__formEffect == NULL)
  266. {
  267. __formEffect = Effect::createFromFile(FORM_VSH, FORM_FSH);
  268. if (__formEffect == NULL)
  269. {
  270. GP_ERROR("Unable to load form effect.");
  271. return NULL;
  272. }
  273. effect = __formEffect;
  274. }
  275. else
  276. {
  277. effect = __formEffect;
  278. }
  279. return effect;
  280. }
  281. void Form::setNode(Node* node)
  282. {
  283. // If the user wants a custom node then we need to create a 3D quad
  284. if (node && node != _node)
  285. {
  286. // Set this Form up to be 3D by initializing a quad.
  287. float x2 = _bounds.width;
  288. float y2 = _bounds.height;
  289. float vertices[] =
  290. {
  291. 0, y2, 0, 0, _v1,
  292. 0, 0, 0, 0, 0,
  293. x2, y2, 0, _u2, _v1,
  294. x2, 0, 0, _u2, 0
  295. };
  296. VertexFormat::Element elements[] =
  297. {
  298. VertexFormat::Element(VertexFormat::POSITION, 3),
  299. VertexFormat::Element(VertexFormat::TEXCOORD0, 2)
  300. };
  301. Mesh* mesh = Mesh::createMesh(VertexFormat(elements, 2), 4, false);
  302. GP_ASSERT(mesh);
  303. mesh->setPrimitiveType(Mesh::TRIANGLE_STRIP);
  304. mesh->setVertexData(vertices, 0, 4);
  305. _nodeQuad = Model::create(mesh);
  306. SAFE_RELEASE(mesh);
  307. GP_ASSERT(_nodeQuad);
  308. // Create the effect and material
  309. Effect* effect = createEffect();
  310. GP_ASSERT(effect);
  311. _nodeMaterial = Material::create(effect);
  312. GP_ASSERT(_nodeMaterial);
  313. _nodeQuad->setMaterial(_nodeMaterial);
  314. _nodeMaterial->release();
  315. node->setModel(_nodeQuad);
  316. _nodeQuad->release();
  317. // Bind the WorldViewProjection matrix.
  318. _nodeMaterial->setParameterAutoBinding("u_worldViewProjectionMatrix", RenderState::WORLD_VIEW_PROJECTION_MATRIX);
  319. // Bind the texture from the framebuffer and set the texture to clamp
  320. Texture::Sampler* sampler = Texture::Sampler::create(_frameBuffer->getRenderTarget()->getTexture());
  321. GP_ASSERT(sampler);
  322. sampler->setWrapMode(Texture::CLAMP, Texture::CLAMP);
  323. _nodeMaterial->getParameter("u_texture")->setValue(sampler);
  324. sampler->release();
  325. RenderState::StateBlock* rsBlock = _nodeMaterial->getStateBlock();
  326. rsBlock->setDepthWrite(true);
  327. rsBlock->setBlend(true);
  328. rsBlock->setBlendSrc(RenderState::BLEND_SRC_ALPHA);
  329. rsBlock->setBlendDst(RenderState::BLEND_ONE_MINUS_SRC_ALPHA);
  330. }
  331. _node = node;
  332. }
  333. void Form::update(float elapsedTime)
  334. {
  335. if (isDirty())
  336. {
  337. updateBounds();
  338. // Cache themed attributes for performance.
  339. _skin = getSkin(_state);
  340. _opacity = getOpacity(_state);
  341. GP_ASSERT(_layout);
  342. if (_scroll != SCROLL_NONE)
  343. {
  344. updateScroll();
  345. }
  346. else
  347. {
  348. _layout->update(this, Vector2::zero());
  349. }
  350. }
  351. }
  352. void Form::updateBounds()
  353. {
  354. _clearBounds.set(_absoluteClipBounds);
  355. // Calculate the clipped bounds.
  356. float x = 0;
  357. float y = 0;
  358. float width = _bounds.width;
  359. float height = _bounds.height;
  360. Rectangle clip(0, 0, _bounds.width, _bounds.height);
  361. float clipX2 = clip.x + clip.width;
  362. float x2 = clip.x + x + width;
  363. if (x2 > clipX2)
  364. width -= x2 - clipX2;
  365. float clipY2 = clip.y + clip.height;
  366. float y2 = clip.y + y + height;
  367. if (y2 > clipY2)
  368. height -= y2 - clipY2;
  369. if (x < 0)
  370. {
  371. width += x;
  372. x = -x;
  373. }
  374. else
  375. {
  376. x = 0;
  377. }
  378. if (y < 0)
  379. {
  380. height += y;
  381. y = -y;
  382. }
  383. else
  384. {
  385. y = 0;
  386. }
  387. _clipBounds.set(x, y, width, height);
  388. // Calculate the absolute bounds.
  389. x = 0;
  390. y = 0;
  391. _absoluteBounds.set(x, y, _bounds.width, _bounds.height);
  392. // Calculate the absolute viewport bounds. Absolute bounds minus border and padding.
  393. const Theme::Border& border = getBorder(_state);
  394. const Theme::Padding& padding = getPadding();
  395. x += border.left + padding.left;
  396. y += border.top + padding.top;
  397. width = _bounds.width - border.left - padding.left - border.right - padding.right;
  398. height = _bounds.height - border.top - padding.top - border.bottom - padding.bottom;
  399. _viewportBounds.set(x, y, width, height);
  400. // Calculate the clip area. Absolute bounds, minus border and padding, clipped to the parent container's clip area.
  401. clipX2 = clip.x + clip.width;
  402. x2 = x + width;
  403. if (x2 > clipX2)
  404. width = clipX2 - x;
  405. clipY2 = clip.y + clip.height;
  406. y2 = y + height;
  407. if (y2 > clipY2)
  408. height = clipY2 - y;
  409. if (x < clip.x)
  410. {
  411. float dx = clip.x - x;
  412. width -= dx;
  413. x = clip.x;
  414. }
  415. if (y < clip.y)
  416. {
  417. float dy = clip.y - y;
  418. height -= dy;
  419. y = clip.y;
  420. }
  421. _viewportClipBounds.set(x, y, width, height);
  422. _absoluteClipBounds.set(x - border.left - padding.left, y - border.top - padding.top,
  423. width + border.left + padding.left + border.right + padding.right,
  424. height + border.top + padding.top + border.bottom + padding.bottom);
  425. if (_clearBounds.isEmpty())
  426. {
  427. _clearBounds.set(_absoluteClipBounds);
  428. }
  429. // Get scrollbar images and diminish clipping bounds to make room for scrollbars.
  430. if ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL)
  431. {
  432. _scrollBarLeftCap = getImage("scrollBarLeftCap", _state);
  433. _scrollBarHorizontal = getImage("horizontalScrollBar", _state);
  434. _scrollBarRightCap = getImage("scrollBarRightCap", _state);
  435. _viewportClipBounds.height -= _scrollBarHorizontal->getRegion().height;
  436. }
  437. if ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL)
  438. {
  439. _scrollBarTopCap = getImage("scrollBarTopCap", _state);
  440. _scrollBarVertical = getImage("verticalScrollBar", _state);
  441. _scrollBarBottomCap = getImage("scrollBarBottomCap", _state);
  442. _viewportClipBounds.width -= _scrollBarVertical->getRegion().width;
  443. }
  444. }
  445. void Form::draw()
  446. {
  447. // The first time a form is drawn, its contents are rendered into a framebuffer.
  448. // The framebuffer will only be drawn into again when the contents of the form change.
  449. // If this form has a node then it's a 3D form and the framebuffer will be used
  450. // to texture a quad. The quad will be given the same dimensions as the form and
  451. // must be transformed appropriately by the user, unless they call setQuad() themselves.
  452. // On the other hand, if this form has not been set on a node, SpriteBatch will be used
  453. // to render the contents of the framebuffer directly to the display.
  454. // Check whether this form has changed since the last call to draw() and if so, render into the framebuffer.
  455. if (isDirty())
  456. {
  457. GP_ASSERT(_frameBuffer);
  458. FrameBuffer* previousFrameBuffer = _frameBuffer->bind();
  459. Game* game = Game::getInstance();
  460. Rectangle prevViewport = game->getViewport();
  461. game->setViewport(Rectangle(0, 0, _bounds.width, _bounds.height));
  462. GP_ASSERT(_theme);
  463. _theme->setProjectionMatrix(_projectionMatrix);
  464. // By setting needsClear to true here, an optimization meant to clear and redraw only areas of the form
  465. // that have changed is disabled. Currently, repositioning controls can result in areas of the screen being cleared
  466. // after another control has been drawn there. This should probably be done in two passes -- one to clear areas where
  467. // dirty controls were last frame, and another to draw them where they are now.
  468. Container::draw(_theme->getSpriteBatch(), Rectangle(0, 0, _bounds.width, _bounds.height),
  469. /*_skin != NULL*/ true, false, _bounds.height);
  470. _theme->setProjectionMatrix(_defaultProjectionMatrix);
  471. // Restore the previous game viewport.
  472. game->setViewport(prevViewport);
  473. // Rebind the previous framebuffer and game viewport.
  474. previousFrameBuffer->bind();
  475. }
  476. // Draw either with a 3D quad or sprite batch.
  477. if (_node)
  478. {
  479. // If we have the node set, then draw a 3D quad model.
  480. _nodeQuad->draw();
  481. }
  482. else
  483. {
  484. // Otherwise we draw the framebuffer in ortho space with a spritebatch.
  485. if (!_spriteBatch)
  486. {
  487. _spriteBatch = SpriteBatch::create(_frameBuffer->getRenderTarget()->getTexture());
  488. GP_ASSERT(_spriteBatch);
  489. }
  490. _spriteBatch->start();
  491. _spriteBatch->draw(_bounds.x, _bounds.y, 0, _bounds.width, _bounds.height, 0, _v1, _u2, 0, Vector4::one());
  492. _spriteBatch->finish();
  493. }
  494. }
  495. const char* Form::getType() const
  496. {
  497. return "form";
  498. }
  499. void Form::updateInternal(float elapsedTime)
  500. {
  501. size_t size = __forms.size();
  502. for (size_t i = 0; i < size; ++i)
  503. {
  504. Form* form = __forms[i];
  505. GP_ASSERT(form);
  506. if (form->isEnabled() && form->isVisible())
  507. {
  508. form->update(elapsedTime);
  509. }
  510. }
  511. }
  512. static bool shouldPropagateTouchEvent(Control::State state, Touch::TouchEvent evt, const Rectangle& bounds, int x, int y)
  513. {
  514. return (state != Control::NORMAL ||
  515. (evt == Touch::TOUCH_PRESS &&
  516. x >= bounds.x &&
  517. x <= bounds.x + bounds.width &&
  518. y >= bounds.y &&
  519. y <= bounds.y + bounds.height));
  520. }
  521. bool Form::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  522. {
  523. // Check for a collision with each Form in __forms.
  524. // Pass the event on.
  525. size_t size = __forms.size();
  526. for (size_t i = 0; i < size; ++i)
  527. {
  528. Form* form = __forms[i];
  529. GP_ASSERT(form);
  530. if (form->isEnabled() && form->isVisible())
  531. {
  532. if (form->_node)
  533. {
  534. Vector3 point;
  535. if (form->projectPoint(x, y, &point))
  536. {
  537. const Rectangle& bounds = form->getBounds();
  538. if (shouldPropagateTouchEvent(form->getState(), evt, bounds, point.x, point.y))
  539. {
  540. if (form->touchEvent(evt, point.x - bounds.x, bounds.height - point.y - bounds.y, contactIndex))
  541. return true;
  542. }
  543. }
  544. }
  545. else
  546. {
  547. // Simply compare with the form's bounds.
  548. const Rectangle& bounds = form->getBounds();
  549. if (shouldPropagateTouchEvent(form->getState(), evt, bounds, x, y))
  550. {
  551. // Pass on the event's position relative to the form.
  552. if (form->touchEvent(evt, x - bounds.x, y - bounds.y, contactIndex))
  553. return true;
  554. }
  555. }
  556. }
  557. }
  558. return false;
  559. }
  560. bool Form::keyEventInternal(Keyboard::KeyEvent evt, int key)
  561. {
  562. size_t size = __forms.size();
  563. for (size_t i = 0; i < size; ++i)
  564. {
  565. Form* form = __forms[i];
  566. GP_ASSERT(form);
  567. if (form->isEnabled() && form->isVisible() && form->hasFocus() && !form->_isGamepad)
  568. {
  569. if (form->keyEvent(evt, key))
  570. return true;
  571. }
  572. }
  573. return false;
  574. }
  575. static bool shouldPropagateMouseEvent(Control::State state, Mouse::MouseEvent evt, const Rectangle& bounds, int x, int y)
  576. {
  577. return (state != Control::NORMAL ||
  578. ((evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
  579. evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
  580. evt == Mouse::MOUSE_PRESS_RIGHT_BUTTON ||
  581. evt == Mouse::MOUSE_MOVE ||
  582. evt == Mouse::MOUSE_WHEEL) &&
  583. x >= bounds.x &&
  584. x <= bounds.x + bounds.width &&
  585. y >= bounds.y &&
  586. y <= bounds.y + bounds.height));
  587. }
  588. bool Form::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  589. {
  590. // Do not process mouse input when mouse is captured
  591. if (Game::getInstance()->isMouseCaptured())
  592. return false;
  593. for (size_t i = 0; i < __forms.size(); ++i)
  594. {
  595. Form* form = __forms[i];
  596. GP_ASSERT(form);
  597. if (form->isEnabled() && form->isVisible())
  598. {
  599. if (form->_node)
  600. {
  601. Vector3 point;
  602. if (form->projectPoint(x, y, &point))
  603. {
  604. const Rectangle& bounds = form->getBounds();
  605. if (shouldPropagateMouseEvent(form->getState(), evt, bounds, point.x, point.y))
  606. {
  607. if (form->mouseEvent(evt, point.x - bounds.x, bounds.height - point.y - bounds.y, wheelDelta))
  608. return true;
  609. }
  610. }
  611. }
  612. else
  613. {
  614. // Simply compare with the form's bounds.
  615. const Rectangle& bounds = form->getBounds();
  616. if (shouldPropagateMouseEvent(form->getState(), evt, bounds, x, y))
  617. {
  618. // Pass on the event's position relative to the form.
  619. if (form->mouseEvent(evt, x - bounds.x, y - bounds.y, wheelDelta))
  620. return true;
  621. }
  622. }
  623. }
  624. }
  625. return false;
  626. }
  627. void Form::gamepadEventInternal(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  628. {
  629. for (size_t i = 0; i < __forms.size(); ++i)
  630. {
  631. Form* form = __forms[i];
  632. GP_ASSERT(form);
  633. if (form->isEnabled() && form->isVisible() && form->hasFocus())
  634. {
  635. if (form->gamepadEvent(evt, gamepad, analogIndex))
  636. return;
  637. }
  638. }
  639. }
  640. bool Form::projectPoint(int x, int y, Vector3* point)
  641. {
  642. Scene* scene = _node->getScene();
  643. Camera* camera;
  644. if (scene && (camera = scene->getActiveCamera()))
  645. {
  646. // Get info about the form's position.
  647. Matrix m = _node->getWorldMatrix();
  648. Vector3 pointOnPlane(0, 0, 0);
  649. m.transformPoint(&pointOnPlane);
  650. // Unproject point into world space.
  651. Ray ray;
  652. camera->pickRay(Game::getInstance()->getViewport(), x, y, &ray);
  653. // Find the quad's plane. We know its normal is the quad's forward vector.
  654. Vector3 normal = _node->getForwardVectorWorld().normalize();
  655. // To get the plane's distance from the origin, we project a point on the
  656. // plane onto the plane's normal vector.
  657. const float distance = fabs(Vector3::dot(pointOnPlane, normal));
  658. Plane plane(normal, -distance);
  659. // Check for collision with plane.
  660. float collisionDistance = ray.intersects(plane);
  661. if (collisionDistance != Ray::INTERSECTS_NONE)
  662. {
  663. // Multiply the ray's direction vector by collision distance and add that to the ray's origin.
  664. point->set(ray.getOrigin() + collisionDistance*ray.getDirection());
  665. // Project this point into the plane.
  666. m.invert();
  667. m.transformPoint(point);
  668. return true;
  669. }
  670. }
  671. return false;
  672. }
  673. unsigned int Form::nextPowerOfTwo(unsigned int v)
  674. {
  675. if (!((v & (v - 1)) == 0))
  676. {
  677. v--;
  678. v |= v >> 1;
  679. v |= v >> 2;
  680. v |= v >> 4;
  681. v |= v >> 8;
  682. v |= v >> 16;
  683. return v + 1;
  684. }
  685. else
  686. {
  687. return v;
  688. }
  689. }
  690. }