mosue management

This commit is contained in:
arthur.jamet
2021-06-15 17:59:45 +02:00
parent eb65c73fbc
commit 8a4c3fa586
5 changed files with 68 additions and 12 deletions
@@ -7,6 +7,12 @@
#include "System/MenuControllable/MenuControllableSystem.hpp"
#include "Component/Controllable/ControllableComponent.hpp"
#include "Entity/Entity.hpp"
#include "Drawables/Texture.hpp"
#include "Drawables/2D/Text.hpp"
#include "Controllers/Mouse.hpp"
namespace RAYControl = RAY::Controller;
namespace RAY2D = RAY::Drawables::Drawables2D;
namespace BBM
{
@@ -45,14 +51,43 @@ namespace BBM
this->_currentButton->getComponent<OnClickComponent>().onEvent(*this->_currentButton, this->_wal);
}
void MenuControllableSystem::onFixedUpdate(WAL::ViewEntity<ControllableComponent> &entity)
bool MenuControllableSystem::_mouseOnButton(WAL::ViewEntity<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent> &entity) const
{
auto &controllable = entity.get<ControllableComponent>();
auto &buttons = _wal.getScene()->view<OnClickComponent, OnHoverComponent, OnIdleComponent>();
auto &positionComponent = entity.get<PositionComponent>();
RAY::Vector2 rayMousePos = RAYControl::Mouse::getCursorPosition();
RAY::Texture *texture = dynamic_cast<RAY::Texture *>(entity.get<Drawable2DComponent>().drawable.get());
RAY2D::Text *text = dynamic_cast<RAY2D::Text *>(entity.get<Drawable2DComponent>().drawable.get());
Vector2f buttonPos(positionComponent.getX(), positionComponent.getY());
Vector2f mousePos(rayMousePos.x, rayMousePos.y);
Vector2f dimensions;
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 false;
if ((buttonPos.x <= mousePos.x && mousePos.x <= buttonPos.x + dimensions.x)
&& (buttonPos.y <= mousePos.y && mousePos.y <= buttonPos.y + dimensions.y))
return true;
return false;
}
void MenuControllableSystem::onSelfUpdate()
{
auto &controllableView = this->_wal.getScene()->view<ControllableComponent>();
auto &buttons = _wal.getScene()->view<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent>();
if (this->_currentButton && this->_currentButton->_scene.getID() != this->_wal.getScene()->getID()) {
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
this->_currentButton = nullptr;
return;
}
if (this->_currentButton == nullptr && buttons.size()) {
this->_currentButton = &(*buttons.front());
@@ -60,6 +95,20 @@ namespace BBM
}
if (!this->_currentButton)
return;
this->_updateCurrentButton(controllable.jump, controllable.move);
for (auto &[_, controllable]: controllableView)
if (controllable.move.x || controllable.move.y || controllable.jump) {
this->_updateCurrentButton(controllable.jump, controllable.move);
return;
}
for (auto &entity: buttons) {
if (_mouseOnButton(entity)) {
if (this->_currentButton)
this->_currentButton->getComponent<OnIdleComponent>().onEvent(*this->_currentButton, this->_wal);
this->_currentButton = &(*entity);
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);
}
}
}
}
@@ -7,11 +7,14 @@
#include "Component/Controllable/ControllableComponent.hpp"
#include "Models/Vector2.hpp"
#include "System/System.hpp"
#include "Component/Position/PositionComponent.hpp"
#include "Component/Renderer/Drawable2DComponent.hpp"
#include "Component/Button/ButtonComponent.hpp"
namespace BBM
{
//! @brief A system to handle Controllable entities in a menu.
class MenuControllableSystem : public WAL::System<ControllableComponent>
class MenuControllableSystem : public WAL::System<>
{
private:
//! @brief index of the current button selected
@@ -21,11 +24,13 @@ namespace BBM
//! @param selected lets know if te new selected button is 'pressed'
void _updateCurrentButton(bool selected, Vector2f move);
//! @return true if mouse on entity
bool _mouseOnButton(WAL::ViewEntity<OnClickComponent, OnHoverComponent, OnIdleComponent, PositionComponent, Drawable2DComponent> &entity) const;
public:
//! @brief time (in millisecond) since last check
std::chrono::time_point<std::chrono::steady_clock> now;
//! @inherit
void onFixedUpdate(WAL::ViewEntity<ControllableComponent> &entities) override;
void onSelfUpdate() override;
//! @brief A default constructor
explicit MenuControllableSystem(WAL::Wal &wal);