SampleSelector.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/IO/Log.h>
  24. #include <Atomic/Graphics/Renderer.h>
  25. #include <Atomic/UI/UI.h>
  26. #include <Atomic/UI/UIEvents.h>
  27. #include <Atomic/UI/UIView.h>
  28. #include <Atomic/UI/UILayout.h>
  29. #include <Atomic/UI/UIButton.h>
  30. #include "FeatureExamples.h"
  31. #include "SampleSelector.h"
  32. #include "HelloWorld.h"
  33. #include "HelloGui.h"
  34. #include "2DSprite.h"
  35. #include "Physics2D.h"
  36. #include "Constraints2D.h"
  37. #include "StaticScene.h"
  38. #include "AnimatingScene.h"
  39. #include "SkeletalAnimation.h"
  40. #include "CharacterDemo.h"
  41. #include "Physics3D.h"
  42. #include "Ragdolls.h"
  43. #include "VehicleDemo.h"
  44. #include "CrowdNavigation.h"
  45. #include "DynamicGeometry.h"
  46. #include "Water.h"
  47. #include "Billboards.h"
  48. #include "MultipleViewports.h"
  49. #include "Decals.h"
  50. #include "PhysicsRope2D.h"
  51. #include "RenderToTexture.h"
  52. #include "SpriterAnimation.h"
  53. #include "LightAnimation.h"
  54. #include "Particles3D.h"
  55. #include "SignedDistanceFieldText.h"
  56. SampleSelector::SampleSelector(Context* context) :
  57. Object(context)
  58. {
  59. constructionFrame_ = GetSubsystem<Renderer>()->GetFrameInfo().frameNumber_;
  60. UIView* view = FeatureExamples::GetUIView();
  61. UILayout* rootLayout = new UILayout(context_);
  62. rootLayout->SetAxis(UI_AXIS_Y);
  63. rootLayout->SetRect(view->GetRect());
  64. view->AddChild(rootLayout);
  65. const char* examples[] = {
  66. "Hello World",
  67. "Hello GUI",
  68. "Render to Texture",
  69. "2D Sprite",
  70. "2D Physics",
  71. "2D Constraints",
  72. "2D Rope",
  73. "2D Spriter Animation",
  74. "3D Static Scene",
  75. "3D Animating Scene",
  76. "3D Light Animation",
  77. "3D Signed Distance Field Text",
  78. "3D Billboards",
  79. "3D Particles",
  80. "3D Physics",
  81. "3D Skeletal Animation",
  82. "3D Decals",
  83. "3D Character",
  84. "3D Dynamic Geometry",
  85. "3D Ragdolls",
  86. "3D Vehicle Demo",
  87. "3D Crowd Navigation",
  88. "3D Water",
  89. "3D Multiple Viewports"
  90. };
  91. for (size_t i = 0; i < sizeof(examples) / sizeof(examples[0]); i++)
  92. {
  93. UIButton* button = new UIButton(context_);
  94. button->SetLayoutMinWidth(128);
  95. button->SetText(examples[i]);
  96. button->SetId(examples[i]);
  97. button->SubscribeToEvent(button, E_WIDGETEVENT, ATOMIC_HANDLER(SampleSelector, HandleWidgetEvent));
  98. rootLayout->AddChild(button);
  99. }
  100. Input* input = GetSubsystem<Input>();
  101. input->SetMouseVisible(true);
  102. input->SetMouseMode(MM_FREE);
  103. // Subscribe key down event
  104. SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(SampleSelector, HandleKeyDown));
  105. context->RegisterSubsystem(this);
  106. }
  107. void SampleSelector::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  108. {
  109. using namespace KeyDown;
  110. int key = eventData[P_KEY].GetInt();
  111. // Close console (if open) or exit when ESC is pressed, and not same frame as construction
  112. if (key == KEY_ESCAPE && ( constructionFrame_ != GetSubsystem<Renderer>()->GetFrameInfo().frameNumber_ ) )
  113. {
  114. GetSubsystem<Engine>()->Exit();
  115. }
  116. }
  117. void SampleSelector::HandleWidgetEvent(StringHash eventType, VariantMap& eventData)
  118. {
  119. using namespace WidgetEvent;
  120. if (eventData[P_TYPE].GetUInt() == UI_EVENT_TYPE_CLICK)
  121. {
  122. UIWidget* target = static_cast<UIWidget*>(eventData[P_TARGET].GetPtr());
  123. String exampleName = target->GetId().CString();
  124. // Make sure we mark event handled, as we're deleting the button
  125. eventData[P_HANDLED] = true;
  126. // Goodbye UI
  127. FeatureExamples::GetUIView()->DeleteAllChildren();
  128. currentSample_ = 0;
  129. if (exampleName == "Hello World")
  130. {
  131. currentSample_ = new HelloWorld(context_);
  132. }
  133. else if (exampleName == "Hello GUI")
  134. {
  135. currentSample_ = new HelloGui(context_);
  136. }
  137. else if (exampleName == "2D Sprite")
  138. {
  139. currentSample_ = new Atomic2DSprite(context_);
  140. }
  141. else if (exampleName == "2D Physics")
  142. {
  143. currentSample_ = new Physics2D(context_);
  144. }
  145. else if (exampleName == "2D Constraints")
  146. {
  147. currentSample_ = new Constraints2D(context_);
  148. }
  149. else if (exampleName == "2D Spriter Animation")
  150. {
  151. currentSample_ = new SpriterAnimation(context_);
  152. }
  153. else if (exampleName == "3D Static Scene")
  154. {
  155. currentSample_ = new StaticScene(context_);
  156. }
  157. else if (exampleName == "3D Animating Scene")
  158. {
  159. currentSample_ = new AnimatingScene(context_);
  160. }
  161. else if (exampleName == "3D Physics")
  162. {
  163. currentSample_ = new Physics3D(context_);
  164. }
  165. else if (exampleName == "3D Skeletal Animation")
  166. {
  167. currentSample_ = new SkeletalAnimation(context_);
  168. }
  169. else if (exampleName == "3D Character")
  170. {
  171. currentSample_ = new CharacterDemo(context_);
  172. }
  173. else if (exampleName == "3D Ragdolls")
  174. {
  175. currentSample_ = new Ragdolls(context_);
  176. }
  177. else if (exampleName == "3D Vehicle Demo")
  178. {
  179. currentSample_ = new VehicleDemo(context_);
  180. }
  181. else if (exampleName == "3D Crowd Navigation")
  182. {
  183. currentSample_ = new CrowdNavigation(context_);
  184. }
  185. else if (exampleName == "3D Dynamic Geometry")
  186. {
  187. currentSample_ = new DynamicGeometry(context_);
  188. }
  189. else if (exampleName == "3D Water")
  190. {
  191. currentSample_ = new Water(context_);
  192. }
  193. else if (exampleName == "3D Billboards")
  194. {
  195. currentSample_ = new Billboards(context_);
  196. }
  197. else if (exampleName == "3D Multiple Viewports")
  198. {
  199. currentSample_ = new MultipleViewports(context_);
  200. }
  201. else if (exampleName == "3D Decals")
  202. {
  203. currentSample_ = new Decals(context_);
  204. }
  205. else if (exampleName == "2D Rope")
  206. {
  207. currentSample_ = new PhysicsRope2D(context_);
  208. }
  209. else if (exampleName == "Render to Texture")
  210. {
  211. currentSample_ = new RenderToTexture(context_);
  212. }
  213. else if (exampleName == "3D Light Animation")
  214. {
  215. currentSample_ = new LightAnimation(context_);
  216. }
  217. else if (exampleName == "3D Particles")
  218. {
  219. currentSample_ = new Particles3D(context_);
  220. }
  221. else if (exampleName == "3D Signed Distance Field Text")
  222. {
  223. currentSample_ = new SignedDistanceFieldText(context_);
  224. }
  225. if (currentSample_.NotNull())
  226. {
  227. UnsubscribeFromEvent(E_KEYDOWN);
  228. currentSample_->Start();
  229. }
  230. }
  231. }