Renamed several functions, testing tween support.

This commit is contained in:
2025-02-08 04:07:00 -05:00
parent 9a70839d2e
commit 1053a32317
5 changed files with 75 additions and 33 deletions

View File

@@ -26,22 +26,13 @@ using namespace JGL;
namespace JUI
{
class ITween {};
template <typename T>
class Tween : public ITween
class Tween
{
public:
std::function<T(void)> getter;
std::function<void(T)> setter;
std::function<void()> getter;
float lifetime = 5;
float progress = 0;
T goal;
void Update(float dt)
{
}
};
/// Support Lerp/Tween of the following types:
@@ -108,6 +99,41 @@ namespace JUI
}
void TweenPositionTo(const UDim2& goal)
{
std::function<void(float)> updateTillGoalReached = [this, goal] (float elapsed)
{
auto pos = this->Position();
// TODO: Implement UDim equality operators (with epsilon)
// TODO: Implement Easing Functions
// TODO: Implement UDim::Lerp
// TODO: Implement UDim2::Lerp
if (pos.X.Pixels == goal.X.Pixels && pos.Y.Pixels == goal.Y.Pixels && pos.X.Scale == goal.X.Scale && pos.Y.Scale == goal.Y.Scale)
{
// Reached Goal, Complete this tween.
return;
}
float lerp_factor = 1.f/10.f;
pos.X.Pixels = Math::Lerp(pos.X.Pixels, goal.X.Pixels, lerp_factor);
pos.Y.Pixels = Math::Lerp(pos.Y.Pixels, goal.Y.Pixels, lerp_factor);
};
}
void TweenPositionFromTo();
void TweenSizeTo();
void TweenSizeFromTo();
void TweenTest()
{
}
/// Adds a given widget to this widget's list of children.
/// @return The widget in question.
Widget* Add(Widget* newChild);
@@ -178,8 +204,11 @@ namespace JUI
bool IsAncestorOf(Widget *descendant);
/// Determines the origin point of a Widget, relative to it's absolute size.
/// TODO: Better explain what this allows for.
[[nodiscard]] Vector2 AnchorPoint() const;
void AnchorPoint(const Vector2 &point);
/// Returns the padding factor on the left of this widget.
/// Padding refers to spacing on the inside of elements, while margin is spacing outside the element.
/// @see PaddingLeft(), class UDim.
@@ -284,13 +313,15 @@ namespace JUI
/// Returns whether or not the mouse is inside this widget's approximate bounding-box.
bool IsMouseInside() const;
void AnchorPoint(const Vector2 &point);
/// Returns the complete bounding box around this instance that will be rendered onto.
/// This differs from AbsolutePosition and AbsoluteSize in that they are computed for positioning elements relative to each other.
/// Only this method represents the full bounding box of the widget.
[[nodiscard]] virtual AABB2D GetActualRenderBounds() const;
AABB2D AbsoluteBounds() const;
void SetViewportSize(const Vector2& vps);
public:
@@ -339,6 +370,7 @@ namespace JUI
UDim2 size = {50_px, 50_px};
Widget* parent = nullptr;
std::vector<Widget*> children;
std::vector<Tween> tweens;
float rotation = 0;
std::string name;
bool selected = false;

View File

@@ -87,18 +87,20 @@ namespace JUI
void SetResizable(bool value);
/// Returns a pointer to the Text Widget that is used to render the title bar's text.
Text* GetTitleInstance();
Text* TitleInstance();
/// Returns a pointer to the Rect Widget that is used to layout the title bar contents.
Rect* GetTopbarInstance();
Rect* TopbarInstance();
/// Returns a pointer to the Rect Widget that is used to layout the contents of the window.
Rect* GetViewportInstance();
Vector2 AbsoluteViewportPosition();
Vector2 AbsoluteViewportSize();
Rect* ViewportInstance();
/// Returns a pointer to the Exit Button Widget.
TextButton* GetExitButtonInstance();
TextButton* ExitButtonInstance();
Vector2 AbsoluteViewportPosition() const;
Vector2 AbsoluteViewportSize() const;
AABB2D AbsoluteViewportBounds() const;
/// Sets the font used by the title-bar text on this Window.
void SetTitleFont(const Font& f);

View File

@@ -133,7 +133,7 @@ JUI::Scene* CreateScene() {
other_window->SetTitle("Another Window");
scroller = new JUI::ScrollingRect(other_window->GetViewportInstance());
scroller = new JUI::ScrollingRect(other_window->ViewportInstance());
scroller->Size({100_percent, 100_percent});
scroller->BGColor(Colors::Reds::LightCoral);
@@ -164,11 +164,11 @@ JUI::Scene* CreateScene() {
nineslice->CenterQuad({{96, 96}, {384-(96*2), 378-(96*2)}});
auto darkie = new JUI::Image(nineslice_demo_window->GetViewportInstance(), sample_texture);
auto darkie = new JUI::Image(nineslice_demo_window->ViewportInstance(), sample_texture);
darkie->FitImageToParent(true);
darkie->Color({255,255,255,128});
auto list = new VerticalListLayout(nineslice_demo_window->GetViewportInstance());
auto list = new VerticalListLayout(nineslice_demo_window->ViewportInstance());
list->Padding(10_px);

View File

@@ -441,4 +441,8 @@ namespace JUI {
}
}
AABB2D Widget::AbsoluteBounds() const {
return {GetAbsolutePosition(), GetAbsoluteSize()};
}
}

View File

@@ -95,11 +95,11 @@ namespace JUI
bool Window::IsResizable() const { return resizable; }
Text *Window::GetTitleInstance() { return TitleLabel; }
Text *Window::TitleInstance() { return TitleLabel; }
Rect *Window::GetTopbarInstance() { return Topbar; }
Rect *Window::TopbarInstance() { return Topbar; }
Rect *Window::GetViewportInstance() { return Viewport; }
Rect *Window::ViewportInstance() { return Viewport; }
void Window::SetDrag(bool d) {
Draggable::SetDrag(d);
@@ -116,10 +116,10 @@ namespace JUI
Clickable::OnClick(mouse_pos, btn);
clicked = true;
if (draggable && btn == MouseButton::Left && GetTitleInstance()->IsMouseInside())
if (draggable && btn == MouseButton::Left && TitleInstance()->IsMouseInside())
this->SetDrag(true);
if (resizable && btn == MouseButton::Right && GetTitleInstance()->IsMouseInside()) {
if (resizable && btn == MouseButton::Right && TitleInstance()->IsMouseInside()) {
this->StartResizing(last_known_mouse_pos);
this->SetResize(true);
size_when_restart_began = Size();
@@ -197,7 +197,7 @@ namespace JUI
}
void Window::SetTitle(const std::string &title) {
GetTitleInstance()->SetContent(title);
TitleInstance()->SetContent(title);
}
void Window::MinSize(const Vector2 &constraint) {
@@ -220,7 +220,7 @@ namespace JUI
resizable = value;
}
TextButton *Window::GetExitButtonInstance() {
TextButton *Window::ExitButtonInstance() {
return exit_btn;
}
@@ -234,11 +234,15 @@ namespace JUI
exit_btn->Position({100_percent, 0_px});
}
Vector2 Window::AbsoluteViewportPosition() {
Vector2 Window::AbsoluteViewportPosition() const {
return Viewport->GetAbsolutePosition();
}
Vector2 Window::AbsoluteViewportSize() {
Vector2 Window::AbsoluteViewportSize() const {
return Viewport->GetAbsoluteSize();
}
AABB2D Window::AbsoluteViewportBounds() const {
return {AbsoluteViewportPosition(), AbsoluteViewportSize()};
}
}