Add example

This commit is contained in:
Isaac Mills 2024-02-05 11:30:20 -05:00
parent c635390455
commit 719e904831
Signed by: fnmain
GPG key ID: B67D7410F33A0F61
3 changed files with 221 additions and 1 deletions

98
Cargo.lock generated
View file

@ -159,6 +159,54 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2faccea4cc4ab4a667ce676a30e8ec13922a692c99bb8f5b11f1502c72e04220"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "arboard" name = "arboard"
version = "3.3.0" version = "3.3.0"
@ -694,6 +742,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]] [[package]]
name = "com" name = "com"
version = "0.6.0" version = "0.6.0"
@ -936,6 +990,7 @@ dependencies = [
"objc", "objc",
"parking_lot", "parking_lot",
"percent-encoding", "percent-encoding",
"pollster",
"raw-window-handle 0.5.2", "raw-window-handle 0.5.2",
"raw-window-handle 0.6.0", "raw-window-handle 0.6.0",
"static_assertions", "static_assertions",
@ -944,6 +999,7 @@ dependencies = [
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
"web-time", "web-time",
"wgpu",
"winapi", "winapi",
"winit", "winit",
] ]
@ -967,6 +1023,7 @@ dependencies = [
"eframe", "eframe",
"egui", "egui",
"egui-wgpu", "egui-wgpu",
"env_logger",
"glyphon", "glyphon",
] ]
@ -1048,6 +1105,29 @@ dependencies = [
"syn 2.0.48", "syn 2.0.48",
] ]
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05e7cf40684ae96ade6232ed84582f40ce0a66efcd43a5117aef610534f8e0b8"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]] [[package]]
name = "epaint" name = "epaint"
version = "0.25.0" version = "0.25.0"
@ -1557,6 +1637,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "icrate" name = "icrate"
version = "0.0.4" version = "0.0.4"
@ -2173,6 +2259,12 @@ dependencies = [
"windows-sys 0.52.0", "windows-sys 0.52.0",
] ]
[[package]]
name = "pollster"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22686f4785f02a4fcc856d3b3bb19bf6c8160d103f7a99cc258bddd0251dc7f2"
[[package]] [[package]]
name = "ppv-lite86" name = "ppv-lite86"
version = "0.2.17" version = "0.2.17"
@ -2919,6 +3011,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"

View file

@ -11,4 +11,5 @@ egui-wgpu = { git = "https://github.com/emilk/egui" }
glyphon = { git = "https://github.com/StratusFearMe21/glyphon" } glyphon = { git = "https://github.com/StratusFearMe21/glyphon" }
[dev-dependencies] [dev-dependencies]
eframe = { git = "https://github.com/emilk/egui" } eframe = { git = "https://github.com/emilk/egui", features = ["wgpu"] }
env_logger = "0.11.1"

121
examples/hello-world.rs Normal file
View file

@ -0,0 +1,121 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use std::{
ops::{Deref, DerefMut},
sync::Arc,
};
use eframe::{
egui::{self, Slider},
egui_wgpu,
epaint::{
mutex::{Mutex, RwLock},
Rect, Vec2,
},
CreationContext,
};
use egui_glyphon::{
glyphon::{Attrs, Family, FontSystem, Metrics, Shaping},
BufferWithTextArea, GlyphonRenderer, GlyphonRendererCallback,
};
fn main() -> Result<(), eframe::Error> {
env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`).
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
eframe::run_native(
"My egui App",
options,
Box::new(|cc| Box::new(MyApp::new(cc))),
)
}
struct Buffer(egui_glyphon::glyphon::Buffer);
impl AsRef<egui_glyphon::glyphon::Buffer> for Buffer {
fn as_ref(&self) -> &egui_glyphon::glyphon::Buffer {
&self.0
}
}
impl Deref for Buffer {
type Target = egui_glyphon::glyphon::Buffer;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Buffer {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
struct MyApp {
font_system: Arc<Mutex<FontSystem>>,
size: f32,
buffer: Arc<RwLock<Buffer>>,
}
impl Default for MyApp {
fn default() -> Self {
let mut font_system = FontSystem::new();
let mut buffer = Buffer(egui_glyphon::glyphon::Buffer::new(
&mut font_system,
Metrics::new(30.0, 42.0),
));
buffer.set_size(&mut font_system, 16.0, 9.0);
buffer.set_text(&mut font_system, "<== Hello world! ==> 👋\nThis is rendered with 🦅 glyphon 🦁\nThe text below should be partially clipped.\na b c d e f g h i j k l m n o p q r s t u v w x y z", Attrs::new().family(Family::SansSerif), Shaping::Advanced);
buffer.shape_until_scroll(&mut font_system);
Self {
font_system: Arc::new(Mutex::new(font_system)),
buffer: Arc::new(RwLock::new(buffer)),
size: 35.0,
}
}
}
impl MyApp {
fn new(cc: &CreationContext<'_>) -> Self {
let app = Self::default();
if let Some(ref wgpu) = cc.wgpu_render_state {
GlyphonRenderer::insert(wgpu, Arc::clone(&app.font_system));
}
app
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let size = Vec2::new(16.0 * self.size, 9.0 * self.size);
{
let mut font_system = self.font_system.lock();
let mut buffer = self.buffer.write();
buffer.set_metrics(&mut font_system, Metrics::new(self.size, self.size));
buffer.set_size(&mut font_system, size.x, size.y);
buffer.shape_until_scroll(&mut font_system);
}
egui::CentralPanel::default().show(ctx, |ui| {
ui.add(Slider::new(&mut self.size, 0.1..=67.5));
let rect = Rect::from_min_size(ui.cursor().min, size);
let buffers: Vec<BufferWithTextArea<Buffer>> = vec![BufferWithTextArea::new(
Arc::clone(&self.buffer),
rect,
1.0,
egui_glyphon::glyphon::Color::rgb(255, 255, 255),
ui.ctx(),
)];
ui.painter().add(egui_wgpu::Callback::new_paint_callback(
ui.max_rect(),
GlyphonRendererCallback { buffers },
));
});
}
}