Skip to content

Commit

Permalink
Draw directions to other UI windows when examining focus system in th…
Browse files Browse the repository at this point in the history
…e UI debugger
  • Loading branch information
Xottab-DUTY committed Jan 20, 2025
1 parent 1b627e0 commit 214fd3a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/xrEngine/editor_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#define IMGUI_DISABLE_OBSOLETE_KEYIO
#define IMGUI_DISABLE_OBSOLETE_FUNCTIONS
#define IMGUI_DEFINE_MATH_OPERATORS
#include <imgui.h>

namespace xray::editor
Expand Down
19 changes: 19 additions & 0 deletions src/xrUICore/ui_debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ void CUIDebugger::on_tool_frame()
imgui::ItemHelp("Non-valuable window hovered by in-game cursor");
ImGui::EndDisabled();

imgui::ColorEdit4("Direction arrow", colors.directionArrow);
imgui::ItemHelp("The color of the arrow being drawn when examining the focus system");
imgui::ColorEdit4("Direction text", colors.directionText);
imgui::ItemHelp("The color of the text being drawn when examining the focus system");

ImGui::EndMenu();
}
ImGui::EndMenuBar();
Expand Down Expand Up @@ -153,6 +158,8 @@ void CUIDebugger::reset_settings()
/*.focusableValuableHovered =*/ color_rgba(0, 255, 0, 255),
/*.focusableNonValuable =*/ color_rgba(255, 0, 0, 200),
/*.focusableNonValuableHovered =*/ color_rgba(255, 0, 0, 255),
/*.directionArrow =*/ color_rgba(192, 192, 192, 255),
/*.directionText =*/ color_rgba(255, 0, 0, 255),
},
/*.drawWndRects =*/ true,
/*.coloredRects =*/ false,
Expand Down Expand Up @@ -185,6 +192,10 @@ void CUIDebugger::apply_setting(pcstr line)
settings.colors.focusableNonValuable = color;
else if (sscanf(line, "FocusableNonValuableHoveredColor=0x%X", &color) == 1)
settings.colors.focusableNonValuableHovered = color;
else if (sscanf(line, "DirectionArrowColor=0x%X", &color) == 1)
settings.colors.directionArrow = color;
else if (sscanf(line, "DirectionTextColor=0x%X", &color) == 1)
settings.colors.directionText = color;
}

void CUIDebugger::save_settings(ImGuiTextBuffer* buffer) const
Expand All @@ -203,6 +214,8 @@ void CUIDebugger::save_settings(ImGuiTextBuffer* buffer) const
buffer->appendf("FocusableValuableHoveredColor=0x%X\n", colors.focusableValuableHovered);
buffer->appendf("FocusableNonValuableColor=0x%X\n", colors.focusableNonValuable);
buffer->appendf("FocusableNonValuableHoveredColor=0x%X\n", colors.focusableNonValuableHovered);
buffer->appendf("DirectionArrowColor=0x%X\n", colors.directionArrow);
buffer->appendf("DirectionTextColor=0x%X\n", colors.directionText);
}

size_t CUIDebugger::estimate_settings_size() const
Expand Down Expand Up @@ -243,5 +256,11 @@ size_t CUIDebugger::estimate_settings_size() const
// "FocusableNonValuableHoveredColor=0x%X\n"
size += std::size("FocusableNonValuableHoveredColor=0x") + HEXNUMBER_SIZE;

// "DirectionArrowColor=0x%X\n"
size += std::size("DirectionArrowColor=0x") + HEXNUMBER_SIZE;

// "DirectionTextColor=0x%X\n"
size += std::size("DirectionTextColor=0x") + HEXNUMBER_SIZE;

return size;
}
6 changes: 5 additions & 1 deletion src/xrUICore/ui_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ inline pcstr CUIDebuggable::GetDebugType() { return "CUIDebuggable"; }

struct CUIDebuggerSettings
{
struct
struct Colors
{
// Just a window
u32 normal;
Expand All @@ -38,6 +38,10 @@ struct CUIDebuggerSettings
u32 focusableNonValuable;
// Non-valuable window hovered by in-game cursor
u32 focusableNonValuableHovered;
// The color of the arrow being drawn when examining the focus system
u32 directionArrow;
// The color of the text being drawn when examining the focus system
u32 directionText;
} colors;

bool drawWndRects;
Expand Down
68 changes: 67 additions & 1 deletion src/xrUICore/ui_focus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,30 @@ limitations under the License.
#include "Cursor/UICursor.h"

#include "xrCore/buffer_vector.h"
#include "xrEngine/editor_helper.h"

#include <array>

namespace
{
constexpr cpcstr short_direction(FocusDirection direction)
{
switch (direction)
{
case FocusDirection::Same: return "S";
case FocusDirection::Up: return "U";
case FocusDirection::Down: return "D";
case FocusDirection::Left: return "L";
case FocusDirection::Right: return "R";
case FocusDirection::UpperLeft: return "UL";
case FocusDirection::UpperRight: return "UR";
case FocusDirection::LowerLeft: return "LL";
case FocusDirection::LowerRight: return "LR";
}
XR_ASSUME(false);
return "";
}

std::array<FocusDirection, 3> allowed_directions(FocusDirection direction)
{
switch (direction)
Expand Down Expand Up @@ -253,8 +272,27 @@ bool CUIFocusSystem::FillDebugTree(const CUIDebugState& debugState)
{
if (ImGui::TreeNode(&m_valuable, "Valuable: %zu", m_valuable.size()))
{
for (auto& window : m_valuable)
const auto prevExamined = debugState.examined;
// Make sure we only draw debug data only in the focus system tree structure
debugState.examined = nullptr;

for (const auto& window : m_valuable)
{
const_cast<CUIWindow*>(window)->FillDebugTree(debugState);
}

if (CUIWindow* examinedWindow = dynamic_cast<CUIWindow*>(debugState.examined))
{
const auto& colors = debugState.settings.colors;
for (const auto& window : m_valuable)
{
if (window == examinedWindow)
continue;
DrawDebugInfo(*examinedWindow, *window, colors.directionArrow, colors.directionText);
}
}

debugState.examined = prevExamined;
ImGui::TreePop();
}
}
Expand Down Expand Up @@ -289,3 +327,31 @@ void CUIFocusSystem::FillDebugInfo()
ImGui::LabelText("Locker", "%s", m_focus_locker ? m_focus_locker->WindowName().c_str() : "none");
#endif
}

void CUIFocusSystem::DrawDebugInfo(const CUIWindow& from, const CUIWindow& to, u32 color, u32 textColor) const
{
#ifndef MASTER_GOLD
const auto mainVP = ImGui::GetMainViewport();
const auto draw_list = ImGui::GetForegroundDrawList(mainVP);

auto fromPos = from.GetAbsoluteCenterPos();
auto toPos = to.GetAbsoluteCenterPos();

UI().ClientToScreenScaled(fromPos);
UI().ClientToScreenScaled(toPos);

const auto direction = get_focus_direction(fromPos, toPos);
const auto text = short_direction(direction);
ImVec2 text_size = ImGui::CalcTextSize(text);

Fcolor clr = color;
const float radius = text_size.x > text_size.y ? text_size.x / 1.5f : text_size.y / 1.5f;

draw_list->AddLine((ImVec2&)fromPos, (ImVec2&)toPos, clr.get_windows(), 2.0f);
draw_list->AddCircleFilled((ImVec2&)toPos, radius, clr.get_windows());

clr = textColor;
toPos.sub((Fvector2&)(text_size /= 2));
draw_list->AddText((ImVec2&)toPos, clr.get_windows(), text);
#endif
}
2 changes: 2 additions & 0 deletions src/xrUICore/ui_focus.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ class XRUICORE_API CUIFocusSystem : public CUIDebuggable
pcstr GetDebugType() override { return "CUIFocusSystem"; }
bool FillDebugTree(const CUIDebugState& debugState) override;
void FillDebugInfo() override;

void DrawDebugInfo(const CUIWindow& from, const CUIWindow& to, u32 color, u32 textColor) const;
};

0 comments on commit 214fd3a

Please sign in to comment.