text: //
  // Copyright (c) 2008-2017 the Urho3D 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 <Urho3D/Core/CoreEvents.h>
  #include <Urho3D/Input/Input.h>
  #include <Urho3D/Graphics/Graphics.h>
  #include <Urho3D/Graphics/Octree.h>
  #include <Urho3D/Graphics/Zone.h>
  #include <Urho3D/Graphics/Camera.h>
  #include <Urho3D/Graphics/Renderer.h>
  #include <Urho3D/Graphics/Model.h>
  #include <Urho3D/Graphics/StaticModel.h>
  #include <Urho3D/Resource/ResourceCache.h>
  #include <Urho3D/Scene/Scene.h>
  #include <Urho3D/SystemUI/SystemUI.h>
  #include <Urho3D/SystemUI/Console.h>
  #include <Urho3D/Input/FreeFlyController.h>
  
  #include "HelloSystemUI.h"
  
  
  
  #include <Urho3D/DebugNew.h>
  
  // Expands to this example's entry-point
  
  HelloSystemUi::HelloSystemUi(Context* context)
      : Sample(context)
  {
  }
  
  void HelloSystemUi::Start()
  {
      // Execute base class startup
      Sample::Start();
  
      // Create scene providing a colored background.
      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
      SetMouseMode(MM_FREE);
      SetMouseVisible(true);
  
      // Pass console commands to file system.
      GetSubsystem<FileSystem>()->SetExecuteConsoleCommands(true);
      GetSubsystem<Console>()->RefreshInterpreters();
  }
  
  void HelloSystemUi::SubscribeToEvents()
  {
      SubscribeToEvent(E_KEYDOWN, URHO3D_HANDLER(HelloSystemUi, HandleKeyDown));
      SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HelloSystemUi, RenderUi));
  }
  
  void HelloSystemUi::RenderUi(StringHash eventType, VariantMap& eventData)
  {
      gizmo_->ManipulateNode(cameraNode_->GetComponent<Camera>(), boxNode_);
  
      ui::SetNextWindowSize(ImVec2(200, 300), ImGuiCond_FirstUseEver);
      ui::SetNextWindowPos(ImVec2(200, 300), ImGuiCond_FirstUseEver);
      if (ui::Begin("Sample SystemUI", 0, ImGuiWindowFlags_NoSavedSettings))
      {
          if (messageBox_)
          {
              if (ui::Button("Close message box"))
                  messageBox_ = nullptr;
          }
          else
          {
              if (ui::Button("Show message box"))
              {
                  messageBox_ = new SystemMessageBox(context_, "Hello from SystemUI", "Sample Message Box");
                  SubscribeToEvent(E_MESSAGEACK, [&](StringHash, VariantMap&) {
                      messageBox_ = nullptr;
                  });
              }
          }
  
          if (ui::Button("Toggle console"))
              GetSubsystem<Console>()->Toggle();
  
          if (ui::Button("Toggle metrics window"))
              metricsOpen_ ^= true;
  
          gizmo_->RenderUI();
      }
      ui::End();
      if (metricsOpen_)
          ui::ShowMetricsWindow(&metricsOpen_);
  }
  
  void HelloSystemUi::HandleKeyDown(StringHash eventType, VariantMap& eventData)
  {
      if (eventData[KeyDown::P_KEY].GetUInt() == KEY_BACKQUOTE)
          GetSubsystem<Console>()->Toggle();
  }
  
  void HelloSystemUi::CreateScene()
  {
      ui::GetIO().ConfigFlags |= ImGuiConfigFlags_DockingEnable | ImGuiConfigFlags_NavEnableKeyboard;
      ui::GetIO().BackendFlags |= ImGuiBackendFlags_HasMouseCursors;
  
      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");
      cameraNode_->CreateComponent<FreeFlyController>();
  
      auto camera = cameraNode_->CreateComponent<Camera>();
      GetSubsystem<Renderer>()->SetViewport(0, new Viewport(context_, scene_, camera));
  
      boxNode_ = scene_->CreateChild("Box");
      boxNode_->SetPosition(cameraNode_->LocalToWorld(Vector3::FORWARD * 3.0f));
      const auto staticModel = boxNode_->CreateComponent<StaticModel>();
      staticModel->SetModel(GetSubsystem<ResourceCache>()->GetResource<Model>("Models/Box.mdl"));
      staticModel->SetMaterial(GetSubsystem<ResourceCache>()->GetResource<Material>("Materials/DefaultGrey.xml"));
  
      gizmo_ = MakeShared<Gizmo>(context_);
  }
  