Skip to content

Commit

Permalink
Move rectangle filtering to the point of making the rectangle instead…
Browse files Browse the repository at this point in the history
… of where the rectangles are used.
  • Loading branch information
uglyoldbob committed Aug 7, 2023
1 parent f757ccd commit ae6a468
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 90 deletions.
6 changes: 3 additions & 3 deletions src/general.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
use egui_multiwin::egui;
/// interacts with ui elements (rectangles), combining them all into a single response
pub fn respond(ui: &mut egui::Ui, id: String, rects: Vec<egui::Rect>, bounds: egui::Rect) -> egui::Response {
pub fn respond(ui: &mut egui::Ui, id: String, rects: Vec<egui::Rect>) -> egui::Response {
let mut resp = ui.interact(
rects[0].intersect(bounds),
rects[0],
egui::Id::from(format!("{}.{}", id, 0)),
egui::Sense {
click: true,
Expand All @@ -15,7 +15,7 @@ pub fn respond(ui: &mut egui::Ui, id: String, rects: Vec<egui::Rect>, bounds: eg
for (num, r) in rects.iter().skip(1).enumerate() {
let num = num + 1;
let resp2 = ui.interact(
r.intersect(bounds),
*r,
egui::Id::from(format!("{}.{}", id, num)),
egui::Sense {
click: true,
Expand Down
91 changes: 47 additions & 44 deletions src/schematic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use std::collections::HashMap;

use egui_multiwin::egui;
use egui_multiwin::egui::{self, Rect};

use crate::{
component::ComponentVariantReference, general::StoragePath, library::LibraryHolder,
Expand Down Expand Up @@ -729,49 +729,52 @@ impl<'a> egui::Widget for SchematicWidget<'a> {
.color
.get_color32(crate::general::ColorMode::ScreenModeDark);
let r = pntr.text(pos, align, t.text.clone(), font, color);
let id = egui::Id::new(1 + i);
let response = ui.interact(r.intersect(area), id, sense);
let response = match self.mm {
MouseMode::NewComponent => response,
MouseMode::NewText => response,
MouseMode::Selection => {
if response.clicked() {
println!("Clicked in selection mode");
*self.selection = Some(SchematicSelection::Text {
page: self.page,
textnum: i,
});
}
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
let r = r.intersect(area);
if r.is_positive() {
let id = egui::Id::new(1 + i);
let response = ui.interact(r.intersect(area), id, sense);
let response = match self.mm {
MouseMode::NewComponent => response,
MouseMode::NewText => response,
MouseMode::Selection => {
if response.clicked() {
println!("Clicked in selection mode");
*self.selection = Some(SchematicSelection::Text {
page: self.page,
textnum: i,
});
}
})
}
MouseMode::TextDrag => {
if response.clicked() {
println!("Clicked in drag mode");
}
if response.dragged() {
let amount = response.drag_delta();
let a = SchematicAction::MoveText {
pagenum: self.page,
textnum: i,
delta: crate::general::Coordinates::from_pos2(
amount.to_pos2(),
*self.zoom,
),
};
actions.push(a);
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
}
})
}
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
MouseMode::TextDrag => {
if response.clicked() {
println!("Clicked in drag mode");
}
})
}
};
pr = pr.union(response);
if response.dragged() {
let amount = response.drag_delta();
let a = SchematicAction::MoveText {
pagenum: self.page,
textnum: i,
delta: crate::general::Coordinates::from_pos2(
amount.to_pos2(),
*self.zoom,
),
};
actions.push(a);
}
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
}
})
}
};
pr = pr.union(response);
}
}

for a in actions {
Expand Down Expand Up @@ -800,9 +803,9 @@ impl<'a> egui::Widget for SchematicWidget<'a> {
if let Some(lib) = &lib.library {
if let Some(symbol) = lib.syms.get(&sym.sym) {
let pos = sch.pos.get_pos2(*self.zoom, origin) - zoom_origin.to_vec2();
let rects = symbol.draw(*self.zoom, zoom_origin, &pntr, pos);
let rects = symbol.draw(*self.zoom, zoom_origin, &pntr, pos, area);
let response =
crate::general::respond(ui, format!("symbol{}", i), rects, area);
crate::general::respond(ui, format!("symbol{}", i), rects);
let response = match &self.mm {
MouseMode::Selection => {
if response.clicked() {
Expand Down Expand Up @@ -847,7 +850,7 @@ impl<'a> egui::Widget for SchematicWidget<'a> {
var: vr,
});
} else {
symdef.draw(*self.zoom, zoom_origin, &pntr, pos2);
symdef.draw(*self.zoom, zoom_origin, &pntr, pos2, area);
}
}
}
Expand Down
99 changes: 56 additions & 43 deletions src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ impl Pin {
zoom_center: egui_multiwin::egui::Pos2,
pntr: &egui::Painter,
pos: egui::Pos2,
bounds: egui::Rect,
) -> Vec<egui::Rect> {
let mult = crate::general::Length::Inches(0.1).get_screen(zoom, zoom_center);
let pos2 = pos
Expand Down Expand Up @@ -57,7 +58,12 @@ impl Pin {
.get_color32(crate::general::ColorMode::ScreenModeDark),
},
);
vec![rect]
let r2 = rect.intersect(bounds);
if r2.is_positive() {
vec![r2]
} else {
vec![]
}
}
}

Expand Down Expand Up @@ -135,6 +141,7 @@ impl SymbolDefinition {
zoom_center: egui_multiwin::egui::Pos2,
pntr: &egui::Painter,
pos: egui::Pos2,
bounds: egui::Rect,
) -> Vec<egui::Rect> {
let mut response = vec![];
for t in &self.texts {
Expand All @@ -149,13 +156,16 @@ impl SymbolDefinition {
.color
.get_color32(crate::general::ColorMode::ScreenModeDark);
let r = pntr.text(temp, align, t.text.clone(), font, color);
response.push(r);
let rcheck = r.intersect(bounds);
if rcheck.is_positive() {
response.push(rcheck);
}
}

for p in &self.pins {
let pos = p.location.get_pos2(zoom, zoom_center).to_vec2() + pos.to_vec2();
let temp = pos;
let mut rects = p.draw(zoom, zoom_center, &pntr, temp.to_pos2());
let mut rects = p.draw(zoom, zoom_center, &pntr, temp.to_pos2(), bounds);
response.append(&mut rects);
}
response
Expand Down Expand Up @@ -322,55 +332,58 @@ impl<'a> egui::Widget for SymbolDefinitionWidget<'a> {
.color
.get_color32(crate::general::ColorMode::ScreenModeDark);
let r = pntr.text(temp, align, t.text.clone(), font, color);
let r = r.intersect(area);
let id = egui::Id::new(1 + i);
let response = ui.interact(r, id, sense);
let response = match self.mm {
MouseMode::NewPin => response,
MouseMode::NewText => response,
MouseMode::Selection => {
if response.clicked() {
let inp = ui.input(|i| i.modifiers);
if !inp.shift && !inp.ctrl {
self.selection.clear();
if r.is_positive() {
let response = ui.interact(r, id, sense);
let response = match self.mm {
MouseMode::NewPin => response,
MouseMode::NewText => response,
MouseMode::Selection => {
if response.clicked() {
let inp = ui.input(|i| i.modifiers);
if !inp.shift && !inp.ctrl {
self.selection.clear();
}
self.selection
.push(SymbolWidgetSelection::Text { textnum: i });
}
self.selection
.push(SymbolWidgetSelection::Text { textnum: i });
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
}
})
}
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
MouseMode::TextDrag => {
if response.dragged_by(egui::PointerButton::Primary) {
let amount = response.drag_delta() / *self.zoom;
let a = LibraryAction::MoveText {
libname: self.sym.libname.clone(),
symname: self.sym.sym.name.clone(),
textnum: i,
delta: crate::general::Coordinates::from_pos2(
amount.to_pos2(),
*self.zoom,
),
};
self.actions.push(a);
}
})
}
MouseMode::TextDrag => {
if response.dragged_by(egui::PointerButton::Primary) {
let amount = response.drag_delta() / *self.zoom;
let a = LibraryAction::MoveText {
libname: self.sym.libname.clone(),
symname: self.sym.sym.name.clone(),
textnum: i,
delta: crate::general::Coordinates::from_pos2(
amount.to_pos2(),
*self.zoom,
),
};
self.actions.push(a);
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
}
})
}
response.context_menu(|ui| {
if ui.button("Properties").clicked() {
ui.close_menu();
}
})
}
};
pr = pr.union(response);
};
pr = pr.union(response);
}
}

for (i, p) in self.sym.sym.pins.iter().enumerate() {
let pos = p.location.get_pos2(*self.zoom, origin).to_vec2();
let temp = pos;
let rects = p.draw(*self.zoom, zoom_origin, &pntr, temp.to_pos2());
let response = crate::general::respond(ui, format!("pin {}", i), rects, area);
let rects = p.draw(*self.zoom, zoom_origin, &pntr, temp.to_pos2(), area);
let response = crate::general::respond(ui, format!("pin {}", i), rects);
let response = match self.mm {
MouseMode::NewPin => response,
MouseMode::NewText => response,
Expand Down Expand Up @@ -452,7 +465,7 @@ impl<'a> egui::Widget for SymbolDefinitionWidget<'a> {
pin: Some(pin),
});
} else {
pin.draw(*self.zoom, zoom_origin, &pntr, pos);
pin.draw(*self.zoom, zoom_origin, &pntr, pos, area);
}
}
}
Expand Down

0 comments on commit ae6a468

Please sign in to comment.