Skip to content

Commit

Permalink
added ability to select connection type, remove connect, and hints fo…
Browse files Browse the repository at this point in the history
…r in-progress connections
  • Loading branch information
shulltronics committed Jun 18, 2024
1 parent f243821 commit 41a9a81
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl IronCoderApp {
pub fn display_project_editor(&mut self, ctx: &egui::Context) {
// first render the top panel with project name, buttons, etc.
egui::TopBottomPanel::top("project_editor_top_panel").show(ctx, |ui| {
if let Some(mode) = self.project.display_system_editor_hud(ctx, ui, &mut self.warning_flags) {
if let Some(mode) = self.project.display_system_editor_top_bar(ctx, ui, &mut self.warning_flags) {
self.mode = mode;
}
});
Expand All @@ -353,6 +353,15 @@ impl IronCoderApp {
});
// Display the board editor
self.project.display_system_editor_boards(ctx, ui);
// Display help text for in-progress connections
if let Some(true) = ctx.data(|data| {
data.get_temp::<bool>(egui::Id::new("connection_in_progress"))
}) {
ui.with_layout(egui::Layout::bottom_up(egui::Align::Min), |ui| {
ui.label("Click the pins to form your connection... or use ESC to cancel.");
});
}
// Display a context menu on right-click.
}).response.context_menu(|ui| {
let id = egui::Id::new("show_known_boards");
let mut should_show_boards_window = ctx.data_mut(|data| {
Expand Down
5 changes: 1 addition & 4 deletions src/board/pinout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl fmt::Display for InterfaceDirection {
}
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, Sequence)]
#[derive(Serialize, Deserialize, Copy, Clone, Debug, PartialEq, Eq, Sequence)]
#[non_exhaustive]
/// The various types of electrical interfaces we use with dev boards
pub enum InterfaceType {
Expand All @@ -32,9 +32,6 @@ pub enum InterfaceType {
UART,
I2C,
SPI,
PIO,
I2S,
USB,
}

impl fmt::Display for InterfaceType {
Expand Down
48 changes: 42 additions & 6 deletions src/project/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ use egui::widgets::Button;

use git2::{Repository, StatusOptions};

use crate::board::Board;
use crate::{project::Project, board};
use crate::board;
use crate::project::Project;
use crate::app::icons::IconSet;
use crate::app::{Mode, Warnings, Git};

use enum_iterator;

use serde::{Serialize, Deserialize};

use super::system;

#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub enum ProjectViewType {
#[default]
Expand Down Expand Up @@ -381,7 +384,7 @@ impl Project {
/// Show the boards in egui "Area"s so we can move them around!
pub fn display_system_editor_boards(&mut self, ctx: &egui::Context, ui: &mut egui::Ui) {

let mut pin_locations: HashMap<(Board, String), egui::Pos2> = HashMap::new();
let mut pin_locations: HashMap<(board::Board, String), egui::Pos2> = HashMap::new();

// iterate through the system boards and draw them on the screen
for board in self.system.get_all_boards().iter_mut() {
Expand Down Expand Up @@ -439,6 +442,12 @@ impl Project {
let mut connection_in_progress = ctx.data_mut(|data| {
data.get_temp_mut_or(id, false).clone()
});

if connection_in_progress {
ctx.output_mut(|o| {
o.cursor_icon = egui::CursorIcon::PointingHand;
});
}

if connection_in_progress && r.clicked() {
// check conditions for starting/ending a connection
Expand Down Expand Up @@ -548,6 +557,7 @@ impl Project {
}

// go through the system connections and see if this pin is a part of any of them
let mut connection_to_remove: Option<system::Connection> = None;
for connection in self.system.connections.iter_mut() {
// get the start and end pin locations. If they're not in the map (which they should be...), just skip
let start_loc: egui::Pos2 = match pin_locations.get(&(connection.start_board.clone(), connection.start_pin.clone())) {
Expand All @@ -559,19 +569,42 @@ impl Project {
None => continue,
};
// draw the connection and perform interactions.
let resp = draw_connection(ctx, ui, start_loc, end_loc, egui::Color32::WHITE);
let c = match connection.interface_mapping.interface.iface_type {
board::pinout::InterfaceType::I2C => egui::Color32::RED,
board::pinout::InterfaceType::UART => egui::Color32::BLUE,
board::pinout::InterfaceType::SPI => egui::Color32::YELLOW,
board::pinout::InterfaceType::NONE => egui::Color32::GREEN,
_ => egui::Color32::WHITE,
};
let resp = draw_connection(ctx, ui, start_loc, end_loc, c);
// Connection-level right click menu
resp.context_menu(|ui| {
ui.label("connection name:");
ui.text_edit_singleline(&mut connection.name);
ui.label(format!("start board: {}", connection.start_board.get_name()));
ui.separator();
ui.label("connection type:");
for iface_type in enum_iterator::all::<board::pinout::InterfaceType>() {
ui.selectable_value(&mut connection.interface_mapping.interface.iface_type, iface_type, format!("{:?}", iface_type));
}
ui.separator();
if ui.button("delete connection").clicked() {
connection_to_remove = Some(connection.clone());
}
});
}

// remove the connection if it was selected for deletion
if let Some(conn) = connection_to_remove {
self.system.connections.retain(|elem| {
elem.name != conn.name
});
}

}

/// Show the project HUD with information about the current system. Return a "Mode" so that
/// the calling module (app) can update the GUI accordingly.
pub fn display_system_editor_hud(&mut self, ctx: &egui::Context, ui: &mut egui::Ui, warning_flags: &mut Warnings) -> Option<Mode> {
pub fn display_system_editor_top_bar(&mut self, ctx: &egui::Context, ui: &mut egui::Ui, warning_flags: &mut Warnings) -> Option<Mode> {

// prepare the return value
let mut ret: Option<Mode> = None;
Expand All @@ -590,6 +623,8 @@ impl Project {
let top_hud_rect = ui.vertical_centered(|ui| {
let te = egui::TextEdit::singleline(self.borrow_name())
.horizontal_align(egui::Align::Center)
// .desired_width(f32::INFINITY)
.clip_text(false)
.frame(false)
.hint_text("enter project name here")
.font(font);
Expand Down Expand Up @@ -663,6 +698,7 @@ impl Project {
}
}

// Below code should go into a "bottom_bar" display function
// Show some system stats
// ui.with_layout(egui::Layout::bottom_up(egui::Align::Center), |ui| {
// ui.label(format!("number of connections: {}", self.system.connections.len()));
Expand Down

0 comments on commit 41a9a81

Please sign in to comment.