Skip to content

Commit

Permalink
Add insert options
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshuaMKW committed Mar 7, 2024
1 parent 09db690 commit d4ca5dd
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
25 changes: 18 additions & 7 deletions include/gui/scene/objdialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,33 @@

#include <array>
#include <functional>
#include <vector>
#include <imgui.h>
#include <vector>

#include "core/memory.hpp"
#include "core/error.hpp"
#include "core/memory.hpp"
#include "gui/scene/nodeinfo.hpp"
#include "objlib/template.hpp"

namespace Toolbox::UI {

class CreateObjDialog {
public:
using action_t = std::function<void(size_t,
std::string_view, const Object::Template &, std::string_view, SelectionNodeInfo<Object::ISceneObject>)>;
enum class InsertPolicy {
INSERT_BEFORE,
INSERT_AFTER,
INSERT_CHILD,
};

using action_t =
std::function<void(size_t, std::string_view, const Object::Template &, std::string_view,
InsertPolicy, SelectionNodeInfo<Object::ISceneObject>)>;
using cancel_t = std::function<void(SelectionNodeInfo<Object::ISceneObject>)>;

CreateObjDialog() = default;
~CreateObjDialog() = default;

void setInsertPolicy(InsertPolicy policy) { m_insert_policy = policy; }
void setActionOnAccept(action_t on_accept) { m_on_accept = on_accept; }
void setActionOnReject(cancel_t on_reject) { m_on_reject = on_reject; }

Expand All @@ -33,7 +41,7 @@ namespace Toolbox::UI {
void render(SelectionNodeInfo<Object::ISceneObject> node_info);

private:
bool m_open = false;
bool m_open = false;
bool m_opening = false;

int m_template_index = -1;
Expand All @@ -44,6 +52,8 @@ namespace Toolbox::UI {

std::array<char, 128> m_object_name = {};

InsertPolicy m_insert_policy = InsertPolicy::INSERT_BEFORE;

action_t m_on_accept;
cancel_t m_on_reject;

Expand All @@ -52,7 +62,8 @@ namespace Toolbox::UI {

class RenameObjDialog {
public:
using action_t = std::function<void(std::string_view, SelectionNodeInfo<Object::ISceneObject>)>;
using action_t =
std::function<void(std::string_view, SelectionNodeInfo<Object::ISceneObject>)>;
using cancel_t = std::function<void(SelectionNodeInfo<Object::ISceneObject>)>;

RenameObjDialog() = default;
Expand All @@ -67,7 +78,7 @@ namespace Toolbox::UI {
std::copy(cropped_name.begin(), cropped_name.end(), m_original_name.begin());
std::copy(cropped_name.begin(), cropped_name.end(), m_object_name.begin());
m_original_name.at(end_pos) = '\0';
m_object_name.at(end_pos) = '\0';
m_object_name.at(end_pos) = '\0';
}

void setup();
Expand Down
4 changes: 2 additions & 2 deletions src/gui/scene/objdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ namespace Toolbox::UI {
}

if (ImGui::Button("Create")) {
m_on_accept(0, proposed_name, *m_templates.at(m_template_index),
m_on_accept(node_info.m_selection_index, proposed_name, *m_templates.at(m_template_index),
m_templates.at(m_template_index)->wizards().at(m_wizard_index).m_name,
node_info);
m_insert_policy, node_info);
m_open = false;
}

Expand Down
67 changes: 55 additions & 12 deletions src/gui/scene/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,17 @@ namespace Toolbox::UI {
m_hierarchy_virtual_node_menu = ContextMenu<SelectionNodeInfo<Object::ISceneObject>>();

m_hierarchy_virtual_node_menu.addOption(
"Insert Object Here...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_I},
"Insert Object Before...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_BEFORE);
m_create_obj_dialog.open();
return;
});

m_hierarchy_virtual_node_menu.addOption(
"Insert Object After...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_AFTER);
m_create_obj_dialog.open();
return;
});
Expand Down Expand Up @@ -1242,12 +1251,29 @@ namespace Toolbox::UI {
void SceneWindow::buildContextMenuGroupObj() {
m_hierarchy_group_node_menu = ContextMenu<SelectionNodeInfo<Object::ISceneObject>>();

m_hierarchy_group_node_menu.addOption("Add Child Object...",
{GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.open();
return;
});
m_hierarchy_group_node_menu.addOption(
"Add Child Object...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_CHILD);
m_create_obj_dialog.open();
return;
});

m_hierarchy_group_node_menu.addOption(
"Insert Object Before...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_BEFORE);
m_create_obj_dialog.open();
return;
});

m_hierarchy_group_node_menu.addOption(
"Insert Object After...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_AFTER);
m_create_obj_dialog.open();
return;
});

m_hierarchy_group_node_menu.addDivider();

Expand Down Expand Up @@ -1325,8 +1351,17 @@ namespace Toolbox::UI {
m_hierarchy_physical_node_menu = ContextMenu<SelectionNodeInfo<Object::ISceneObject>>();

m_hierarchy_physical_node_menu.addOption(
"Insert Object Here...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
"Insert Object Before...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_BEFORE);
m_create_obj_dialog.open();
return;
});

m_hierarchy_physical_node_menu.addOption(
"Insert Object After...", {GLFW_KEY_LEFT_CONTROL, GLFW_KEY_N},
[this](SelectionNodeInfo<Object::ISceneObject> info) {
m_create_obj_dialog.setInsertPolicy(CreateObjDialog::InsertPolicy::INSERT_AFTER);
m_create_obj_dialog.open();
return;
});
Expand Down Expand Up @@ -1815,18 +1850,26 @@ namespace Toolbox::UI {
m_create_obj_dialog.setup();
m_create_obj_dialog.setActionOnAccept(
[this](size_t sibling_index, std::string_view name, const Object::Template &template_,
std::string_view wizard_name, SelectionNodeInfo<Object::ISceneObject> info) {
std::string_view wizard_name, CreateObjDialog::InsertPolicy policy,
SelectionNodeInfo<Object::ISceneObject> info) {
auto new_object_result = Object::ObjectFactory::create(template_, wizard_name);
if (!name.empty()) {
new_object_result->setNameRef(name);
}

size_t insert_index;

GroupSceneObject *this_parent;
if (info.m_selected->isGroupObject()) {
this_parent = reinterpret_cast<GroupSceneObject *>(info.m_selected.get());
if (info.m_selected->isGroupObject() &&
policy == CreateObjDialog::InsertPolicy::INSERT_CHILD) {
this_parent = reinterpret_cast<GroupSceneObject *>(info.m_selected.get());
insert_index = this_parent->getChildren().size();
} else {
this_parent =
reinterpret_cast<GroupSceneObject *>(info.m_selected->getParent());
insert_index = policy == CreateObjDialog::InsertPolicy::INSERT_BEFORE
? sibling_index
: sibling_index + 1;
}

if (!this_parent) {
Expand All @@ -1836,7 +1879,7 @@ namespace Toolbox::UI {
return;
}

auto result = this_parent->insertChild(sibling_index, std::move(new_object_result));
auto result = this_parent->insertChild(insert_index, std::move(new_object_result));
if (!result) {
logObjectGroupError(result.error());
return;
Expand Down

0 comments on commit d4ca5dd

Please sign in to comment.