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

152 lines
5.7 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 TextBase.hpp
/// @desc Demo Program Entry Point
/// @edit 2024-11-16
#pragma once
#include <J3ML/LinearAlgebra.hpp>
#include <Color4.hpp>
#include <JGL/JGL.h>
#include <JGL/types/Font.h>
#include "../Style/TextStyler.hpp"
using J3ML::LinearAlgebra::Vector2;
namespace JUI {
class TextBase;
/// How should a text widget behave when it's text runs out of room?
enum class TextOverflowMode {
WRAP, /// Wraps at the nearest 'word', or otherwise appropriate stop in text.
WRAP_ANYWHERE, /// Wraps the text anywhere.
TRUNCATE, /// Cuts the text off and suffixes '...' to indicate the lack of the full intended message.
TRUNCATE_ANYWHERE, /// Cuts the text off and suffixes '...' to indicate the lack of the full intended message.
TRUNCATE_NO_DOTS, /// Cuts the text off at the first appropriate break in text.
TRUNCATE_ANYWHERE_NO_DOTS /// Cuts the text off at any point in the string.
};
}
/// Enumerations for alignment of text within a desired "Box".
namespace JUI::TextAlign {
// Horizontal
enum class H { Left, Center, Right };
// Vertical
enum class V { Top, Center, Bottom };
}
/// TextBase class, implements core mechanics of drawing text in JUI, and is used by Text Widget class.
class JUI::TextBase : public TextStyler {
public:
#pragma region Constructors
~TextBase() { delete text_canvas; };
TextBase() : TextStyler(), text_canvas(new RenderTarget({1, 1}, {0,0,0,0})) {};
#pragma endregion
//TextBase() : set_font(JGL::Fonts::Jupiteroid), state_redraw(true), text_canvas(new RenderTarget({1, 1}, {0, 0, 0, 0})) {};
public:
#pragma region Getters
#pragma endregion
public:
#pragma region Setters
#pragma endregion
void Update(float delta);
// I don't know why this function even exists, or why it was public. It lets you circumvent
// the whole purpose of storing the state things are in :/ - Redacted.
void Draw(const Vector2& abs_pos, const Vector2& abs_size, const std::string& content, unsigned int size, const Color4& color);
/// Renders the aligned text string within a bounding-box specified by abs_pos (top-left corner), and abs_size.
/// @see Widget::Draw(), Text::Draw().
void Draw(const Vector2& abs_pos, const Vector2& abs_size);
private:
bool state_redraw = true;
protected:
RenderTarget* text_canvas;
std::string content = "Sample Text";
float text_outline = 1.f;
Color4 outline_color = {255,255,255};
bool word_wrap = false;
TextAlign::H h_align = TextAlign::H::Left;
TextAlign::V v_align = TextAlign::V::Top;
TextOverflowMode overflow_mode;
//Color4 text_color = {255,255,255};
//JGL::Font set_font = JGL::Fonts::Jupiteroid;
//u32 text_size = 12;
protected:
public:
[[nodiscard]] std::string Content() const;
[[nodiscard]] Color4 OutlineColor() const;
[[nodiscard]] TextAlign::H HorizontalTextAlign() const;
[[nodiscard]] TextAlign::V VerticalTextAlign() const;
TextOverflowMode OverflowMode() const;
[[nodiscard]] Vector2 TextBounds();
[[nodiscard]] Vector2 TextPosition() const;
public:
void WordWrap(bool wrap);
void OverflowMode(const TextOverflowMode& mode);
void Content(const std::string& content);
/// @note This member is deprecated in favor of TextSize().
void TextSize(int size) override;
/// @note This member is deprecated in favor of Font().
void Font(const JGL::Font& font) override;
/// @note This member is deprecated in favor of TextColor().
void TextColor(const Color4& color) override;
void OutlineColor(const Color4& color);
/// Set the horizontal alignment of this text widget in relation to it's parent,
/// while keeping the text retained inside the parent widget's bounds.
/// @see TextAlign::H
void HorizontalTextAlign(const TextAlign::H& align);
/// Set the vertical alignment of this text widget in relation to it's parent,
/// while keeping the text retained inside the parent widget's bounds.
/// @see TextAlign::V
void VerticalTextAlign(const TextAlign::V& align);
/// Sets the horizontal and vertical alignment of this text widget.
/// @see TextAlign::H and TextAlign::V.
void Align(const TextAlign::H& h_align, const TextAlign::V& v_align);
/// Aligns the text of this widget to the left-hand-side of the parent's bounding box.
/// @see SetHorizontalTextAlign, TextAlign::H
void AlignLeft();
/// Aligns the text of this widget to the right-hand-side of the parent's bounding box.
/// @see SetHorizontalTextAlign, TextAlign::H
void AlignRight();
/// Aligns the text of this widget to the top of the parent's bounding box.
/// @see SetVerticalTextAlign, TextAlign::V
void AlignTop();
/// Aligns the text of this widget to the bottom of the parent's bounding box.
/// @see SetVerticalTextAlign, TextAlign::V
void AlignBottom();
/// Centers the text of this widget in relation to the parent's bounding box.
/// @see SetHorizontalTextAlign, SetVerticalTextAlign, TextAlign enums
void AlignCenterBoth();
/// Alias for AlignCenterBoth().
void Center();
/// Aligns the text of this widget to the horizontal center of the parent's bounding box.
void AlignCenterHorizontally();
/// Aligns the text of this widget to the vertical center of the parent's bounding box.
void AlignCenterVertically();
};