| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // Copyright (c) 2008-2016 the Urho3D project.
- // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include <Atomic/Core/ProcessUtils.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Graphics/Graphics.h>
- #include <Atomic/Graphics/Renderer.h>
- #include <Atomic/Resource/Image.h>
- #include <Atomic/Scene/Scene.h>
- #include <Atomic/UI/UI.h>
- #include <Atomic/UI/UIEvents.h>
- #include <Atomic/UI/UIFontDescription.h>
- #include <Atomic/UI/UIView.h>
- #include <Atomic/UI/UILayout.h>
- #include <Atomic/UI/UIEditField.h>
- #include "Sample.h"
- #include "SampleSelector.h"
- #include "FeatureExamples.h"
- Sample::Sample(Context* context) :
- Object(context),
- yaw_(0.0f),
- pitch_(0.0f),
- touchEnabled_(false),
- paused_(false),
- useMouseMode_(MM_ABSOLUTE)
- {
- }
- void Sample::Start()
- {
- SubscribeToEvent(E_KEYUP, ATOMIC_HANDLER(Sample, HandleKeyDown));
- }
- void Sample::Stop()
- {
- }
- void Sample::InitMouseMode(MouseMode mode)
- {
- useMouseMode_ = mode;
- Input* input = GetSubsystem<Input>();
- if (GetPlatform() != "Web")
- {
- if (useMouseMode_ == MM_FREE)
- input->SetMouseVisible(true);
- // ATOMIC: FIXME
- // Console* console = GetSubsystem<Console>();
- if (useMouseMode_ != MM_ABSOLUTE)
- {
- input->SetMouseMode(useMouseMode_);
- // ATOMIC: FIXME
- /*
- if (console && console->IsVisible())
- input->SetMouseMode(MM_ABSOLUTE, true);
- */
- }
- }
- else
- {
- input->SetMouseVisible(true);
- SubscribeToEvent(E_MOUSEBUTTONDOWN, ATOMIC_HANDLER(Sample, HandleMouseModeRequest));
- SubscribeToEvent(E_MOUSEMODECHANGED, ATOMIC_HANDLER(Sample, HandleMouseModeChange));
- }
- }
- void Sample::BackToSelector()
- {
- GetSubsystem<Input>()->SetMouseVisible(true);
-
- UnsubscribeFromAllEvents();
- Renderer* renderer = GetSubsystem<Renderer>();
- for (unsigned i = 0; i < renderer->GetNumViewports(); i++)
- {
- renderer->SetViewport(i, 0);
- }
- FeatureExamples::GetUIView()->DeleteAllChildren();
- new SampleSelector(context_);
- }
- void Sample::HandleKeyDown(StringHash eventType, VariantMap& eventData)
- {
- using namespace KeyDown;
- int key = eventData[P_KEY].GetInt();
- if (key == KEY_ESCAPE )
- {
- BackToSelector();
- return;
- }
- // Common rendering quality controls, only when UI has no focused element
- Renderer* renderer = GetSubsystem<Renderer>();
- // Texture quality
- if (key == '1')
- {
- int quality = renderer->GetTextureQuality();
- ++quality;
- if (quality > QUALITY_HIGH)
- quality = QUALITY_LOW;
- renderer->SetTextureQuality(quality);
- }
- // Material quality
- else if (key == '2')
- {
- int quality = renderer->GetMaterialQuality();
- ++quality;
- if (quality > QUALITY_HIGH)
- quality = QUALITY_LOW;
- renderer->SetMaterialQuality(quality);
- }
- // Specular lighting
- else if (key == '3')
- renderer->SetSpecularLighting(!renderer->GetSpecularLighting());
- // Shadow rendering
- else if (key == '4')
- renderer->SetDrawShadows(!renderer->GetDrawShadows());
- // Shadow map resolution
- else if (key == '5')
- {
- int shadowMapSize = renderer->GetShadowMapSize();
- shadowMapSize *= 2;
- if (shadowMapSize > 2048)
- shadowMapSize = 512;
- renderer->SetShadowMapSize(shadowMapSize);
- }
- // Shadow depth and filtering quality
- else if (key == '6')
- {
- ShadowQuality quality = renderer->GetShadowQuality();
- quality = (ShadowQuality)(quality + 1);
- if (quality > SHADOWQUALITY_BLUR_VSM)
- quality = SHADOWQUALITY_SIMPLE_16BIT;
- renderer->SetShadowQuality(quality);
- }
- // Occlusion culling
- else if (key == '7')
- {
- bool occlusion = renderer->GetMaxOccluderTriangles() > 0;
- occlusion = !occlusion;
- renderer->SetMaxOccluderTriangles(occlusion ? 5000 : 0);
- }
- // Instancing
- else if (key == '8')
- renderer->SetDynamicInstancing(!renderer->GetDynamicInstancing());
- // Take screenshot
- else if (key == '9')
- {
- Graphics* graphics = GetSubsystem<Graphics>();
- Image screenshot(context_);
- graphics->TakeScreenShot(&screenshot);
- // Here we save in the Data folder with date and time appended
- screenshot.SavePNG(GetSubsystem<FileSystem>()->GetProgramDir() + "Data/Screenshot_" +
- Time::GetTimeStamp().Replaced(':', '_').Replaced('.', '_').Replaced(' ', '_') + ".png");
- }
- }
- void Sample::SimpleCreateInstructionsWithWasd(const String& extra)
- {
- SimpleCreateInstructions("Use WASD keys and mouse/touch to move" + extra);
- }
- void Sample::SimpleCreateInstructions(const String& text)
- {
- UILayout* layout = new UILayout(context_);
- layout->SetRect(FeatureExamples::GetUIView()->GetRect());
- layout->SetLayoutPosition (UI_LAYOUT_POSITION_RIGHT_BOTTOM);
- layout->SetLayoutDistributionPosition (UI_LAYOUT_DISTRIBUTION_POSITION_RIGHT_BOTTOM);
- SharedPtr<UIFontDescription> fontDesc(new UIFontDescription(context_));
- fontDesc->SetId("Vera");
- fontDesc->SetSize(18);
- String msgText = text;
- if (text.Length() && !text.EndsWith("\n"))
- msgText += "\n";
- msgText += "Press ESC for menu";
- UIEditField* label = new UIEditField(context_);
- label->SetFontDescription(fontDesc);
- label->SetReadOnly(true);
- label->SetMultiline(true);
- label->SetAdaptToContentSize(true);
- label->SetText(msgText);
- layout->AddChild(label);
- FeatureExamples::GetUIView()->AddChild(layout);
- }
- // If the user clicks the canvas, attempt to switch to relative mouse mode on web platform
- void Sample::HandleMouseModeRequest(StringHash eventType, VariantMap& eventData)
- {
- // ATOMIC: FIXME
- /*
- Console* console = GetSubsystem<Console>();
- if (console && console->IsVisible())
- return;
- */
- Input* input = GetSubsystem<Input>();
- if (useMouseMode_ == MM_ABSOLUTE)
- input->SetMouseVisible(false);
- else if (useMouseMode_ == MM_FREE)
- input->SetMouseVisible(true);
- input->SetMouseMode(useMouseMode_);
- }
- void Sample::HandleMouseModeChange(StringHash eventType, VariantMap& eventData)
- {
- Input* input = GetSubsystem<Input>();
- bool mouseLocked = eventData[MouseModeChanged::P_MOUSELOCKED].GetBool();
- input->SetMouseVisible(!mouseLocked);
- }
|