Form.cpp 24 KB

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