From 8d52e27c2b827eb43351b6b2339829fa0824b1dd Mon Sep 17 00:00:00 2001 From: krzys-h Date: Tue, 27 Dec 2016 14:58:41 +0100 Subject: [PATCH] Add coordinates under cursor overlay and copy function (#868) --- src/common/event.h | 2 +- src/ui/debug_menu.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++- src/ui/debug_menu.h | 31 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/common/event.h b/src/common/event.h index 315a4b5b..c67b7c3d 100644 --- a/src/common/event.h +++ b/src/common/event.h @@ -172,7 +172,7 @@ enum EventType EVENT_LABEL16 = 106, EVENT_LABEL17 = 107, EVENT_LABEL18 = 108, - EVENT_LABEL19 = 109, + EVENT_LABEL19 = 109, // cursor position overlay EVENT_LIST0 = 110, EVENT_LIST1 = 111, diff --git a/src/ui/debug_menu.cpp b/src/ui/debug_menu.cpp index 10aa9397..1510e2bb 100644 --- a/src/ui/debug_menu.cpp +++ b/src/ui/debug_menu.cpp @@ -20,6 +20,9 @@ #include "ui/debug_menu.h" #include "common/event.h" +#include "common/stringutils.h" + +#include "app/app.h" #include "graphics/engine/lightning.h" #include "graphics/engine/terrain.h" @@ -34,8 +37,11 @@ #include "ui/controls/button.h" #include "ui/controls/check.h" #include "ui/controls/interface.h" +#include "ui/controls/label.h" #include "ui/controls/window.h" +#include + namespace Ui { @@ -55,10 +61,17 @@ CDebugMenu::~CDebugMenu() void CDebugMenu::ToggleInterface() { - if (m_interface->SearchControl(EVENT_WINDOW7) == nullptr) + if (!IsActive()) + { CreateInterface(); + CLabel* pl = m_interface->CreateLabel(Math::Point(0.0f, 0.9f), Math::Point(1.0f, 0.1f), -1, EVENT_LABEL19, "??"); + pl->SetFontType(Gfx::FONT_COURIER); + } else + { + m_interface->DeleteControl(EVENT_LABEL19); DestroyInterface(); + } } const Math::Point dim = Math::Point(33.0f/640.0f, 33.0f/480.0f); @@ -359,6 +372,18 @@ bool CDebugMenu::EventProcess(const Event &event) } break; + case EVENT_FRAME: + HandleFrameUpdate(event); + break; + + case EVENT_KEY_DOWN: + if (event.GetData()->key == KEY(c) && (event.kmodState & KMOD_CTRL) != 0) + { + if (IsActive()) + { + return !HandleCopy(event.mousePos); + } + } default: break; @@ -427,4 +452,38 @@ bool CDebugMenu::HandleTeleport(Math::Point mousePos) return true; } +void CDebugMenu::HandleFrameUpdate(const Event &event) +{ + std::string str = "-"; + Math::Vector pos; + int obj; + if ((obj = m_engine->DetectObject(event.mousePos, pos, true)) != -1) + str = StrUtils::Format("pos=% 3.2f; % 3.2f height=% 3.2f objId=% 4d", pos.x, pos.z, pos.y, obj); + + CLabel* pl = static_cast(m_interface->SearchControl(EVENT_LABEL19)); + if (pl == nullptr) return; + pl->SetName(str.c_str()); +} + +bool CDebugMenu::HandleCopy(Math::Point mousePos) +{ + Math::Vector pos; + if (m_engine->DetectObject(mousePos, pos, true) == -1) + { + m_sound->Play(SOUND_CLICK, 1.0f, 0.5f); + return false; + } + + std::string str = StrUtils::Format("pos=%.2f;%.2f", pos.x, pos.z); + + GetLogger()->Debug("%s\n", str.c_str()); + SDL_SetClipboardText(str.c_str()); + return true; +} + +bool CDebugMenu::IsActive() +{ + return m_interface->SearchControl(EVENT_WINDOW7) != nullptr; +} + } diff --git a/src/ui/debug_menu.h b/src/ui/debug_menu.h index a45623ac..67e183fd 100644 --- a/src/ui/debug_menu.h +++ b/src/ui/debug_menu.h @@ -37,24 +37,55 @@ namespace Ui { class CInterface; +/** + * \class CDebugMenu + * \brief Handles debug menu (F11) + * + * There should always be only one instance of this class for each associated CRobotMain class. + */ class CDebugMenu { public: + //! Creates the CDebugMenu instance CDebugMenu(CRobotMain* main, Gfx::CEngine* engine, CObjectManager* objMan, CSoundInterface* sound); + //! Destroys the CDebugMenu instance + //! \note Does not clean up the interface, should be called only when CRobotMain is destroyed virtual ~CDebugMenu(); + //! Toggle the debug interface void ToggleInterface(); + //! Check if the debug interface is open + bool IsActive(); + + //! Event processing bool EventProcess(const Event& event); protected: + //! Create the main page of debug interface void CreateInterface(); + //! Create the spawn object interface void CreateSpawnInterface(); + //! Update controls in the debug interface void UpdateInterface(); + //! Destroy the debug interface window void DestroyInterface(); + //! Handle frame update + //! This is used to update the cursor coordinates overlay + void HandleFrameUpdate(const Event &event); + + //! Handle spawning a new object at mouse position + //! \return true on success, false on error bool HandleSpawnObject(ObjectType type, Math::Point mousePos); + //! Handle lightning at position + //! \return true on success, false on error bool HandleLightning(Math::Point mousePos); + //! Handle teleport to position + //! \return true on success, false on error bool HandleTeleport(Math::Point mousePos); + //! Handle ctrl+c (copy coordinates under cursor to clipboard) + //! \return true on success, false on error + bool HandleCopy(Math::Point mousePos); protected: CRobotMain* m_main;