Crafter.Graphics/interfaces/Crafter.Graphics-UIComponents.cppm

115 lines
4.6 KiB
Text
Raw Normal View History

2026-05-02 21:08:20 +02:00
/*
Crafter®.Graphics
Copyright (C) 2026 Catcrafts®
catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 3.0 as published by the Free Software Foundation;
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
module;
#include "vulkan/vulkan.h"
export module Crafter.Graphics:UIComponents;
import std;
import :UI;
import :Font;
import :FontAtlas;
// Tier 3: stateless presentation functions. These append items to the user's
// QuadItem / GlyphItem buffers — they do NOT dispatch. The user dispatches
// via UIRenderer::DispatchQuads / DispatchText after their onBuild fills
// everything, so a frame stays one quads dispatch + one text dispatch
// regardless of how many components were drawn.
//
// State for components that need it (hovered, pressed, dragging, t01) is the
// USER's responsibility — these functions are pure presentation.
//
// EXTENSION MODEL: each function below is short on purpose. If you want a
// hexagon button, an icon-with-label button, a tristate checkbox — copy the
// function body into your code and modify it. There is no override hook.
export namespace Crafter {
// Aggregate for the two item buffers + the optional text-shaping deps.
// Build one per frame in onBuild and pass it to component calls.
struct UIBuffer {
QuadItem* quads = nullptr;
std::uint32_t* quadCount = nullptr;
std::uint32_t quadCap = 0;
GlyphItem* glyphs = nullptr;
std::uint32_t* glyphCount = nullptr;
std::uint32_t glyphCap = 0;
FontAtlas* atlas = nullptr; // for text-emitting components
UIRenderer* renderer = nullptr; // for ShapeText
};
// ─── per-component color blocks ─────────────────────────────────────
// Inline POD aggregates. Users compose their own application-level theme
// by holding a few of these together; the library has no Theme type.
struct ButtonColors {
std::array<float, 4> bg;
std::array<float, 4> bgHover;
std::array<float, 4> bgPressed;
std::array<float, 4> text;
std::array<float, 4> border = {0, 0, 0, 0};
float cornerRadius = 0;
float borderThickness = 0;
};
struct CheckboxColors {
std::array<float, 4> bg;
std::array<float, 4> bgHover;
std::array<float, 4> check;
std::array<float, 4> border = {0, 0, 0, 0};
float cornerRadius = 4;
float borderThickness = 1;
float checkInset = 4; // px on each side
};
struct SliderColors {
std::array<float, 4> track;
std::array<float, 4> trackFilled;
std::array<float, 4> thumb;
std::array<float, 4> thumbHover;
float trackHeight = 4;
float thumbRadius = 8;
};
struct ProgressColors {
std::array<float, 4> bg;
std::array<float, 4> fill;
float cornerRadius = 0;
};
// ─── component functions ───────────────────────────────────────────
// Background quad (color depends on state) + centered label glyphs.
void DrawButton(UIBuffer& buf, Rect r, std::string_view label,
bool hovered, bool pressed,
Font& font, float fontSize,
const ButtonColors& c);
// Outlined quad + a smaller filled inset quad when `checked`.
void DrawCheckbox(UIBuffer& buf, Rect r, bool checked, bool hovered,
const CheckboxColors& c);
// Thin track quad split at `t01` into filled/empty + a circular thumb
// (drawn as a quad with cornerRadius = thumbRadius).
void DrawSlider(UIBuffer& buf, Rect r, float t01, bool dragging,
const SliderColors& c);
// Background quad + a filled quad clipped to t01 of the inner width.
void DrawProgressBar(UIBuffer& buf, Rect r, float t01,
const ProgressColors& c);
}