Loading...
Searching...
No Matches
Positionable.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4//
5// Created by Dave Smith on 11/13/22.
6//
7
8#ifndef MALENA_PORTFOLIO_POSITIONABLE_H
9#define MALENA_PORTFOLIO_POSITIONABLE_H
10
11#pragma once
12
14#include <SFML/Graphics.hpp>
15#include <float.h>
16#include <queue>
19
20namespace ml
21{
68 {
69 private:
70 float velocityX{}, velocityY{}, framerate = 60.f;
71 sf::Vector2f initialPosition = {FLT_MIN, FLT_MAX};
72 sf::Clock clock;
73 std::queue<sf::Vector2f> points;
74 bool scrolling = false;
75
76 bool error(sf::Vector2f a, sf::Vector2f b, float err = 0.1f);
77 void generatePoints(sf::Vector2f position, float duration, Tween tween);
78 void generateExponential(sf::Vector2f position, float duration);
79 void calcVelocity(sf::Vector2f velocity, float seconds);
80
81 public:
91 void setFramerate(float framerate);
92
103 void moveTo(sf::Vector2f position, float seconds = 1.f);
104
114 void moveTo(sf::FloatRect position, float seconds = 1.f);
115
124 void moveDistance(sf::Vector2f distance, float seconds = 1.f);
125
133
141 virtual void setPosition(const sf::Vector2f& position) {}
142
148 virtual sf::Vector2f getPosition() const { return {}; }
149
159 virtual sf::FloatRect getGlobalBounds() const { return {}; }
160
161 // ── Relative layout helpers ──────────────────────────────────────────
162
169 template<class T>
170 void setRightOf(T& obj, float spacing = 0);
171
178 template<class T>
179 void setLeftOf(T& obj, float spacing = 0);
180
187 template<class T>
188 void setBelow(T& obj, float spacing = 0);
189
196 template<class T>
197 void setAbove(T& obj, float spacing = 0);
198
204 template<class T>
205 void center(T& obj);
206
212 template<class T>
213 void centerHorizonally(T& obj);
214
220 template<class T>
221 void centerVertically(T& obj);
222
232 };
233
234} // namespace ml
235
236#include "../../../../src/Traits/Spatial/Positionable.tpp"
237#endif // MALENA_PORTFOLIO_POSITIONABLE_H
Trait that provides position, bounds, and animated movement.
virtual void setPosition(const sf::Vector2f &position)
Set the world-space position immediately (no animation).
bool isScrolling()
Return true while a moveTo or moveDistance animation is in progress.
void setBelow(T &obj, float spacing=0)
Position this object immediately below obj.
void center(T &obj)
Center this object within obj (both axes).
virtual sf::Vector2f getPosition() const
Return the current world-space position.
void setAbove(T &obj, float spacing=0)
Position this object immediately above obj.
void centerVertically(T &obj)
Center this object vertically within obj.
void moveDistance(sf::Vector2f distance, float seconds=1.f)
Animate by a relative offset from the current position.
void centerHorizonally(T &obj)
Center this object horizontally within obj.
void setRightOf(T &obj, float spacing=0)
Position this object immediately to the right of obj.
void moveTo(sf::Vector2f position, float seconds=1.f)
Animate to an absolute world-space position.
virtual sf::FloatRect getGlobalBounds() const
Return the axis-aligned bounding box in world space.
void setFramerate(float framerate)
Set the assumed framerate for movement interpolation.
void setLeftOf(T &obj, float spacing=0)
Position this object immediately to the left of obj.
void moveTo(sf::FloatRect position, float seconds=1.f)
Animate to a position defined by a FloatRect origin.
void centerText(sf::Text &obj)
Center an sf::Text object within this object's bounds.
Empty marker base class for all Malena traits.
Definition Trait.h:35
Tween
Easing curve selection for animated movement.
Definition Tween.h:45
#define MALENA_API
Definition Component.h:22
Rect< float > FloatRect
Vector2< float > Vector2f