Form.cpp 23 KB

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