Sample.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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/Core/ProcessUtils.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Graphics/Graphics.h>
  26. #include <Atomic/Graphics/Renderer.h>
  27. #include <Atomic/Resource/Image.h>
  28. #include <Atomic/Scene/Scene.h>
  29. #include <Atomic/UI/UI.h>
  30. #include <Atomic/UI/UIEvents.h>
  31. #include <Atomic/UI/UIFontDescription.h>
  32. #include <Atomic/UI/UIView.h>
  33. #include <Atomic/UI/UILayout.h>
  34. #include "Sample.h"
  35. #include "SampleSelector.h"
  36. #include "FeatureExamples.h"
  37. Sample::Sample(Context* context) :
  38. Object(context),
  39. yaw_(0.0f),
  40. pitch_(0.0f),
  41. touchEnabled_(false),
  42. paused_(false),
  43. useMouseMode_(MM_ABSOLUTE)
  44. {
  45. }
  46. void Sample::Start()
  47. {
  48. SubscribeToEvent(E_KEYUP, ATOMIC_HANDLER(Sample, HandleKeyDown));
  49. }
  50. void Sample::Stop()
  51. {
  52. }
  53. void Sample::InitMouseMode(MouseMode mode)
  54. {
  55. useMouseMode_ = mode;
  56. Input* input = GetSubsystem<Input>();
  57. if (GetPlatform() != "Web")
  58. {
  59. if (useMouseMode_ == MM_FREE)
  60. input->SetMouseVisible(true);
  61. // ATOMIC: FIXME
  62. // Console* console = GetSubsystem<Console>();
  63. if (useMouseMode_ != MM_ABSOLUTE)
  64. {
  65. input->SetMouseMode(useMouseMode_);
  66. // ATOMIC: FIXME
  67. /*
  68. if (console && console->IsVisible())
  69. input->SetMouseMode(MM_ABSOLUTE, true);
  70. */
  71. }
  72. }
  73. else
  74. {
  75. input->SetMouseVisible(true);
  76. SubscribeToEvent(E_MOUSEBUTTONDOWN, ATOMIC_HANDLER(Sample, HandleMouseModeRequest));
  77. SubscribeToEvent(E_MOUSEMODECHANGED, ATOMIC_HANDLER(Sample, HandleMouseModeChange));
  78. }
  79. }
  80. void Sample::BackToSelector()
  81. {
  82. GetSubsystem<Input>()->SetMouseVisible(true);
  83. UnsubscribeFromAllEvents();
  84. Renderer* renderer = GetSubsystem<Renderer>();
  85. for (unsigned i = 0; i < renderer->GetNumViewports(); i++)
  86. {
  87. renderer->SetViewport(i, 0);
  88. }
  89. FeatureExamples::GetUIView()->DeleteAllChildren();
  90. new SampleSelector(context_);
  91. }
  92. void Sample::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  93. {
  94. using namespace KeyDown;
  95. int key = eventData[P_KEY].GetInt();
  96. if (key == KEY_ESCAPE )
  97. {
  98. BackToSelector();
  99. return;
  100. }
  101. // Common rendering quality controls, only when UI has no focused element
  102. Renderer* renderer = GetSubsystem<Renderer>();
  103. // Texture quality
  104. if (key == '1')
  105. {
  106. int quality = renderer->GetTextureQuality();
  107. ++quality;
  108. if (quality > QUALITY_HIGH)
  109. quality = QUALITY_LOW;
  110. renderer->SetTextureQuality(quality);
  111. }
  112. // Material quality
  113. else if (key == '2')
  114. {
  115. int quality = renderer->GetMaterialQuality();
  116. ++quality;
  117. if (quality > QUALITY_HIGH)
  118. quality = QUALITY_LOW;
  119. renderer->SetMaterialQuality(quality);
  120. }
  121. // Specular lighting
  122. else if (key == '3')
  123. renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
  124. // Shadow rendering
  125. else if (key == '4')
  126. renderer->SetDrawShadows(!renderer->GetDrawShadows());
  127. // Shadow map resolution
  128. else if (key == '5')
  129. {
  130. int shadowMapSize = renderer->GetShadowMapSize();
  131. shadowMapSize *= 2;
  132. if (shadowMapSize > 2048)
  133. shadowMapSize = 512;
  134. renderer->SetShadowMapSize(shadowMapSize);
  135. }
  136. // Shadow depth and filtering quality
  137. else if (key == '6')
  138. {
  139. ShadowQuality quality = renderer->GetShadowQuality();
  140. quality = (ShadowQuality)(quality + 1);
  141. if (quality > SHADOWQUALITY_BLUR_VSM)
  142. quality = SHADOWQUALITY_SIMPLE_16BIT;
  143. renderer->SetShadowQuality(quality);
  144. }
  145. // Occlusion culling
  146. else if (key == '7')
  147. {
  148. bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
  149. occlusion = !occlusion;
  150. renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
  151. }
  152. // Instancing
  153. else if (key == '8')
  154. renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
  155. // Take screenshot
  156. else if (key == '9')
  157. {
  158. Graphics* graphics = GetSubsystem<Graphics>();
  159. Image screenshot(context_);
  160. graphics->TakeScreenShot(&screenshot);
  161. // Here we save in the Data folder with date and time appended
  162. screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
  163. Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
  164. }
  165. }
  166. void Sample::SimpleCreateInstructionsWithWasd(const String& extra)
  167. {
  168. SimpleCreateInstructions("Use WASD keys and mouse/touch to move" + extra);
  169. }
  170. void Sample::SimpleCreateInstructions(const String& text)
  171. {
  172. UILayout* layout = new UILayout(context_);
  173. layout->SetRect(FeatureExamples::GetUIView()->GetRect());
  174. layout->SetLayoutPosition (UI_LAYOUT_POSITION_RIGHT_BOTTOM);
  175. layout->SetLayoutDistributionPosition (UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM);
  176. SharedPtr<UIFontDescription> fontDesc(new UIFontDescription(context_));
  177. fontDesc->SetId("Vera");
  178. fontDesc->SetSize(18);
  179. String msgText = text;
  180. if (text.Length() && !text.EndsWith("\n"))
  181. msgText += "\n";
  182. msgText += "Press ESC for menu";
  183. label_ = new UIEditField(context_);
  184. label_->SetFontDescription(fontDesc);
  185. label_->SetReadOnly(true);
  186. label_->SetMultiline(true);
  187. label_->SetAdaptToContentSize(true);
  188. label_->SetText(msgText);
  189. layout->AddChild(label_);
  190. FeatureExamples::GetUIView()->AddChild(layout);
  191. }
  192. void Sample::SetInstructions(const String& text)
  193. {
  194. if ( label_ != NULL )
  195. label_->SetText(text);
  196. }
  197. // If the user clicks the canvas, attempt to switch to relative mouse mode on web platform
  198. void Sample::HandleMouseModeRequest(StringHash eventType, VariantMap& eventData)
  199. {
  200. // ATOMIC: FIXME
  201. /*
  202. Console* console = GetSubsystem<Console>();
  203. if (console && console->IsVisible())
  204. return;
  205. */
  206. Input* input = GetSubsystem<Input>();
  207. if (useMouseMode_ == MM_ABSOLUTE)
  208. input->SetMouseVisible(false);
  209. else if (useMouseMode_ == MM_FREE)
  210. input->SetMouseVisible(true);
  211. input->SetMouseMode(useMouseMode_);
  212. }
  213. void Sample::HandleMouseModeChange(StringHash eventType, VariantMap& eventData)
  214. {
  215. Input* input = GetSubsystem<Input>();
  216. bool mouseLocked = eventData[MouseModeChanged::P_MOUSELOCKED].GetBool();
  217. input->SetMouseVisible(!mouseLocked);
  218. }