Form.cpp 23 KB

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