Rust egui(3) 增加多个tab

话说不知道咋写,要不直接上git patch吧。代码都是移植的官方demo,核心改动就是把原来的line_demo换成了plot_demo,里面实现多个ui,然后点击tab标题会切换不同的ui。
如下图,Lines和Markers两个不同的标签对应不同的ui。
参考 https://crazyskady.github.io/index.html
在这里插入图片描述
直接上git diff吧。

Subject: [PATCH] A demo for adding a new ui in a panel

---
 eframe_test/src/app.rs | 140 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 135 insertions(+), 5 deletions(-)

diff --git a/eframe_test/src/app.rs b/eframe_test/src/app.rs
index a2a1351..57d8107 100644
--- a/eframe_test/src/app.rs
+++ b/eframe_test/src/app.rs
@@ -4,16 +4,71 @@ use egui::*;
 
 use egui_plot::{
     CoordinatesFormatter, Corner, Legend, Line, LineStyle, 
-    Plot, PlotPoints,
+    Plot, PlotPoints, MarkerShape, Points, // 增加所需的库
 };
 //定义一个Panel类型,用于包裹不同类型的ui
+#[derive(PartialEq, Eq)]
+enum Panel {
+    Lines,
+    Markers,
+}
+
+impl Default for Panel {
+    fn default() -> Self {
+        Self::Lines
+    }
+}
+
// 用PlotDemo替换原来的LineDemo,其中open_panel用于标记当前切换到了哪个ui
+#[derive(PartialEq, Default)]
+pub struct PlotDemo {
+    line_demo: LineDemo,
+    marker_demo: MarkerDemo,
+    open_panel: Panel,
+}
+
+impl PlotDemo {
+    fn ui(&mut self, ui: &mut Ui) {
+        ui.horizontal(|ui| {
+            egui::reset_button(ui, self);
+            ui.collapsing("Instructions", |ui| {
+                ui.label("Pan by dragging, or scroll (+ shift = horizontal).");
+                ui.label("Box zooming: Right click to zoom in and zoom out using a selection.");
+                if cfg!(target_arch = "wasm32") {
+                    ui.label("Zoom with ctrl / ⌘ + pointer wheel, or with pinch gesture.");
+                } else if cfg!(target_os = "macos") {
+                    ui.label("Zoom with ctrl / ⌘ + scroll.");
+                } else {
+                    ui.label("Zoom with ctrl + scroll.");
+                }
+                ui.label("Reset view with double-click.");
+                //ui.add(crate::egui_github_link_file!());
+            });
+        });
+        ui.separator();
// 这里用selectable_value来设定两个Panel,绑定到self.open_panel上用于判断当前应该使用哪个ui来绘图
+        ui.horizontal(|ui| {
+            ui.selectable_value(&mut self.open_panel, Panel::Lines, "Lines");
+            ui.selectable_value(&mut self.open_panel, Panel::Markers, "Markers");
+        });
+        ui.separator();
+
// 根据open_panel来进行ui的绘制
+        match self.open_panel {
+            Panel::Lines => {
+                self.line_demo.ui(ui);
+            }
+            Panel::Markers => {
+                self.marker_demo.ui(ui);
+            }
+        }
+    }
+}
+
 #[derive(serde::Deserialize, serde::Serialize)]
 #[serde(default)] // if we add new fields, give them default values when deserializing old state
 pub struct TemplateApp {
     // Example stuff:
     label: String,
     #[serde(skip)]
// 替换掉原来单一的line_demo,替换为包含多个demo的plotdemo
-    line_demo: LineDemo,
+    plot_demo: PlotDemo,
     #[serde(skip)] // This how you opt-out of serialization of a field
     value: f32,
 }
@@ -23,7 +78,7 @@ impl Default for TemplateApp {
         Self {
             // Example stuff:
             label: "Hello World!Ruster.".to_owned(),
-            line_demo: LineDemo::default(),
+            plot_demo: PlotDemo::default(),
             value: 2.7,
         }
     }
@@ -75,7 +130,7 @@ impl eframe::App for TemplateApp {
             });
         });
         egui::CentralPanel::default().show(ctx, |ui| {
-            self.line_demo.ui(ui);
+            self.plot_demo.ui(ui);
         });
 /*
         egui::CentralPanel::default().show(ctx, |ui| {
@@ -294,4 +349,79 @@ impl LineDemo {
         })
         .response
     }
-}
\ No newline at end of file
+}
+
// 实现MarkerDemo
// 具体MarkerDemo的实现不做注释。。。。。
+#[derive(PartialEq)]
+struct MarkerDemo {
+    fill_markers: bool,
+    marker_radius: f32,
+    automatic_colors: bool,
+    marker_color: Color32,
+}
+
+impl Default for MarkerDemo {
+    fn default() -> Self {
+        Self {
+            fill_markers: true,
+            marker_radius: 5.0,
+            automatic_colors: true,
+            marker_color: Color32::GREEN,
+        }
+    }
+}
+
+impl MarkerDemo {
+    fn markers(&self) -> Vec<Points> {
+        MarkerShape::all()
+            .enumerate()
+            .map(|(i, marker)| {
+                let y_offset = i as f64 * 0.5 + 1.0;
+                let mut points = Points::new(vec![
+                    [1.0, 0.0 + y_offset],
+                    [2.0, 0.5 + y_offset],
+                    [3.0, 0.0 + y_offset],
+                    [4.0, 0.5 + y_offset],
+                    [5.0, 0.0 + y_offset],
+                    [6.0, 0.5 + y_offset],
+                ])
+                .name(format!("{marker:?}"))
+                .filled(self.fill_markers)
+                .radius(self.marker_radius)
+                .shape(marker);
+
+                if !self.automatic_colors {
+                    points = points.color(self.marker_color);
+                }
+
+                points
+            })
+            .collect()
+    }
+
+    fn ui(&mut self, ui: &mut Ui) -> Response {
+        ui.horizontal(|ui| {
+            ui.checkbox(&mut self.fill_markers, "Fill");
+            ui.add(
+                egui::DragValue::new(&mut self.marker_radius)
+                    .speed(0.1)
+                    .clamp_range(0.0..=f64::INFINITY)
+                    .prefix("Radius: "),
+            );
+            ui.checkbox(&mut self.automatic_colors, "Automatic colors");
+            if !self.automatic_colors {
+                ui.color_edit_button_srgba(&mut self.marker_color);
+            }
+        });
+
+        let markers_plot = Plot::new("markers_demo")
+            .data_aspect(1.0)
+            .legend(Legend::default());
+        markers_plot
+            .show(ui, |plot_ui| {
+                for marker in self.markers() {
+                    plot_ui.points(marker);
+                }
+            })
+            .response
+    }
+}
-- 

ffmpeg工具转的mov到gif看起来也还可以呀。。。像素懒得调了
请添加图片描述

相关推荐

  1. MP3音频合成

    2024-03-25 06:46:03       34 阅读
  2. eventbus增加缓存池

    2024-03-25 06:46:03       30 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-25 06:46:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-25 06:46:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-25 06:46:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-25 06:46:03       20 阅读

热门阅读

  1. Android 10.0 mt8788关于摄像头方向旋转功能实现

    2024-03-25 06:46:03       17 阅读
  2. haproxy和keepalived的区别与联系

    2024-03-25 06:46:03       19 阅读
  3. 自定义android音频焦点

    2024-03-25 06:46:03       19 阅读
  4. 关于C/C++,Linux/MacOS/Windows 平台虚拟内存分配

    2024-03-25 06:46:03       17 阅读
  5. C语言学习笔记day14

    2024-03-25 06:46:03       14 阅读
  6. linux命令(CentOS7)yum provides

    2024-03-25 06:46:03       19 阅读
  7. 网络基础:构建你的数字世界之桥

    2024-03-25 06:46:03       22 阅读
  8. 面试宝典:MySQL索引进阶深度分析

    2024-03-25 06:46:03       20 阅读
  9. Android下的Touch事件分发详解

    2024-03-25 06:46:03       18 阅读
  10. 【Android 内存优化】Koom核心内存指标分析

    2024-03-25 06:46:03       26 阅读
  11. 【兆易创新GD32H759I-EVAL开发板】USB设备 介绍1

    2024-03-25 06:46:03       18 阅读