Skip to content

Commit

Permalink
[#141]: Unselect the group with Escape
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Dec 11, 2023
1 parent 1fb1df7 commit 30de80e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
22 changes: 15 additions & 7 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Editor::KeyCallback(KeyEvent& event)
if (event.key_ == GLFW_KEY_ESCAPE)
{
ActionOnObject(ACTION::UNSELECT);
selectedObjects_.clear();
event.handled_ = true;
}

Expand Down Expand Up @@ -140,11 +141,16 @@ Editor::MouseButtonCallback(MouseButtonEvent& event)

movementOnEditorObject_ = false;
movementOnGameObject_ = false;
mouseDrag_ = false;

selectedObjects_ = GetObjectsInArea(selectRect_);
selectStartPos_ = glm::vec2{};
selectRect_ = std::array< glm::vec2, 4 >{};
if (selectingObjects_)
{
selectedObjects_ = GetObjectsInArea(selectRect_);
selectStartPos_ = glm::vec2{};
selectRect_ = std::array< glm::vec2, 4 >{};
}

mouseDrag_ = false;
selectingObjects_ = false;
}

event.handled_ = true;
Expand Down Expand Up @@ -179,8 +185,10 @@ Editor::HandleMouseDrag(const glm::vec2& currentCursorPos, const glm::vec2& axis
{
const auto globalPos = ScreenToGlobal(currentCursorPos);

// Initial mouse drag on selection
if (selectStartPos_ == glm::vec2{})
{
selectingObjects_ = true;
selectStartPos_ = globalPos;
}

Expand Down Expand Up @@ -339,7 +347,7 @@ Editor::HandleObjectSelected(Object::ID objectID, bool fromGUI)
switch (Object::GetTypeFromID(objectID))
{
case ObjectType::ANIMATION_POINT: {
auto it = std::ranges::find_if(animationPoints_, [objectID](const auto& point) {
auto it = stl::find_if(animationPoints_, [objectID](const auto& point) {
return point->GetLinkedObjectID() == objectID;
});

Expand Down Expand Up @@ -506,7 +514,6 @@ Editor::GetObjectsInArea(const std::array< glm::vec2, 4 >& area) const
{
std::set< Object::ID > objectsList = {};

const auto& objects = m_currentLevel->GetObjects();
const auto tiles = m_currentLevel->GetTilesFromRectangle(area);
auto pathfinder = m_currentLevel->GetPathfinder();

Expand Down Expand Up @@ -573,12 +580,13 @@ bool
Editor::IsAnyObjectSelected() const
{
return editorObjectSelected_ or currentEditorObjectSelected_ or gameObjectSelected_
or currentSelectedGameObject_;
or currentSelectedGameObject_ or not selectedObjects_.empty();
}

void
Editor::ActionOnObject(Editor::ACTION action, const std::optional< Object::ID >& object)
{
// This moves the camera to the object
if (object)
{
HandleObjectSelected(object.value(), true);
Expand Down
1 change: 1 addition & 0 deletions editor/editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ class Editor : public Application
bool RMBPressedLastUpdate_ = false;

bool mouseDrag_ = false;
bool selectingObjects_ = false;

glm::vec2 selectStartPos_ = {};
std::array< glm::vec2, 4 > selectRect_ = {};
Expand Down
10 changes: 4 additions & 6 deletions engine/game/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ Level::Load(Application* context, const std::string& pathToLevel)
// BACKGROUND
{
const auto& background = json["BACKGROUND"];
const auto backgroundTex = background["texture"];
const auto size = background["size"];
const auto& backgroundTex = background["texture"];
const auto& size = background["size"];

LoadPremade(backgroundTex, glm::ivec2(size[0], size[1]));

Expand Down Expand Up @@ -175,8 +175,6 @@ Level::Save(const std::string& pathToLevel)
// Serialize game objects
for (const auto& object : m_objects)
{
const auto id = object->GetID();

switch (object->GetType())
{
case ObjectType::PLAYER: {
Expand Down Expand Up @@ -392,7 +390,7 @@ Level::GetTilesFromRectangle(const std::array< glm::vec2, 4 >& rect) const
{
for (auto x = tileRect.at(0).first; x <= tileRect.at(1).first; ++x)
{
tiles.push_back({x, y});
tiles.emplace_back(x, y);
}
}

Expand Down Expand Up @@ -642,7 +640,7 @@ Level::GetObjectRef(Object::ID objectID)
}

std::vector< std::shared_ptr< GameObject > >
Level::GetObjects(const std::vector< Object::ID >& objectIDs)
Level::GetObjects(const std::vector< Object::ID >& objectIDs) const
{
std::vector< std::shared_ptr< GameObject > > objects = {};

Expand Down
2 changes: 1 addition & 1 deletion engine/game/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Level
GetObjectRef(Object::ID objectID);

std::vector<std::shared_ptr<GameObject>>
GetObjects(const std::vector<Object::ID>& objectIDs);
GetObjects(const std::vector<Object::ID>& objectIDs) const;

void
Update(bool isReverse);
Expand Down

0 comments on commit 30de80e

Please sign in to comment.