mirror of
https://github.com/zoriya/Bomberman.git
synced 2026-05-27 16:22:09 +00:00
add mouse system for button
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
//
|
||||
// Created by Louis Auzuret 06/03/21
|
||||
//
|
||||
|
||||
#include <algorithm>
|
||||
#include "Component/Button/ButtonComponent.hpp"
|
||||
#include "System/MenuControllable/MenuControllableMouseSystem.hpp"
|
||||
#include "Component/Controllable/ControllableComponent.hpp"
|
||||
#include "Entity/Entity.hpp"
|
||||
#include "Controllers/Mouse.hpp"
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
#include "Drawables/Texture.hpp"
|
||||
#include "Drawables/2D/Text.hpp"
|
||||
#include "Component/Renderer/Drawable2DComponent.hpp"
|
||||
|
||||
namespace RAYControl = RAY::Controller;
|
||||
namespace RAY2D = RAY::Drawables::Drawables2D;
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
MenuControllableMouseSystem::MenuControllableMouseSystem(WAL::Wal &wal)
|
||||
: System(wal), _currentButton()
|
||||
{}
|
||||
|
||||
void MenuControllableMouseSystem::onFixedUpdate(WAL::ViewEntity<ControllableComponent, PositionComponent, Drawable2DComponent> &entity)
|
||||
{
|
||||
auto &positionComponent = entity.get<PositionComponent>();
|
||||
RAY::Vector2 rayMousePos = RAYControl::Mouse::getCursorPosition();
|
||||
Vector2f buttonPos(positionComponent.getX(), positionComponent.getY());
|
||||
Vector2f mousePos(rayMousePos.x, rayMousePos.y);
|
||||
Vector2f dimensions;
|
||||
auto &controllable = entity.get<ControllableComponent>();
|
||||
|
||||
if (this->_currentButton && this->_currentButton->_scene.getID() != this->_wal.getScene()->getID()) {
|
||||
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
|
||||
this->_currentButton = nullptr;
|
||||
}
|
||||
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.get<Drawable2DComponent>().drawable.get());
|
||||
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.get<Drawable2DComponent>().drawable.get());
|
||||
WAL::Entity *newButton = nullptr;
|
||||
|
||||
if (texture) {
|
||||
dimensions.x = texture->getDimensions().x;
|
||||
dimensions.y = texture->getDimensions().y;
|
||||
} else if (text) {
|
||||
dimensions.y = text->getFontSize();
|
||||
dimensions.x = text->getString().size() * (text->getLetterSpacing() + text->getFontSize());
|
||||
} else
|
||||
return;
|
||||
if ((buttonPos.x <= mousePos.x && mousePos.x <= buttonPos.x + dimensions.x)
|
||||
&& (buttonPos.y <= mousePos.y && mousePos.y <= buttonPos.y + dimensions.y))
|
||||
newButton = &(*entity);
|
||||
if (newButton) {
|
||||
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
|
||||
this->_currentButton = newButton;
|
||||
this->_currentButton->getComponent<OnHoverComponent>().onEvent(*this->_currentButton, this->_wal);
|
||||
if (RAYControl::Mouse::isPressed(RAYControl::Mouse::Button::MOUSE_BUTTON_LEFT))
|
||||
this->_currentButton->getComponent<OnClickComponent>().onEvent(*this->_currentButton, this->_wal);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
//
|
||||
// Created by Louis Auzuret on 06/03/21
|
||||
//
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Component/Controllable/ControllableComponent.hpp"
|
||||
#include "Models/Vector2.hpp"
|
||||
#include "System/System.hpp"
|
||||
#include "Component/Position/PositionComponent.hpp"
|
||||
#include "Component/Renderer/Drawable2DComponent.hpp"
|
||||
|
||||
namespace BBM
|
||||
{
|
||||
//! @brief A system to handle Controllable entities in a menu.
|
||||
class MenuControllableMouseSystem : public WAL::System<ControllableComponent, PositionComponent, Drawable2DComponent>
|
||||
{
|
||||
private:
|
||||
//! @brief index of the current button selected
|
||||
WAL::Entity *_currentButton;
|
||||
public:
|
||||
//! @brief time (in millisecond) since last check
|
||||
std::chrono::time_point<std::chrono::steady_clock> now;
|
||||
//! @inherit
|
||||
void onFixedUpdate(WAL::ViewEntity<ControllableComponent, PositionComponent, Drawable2DComponent> &entities) override;
|
||||
|
||||
//! @brief A default constructor
|
||||
explicit MenuControllableMouseSystem(WAL::Wal &wal);
|
||||
//! @brief A MenuControllable system is not copy constructable
|
||||
MenuControllableMouseSystem(const MenuControllableMouseSystem &) = delete;
|
||||
//! @brief A default destructor
|
||||
~MenuControllableMouseSystem() override = default;
|
||||
//! @brief A MenuControllable system is not assignable.
|
||||
MenuControllableMouseSystem &operator=(const MenuControllableMouseSystem &) = delete;
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user