Implement rough-draft of Button class.

This commit is contained in:
2024-07-30 13:35:56 -04:00
parent 409ccd07e3
commit 0283d43c44
7 changed files with 156 additions and 63 deletions

View File

@@ -1,10 +1,65 @@
#pragma once
#include <J3ML/LinearAlgebra.h>
#include <Event.h>
#include <JGL/Color4.h>
using JGL::Color4;
namespace JUI
{
enum class BorderMode
{
Outline, /// As BorderWidth increases, the border grows outward. The dimensions of the widgets contents do not change.
Middle, /// As BorderWidth increases, the border grows evenly inward and outward. The dimensions of the widgets' contents are reduced at a 1:1 ratio.
Inset /// As BorderWidth increases, the border grows inward only. The dimensions of the widget's contents are reduced at a 1:2 ratio.
};
/// Base implementation for rendering rectangles with decorations.
class RectBase {
public:
RectBase();
public:
Event<Vector2> MouseEnter;
Event<Vector2> MouseExit;
Event<Vector2> MousePress;
Event<Vector2> MouseRelease;
public:
void SetCornerRounding(float radius);
void SetCornerRounding(float tlRadius, float trRadius, float blRadius, float brRadius);
void SetCornerRoundingTL(float radius);
void SetCornerRoundingTR(float radius);
void SetCornerRoundingBL(float radius);
void SetCornerRoundingBR(float radius);
void SetClipsDescendants(bool clipping);
void BGColor(const Color4& col);
void SetBorderColor(const Color4& col);
void SetBorderWidth(float w);
Color4 BGColor() const;
Color4 GetBorderColor() const;
float GetBorderWidth() const;
BorderMode GetBorderMode() const;
void SetBorderMode(const BorderMode& mode);
bool GetClipsDescendants() const;
void SetBorderStyling(const Color4& color, float width);
protected:
BorderMode border_mode = BorderMode::Middle;
protected:
bool mouse_inside_debounce;
float border_width = 1.f;
Color4 bg_color = {128,128,128, 255};
Color4 border_color = {192, 192, 192, 0};
bool clips_descendants = false; // Controls if child objects can render outside of their parent's rectangle bounds.
float corner_rounding_radius = 0.f; // Curves the rectangle corners by N degrees.
};
}

View File

@@ -77,10 +77,31 @@ namespace JUI
/// @see class UDim2, SetSize(),
[[nodiscard]] UDim2 GetPosition() const;
/// Retrieves this widgets z-index. This value determines the order in which a widget
/// renders to the screen relative to other Widgets.
/// @note This applies in ascending priority order,
/// meaning lower values are rendered first, placing higher values on top of lower values.
/// @note The range of valid values is -MAX_INT to MAX_INT.
/// @note This does not manipulate the OpenGL Z buffer, rather, when rendering,
/// we sort the direct children of a widget widget and render.
int GetZIndex() const;
/// Sets this widgets z-index. This value determines the order in which a widget
/// renders to the screen relative to other Widgets.
/// @note This applies in ascending priority order,
/// meaning lower values are rendered first, placing higher values on top of lower values.
/// @note The range of valid values is -MAX_INT to MAX_INT.
/// @note This does not manipulate the OpenGL Z buffer, rather, when rendering,
/// we sort the direct children of a widget widget and render.
void SetZIndex(int zindex);
/// Returns the menu-coordinates that are used to size this widget in relation to its parent.
/// @see SetSize(), class UDim2
[[nodiscard]] UDim2 GetSize() const;
/// Sets the widgets pixel and scalar size using a combined data type.
/// This position is centered around the object's anchor point.
/// @see GetPosition(), SetAnchorPoint(), GetAnchorPoint(), class UDim2.
void SetPosition(const UDim2 &pos);
void SetSize(const UDim2 &s);
@@ -91,11 +112,20 @@ namespace JUI
bool IsAncestorOf(Widget *descendant);
/// Determines the origin point of a Widget, relative to it's absolute size.
[[nodiscard]] Vector2 GetAnchorPoint() const;
/// Returns the padding factor on the left of this widget.
/// @see SetPaddingLeft(), class UDim.
[[nodiscard]] UDim GetPaddingLeft() const;
/// Returns the padding factor on the top of this widget.
/// @see SetPaddingTop(), class UDim.
[[nodiscard]] UDim GetPaddingTop() const;
/// Returns the padding factor on the right of this widget.
/// @see SetPaddingRight(), class UDim.
[[nodiscard]] UDim GetPaddingRight() const;
/// Returns the padding factor on the bottom of this widget.
/// @see SetPaddingBottom(), class UDim.
[[nodiscard]] UDim GetPaddingBottom() const;
void SetPaddingLeft(const UDim &pad_left);

View File

@@ -5,13 +5,46 @@
namespace JUI
{
enum class ButtonState
{
Base,
Hovered,
Pressed,
Disabled
};
class Button : public Widget, public RectBase, public Clickable
{
public:
Color4 GetHoveredBGColor()
{
Button();
explicit Button(Widget* parent);
~Button() override {};
}
// TODO: This is duplicated between here and Rect widget
// Ideally, it would be in RectBase, or better yet
// the Clickable mixin class for all input-related shit.
bool IsMouseInside() const;
public:
// TODO: These suffice for now as a proof-of-concept
// But I want more sophisticated style-animation support
// for the various states of the button
Color4 GetHoveredBGColor() const;
Color4 GetBaseBGColor() const;
Color4 GetPressedBGColor() const;
Color4 GetDisabledBGColor() const;
Color4 GetHoveredBorderColor() const;
Color4 GetBaseBorderColor() const;
Color4 GetPressedBorderColor() const;
Color4 GetDisabledBorderColor() const;
bool IsHovered() const;
bool IsPressed() const;
bool IsEnabled() const;
void SetEnabled(bool enabled);
protected:
private:
};

View File

@@ -3,55 +3,28 @@
#pragma once
#include "JUI/Base/Widget.hpp"
#include <JUI/Base/RectBase.hpp>
namespace JUI {
/// The Rect Widget Class
/// Renders a rectangle or "box" and also serves as a vesatile container for laying out other widgets.
class Rect : public Widget {
class Rect : public Widget, public RectBase {
public: /// Constructors
/// The default constructor.
Rect();
Rect(Widget* parent);
explicit Rect(Widget* parent);
~Rect() override {}
public:
Event<Vector2> MouseEnter;
Event<Vector2> MouseExit;
Event<Vector2> MousePress;
Event<Vector2> MouseRelease;
bool IsMouseInside() const;
public:
void Draw() override;
void Update(float delta) override;
bool IsMouseInside() const;
Color4 GetBGColor() const;
Color4 GetBorderColor() const;
float GetBorderWidth() const;
bool GetClipsDescendants() const;
void SetClipsDescendants(bool clipping);
void SetBGColor(const Color4& col);
void SetBorderColor(const Color4& col);
void SetBorderWidth(float w);
void SetCornerRounding(float radius);
void SetCornerRounding(float tlRadius, float trRadius, float blRadius, float brRadius);
void SetCornerRoundingTL(float radius);
void SetCornerRoundingTR(float radius);
void SetCornerRoundingBL(float radius);
void SetCornerRoundingBR(float radius);
void SetBorderStyling(const Color4& color, float width);
protected:
bool mouse_inside_debounce;
float border_width = 1.f;
Color4 bg_color = {128,128,128, 255};
Color4 border_color = {192, 192, 192, 0};
bool clips_descendants = false; // Controls if child objects can render outside of their parent's rectangle bounds.
float corner_rounding_radius = 0.f; // Curves the rectangle corners by N degrees.
};
}

View File

@@ -27,8 +27,8 @@ JUI::Scene* CreateScene() {
Rect* element = new Rect(root);
element->SetName("JimBob");
//element->SetBGColor({0,255,0});
element->SetBGColor({0,64,0});
//element->BGColor({0,255,0});
element->BGColor({0, 64, 0});
auto size = UDim2(64, 64, 0, 0);
element->SetSize({0, 0, 0.5f, 0.5f});
element->SetClipsDescendants(true);
@@ -36,11 +36,11 @@ JUI::Scene* CreateScene() {
element->SetBorderWidth(2.f);
element->MouseEnter += [element] (auto coords) {
element->SetBGColor({0,128,0});
element->BGColor({0, 128, 0});
};
element->MouseExit += [element] (auto coords) {
element->SetBGColor({0, 64, 0});
element->BGColor({0, 64, 0});
};
Text* text = new Text(element);

View File

@@ -2,5 +2,26 @@
namespace JUI {
bool RectBase::GetClipsDescendants() const { return clips_descendants;}
void RectBase::SetClipsDescendants(bool clipping) { clips_descendants = clipping;}
void RectBase::BGColor(const Color4 &col) { bg_color = col;}
void RectBase::SetBorderColor(const Color4 &col) { border_color = col;}
void RectBase::SetBorderWidth(float w) {border_width = w;}
float RectBase::GetBorderWidth() const { return border_width;}
Color4 RectBase::GetBorderColor() const { return border_color;}
Color4 RectBase::BGColor() const { return bg_color; }
void RectBase::SetBorderStyling(const Color4 &color, float width) {
SetBorderColor(color);
SetBorderWidth(width);
}
}

View File

@@ -78,27 +78,6 @@ namespace JUI {
Widget::Update(delta);
}
bool Rect::GetClipsDescendants() const { return clips_descendants;}
void Rect::SetClipsDescendants(bool clipping) { clips_descendants = clipping;}
void Rect::SetBGColor(const Color4 &col) { bg_color = col;}
void Rect::SetBorderColor(const Color4 &col) { border_color = col;}
void Rect::SetBorderWidth(float w) {border_width = w;}
float Rect::GetBorderWidth() const { return border_width;}
Color4 Rect::GetBorderColor() const { return border_color;}
Color4 Rect::GetBGColor() const { return bg_color; }
void Rect::SetBorderStyling(const Color4 &color, float width) {
SetBorderColor(color);
SetBorderWidth(width);
}
bool Rect::IsMouseInside() const {
float x = last_known_mouse_pos.x;
float y = last_known_mouse_pos.y;
@@ -110,6 +89,8 @@ namespace JUI {
return false;
}
}