mirror of
https://github.com/OMGeeky/logisim.git
synced 2026-02-23 15:49:52 +01:00
load block from json
This commit is contained in:
21
Cargo.lock
generated
21
Cargo.lock
generated
@@ -166,6 +166,12 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "anyhow"
|
||||||
|
version = "1.0.98"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e16d2d3311acee920a9eb8d33b8cbc1787ce4a264e85f964c2404b969bdcd487"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "approx"
|
name = "approx"
|
||||||
version = "0.5.1"
|
version = "0.5.1"
|
||||||
@@ -466,6 +472,19 @@ dependencies = [
|
|||||||
"wgpu-types",
|
"wgpu-types",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bevy_common_assets"
|
||||||
|
version = "0.12.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3521990269672c442f2bf0fbed0fce9db719e3dd136dd4012a97809464a4389d"
|
||||||
|
dependencies = [
|
||||||
|
"anyhow",
|
||||||
|
"bevy",
|
||||||
|
"serde",
|
||||||
|
"serde_json",
|
||||||
|
"thiserror",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "bevy_core"
|
name = "bevy_core"
|
||||||
version = "0.15.3"
|
version = "0.15.3"
|
||||||
@@ -2635,7 +2654,9 @@ name = "logisim"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bevy",
|
"bevy",
|
||||||
|
"bevy_common_assets",
|
||||||
"iyes_perf_ui",
|
"iyes_perf_ui",
|
||||||
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -6,7 +6,8 @@ edition = "2024"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
bevy = { version = "0.15.0", features = ["bevy_dev_tools"] }
|
bevy = { version = "0.15.0", features = ["bevy_dev_tools"] }
|
||||||
iyes_perf_ui = "0.4.0"
|
iyes_perf_ui = "0.4.0"
|
||||||
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
|
bevy_common_assets = { version = "0.12.0", features = ["json"] }
|
||||||
|
|
||||||
|
|
||||||
# Enable more optimization in the release profile at the cost of compile time.
|
# Enable more optimization in the release profile at the cost of compile time.
|
||||||
|
|||||||
117
assets/logisim/blocks/sample1.blockdef.json
Normal file
117
assets/logisim/blocks/sample1.blockdef.json
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"pos": [
|
||||||
|
0.0,
|
||||||
|
0.0
|
||||||
|
],
|
||||||
|
"size": [
|
||||||
|
50,
|
||||||
|
80
|
||||||
|
],
|
||||||
|
"name": "AND",
|
||||||
|
"color": {
|
||||||
|
"Srgba": {
|
||||||
|
"red": 1.0,
|
||||||
|
"green": 0.0,
|
||||||
|
"blue": 0.0,
|
||||||
|
"alpha": 1.0
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"wires": [
|
||||||
|
{
|
||||||
|
"connections": [
|
||||||
|
{
|
||||||
|
"parent_block": 1,
|
||||||
|
"id": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parent_block": 1,
|
||||||
|
"id": 2
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parent_block": 1,
|
||||||
|
"id": 3
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parent_block": 1,
|
||||||
|
"id": 4
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"parent_block": 1,
|
||||||
|
"id": 6
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"inputs": [
|
||||||
|
{
|
||||||
|
"id": 1,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 2,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"id": 3,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 4,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 5,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": 6,
|
||||||
|
"value": {
|
||||||
|
"HalfByte": [
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
true,
|
||||||
|
true
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"inner_blocks": []
|
||||||
|
}
|
||||||
@@ -1,2 +0,0 @@
|
|||||||
{
|
|
||||||
}
|
|
||||||
@@ -2,11 +2,22 @@ use crate::camera::Canvas;
|
|||||||
use bevy::color::palettes::basic::{GREEN, RED, WHITE};
|
use bevy::color::palettes::basic::{GREEN, RED, WHITE};
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
use bevy::text::TextBounds;
|
use bevy::text::TextBounds;
|
||||||
|
use bevy_common_assets::json::JsonAssetPlugin;
|
||||||
|
use serde::Deserialize;
|
||||||
use std::ops::BitOr;
|
use std::ops::BitOr;
|
||||||
|
|
||||||
const LABEL_SCALING_FACTOR: f32 = 0.2;
|
const LABEL_SCALING_FACTOR: f32 = 0.2;
|
||||||
|
|
||||||
#[derive(Resource, Debug, Clone)]
|
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq, Hash, States)]
|
||||||
|
enum AppState {
|
||||||
|
#[default]
|
||||||
|
Loading,
|
||||||
|
Running,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Resource)]
|
||||||
|
struct BlockDefinitionHandle(Handle<BlockDefinition>);
|
||||||
|
#[derive(Deserialize, Asset, TypePath, Debug, Clone)]
|
||||||
pub struct BlockDefinition {
|
pub struct BlockDefinition {
|
||||||
id: usize,
|
id: usize,
|
||||||
pos: Vec2,
|
pos: Vec2,
|
||||||
@@ -18,22 +29,23 @@ pub struct BlockDefinition {
|
|||||||
inputs: Vec<ConnectionDefinition>,
|
inputs: Vec<ConnectionDefinition>,
|
||||||
outputs: Vec<ConnectionDefinition>,
|
outputs: Vec<ConnectionDefinition>,
|
||||||
}
|
}
|
||||||
#[derive(Resource, Debug, Clone)]
|
#[derive(Deserialize, Asset, TypePath, Debug, Clone)]
|
||||||
pub struct WireDefinition {
|
pub struct WireDefinition {
|
||||||
connections: Vec<ConnectionDefinitionRef>,
|
connections: Vec<ConnectionDefinitionRef>,
|
||||||
}
|
}
|
||||||
#[derive(Resource, Debug, Clone, Copy)]
|
#[derive(Deserialize, Asset, TypePath, Debug, Clone, Copy)]
|
||||||
pub struct ConnectionDefinition {
|
pub struct ConnectionDefinition {
|
||||||
id: usize,
|
id: usize,
|
||||||
value: ConnectionValues,
|
value: ConnectionValues,
|
||||||
}
|
}
|
||||||
#[derive(Resource, Debug, Clone, Copy)]
|
#[derive(Deserialize, Asset, TypePath, Debug, Clone, Copy)]
|
||||||
pub struct ConnectionDefinitionRef {
|
pub struct ConnectionDefinitionRef {
|
||||||
parent_block: usize,
|
parent_block: usize,
|
||||||
id: usize,
|
id: usize,
|
||||||
}
|
}
|
||||||
#[derive(Bundle, Debug)]
|
#[derive(Bundle, Debug)]
|
||||||
pub struct BlockBundle {
|
pub struct BlockBundle {
|
||||||
|
id: BlockId,
|
||||||
block: Block,
|
block: Block,
|
||||||
transform: Transform,
|
transform: Transform,
|
||||||
global_transform: GlobalTransform,
|
global_transform: GlobalTransform,
|
||||||
@@ -72,6 +84,10 @@ pub struct BlockVisuals {
|
|||||||
#[derive(Component, Debug, Copy, Clone)]
|
#[derive(Component, Debug, Copy, Clone)]
|
||||||
pub struct ConnectionReference(Entity);
|
pub struct ConnectionReference(Entity);
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
|
pub struct BlockId {
|
||||||
|
id: usize,
|
||||||
|
}
|
||||||
|
#[derive(Component, Debug)]
|
||||||
pub struct Block {
|
pub struct Block {
|
||||||
inputs: Vec<ConnectionReference>,
|
inputs: Vec<ConnectionReference>,
|
||||||
outputs: Vec<ConnectionReference>,
|
outputs: Vec<ConnectionReference>,
|
||||||
@@ -91,7 +107,7 @@ pub struct OutputConnection;
|
|||||||
pub struct Connection {
|
pub struct Connection {
|
||||||
values: ConnectionValues,
|
values: ConnectionValues,
|
||||||
}
|
}
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Deserialize, Debug, Clone, Copy)]
|
||||||
pub enum ConnectionValues {
|
pub enum ConnectionValues {
|
||||||
Single(bool),
|
Single(bool),
|
||||||
HalfByte(bool, bool, bool, bool),
|
HalfByte(bool, bool, bool, bool),
|
||||||
@@ -353,8 +369,14 @@ impl Plugin for LogicSimPlugin {
|
|||||||
app
|
app
|
||||||
//hi
|
//hi
|
||||||
// .insert_resource(get_sample_block())
|
// .insert_resource(get_sample_block())
|
||||||
|
.add_plugins(JsonAssetPlugin::<BlockDefinition>::new(&["blockdef.json"]))
|
||||||
.add_systems(Startup, setup)
|
.add_systems(Startup, setup)
|
||||||
.add_systems(Update, spawn_block_resources)
|
.init_asset::<BlockDefinition>()
|
||||||
|
.init_state::<AppState>()
|
||||||
|
.add_systems(
|
||||||
|
Update,
|
||||||
|
spawn_block_definition_from_asset.run_if(in_state(AppState::Loading)),
|
||||||
|
)
|
||||||
.add_systems(
|
.add_systems(
|
||||||
Update,
|
Update,
|
||||||
(
|
(
|
||||||
@@ -429,11 +451,34 @@ fn get_sample_block() -> BlockDefinition {
|
|||||||
],
|
],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn setup(commands: Commands, asset_server: Res<AssetServer>) {
|
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||||
let block = get_sample_block();
|
let block_def =
|
||||||
spawn_block_definition(commands, asset_server, block);
|
BlockDefinitionHandle(asset_server.load("logisim/blocks/sample1.blockdef.json"));
|
||||||
|
commands.insert_resource(block_def);
|
||||||
|
|
||||||
|
// let block = get_sample_block();
|
||||||
|
// spawn_block_definition(commands, asset_server, block);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn spawn_block_definition_from_asset(
|
||||||
|
mut commands: Commands,
|
||||||
|
asset_server: Res<AssetServer>,
|
||||||
|
block: Res<BlockDefinitionHandle>,
|
||||||
|
mut blocks: ResMut<Assets<BlockDefinition>>,
|
||||||
|
mut state: ResMut<NextState<AppState>>,
|
||||||
|
spawned_blocks: Query<(Entity, &BlockId)>,
|
||||||
|
) {
|
||||||
|
if let Some(block) = blocks.remove(block.0.id()) {
|
||||||
|
let spawned_block = spawned_blocks
|
||||||
|
.iter()
|
||||||
|
.find_map(|(e, b)| if b.id == block.id { Some(e) } else { None });
|
||||||
|
if let Some(e) = spawned_block {
|
||||||
|
commands.entity(e).despawn_recursive();
|
||||||
|
}
|
||||||
|
spawn_block_definition(commands, asset_server, block);
|
||||||
|
state.set(AppState::Running)
|
||||||
|
}
|
||||||
|
}
|
||||||
fn spawn_block_definition(
|
fn spawn_block_definition(
|
||||||
mut commands: Commands,
|
mut commands: Commands,
|
||||||
asset_server: Res<AssetServer>,
|
asset_server: Res<AssetServer>,
|
||||||
@@ -502,6 +547,7 @@ fn spawn_block_definition(
|
|||||||
|
|
||||||
commands
|
commands
|
||||||
.spawn(BlockBundle {
|
.spawn(BlockBundle {
|
||||||
|
id: BlockId { id: block.id },
|
||||||
block_visuals: BlockVisuals {
|
block_visuals: BlockVisuals {
|
||||||
size: block.size,
|
size: block.size,
|
||||||
color: block.color,
|
color: block.color,
|
||||||
@@ -516,9 +562,6 @@ fn spawn_block_definition(
|
|||||||
.with_child(BlockLabelBundle::new(block.name, block.size, text_font));
|
.with_child(BlockLabelBundle::new(block.name, block.size, text_font));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_block_resources(// mut ev_asset
|
|
||||||
) {
|
|
||||||
}
|
|
||||||
fn render_blocks(
|
fn render_blocks(
|
||||||
blocks: Query<(&BlockVisuals, &Transform)>,
|
blocks: Query<(&BlockVisuals, &Transform)>,
|
||||||
canvas: Res<Canvas>,
|
canvas: Res<Canvas>,
|
||||||
|
|||||||
Reference in New Issue
Block a user