SamplesGame.cpp 8.1 KB

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