SamplesGame.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include "SamplesGame.h"
  2. using std::string;
  3. using std::pair;
  4. std::vector<std::string>* SamplesGame::_categories = NULL;
  5. std::vector<SamplesGame::SampleRecordList>* SamplesGame::_samples = NULL;
  6. // Declare our game instance
  7. SamplesGame game;
  8. SamplesGame::SamplesGame()
  9. : _activeSample(NULL), _font(NULL), _sampleSelectForm(NULL)
  10. {
  11. }
  12. void SamplesGame::initialize()
  13. {
  14. _font = Font::create("res/ui/arial.gpb");
  15. for (size_t i = 0; i < _categories->size(); ++i)
  16. {
  17. std::sort((*_samples)[i].begin(), (*_samples)[i].end());
  18. }
  19. // Load camera script
  20. getScriptController()->loadScript("res/common/camera.lua");
  21. // Create the selection form
  22. _sampleSelectForm = Form::create("sampleSelect", NULL, Layout::LAYOUT_VERTICAL);
  23. _sampleSelectForm->setWidth(200);
  24. _sampleSelectForm->setAutoHeight(Control::AUTO_SIZE_STRETCH);
  25. _sampleSelectForm->setScroll(Container::SCROLL_VERTICAL);
  26. const size_t size = _samples->size();
  27. for (size_t i = 0; i < size; ++i)
  28. {
  29. Label* categoryLabel = Label::create((*_categories)[i].c_str());
  30. categoryLabel->setAutoWidth(Control::AUTO_SIZE_FIT);
  31. categoryLabel->setAutoHeight(Control::AUTO_SIZE_FIT);
  32. categoryLabel->setFontSize(22);
  33. categoryLabel->setText((*_categories)[i].c_str());
  34. _sampleSelectForm->addControl(categoryLabel);
  35. categoryLabel->release();
  36. SampleRecordList list = (*_samples)[i];
  37. const size_t listSize = list.size();
  38. for (size_t j = 0; j < listSize; ++j)
  39. {
  40. SampleRecord sampleRecord = list[j];
  41. Button* sampleButton = Button::create(sampleRecord.title.c_str());
  42. sampleButton->setText(sampleRecord.title.c_str());
  43. sampleButton->setAutoWidth(Control::AUTO_SIZE_STRETCH);
  44. sampleButton->setHeight(50);
  45. sampleButton->addListener(this, Control::Listener::CLICK);
  46. _sampleSelectForm->addControl(sampleButton);
  47. sampleButton->release();
  48. }
  49. }
  50. _sampleSelectForm->setFocus();
  51. // Disable virtual gamepads.
  52. unsigned int gamepadCount = getGamepadCount();
  53. for (unsigned int i = 0; i < gamepadCount; i++)
  54. {
  55. Gamepad* gamepad = getGamepad(i, false);
  56. if (gamepad->isVirtual())
  57. {
  58. gamepad->getForm()->setEnabled(false);
  59. }
  60. }
  61. }
  62. void SamplesGame::finalize()
  63. {
  64. SAFE_RELEASE(_font);
  65. if (_activeSample)
  66. _activeSample->finalize();
  67. SAFE_DELETE(_activeSample);
  68. SAFE_DELETE(_categories);
  69. SAFE_DELETE(_samples);
  70. SAFE_RELEASE(_sampleSelectForm);
  71. }
  72. void SamplesGame::update(float elapsedTime)
  73. {
  74. if (_activeSample)
  75. {
  76. Gamepad* gamepad = getGamepad(0);
  77. if (gamepad && gamepad->isButtonDown(Gamepad::BUTTON_MENU2))
  78. {
  79. exitActiveSample();
  80. return;
  81. }
  82. getScriptController()->executeFunction<void>("camera_update", "f", elapsedTime);
  83. _activeSample->update(elapsedTime);
  84. return;
  85. }
  86. _sampleSelectForm->update(elapsedTime);
  87. }
  88. void SamplesGame::render(float elapsedTime)
  89. {
  90. if (_activeSample)
  91. {
  92. _activeSample->render(elapsedTime);
  93. // Draw back arrow
  94. _font->start();
  95. _font->drawText("<<", getWidth() - 40, 20, Vector4::one(), 28);
  96. _font->finish();
  97. return;
  98. }
  99. // Clear the color and depth buffers
  100. clear(CLEAR_COLOR_DEPTH, Vector4::zero(), 1.0f, 0);
  101. _sampleSelectForm->draw();
  102. }
  103. void SamplesGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  104. {
  105. if (_activeSample)
  106. {
  107. if (evt == Touch::TOUCH_PRESS && x >= ((int)getWidth() - 80) && y <= 80)
  108. {
  109. exitActiveSample();
  110. }
  111. else
  112. {
  113. getScriptController()->executeFunction<void>("camera_touchEvent", "[Touch::TouchEvent]iiui", evt, x, y, contactIndex);
  114. _activeSample->touchEvent(evt, x, y, contactIndex);
  115. }
  116. return;
  117. }
  118. }
  119. void SamplesGame::keyEvent(Keyboard::KeyEvent evt, int key)
  120. {
  121. if (_activeSample)
  122. {
  123. if (key == Keyboard::KEY_MENU || (evt == Keyboard::KEY_PRESS && (key == Keyboard::KEY_ESCAPE)))
  124. {
  125. // Pressing escape exits the active sample
  126. exitActiveSample();
  127. }
  128. else
  129. {
  130. getScriptController()->executeFunction<void>("camera_keyEvent", "[Keyboard::KeyEvent][Keyboard::Key]", evt, key);
  131. _activeSample->keyEvent(evt, key);
  132. }
  133. return;
  134. }
  135. if (evt == Keyboard::KEY_PRESS)
  136. {
  137. switch (key)
  138. {
  139. case Keyboard::KEY_ESCAPE:
  140. exit();
  141. break;
  142. }
  143. }
  144. }
  145. bool SamplesGame::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  146. {
  147. if (_activeSample)
  148. {
  149. return _activeSample->mouseEvent(evt, x, y, wheelDelta);
  150. }
  151. return false;
  152. }
  153. void SamplesGame::menuEvent()
  154. {
  155. exitActiveSample();
  156. }
  157. void SamplesGame::gestureSwipeEvent(int x, int y, int direction)
  158. {
  159. if (_activeSample)
  160. _activeSample->gestureSwipeEvent(x, y, direction);
  161. }
  162. void SamplesGame::gesturePinchEvent(int x, int y, float scale)
  163. {
  164. if (_activeSample)
  165. _activeSample->gesturePinchEvent(x, y, scale);
  166. }
  167. void SamplesGame::gestureTapEvent(int x, int y)
  168. {
  169. if (_activeSample)
  170. _activeSample->gestureTapEvent(x, y);
  171. }
  172. void SamplesGame::gestureLongTapEvent(int x, int y, float duration)
  173. {
  174. if (_activeSample)
  175. _activeSample->gestureLongTapEvent(x, y, duration);
  176. }
  177. void SamplesGame::gestureDragEvent(int x, int y)
  178. {
  179. if (_activeSample)
  180. _activeSample->gestureDragEvent(x, y);
  181. }
  182. void SamplesGame::gestureDropEvent(int x, int y)
  183. {
  184. if (_activeSample)
  185. _activeSample->gestureDropEvent(x, y);
  186. }
  187. void SamplesGame::controlEvent(Control* control, EventType evt)
  188. {
  189. const size_t size = _samples->size();
  190. for (size_t i = 0; i < size; ++i)
  191. {
  192. SampleRecordList list = (*_samples)[i];
  193. const size_t listSize = list.size();
  194. for (size_t j = 0; j < listSize; ++j)
  195. {
  196. SampleRecord sampleRecord = list[j];
  197. if (sampleRecord.title.compare(control->getId()) == 0)
  198. {
  199. _sampleSelectForm->setEnabled(false);
  200. runSample(sampleRecord.funcPtr);
  201. return;
  202. }
  203. }
  204. }
  205. }
  206. void SamplesGame::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  207. {
  208. if (_activeSample)
  209. _activeSample->gamepadEvent(evt, gamepad);
  210. }
  211. void SamplesGame::runSample(void* func)
  212. {
  213. exitActiveSample();
  214. SampleGameCreatePtr p = (SampleGameCreatePtr)func;
  215. _activeSample = reinterpret_cast<Sample*>(p());
  216. _activeSample->initialize();
  217. resume();
  218. }
  219. void SamplesGame::exitActiveSample()
  220. {
  221. Gamepad* virtualGamepad = getGamepad(0, false);
  222. if (virtualGamepad && virtualGamepad->isVirtual())
  223. {
  224. virtualGamepad->getForm()->setEnabled(false);
  225. }
  226. if (_activeSample)
  227. {
  228. _activeSample->finalize();
  229. SAFE_DELETE(_activeSample);
  230. _sampleSelectForm->setEnabled(true);
  231. _sampleSelectForm->setFocus();
  232. }
  233. // Reset some game options
  234. setMultiTouch(false);
  235. }
  236. void SamplesGame::addSample(const char* category, const char* title, void* func, unsigned int order)
  237. {
  238. if (_samples == NULL)
  239. _samples = new std::vector<SampleRecordList>();
  240. if (_categories == NULL)
  241. {
  242. _categories = new std::vector<std::string>();
  243. _categories->push_back("Graphics");
  244. _categories->push_back("Scene");
  245. _categories->push_back("Input");
  246. _categories->push_back("Physics");
  247. _categories->push_back("Audio");
  248. _samples->resize(_categories->size());
  249. }
  250. string categoryString(category);
  251. string titleString(title);
  252. int index = -1;
  253. const int size = (int)_categories->size();
  254. for (int i = 0; i < size; ++i)
  255. {
  256. if ((*_categories)[i].compare(categoryString) == 0)
  257. {
  258. index = i;
  259. }
  260. }
  261. if (index < 0)
  262. {
  263. _categories->push_back(categoryString);
  264. index = (int)_categories->size() - 1;
  265. }
  266. if (index <= (int)_samples->size())
  267. {
  268. _samples->resize(_categories->size());
  269. }
  270. (*_samples)[index].push_back(SampleRecord(titleString, func, order));
  271. }
  272. SamplesGame* SamplesGame::getInstance()
  273. {
  274. return &game;
  275. }