SamplesGame.cpp 8.8 KB

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