Skip to content

Commit

Permalink
gui editor - drag and drop sprites from asset window; texture multied…
Browse files Browse the repository at this point in the history
…itor
  • Loading branch information
nem0 committed Jun 17, 2024
1 parent 46fcc4b commit 274cd5e
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 143 deletions.
1 change: 1 addition & 0 deletions projects/genie.lua
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ function defaultConfigurations()
links { "pthread" }

configuration { "vs20*"}
defines { "_SILENCE_ALL_CXX20_DEPRECATION_WARNINGS" }
buildoptions { "/wd4503 /wd4251"}

configuration {}
Expand Down
84 changes: 67 additions & 17 deletions src/gui/editor/gui_plugins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
else return false;
return true;
}

bool hasFocus() const override { return m_has_focus; }

void onSettingsLoaded() override {
Expand All @@ -305,6 +306,46 @@ struct GUIEditor final : StudioApp::GUIPlugin
void onToggleOpen() { m_is_window_open = !m_is_window_open; }
bool isOpen() const { return m_is_window_open; }

void handleDrop(const char* path, const ImVec2& drop_pos, const ImVec2& canvas_size) {
if (!Path::hasExtension(path, "spr")) return;

WorldEditor& editor = m_app.getWorldEditor();
GUIModule* module = (GUIModule*)editor.getWorld()->getModule("gui");
EntityPtr entity = module->getRectAtEx(drop_pos, canvas_size, INVALID_ENTITY);
if (!entity) return;

const GUIModule::Rect rect = module->getRectEx(*entity, canvas_size);

editor.beginCommandGroup("gui_drop_sprite");
EntityRef child = editor.addEntity();
editor.makeParent(entity, child);
editor.selectEntities(Span(&child, 1), false);
editor.addComponent(Span(&child, 1), GUI_RECT_TYPE);
editor.addComponent(Span(&child, 1), GUI_IMAGE_TYPE);
editor.setProperty(GUI_IMAGE_TYPE, "", 0, "Sprite", Span(&child, 1), Path(path));

Sprite* sprite = m_app.getEngine().getResourceManager().load<Sprite>(Path(path));
Texture* texture = sprite->getTexture();
if (sprite->isReady() && texture) {
editor.setProperty(GUI_RECT_TYPE, "", 0, "Top Relative", Span(&child, 1), 0.f);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Bottom Relative", Span(&child, 1), 0.f);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Left Relative", Span(&child, 1), 0.f);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Right Relative", Span(&child, 1), 0.f);

float w = (float)texture->width;
float h = (float)texture->height;
float x = drop_pos.x - rect.x - w / 2;
float y = drop_pos.y - rect.y - h / 2;

editor.setProperty(GUI_RECT_TYPE, "", 0, "Top Points", Span(&child, 1), y);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Bottom Points", Span(&child, 1), y + h);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Left Points", Span(&child, 1), x);
editor.setProperty(GUI_RECT_TYPE, "", 0, "Right Points", Span(&child, 1), x + w);
}
sprite->decRefCount();

editor.endCommandGroup();
}

MouseMode drawGizmo(Draw2D& draw, GUIModule& module, const Vec2& canvas_size, const ImVec2& mouse_canvas_pos, Span<const EntityRef> selected_entities)
{
Expand Down Expand Up @@ -438,6 +479,13 @@ struct GUIEditor final : StudioApp::GUIPlugin
editor.endCommandGroup();
}

void menuActionItem(const Action& action, const char* label = nullptr) {
char shortcut[64];
getShortcut(action, Span(shortcut));
if (ImGui::MenuItem(label ? label : action.label_short.data, shortcut)) {
onAction(action);
}
}

void onGUI() override
{
Expand Down Expand Up @@ -517,8 +565,16 @@ struct GUIEditor final : StudioApp::GUIPlugin
ImGui::Image(m_texture_handle, size);
}
}

if (ImGui::BeginDragDropTarget()) {
if (auto* payload = ImGui::AcceptDragDropPayload("path")) {
handleDrop((const char*)payload->Data, mouse_canvas_pos, m_canvas_size);
}
ImGui::EndDragDropTarget();
}
}


if (ImGui::IsMouseClicked(0) && ImGui::IsItemHovered() && m_mouse_mode == MouseMode::NONE)
{
const Array<EntityRef>& selected = editor.getSelectedEntities();
Expand Down Expand Up @@ -556,48 +612,43 @@ struct GUIEditor final : StudioApp::GUIPlugin
if (has_rect && ImGui::BeginPopupContextItem("context"))
{
EntityRef e = editor.getSelectedEntities()[0];
if (ImGui::BeginMenu("Create child"))
{
if (ImGui::BeginMenu("Create child")) {
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);
if (ImGui::MenuItem("Text")) createChild(e, GUI_TEXT_TYPE, editor);
if (ImGui::MenuItem("Render target")) createChild(e, GUI_RENDER_TARGET_TYPE, editor);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Align"))
{
if (ImGui::BeginMenu("Align")) {
if (ImGui::MenuItem("Top")) align(e, (u8)EdgeMask::TOP, editor);
if (ImGui::MenuItem("Right")) align(e, (u8)EdgeMask::RIGHT, editor);
if (ImGui::MenuItem("Bottom")) align(e, (u8)EdgeMask::BOTTOM, editor);
if (ImGui::MenuItem("Left")) align(e, (u8)EdgeMask::LEFT, editor);
if (ImGui::MenuItem("Center horizontal")) align(e, (u8)EdgeMask::CENTER_HORIZONTAL, editor);
if (ImGui::MenuItem("Center vertical")) align(e, (u8)EdgeMask::CENTER_VERTICAL, editor);
menuActionItem(m_hcenter_action);
menuActionItem(m_vcenter_action);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Expand"))
{
if (ImGui::BeginMenu("Expand")) {
if (ImGui::MenuItem("All")) expand(e, (u8)EdgeMask::ALL, editor);
if (ImGui::MenuItem("Top")) expand(e, (u8)EdgeMask::TOP, editor);
if (ImGui::MenuItem("Right")) expand(e, (u8)EdgeMask::RIGHT, editor);
if (ImGui::MenuItem("Bottom")) expand(e, (u8)EdgeMask::BOTTOM, editor);
if (ImGui::MenuItem("Left")) expand(e, (u8)EdgeMask::LEFT, editor);
if (ImGui::MenuItem("Horizontal")) expand(e, (u8)EdgeMask::HORIZONTAL, editor);
if (ImGui::MenuItem("Vertical")) expand(e, (u8)EdgeMask::VERTICAL, editor);
menuActionItem(m_hexpand_action, "Horizontal");
menuActionItem(m_vexpand_action, "Vertical");
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Make relative"))
{
if (ImGui::MenuItem("All")) makeRelative(e, toLumix(size), (u8)EdgeMask::ALL, editor);
if (ImGui::BeginMenu("Make relative")) {
menuActionItem(m_make_rel_action, "All");
if (ImGui::MenuItem("Top")) makeRelative(e, toLumix(size), (u8)EdgeMask::TOP, editor);
if (ImGui::MenuItem("Right")) makeRelative(e, toLumix(size), (u8)EdgeMask::RIGHT, editor);
if (ImGui::MenuItem("Bottom")) makeRelative(e, toLumix(size), (u8)EdgeMask::BOTTOM, editor);
if (ImGui::MenuItem("Left")) makeRelative(e, toLumix(size), (u8)EdgeMask::LEFT, editor);

ImGui::EndMenu();
}
if (ImGui::BeginMenu("Make absolute"))
{
if (ImGui::BeginMenu("Make absolute")) {
if (ImGui::MenuItem("All")) makeAbsolute(e, toLumix(size), (u8)EdgeMask::ALL, editor);
if (ImGui::MenuItem("Top")) makeAbsolute(e, toLumix(size), (u8)EdgeMask::TOP, editor);
if (ImGui::MenuItem("Right")) makeAbsolute(e, toLumix(size), (u8)EdgeMask::RIGHT, editor);
Expand All @@ -617,8 +668,7 @@ struct GUIEditor final : StudioApp::GUIPlugin
if (ImGui::MenuItem("Bottom right")) anchor(e, (u8)EdgeMask::BOTTOM | (u8)EdgeMask::RIGHT, editor);
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Copy position"))
{
if (ImGui::BeginMenu("Copy position")) {
if (ImGui::MenuItem("All")) copy(e, (u8)EdgeMask::ALL, editor);
if (ImGui::MenuItem("Top")) copy(e, (u8)EdgeMask::TOP, editor);
if (ImGui::MenuItem("Right")) copy(e, (u8)EdgeMask::RIGHT, editor);
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/editor/fbx_importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct FBXImporter {
CENTER_EACH_MESH // center each mesh in fbx separately, when exporting each mesh as a subresources
};

enum class Physics {
enum class Physics : i32 {
NONE,
CONVEX,
TRIMESH
Expand Down
Loading

0 comments on commit 274cd5e

Please sign in to comment.