crop text images that are too big
This commit is contained in:
@@ -19,7 +19,7 @@ pub struct TextView {
|
||||
pub attrs: MutDetect<TextAttrs>,
|
||||
pub buf: MutDetect<TextBuffer>,
|
||||
// cache
|
||||
tex: Option<TextTexture>,
|
||||
tex: Option<RenderedText>,
|
||||
width: Option<f32>,
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ impl TextView {
|
||||
width: None,
|
||||
}
|
||||
}
|
||||
pub fn draw(&mut self, ctx: &mut SizeCtx) -> TextTexture {
|
||||
pub fn render(&mut self, ctx: &mut SizeCtx) -> RenderedText {
|
||||
let width = if self.attrs.wrap {
|
||||
Some(ctx.px_size().x)
|
||||
} else {
|
||||
@@ -55,9 +55,25 @@ impl TextView {
|
||||
self.buf.changed = false;
|
||||
tex
|
||||
}
|
||||
pub fn tex(&self) -> Option<&TextTexture> {
|
||||
pub fn tex(&self) -> Option<&RenderedText> {
|
||||
self.tex.as_ref()
|
||||
}
|
||||
pub fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.render(ctx).size.x)
|
||||
}
|
||||
pub fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.render(ctx).size.y)
|
||||
}
|
||||
pub fn draw(&mut self, painter: &mut Painter) -> UiRegion {
|
||||
let tex = self.render(&mut painter.size_ctx());
|
||||
let region = tex.size.align(self.align);
|
||||
let dims = tex.handle.size();
|
||||
let mut region = region.offset(tex.top_left_offset);
|
||||
region.x.end = region.x.start + UiScalar::abs(dims.x);
|
||||
region.y.end = region.y.start + UiScalar::abs(dims.y);
|
||||
painter.texture_within(&tex.handle, region);
|
||||
region
|
||||
}
|
||||
}
|
||||
|
||||
impl Text {
|
||||
@@ -69,7 +85,7 @@ impl Text {
|
||||
view: TextView::new(buf, attrs),
|
||||
}
|
||||
}
|
||||
fn update_buf(&mut self, ctx: &mut SizeCtx) -> TextTexture {
|
||||
fn update_buf(&mut self, ctx: &mut SizeCtx) {
|
||||
if self.content.changed {
|
||||
self.content.changed = false;
|
||||
self.view.buf.set_text(
|
||||
@@ -80,36 +96,26 @@ impl Text {
|
||||
None,
|
||||
);
|
||||
}
|
||||
self.view.draw(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for Text {
|
||||
fn draw(&mut self, painter: &mut Painter) {
|
||||
let tex = self.update_buf(&mut painter.size_ctx());
|
||||
let region = text_region(&tex, self.align);
|
||||
painter.texture_within(&tex.handle, region);
|
||||
self.update_buf(&mut painter.size_ctx());
|
||||
self.view.draw(painter);
|
||||
}
|
||||
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.update_buf(ctx).size.x)
|
||||
self.update_buf(ctx);
|
||||
self.view.desired_width(ctx)
|
||||
}
|
||||
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
Len::abs(self.update_buf(ctx).size.y)
|
||||
self.update_buf(ctx);
|
||||
self.view.desired_height(ctx)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn text_region(tex: &TextTexture, align: RegionAlign) -> UiRegion {
|
||||
let tex_dims = tex.handle.size();
|
||||
let mut region = tex.size.align(align);
|
||||
region.x.start.abs += tex.top_left_offset.x;
|
||||
region.y.start.abs += tex.top_left_offset.y;
|
||||
region.x.end.abs = region.x.start.abs + tex_dims.x;
|
||||
region.y.end.abs = region.y.start.abs + tex_dims.y;
|
||||
region
|
||||
}
|
||||
|
||||
impl Deref for Text {
|
||||
type Target = TextAttrs;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user