1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-30 21:30:03 -04:00

Text and circle

This commit is contained in:
Emil Ernerfeldt
2019-01-05 15:28:07 +01:00
parent a1ddef742d
commit aa1c53f707
14 changed files with 534 additions and 176 deletions

View File

@@ -1,7 +1,7 @@
use crate::{layout, style, types::*};
/// Encapsulates input, layout and painting for ease of use.
#[derive(Clone, Debug, Default)]
#[derive(Clone)]
pub struct Emgui {
pub last_input: RawInput,
pub layout: layout::Layout,
@@ -9,6 +9,14 @@ pub struct Emgui {
}
impl Emgui {
pub fn new() -> Emgui {
Emgui {
last_input: Default::default(),
layout: layout::Layout::new(),
style: Default::default(),
}
}
pub fn new_frame(&mut self, new_input: RawInput) {
let gui_input = GuiInput::from_last_and_new(&self.last_input, &new_input);
self.last_input = new_input;

View File

@@ -5,19 +5,20 @@ use rusttype::{point, Scale};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct GlyphInfo {
/// X offset for nice rendering
offset_x: u16,
pub offset_x: u16,
/// Y offset for nice rendering
offset_y: u16,
pub offset_y: u16,
min_x: u16,
min_y: u16,
// Texture coordinates:
pub min_x: u16,
pub min_y: u16,
/// Inclusive.
max_x: u16,
pub max_x: u16,
/// Inclusive
max_y: u16,
pub max_y: u16,
}
/// Printable ascii characters [33, 126], which excludes 32 (space) and 127 (DEL)
@@ -26,6 +27,7 @@ const FIRST_ASCII: usize = 33;
/// Inclusive
const LAST_ASCII: usize = 126;
// TODO: break out texture atlas into separate struct, and fill it dynamically, potentially from multiple fonts.
#[derive(Clone)]
pub struct Font {
/// Maximum character height
@@ -138,8 +140,12 @@ impl Font {
(FIRST_ASCII..=LAST_ASCII).map(|c| c as u8 as char)
}
pub fn texture(&self) -> (usize, usize, &[u8]) {
(self.atlas_width, self.atlas_height, &self.atlas)
pub fn texture(&self) -> (u16, u16, &[u8]) {
(
self.atlas_width as u16,
self.atlas_height as u16,
&self.atlas,
)
}
pub fn pixel(&self, x: u16, y: u16) -> u8 {
@@ -159,6 +165,19 @@ impl Font {
}
}
/// Returns the start (X) of each character, starting at zero, plus the total width.
/// i.e. returns text.chars().count() + 1 numbers.
pub fn layout_single_line(&self, text: &str) -> Vec<f32> {
let mut x_offsets = Vec::new();
let mut x = 0.0;
for c in text.chars() {
x_offsets.push(x);
x += 7.0; // TODO: kerning
}
x_offsets.push(x);
x_offsets
}
pub fn debug_print_atlas_ascii_art(&self) {
for y in 0..self.atlas_height {
println!(

View File

@@ -1,6 +1,6 @@
use std::collections::HashSet;
use crate::{math::*, types::*};
use crate::{font::Font, math::*, types::*};
// ----------------------------------------------------------------------------
@@ -95,7 +95,10 @@ struct Memory {
// ----------------------------------------------------------------------------
struct TextFragment {
rect: Rect,
/// The start of each character, starting at zero.
x_offsets: Vec<f32>,
/// 0 for the first line, n * line_spacing for the rest
y_offset: f32,
text: String,
}
@@ -149,9 +152,10 @@ impl Layouter {
type Id = u64;
#[derive(Clone, Debug, Default)]
#[derive(Clone)]
pub struct Layout {
options: LayoutOptions,
font: Font, // TODO: Arc?
input: GuiInput,
memory: Memory,
id: Id,
@@ -161,6 +165,19 @@ pub struct Layout {
}
impl Layout {
pub fn new() -> Layout {
Layout {
options: Default::default(),
font: Font::new(13),
input: Default::default(),
memory: Default::default(),
id: Default::default(),
layouter: Default::default(),
graphics: Default::default(),
hovering_graphics: Default::default(),
}
}
pub fn input(&self) -> &GuiInput {
&self.input
}
@@ -388,6 +405,7 @@ impl Layout {
let mut popup_layout = Layout {
options: self.options,
input: self.input,
font: self.font.clone(),
memory: self.memory.clone(), // TODO: Arc
id: self.id,
layouter: Default::default(),
@@ -450,11 +468,11 @@ impl Layout {
let mut max_width = 0.0;
let mut text_fragments = Vec::new();
for line in text.split('\n') {
// TODO: break long lines
let line_width = char_size.x * (line.len() as f32);
let x_offsets = self.font.layout_single_line(&line);
let line_width = *x_offsets.last().unwrap();
text_fragments.push(TextFragment {
rect: Rect::from_min_size(vec2(0.0, cursor_y), vec2(line_width, char_size.y)),
x_offsets,
y_offset: cursor_y,
text: line.into(),
});
@@ -468,9 +486,10 @@ impl Layout {
fn add_text(&mut self, pos: Vec2, text: Vec<TextFragment>) {
for fragment in text {
self.graphics.push(GuiCmd::Text {
pos: pos + vec2(fragment.rect.pos.x, fragment.rect.center().y),
pos: pos + vec2(0.0, fragment.y_offset),
style: TextStyle::Label,
text: fragment.text,
x_offsets: fragment.x_offsets,
});
}
}

View File

@@ -92,6 +92,11 @@ pub fn lerp(min: f32, max: f32, t: f32) -> f32 {
(1.0 - t) * min + t * max
}
pub fn remap(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 {
let t = (from - from_min) / (from_max - from_min);
lerp(to_min, to_max, t)
}
pub fn remap_clamp(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max: f32) -> f32 {
let t = if from <= from_min {
0.0
@@ -102,3 +107,5 @@ pub fn remap_clamp(from: f32, from_min: f32, from_max: f32, to_min: f32, to_max:
};
lerp(to_min, to_max, t)
}
pub const TAU: f32 = 2.0 * std::f32::consts::PI;

View File

@@ -1,6 +1,7 @@
/// Outputs render info in a format suitable for e.g. OpenGL.
use crate::{
font::Font,
math::{remap, Vec2, TAU},
types::{Color, PaintCmd},
};
@@ -21,11 +22,102 @@ pub struct Vertex {
#[derive(Clone, Debug, Default)]
pub struct Frame {
pub clear_color: Option<Color>,
/// One big triangle strip
/// Draw as triangles (i.e. the length is a multiple of three)
pub indices: Vec<u32>,
pub vertices: Vec<Vertex>,
}
impl Frame {
/// Uniformly colored rectangle
pub fn add_rect(&mut self, top_left: Vertex, bottom_right: Vertex) {
let idx = self.vertices.len() as u32;
self.indices.push(idx + 0);
self.indices.push(idx + 1);
self.indices.push(idx + 2);
self.indices.push(idx + 2);
self.indices.push(idx + 1);
self.indices.push(idx + 3);
let top_right = Vertex {
x: bottom_right.x,
y: top_left.y,
u: bottom_right.u,
v: top_left.v,
color: top_left.color,
};
let botom_left = Vertex {
x: top_left.x,
y: bottom_right.y,
u: top_left.u,
v: bottom_right.v,
color: top_left.color,
};
self.vertices.push(top_left);
self.vertices.push(top_right);
self.vertices.push(botom_left);
self.vertices.push(bottom_right);
}
pub fn fill_closed_path(&mut self, points: &[Vec2], normals: &[Vec2], color: Color) {
self.vertices.extend(points.iter().map(|p| Vertex {
x: p.x,
y: p.y,
u: 0,
v: 0,
color,
}));
// TODO: use normals for anti-aliasing
assert_eq!(points.len(), normals.len());
let n = points.len() as u32;
let idx = self.vertices.len() as u32;
for i in 2..n {
self.indices.push(idx);
self.indices.push(idx + i - 1);
self.indices.push(idx + i);
}
}
pub fn draw_closed_path(
&mut self,
points: &[Vec2],
normals: &[Vec2],
width: f32,
color: Color,
) {
// TODO: anti-aliasing
assert_eq!(points.len(), normals.len());
let n = points.len() as u32;
let hw = width / 2.0;
let idx = self.vertices.len() as u32;
for i in 0..n {
self.indices.push(idx + (2 * i + 0) % (2 * n));
self.indices.push(idx + (2 * i + 1) % (2 * n));
self.indices.push(idx + (2 * i + 2) % (2 * n));
self.indices.push(idx + (2 * i + 2) % (2 * n));
self.indices.push(idx + (2 * i + 1) % (2 * n));
self.indices.push(idx + (2 * i + 3) % (2 * n));
}
for (p, n) in points.iter().zip(normals) {
self.vertices.push(Vertex {
x: p.x + hw * n.x,
y: p.y + hw * n.x,
u: 0,
v: 0,
color,
});
self.vertices.push(Vertex {
x: p.x - hw * n.x,
y: p.y - hw * n.x,
u: 0,
v: 0,
color,
});
}
}
}
#[derive(Clone)]
pub struct Painter {
font: Font,
@@ -39,48 +131,132 @@ impl Painter {
}
/// 8-bit row-major font atlas texture, (width, height, pixels).
pub fn texture(&self) -> (usize, usize, &[u8]) {
pub fn texture(&self) -> (u16, u16, &[u8]) {
self.font.texture()
}
pub fn paint(&self, commands: &[PaintCmd]) -> Frame {
// let mut path_points = Vec::new();
// let mut path_normals = Vec::new();
let mut frame = Frame::default();
for cmd in commands {
match cmd {
PaintCmd::Circle { .. } => {} // TODO
PaintCmd::Circle {
center,
fill_color,
outline,
radius,
} => {
let n = 64; // TODO: parameter
if let Some(color) = fill_color {
let idx = frame.vertices.len() as u32;
for i in 2..n {
frame.indices.push(idx);
frame.indices.push(idx + i - 1);
frame.indices.push(idx + i);
}
for i in 0..n {
let angle = remap(i as f32, 0.0, n as f32, 0.0, TAU);
frame.vertices.push(Vertex {
x: center.x + radius * angle.cos(),
y: center.y + radius * angle.sin(),
u: 0,
v: 0,
color: *color,
});
}
}
if let Some(_outline) = outline {
// TODO
}
}
PaintCmd::Clear { fill_color } => {
frame.clear_color = Some(*fill_color);
}
PaintCmd::Line { .. } => {} // TODO
PaintCmd::Rect {
fill_color,
outline,
pos,
size,
fill_color,
..
} => {
// TODO: rounded corners, colors etc.
let idx = frame.vertices.len() as u32;
frame.indices.push(idx + 0);
frame.indices.push(idx + 0);
frame.indices.push(idx + 1);
frame.indices.push(idx + 2);
frame.indices.push(idx + 3);
frame.indices.push(idx + 3);
// TODO: rounded corners
// TODO: anti-aliasing
// TODO: FilledRect and RectOutline as separate commands?
if let Some(color) = fill_color {
let vert = |pos: Vec2| Vertex {
x: pos.x,
y: pos.y,
u: 0,
v: 0,
color: *color,
};
frame.add_rect(vert(*pos), vert(*pos + *size));
}
if let Some(outline) = outline {
let vert = |x, y| Vertex {
x,
y,
u: 0,
v: 0,
color: outline.color,
};
let vert = |x, y| Vertex {
x,
y,
u: 0,
v: 0,
color: fill_color.unwrap_or(Color::WHITE),
};
// Draw this counter-clockwise from top-left corner,
// outer to inner on each step.
let hw = outline.width / 2.0;
frame.vertices.push(vert(pos.x, pos.y));
frame.vertices.push(vert(pos.x + size.x, pos.y));
frame.vertices.push(vert(pos.x, pos.y + size.y));
frame.vertices.push(vert(pos.x + size.x, pos.y + size.y));
let idx = frame.vertices.len() as u32;
for i in 0..4 {
frame.indices.push(idx + (2 * i + 0) % 8);
frame.indices.push(idx + (2 * i + 1) % 8);
frame.indices.push(idx + (2 * i + 2) % 8);
frame.indices.push(idx + (2 * i + 2) % 8);
frame.indices.push(idx + (2 * i + 1) % 8);
frame.indices.push(idx + (2 * i + 3) % 8);
}
let min = *pos;
let max = *pos + *size;
frame.vertices.push(vert(min.x - hw, min.y - hw));
frame.vertices.push(vert(min.x + hw, min.y + hw));
frame.vertices.push(vert(max.x + hw, min.y - hw));
frame.vertices.push(vert(max.x - hw, min.y + hw));
frame.vertices.push(vert(max.x + hw, max.y + hw));
frame.vertices.push(vert(max.x - hw, max.y - hw));
frame.vertices.push(vert(min.x - hw, max.y + hw));
frame.vertices.push(vert(min.x + hw, max.y - hw));
}
}
PaintCmd::Text {
color,
pos,
text,
x_offsets,
} => {
for (c, x_offset) in text.chars().zip(x_offsets.iter()) {
if let Some(glyph) = self.font.glyph_info(c) {
let top_left = Vertex {
x: pos.x + x_offset + (glyph.offset_x as f32),
y: pos.y + (glyph.offset_y as f32),
u: glyph.min_x,
v: glyph.min_y,
color: *color,
};
let bottom_right = Vertex {
x: top_left.x + (1 + glyph.max_x - glyph.min_x) as f32,
y: top_left.y + (1 + glyph.max_y - glyph.min_y) as f32,
u: glyph.max_x + 1,
v: glyph.max_y + 1,
color: *color,
};
frame.add_rect(top_left, bottom_right);
}
}
}
PaintCmd::Text { .. } => {} // TODO
}
}
frame

View File

@@ -241,18 +241,18 @@ fn translate_cmd(out_commands: &mut Vec<PaintCmd>, style: &Style, cmd: GuiCmd) {
}
GuiCmd::Text {
pos,
text,
style: text_style,
text,
x_offsets,
} => {
let fill_color = match text_style {
let color = match text_style {
TextStyle::Label => style.text_color(),
};
out_commands.push(PaintCmd::Text {
fill_color,
font_name: style.font_name.clone(),
font_size: style.font_size,
color,
pos,
text,
x_offsets,
});
}
GuiCmd::Window { rect } => {

View File

@@ -123,6 +123,8 @@ pub enum GuiCmd {
pos: Vec2,
style: TextStyle,
text: String,
/// Start each character in the text, as offset from pos.
x_offsets: Vec<f32>,
},
/// Background of e.g. a popup
Window {
@@ -162,16 +164,14 @@ pub enum PaintCmd {
pos: Vec2,
size: Vec2,
},
/// Paint a single line of mono-space text.
/// The text should start at the given position and flow to the right.
/// The text should be vertically centered at the given position.
/// Paint a single line of text
Text {
fill_color: Color,
/// Name, e.g. Palatino
font_name: String,
/// Height in pixels, e.g. 12
font_size: f32,
color: Color,
/// Top left corner of the first character.
pos: Vec2,
text: String,
/// Start each character in the text, as offset from pos.
x_offsets: Vec<f32>,
// TODO: font info
},
}