Compare commits

..

4 Commits

@ -11,11 +11,18 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "logicworld-subassembly" name = "logicworld-subassembly"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"binary-serialize-derive", "binary-serialize-derive",
"lazy_static",
"quaternion", "quaternion",
"vecmath", "vecmath",
] ]

@ -5,5 +5,6 @@ edition = "2021"
[dependencies] [dependencies]
binary-serialize-derive = { path = "../binary-serialize-derive" } binary-serialize-derive = { path = "../binary-serialize-derive" }
lazy_static = "1.5.0"
quaternion = "2.0.0" quaternion = "2.0.0"
vecmath = "1.0.0" vecmath = "1.0.0"

@ -1,11 +1,34 @@
use lazy_static::lazy_static;
use std::{ use std::{
fs::{self, File}, i32, io::{self, Write}, path::Path collections::HashMap,
fs::{self, File},
io::{self, Write},
path::Path,
}; };
use crate::lw::{BinarySerializable, ComponentIdMap, ModVersion, Version}; use crate::lw::{BinarySerializable, ComponentIdMap, ModVersion, Version};
pub mod lw; pub mod lw;
lazy_static! {
pub static ref COMPONENT_MAP: HashMap<&'static str, u16> = HashMap::from([
("MHG.CircuitBoard", 1),
("MHG.Peg", 2),
("MHG.Inverter", 3),
("MHG.AndGate", 4),
("MHG.XorGate", 5),
("MHG.DLatch", 6),
("MHG.Relay", 7),
("MHG.Buffer", 8),
("MHG.Buffer_WithOutput", 9),
("MHG.Delayer", 10),
("MHG.Socket", 11),
("MHG.ChubbySocket", 12),
("MHG.Mount", 13),
("MHG.PanelLabel", 14),
]);
}
enum SaveType { enum SaveType {
_World = 1, _World = 1,
Subassembly = 2, Subassembly = 2,
@ -56,10 +79,10 @@ fn write_subassembly_file(
}]; }];
mods.write_to(file)?; mods.write_to(file)?;
let component_id_map = vec![ComponentIdMap { let component_id_map: Vec<ComponentIdMap> = COMPONENT_MAP.iter().map(|pair| ComponentIdMap {
numeric_id: 15, text_id: pair.0.to_string(),
text_id: "MHG.CircuitBoard".to_owned(), numeric_id: *pair.1,
}]; }).collect();
component_id_map.write_to(file)?; component_id_map.write_to(file)?;
for component in components { for component in components {

@ -47,14 +47,24 @@ pub struct ModVersion {
pub struct Input { pub struct Input {
circuit_state_id: Int, circuit_state_id: Int,
} }
impl Input {
pub fn new(circuit_state_id: Int) -> Self {
Self { circuit_state_id }
}
}
#[derive(BinarySerializable)] #[derive(BinarySerializable)]
pub struct Output { pub struct Output {
circuit_state_id: Int, circuit_state_id: Int,
} }
impl Output {
pub fn new(circuit_state_id: Int) -> Self {
Self { circuit_state_id }
}
}
#[derive(BinarySerializable)] #[derive(BinarySerializable)]
pub struct ComponentIdMap { pub struct ComponentIdMap {
pub numeric_id: i16, pub numeric_id: u16,
pub text_id: String, pub text_id: String,
} }
@ -64,6 +74,22 @@ pub struct PegAddress {
component_address: ComponentAddress, component_address: ComponentAddress,
index: Int, index: Int,
} }
impl PegAddress {
pub fn input(component_address: ComponentAddress, index: Int) -> Self {
Self {
peg_type: PegType::Input,
component_address,
index,
}
}
pub fn output(component_address: ComponentAddress, index: Int) -> Self {
Self {
peg_type: PegType::Output,
component_address,
index,
}
}
}
#[derive(BinarySerializable)] #[derive(BinarySerializable)]
pub struct Component { pub struct Component {
@ -79,10 +105,10 @@ pub struct Component {
#[derive(BinarySerializable)] #[derive(BinarySerializable)]
pub struct Wire { pub struct Wire {
first_point: PegAddress, pub first_point: PegAddress,
second_point: PegAddress, pub second_point: PegAddress,
circuit_state_id: Int, pub circuit_state_id: Int,
wire_rotation: Float, pub wire_rotation: Float,
} }
pub struct CircuitBoard { pub struct CircuitBoard {
@ -119,6 +145,85 @@ impl CircuitBoard {
return custom_data; return custom_data;
} }
} }
pub struct Label<'a> {
text: &'a str,
color: Vec<u8>,
size: Float,
mono: bool,
width: Int,
height: Int,
horizontal_align: HorizontalAlign,
vertical_align: VerticalAlign,
}
#[repr(i32)]
#[derive(Copy, Clone)]
pub enum HorizontalAlign {
Left = 0,
Middle = 1,
Right = 2,
}
#[repr(i32)]
#[derive(Copy, Clone)]
pub enum VerticalAlign {
Top = 0,
Middle = 1,
Bottom = 2,
}
impl<'a> Label<'a> {
pub fn new(text: &'a str) -> Self {
Self {
text,
color: vec![51, 51, 51],
size: 0.8,
mono: false,
width: 1,
height: 1,
horizontal_align: HorizontalAlign::Middle,
vertical_align: VerticalAlign::Middle,
}
}
pub fn mono(&mut self) -> &mut Self {
self.mono = true;
return self;
}
pub fn with_color(&mut self, r: u8, g: u8, b: u8) -> &mut Self {
self.color[0] = r;
self.color[1] = g;
self.color[2] = b;
return self;
}
pub fn with_size(&mut self, size: Float) -> &mut Self {
self.size = size;
return self;
}
pub fn with_dimensions(&mut self, width: Int, height: Int) -> &mut Self {
self.width = width;
self.height = height;
return self;
}
pub fn with_align(
&mut self,
horizontal: HorizontalAlign,
vertical: VerticalAlign,
) -> &mut Self {
self.horizontal_align = horizontal;
self.vertical_align = vertical;
return self;
}
pub fn custom_data(&self) -> Vec<u8> {
let mut custom_data = Vec::with_capacity(33);
custom_data.extend(&(self.horizontal_align as Int).to_le_bytes());
custom_data.extend(&self.color);
custom_data.extend(&self.size.to_le_bytes());
custom_data.extend(&[self.mono as u8]);
custom_data.extend(&(self.text.len() as Int).to_le_bytes());
custom_data.extend(self.text.as_bytes());
custom_data.extend(&self.width.to_le_bytes());
custom_data.extend(&self.height.to_le_bytes());
custom_data.extend(&(self.vertical_align as Int).to_le_bytes());
return custom_data;
}
}
impl<T: BinarySerializable> BinarySerializable for Vec<T> { impl<T: BinarySerializable> BinarySerializable for Vec<T> {
fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()> { fn write_to<W: Write>(&self, writer: &mut W) -> io::Result<()> {

Loading…
Cancel
Save