add example
This commit is contained in:
parent
d0307417e1
commit
099e41bf3f
5 changed files with 114 additions and 0 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
@ -4983,6 +4983,15 @@ dependencies = [
|
|||
"linked-hash-map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "z_order"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"eframe",
|
||||
"log",
|
||||
"tracing-subscriber",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "zbus"
|
||||
version = "3.6.0"
|
||||
|
|
16
examples/z_order/Cargo.toml
Normal file
16
examples/z_order/Cargo.toml
Normal file
|
@ -0,0 +1,16 @@
|
|||
[package]
|
||||
name = "z_order"
|
||||
version = "0.1.0"
|
||||
authors = ["Max Stoumen <max@mxs.dev>"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
edition = "2021"
|
||||
rust-version = "1.65"
|
||||
publish = false
|
||||
|
||||
|
||||
[dependencies]
|
||||
eframe = { path = "../../crates/eframe", features = [
|
||||
"__screenshot", # __screenshot is so we can dump a screenshot using EFRAME_SCREENSHOT_TO
|
||||
] }
|
||||
log = "0.4.17"
|
||||
tracing-subscriber = "0.3"
|
7
examples/z_order/README.md
Normal file
7
examples/z_order/README.md
Normal file
|
@ -0,0 +1,7 @@
|
|||
Example for how to set Z-order for drawing and interaction.
|
||||
|
||||
```sh
|
||||
cargo run -p z_order
|
||||
```
|
||||
|
||||

|
BIN
examples/z_order/screenshot.png
Normal file
BIN
examples/z_order/screenshot.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 14 KiB |
82
examples/z_order/src/main.rs
Normal file
82
examples/z_order/src/main.rs
Normal file
|
@ -0,0 +1,82 @@
|
|||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
|
||||
|
||||
use eframe::{
|
||||
egui::{self, layers::ZOrder, Sense, Ui},
|
||||
epaint::{Color32, Rect, Rounding, Vec2},
|
||||
};
|
||||
|
||||
fn main() -> Result<(), eframe::Error> {
|
||||
// Log to stdout (if you run with `RUST_LOG=debug`).
|
||||
tracing_subscriber::fmt::init();
|
||||
|
||||
let options = eframe::NativeOptions {
|
||||
initial_window_size: Some(egui::vec2(220.0, 150.0)),
|
||||
..Default::default()
|
||||
};
|
||||
eframe::run_native(
|
||||
"Z Order Test",
|
||||
options,
|
||||
Box::new(|_cc| Box::new(MyApp::default())),
|
||||
)
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct MyApp {}
|
||||
|
||||
impl eframe::App for MyApp {
|
||||
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||
egui::CentralPanel::default().show(ctx, |ui| {
|
||||
ui.label("The left example should show blue over green. The right should show green over blue.");
|
||||
|
||||
egui::Frame::default()
|
||||
.fill(ui.style().visuals.window_fill)
|
||||
.inner_margin(10.0)
|
||||
.show(ui, |ui| {
|
||||
const SIZE: Vec2 = Vec2::new(50.0, 50.0);
|
||||
const DELTA: Vec2 = Vec2::new(20.0, 20.0);
|
||||
|
||||
fn draw_squares(
|
||||
ui: &mut Ui,
|
||||
shift: Vec2,
|
||||
order1: ZOrder,
|
||||
order2: ZOrder,
|
||||
) {
|
||||
let pos = ui.max_rect().left_top();
|
||||
|
||||
ui.with_z(order1, |ui| {
|
||||
let response = ui.allocate_rect(
|
||||
Rect::from_min_size(pos + shift, SIZE),
|
||||
Sense::click(),
|
||||
);
|
||||
|
||||
let painter = ui.painter_at(response.rect);
|
||||
painter.rect_filled(response.rect, Rounding::none(), Color32::GREEN);
|
||||
|
||||
if response.clicked() {
|
||||
log::info!("Clicked green");
|
||||
}
|
||||
});
|
||||
|
||||
ui.with_z(order2, |ui| {
|
||||
let response = ui.allocate_rect(
|
||||
Rect::from_min_size(pos + DELTA + shift, SIZE),
|
||||
Sense::click(),
|
||||
);
|
||||
let painter = ui.painter_at(response.rect);
|
||||
painter.rect_filled(response.rect, Rounding::none(), Color32::BLUE);
|
||||
|
||||
if response.clicked() {
|
||||
log::info!("Clicked blue");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
let base = ZOrder::base();
|
||||
let above = base.above();
|
||||
|
||||
draw_squares(ui, Vec2::new(0.0, 0.0), base, above);
|
||||
draw_squares(ui, Vec2::new(100.0, 0.0), above, base);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue