Skip to content

Commit

Permalink
gui editor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Jul 16, 2024
1 parent cb093a5 commit d706fe3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
61 changes: 57 additions & 4 deletions src/gui/editor/gui_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ using namespace Lumix;

namespace {

static const ComponentType GUI_CANVAS_TYPE = reflection::getComponentType("gui_canvas");
static const ComponentType GUI_RECT_TYPE = reflection::getComponentType("gui_rect");
static const ComponentType GUI_IMAGE_TYPE = reflection::getComponentType("gui_image");
static const ComponentType GUI_TEXT_TYPE = reflection::getComponentType("gui_text");
Expand Down Expand Up @@ -499,16 +500,50 @@ struct GUIEditor final : StudioApp::GUIPlugin
mouse_canvas_pos.x -= ImGui::GetCursorScreenPos().x;
mouse_canvas_pos.y -= ImGui::GetCursorScreenPos().y;

m_pipeline->setWorld(editor.getWorld());

GUIModule* module = (GUIModule*)editor.getWorld()->getModule("gui");
HashMap<EntityRef, GUICanvas>& canvases = module->getCanvases();
if (!m_canvas_entity.isValid() && canvases.size() > 0) {
m_canvas_entity = canvases.begin().key();
}

if (canvases.size() > 1) {
char entity_name[64] = "N/A";
getEntityListDisplayName(m_app, *editor.getWorld(), Span(entity_name), m_canvas_entity);
if (ImGui::BeginCombo("Canvas", entity_name)) {
for (auto iter = canvases.begin(), end = canvases.end(); iter != end; ++iter) {
getEntityListDisplayName(m_app, *editor.getWorld(), Span(entity_name), iter.key());
if (ImGui::Selectable(entity_name)) {
m_canvas_entity = iter.key();
}
}
ImGui::EndCombo();
}
}

if (!m_canvas_entity.isValid()) {
if (canvases.empty()) {
ImGui::TextUnformatted("No canvases found.");
if (ImGui::Button("Create canvas")) {
editor.beginCommandGroup("create_gui_canvas");
EntityRef e = editor.addEntity();
editor.setEntityName(e, "GUI canvas");
editor.addComponent(Span(&e, 1), GUI_CANVAS_TYPE);
editor.addComponent(Span(&e, 1), GUI_RECT_TYPE);
editor.endCommandGroup();
}
}
ImGui::End();
return;
}

const ImVec2 size = ImGui::GetContentRegionAvail();
m_canvas_size = size;
if (!m_pipeline->isReady() || size.x == 0 || size.y == 0) {
ImGui::End();
return;
}

m_pipeline->setWorld(editor.getWorld());

GUIModule* module = (GUIModule*)editor.getWorld()->getModule("gui");
module->render(*m_pipeline, { size.x, size.y }, false);

MouseMode new_mode = drawGizmo(m_pipeline->getDraw2D(), *module, { size.x, size.y }, mouse_canvas_pos, editor.getSelectedEntities());
Expand Down Expand Up @@ -613,6 +648,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
{
EntityRef e = editor.getSelectedEntities()[0];
if (ImGui::BeginMenu("Create child")) {
if (ImGui::MenuItem("Button + Image + Text")) createChildren(e, editor, GUI_BUTTON_TYPE, GUI_IMAGE_TYPE, GUI_TEXT_TYPE);
if (ImGui::MenuItem("Button")) createChild(e, GUI_BUTTON_TYPE, editor);
if (ImGui::MenuItem("Image")) createChild(e, GUI_IMAGE_TYPE, editor);
if (ImGui::MenuItem("Rect")) createChild(e, GUI_RECT_TYPE, editor);
Expand Down Expand Up @@ -744,6 +780,22 @@ struct GUIEditor final : StudioApp::GUIPlugin
}


void createChildren(EntityRef entity, WorldEditor& editor, ComponentType child_type0, ComponentType child_type1, ComponentType child_type2) {
editor.beginCommandGroup("create_gui_rect_child");
EntityRef child = editor.addEntity();
editor.makeParent(entity, child);
editor.selectEntities(Span(&child, 1), false);
editor.addComponent(Span(&child, 1), GUI_RECT_TYPE);
ASSERT(child_type0 != GUI_RECT_TYPE);
ASSERT(child_type1 != GUI_RECT_TYPE);
ASSERT(child_type2 != GUI_RECT_TYPE);
editor.addComponent(Span(&child, 1), child_type0);
editor.addComponent(Span(&child, 1), child_type1);
editor.addComponent(Span(&child, 1), child_type2);
editor.endCommandGroup();

}

void createChild(EntityRef entity, ComponentType child_type, WorldEditor& editor)
{
editor.beginCommandGroup("create_gui_rect_child");
Expand Down Expand Up @@ -985,6 +1037,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
Vec2 m_bottom_right_start_transform;
Vec2 m_top_left_start_move;
Vec2 m_canvas_size;
EntityPtr m_canvas_entity = INVALID_ENTITY;

Action m_hcenter_action;
Action m_vcenter_action;
Expand Down
4 changes: 4 additions & 0 deletions src/gui/gui_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,10 @@ struct GUIModuleImpl final : GUIModule {
GUIImage* image = m_rects[entity]->image;
return image->sprite ? image->sprite->getPath() : Path();
}

HashMap<EntityRef, GUICanvas>& getCanvases() override {
return m_canvas;
}

GUICanvas& getCanvas(EntityRef entity) override {
return m_canvas[entity];
Expand Down
1 change: 1 addition & 0 deletions src/gui/gui_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct GUIModule : IModule
virtual void setImageSprite(EntityRef entity, const Path& path) = 0;

virtual GUICanvas& getCanvas(EntityRef entity) = 0;
virtual HashMap<EntityRef, GUICanvas>& getCanvases() = 0;

virtual void setText(EntityRef entity, const char* text) = 0;
virtual const char* getText(EntityRef entity) = 0;
Expand Down

0 comments on commit d706fe3

Please sign in to comment.