Show rectangle when selecting entities

This commit is contained in:
Jan Mrna 2025-10-12 16:04:40 +02:00
parent 4b3a4c53e8
commit d3af793092
7 changed files with 44 additions and 7 deletions

View File

@ -21,7 +21,7 @@ void GameLoop::Draw() {
TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)})); TilePos{static_cast<int32_t>(row), static_cast<int32_t>(col)}));
const auto &size = camera.WorldToWindowSize(map.GetTileSize()); const auto &size = camera.WorldToWindowSize(map.GetTileSize());
// LOG_DEBUG("Drawing rect (", row, ", ", col, ")"); // LOG_DEBUG("Drawing rect (", row, ", ", col, ")");
m_Window->DrawRect(position, size, tiles[row][col]->R, tiles[row][col]->G, m_Window->DrawFilledRect(position, size, tiles[row][col]->R, tiles[row][col]->G,
tiles[row][col]->B, tiles[row][col]->A); tiles[row][col]->B, tiles[row][col]->A);
} }
} }
@ -52,6 +52,15 @@ void GameLoop::Draw() {
m_Window->DrawCircle(entity_pos_window, entity->GetCollisionRadius()); m_Window->DrawCircle(entity_pos_window, entity->GetCollisionRadius());
} }
} }
// draw the selection box, if present
if (m_Game->IsSelectionBoxActive())
{
const auto& [corner_pos, size] = m_Game->GetSelectionBoxPosSize();
m_Window->DrawRect(corner_pos, size, 200, 20, 20, 100);
}
} }
// TODO rethink coupling and dependencies in the game loop class // TODO rethink coupling and dependencies in the game loop class

View File

@ -219,6 +219,12 @@ void PathFindingDemo::HandleActions(const std::vector<UserAction> &actions)
WorldPos end = m_Camera.WindowToWorld(m_SelectionBox.end); WorldPos end = m_Camera.WindowToWorld(m_SelectionBox.end);
SelectEntitiesInRectangle(start, end); SelectEntitiesInRectangle(start, end);
} }
else if (action.type == UserAction::Type::SELECTION_CHANGE)
{
m_SelectionBox.end = action.Argument.position;
auto diff = m_SelectionBox.end - m_SelectionBox.start;
m_SelectionBox.size = diff.ChangeTag<WindowSizeTag>();
}
}; };
} }
@ -241,10 +247,12 @@ void PathFindingDemo::SelectEntitiesInRectangle(WorldPos A, WorldPos B)
LOG_INFO("Selected ", m_SelectedEntities.size(), " entities"); LOG_INFO("Selected ", m_SelectedEntities.size(), " entities");
} }
std::pair<const WorldPos&, const WorldSize&> PathFindingDemo::GetSelectionBoxPosSize() std::pair<WindowPos, WindowSize> PathFindingDemo::GetSelectionBoxPosSize()
{ {
const auto& pos = m_SelectionBox.start; const auto& pos = m_SelectionBox.start;
WindowPos diff = m_SelectionBox.end - m_SelectionBox.start; WindowPos size_pos = m_SelectionBox.end - m_SelectionBox.start;
WindowSize size = size_pos.ChangeTag<WindowSizeTag>();
return std::pair(pos, size);
} }

View File

@ -17,6 +17,7 @@ using Collision = std::pair<std::weak_ptr<Entity>, std::weak_ptr<Entity>>;
struct SelectionBox struct SelectionBox
{ {
WindowPos start, end; WindowPos start, end;
WindowSize size;
bool active; bool active;
}; };
@ -43,7 +44,7 @@ public:
void SelectEntitiesInRectangle(WorldPos A, WorldPos B); void SelectEntitiesInRectangle(WorldPos A, WorldPos B);
bool IsSelectionBoxActive() const { return m_SelectionBox.active; } bool IsSelectionBoxActive() const { return m_SelectionBox.active; }
std::pair<const WorldPos&, const WorldSize&> GetSelectionBoxPosSize(); std::pair<WindowPos, WindowSize> GetSelectionBoxPosSize();
private: private:
const std::vector<Collision>& GetEntityCollisions(); const std::vector<Collision>& GetEntityCollisions();

View File

@ -32,6 +32,7 @@ void UserInput::GetActions_mouse(const SDL_Event& event)
if (button == MouseButton::LEFT) if (button == MouseButton::LEFT)
{ {
LOG_DEBUG("Selection start at ", mouse_event.x, ", ", mouse_event.y); LOG_DEBUG("Selection start at ", mouse_event.x, ", ", mouse_event.y);
m_SelectionActive = true;
m_Actions.emplace_back(UserAction::Type::SELECTION_START, m_Actions.emplace_back(UserAction::Type::SELECTION_START,
WindowPos{mouse_event.x, mouse_event.y}); WindowPos{mouse_event.x, mouse_event.y});
} }
@ -51,6 +52,7 @@ void UserInput::GetActions_mouse(const SDL_Event& event)
if (button == MouseButton::LEFT) if (button == MouseButton::LEFT)
{ {
LOG_DEBUG("Selection end at ", mouse_event.x, ", ", mouse_event.y); LOG_DEBUG("Selection end at ", mouse_event.x, ", ", mouse_event.y);
m_SelectionActive = false;
m_Actions.emplace_back(UserAction::Type::SELECTION_END, m_Actions.emplace_back(UserAction::Type::SELECTION_END,
WindowPos{mouse_event.x, mouse_event.y}); WindowPos{mouse_event.x, mouse_event.y});
} }
@ -67,6 +69,11 @@ void UserInput::GetActions_mouse(const SDL_Event& event)
m_Actions.emplace_back(UserAction::Type::CAMERA_PAN, m_Actions.emplace_back(UserAction::Type::CAMERA_PAN,
WindowPos{motion_event.xrel, motion_event.yrel}); WindowPos{motion_event.xrel, motion_event.yrel});
} }
if (m_SelectionActive)
{
m_Actions.emplace_back(UserAction::Type::SELECTION_CHANGE,
WindowPos{mouse_event.x, mouse_event.y});
}
} }
else if(event.type == SDL_EVENT_MOUSE_WHEEL) else if(event.type == SDL_EVENT_MOUSE_WHEEL)
{ {

View File

@ -22,6 +22,7 @@ public:
CAMERA_PAN, CAMERA_PAN,
CAMERA_ZOOM, CAMERA_ZOOM,
SELECTION_START, SELECTION_START,
SELECTION_CHANGE,
SELECTION_END SELECTION_END
}; };
@ -62,6 +63,7 @@ public:
private: private:
std::vector<UserAction> m_Actions; std::vector<UserAction> m_Actions;
bool m_SelectionActive = false;
void GetActions_keyboard(const SDL_Event&); void GetActions_keyboard(const SDL_Event&);
void GetActions_mouse(const SDL_Event&); void GetActions_mouse(const SDL_Event&);

View File

@ -84,13 +84,21 @@ void Window::DrawSprite(const WindowPos &position, Sprite &s, float scale) {
SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect); SDL_RenderTexture(m_Renderer.get(), s.GetTexture(), nullptr, &rect);
} }
void Window::DrawRect(const WindowPos &position, const WindowSize size, uint8_t R, void Window::DrawFilledRect(const WindowPos &position, const WindowSize size, uint8_t R,
uint8_t G, uint8_t B, uint8_t A) { uint8_t G, uint8_t B, uint8_t A) {
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()}; SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A); SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, A);
SDL_RenderFillRect(m_Renderer.get(), &rect); SDL_RenderFillRect(m_Renderer.get(), &rect);
} }
void Window::DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
uint8_t G, uint8_t B, uint8_t fill_alpha) {
SDL_FRect rect = {position.x(), position.y(), size.x(), size.y()};
SDL_SetRenderDrawColor(m_Renderer.get(), R, G, B, 255);
SDL_RenderRect(m_Renderer.get(), &rect);
//SDL_RenderFillRect(m_Renderer.get(), &rect);
}
void Window::ClearWindow() { void Window::ClearWindow() {
SDL_SetRenderDrawColor(m_Renderer.get(), 50, 50, 50, 255); SDL_SetRenderDrawColor(m_Renderer.get(), 50, 50, 50, 255);
SDL_RenderClear(m_Renderer.get()); SDL_RenderClear(m_Renderer.get());

View File

@ -23,8 +23,10 @@ public:
std::expected<void, std::string> Init(); std::expected<void, std::string> Init();
void DrawSprite(const WindowPos &position, Sprite &s, float scale = 1.0f); void DrawSprite(const WindowPos &position, Sprite &s, float scale = 1.0f);
void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R, void DrawFilledRect(const WindowPos &position, const WindowSize size, uint8_t R,
uint8_t G, uint8_t B, uint8_t A); uint8_t G, uint8_t B, uint8_t A);
void DrawRect(const WindowPos &position, const WindowSize size, uint8_t R,
uint8_t G, uint8_t B, uint8_t fill_alpha);
void ClearWindow(); void ClearWindow();
void Flush(); void Flush();
void DrawCircle(const WindowPos &position, float radius); void DrawCircle(const WindowPos &position, float radius);