Form.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  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_RELEASE(_node);
  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", true);
  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. updateBounds();
  336. }
  337. void Form::updateBounds()
  338. {
  339. if (isDirty())
  340. {
  341. _clearBounds.set(_absoluteClipBounds);
  342. // Calculate the clipped bounds.
  343. float x = 0;
  344. float y = 0;
  345. float width = _bounds.width;
  346. float height = _bounds.height;
  347. Rectangle clip(0, 0, _bounds.width, _bounds.height);
  348. float clipX2 = clip.x + clip.width;
  349. float x2 = clip.x + x + width;
  350. if (x2 > clipX2)
  351. width -= x2 - clipX2;
  352. float clipY2 = clip.y + clip.height;
  353. float y2 = clip.y + y + height;
  354. if (y2 > clipY2)
  355. height -= y2 - clipY2;
  356. if (x < 0)
  357. {
  358. width += x;
  359. x = -x;
  360. }
  361. else
  362. {
  363. x = 0;
  364. }
  365. if (y < 0)
  366. {
  367. height += y;
  368. y = -y;
  369. }
  370. else
  371. {
  372. y = 0;
  373. }
  374. _clipBounds.set(x, y, width, height);
  375. // Calculate the absolute bounds.
  376. x = 0;
  377. y = 0;
  378. _absoluteBounds.set(x, y, _bounds.width, _bounds.height);
  379. // Calculate the absolute viewport bounds. Absolute bounds minus border and padding.
  380. const Theme::Border& border = getBorder(_state);
  381. const Theme::Padding& padding = getPadding();
  382. x += border.left + padding.left;
  383. y += border.top + padding.top;
  384. width = _bounds.width - border.left - padding.left - border.right - padding.right;
  385. height = _bounds.height - border.top - padding.top - border.bottom - padding.bottom;
  386. _viewportBounds.set(x, y, width, height);
  387. // Calculate the clip area. Absolute bounds, minus border and padding, clipped to the parent container's clip area.
  388. clipX2 = clip.x + clip.width;
  389. x2 = x + width;
  390. if (x2 > clipX2)
  391. width = clipX2 - x;
  392. clipY2 = clip.y + clip.height;
  393. y2 = y + height;
  394. if (y2 > clipY2)
  395. height = clipY2 - y;
  396. if (x < clip.x)
  397. {
  398. float dx = clip.x - x;
  399. width -= dx;
  400. x = clip.x;
  401. }
  402. if (y < clip.y)
  403. {
  404. float dy = clip.y - y;
  405. height -= dy;
  406. y = clip.y;
  407. }
  408. _viewportClipBounds.set(x, y, width, height);
  409. _absoluteClipBounds.set(x - border.left - padding.left, y - border.top - padding.top,
  410. width + border.left + padding.left + border.right + padding.right,
  411. height + border.top + padding.top + border.bottom + padding.bottom);
  412. if (_clearBounds.isEmpty())
  413. {
  414. _clearBounds.set(_absoluteClipBounds);
  415. }
  416. // Cache themed attributes for performance.
  417. _skin = getSkin(_state);
  418. _opacity = getOpacity(_state);
  419. // Get scrollbar images and diminish clipping bounds to make room for scrollbars.
  420. if ((_scroll & SCROLL_HORIZONTAL) == SCROLL_HORIZONTAL)
  421. {
  422. _scrollBarLeftCap = getImage("scrollBarLeftCap", _state);
  423. _scrollBarHorizontal = getImage("horizontalScrollBar", _state);
  424. _scrollBarRightCap = getImage("scrollBarRightCap", _state);
  425. _viewportClipBounds.height -= _scrollBarHorizontal->getRegion().height;
  426. }
  427. if ((_scroll & SCROLL_VERTICAL) == SCROLL_VERTICAL)
  428. {
  429. _scrollBarTopCap = getImage("scrollBarTopCap", _state);
  430. _scrollBarVertical = getImage("verticalScrollBar", _state);
  431. _scrollBarBottomCap = getImage("scrollBarBottomCap", _state);
  432. _viewportClipBounds.width -= _scrollBarVertical->getRegion().width;
  433. }
  434. GP_ASSERT(_layout);
  435. if (_scroll != SCROLL_NONE)
  436. updateScroll();
  437. else
  438. _layout->update(this, Vector2::zero());
  439. }
  440. }
  441. void Form::draw()
  442. {
  443. // The first time a form is drawn, its contents are rendered into a framebuffer.
  444. // The framebuffer will only be drawn into again when the contents of the form change.
  445. // If this form has a node then it's a 3D form and the framebuffer will be used
  446. // to texture a quad. The quad will be given the same dimensions as the form and
  447. // must be transformed appropriately by the user, unless they call setQuad() themselves.
  448. // On the other hand, if this form has not been set on a node, SpriteBatch will be used
  449. // to render the contents of the framebuffer directly to the display.
  450. // Check whether this form has changed since the last call to draw() and if so, render into the framebuffer.
  451. if (isDirty())
  452. {
  453. GP_ASSERT(_frameBuffer);
  454. FrameBuffer* previousFrameBuffer = _frameBuffer->bind();
  455. Game* game = Game::getInstance();
  456. Rectangle prevViewport = game->getViewport();
  457. game->setViewport(Rectangle(0, 0, _bounds.width, _bounds.height));
  458. GP_ASSERT(_theme);
  459. _theme->setProjectionMatrix(_projectionMatrix);
  460. Container::draw(_theme->getSpriteBatch(), Rectangle(0, 0, _bounds.width, _bounds.height), true/*WAS _skin!=NULL*/, false, _bounds.height);
  461. _theme->setProjectionMatrix(_defaultProjectionMatrix);
  462. // Rebind the previous framebuffer and game viewport.
  463. previousFrameBuffer->bind();
  464. // restore the previous game viewport
  465. game->setViewport(prevViewport);
  466. }
  467. // Draw either with a 3D quad or sprite batch
  468. if (_node)
  469. {
  470. // If we have the node set, then draw a 3D quad model
  471. _nodeQuad->draw();
  472. }
  473. else
  474. {
  475. // Otherwise we draw the framebuffer in ortho space with a spritebatch.
  476. if (!_spriteBatch)
  477. {
  478. _spriteBatch = SpriteBatch::create(_frameBuffer->getRenderTarget()->getTexture());
  479. GP_ASSERT(_spriteBatch);
  480. }
  481. _spriteBatch->start();
  482. _spriteBatch->draw(_bounds.x, _bounds.y, 0, _bounds.width, _bounds.height, 0, _v1, _u2, 0, Vector4::one());
  483. _spriteBatch->finish();
  484. }
  485. }
  486. const char* Form::getType() const
  487. {
  488. return "form";
  489. }
  490. bool Form::touchEventInternal(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  491. {
  492. // Check for a collision with each Form in __forms.
  493. // Pass the event on.
  494. bool eventConsumed = false;
  495. size_t size = __forms.size();
  496. for (size_t i = 0; i < size; ++i)
  497. {
  498. Form* form = __forms[i];
  499. GP_ASSERT(form);
  500. if (form->isEnabled() && form->isVisible())
  501. {
  502. if (form->_node)
  503. {
  504. Vector3 point;
  505. if (form->projectPoint(x, y, &point))
  506. {
  507. const Rectangle& bounds = form->getBounds();
  508. if (form->getState() == Control::FOCUS ||
  509. (evt == Touch::TOUCH_PRESS &&
  510. point.x >= bounds.x &&
  511. point.x <= bounds.x + bounds.width &&
  512. point.y >= bounds.y &&
  513. point.y <= bounds.y + bounds.height))
  514. {
  515. eventConsumed |= form->touchEvent(evt, point.x - bounds.x, bounds.height - point.y - bounds.y, contactIndex);
  516. }
  517. }
  518. }
  519. else
  520. {
  521. // Simply compare with the form's bounds.
  522. const Rectangle& bounds = form->getBounds();
  523. if (form->getState() == Control::FOCUS ||
  524. (evt == Touch::TOUCH_PRESS &&
  525. x >= bounds.x &&
  526. x <= bounds.x + bounds.width &&
  527. y >= bounds.y &&
  528. y <= bounds.y + bounds.height))
  529. {
  530. // Pass on the event's position relative to the form.
  531. eventConsumed |= form->touchEvent(evt, x - bounds.x, y - bounds.y, contactIndex);
  532. }
  533. }
  534. }
  535. }
  536. return eventConsumed;
  537. }
  538. bool Form::keyEventInternal(Keyboard::KeyEvent evt, int key)
  539. {
  540. size_t size = __forms.size();
  541. for (size_t i = 0; i < size; ++i)
  542. {
  543. Form* form = __forms[i];
  544. GP_ASSERT(form);
  545. if (form->isEnabled() && form->isVisible())
  546. {
  547. if (form->keyEvent(evt, key))
  548. return true;
  549. }
  550. }
  551. return false;
  552. }
  553. bool Form::mouseEventInternal(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  554. {
  555. bool eventConsumed = false;
  556. for (size_t i = 0; i < __forms.size(); ++i)
  557. {
  558. Form* form = __forms[i];
  559. GP_ASSERT(form);
  560. if (form->isEnabled() && form->isVisible())
  561. {
  562. if (form->_node)
  563. {
  564. Vector3 point;
  565. if (form->projectPoint(x, y, &point))
  566. {
  567. const Rectangle& bounds = form->getBounds();
  568. if (form->getState() == Control::FOCUS ||
  569. ((evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
  570. evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
  571. evt == Mouse::MOUSE_PRESS_RIGHT_BUTTON ||
  572. evt == Mouse::MOUSE_WHEEL) &&
  573. point.x >= bounds.x &&
  574. point.x <= bounds.x + bounds.width &&
  575. point.y >= bounds.y &&
  576. point.y <= bounds.y + bounds.height))
  577. {
  578. eventConsumed |= form->mouseEvent(evt, point.x - bounds.x, bounds.height - point.y - bounds.y, wheelDelta);
  579. }
  580. }
  581. }
  582. else
  583. {
  584. // Simply compare with the form's bounds.
  585. const Rectangle& bounds = form->getBounds();
  586. if (form->getState() == Control::FOCUS ||
  587. ((evt == Mouse::MOUSE_PRESS_LEFT_BUTTON ||
  588. evt == Mouse::MOUSE_PRESS_MIDDLE_BUTTON ||
  589. evt == Mouse::MOUSE_PRESS_RIGHT_BUTTON ||
  590. evt == Mouse::MOUSE_WHEEL) &&
  591. x >= bounds.x &&
  592. x <= bounds.x + bounds.width &&
  593. y >= bounds.y &&
  594. y <= bounds.y + bounds.height))
  595. {
  596. // Pass on the event's position relative to the form.
  597. eventConsumed |= form->mouseEvent(evt, x - bounds.x, y - bounds.y, wheelDelta);
  598. }
  599. }
  600. }
  601. }
  602. return eventConsumed;
  603. }
  604. bool Form::projectPoint(int x, int y, Vector3* point)
  605. {
  606. Scene* scene = _node->getScene();
  607. Camera* camera;
  608. if (scene && (camera = scene->getActiveCamera()))
  609. {
  610. // Get info about the form's position.
  611. Matrix m = _node->getWorldMatrix();
  612. Vector3 min(0, 0, 0);
  613. m.transformPoint(&min);
  614. // Unproject point into world space.
  615. Ray ray;
  616. camera->pickRay(Game::getInstance()->getViewport(), x, y, &ray);
  617. // Find the quad's plane. We know its normal is the quad's forward vector.
  618. Vector3 normal = _node->getForwardVectorWorld();
  619. // To get the plane's distance from the origin, we'll find the distance from the plane defined
  620. // by the quad's forward vector and one of its points to the plane defined by the same vector and the origin.
  621. const float& a = normal.x; const float& b = normal.y; const float& c = normal.z;
  622. const float d = -(a*min.x) - (b*min.y) - (c*min.z);
  623. const float distance = fabs(d) / sqrt(a*a + b*b + c*c);
  624. Plane plane(normal, -distance);
  625. // Check for collision with plane.
  626. float collisionDistance = ray.intersects(plane);
  627. if (collisionDistance != Ray::INTERSECTS_NONE)
  628. {
  629. // Multiply the ray's direction vector by collision distance and add that to the ray's origin.
  630. point->set(ray.getOrigin() + collisionDistance*ray.getDirection());
  631. // Project this point into the plane.
  632. m.invert();
  633. m.transformPoint(point);
  634. return true;
  635. }
  636. }
  637. return false;
  638. }
  639. unsigned int Form::nextPowerOfTwo(unsigned int v)
  640. {
  641. if (!((v & (v - 1)) == 0))
  642. {
  643. v--;
  644. v |= v >> 1;
  645. v |= v >> 2;
  646. v |= v >> 4;
  647. v |= v >> 8;
  648. v |= v >> 16;
  649. return v + 1;
  650. }
  651. else
  652. {
  653. return v;
  654. }
  655. }
  656. }