Skip to content

Commit

Permalink
Merge pull request #34 from sparshg/format
Browse files Browse the repository at this point in the history
Fix linting issues
  • Loading branch information
Spydr06 authored Oct 3, 2023
2 parents 84428c1 + b9707b4 commit b4b7826
Show file tree
Hide file tree
Showing 31 changed files with 2,079 additions and 1,009 deletions.
160 changes: 109 additions & 51 deletions src/application/action.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use crate::{simulator::*, config, project::ProjectRef, renderer::{vector::Vector2, Color}, id::Id};
use crate::{
config,
id::Id,
project::ProjectRef,
renderer::{vector::Vector2, Color},
simulator::*,
};

use super::*;

#[derive(Default)]
pub struct ActionStack {
actions: Vec<Action>,
next: usize,
dirty: bool
dirty: bool,
}

impl ActionStack {
Expand All @@ -29,7 +35,7 @@ impl ActionStack {
if let Some(action) = self.actions.get_mut(self.next) {
self.next += 1;
self.dirty = true;

info!("Re-doing action {}", self.next - 1);
action.exec(app);

Expand All @@ -48,7 +54,7 @@ impl ActionStack {
}

action.exec(app);

self.next += 1;
self.dirty = true;
self.actions.push(action);
Expand Down Expand Up @@ -92,29 +98,39 @@ pub enum Action {
impl Action {
fn exec(&mut self, app: &Application) {
match self {
Self::NewBlock(plot_provider, block) => { // place a new block
Self::NewBlock(plot_provider, block) => {
// place a new block
plot_provider.with_mut(|plot| plot.add_block(block.clone()));
app.imp().rerender_editor();
}
Self::PasteBlocks(plot_provier, blocks, connections) => {
plot_provier.with_mut(|plot| {
blocks.iter().for_each(|block| plot.add_block(block.clone()));
connections.iter().for_each(|connection| unsafe { plot.add_connection_unsafe(connection.clone()) });
blocks
.iter()
.for_each(|block| plot.add_block(block.clone()));
connections.iter().for_each(|connection| unsafe {
plot.add_connection_unsafe(connection.clone())
});
});
app.imp().rerender_editor();
}
Self::MoveBlock(plot_provider, block_id, _from, to) => {
plot_provider.with_mut(|plot| if let Some(block) = plot.get_block_mut(*block_id) {
block.set_position(*to);
plot_provider.with_mut(|plot| {
if let Some(block) = plot.get_block_mut(*block_id) {
block.set_position(*to);
}
});
app.imp().rerender_editor();
}
Self::MoveWaypoint(plot_provider, segment_id, _from, to) => {
plot_provider.with_mut(|plot|
if let Some(waypoint) = plot.get_connection_mut(segment_id.connection_id()).and_then(|c| c.get_segment_mut(segment_id.location())) {
plot_provider.with_mut(|plot| {
if let Some(waypoint) = plot
.get_connection_mut(segment_id.connection_id())
.and_then(|c| c.get_segment_mut(segment_id.location()))
{
waypoint.set_position(*to);
}
);
});
app.imp().rerender_editor();
}
Self::NewConnection(plot_provider, connection) => {
Expand All @@ -123,17 +139,29 @@ impl Action {
});
app.imp().rerender_editor();
}
Self::WaypointToConnection(plot_provider, segment_id, _segment, block_id, block_port) => {
plot_provider.with_mut(|plot|
if let Some(waypoint) = plot.get_connection_mut(segment_id.connection_id()).and_then(|c| c.get_segment_mut(segment_id.location())) {
Self::WaypointToConnection(
plot_provider,
segment_id,
_segment,
block_id,
block_port,
) => {
plot_provider.with_mut(|plot| {
if let Some(waypoint) = plot
.get_connection_mut(segment_id.connection_id())
.and_then(|c| c.get_segment_mut(segment_id.location()))
{
waypoint.convert(*block_id, *block_port);

if let Some(block) = plot.get_block_mut(*block_id) {
block.set_connection(Connector::Input(*block_port), Some(*segment_id.connection_id()));
block.set_connection(
Connector::Input(*block_port),
Some(*segment_id.connection_id()),
);
plot.add_block_to_update(*block_id);
}
}
);
});
app.imp().rerender_editor();
}
Self::AddSegment(plot_provider, segment_id, segment, index) => {
Expand All @@ -156,13 +184,16 @@ impl Action {
}
Self::ChangeBorderColor(plot_provider, new_color, block_ids, old_colors) => {
let old = plot_provider.with_mut(|plot| {
block_ids.iter().filter_map(|block_id| {
plot.get_block_mut(*block_id).map(|block| {
let old_color = *block.color();
block.set_color(Some(*new_color));
old_color
block_ids
.iter()
.filter_map(|block_id| {
plot.get_block_mut(*block_id).map(|block| {
let old_color = *block.color();
block.set_color(Some(*new_color));
old_color
})
})
}).collect()
.collect()
});
if let Some(old) = old {
*old_colors = old;
Expand All @@ -172,19 +203,23 @@ impl Action {
}
Self::DeleteSelection(plot_provider, blocks, connections, incoming) => {
//println!("delete connections: {connections:?} incoming: {incoming:?}");
*incoming = plot_provider.with_mut(|plot| {
for connection in &*connections {
plot.remove_connection(connection.id());
}
*incoming = plot_provider
.with_mut(|plot| {
for connection in &*connections {
plot.remove_connection(connection.id());
}

let mut incoming = vec![];
for block in blocks.iter() {
incoming.append(&mut plot.delete_block(block.id()))
}
incoming
}).unwrap_or_default();
let mut incoming = vec![];
for block in blocks.iter() {
incoming.append(&mut plot.delete_block(block.id()))
}
incoming
})
.unwrap_or_default();

blocks.iter_mut().for_each(|block| block.set_highlighted(false));
blocks
.iter_mut()
.for_each(|block| block.set_highlighted(false));
app.imp().rerender_editor();
}
Self::CreateModule(project, module) => {
Expand All @@ -204,13 +239,14 @@ impl Action {

fn undo(&self, app: &Application) {
match self {
Self::NewBlock(plot_provider, block) => { // remove a block
Self::NewBlock(plot_provider, block) => {
// remove a block
plot_provider.with_mut(|plot| plot.delete_block(block.id()));
app.imp().rerender_editor();
}
Self::PasteBlocks(plot_provier, blocks, connections) => {
plot_provier.with_mut(|plot| {
blocks.iter().for_each(|block| {
plot_provier.with_mut(|plot| {
blocks.iter().for_each(|block| {
plot.delete_block(block.id());
});
connections.iter().for_each(|connection| {
Expand All @@ -220,19 +256,22 @@ impl Action {
app.imp().rerender_editor();
}
Self::MoveBlock(plot_provider, block_id, from, _to) => {
plot_provider.with_mut(|plot|
plot_provider.with_mut(|plot| {
if let Some(block) = plot.get_block_mut(*block_id) {
block.set_position(*from);
}
);
});
app.imp().rerender_editor();
}
Self::MoveWaypoint(plot_provider, segment_id, from, _to) => {
plot_provider.with_mut(|plot|
if let Some(waypoint) = plot.get_connection_mut(segment_id.connection_id()).and_then(|c| c.get_segment_mut(segment_id.location())) {
plot_provider.with_mut(|plot| {
if let Some(waypoint) = plot
.get_connection_mut(segment_id.connection_id())
.and_then(|c| c.get_segment_mut(segment_id.location()))
{
waypoint.set_position(*from);
}
);
});
app.imp().rerender_editor();
}
Self::NewConnection(plot_provider, connection) => {
Expand All @@ -241,9 +280,18 @@ impl Action {
});
app.imp().rerender_editor();
}
Self::WaypointToConnection(plot_provider, segment_id, segment, block_id, block_port) => {
Self::WaypointToConnection(
plot_provider,
segment_id,
segment,
block_id,
block_port,
) => {
plot_provider.with_mut(|plot| {
if let Some(waypoint) = plot.get_connection_mut(segment_id.connection_id()).and_then(|c| c.get_segment_mut(segment_id.location())) {
if let Some(waypoint) = plot
.get_connection_mut(segment_id.connection_id())
.and_then(|c| c.get_segment_mut(segment_id.location()))
{
*waypoint = segment.clone();
}

Expand All @@ -270,20 +318,30 @@ impl Action {
}
Self::ChangeBorderColor(plot_provider, _new_color, block_ids, old_colors) => {
plot_provider.with_mut(|plot| {
block_ids.iter().zip(old_colors).for_each(|(block_id, old_color)| {
if let Some(block) = plot.get_block_mut(*block_id) {
block.set_color(*old_color);
}
});
block_ids
.iter()
.zip(old_colors)
.for_each(|(block_id, old_color)| {
if let Some(block) = plot.get_block_mut(*block_id) {
block.set_color(*old_color);
}
});
});

app.imp().rerender_editor();
}
Self::DeleteSelection(plot_provider, blocks, connections, incoming) => {
println!("restore connections: {connections:?} incoming: {incoming:?}");
plot_provider.with_mut(|plot| {
blocks.iter().for_each(|block| plot.add_block(block.clone()));
connections.iter().chain(incoming.iter()).for_each(|connection| unsafe { plot.add_connection_unsafe(connection.clone()) });
blocks
.iter()
.for_each(|block| plot.add_block(block.clone()));
connections
.iter()
.chain(incoming.iter())
.for_each(|connection| unsafe {
plot.add_connection_unsafe(connection.clone())
});
});
app.imp().rerender_editor();
}
Expand Down
50 changes: 33 additions & 17 deletions src/application/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{simulator::*, renderer::vector::*, id::Id};
use serde::{Serialize, Deserialize};
use crate::{id::Id, renderer::vector::*, simulator::*};
use serde::{Deserialize, Serialize};

use super::{action::Action, selection::*};

#[derive(Serialize, Deserialize, Debug)]
pub enum Clipboard {
Empty,
Blocks(Vec<Block>, Vec<Connection>),
Module(Box<Module>)
Module(Box<Module>),
}

impl Default for Clipboard {
Expand All @@ -25,17 +25,26 @@ impl Clipboard {
serde_json::from_str(data).map_err(|err| err.to_string())
}

pub fn paste_to(&self, plot_provider: PlotProvider, position: Vector2<f64>) -> Result<Action, String> {
pub fn paste_to(
&self,
plot_provider: PlotProvider,
position: Vector2<f64>,
) -> Result<Action, String> {
if let Clipboard::Blocks(blocks, connections) = self {
let mut data = (blocks.to_owned(), connections.to_owned());
data.prepare_pasting(position);
plot_provider.with_mut(|plot| {
plot.unhighlight();
plot.set_selection(Selection::Many(data.0.iter().map(|block| Selectable::Block(block.id())).collect()));
plot.set_selection(Selection::Many(
data.0
.iter()
.map(|block| Selectable::Block(block.id()))
.collect(),
));
});
return Ok(Action::PasteBlocks(plot_provider, data.0, data.1));
}

panic!("called `paste_to()` on clipboard != Clipboard::Blocks")
}
}
Expand Down Expand Up @@ -86,8 +95,7 @@ impl Copyable<(&Plot, Vec<BlockID>)> for (Vec<Block>, Vec<Connection>) {
let mut connection = connection.clone();
if connection.remove_unselected_branches(&block_ids) {
*c = None;
}
else {
} else {
connections.push(connection);
}
}
Expand Down Expand Up @@ -120,7 +128,12 @@ trait Pasteable<T> {

impl Pasteable<Vector2<f64>> for (Vec<Block>, Vec<Connection>) {
fn prepare_pasting(&mut self, position: Vector2<f64>) -> &mut Self {
let min = self.0.iter().map(|block| block.position()).min().unwrap_or_default();
let min = self
.0
.iter()
.map(|block| block.position())
.min()
.unwrap_or_default();
let offset = Vector2::cast(position) - min;

self.0.iter_mut().for_each(|block| {
Expand All @@ -130,28 +143,31 @@ impl Pasteable<Vector2<f64>> for (Vec<Block>, Vec<Connection>) {
block.set_position(block.position() + offset);
block.set_highlighted(true);

self.1.iter_mut().for_each(|connection| connection.refactor_id(old_id, new_id));
self.1
.iter_mut()
.for_each(|connection| connection.refactor_id(old_id, new_id));
});

self.1.iter_mut().for_each(|connection| {
let old_id = connection.id();
let new_id = Id::new();
connection.set_id(new_id);
connection.for_each_mut_segment(|segment|
connection.for_each_mut_segment(|segment| {
if let Some(position) = segment.position() {
segment.set_position(*position + offset)
}
);
});

self.0.iter_mut().for_each(|block|
block.connections_mut()
self.0.iter_mut().for_each(|block| {
block
.connections_mut()
.filter_map(|c| c.as_mut())
.for_each(|c|
.for_each(|c| {
if *c == old_id {
*c = new_id;
}
)
);
})
});
});

self
Expand Down
Loading

0 comments on commit b4b7826

Please sign in to comment.