Selection rectangle
This commit is contained in:
parent
250f0963c8
commit
08b4b10113
@ -203,5 +203,35 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions)
|
||||
m_Camera.Zoom(action.Argument.float_number);
|
||||
LOG_INFO("Camera zoom: ", action.Argument.float_number);
|
||||
}
|
||||
else if (action.type == UserAction::Type::SELECTION_START)
|
||||
{
|
||||
m_SelectionBox.active = true;
|
||||
m_SelectionBox.start = action.Argument.position;
|
||||
}
|
||||
else if (action.type == UserAction::Type::SELECTION_END)
|
||||
{
|
||||
m_SelectionBox.end = action.Argument.position;
|
||||
m_SelectionBox.active = false;
|
||||
SelectEntitiesInRectangle(m_SelectionBox.start, m_SelectionBox.end);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void PathFindingDemo::SelectEntitiesInRectangle(WindowPos A, WindowPos B)
|
||||
{
|
||||
// TODO use colliders for this
|
||||
m_SelectedEntities.clear();
|
||||
auto [x_min, x_max] = std::minmax(A.x(), B.x());
|
||||
auto [y_min, y_max] = std::minmax(A.y(), B.y());
|
||||
for (const auto& entity : m_Entities)
|
||||
{
|
||||
const auto& pos = entity->GetPosition();
|
||||
bool x_in_range = x_min < pos.x() && pos.x() < x_max;
|
||||
bool y_in_range = y_min < pos.y() && pos.y() < y_max;
|
||||
if (x_in_range && y_in_range)
|
||||
{
|
||||
m_SelectedEntities.push_back(std::weak_ptr(entity));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,12 @@
|
||||
|
||||
using Collision = std::pair<std::weak_ptr<Entity>, std::weak_ptr<Entity>>;
|
||||
|
||||
struct SelectionBox
|
||||
{
|
||||
WindowPos start, end;
|
||||
bool active;
|
||||
};
|
||||
|
||||
class PathFindingDemo {
|
||||
public:
|
||||
PathFindingDemo(int width, int height);
|
||||
@ -35,6 +41,7 @@ public:
|
||||
void HandleActions(const std::vector<UserAction> &actions);
|
||||
WorldPos GetRandomPosition() const;
|
||||
|
||||
void SelectEntitiesInRectangle(WindowPos A, WindowPos B);
|
||||
|
||||
private:
|
||||
const std::vector<Collision>& GetEntityCollisions();
|
||||
@ -45,4 +52,5 @@ private:
|
||||
std::vector<std::shared_ptr<Entity>> m_Entities;
|
||||
std::unique_ptr<pathfinder::PathFinderBase> m_PathFinder;
|
||||
std::vector<std::weak_ptr<Entity>> m_SelectedEntities;
|
||||
SelectionBox m_SelectionBox;
|
||||
};
|
||||
|
@ -31,7 +31,13 @@ void UserInput::GetActions_mouse(const SDL_Event& event)
|
||||
{
|
||||
if (button == MouseButton::LEFT)
|
||||
{
|
||||
LOG_DEBUG("Mouse down: ", mouse_event.x, ", ", mouse_event.y);
|
||||
LOG_DEBUG("Selection start at ", mouse_event.x, ", ", mouse_event.y);
|
||||
m_Actions.emplace_back(UserAction::Type::SELECTION_START,
|
||||
WindowPos{mouse_event.x, mouse_event.y});
|
||||
}
|
||||
else if (button == MouseButton::RIGHT)
|
||||
{
|
||||
LOG_DEBUG("Set move target to: ", mouse_event.x, ", ", mouse_event.y);
|
||||
m_Actions.emplace_back(UserAction::Type::SET_MOVE_TARGET,
|
||||
WindowPos{mouse_event.x, mouse_event.y});
|
||||
}
|
||||
@ -42,6 +48,12 @@ void UserInput::GetActions_mouse(const SDL_Event& event)
|
||||
}
|
||||
else if (event.type == SDL_EVENT_MOUSE_BUTTON_UP)
|
||||
{
|
||||
if (button == MouseButton::LEFT)
|
||||
{
|
||||
LOG_DEBUG("Selection end at ", mouse_event.x, ", ", mouse_event.y);
|
||||
m_Actions.emplace_back(UserAction::Type::SELECTION_END,
|
||||
WindowPos{mouse_event.x, mouse_event.y});
|
||||
}
|
||||
if (button == MouseButton::MIDDLE)
|
||||
{
|
||||
mouse_pan = false;
|
||||
|
@ -12,7 +12,18 @@ enum class MouseButton { LEFT = 1, MIDDLE, RIGHT };
|
||||
|
||||
class UserAction {
|
||||
public:
|
||||
enum class Type { NONE, EXIT, SET_MOVE_TARGET, SELECT_PATHFINDER, CAMERA_PAN, CAMERA_ZOOM };
|
||||
|
||||
enum class Type
|
||||
{
|
||||
NONE,
|
||||
EXIT,
|
||||
SET_MOVE_TARGET,
|
||||
SELECT_PATHFINDER,
|
||||
CAMERA_PAN,
|
||||
CAMERA_ZOOM,
|
||||
SELECTION_START,
|
||||
SELECTION_END
|
||||
};
|
||||
|
||||
UserAction() : type(Type::NONE), Argument{.number = 0} {}
|
||||
UserAction(Type t) : type(t), Argument{.number = 0} {}
|
||||
|
Loading…
x
Reference in New Issue
Block a user