Implement static draw routines to RectBase to unify JUI's rectangle motifs.

This commit is contained in:
2025-06-24 01:57:38 -05:00
parent d5d6703ee9
commit 9d901c6300
2 changed files with 62 additions and 18 deletions

View File

@@ -18,6 +18,8 @@
#include "JGL/types/RenderTarget.h"
#include <JUI/DefaultStyle.hpp>
#include "JGL/JGL.h"
namespace JUI
{
enum class BorderMode
@@ -28,7 +30,7 @@ namespace JUI
};
enum class CornerRoundingMode {
None, Circular, Chamfer,
None, Rounded, Chamfer,
};
/// Base implementation for rendering rectangles with decorations.
@@ -72,9 +74,29 @@ namespace JUI
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;

View File

@@ -34,32 +34,54 @@ namespace JUI {
void RectBase::Draw(const Color4& bgColor, const Color4& borderColor, const Vector2 &abs_pos, const Vector2 &abs_size) {
// Background rect
if (corner_rounding_radius > 0)
J2D::FillRoundedRect(bgColor, abs_pos, abs_size, corner_rounding_radius);
else
J2D::FillRect(bgColor, abs_pos, abs_size);
RectBase::Draw(bgColor, borderColor, abs_pos, abs_size, corner_rounding_radius, border_width, border_mode, corner_mode);
}
void RectBase::DrawOutline(const Color4 &color, const Vector2 &pos, const Vector2 &size, float rounding,
float borderWidth, enum JUI::BorderMode borderMode, enum JUI::CornerRoundingMode cornerRoundingMode) {
// Outline rect - compute the size change to fit the border accurately.
Vector2 border_offset = {0, 0};
if (border_mode == BorderMode::Inset)
border_offset = {-border_width/2.f, -border_width/2.f};
if (border_mode == BorderMode::Middle)
/// Border is too small, don't draw it.
if (borderWidth <= 0) return;
if (borderMode == BorderMode::Inset)
border_offset = {-borderWidth/2.f, -borderWidth/2.f};
if (borderMode == BorderMode::Middle)
border_offset = {0, 0};
if (border_mode == BorderMode::Outline)
border_offset = {border_width/2.f, border_width/2.f};
if (borderMode == BorderMode::Outline)
border_offset = {borderWidth/2.f, borderWidth/2.f};
// Draw the outline.
if (border_width > 0) {
if (corner_rounding_radius > 0)
J2D::OutlineRoundedRect(borderColor, abs_pos - border_offset, abs_size + (border_offset*2), corner_rounding_radius, border_width);
else
J2D::OutlineRect(borderColor, abs_pos - border_offset, abs_size + (border_offset*2), border_width);
}
if (cornerRoundingMode == CornerRoundingMode::None || rounding <= 0)
J2D::OutlineRect(color, pos - border_offset, size + (border_offset * 2), borderWidth);
else if (cornerRoundingMode == CornerRoundingMode::Rounded)
J2D::OutlineRoundedRect(color, pos - border_offset, size + (border_offset*2), rounding, borderWidth);
else if (cornerRoundingMode == CornerRoundingMode::Chamfer)
J2D::OutlineChamferRect(color, pos - border_offset, size + (border_offset*2), rounding, borderWidth);
}
void RectBase::DrawBG(const Color4 &color, const Vector2 &pos, const Vector2 &size, float rounding,
enum JUI::CornerRoundingMode rounding_mode) {
if (rounding_mode == CornerRoundingMode::None || rounding <= 0)
J2D::FillRect(color, pos, size);
else if (rounding_mode == CornerRoundingMode::Rounded)
J2D::FillRoundedRect(color, pos, size, rounding);
else if (rounding_mode == CornerRoundingMode::Chamfer)
J2D::FillChamferRect(color, pos, size, rounding);
}
void RectBase::Draw(const Color4 &bgColor, const Color4 &fgColor, const Vector2 &pos, const Vector2 &size,
float rounding, float borderWidth, enum JUI::BorderMode borderMode,
enum JUI::CornerRoundingMode rounding_mode) {
DrawBG(bgColor, pos, size, rounding, rounding_mode);
DrawOutline(fgColor, pos, size, rounding, borderWidth, borderMode, rounding_mode);
}
void RectBase::BorderMode(const enum BorderMode &mode) {
border_mode = mode;
}