SamplesGame.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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::resizeEvent(unsigned int width, unsigned int height)
  102. {
  103. setViewport(gameplay::Rectangle(width, height));
  104. }
  105. void SamplesGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
  106. {
  107. if (_activeSample)
  108. {
  109. if (evt == Touch::TOUCH_PRESS && x >= ((int)getWidth() - 80) && y <= 80)
  110. {
  111. exitActiveSample();
  112. }
  113. else
  114. {
  115. getScriptController()->executeFunction<void>("camera_touchEvent", "[Touch::TouchEvent]iiui", evt, x, y, contactIndex);
  116. _activeSample->touchEvent(evt, x, y, contactIndex);
  117. }
  118. return;
  119. }
  120. }
  121. void SamplesGame::keyEvent(Keyboard::KeyEvent evt, int key)
  122. {
  123. if (_activeSample)
  124. {
  125. if (key == Keyboard::KEY_MENU || (evt == Keyboard::KEY_PRESS && (key == Keyboard::KEY_ESCAPE)))
  126. {
  127. // Pressing escape exits the active sample
  128. exitActiveSample();
  129. }
  130. else
  131. {
  132. getScriptController()->executeFunction<void>("camera_keyEvent", "[Keyboard::KeyEvent][Keyboard::Key]", evt, key);
  133. _activeSample->keyEvent(evt, key);
  134. }
  135. return;
  136. }
  137. if (evt == Keyboard::KEY_PRESS)
  138. {
  139. switch (key)
  140. {
  141. case Keyboard::KEY_ESCAPE:
  142. exit();
  143. break;
  144. }
  145. }
  146. }
  147. bool SamplesGame::mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta)
  148. {
  149. if (_activeSample)
  150. {
  151. return _activeSample->mouseEvent(evt, x, y, wheelDelta);
  152. }
  153. return false;
  154. }
  155. void SamplesGame::menuEvent()
  156. {
  157. exitActiveSample();
  158. }
  159. void SamplesGame::gestureSwipeEvent(int x, int y, int direction)
  160. {
  161. if (_activeSample)
  162. _activeSample->gestureSwipeEvent(x, y, direction);
  163. }
  164. void SamplesGame::gesturePinchEvent(int x, int y, float scale)
  165. {
  166. if (_activeSample)
  167. _activeSample->gesturePinchEvent(x, y, scale);
  168. }
  169. void SamplesGame::gestureTapEvent(int x, int y)
  170. {
  171. if (_activeSample)
  172. _activeSample->gestureTapEvent(x, y);
  173. }
  174. void SamplesGame::gestureLongTapEvent(int x, int y, float duration)
  175. {
  176. if (_activeSample)
  177. _activeSample->gestureLongTapEvent(x, y, duration);
  178. }
  179. void SamplesGame::gestureDragEvent(int x, int y)
  180. {
  181. if (_activeSample)
  182. _activeSample->gestureDragEvent(x, y);
  183. }
  184. void SamplesGame::gestureDropEvent(int x, int y)
  185. {
  186. if (_activeSample)
  187. _activeSample->gestureDropEvent(x, y);
  188. }
  189. void SamplesGame::controlEvent(Control* control, EventType evt)
  190. {
  191. const size_t size = _samples->size();
  192. for (size_t i = 0; i < size; ++i)
  193. {
  194. SampleRecordList list = (*_samples)[i];
  195. const size_t listSize = list.size();
  196. for (size_t j = 0; j < listSize; ++j)
  197. {
  198. SampleRecord sampleRecord = list[j];
  199. if (sampleRecord.title.compare(control->getId()) == 0)
  200. {
  201. _sampleSelectForm->setEnabled(false);
  202. runSample(sampleRecord.funcPtr);
  203. return;
  204. }
  205. }
  206. }
  207. }
  208. void SamplesGame::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex)
  209. {
  210. if (_activeSample)
  211. _activeSample->gamepadEvent(evt, gamepad);
  212. }
  213. void SamplesGame::runSample(void* func)
  214. {
  215. exitActiveSample();
  216. SampleGameCreatePtr p = (SampleGameCreatePtr)func;
  217. _activeSample = reinterpret_cast<Sample*>(p());
  218. _activeSample->initialize();
  219. resume();
  220. }
  221. void SamplesGame::exitActiveSample()
  222. {
  223. Gamepad* virtualGamepad = getGamepad(0, false);
  224. if (virtualGamepad && virtualGamepad->isVirtual())
  225. {
  226. virtualGamepad->getForm()->setEnabled(false);
  227. }
  228. if (_activeSample)
  229. {
  230. _activeSample->finalize();
  231. SAFE_DELETE(_activeSample);
  232. _sampleSelectForm->setEnabled(true);
  233. _sampleSelectForm->setFocus();
  234. }
  235. // Reset some game options
  236. setMultiTouch(false);
  237. }
  238. void SamplesGame::addSample(const char* category, const char* title, void* func, unsigned int order)
  239. {
  240. if (_samples == NULL)
  241. _samples = new std::vector<SampleRecordList>();
  242. if (_categories == NULL)
  243. {
  244. _categories = new std::vector<std::string>();
  245. _categories->push_back("Graphics");
  246. _categories->push_back("Scene");
  247. _categories->push_back("Input");
  248. _categories->push_back("Physics");
  249. _categories->push_back("Audio");
  250. _samples->resize(_categories->size());
  251. }
  252. string categoryString(category);
  253. string titleString(title);
  254. int index = -1;
  255. const int size = (int)_categories->size();
  256. for (int i = 0; i < size; ++i)
  257. {
  258. if ((*_categories)[i].compare(categoryString) == 0)
  259. {
  260. index = i;
  261. }
  262. }
  263. if (index < 0)
  264. {
  265. _categories->push_back(categoryString);
  266. index = (int)_categories->size() - 1;
  267. }
  268. if (index <= (int)_samples->size())
  269. {
  270. _samples->resize(_categories->size());
  271. }
  272. (*_samples)[index].push_back(SampleRecord(titleString, func, order));
  273. }
  274. SamplesGame* SamplesGame::getInstance()
  275. {
  276. return &game;
  277. }