Button Click System
Source: client/engine/systems/systems_functions/ButtonClickSystem.cpp
Purpose: Detect mouse hover and click on clickable entities, fire their callbacks, and update their visual color state.
Components Used
| Component | Access | Description |
|---|---|---|
HitBox | Read | Size and scaling behavior |
Clickable | Read/Write | Hover/click state, colors, callback |
Drawable | Write | Sprite color updated |
Transform | Read | Position, scale, origin |
Dependencies
| Dependency | Type | Description |
|---|---|---|
GameWorld | Reference | Access to InputManager and window |
InputManager | Via GameWorld | Mouse button state queries |
Behavior
- Focus Check: Returns immediately if the window doesn't have focus
- Mouse Button Query: Uses
InputManager::IsMouseButtonPressed()for backend-agnostic mouse state - Hit Detection: For each clickable entity:
- Converts mouse position to world coords via
window_.mapPixelToCoords - Builds the clickable rectangle considering transform scale and origin
- Sets
isHoveredwhen pointer is inside the rectangle
- Converts mouse position to world coords via
- Click Detection: Fires on button release after being pressed on the same entity
- Visual Feedback: Updates
drawable.colorbased on state priority:clickColor>hoverColor>idleColor
Function Signature
void ButtonClickSystem(Engine::registry ®, GameWorld &game_world,
Engine::sparse_array<Component::HitBox> &hit_boxes,
Engine::sparse_array<Component::Clickable> &clickables,
Engine::sparse_array<Component::Drawable> &drawables,
Engine::sparse_array<Component::Transform> &transforms);
Implementation Notes
The system uses the InputManager abstraction for mouse queries:
// Query mouse button through InputManager (backend-agnostic)
const bool is_left_pressed =
game_world.input_manager_->IsMouseButtonPressed(
Engine::Input::MouseButton::Left);
This ensures the button click system works regardless of the underlying input backend (SFML, SDL, etc.).
Notes
- Backend Agnostic: Uses
InputManagerfor mouse state, notsf::Mousedirectly - Click fires on release: Not on press, for better UX
- Colors applied directly: No shader required for visual feedback
Related Documentation
- Input Abstraction Layer - Mouse button abstraction
- Input System - Keyboard input handling