Added text color

master
Mark 2024-02-03 16:21:21 -08:00
parent eca7e7f5e6
commit b7839efc4b
Signed by: Mark
GPG Key ID: C6D63995FE72FD80
8 changed files with 52 additions and 24 deletions

View File

@ -52,7 +52,8 @@ fn init(state) {
-70.79, 138.0, 59.867, 10.0, -70.79, 138.0, 59.867, 10.0,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::Center SpriteAnchor::Center
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
if player.is_landed() { if player.is_landed() {
title.set_text(player.landed_on().name()); title.set_text(player.landed_on().name());
@ -67,7 +68,8 @@ fn init(state) {
-178.92, -20.3, 343.0, 81.467, -178.92, -20.3, 343.0, 81.467,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::Center SpriteAnchor::Center
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
if player.is_landed() { if player.is_landed() {
desc.set_text(player.landed_on().desc()); desc.set_text(player.landed_on().desc());

View File

@ -23,7 +23,8 @@ fn init(state) {
122.71, 48.0, 51.0, 12.0, 122.71, 48.0, 51.0, 12.0,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::SouthWest SpriteAnchor::SouthWest
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
exit_text.set_text("Exit"); exit_text.set_text("Exit");
@ -68,7 +69,8 @@ fn init(state) {
111.0, -167.27, 145.0, 10.0, 111.0, -167.27, 145.0, 10.0,
SpriteAnchor::Center, SpriteAnchor::Center,
SpriteAnchor::NorthWest SpriteAnchor::NorthWest
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
ship_name.set_text("Hyperion"); ship_name.set_text("Hyperion");
@ -79,7 +81,8 @@ fn init(state) {
111.0, -178.0, 145.0, 8.5, 111.0, -178.0, 145.0, 8.5,
SpriteAnchor::Center, SpriteAnchor::Center,
SpriteAnchor::NorthWest SpriteAnchor::NorthWest
) ),
Color(0.7, 0.7, 0.7, 1.0)
); );
if state.player_ship().is_some() { if state.player_ship().is_some() {
ship_type.set_text(state.player_ship().name()); ship_type.set_text(state.player_ship().name());
@ -94,7 +97,8 @@ fn init(state) {
38.526, -192.332, 144.948, 154.5, 38.526, -192.332, 144.948, 154.5,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
ship_stats.set_text("Earth"); ship_stats.set_text("Earth");
@ -127,7 +131,8 @@ fn init(state) {
-312.0, -20.0, 200.0, 16.0, -312.0, -20.0, 200.0, 16.0,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::NorthEast, SpriteAnchor::NorthEast,
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
outfit_name.set_text("Earth"); outfit_name.set_text("Earth");
@ -138,7 +143,8 @@ fn init(state) {
-166.0, -219.0, 260.0, 78.0, -166.0, -219.0, 260.0, 78.0,
SpriteAnchor::Center, SpriteAnchor::Center,
SpriteAnchor::NorthEast, SpriteAnchor::NorthEast,
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
outfit_desc.set_text("Earth"); outfit_desc.set_text("Earth");
@ -149,7 +155,8 @@ fn init(state) {
-295.0, -271.0, 164.0, 216.0, -295.0, -271.0, 164.0, 216.0,
SpriteAnchor::NorthWest, SpriteAnchor::NorthWest,
SpriteAnchor::NorthEast, SpriteAnchor::NorthEast,
) ),
Color(1.0, 1.0, 1.0, 1.0)
); );
outfit_stats.set_text("Earth"); outfit_stats.set_text("Earth");

View File

@ -9,13 +9,23 @@ pub struct Color {
impl Color { impl Color {
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self { pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { Self {
val: Vector4::new(r, g, b, a), val: Vector4::new(
r.clamp(0.0, 1.0),
g.clamp(0.0, 1.0),
b.clamp(0.0, 1.0),
a.clamp(0.0, 1.0),
),
} }
} }
pub fn as_array(&self) -> [f32; 4] { pub fn as_array(&self) -> [f32; 4] {
[self.val.x, self.val.y, self.val.z, self.val.w] [self.val.x, self.val.y, self.val.z, self.val.w]
} }
pub fn as_array_u8(&self) -> [u8; 4] {
let val = self.val * 255.0;
[val.x as u8, val.y as u8, val.z as u8, val.w as u8]
}
} }
impl CustomType for Color { impl CustomType for Color {

View File

@ -1,6 +1,6 @@
use rhai::{CustomType, ImmutableString, TypeBuilder}; use rhai::{CustomType, ImmutableString, TypeBuilder};
use super::{Rect, TextBoxFont, TextBoxJustify}; use super::{Color, Rect, TextBoxFont, TextBoxJustify};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct TextBoxBuilder { pub struct TextBoxBuilder {
@ -11,6 +11,7 @@ pub struct TextBoxBuilder {
pub justify: TextBoxJustify, pub justify: TextBoxJustify,
pub rect: Rect, pub rect: Rect,
pub text: ImmutableString, pub text: ImmutableString,
pub color: Color,
} }
impl TextBoxBuilder { impl TextBoxBuilder {
@ -21,8 +22,10 @@ impl TextBoxBuilder {
font: TextBoxFont, font: TextBoxFont,
justify: TextBoxJustify, justify: TextBoxJustify,
rect: Rect, rect: Rect,
color: Color,
) -> Self { ) -> Self {
Self { Self {
color,
name, name,
font_size, font_size,
line_height, line_height,

View File

@ -169,6 +169,7 @@ impl UiManager {
t.font, t.font,
t.justify, t.justify,
t.rect, t.rect,
t.color,
); );
b.set_text(state, &t.text); b.set_text(state, &t.text);
self.elements.push(UiElement::new_text(b)); self.elements.push(UiElement::new_text(b));

View File

@ -7,10 +7,10 @@ use crate::{ui::api::Color, vertexbuffer::types::RadialBarInstance, RenderInput,
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RadialBar { pub struct RadialBar {
pub name: String, pub name: String,
pub rect: Rect, rect: Rect,
pub stroke: f32, stroke: f32,
pub color: Color, color: Color,
pub progress: f32, progress: f32,
} }
impl RadialBar { impl RadialBar {

View File

@ -7,17 +7,18 @@ use crate::{ui::event::Event, vertexbuffer::types::UiInstance, RenderInput, Rend
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Sprite { pub struct Sprite {
pub name: String,
/// If this is none, this was constructed with an invalid sprite /// If this is none, this was constructed with an invalid sprite
pub anim: Option<SpriteAutomaton>, pub anim: Option<SpriteAutomaton>,
rect: Rect, rect: Rect,
mask: Option<SpriteHandle>, mask: Option<SpriteHandle>,
pub name: String,
has_mouse: bool,
has_click: bool,
/// If true, ignore mouse events until click is released /// If true, ignore mouse events until click is released
waiting_for_release: bool, waiting_for_release: bool,
has_mouse: bool,
has_click: bool,
} }
impl Sprite { impl Sprite {

View File

@ -2,15 +2,16 @@ use glyphon::{cosmic_text::Align, Attrs, Buffer, Color, Metrics, Shaping, TextAr
use nalgebra::Vector2; use nalgebra::Vector2;
use super::super::api::{Rect, TextBoxFont, TextBoxJustify}; use super::super::api::{Rect, TextBoxFont, TextBoxJustify};
use crate::{RenderInput, RenderState}; use crate::{ui::api, RenderInput, RenderState};
#[derive(Debug)] #[derive(Debug)]
pub struct TextBox { pub struct TextBox {
pub name: String, pub name: String,
pub font: TextBoxFont, font: TextBoxFont,
pub justify: TextBoxJustify, justify: TextBoxJustify,
pub rect: Rect, rect: Rect,
pub buffer: Buffer, buffer: Buffer,
color: api::Color,
} }
impl TextBox { impl TextBox {
@ -22,6 +23,7 @@ impl TextBox {
font: TextBoxFont, font: TextBoxFont,
justify: TextBoxJustify, justify: TextBoxJustify,
rect: Rect, rect: Rect,
color: api::Color,
) -> Self { ) -> Self {
let mut buffer = Buffer::new( let mut buffer = Buffer::new(
&mut state.text_font_system, &mut state.text_font_system,
@ -37,6 +39,7 @@ impl TextBox {
justify, justify,
rect, rect,
buffer, buffer,
color,
} }
} }
@ -73,6 +76,7 @@ impl<'a, 'b: 'a> TextBox {
state.window_size.height as f32 / 2.0 - (rect.pos.y * fac + rect.dim.y / 2.0), state.window_size.height as f32 / 2.0 - (rect.pos.y * fac + rect.dim.y / 2.0),
); );
let corner_sw = corner_ne + rect.dim * fac; let corner_sw = corner_ne + rect.dim * fac;
let c = self.color.as_array_u8();
TextArea { TextArea {
buffer: &self.buffer, buffer: &self.buffer,
@ -85,7 +89,7 @@ impl<'a, 'b: 'a> TextBox {
left: (corner_ne.x) as i32, left: (corner_ne.x) as i32,
right: (corner_sw.x) as i32, right: (corner_sw.x) as i32,
}, },
default_color: Color::rgb(255, 255, 255), default_color: Color::rgba(c[0], c[1], c[2], c[3]),
} }
} }
} }