-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
Sequantial componets evalultion order fix
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::path::PathBuf; | ||
#[cfg(feature = "gui-egui")] | ||
use syncrim::gui_egui::editor::Library; | ||
use syncrim::{ | ||
common::{ComponentStore, Input}, | ||
components::*, | ||
fern::fern_setup, | ||
}; | ||
fn main() { | ||
fern_setup(); | ||
let cs = ComponentStore { | ||
store: vec![ | ||
Add::rc_new( | ||
"add", | ||
(200.0, 120.0), | ||
Input::new("c", "out"), | ||
Input::new("reg", "out"), | ||
), | ||
Constant::rc_new("c", (100.0, 100.0), 3), | ||
Register::rc_new( | ||
"reg", | ||
(100.0, 140.0), | ||
Input::new("pass", PASS_THROUGH_OUT_ID), | ||
), | ||
Wire::rc_new( | ||
"w1", | ||
vec![(110.0, 100.0), (180.0, 100.0)], | ||
Input::new("c", "out"), | ||
), | ||
Wire::rc_new( | ||
"w2", | ||
vec![(110.0, 140.0), (180.0, 140.0)], | ||
Input::new("reg", "out"), | ||
), | ||
Wire::rc_new( | ||
"w3", | ||
vec![(220.0, 120.0), (260.0, 120.0), (260.0, 180.0)], | ||
Input::new("add", "out"), | ||
), | ||
PassThrough::rc_new("pass", (260.0, 180.0), Input::new("add", "out")), | ||
Wire::rc_new( | ||
"w4", | ||
vec![(260.0, 180.0), (60.0, 180.0), (60.0, 140.0), (90.0, 140.0)], | ||
Input::new("pass", PASS_THROUGH_OUT_ID), | ||
), | ||
Probe::rc_new( | ||
"p_add", | ||
(280.0, 120.0), | ||
Input::new("pass", PASS_THROUGH_OUT_ID), | ||
), | ||
Probe::rc_new("p_reg", (130.0, 120.0), Input::new("reg", "out")), | ||
], | ||
}; | ||
|
||
let path = PathBuf::from("add_reg_compound.json"); | ||
cs.save_file(&path); | ||
|
||
#[cfg(feature = "gui-egui")] | ||
syncrim::gui_egui::gui(cs, &path, Library::default()).ok(); | ||
|
||
#[cfg(feature = "gui-vizia")] | ||
syncrim::gui_vizia::gui(cs, &path); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#[cfg(feature = "gui-egui")] | ||
use crate::common::EguiComponent; | ||
use crate::common::{Component, Condition, Id, Input, InputPort, OutputType, Ports, Simulator}; | ||
use log::*; | ||
use serde::{Deserialize, Serialize}; | ||
use std::any::Any; | ||
use std::rc::Rc; | ||
|
||
pub const PASS_THROUGH_IN_ID: &str = "pass_through_in"; | ||
|
||
pub const PASS_THROUGH_OUT_ID: &str = "pass_through_out"; | ||
|
||
#[derive(Serialize, Deserialize, Clone)] | ||
pub struct PassThrough { | ||
pub(crate) id: Id, | ||
pub(crate) pos: (f32, f32), | ||
pub(crate) input: Input, | ||
} | ||
|
||
#[typetag::serde] | ||
impl Component for PassThrough { | ||
fn to_(&self) { | ||
trace!("pass_through"); | ||
} | ||
#[cfg(feature = "gui-egui")] | ||
fn dummy(&self, id: &str, pos: (f32, f32)) -> Box<Rc<dyn EguiComponent>> { | ||
let dummy_input = Input::new("dummy", "out"); | ||
Box::new(Rc::new(PassThrough { | ||
id: id.to_string(), | ||
pos: (pos.0, pos.1), | ||
input: dummy_input.clone(), | ||
})) | ||
} | ||
fn get_id_ports(&self) -> (Id, Ports) { | ||
( | ||
self.id.clone(), | ||
Ports::new( | ||
// Vector of inputs | ||
vec![&InputPort { | ||
port_id: PASS_THROUGH_IN_ID.to_string(), | ||
input: self.input.clone(), | ||
}], | ||
OutputType::Combinatorial, | ||
vec![PASS_THROUGH_OUT_ID], | ||
), | ||
) | ||
} | ||
|
||
// propagate input value to output | ||
fn clock(&self, simulator: &mut Simulator) -> Result<(), Condition> { | ||
// get input value | ||
let value = simulator.get_input_value_mut(self.id.clone(), &self.input); | ||
// set output | ||
simulator.set_out_value(&self.id, PASS_THROUGH_OUT_ID, value); | ||
Ok(()) | ||
} | ||
|
||
fn set_id_port(&mut self, target_port_id: Id, new_input: Input) { | ||
if target_port_id == PASS_THROUGH_IN_ID { | ||
self.input = new_input; | ||
} | ||
} | ||
|
||
fn as_any(&self) -> &dyn Any { | ||
self | ||
} | ||
} | ||
|
||
impl PassThrough { | ||
pub fn new(id: &str, pos: (f32, f32), input: Input) -> Self { | ||
PassThrough { | ||
id: id.to_string(), | ||
pos, | ||
input, | ||
} | ||
} | ||
|
||
pub fn rc_new(id: &str, pos: (f32, f32), input: Input) -> Rc<Self> { | ||
Rc::new(PassThrough::new(id, pos, input)) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use crate::common::{Component, EguiComponent, Ports, Simulator}; | ||
use crate::components::PassThrough; | ||
use crate::gui_egui::editor::{EditorMode, EditorRenderReturn, GridOptions}; | ||
use crate::gui_egui::gui::EguiExtra; | ||
use egui::{ | ||
Align2, Area, Context, InnerResponse, Order, Pos2, Rect, Response, TextWrapMode, Ui, Vec2, | ||
}; | ||
|
||
#[typetag::serde] | ||
impl EguiComponent for PassThrough { | ||
/// TODO this need to be rewritten when newer helper functions becomes available | ||
fn render( | ||
&self, | ||
ui: &mut Ui, | ||
_context: &mut EguiExtra, | ||
_simulator: Option<&mut Simulator>, | ||
offset: Vec2, | ||
scale: f32, | ||
clip_rect: Rect, | ||
_editor_mode: EditorMode, | ||
) -> Option<Vec<Response>> { | ||
fn component_area<R>( | ||
id: String, | ||
ctx: &Context, | ||
pos: impl Into<Pos2>, | ||
content: impl FnOnce(&mut Ui) -> R, | ||
) -> InnerResponse<R> { | ||
Area::new(egui::Id::from(id)) | ||
.order(Order::Middle) | ||
.current_pos(pos) | ||
.movable(false) | ||
.enabled(true) | ||
.interactable(false) | ||
.pivot(Align2::CENTER_CENTER) | ||
.constrain(false) | ||
.show(ctx, content) | ||
} | ||
|
||
let offset: Vec2 = offset.into(); | ||
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-pc-windows-gnu)useless conversion to the same type: `egui::Vec2`
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-unknown-linux-musl)useless conversion to the same type: `egui::Vec2`
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-apple-darwin)useless conversion to the same type: `egui::Vec2`
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-pc-windows-gnu)useless conversion to the same type: `egui::Vec2`
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-unknown-linux-musl)useless conversion to the same type: `egui::Vec2`
Check warning on line 39 in src/gui_egui/components/pass_through.rs GitHub Actions / build (x86_64-apple-darwin)useless conversion to the same type: `egui::Vec2`
|
||
|
||
let r = component_area( | ||
self.get_id_ports().0, | ||
ui.ctx(), | ||
Pos2::from(self.get_pos()) * scale + offset, | ||
|ui| { | ||
ui.set_clip_rect(clip_rect); | ||
|
||
ui.style_mut().wrap_mode = Some(TextWrapMode::Extend); | ||
|
||
for (_text_style, font) in ui.style_mut().text_styles.iter_mut() { | ||
font.size *= scale; | ||
} | ||
ui.spacing_mut().button_padding *= scale; | ||
ui.spacing_mut().item_spacing *= scale; | ||
ui.spacing_mut().combo_height *= scale; | ||
ui.spacing_mut().combo_width *= scale; | ||
ui.spacing_mut().icon_width *= scale; | ||
ui.spacing_mut().icon_width_inner *= scale; | ||
ui.spacing_mut().icon_spacing *= scale; | ||
ui.spacing_mut().interact_size *= scale; | ||
|
||
let mut group = egui::containers::Frame::group(ui.style()); | ||
group.inner_margin *= scale; | ||
group.rounding *= scale; | ||
// group.fill = Color32::LIGHT_RED; // Use this ween component background is implemented, probably when we implement dark mode | ||
group | ||
.show(ui, |ui| { | ||
ui.label("➡️"); | ||
}) | ||
.response | ||
}, | ||
) | ||
.inner; | ||
Some(vec![r]) | ||
} | ||
|
||
fn render_editor( | ||
&mut self, | ||
_ui: &mut Ui, | ||
_context: &mut EguiExtra, | ||
_simulator: Option<&mut Simulator>, | ||
_offset: Vec2, | ||
_scale: f32, | ||
_clip_rect: Rect, | ||
_id_ports: &[(crate::common::Id, Ports)], | ||
_grid: &GridOptions, | ||
_editor_mode: EditorMode, | ||
) -> EditorRenderReturn { | ||
EditorRenderReturn { | ||
delete: false, | ||
resp: Some(vec![]), | ||
} | ||
} | ||
|
||
fn ports_location(&self) -> Vec<(crate::common::Id, Pos2)> { | ||
let own_pos = Vec2::new(self.pos.0, self.pos.1); | ||
vec![ | ||
( | ||
crate::components::REGISTER_R_IN_ID.to_string(), | ||
Pos2::new(-10f32, 0f32) + own_pos, | ||
), | ||
( | ||
crate::components::REGISTER_OUT_ID.to_string(), | ||
Pos2::new(10f32, 0f32) + own_pos, | ||
), | ||
] | ||
} | ||
|
||
fn top_padding(&self) -> f32 { | ||
20f32 | ||
} | ||
|
||
fn set_pos(&mut self, pos: (f32, f32)) { | ||
self.pos = pos; | ||
} | ||
|
||
fn get_pos(&self) -> (f32, f32) { | ||
self.pos | ||
} | ||
} |