Sample.cpp 7.4 KB

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