hint gaming

This commit is contained in:
2025-11-21 20:18:39 -05:00
parent 23c5abe5a9
commit 31ff17c21a
5 changed files with 58 additions and 19 deletions

View File

@@ -1,20 +1,15 @@
use std::marker::{PhantomData, Sized};
use crate::prelude::*;
use cosmic_text::{Attrs, Family, Metrics};
pub trait TextBuilderOutput: Sized {
type Output;
fn run(ui: &mut Ui, builder: TextBuilder<Self>) -> Self::Output;
}
pub struct TextBuilder<O = TextOutput> {
pub struct TextBuilder<O = TextOutput, H: WidgetOption = ()> {
pub content: String,
pub attrs: TextAttrs,
pub hint: H,
_pd: PhantomData<O>,
}
impl<O> TextBuilder<O> {
impl<O, H: WidgetOption> TextBuilder<O, H> {
pub fn size(mut self, size: impl UiNum) -> Self {
self.attrs.font_size = size.to_f32();
self.attrs.line_height = self.attrs.font_size * 1.1;
@@ -40,29 +35,47 @@ impl<O> TextBuilder<O> {
self.attrs.wrap = wrap;
self
}
pub fn editable(self) -> TextBuilder<TextEditOutput> {
pub fn editable(self) -> TextBuilder<TextEditOutput, H> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: self.hint,
_pd: PhantomData,
}
}
}
impl<O> TextBuilder<O> {
pub fn hint<W: WidgetLike<Tag>, Tag>(self, hint: W) -> TextBuilder<O, impl WidgetOption> {
TextBuilder {
content: self.content,
attrs: self.attrs,
hint: move |ui: &mut Ui| Some(hint.add(ui).any()),
_pd: PhantomData,
}
}
}
pub trait TextBuilderOutput: Sized {
type Output;
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output;
}
pub struct TextOutput;
impl TextBuilderOutput for TextOutput {
type Output = Text;
fn run(ui: &mut Ui, builder: TextBuilder<Self>) -> Self::Output {
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
let mut buf = TextBuffer::new_empty(Metrics::new(
builder.attrs.font_size,
builder.attrs.line_height,
));
let hint = builder.hint.get(ui);
let font_system = &mut ui.data.text.font_system;
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
let mut text = Text {
content: builder.content.into(),
view: TextView::new(buf, builder.attrs),
view: TextView::new(buf, builder.attrs, hint),
};
text.content.changed = false;
builder.attrs.apply(font_system, &mut text.view.buf, None);
@@ -74,13 +87,13 @@ pub struct TextEditOutput;
impl TextBuilderOutput for TextEditOutput {
type Output = TextEdit;
fn run(ui: &mut Ui, builder: TextBuilder<Self>) -> Self::Output {
fn run<H: WidgetOption>(ui: &mut Ui, builder: TextBuilder<Self, H>) -> Self::Output {
let buf = TextBuffer::new_empty(Metrics::new(
builder.attrs.font_size,
builder.attrs.line_height,
));
let mut text = TextEdit {
view: TextView::new(buf, builder.attrs),
view: TextView::new(buf, builder.attrs, builder.hint.get(ui)),
cursor: None,
};
let font_system = &mut ui.data.text.font_system;
@@ -91,7 +104,7 @@ impl TextBuilderOutput for TextEditOutput {
}
}
impl<O: TextBuilderOutput> FnOnce<(&mut Ui,)> for TextBuilder<O> {
impl<O: TextBuilderOutput, H: WidgetOption> FnOnce<(&mut Ui,)> for TextBuilder<O, H> {
type Output = O::Output;
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
@@ -103,6 +116,7 @@ pub fn text(content: impl Into<String>) -> TextBuilder {
TextBuilder {
content: content.into(),
attrs: TextAttrs::default(),
hint: (),
_pd: PhantomData,
}
}