FormsSample.cpp 11 KB

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