Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline text layout and rendering #273

Merged
merged 15 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
663 changes: 523 additions & 140 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion engine/lib/luajit-ffi-gen/src/impl_item/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,14 @@ fn gen_func_body(self_ident: &Ident, method: &MethodInfo) -> TokenStream {
},
TypeVariant::CString => quote! { #name_accessor.as_cstring() },
TypeVariant::Custom(custom_ty) => {
if param.ty.is_reference || param.ty.is_boxed || TypeInfo::is_copyable(&custom_ty) {
if param.ty.is_boxed || TypeInfo::is_copyable(&custom_ty) {
quote! { #name_accessor }
} else if param.ty.is_reference {
if param.ty.is_option {
quote! { &#name_accessor }
} else {
quote! { #name_accessor }
}
} else {
quote! { *#name_accessor }
}
Expand Down
1 change: 1 addition & 0 deletions engine/lib/luajit-ffi-gen/src/impl_item/type_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const COPY_TYPES: &[&str] = &[
"HmGuiPropertyType",
"LayoutType",
"HmGuiStyleId",
"TextAlignment",
];

#[derive(Debug)]
Expand Down
3 changes: 3 additions & 0 deletions engine/lib/phx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ kira = { version = "0.8", features = ["symphonia", "cpal", "serde"] }
libc = "0.2"
memoffset = "0.9"
mlua = { version = "0.9", features = ["luajit52", "vendored"] }
once_cell = "1.19"
parley = "0.1"
swash = "0.1"
rapier3d-f64 = { version = "0.18", features = ["simd-stable", "debug-render"] }
num_cpus = "1.15"
raw-window-handle = "0.5"
Expand Down
2 changes: 1 addition & 1 deletion engine/lib/phx/script/ffi_gen/Font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Loader.defineType()
ffi.cdef [[
void Font_Free (Font*);
Font* Font_Load (cstr name, uint32 size);
void Font_Draw (Font const*, cstr text, float x, float y, float r, float g, float b, float a);
void Font_Draw (Font const*, cstr text, float x, float y, Color const* color);
void Font_DrawShaded (Font const*, cstr text, float x, float y);
int Font_GetLineHeight (Font const*);
void Font_GetSize (Font const*, cstr text, Vec4i* out);
Expand Down
2 changes: 2 additions & 0 deletions engine/lib/phx/script/ffi_gen/HmGui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function Loader.defineType()
void HmGui_Image (HmGui*, Tex2D* image);
void HmGui_Rect (HmGui*);
void HmGui_Text (HmGui*, cstr text, Font const* font, Color const* color);
void HmGui_TextView (HmGui*, TextData const* textData);
bool HmGui_IsMouseOver (HmGui const*, FocusType ty);
void HmGui_SetMinWidth (HmGui const*, float width);
void HmGui_SetMinHeight (HmGui const*, float height);
Expand Down Expand Up @@ -108,6 +109,7 @@ function Loader.defineType()
image = libphx.HmGui_Image,
rect = libphx.HmGui_Rect,
text = libphx.HmGui_Text,
textView = libphx.HmGui_TextView,
isMouseOver = libphx.HmGui_IsMouseOver,
setMinWidth = libphx.HmGui_SetMinWidth,
setMinHeight = libphx.HmGui_SetMinHeight,
Expand Down
45 changes: 45 additions & 0 deletions engine/lib/phx/script/ffi_gen/TextAlignment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
-- TextAlignment ---------------------------------------------------------------
local Loader = {}

function Loader.declareType()
ffi.cdef [[
typedef uint8 TextAlignment;
]]

return 2, 'TextAlignment'
end

function Loader.defineType()
local ffi = require('ffi')
local libphx = require('libphx').lib
local TextAlignment

do -- C Definitions
ffi.cdef [[
TextAlignment TextAlignment_Start;
TextAlignment TextAlignment_Middle;
TextAlignment TextAlignment_End;
TextAlignment TextAlignment_Justified;

cstr TextAlignment_ToString(TextAlignment);
]]
end

do -- Global Symbol Table
TextAlignment = {
Start = libphx.TextAlignment_Start,
Middle = libphx.TextAlignment_Middle,
End = libphx.TextAlignment_End,
Justified = libphx.TextAlignment_Justified,

ToString = libphx.TextAlignment_ToString,
}

if onDef_TextAlignment then onDef_TextAlignment(TextAlignment, mt) end
TextAlignment = setmetatable(TextAlignment, mt)
end

return TextAlignment
end

return Loader
52 changes: 52 additions & 0 deletions engine/lib/phx/script/ffi_gen/TextData.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
-- TextData --------------------------------------------------------------------
local Loader = {}

function Loader.declareType()
ffi.cdef [[
typedef struct TextData {} TextData;
]]

return 1, 'TextData'
end

function Loader.defineType()
local ffi = require('ffi')
local libphx = require('libphx').lib
local TextData

do -- C Definitions
ffi.cdef [[
void TextData_Free (TextData*);
TextData* TextData_Create (cstr text, TextStyle const* defaultStyle, TextAlignment alignment, bool multiline);
void TextData_SetSectionStyle (TextData*, uint64 startPos, uint64 endPos, TextStyle const* style);
]]
end

do -- Global Symbol Table
TextData = {
Create = function(...)
local instance = libphx.TextData_Create(...)
return Core.ManagedObject(instance, libphx.TextData_Free)
end,
}

if onDef_TextData then onDef_TextData(TextData, mt) end
TextData = setmetatable(TextData, mt)
end

do -- Metatype for class instances
local t = ffi.typeof('TextData')
local mt = {
__index = {
setSectionStyle = libphx.TextData_SetSectionStyle,
},
}

if onDef_TextData_t then onDef_TextData_t(t, mt) end
TextData_t = ffi.metatype(t, mt)
end

return TextData
end

return Loader
86 changes: 86 additions & 0 deletions engine/lib/phx/script/ffi_gen/TextStyle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
-- TextStyle -------------------------------------------------------------------
local Loader = {}

function Loader.declareType()
ffi.cdef [[
typedef struct TextStyle {} TextStyle;
]]

return 1, 'TextStyle'
end

function Loader.defineType()
local ffi = require('ffi')
local libphx = require('libphx').lib
local TextStyle

do -- C Definitions
ffi.cdef [[
void TextStyle_Free (TextStyle*);
TextStyle* TextStyle_Create ();
void TextStyle_SetFontFamily (TextStyle*, cstr family);
void TextStyle_SetFontSize (TextStyle*, float size);
void TextStyle_SetFontStretch (TextStyle*, float stretch);
void TextStyle_SetFontItalic (TextStyle*, bool italic);
void TextStyle_SetFontWeight (TextStyle*, float weight);
void TextStyle_SetLocale (TextStyle*, cstr locale);
void TextStyle_SetBrush (TextStyle*, Color const* color);
void TextStyle_SetUnderline (TextStyle*, bool underline);
void TextStyle_SetUnderlineOffset (TextStyle*, float offset);
void TextStyle_SetUnderlineSize (TextStyle*, float size);
void TextStyle_SetUnderlineBrush (TextStyle*, Color const* color);
void TextStyle_SetStrikethrough (TextStyle*, bool strikethrough);
void TextStyle_SetStrikethroughOffset (TextStyle*, float offset);
void TextStyle_SetStrikethroughSize (TextStyle*, float size);
void TextStyle_SetStrikethroughBrush (TextStyle*, Color const* color);
void TextStyle_SetLineHeight (TextStyle*, float height);
void TextStyle_SetWordSpacing (TextStyle*, float size);
void TextStyle_SetLetterSpacing (TextStyle*, float size);
]]
end

do -- Global Symbol Table
TextStyle = {
Create = function(...)
local instance = libphx.TextStyle_Create(...)
return Core.ManagedObject(instance, libphx.TextStyle_Free)
end,
}

if onDef_TextStyle then onDef_TextStyle(TextStyle, mt) end
TextStyle = setmetatable(TextStyle, mt)
end

do -- Metatype for class instances
local t = ffi.typeof('TextStyle')
local mt = {
__index = {
setFontFamily = libphx.TextStyle_SetFontFamily,
setFontSize = libphx.TextStyle_SetFontSize,
setFontStretch = libphx.TextStyle_SetFontStretch,
setFontItalic = libphx.TextStyle_SetFontItalic,
setFontWeight = libphx.TextStyle_SetFontWeight,
setLocale = libphx.TextStyle_SetLocale,
setBrush = libphx.TextStyle_SetBrush,
setUnderline = libphx.TextStyle_SetUnderline,
setUnderlineOffset = libphx.TextStyle_SetUnderlineOffset,
setUnderlineSize = libphx.TextStyle_SetUnderlineSize,
setUnderlineBrush = libphx.TextStyle_SetUnderlineBrush,
setStrikethrough = libphx.TextStyle_SetStrikethrough,
setStrikethroughOffset = libphx.TextStyle_SetStrikethroughOffset,
setStrikethroughSize = libphx.TextStyle_SetStrikethroughSize,
setStrikethroughBrush = libphx.TextStyle_SetStrikethroughBrush,
setLineHeight = libphx.TextStyle_SetLineHeight,
setWordSpacing = libphx.TextStyle_SetWordSpacing,
setLetterSpacing = libphx.TextStyle_SetLetterSpacing,
},
}

if onDef_TextStyle_t then onDef_TextStyle_t(t, mt) end
TextStyle_t = ffi.metatype(t, mt)
end

return TextStyle
end

return Loader
7 changes: 2 additions & 5 deletions engine/lib/phx/script/meta/Font.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,8 @@ function Font.Load(name, size) end
---@param text string
---@param x number
---@param y number
---@param r number
---@param g number
---@param b number
---@param a number
function Font:draw(text, x, y, r, g, b, a) end
---@param color Color
function Font:draw(text, x, y, color) end

---@param text string
---@param x number
Expand Down
4 changes: 4 additions & 0 deletions engine/lib/phx/script/meta/HmGui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ function HmGui:rect() end
---@param color Color
function HmGui:text(text, font, color) end

-- Add multiline styled text element.
---@param textData TextData
function HmGui:textView(textData) end

-- Makes current widget `focusable` and returns true if mouse is over it.
-- Returns true if mouse is over the widget (was calculated in the previous frame).
---@param ty FocusType
Expand Down
10 changes: 10 additions & 0 deletions engine/lib/phx/script/meta/TextAlignment.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---@meta

---@enum TextAlignment
TextAlignment = {
Start = 0,
Middle = 1,
End = 2,
Justified = 3,
}

18 changes: 18 additions & 0 deletions engine/lib/phx/script/meta/TextData.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---@meta

---@class TextData
TextData = {}

---@param text string
---@param defaultStyle TextStyle
---@param alignment TextAlignment
---@param multiline boolean
---@return TextData
function TextData.Create(text, defaultStyle, alignment, multiline) end

-- Set style of the text section beginning at 'start_pos' position and up to 'end_pos'.
---@param startPos integer
---@param endPos integer
---@param style TextStyle
function TextData:setSectionStyle(startPos, endPos, style) end

79 changes: 79 additions & 0 deletions engine/lib/phx/script/meta/TextStyle.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---@meta

---@class TextStyle
TextStyle = {}

---@return TextStyle
function TextStyle.Create() end

-- Font family list in CSS format.
---@param family string
function TextStyle:setFontFamily(family) end

---@param size number
function TextStyle:setFontSize(size) end

-- Visual width of a font-- a relative change from the normal aspect
-- ratio, typically in the range 0.5 to 2.0.
---@param stretch number
function TextStyle:setFontStretch(stretch) end

-- Specify whether font italic or normal.
---@param italic boolean
function TextStyle:setFontItalic(italic) end

-- Visual weight class of a font, typically on a scale from 1.0 to 1000.0.
---@param weight number
function TextStyle:setFontWeight(weight) end

---@param locale string
function TextStyle:setLocale(locale) end

-- Brush for rendering text.
---@param color Color
function TextStyle:setBrush(color) end

-- Underline decoration.
---@param underline boolean
function TextStyle:setUnderline(underline) end

-- Offset of the underline decoration.
---@param offset number
function TextStyle:setUnderlineOffset(offset) end

-- Size of the underline decoration.
---@param size number
function TextStyle:setUnderlineSize(size) end

-- Brush for rendering the underline decoration.
---@param color Color
function TextStyle:setUnderlineBrush(color) end

-- Strikethrough decoration.
---@param strikethrough boolean
function TextStyle:setStrikethrough(strikethrough) end

-- Offset of the strikethrough decoration.
---@param offset number
function TextStyle:setStrikethroughOffset(offset) end

-- Size of the strikethrough decoration.
---@param size number
function TextStyle:setStrikethroughSize(size) end

-- Brush for rendering the strikethrough decoration.
---@param color Color
function TextStyle:setStrikethroughBrush(color) end

-- Line height multiplier.
---@param height number
function TextStyle:setLineHeight(height) end

-- Extra spacing between words.
---@param size number
function TextStyle:setWordSpacing(size) end

-- Extra spacing between letters.
---@param size number
function TextStyle:setLetterSpacing(size) end

Loading