hint gaming
This commit is contained in:
@@ -1,20 +1,15 @@
|
|||||||
use std::marker::{PhantomData, Sized};
|
use std::marker::{PhantomData, Sized};
|
||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use cosmic_text::{Attrs, Family, Metrics};
|
use cosmic_text::{Attrs, Family, Metrics};
|
||||||
|
|
||||||
pub trait TextBuilderOutput: Sized {
|
pub struct TextBuilder<O = TextOutput, H: WidgetOption = ()> {
|
||||||
type Output;
|
|
||||||
fn run(ui: &mut Ui, builder: TextBuilder<Self>) -> Self::Output;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TextBuilder<O = TextOutput> {
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub attrs: TextAttrs,
|
pub attrs: TextAttrs,
|
||||||
|
pub hint: H,
|
||||||
_pd: PhantomData<O>,
|
_pd: PhantomData<O>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<O> TextBuilder<O> {
|
impl<O, H: WidgetOption> TextBuilder<O, H> {
|
||||||
pub fn size(mut self, size: impl UiNum) -> Self {
|
pub fn size(mut self, size: impl UiNum) -> Self {
|
||||||
self.attrs.font_size = size.to_f32();
|
self.attrs.font_size = size.to_f32();
|
||||||
self.attrs.line_height = self.attrs.font_size * 1.1;
|
self.attrs.line_height = self.attrs.font_size * 1.1;
|
||||||
@@ -40,29 +35,47 @@ impl<O> TextBuilder<O> {
|
|||||||
self.attrs.wrap = wrap;
|
self.attrs.wrap = wrap;
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
pub fn editable(self) -> TextBuilder<TextEditOutput> {
|
pub fn editable(self) -> TextBuilder<TextEditOutput, H> {
|
||||||
TextBuilder {
|
TextBuilder {
|
||||||
content: self.content,
|
content: self.content,
|
||||||
attrs: self.attrs,
|
attrs: self.attrs,
|
||||||
|
hint: self.hint,
|
||||||
_pd: PhantomData,
|
_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;
|
pub struct TextOutput;
|
||||||
impl TextBuilderOutput for TextOutput {
|
impl TextBuilderOutput for TextOutput {
|
||||||
type Output = Text;
|
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(
|
let mut buf = TextBuffer::new_empty(Metrics::new(
|
||||||
builder.attrs.font_size,
|
builder.attrs.font_size,
|
||||||
builder.attrs.line_height,
|
builder.attrs.line_height,
|
||||||
));
|
));
|
||||||
|
let hint = builder.hint.get(ui);
|
||||||
let font_system = &mut ui.data.text.font_system;
|
let font_system = &mut ui.data.text.font_system;
|
||||||
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
buf.set_text(font_system, &builder.content, &Attrs::new(), SHAPING, None);
|
||||||
let mut text = Text {
|
let mut text = Text {
|
||||||
content: builder.content.into(),
|
content: builder.content.into(),
|
||||||
view: TextView::new(buf, builder.attrs),
|
view: TextView::new(buf, builder.attrs, hint),
|
||||||
};
|
};
|
||||||
text.content.changed = false;
|
text.content.changed = false;
|
||||||
builder.attrs.apply(font_system, &mut text.view.buf, None);
|
builder.attrs.apply(font_system, &mut text.view.buf, None);
|
||||||
@@ -74,13 +87,13 @@ pub struct TextEditOutput;
|
|||||||
impl TextBuilderOutput for TextEditOutput {
|
impl TextBuilderOutput for TextEditOutput {
|
||||||
type Output = TextEdit;
|
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(
|
let buf = TextBuffer::new_empty(Metrics::new(
|
||||||
builder.attrs.font_size,
|
builder.attrs.font_size,
|
||||||
builder.attrs.line_height,
|
builder.attrs.line_height,
|
||||||
));
|
));
|
||||||
let mut text = TextEdit {
|
let mut text = TextEdit {
|
||||||
view: TextView::new(buf, builder.attrs),
|
view: TextView::new(buf, builder.attrs, builder.hint.get(ui)),
|
||||||
cursor: None,
|
cursor: None,
|
||||||
};
|
};
|
||||||
let font_system = &mut ui.data.text.font_system;
|
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;
|
type Output = O::Output;
|
||||||
|
|
||||||
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::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 {
|
TextBuilder {
|
||||||
content: content.into(),
|
content: content.into(),
|
||||||
attrs: TextAttrs::default(),
|
attrs: TextAttrs::default(),
|
||||||
|
hint: (),
|
||||||
_pd: PhantomData,
|
_pd: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,11 +47,11 @@ impl Widget for TextEdit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||||
Len::abs(self.view.render(ctx).size.x)
|
self.view.desired_width(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||||
Len::abs(self.view.render(ctx).size.y)
|
self.view.desired_height(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,18 +21,20 @@ pub struct TextView {
|
|||||||
// cache
|
// cache
|
||||||
tex: Option<RenderedText>,
|
tex: Option<RenderedText>,
|
||||||
width: Option<f32>,
|
width: Option<f32>,
|
||||||
|
pub hint: Option<WidgetId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextView {
|
impl TextView {
|
||||||
pub fn new(buf: TextBuffer, attrs: TextAttrs) -> Self {
|
pub fn new(buf: TextBuffer, attrs: TextAttrs, hint: Option<WidgetId>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
attrs: attrs.into(),
|
attrs: attrs.into(),
|
||||||
buf: buf.into(),
|
buf: buf.into(),
|
||||||
tex: None,
|
tex: None,
|
||||||
width: None,
|
width: None,
|
||||||
|
hint,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
|
fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
|
||||||
let width = if self.attrs.wrap {
|
let width = if self.attrs.wrap {
|
||||||
Some(ctx.px_size().x)
|
Some(ctx.px_size().x)
|
||||||
} else {
|
} else {
|
||||||
@@ -71,6 +73,12 @@ impl TextView {
|
|||||||
let mut region = region.offset(tex.top_left_offset);
|
let mut region = region.offset(tex.top_left_offset);
|
||||||
region.x.end = region.x.start + UiScalar::abs(dims.x);
|
region.x.end = region.x.start + UiScalar::abs(dims.x);
|
||||||
region.y.end = region.y.start + UiScalar::abs(dims.y);
|
region.y.end = region.y.start + UiScalar::abs(dims.y);
|
||||||
|
if let Some(hint) = &self.hint
|
||||||
|
&& let [line] = &self.buf.lines[..]
|
||||||
|
&& line.text().is_empty()
|
||||||
|
{
|
||||||
|
painter.widget(hint);
|
||||||
|
}
|
||||||
painter.texture_within(&tex.handle, region);
|
painter.texture_within(&tex.handle, region);
|
||||||
region
|
region
|
||||||
}
|
}
|
||||||
@@ -82,7 +90,7 @@ impl Text {
|
|||||||
let buf = TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height));
|
let buf = TextBuffer::new_empty(Metrics::new(attrs.font_size, attrs.line_height));
|
||||||
Self {
|
Self {
|
||||||
content: content.into().into(),
|
content: content.into().into(),
|
||||||
view: TextView::new(buf, attrs),
|
view: TextView::new(buf, attrs, None),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn update_buf(&mut self, ctx: &mut SizeCtx) {
|
fn update_buf(&mut self, ctx: &mut SizeCtx) {
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ pub struct Color<T: ColorNum> {
|
|||||||
impl<T: ColorNum> Color<T> {
|
impl<T: ColorNum> Color<T> {
|
||||||
pub const BLACK: Self = Self::rgb(T::MIN, T::MIN, T::MIN);
|
pub const BLACK: Self = Self::rgb(T::MIN, T::MIN, T::MIN);
|
||||||
pub const WHITE: Self = Self::rgb(T::MAX, T::MAX, T::MAX);
|
pub const WHITE: Self = Self::rgb(T::MAX, T::MAX, T::MAX);
|
||||||
|
pub const GRAY: Self = Self::rgb(T::MID, T::MID, T::MID);
|
||||||
|
|
||||||
pub const RED: Self = Self::rgb(T::MAX, T::MIN, T::MIN);
|
pub const RED: Self = Self::rgb(T::MAX, T::MIN, T::MIN);
|
||||||
pub const ORANGE: Self = Self::rgb(T::MAX, T::MID, T::MIN);
|
pub const ORANGE: Self = Self::rgb(T::MAX, T::MID, T::MIN);
|
||||||
|
|||||||
@@ -135,3 +135,19 @@ impl_widget_arr!(9;A B C D E F G H I);
|
|||||||
impl_widget_arr!(10;A B C D E F G H I J);
|
impl_widget_arr!(10;A B C D E F G H I J);
|
||||||
impl_widget_arr!(11;A B C D E F G H I J K);
|
impl_widget_arr!(11;A B C D E F G H I J K);
|
||||||
impl_widget_arr!(12;A B C D E F G H I J K L);
|
impl_widget_arr!(12;A B C D E F G H I J K L);
|
||||||
|
|
||||||
|
pub trait WidgetOption {
|
||||||
|
fn get(self, ui: &mut Ui) -> Option<WidgetId>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl WidgetOption for () {
|
||||||
|
fn get(self, _: &mut Ui) -> Option<WidgetId> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: FnOnce(&mut Ui) -> Option<WidgetId>> WidgetOption for F {
|
||||||
|
fn get(self, ui: &mut Ui) -> Option<WidgetId> {
|
||||||
|
self(ui)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user