Browse Source

Merge pull request #122 from rokups/feature/imgui

imgui example
JoshEngebretson 8 years ago
parent
commit
776b1699bc

+ 11 - 11
FeatureExamples/CPlusPlus/Source/FeatureExamples.cpp

@@ -38,6 +38,7 @@
 #include <Atomic/UI/UIView.h>
 #include <Atomic/Resource/XMLFile.h>
 #include <Atomic/IO/Log.h>
+#include <Atomic/Engine/EngineDefs.h>
 
 #include "FeatureExamples.h"
 #include "SampleSelector.h"
@@ -55,24 +56,23 @@ FeatureExamples::FeatureExamples(Context* context) :
 void FeatureExamples::Setup()
 {
     // Modify engine startup parameters
-    engineParameters_["WindowTitle"] = GetTypeName();
-    engineParameters_["WindowWidth"] = 1280;
-    engineParameters_["WindowHeight"] = 720;
-    engineParameters_["LogName"]     = GetSubsystem<FileSystem>()->GetAppPreferencesDir("atomic", "logs") + GetTypeName() + ".log";
-    engineParameters_["FullScreen"]  = false;
-    engineParameters_["Headless"]    = false;
-    engineParameters_["Sound"]       = false;
-
-    engineParameters_["ResourcePaths"]       = "Data;PlayerData;CoreData";
+    engineParameters_[EP_WINDOW_TITLE]   = GetTypeName();
+    engineParameters_[EP_WINDOW_WIDTH]   = 1440;
+    engineParameters_[EP_WINDOW_HEIGHT]  = 960;
+    engineParameters_[EP_LOG_NAME]       = GetSubsystem<FileSystem>()->GetAppPreferencesDir("atomic", "logs") + GetTypeName() + ".log";
+    engineParameters_[EP_FULL_SCREEN]    = false;
+    engineParameters_[EP_HEADLESS ]      = false;
+    engineParameters_[EP_SOUND]          = false;
+    engineParameters_[EP_RESOURCE_PATHS] = "Data;PlayerData;CoreData";
 
     // Construct a search path to find the resource prefix with two entries:
     // The first entry is an empty path which will be substituted with program/bin directory -- this entry is for binary when it is still in build tree
     // The second and third entries are possible relative paths from the installed program/bin directory to the asset directory -- these entries are for binary when it is in the Atomic SDK installation location
-    if (!engineParameters_.Contains("ResourcePrefixPaths"))
+    if (!engineParameters_.Contains(EP_RESOURCE_PREFIX_PATHS))
     {
         // TODO: This is dependent on a source build
         String resourcePrefix = ToString("%s/Resources;%s/Submodules/AtomicExamples/FeatureExamples/CPlusPlus", ATOMIC_ROOT_SOURCE_DIR, ATOMIC_ROOT_SOURCE_DIR);
-        engineParameters_["ResourcePrefixPaths"] = resourcePrefix;
+        engineParameters_[EP_RESOURCE_PREFIX_PATHS] = resourcePrefix;
     }
 
 }

+ 130 - 0
FeatureExamples/CPlusPlus/Source/HelloSystemUi.cpp

@@ -0,0 +1,130 @@
+//
+// Copyright (c) 2017 the Atomic project.
+//
+// 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/CoreEvents.h>
+#include <Atomic/Core/ProcessUtils.h>
+#include <Atomic/Input/Input.h>
+#include <Atomic/Graphics/Graphics.h>
+#include <Atomic/Graphics/Octree.h>
+#include <Atomic/Graphics/Zone.h>
+#include <Atomic/Graphics/Camera.h>
+#include <Atomic/Graphics/Renderer.h>
+#include <Atomic/Resource/ResourceCache.h>
+#include <Atomic/Scene/Scene.h>
+#include <Atomic/UI/SystemUI/SystemUI.h>
+#include <Atomic/UI/SystemUI/SystemUIEvents.h>
+#include <Atomic/UI/SystemUI/Console.h>
+
+#include "FeatureExamples.h"
+#include "HelloSystemUi.h"
+
+#include <Atomic/DebugNew.h>
+
+
+HelloSystemUi::HelloSystemUi(Context* context) :
+    Sample(context)
+{
+}
+
+void HelloSystemUi::Start()
+{
+    // Execute base class startup
+    Sample::Start();
+
+    SimpleCreateInstructions();
+
+    // Scene is only required to give screen a background color.
+    CreateScene();
+
+    // Finally subscribe to the update event. Note that by subscribing events at this point we have already missed some events
+    // like the ScreenMode event sent by the Graphics subsystem when opening the application window. To catch those as well we
+    // could subscribe in the constructor instead.
+    SubscribeToEvents();
+
+    // Set the mouse mode to use in the sample
+    Sample::InitMouseMode(MM_FREE);
+}
+
+void HelloSystemUi::SubscribeToEvents()
+{
+    SubscribeToEvent(E_KEYDOWN, ATOMIC_HANDLER(HelloSystemUi, HandleKeyDown));
+    SubscribeToEvent(E_SYSTEMUIFRAME, ATOMIC_HANDLER(HelloSystemUi, RenderUi));
+}
+
+void HelloSystemUi::RenderUi(StringHash eventType, VariantMap& eventData)
+{
+    ImGui::SetNextWindowSize(ImVec2(200, 300), ImGuiSetCond_FirstUseEver);
+    ImGui::SetNextWindowPos(ImVec2(200, 300), ImGuiSetCond_FirstUseEver);
+    if (ImGui::Begin("Sample SystemUI", 0, ImGuiWindowFlags_NoSavedSettings))
+    {
+        if (messageBox_.NotNull())
+        {
+            if (ImGui::Button("Close message box"))
+                messageBox_ = nullptr;
+        }
+        else
+        {
+            if (ImGui::Button("Show message box"))
+            {
+                messageBox_ = new MessageBox(context_, "Hello from SystemUI", "Sample Message Box");
+                SubscribeToEvent(E_MESSAGEACK, [&](StringHash, VariantMap&) {
+                    messageBox_ = nullptr;
+                });
+            }
+        }
+
+        if (ImGui::Button("Toggle console"))
+            GetSubsystem<Console>()->Toggle();
+    }
+    ImGui::End();
+}
+
+void HelloSystemUi::HandleKeyDown(StringHash eventType, VariantMap& eventData)
+{
+    if (eventData[KeyDown::P_KEY].GetUInt() == KEY_BACKQUOTE)
+        GetSubsystem<Console>()->Toggle();
+}
+
+void HelloSystemUi::CreateScene()
+{
+    scene_ = new Scene(context_);
+
+    // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
+    // (-1000, -1000, -1000) to (1000, 1000, 1000)
+    scene_->CreateComponent<Octree>();
+
+    // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
+    // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
+    // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
+    Node* zoneNode = scene_->CreateChild("Zone");
+    Zone* zone = zoneNode->CreateComponent<Zone>();
+    // Set same volume as the Octree, set a close bluish fog and some ambient light
+    zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
+    zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
+    zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
+    zone->SetFogStart(100.0f);
+    zone->SetFogEnd(300.0f);
+
+    cameraNode_ = scene_->CreateChild("Camera");
+    auto camera = cameraNode_->CreateComponent<Camera>();
+    GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
+}

+ 50 - 0
FeatureExamples/CPlusPlus/Source/HelloSystemUi.h

@@ -0,0 +1,50 @@
+//
+// Copyright (c) 2017 the Atomic project.
+//
+// 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.
+//
+
+#pragma once
+
+#include "Sample.h"
+#include <Atomic/UI/SystemUI/MessageBox.h>
+
+
+class HelloSystemUi : public Sample
+{
+    ATOMIC_OBJECT(HelloSystemUi, Sample)
+public:
+    /// Construct.
+    HelloSystemUi(Context* context);
+    /// Setup after engine initialization and before running the main loop.
+    virtual void Start();
+
+private:
+    /// Creates scene that gives screen a background color.
+    void CreateScene();
+    /// Subscribe to application-wide logic update events.
+    void SubscribeToEvents();
+    /// Render SystemUi.
+    void RenderUi(StringHash eventType, VariantMap& eventData);
+    /// Handle keyboard events.
+    void HandleKeyDown(StringHash eventType, VariantMap& eventData);
+
+    /// Reference holding message box.
+    SharedPtr<MessageBox> messageBox_;
+};

+ 6 - 0
FeatureExamples/CPlusPlus/Source/SampleSelector.cpp

@@ -35,6 +35,7 @@
 
 #include "HelloWorld.h"
 #include "HelloGui.h"
+#include "HelloSystemUi.h"
 #include "2DSprite.h"
 #include "Physics2D.h"
 #include "Constraints2D.h"
@@ -75,6 +76,7 @@ SampleSelector::SampleSelector(Context* context) :
     const char* examples[] = {
         "Hello World",
         "Hello GUI",
+        "Hello SystemUI",
         "Render to Texture",
         "2D Sprite",
         "2D Physics",
@@ -157,6 +159,10 @@ void SampleSelector::HandleWidgetEvent(StringHash eventType, VariantMap& eventDa
         {
             currentSample_ = new HelloGui(context_);
         }
+        else if (exampleName == "Hello SystemUI")
+        {
+            currentSample_ = new HelloSystemUi(context_);
+        }
         else if (exampleName == "2D Sprite")
         {
             currentSample_ = new Atomic2DSprite(context_);