- rust新手,egui没啥找到啥教程,这里自己记录下学习过程
- 环境:windows11 22H2
- rust版本:rustc 1.71.1
- egui版本:0.22.0
- eframe版本:0.22.0
- 上一篇:这里
pub struct Painter {
/// Source of fonts and destination of shapes
ctx: Context,
/// Where we paint
layer_id: LayerId,
/// Everything painted in this [`Painter`] will be clipped against this.
/// This means nothing outside of this rectangle will be visible on screen.
clip_rect: Rect,
/// If set, all shapes will have their colors modified to be closer to this.
/// This is used to implement grayed out interfaces.
fade_to_color: Option<Color32>,
}
egui::CentralPanel::default().show(ctx, |ui| {
// 从ui中指定一块区域
let response =
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
// 添加一个空Shape
let shape = ui.painter().add(egui::Shape::Noop);
// 定义圆角大小
let rounding = egui::Rounding::same(4.0);
// 定义要绘制的矩形大小及位置
let body_rect = egui::Rect::from_min_size(egui::pos2(10.0, 100.0), egui::vec2(100.0, 200.0));
// 添加一个矩形
let body = egui::Shape::Rect(egui::epaint::RectShape {
rect: body_rect,
rounding: rounding,
fill: color_from_hex("#3f3f3f").unwrap(),
stroke: egui::Stroke::NONE,
});
// 绘制
ui.painter().set(shape, body);
});


CentralPanel中绘制,需要进行转换:egui::CentralPanel::default().show(ctx, |ui| {
let response = ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
// 定义转换
let to_screen = egui::emath::RectTransform::from_to(
egui::Rect::from_min_size(egui::Pos2::ZERO, response.rect.size()),
response.rect,
);
let shape = ui.painter().add(egui::Shape::Noop);
let rounding_radius = 4.0;
let rounding = egui::Rounding::same(rounding_radius);
let mut body_rect =
egui::Rect::from_min_size(egui::pos2(10.0, 100.0), egui::vec2(100.0, 200.0));
// 进行转换
body_rect = to_screen.transform_rect(body_rect);
let body = egui::Shape::Rect(egui::epaint::RectShape {
rect: body_rect,
rounding: rounding,
fill: color_from_hex("#3f3f3f").unwrap(),
stroke: egui::Stroke::NONE,
});
ui.painter().set(shape, body);
});


egui::CentralPanel::default().show(ctx, |ui| {
let response = ui.allocate_rect(ui.max_rect(), egui::Sense::hover());
let to_screen = egui::emath::RectTransform::from_to(
egui::Rect::from_min_size(egui::Pos2::ZERO, response.rect.size()),
response.rect,
);
let shape = ui.painter().add(egui::Shape::Noop);
let rounding_radius = 4.0;
let rounding = egui::Rounding::same(rounding_radius);
let mut body_rect = egui::Rect::from_min_size(
egui::Pos2 { x: 10.0, y: 100.0 },
egui::vec2(100.0, 200.0),
);
body_rect = to_screen.transform_rect(body_rect);
// 添加拖拽事件
let window_response = ui.interact(
body_rect,
egui::Id::new((1, "window")),
egui::Sense::click_and_drag(),
);
// 计算拖拽偏移量
let drag_delta = window_response.drag_delta();
if drag_delta.length_sq() > 0.0 {
// 移动矩形
body_rect = body_rect.translate(drag_delta);
}
let body = egui::Shape::Rect(egui::epaint::RectShape {
rect: body_rect,
rounding: rounding,
fill: color_from_hex("#3f3f3f").unwrap(),
stroke: egui::Stroke::NONE,
});
ui.painter().set(shape, body);
});

let mut body_rect = egui::Rect::from_min_size(
egui::Pos2 { x: 10.0, y: 100.0 },
egui::vec2(100.0, 200.0),
);
pub struct TemplateApp {
// ..
// pos
#[serde(skip)]
rect_pos: egui::Pos2,
}
egui::CentralPanel::default().show(ctx, |ui| {
let response = ui.allocate_rect(ui.max_rect(), egui::Sense::hover());
let to_screen = egui::emath::RectTransform::from_to(
egui::Rect::from_min_size(egui::Pos2::ZERO, response.rect.size()),
response.rect,
);
let shape = ui.painter().add(egui::Shape::Noop);
let rounding_radius = 4.0;
let rounding = egui::Rounding::same(rounding_radius);
let mut body_rect = egui::Rect::from_min_size(
self.rect_pos,
egui::vec2(100.0, 200.0),
);
body_rect = to_screen.transform_rect(body_rect);
// 添加拖拽事件
let window_response = ui.interact(
body_rect,
egui::Id::new((1, "window")),
egui::Sense::click_and_drag(),
);
// 计算拖拽偏移量
let drag_delta = window_response.drag_delta();
if drag_delta.length_sq() > 0.0 {
// 移动矩形
body_rect = body_rect.translate(drag_delta);
// 改变初始位置
self.rect_pos = self.rect_pos + drag_delta;
}
let body = egui::Shape::Rect(egui::epaint::RectShape {
rect: body_rect,
rounding: rounding,
fill: color_from_hex("#3f3f3f").unwrap(),
stroke: egui::Stroke::NONE,
});
ui.painter().set(shape, body);
});
