SamplesGame.cpp 8.3 KB

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