Skip to content
Snippets Groups Projects
Commit 7c4b8fc6 authored by Federico Lolli's avatar Federico Lolli
Browse files

added label to P&ID visualizer

parent 3f510f76
No related branches found
No related tags found
No related merge requests found
pub mod icons;
mod labels;
mod label;
mod value_display;
use egui::{Theme, Ui};
use enum_dispatch::enum_dispatch;
use glam::Vec2;
use icons::Icon;
use labels::Label;
use serde::{Deserialize, Serialize};
use strum_macros::{Display, EnumIter};
use icons::Icon;
use label::Label;
use value_display::ValueDisplay;
use crate::mavlink::MavMessage;
#[derive(Clone, Serialize, Deserialize, PartialEq, EnumIter, Display, Debug)]
#[enum_dispatch]
pub enum Symbol {
Icon(Icon),
ValueDisplay(ValueDisplay),
Label(Label),
}
......
use serde::{Deserialize, Serialize};
use egui::{
Align2, Color32, CornerRadius, FontId, Stroke, StrokeKind, TextEdit, Theme, Ui, Window,
};
use glam::Vec2;
use crate::{
mavlink::MavMessage,
ui::utils::{egui_to_glam, glam_to_egui},
};
use super::SymbolBehavior;
const FONT_SIZE: f32 = 2.0;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Label {
text: String,
size: Vec2,
#[serde(skip)]
is_window_visible: bool,
}
impl Default for Label {
fn default() -> Self {
Self {
text: "Label".to_string(),
size: Vec2::new(FONT_SIZE * 0.6 * 4.0, FONT_SIZE),
is_window_visible: false,
}
}
}
impl SymbolBehavior for Label {
fn update(&mut self, _message: &MavMessage, _subscribed_msg_id: u32) {}
fn reset_subscriptions(&mut self) {}
fn paint(&mut self, ui: &mut Ui, theme: Theme, pos: Vec2, size: f32, _: f32) {
let painter = ui.painter();
let color = match theme {
Theme::Light => Color32::BLACK,
Theme::Dark => Color32::WHITE,
};
let text_rect = painter.text(
glam_to_egui(pos).to_pos2(),
Align2::LEFT_TOP,
&self.text,
FontId::monospace(FONT_SIZE * size),
color,
);
self.size = egui_to_glam(text_rect.size()) / size;
painter.rect(
egui::Rect::from_min_size(
glam_to_egui(pos).to_pos2(),
glam_to_egui(self.size()) * size,
),
CornerRadius::ZERO,
Color32::TRANSPARENT,
Stroke::NONE,
StrokeKind::Middle,
);
}
fn subscriptions_ui(&mut self, ui: &mut Ui, _mavlink_id: u32) {
Window::new("Label Customization")
.id(ui.auto_id_with("customization_settings"))
.auto_sized()
.collapsible(true)
.movable(true)
.open(&mut self.is_window_visible)
.show(ui.ctx(), |ui| subscription_window(ui, &mut self.text));
}
fn context_menu(&mut self, ui: &mut Ui) {
if ui.button("Customize text…").clicked() {
self.is_window_visible = true;
ui.close_menu();
}
}
fn anchor_points(&self) -> Option<Vec<Vec2>> {
None
}
fn size(&self) -> Vec2 {
self.size
}
}
fn subscription_window(ui: &mut Ui, text: &mut String) {
TextEdit::singleline(text)
.hint_text("Label text")
.desired_width(200.0)
.show(ui);
}
......@@ -20,7 +20,7 @@ use super::SymbolBehavior;
const FONT_SIZE: f32 = 2.0;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Label {
pub struct ValueDisplay {
subscribed_field: Option<IndexedField>,
size: Vec2,
......@@ -30,7 +30,7 @@ pub struct Label {
is_subs_window_visible: bool,
}
impl Default for Label {
impl Default for ValueDisplay {
fn default() -> Self {
Self {
subscribed_field: None,
......@@ -41,7 +41,7 @@ impl Default for Label {
}
}
impl SymbolBehavior for Label {
impl SymbolBehavior for ValueDisplay {
fn update(&mut self, message: &MavMessage, subscribed_msg_id: u32) {
if let Some(subscribed_field) = &self.subscribed_field {
if message.message_id() == subscribed_msg_id {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment