Skip to content

Commit 2de1836

Browse files
committed
Added support for custom actions in GroupCollapsable (e.g. Edit button for scripts)
1 parent 300911c commit 2de1836

5 files changed

Lines changed: 50 additions & 13 deletions

File tree

Resources/Editor/Textures/Edit.png

216 Bytes
Loading

Sources/OvEditor/src/OvEditor/Core/EditorResources.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ OvEditor::Core::EditorResources::EditorResources(const std::string& p_editorAsse
133133
{"Ambient_Sphere_Light", CreateTexture<NEAREST>(texturesFolder / "Ambient_Sphere_Light.png")},
134134
{"Empty_Texture", CreateTexture<LINEAR>(texturesFolder / "Empty_Texture.png")},
135135
{"Actor", CreateTexture<LINEAR>(texturesFolder / "Actor.png")},
136-
{"Search", CreateTexture<LINEAR>(texturesFolder / "Search.png")}
136+
{"Search", CreateTexture<LINEAR>(texturesFolder / "Search.png")},
137+
{"Edit", CreateTexture<LINEAR>(texturesFolder / "Edit.png")}
137138
};
138139

139140
m_models = {

Sources/OvEditor/src/OvEditor/Panels/Inspector.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,12 @@ void OvEditor::Panels::Inspector::_DrawBehaviour(Behaviour& p_behaviour, int p_i
378378
header.MoveUpEvent += [move] { move(true); };
379379
header.MoveDownEvent += [move] { move(false); };
380380

381+
const uint32_t editIconID = EDITOR_CONTEXT(editorResources)->GetTexture("Edit")->GetTexture().GetID();
382+
const auto scriptPath = EDITOR_CONTEXT(projectAssetsPath) / p_behaviour.name;
383+
header.actions.push_back({editIconID, [scriptPath] {
384+
EDITOR_EXEC(OpenInCodeEditor(scriptPath));
385+
}});
386+
381387
auto& columns = header.CreateWidget<Layout::Columns<2>>();
382388
columns.SetID("bhv_" + p_behaviour.name);
383389
columns.widths[0] = 200 * OVUI_SCALE;

Sources/OvUI/include/OvUI/Widgets/Layout/GroupCollapsable.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#pragma once
88

9+
#include <functional>
910
#include <vector>
1011

1112
#include <OvTools/Eventing/Event.h>
@@ -20,6 +21,15 @@ namespace OvUI::Widgets::Layout
2021
class GroupCollapsable : public Group
2122
{
2223
public:
24+
/**
25+
* Represents a clickable icon button drawn inside the collapsable header
26+
*/
27+
struct HeaderAction
28+
{
29+
uint32_t textureID;
30+
std::function<void()> callback;
31+
};
32+
2333
/**
2434
* Constructor
2535
* @param p_name
@@ -36,6 +46,7 @@ namespace OvUI::Widgets::Layout
3646
bool reorderable = false;
3747
bool canMoveUp = true;
3848
bool canMoveDown = true;
49+
std::vector<HeaderAction> actions;
3950
OvTools::Eventing::Event<> CloseEvent;
4051
OvTools::Eventing::Event<> OpenEvent;
4152
OvTools::Eventing::Event<> MoveUpEvent;

Sources/OvUI/src/OvUI/Widgets/Layout/GroupCollapsable.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <imgui.h>
88
#include <imgui_internal.h>
99

10+
#include "OvUI/Internal/TextureID.h"
1011
#include "OvUI/Widgets/Layout/GroupCollapsable.h"
1112

1213
OvUI::Widgets::Layout::GroupCollapsable::GroupCollapsable(const std::string & p_name) :
@@ -22,7 +23,7 @@ void OvUI::Widgets::Layout::GroupCollapsable::_Draw_Impl()
2223

2324
EndDisableOverride(); // Early end disable override group so that children are not affected
2425

25-
if (reorderable)
26+
if (reorderable || !actions.empty())
2627
{
2728
const ImVec2 headerMin = ImGui::GetItemRectMin();
2829
const ImVec2 headerMax = ImGui::GetItemRectMax();
@@ -32,22 +33,40 @@ void OvUI::Widgets::Layout::GroupCollapsable::_Draw_Impl()
3233

3334
// Keep original vertical padding so arrow buttons fill the header height exactly
3435
const float padY = ImGui::GetStyle().FramePadding.y;
35-
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{0, padY});
36+
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2{padY, padY});
3637
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4{0, 0, 0, 0});
3738
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImGui::GetStyleColorVec4(ImGuiCol_HeaderHovered));
3839
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImGui::GetStyleColorVec4(ImGuiCol_HeaderActive));
3940

40-
ImGui::SetCursorScreenPos({rightEdge - bSize * 2.0f, headerMin.y});
41-
if (!canMoveUp) ImGui::BeginDisabled();
42-
if (ImGui::ArrowButton(("##mu" + m_widgetID).c_str(), ImGuiDir_Up))
43-
MoveUpEvent.Invoke();
44-
if (!canMoveUp) ImGui::EndDisabled();
41+
if (reorderable)
42+
{
43+
ImGui::SetCursorScreenPos({rightEdge - bSize * 2.0f, headerMin.y});
44+
if (!canMoveUp) ImGui::BeginDisabled();
45+
if (ImGui::ArrowButton(("##mu" + m_widgetID).c_str(), ImGuiDir_Up))
46+
MoveUpEvent.Invoke();
47+
if (!canMoveUp) ImGui::EndDisabled();
4548

46-
ImGui::SetCursorScreenPos({rightEdge - bSize, headerMin.y});
47-
if (!canMoveDown) ImGui::BeginDisabled();
48-
if (ImGui::ArrowButton(("##md" + m_widgetID).c_str(), ImGuiDir_Down))
49-
MoveDownEvent.Invoke();
50-
if (!canMoveDown) ImGui::EndDisabled();
49+
ImGui::SetCursorScreenPos({rightEdge - bSize, headerMin.y});
50+
if (!canMoveDown) ImGui::BeginDisabled();
51+
if (ImGui::ArrowButton(("##md" + m_widgetID).c_str(), ImGuiDir_Down))
52+
MoveDownEvent.Invoke();
53+
if (!canMoveDown) ImGui::EndDisabled();
54+
}
55+
56+
const float actionsRight = rightEdge - (reorderable ? 2.0f * bSize : 0.0f);
57+
for (int i = static_cast<int>(actions.size()) - 1; i >= 0; --i)
58+
{
59+
ImGui::SetCursorScreenPos({actionsRight - static_cast<float>(actions.size() - i) * bSize, headerMin.y});
60+
Internal::TextureID texID;
61+
texID.id = actions[i].textureID;
62+
if (ImGui::ImageButtonEx(
63+
ImGui::GetID(("##act" + std::to_string(i) + m_widgetID).c_str()),
64+
texID.id,
65+
{bSize - 2.0f * padY, bSize - 2.0f * padY},
66+
{0.f, 1.f}, {1.f, 0.f},
67+
{0, 0, 0, 0}, {1, 1, 1, 1}))
68+
actions[i].callback();
69+
}
5170

5271
ImGui::PopStyleColor(3);
5372
ImGui::PopStyleVar();

0 commit comments

Comments
 (0)