Skip to content

Commit

Permalink
[#197]: Add option to rename existing group
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Mar 18, 2024
1 parent fa11c20 commit 4fbbaa8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 21 deletions.
2 changes: 1 addition & 1 deletion assets/levels/MediumLevel.dgl

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/levels/MediumLevel.editor.dgl

Large diffs are not rendered by default.

54 changes: 51 additions & 3 deletions editor/gui/editor_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,53 @@ EditorGUI::CreateNewGroup()
ImGui::End();
}

void
EditorGUI::EditGroup(const std::string& oldName)
{
const auto halfSize = windowSize_ / 2.0f;

ImGui::SetNextWindowPos({halfSize.x - 160, halfSize.y - 40});
ImGui::SetNextWindowSize({300, 140});
ImGui::Begin("Edit Group", nullptr, ImGuiWindowFlags_NoResize);

ImGui::Text("Name:");
ImGui::Dummy(ImVec2(2.0f, 0.0f));
ImGui::SameLine();
ImGui::SetNextItemWidth(ImGui::GetWindowWidth() * 0.6f);

static std::string newName = oldName;
ImGui::InputText("##NewGroupName", newName.data(), newName.capacity() + 1);

ImGui::Dummy(ImVec2(0.0f, 5.0f));
ImGui::Dummy(ImVec2(ImGui::GetWindowWidth() / 10.0f, 0.0f));
ImGui::SameLine();

if (ImGui::Button("Rename", {ImGui::GetWindowWidth() / 3.0f, 35}))
{
renameGroupPushed_ = false;
auto& objects = groups_.at(oldName);
for (auto obj : objects)
{
objectsInfo_.at(obj).groupName = newName;
}

*(stl::find(groupNames_, oldName)) = newName;

groups_[newName] = groups_.at(oldName);
groups_.erase(oldName);
}

ImGui::SameLine();
ImGui::Dummy(ImVec2(2.0f, 0.0f));
ImGui::SameLine();
if (ImGui::Button("Cancel", {ImGui::GetWindowWidth() / 3.0f, 35}))
{
renameGroupPushed_ = false;
}

ImGui::End();
}

void
EditorGUI::KeyCallback(KeyEvent& event)
{
Expand Down Expand Up @@ -329,9 +376,10 @@ EditorGUI::ObjectSelected(Object::ID ID, bool groupSelect)
setScrollTo_ = ID;

const auto& gameObject = parent_.GetLevel().GetGameObjectRef(ID);
selectedObjects_.emplace_back(ID, gameObject.GetHasCollision(),
gameObject.GetSprite().GetRenderInfo().layer,
objectsInfo_.at(ID).groupName);
selectedObjects_.emplace_back(SelectedObjectInfo{ID, gameObject.GetHasCollision(),
gameObject.GetSprite().GetRenderInfo().layer,
objectsInfo_.at(ID).groupName});


RecalculateCommonProperties();

Expand Down
4 changes: 4 additions & 0 deletions editor/gui/editor_gui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ class EditorGUI : public InputListener
void
CreateNewGroup();

void
EditGroup(const std::string& oldName);

void
UpdateGroupForSelection(const std::string& groupName);

Expand Down Expand Up @@ -168,6 +171,7 @@ class EditorGUI : public InputListener

// Group data
bool newGroupPushed_ = false;
bool renameGroupPushed_ = false;
std::unordered_map< std::string, std::vector< Object::ID > > groups_ = {};
std::vector< std::string > groupNames_ = {"Create New"};
std::string selectedGroup_ = "";
Expand Down
53 changes: 37 additions & 16 deletions editor/gui/editor_gui_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -418,28 +418,49 @@ EditorGUI::RenderLevelMenu() // NOLINT
ImGui::SetNextItemOpen(true);
if (ImGui::CollapsingHeader("Groups"))
{
ImGui::BeginChild("Groups", {0, groupNames_.size() * 32.0f}, true);
ImGui::BeginTable("GroupsTable", 2);

const auto totalWidth = ImGui::GetContentRegionAvail().x;
ImGui::TableSetupColumn("GroupName", ImGuiTableColumnFlags_WidthStretch, 0.90f * totalWidth);
ImGui::TableSetupColumn("EditGroup", ImGuiTableColumnFlags_WidthStretch, 0.10f * totalWidth);

static std::string groupNameToEdit = "";
for (uint32_t idx = 1; idx < groupNames_.size(); ++idx)
{
const auto& groupName = groupNames_.at(idx);
bool selected = selectedGroup_ == groupName;
if (ImGui::Selectable(groupName.c_str(), selected))
{
parent_.UnselectAll();
selectedGroup_ = groupName;
CreateActionRow(
[this, idx] {
const auto& groupName = groupNames_.at(idx);
bool selected = selectedGroup_ == groupName;

const auto& objectsInGroup = groups_.at(groupName);
for (const auto obj : objectsInGroup)
{
ObjectSelected(obj, true);
}
if (ImGui::Selectable(groupName.c_str(), selected))
{
parent_.UnselectAll();
selectedGroup_ = groupName;

parent_.ChangeSelectedObjects(objectsInGroup);
break;
}
const auto& objectsInGroup = groups_.at(groupName);
for (const auto obj : objectsInGroup)
{
ObjectSelected(obj, true);
}

parent_.ChangeSelectedObjects(objectsInGroup);
}
},
[this, idx] {
if (ImGui::Button(std::format("{} ##ChangeGroupButton{}", ICON_FA_PENCIL, idx).c_str()))
{
groupNameToEdit = groupNames_.at(idx);
renameGroupPushed_ = true;
}
});
}

ImGui::EndChild();
if (renameGroupPushed_)
{
EditGroup(groupNameToEdit);
}

ImGui::EndTable();
}

ImGui::SetNextItemOpen(true);
Expand Down

0 comments on commit 4fbbaa8

Please sign in to comment.