Sample.cpp 7.4 KB

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