FormsSample.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "FormsSample.h"
  2. #if defined(ADD_SAMPLE)
  3. ADD_SAMPLE("Graphics", "Forms", FormsSample, 10);
  4. #endif
  5. // Input bit-flags (powers of 2)
  6. #define KEY_A_MASK (1 << 0)
  7. #define KEY_B_MASK (1 << 1)
  8. #define KEY_SELECT_MASK (1 << 2)
  9. const static unsigned int __formsCount = 5;
  10. FormsSample::FormsSample()
  11. : _scene(NULL), _formNode(NULL), _formNodeParent(NULL), _formSelect(NULL), _activeForm(NULL), _gamepad(NULL), _keyFlags(0)
  12. {
  13. const char* formFiles[] =
  14. {
  15. "res/common/forms/formBasicControls.form",
  16. "res/common/forms/formScrolling.form",
  17. "res/common/forms/formFlowLayout.form",
  18. "res/common/forms/formVerticalLayout.form",
  19. "res/common/forms/formZOrder.form",
  20. };
  21. _formFiles.assign(formFiles, formFiles + __formsCount);
  22. }
  23. void FormsSample::finalize()
  24. {
  25. SAFE_RELEASE(_scene);
  26. SAFE_RELEASE(_formNode);
  27. SAFE_RELEASE(_formSelect);
  28. for (unsigned int i = 0; i < _forms.size(); i++)
  29. {
  30. SAFE_RELEASE(_forms[i]);
  31. }
  32. }
  33. void printProperties(Properties* properties, unsigned int tabCount)
  34. {
  35. // Print the name and ID of the current namespace.
  36. const char* spacename = properties->getNamespace();
  37. const char* id = properties->getId();
  38. std::string tabs;
  39. for (unsigned int i = 0; i < tabCount; i++)
  40. {
  41. tabs.append("\t");
  42. }
  43. GP_WARN("\n%s%s %s\n%s{", tabs.c_str(), spacename, id, tabs.c_str());
  44. // Print all properties in this namespace.
  45. const char* name = properties->getNextProperty();
  46. const char* value = NULL;
  47. while (name != NULL)
  48. {
  49. value = properties->getString(name);
  50. GP_WARN("%s\t%s = %s", tabs.c_str(), name, value);
  51. name = properties->getNextProperty();
  52. }
  53. // Print the properties of every namespace within this one.
  54. Properties* space = properties->getNextNamespace();
  55. while (space != NULL)
  56. {
  57. printProperties(space, tabCount+1);
  58. space = properties->getNextNamespace();
  59. }
  60. GP_WARN("%s}", tabs.c_str());
  61. }
  62. void FormsSample::initialize()
  63. {
  64. setMultiTouch(true);
  65. setVsync(false);
  66. _formSelect = Form::create("res/common/forms/formSelect.form");
  67. RadioButton* form0Button = static_cast<RadioButton*>(_formSelect->getControl("form0"));
  68. form0Button->addListener(this, Control::Listener::CLICK);
  69. form0Button->setState(Control::FOCUS);
  70. RadioButton* form1Button = static_cast<RadioButton*>(_formSelect->getControl("form1"));
  71. form1Button->addListener(this, Control::Listener::CLICK);
  72. RadioButton* form2Button = static_cast<RadioButton*>(_formSelect->getControl("form2"));
  73. form2Button->addListener(this, Control::Listener::CLICK);
  74. RadioButton* form3Button = static_cast<RadioButton*>(_formSelect->getControl("form3"));
  75. form3Button->addListener(this, Control::Listener::CLICK);
  76. RadioButton* form4Button = static_cast<RadioButton*>(_formSelect->getControl("form4"));
  77. form4Button->addListener(this, Control::Listener::CLICK);
  78. RadioButton* form5Button = static_cast<RadioButton*>(_formSelect->getControl("form5"));
  79. form5Button->addListener(this, Control::Listener::CLICK);
  80. for (unsigned int i = 0; i < _formFiles.size(); i++)
  81. {
  82. Form* form = Form::create(_formFiles[i]);
  83. form->setEnabled(false);
  84. _forms.push_back(form);
  85. }
  86. _formIndex = 0;
  87. // Create a form programmatically.
  88. createSampleForm(_forms[0]->getTheme());
  89. Button* button = static_cast<Button*>(_forms[0]->getControl("testButton"));
  90. button->setState(Control::FOCUS);
  91. // Create a scene with a camera node.
  92. Camera* camera = Camera::createPerspective(45.0f, (float)getWidth() / (float)getHeight(), 0.25f, 100.0f);
  93. _scene = Scene::create();
  94. Node* cameraNode = _scene->addNode("Camera");
  95. cameraNode->setCamera(camera);
  96. _scene->setActiveCamera(camera);
  97. SAFE_RELEASE(camera);
  98. _formNodeParent = _scene->addNode("FormParent");
  99. _formNode = Node::create("Form");
  100. _formNodeParent->addChild(_formNode);
  101. formChanged();
  102. _gamepad = getGamepad(0);
  103. // This is needed because the virtual gamepad is shared between all samples.
  104. // SamplesGame always ensures the virtual gamepad is disabled when a sample is exited.
  105. if (_gamepad && _gamepad->isVirtual())
  106. _gamepad->getForm()->setEnabled(true);
  107. }
  108. void FormsSample::formChanged()
  109. {
  110. if (_activeForm)
  111. _activeForm->setEnabled(false);
  112. _activeForm = _forms[_formIndex];
  113. _activeForm->setEnabled(true);
  114. _activeForm->setState(Control::FOCUS);
  115. _formSelect->setState(Control::NORMAL);
  116. // Add the form to a node to allow it to be placed in 3D space.
  117. const Rectangle& bounds = _activeForm->getBounds();
  118. float scale;
  119. if (bounds.width >= bounds.height)
  120. {
  121. scale = 1.0f / bounds.width;
  122. }
  123. else
  124. {
  125. scale = 1.0f / bounds.height;
  126. }
  127. _formNode->setScale(scale, scale, 1.0f);
  128. _formNodeParent->setTranslation(0, 0, -1.5f);
  129. _formNode->setTranslation(-0.5f, -0.5f, 0);
  130. _formNode->setForm(_activeForm);
  131. }
  132. void FormsSample::createSampleForm(Theme* theme)
  133. {
  134. Form* form = Form::create("testForm", theme->getStyle("buttonStyle"));
  135. form->setSize(600, 600);
  136. Label* label = Label::create("testLabel", theme->getStyle("iconNoBorder"));
  137. label->setPosition(50, 50);
  138. label->setSize(200, 50);
  139. label->setText("Label:");
  140. form->addControl(label);
  141. label->release();
  142. Button* button = Button::create("opacityButton", theme->getStyle("buttonStyle"));
  143. button->setPosition(45, 100);
  144. button->setSize(200, 100);
  145. button->setText("This is a button. Click to change its opacity.");
  146. button->addListener(this, Control::Listener::CLICK);
  147. form->addControl(button);
  148. button->release();
  149. form->setEnabled(false);
  150. _forms.push_back(form);
  151. }
  152. void FormsSample::update(float elapsedTime)
  153. {
  154. float speedFactor = 0.001f * elapsedTime;
  155. bool aDown = (_keyFlags & KEY_A_MASK);
  156. bool bDown = (_keyFlags & KEY_B_MASK);
  157. // If no form is in focus, then we poll the gamepad for movement input.
  158. if (_activeForm->getState() == Control::NORMAL &&
  159. _formSelect->getState() == Control::NORMAL)
  160. {
  161. if (_gamepad->isButtonDown(Gamepad::BUTTON_A))
  162. _keyFlags |= KEY_A_MASK;
  163. else
  164. _keyFlags &= ~KEY_A_MASK;
  165. if (_gamepad->isButtonDown(Gamepad::BUTTON_B))
  166. _keyFlags |= KEY_B_MASK;
  167. else
  168. _keyFlags &= ~KEY_B_MASK;
  169. _gamepad->getJoystickValues(0, &_joysticks[0]);
  170. _gamepad->getJoystickValues(1, &_joysticks[1]);
  171. }
  172. // We'll use a physical gamepad's MENU1 button as the "back" button.
  173. if (!(_keyFlags & KEY_SELECT_MASK) && _gamepad->isButtonDown(Gamepad::BUTTON_MENU1))
  174. {
  175. _keyFlags |= KEY_SELECT_MASK;
  176. if (_formSelect->getState() == Control::FOCUS)
  177. {
  178. _formSelect->setState(Control::NORMAL);
  179. }
  180. else if (_activeForm->getState() == Control::FOCUS)
  181. {
  182. _activeForm->setState(Control::NORMAL);
  183. _formSelect->setState(Control::FOCUS);
  184. }
  185. else
  186. {
  187. _formSelect->setState(Control::FOCUS);
  188. }
  189. }
  190. else if ((_keyFlags & KEY_SELECT_MASK) && !_gamepad->isButtonDown(Gamepad::BUTTON_MENU1))
  191. {
  192. _keyFlags &= ~KEY_SELECT_MASK;
  193. }
  194. if (_gamepad->isVirtual())
  195. {
  196. aDown |= _gamepad->isButtonDown(Gamepad::BUTTON_A);
  197. bDown |= _gamepad->isButtonDown(Gamepad::BUTTON_B);
  198. _gamepad->getJoystickValues(0, &_joysticks[0]);
  199. }
  200. if (!_joysticks[0].isZero())
  201. {
  202. _gamepad->getJoystickValues(0, &_joysticks[0]);
  203. }
  204. if (!_joysticks[1].isZero())
  205. {
  206. Matrix m;
  207. _formNodeParent->getWorldMatrix().transpose(&m);
  208. Vector3 yaw;
  209. m.getUpVector(&yaw);
  210. _formNodeParent->rotate(yaw, speedFactor * _joysticks[1].x * 2.0f);
  211. _formNodeParent->rotateX(-speedFactor * _joysticks[1].y * 2.0f);
  212. }
  213. if (bDown)
  214. {
  215. _formNodeParent->setRotation(0, 0, 0, 1);
  216. }
  217. else if (aDown)
  218. {
  219. // Yaw in world frame, pitch in body frame
  220. Matrix m;
  221. _formNodeParent->getWorldMatrix().transpose(&m);
  222. Vector3 yaw;
  223. m.getUpVector(&yaw);
  224. _formNodeParent->rotate(yaw, speedFactor * _joysticks[0].x);
  225. _formNodeParent->rotateX(-speedFactor * _joysticks[0].y);
  226. }
  227. else
  228. {
  229. _formNodeParent->translate(0.5f * speedFactor * _joysticks[0].x, 0.5f * speedFactor * _joysticks[0].y, 0);
  230. }
  231. }
  232. void FormsSample::render(float elapsedTime)
  233. {
  234. // Clear the screen.
  235. clear(CLEAR_COLOR_DEPTH, Vector4(0, 0, 0, 1), 1.0f, 0);
  236. // Draw the forms.
  237. if (_formSelect)
  238. {
  239. _formSelect->draw();
  240. }
  241. if (_activeForm)
  242. {
  243. _activeForm->draw();
  244. }
  245. _gamepad->draw();
  246. }
  247. void FormsSample::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  248. {
  249. if (_formNode)
  250. {
  251. switch (evt)
  252. {
  253. case Touch::TOUCH_PRESS:
  254. {
  255. _touched = true;
  256. _touchX = x;
  257. }
  258. break;
  259. case Touch::TOUCH_RELEASE:
  260. {
  261. _touched = false;
  262. _touchX = 0;
  263. }
  264. break;
  265. case Touch::TOUCH_MOVE:
  266. {
  267. int deltaX = x - _touchX;
  268. _touchX = x;
  269. // Yaw in world frame
  270. Matrix m;
  271. _formNodeParent->getWorldMatrix().transpose(&m);
  272. Vector3 yaw;
  273. m.getUpVector(&yaw);
  274. _formNodeParent->rotate(yaw, MATH_DEG_TO_RAD(deltaX * 0.5f));
  275. }
  276. break;
  277. default:
  278. break;
  279. };
  280. }
  281. }
  282. void FormsSample::keyEvent(Keyboard::KeyEvent keyEvent, int key)
  283. {
  284. if (_formNode)
  285. {
  286. switch(keyEvent)
  287. {
  288. case Keyboard::KEY_PRESS:
  289. switch (key)
  290. {
  291. case Keyboard::KEY_LEFT_ARROW:
  292. _formNodeParent->translateX(-0.1f);
  293. break;
  294. case Keyboard::KEY_RIGHT_ARROW:
  295. _formNodeParent->translateX(0.1f);
  296. break;
  297. case Keyboard::KEY_UP_ARROW:
  298. _formNodeParent->translateY(0.1f);
  299. break;
  300. case Keyboard::KEY_DOWN_ARROW:
  301. _formNodeParent->translateY(-0.1f);
  302. break;
  303. case Keyboard::KEY_PLUS:
  304. _formNodeParent->translateZ(0.1f);
  305. break;
  306. case Keyboard::KEY_MINUS:
  307. _formNodeParent->translateZ(-0.1f);
  308. break;
  309. case Keyboard::KEY_A:
  310. case Keyboard::KEY_CAPITAL_A:
  311. _keyFlags |= KEY_A_MASK;
  312. break;
  313. case Keyboard::KEY_B:
  314. case Keyboard::KEY_CAPITAL_B:
  315. _keyFlags |= KEY_B_MASK;
  316. break;
  317. }
  318. break;
  319. case Keyboard::KEY_RELEASE:
  320. switch (key)
  321. {
  322. case Keyboard::KEY_A:
  323. case Keyboard::KEY_CAPITAL_A:
  324. _keyFlags &= ~KEY_A_MASK;
  325. break;
  326. case Keyboard::KEY_B:
  327. case Keyboard::KEY_CAPITAL_B:
  328. _keyFlags &= ~KEY_B_MASK;
  329. break;
  330. }
  331. break;
  332. }
  333. }
  334. }
  335. void FormsSample::controlEvent(Control* control, EventType evt)
  336. {
  337. if (strcmp("form0", control->getId()) == 0)
  338. {
  339. _formIndex = 0;
  340. formChanged();
  341. }
  342. else if (strcmp("form1", control->getId()) == 0)
  343. {
  344. _formIndex = 1;
  345. formChanged();
  346. }
  347. else if (strcmp("form2", control->getId()) == 0)
  348. {
  349. _formIndex = 2;
  350. formChanged();
  351. }
  352. else if (strcmp("form3", control->getId()) == 0)
  353. {
  354. _formIndex = 3;
  355. formChanged();
  356. }
  357. else if (strcmp("form4", control->getId()) == 0)
  358. {
  359. _formIndex = 4;
  360. formChanged();
  361. }
  362. else if (strcmp("form5", control->getId()) == 0)
  363. {
  364. _formIndex = 5;
  365. formChanged();
  366. }
  367. else if (strcmp("opacityButton", control->getId()) == 0)
  368. {
  369. float from[] = { 1.0f };
  370. float to[] = { 0.5f };
  371. control->createAnimationFromTo("opacityButton", Form::ANIMATE_OPACITY, from, to, Curve::LINEAR, 1000)->getClip()->play();
  372. }
  373. }
  374. void FormsSample::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad)
  375. {
  376. switch(evt)
  377. {
  378. case Gamepad::CONNECTED_EVENT:
  379. case Gamepad::DISCONNECTED_EVENT:
  380. _gamepad = getGamepad(0);
  381. // This is needed because the virtual gamepad is shared between all samples.
  382. // SamplesGame always ensures the virtual gamepad is disabled when a sample is exited.
  383. if (_gamepad && _gamepad->isVirtual())
  384. _gamepad->getForm()->setEnabled(true);
  385. break;
  386. }
  387. }