Skip to content

Commit

Permalink
Merge pull request #183 from JacobDomagala/182-editor-remove-unwanted…
Browse files Browse the repository at this point in the history
…-stdstring-copy

[#182]: Improve performance for selected Object in Editor
  • Loading branch information
JacobDomagala authored Jan 11, 2024
2 parents 8be9d2a + 71eae31 commit c35754f
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
3 changes: 2 additions & 1 deletion editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,11 @@ Editor::MouseButtonCallback(MouseButtonEvent& event)
if (!playGame_ && !EditorGUI::IsBlockingEvents() && levelLoaded_)
{
const auto mousePressed = event.action_ == GLFW_PRESS;
const auto mouseReleased = event.action_ == GLFW_RELEASE;
LMBPressedLastUpdate_ = mousePressed and event.button_ == GLFW_MOUSE_BUTTON_1;
RMBPressedLastUpdate_ = mousePressed and event.button_ == GLFW_MOUSE_BUTTON_2;

if (mousePressed)
if (mouseReleased and not mouseDrag_)
{
CheckIfObjectGotSelected(InputManager::GetMousePos(),
InputManager::CheckKeyPressed(GLFW_KEY_LEFT_CONTROL));
Expand Down
58 changes: 32 additions & 26 deletions editor/gui/editor_gui_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#include "game_object.hpp"
#include "helpers.hpp"
#include "icons.hpp"
#include "input/input_manager.hpp"
#include "renderer/renderer.hpp"
#include "renderer/shader.hpp"
#include "renderer/texture.hpp"
#include "renderer/types.hpp"
#include "renderer/vulkan_common.hpp"
#include "types.hpp"
#include "utils/file_manager.hpp"
#include "input/input_manager.hpp"

#include <GLFW/glfw3.h>
#include <fmt/format.h>
Expand Down Expand Up @@ -238,8 +238,6 @@ EditorGUI::RenderLevelMenu() // NOLINT
ImGui::SetNextItemOpen(true);
if (ImGui::CollapsingHeader("Objects"))
{
const auto& gameObjects = currentLevel_->GetObjects();

const auto filterObjects = std::to_array< std::string >({"All", "Enemy", "Player", "Object"});

// NOLINTBEGIN
Expand Down Expand Up @@ -267,46 +265,54 @@ EditorGUI::RenderLevelMenu() // NOLINT

ImGui::BeginChild("Loaded Objects", {0, 200}, true);

auto DisplayObjects = [this](const auto& objects) {
for (const auto& object : objects)
{
const auto& objectInfo = objectsInfo_.at(object.GetID());
auto label = objectInfo.first;
auto DisplayObject = [this](const auto& object) {
const auto& objectInfo = objectsInfo_.at(object.GetID());

if (ImGui::Selectable(label.c_str(), objectInfo.second))
{
parent_.GetCamera().SetCameraAtPosition(object.GetPosition());
parent_.HandleGameObjectSelected(object.GetID(), false, true);
if (ImGui::Selectable(objectInfo.first.c_str(), objectInfo.second))
{
parent_.GetCamera().SetCameraAtPosition(object.GetPosition());
parent_.HandleGameObjectSelected(object.GetID(), false, true);

// Don't make the UI jump
setScrollTo_ = {};
}
// Don't make the UI jump
setScrollTo_ = {};
}

if (setScrollTo_ == object.GetID())
{
// Scroll to make this widget visible
ImGui::SetScrollHereY();
}
if (setScrollTo_ == object.GetID())
{
// Scroll to make this widget visible
ImGui::SetScrollHereY();
}
};

if (selectedFilter == "Object")
{
DisplayObjects(gameObjects);
for (const auto& object : currentLevel_->GetObjects())
{
DisplayObject(object);
}
}
else if (selectedFilter == "Enemy")
{
DisplayObjects(parent_.GetLevel().GetEnemies());
for (const auto& object : parent_.GetLevel().GetEnemies())
{
DisplayObject(object);
}
}
else if (selectedFilter == "Player")
{
DisplayObjects(std::vector< GameObject >{parent_.GetPlayer()});
DisplayObject(parent_.GetPlayer());
}
else if (selectedFilter == "All")
{
DisplayObjects(std::vector< GameObject >{parent_.GetPlayer()});
DisplayObjects(parent_.GetLevel().GetEnemies());
DisplayObjects(gameObjects);
DisplayObject(parent_.GetPlayer());
for (const auto& object : parent_.GetLevel().GetEnemies())
{
DisplayObject(object);
}
for (const auto& object : currentLevel_->GetObjects())
{
DisplayObject(object);
}
}

ImGui::EndChild();
Expand Down
9 changes: 4 additions & 5 deletions editor/gui/editor_gui_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ EditorGUI::RenderSelectedObjectsMenu()
{
const auto& objectInfo = objectsInfo_.at(object);

auto label = objectInfo.first;

if (ImGui::Selectable(label.c_str(), currentlySelectedGameObject_ != Object::INVALID_ID
? currentlySelectedGameObject_ == object
: false))
if (ImGui::Selectable(objectInfo.first.c_str(),
currentlySelectedGameObject_ != Object::INVALID_ID
? currentlySelectedGameObject_ == object
: false))
{
const auto& gameObject = currentLevel_->GetGameObjectRef(object);
parent_.GetCamera().SetCameraAtPosition(gameObject.GetPosition());
Expand Down
2 changes: 1 addition & 1 deletion engine/game/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ Level::GetTilesFromRectangle(const std::array< glm::vec2, 4 >& rect) const
const auto w =
static_cast< int32_t >(glm::floor(position.x / static_cast< float >(tileWidth)));
const auto h =
static_cast< int32_t >(glm::floor(position.y / static_cast< float >(tileWidth)));
static_cast< int32_t >(glm::ceil(position.y / static_cast< float >(tileWidth)));

return {glm::clamp(w, 0, (levelSize.x / static_cast< int32_t >(tileWidth)) - 1),
glm::clamp(h, 0, (levelSize.y / static_cast< int32_t >(tileWidth))) - 1};
Expand Down

0 comments on commit c35754f

Please sign in to comment.