cursor finally working properly and removed from render_text
This commit is contained in:
@@ -3,7 +3,7 @@ use std::ops::Range;
|
||||
use crate::{
|
||||
layout::{
|
||||
Active, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures, UiRegion,
|
||||
Vec2, VisualCursor, WidgetId, Widgets,
|
||||
Vec2, WidgetId, Widgets,
|
||||
},
|
||||
render::{Primitive, PrimitiveHandle, Primitives},
|
||||
util::{HashSet, Id},
|
||||
@@ -231,46 +231,53 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn write_at<P: Primitive>(&mut self, data: P, region: UiRegion) {
|
||||
self.primitives
|
||||
.push(self.ctx.primitives.write(self.id.duplicate(), data, region));
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
self.primitives.push(
|
||||
self.ctx
|
||||
.primitives
|
||||
.write(self.id.duplicate(), primitive, region),
|
||||
);
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
pub fn write<P: Primitive>(&mut self, data: P) {
|
||||
self.write_at(data, self.region)
|
||||
pub fn primitive<P: Primitive>(&mut self, primitive: P) {
|
||||
self.primitive_at(primitive, self.region)
|
||||
}
|
||||
|
||||
pub fn primitive_within<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
self.primitive_at(primitive, region.within(&self.region));
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn draw<W>(&mut self, id: &WidgetId<W>) {
|
||||
self.draw_at(id, self.region);
|
||||
pub fn widget<W>(&mut self, id: &WidgetId<W>) {
|
||||
self.widget_at(id, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one.
|
||||
/// Useful for drawing child widgets in select areas.
|
||||
pub fn draw_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.draw_at(id, region.within(&self.region));
|
||||
pub fn widget_within<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.widget_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
fn draw_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
fn widget_at<W>(&mut self, id: &WidgetId<W>, region: UiRegion) {
|
||||
self.children.push(id.id.duplicate());
|
||||
self.ctx
|
||||
.draw_inner(&id.id, region, Some(self.id.duplicate()), None);
|
||||
}
|
||||
|
||||
pub fn draw_texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
pub fn texture_within(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write_at(handle.primitive(), region.within(&self.region));
|
||||
self.primitive_at(handle.primitive(), region.within(&self.region));
|
||||
}
|
||||
|
||||
pub fn draw_texture(&mut self, handle: &TextureHandle) {
|
||||
pub fn texture(&mut self, handle: &TextureHandle) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write(handle.primitive());
|
||||
self.primitive(handle.primitive());
|
||||
}
|
||||
|
||||
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
pub fn texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
|
||||
self.textures.push(handle.clone());
|
||||
self.write_at(handle.primitive(), region);
|
||||
self.primitive_at(handle.primitive(), region);
|
||||
}
|
||||
|
||||
/// returns (handle, offset from top left)
|
||||
@@ -278,9 +285,8 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.ctx.text.draw(buffer, attrs, cursor, self.ctx.textures)
|
||||
self.ctx.text.draw(buffer, attrs, self.ctx.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
@@ -327,9 +333,8 @@ impl SizeCtx<'_> {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.text.draw(buffer, attrs, cursor, self.textures)
|
||||
self.text.draw(buffer, attrs, self.textures)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,16 +39,6 @@ impl TextAttrs {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Copy, Clone)]
|
||||
pub enum VisualCursor {
|
||||
#[default]
|
||||
None,
|
||||
Select {
|
||||
line: isize,
|
||||
col: isize,
|
||||
},
|
||||
}
|
||||
|
||||
pub type TextBuffer = Buffer;
|
||||
|
||||
impl Default for TextAttrs {
|
||||
@@ -68,7 +58,6 @@ impl TextData {
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &VisualCursor,
|
||||
textures: &mut Textures,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
let mut pixels = HashMap::new();
|
||||
@@ -76,30 +65,20 @@ impl TextData {
|
||||
let mut min_y = 0;
|
||||
let mut max_x = 0;
|
||||
let mut max_y = 0;
|
||||
let c = attrs.color;
|
||||
let cosmic_color = {
|
||||
let c = attrs.color;
|
||||
cosmic_text::Color::rgba(c.r, c.g, c.b, c.a)
|
||||
};
|
||||
let mut max_width = 0.0f32;
|
||||
let mut cursor_x = 0;
|
||||
for (run_i, run) in buffer.layout_runs().enumerate() {
|
||||
if let VisualCursor::Select { line, .. } = cursor
|
||||
&& *line == run_i as isize
|
||||
{
|
||||
cursor_x = run.line_w as i32;
|
||||
}
|
||||
for (i, glyph) in run.glyphs.iter().enumerate() {
|
||||
for run in buffer.layout_runs() {
|
||||
for glyph in run.glyphs.iter() {
|
||||
let physical_glyph = glyph.physical((0., 0.), 1.0);
|
||||
|
||||
let glyph_color = match glyph.color_opt {
|
||||
Some(some) => some,
|
||||
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|
||||
None => cosmic_color,
|
||||
};
|
||||
|
||||
if let VisualCursor::Select { col: idx, line } = cursor
|
||||
&& *line == run_i as isize
|
||||
&& *idx == i as isize
|
||||
{
|
||||
cursor_x = physical_glyph.x;
|
||||
}
|
||||
|
||||
self.swash_cache.with_pixels(
|
||||
&mut self.font_system,
|
||||
physical_glyph.cache_key,
|
||||
@@ -117,11 +96,6 @@ impl TextData {
|
||||
}
|
||||
max_width = max_width.max(run.line_w);
|
||||
}
|
||||
if let &VisualCursor::Select { line, .. } = cursor {
|
||||
let y = (attrs.line_height * (line + 1) as f32) as i32 - 1;
|
||||
max_y = max_y.max(y);
|
||||
max_x = max_x.max(cursor_x);
|
||||
}
|
||||
let width = (max_x - min_x + 1) as u32;
|
||||
let height = (max_y - min_y + 1) as u32;
|
||||
let mut image = RgbaImage::new(width, height);
|
||||
@@ -130,15 +104,6 @@ impl TextData {
|
||||
let y = (y - min_y) as u32;
|
||||
image.put_pixel(x, y, color);
|
||||
}
|
||||
if let &VisualCursor::Select { line, .. } = cursor {
|
||||
let x = (cursor_x - min_x) as u32;
|
||||
for y in 0..attrs.line_height as u32 {
|
||||
// no clue if this is good or bad for non integer values
|
||||
// depends on how the layouting is actually done
|
||||
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
|
||||
image.put_pixel(x, y, Rgba(c.as_arr()));
|
||||
}
|
||||
}
|
||||
let lines = buffer.lines.len();
|
||||
let offset = TextOffset {
|
||||
top_left: Vec2::new(min_x as f32, min_y as f32),
|
||||
|
||||
@@ -11,8 +11,8 @@ pub struct Vec2 {
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
pub const fn vec2(x: f32, y: f32) -> Vec2 {
|
||||
Vec2::new(x, y)
|
||||
pub const fn vec2(x: impl const UiNum, y: impl const UiNum) -> Vec2 {
|
||||
Vec2::new(x.to_f32(), y.to_f32())
|
||||
}
|
||||
|
||||
impl Vec2 {
|
||||
|
||||
Reference in New Issue
Block a user