Skip to content

Commit 36e4334

Browse files
authored
nuon: Text color (#280)
1 parent 86d0a2e commit 36e4334

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

neothesia/src/scene/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod playing_scene;
33

44
use crate::context::Context;
55
use iced_core::image::Renderer as _;
6+
use iced_graphics::text::cosmic_text;
67
use midi_file::midly::MidiMessage;
78
use neothesia_core::render::{QuadRenderer, TextRenderer};
89
use std::time::Duration;
@@ -92,9 +93,22 @@ fn render_nuon(ui: &mut nuon::Ui, nuon_renderer: &mut NuonRenderer, ctx: &mut Co
9293

9394
for text in layer.text.iter() {
9495
let buffer = if text.bold {
95-
TextRenderer::gen_buffer_bold(text.size, &text.text)
96+
TextRenderer::gen_buffer_with_attr(
97+
text.size,
98+
&text.text,
99+
cosmic_text::Attrs::new()
100+
.family(cosmic_text::Family::Name("Roboto"))
101+
.weight(cosmic_text::Weight::BOLD)
102+
.color(cosmic_text::Color(text.color.packet_u32())),
103+
)
96104
} else {
97-
TextRenderer::gen_buffer(text.size, &text.text)
105+
TextRenderer::gen_buffer_with_attr(
106+
text.size,
107+
&text.text,
108+
cosmic_text::Attrs::new()
109+
.family(cosmic_text::Family::Name("Roboto"))
110+
.color(cosmic_text::Color(text.color.packet_u32())),
111+
)
98112
};
99113

100114
match text.text_justify {

nuon/src/lib.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ impl Color {
5353
a,
5454
}
5555
}
56+
57+
pub fn packet_u32(&self) -> u32 {
58+
let r = self.r * 255.0;
59+
let g = self.g * 255.0;
60+
let b = self.b * 255.0;
61+
let a = self.a * 255.0;
62+
((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32)
63+
}
5664
}
5765

5866
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
@@ -184,6 +192,7 @@ pub struct TextRenderElement {
184192
pub size: f32,
185193
pub bold: bool,
186194
pub text: String,
195+
pub color: Color,
187196
}
188197

189198
#[derive(Debug, Clone)]
@@ -869,6 +878,8 @@ pub struct Label {
869878
pos: Point,
870879
size: Size,
871880
font_size: f32,
881+
text_justify: TextJustify,
882+
color: Color,
872883
text: String,
873884
icon: String,
874885
bold: bool,
@@ -890,6 +901,8 @@ impl Label {
890901
pos: Point::zero(),
891902
size: Size::new(50.0, 50.0),
892903
font_size: 13.0,
904+
text_justify: TextJustify::Center,
905+
color: Color::new(1.0, 1.0, 1.0, 1.0),
893906
text: String::new(),
894907
icon: String::new(),
895908
bold: false,
@@ -929,6 +942,16 @@ impl Label {
929942
self
930943
}
931944

945+
pub fn color(mut self, color: impl Into<Color>) -> Self {
946+
self.color = color.into();
947+
self
948+
}
949+
950+
pub fn text_justify(mut self, text_justify: TextJustify) -> Self {
951+
self.text_justify = text_justify;
952+
self
953+
}
954+
932955
pub fn text(mut self, text: impl Into<String>) -> Self {
933956
self.text = text.into();
934957
self
@@ -951,10 +974,11 @@ impl Label {
951974
if !self.text.is_empty() {
952975
layer.text.push(TextRenderElement {
953976
rect,
954-
text_justify: TextJustify::Center,
977+
text_justify: self.text_justify,
955978
size: self.font_size,
956979
bold: self.bold,
957980
text: self.text.to_string(),
981+
color: self.color,
958982
});
959983
}
960984

0 commit comments

Comments
 (0)