Files
ReJUI/include/JUI/Base/RectBase.hpp

108 lines
4.1 KiB
C++

/// Josh's User Interface Library
/// A C++20 Library for creating, styling, and rendering of a UI/UX widgets.
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
/// Special Thanks to William Tomasine II and Maxine Hayes.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file RectBase.hpp
/// @desc Base class that defines and implements rectangle (box) rendering routines.
/// @edit 2024-08-02
#pragma once
#include <J3ML/LinearAlgebra.hpp>
#include <Event.h>
#include <Color4.hpp>
#include "JGL/types/RenderTarget.h"
#include <JUI/DefaultStyle.hpp>
#include "JGL/JGL.h"
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.
};
enum class CornerRoundingMode {
None, Rounded, Chamfer,
};
/// Base implementation for rendering rectangles with decorations.
class RectBase {
public:
RectBase();
public:
Event<Vector2> MouseEnter;
Event<Vector2> MouseExit;
//Event<Vector2> MousePress;
//Event<Vector2, bool> MouseRelease;
public:
virtual void CornerRounding(float radius);
[[nodiscard]] float CornerRounding() const;
enum CornerRoundingMode CornerRoundingMode() const;
// TODO: Implement per-corner rounding in JGL::Outline/FillRect
//void CornerRounding(float tlRadius, float trRadius, float blRadius, float brRadius);
//void CornerRoundingTL(float radius);
//void CornerRoundingTR(float radius);
//void CornerRoundingBL(float radius);
//void CornerRoundingBR(float radius);
void SetClipsDescendants(bool clipping);
void BGColor(const Color4& col);
void BorderColor(const Color4& col);
void BorderWidth(float w);
[[nodiscard]] Color4 BGColor() const;
[[nodiscard]] Color4 GetBorderColor() const;
[[nodiscard]] float GetBorderWidth() const;
[[nodiscard]] enum BorderMode BorderMode() const;
void BorderMode(const enum BorderMode& mode);
bool GetClipsDescendants() const;
void SetBorderStyling(const Color4& color, float width);
void Draw(const Vector2& pos, const Vector2& size);
void Draw(const Color4& bgColor, const Color4& fgColor, const Vector2& pos, const Vector2& size);
/// Core routine for drawing outline rects.
static void DrawOutline(const Color4& color,
const Vector2& pos, const Vector2& size,
float rounding, float borderWidth,
enum BorderMode borderMode, enum CornerRoundingMode cornerRoundingMode);
/// Core routine for drawing background rects.
static void DrawBG(const Color4& color,
const Vector2& pos, const Vector2& size,
float rounding,
enum CornerRoundingMode rounding_mode);
/// Core routine for drawing rect boxes, with background and outline.
static void Draw(const Color4& bgColor, const Color4& fgColor,
const Vector2& pos, const Vector2& size,
float rounding, float borderWidth,
enum BorderMode borderMode, enum CornerRoundingMode rounding_mode);
protected:
enum BorderMode border_mode = BorderMode::Middle;
enum CornerRoundingMode corner_mode = CornerRoundingMode::Rounded;
bool mouse_press_debounce{};
bool mouse_inside_debounce{};
float border_width = Style::BorderLineWidth;
Color4 bg_color = Style::BackgroundColor;
Color4 border_color = Style::BorderColor;
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.
};
}