Added text attributes

master
Mark 2024-02-03 16:45:47 -08:00
parent b7839efc4b
commit 62b8a4f2d3
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
8 changed files with 69 additions and 97 deletions

View File

@ -4,13 +4,9 @@
- outfitter
- Clean up scripting & errors
- Clean up sprite content (and content in general)
- Arbitrary scene names
- Text style, color, formatting
- Flying UI
- Mouse colliders
- Clean renderscene/UI scene/change scene on land
- UI captures input?
- Scriptable renderscene: script decides which parts to show
- No UI zoom scroll
- Preserve aspect for icons
- Check game version in config

View File

@ -46,8 +46,7 @@ fn init(state) {
);
let title = TextBoxBuilder(
"title",
10.0, 10.0, TextBoxFont::Serif, TextBoxJustify::Center,
"title", 10.0, 10.0,
Rect(
-70.79, 138.0, 59.867, 10.0,
SpriteAnchor::NorthWest,
@ -60,10 +59,12 @@ fn init(state) {
} else {
title.set_text("");
}
title.align_center();
title.font_serif();
title.weight_bold();
let desc = TextBoxBuilder(
"desc",
7.5, 8.0, TextBoxFont::SansSerif, TextBoxJustify::Left,
"desc", 7.5, 8.0,
Rect(
-178.92, -20.3, 343.0, 81.467,
SpriteAnchor::NorthWest,
@ -76,6 +77,7 @@ fn init(state) {
} else {
desc.set_text("");
}
desc.font_sansserif();
return [
button,

View File

@ -17,8 +17,7 @@ fn init(state) {
);
let exit_text = TextBoxBuilder(
"exit_text",
10.0, 10.0, TextBoxFont::Serif, TextBoxJustify::Center,
"exit_text", 10.0, 10.0,
Rect(
122.71, 48.0, 51.0, 12.0,
SpriteAnchor::NorthWest,
@ -27,6 +26,8 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
exit_text.set_text("Exit");
exit_text.font_serif();
exit_text.align_center();
let exit_button = SpriteBuilder(
"exit_button",
@ -63,8 +64,7 @@ fn init(state) {
);
let ship_name = TextBoxBuilder(
"ship_name",
10.0, 10.0, TextBoxFont::Serif, TextBoxJustify::Center,
"ship_name", 10.0, 10.0,
Rect(
111.0, -167.27, 145.0, 10.0,
SpriteAnchor::Center,
@ -73,10 +73,11 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
ship_name.set_text("Hyperion");
ship_name.font_serif();
ship_name.align_center();
let ship_type = TextBoxBuilder(
"ship_type",
7.0, 8.5, TextBoxFont::SansSerif, TextBoxJustify::Center,
"ship_type", 7.0, 8.5,
Rect(
111.0, -178.0, 145.0, 8.5,
SpriteAnchor::Center,
@ -89,10 +90,11 @@ fn init(state) {
} else {
ship_type.set_text("ERR: SHIP IS NONE");
}
ship_type.font_sansserif();
ship_type.align_center();
let ship_stats = TextBoxBuilder(
"ship_stats",
7.0, 8.5, TextBoxFont::Monospace, TextBoxJustify::Left,
"ship_stats", 7.0, 8.5,
Rect(
38.526, -192.332, 144.948, 154.5,
SpriteAnchor::NorthWest,
@ -101,7 +103,7 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
ship_stats.set_text("Earth");
ship_stats.font_monospace();
let outfit_bg = SpriteBuilder(
@ -125,8 +127,7 @@ fn init(state) {
);
let outfit_name = TextBoxBuilder(
"outfit_name",
16.0, 16.0, TextBoxFont::Serif, TextBoxJustify::Left,
"outfit_name", 16.0, 16.0,
Rect(
-312.0, -20.0, 200.0, 16.0,
SpriteAnchor::NorthWest,
@ -135,10 +136,11 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
outfit_name.set_text("Earth");
outfit_name.font_serif();
outfit_name.weight_bold();
let outfit_desc = TextBoxBuilder(
"outfit_desc",
7.0, 8.5, TextBoxFont::SansSerif, TextBoxJustify::Left,
"outfit_desc", 7.0, 8.5,
Rect(
-166.0, -219.0, 260.0, 78.0,
SpriteAnchor::Center,
@ -147,10 +149,10 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
outfit_desc.set_text("Earth");
outfit_desc.font_serif();
let outfit_stats = TextBoxBuilder(
"outfit_stats",
7.0, 8.5, TextBoxFont::Monospace, TextBoxJustify::Left,
"outfit_stats", 7.0, 8.5,
Rect(
-295.0, -271.0, 164.0, 216.0,
SpriteAnchor::NorthWest,
@ -159,7 +161,8 @@ fn init(state) {
Color(1.0, 1.0, 1.0, 1.0)
);
outfit_stats.set_text("Earth");
ship_stats.font_monospace();
return [
ship_bg,
ship_thumb,

View File

@ -46,16 +46,6 @@ pub fn register_into_engine(engine: &mut Engine) {
// Larger modules
.register_type_with_name::<SpriteAnchor>("SpriteAnchor")
.register_static_module("SpriteAnchor", exported_module!(spriteanchor_mod).into())
.register_type_with_name::<TextBoxFont>("TextBoxFont")
.register_static_module(
"TextBoxFont",
exported_module!(util::textboxfont_mod).into(),
)
.register_type_with_name::<TextBoxJustify>("TextBoxJustify")
.register_static_module(
"TextBoxJustify",
exported_module!(textboxjustify_mod).into(),
)
.register_type_with_name::<SceneAction>("SceneAction")
.register_static_module("SceneAction", exported_module!(sceneaction_mod).into());
}

View File

@ -1,17 +1,19 @@
use glyphon::{cosmic_text::Align, Attrs, AttrsOwned, FamilyOwned, Style, Weight};
use rhai::{CustomType, ImmutableString, TypeBuilder};
use super::{Color, Rect, TextBoxFont, TextBoxJustify};
use super::{Color, Rect};
#[derive(Debug, Clone)]
pub struct TextBoxBuilder {
pub name: ImmutableString,
pub font_size: f32,
pub line_height: f32,
pub font: TextBoxFont,
pub justify: TextBoxJustify,
pub rect: Rect,
pub text: ImmutableString,
pub color: Color,
pub attrs: AttrsOwned,
pub justify: Align,
}
impl TextBoxBuilder {
@ -19,8 +21,6 @@ impl TextBoxBuilder {
name: ImmutableString,
font_size: f32,
line_height: f32,
font: TextBoxFont,
justify: TextBoxJustify,
rect: Rect,
color: Color,
) -> Self {
@ -29,10 +29,10 @@ impl TextBoxBuilder {
name,
font_size,
line_height,
font,
justify,
rect,
text: ImmutableString::new(),
attrs: AttrsOwned::new(Attrs::new()),
justify: Align::Left,
}
}
@ -46,6 +46,25 @@ impl CustomType for TextBoxBuilder {
builder
.with_name("TextBoxBuilder")
.with_fn("TextBoxBuilder", Self::new)
.with_fn("set_text", Self::set_text);
.with_fn("set_text", Self::set_text)
.with_fn("align_left", |s: &mut Self| s.justify = Align::Left)
.with_fn("align_right", |s: &mut Self| s.justify = Align::Right)
.with_fn("align_justify", |s: &mut Self| s.justify = Align::Justified)
.with_fn("align_center", |s: &mut Self| s.justify = Align::Center)
.with_fn("weight_bold", |s: &mut Self| s.attrs.weight = Weight::BOLD)
.with_fn("weight_normal", |s: &mut Self| {
s.attrs.weight = Weight::NORMAL
})
.with_fn("font_serif", |s: &mut Self| {
s.attrs.family_owned = FamilyOwned::Serif
})
.with_fn("font_sansserif", |s: &mut Self| {
s.attrs.family_owned = FamilyOwned::SansSerif
})
.with_fn("font_monospace", |s: &mut Self| {
s.attrs.family_owned = FamilyOwned::Monospace
})
.with_fn("style_normal", |s: &mut Self| s.attrs.style = Style::Normal)
.with_fn("style_italic", |s: &mut Self| s.attrs.style = Style::Italic);
}
}

View File

@ -26,37 +26,3 @@ pub mod spriteanchor_mod {
#[allow(non_upper_case_globals)]
pub const SouthEast: SpriteAnchor = SpriteAnchor::SouthEast;
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum TextBoxFont {
Serif,
SansSerif,
Monospace,
}
#[export_module]
pub mod textboxfont_mod {
#[allow(non_upper_case_globals)]
pub const Serif: TextBoxFont = TextBoxFont::Serif;
#[allow(non_upper_case_globals)]
pub const SansSerif: TextBoxFont = TextBoxFont::SansSerif;
#[allow(non_upper_case_globals)]
pub const Monospace: TextBoxFont = TextBoxFont::Monospace;
}
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub enum TextBoxJustify {
Center,
Left,
}
#[export_module]
pub mod textboxjustify_mod {
#[allow(non_upper_case_globals)]
pub const Center: TextBoxJustify = TextBoxJustify::Center;
#[allow(non_upper_case_globals)]
pub const Left: TextBoxJustify = TextBoxJustify::Left;
}

View File

@ -166,8 +166,8 @@ impl UiManager {
t.name.to_string(),
t.font_size,
t.line_height,
t.font,
t.justify,
t.attrs,
t.rect,
t.color,
);

View File

@ -1,17 +1,19 @@
use glyphon::{cosmic_text::Align, Attrs, Buffer, Color, Metrics, Shaping, TextArea, TextBounds};
use glyphon::{
cosmic_text::Align, AttrsOwned, Buffer, Color, Metrics, Shaping, TextArea, TextBounds,
};
use nalgebra::Vector2;
use super::super::api::{Rect, TextBoxFont, TextBoxJustify};
use super::super::api::Rect;
use crate::{ui::api, RenderInput, RenderState};
#[derive(Debug)]
pub struct TextBox {
pub name: String,
font: TextBoxFont,
justify: TextBoxJustify,
justify: Align,
rect: Rect,
buffer: Buffer,
color: api::Color,
attrs: AttrsOwned,
}
impl TextBox {
@ -20,8 +22,8 @@ impl TextBox {
name: String,
font_size: f32,
line_height: f32,
font: TextBoxFont,
justify: TextBoxJustify,
justify: Align,
attrs: AttrsOwned,
rect: Rect,
color: api::Color,
) -> Self {
@ -35,30 +37,24 @@ impl TextBox {
Self {
name,
font,
justify,
rect,
buffer,
color,
attrs,
}
}
pub fn set_text(&mut self, state: &mut RenderState, text: &str) {
let mut attrs = Attrs::new();
attrs = match self.font {
TextBoxFont::Monospace => attrs.family(glyphon::Family::Monospace),
TextBoxFont::SansSerif => attrs.family(glyphon::Family::SansSerif),
TextBoxFont::Serif => attrs.family(glyphon::Family::Serif),
};
self.buffer
.set_text(&mut state.text_font_system, text, attrs, Shaping::Advanced);
self.buffer.set_text(
&mut state.text_font_system,
text,
self.attrs.as_attrs(),
Shaping::Advanced,
);
for l in &mut self.buffer.lines {
l.set_align(Some(match self.justify {
TextBoxJustify::Center => Align::Center,
TextBoxJustify::Left => Align::Left,
}));
l.set_align(Some(self.justify));
}
self.buffer.shape_until_scroll(&mut state.text_font_system);