Skip to content

Commit

Permalink
allow editing more things about company
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Oct 3, 2023
1 parent 17492e3 commit 40fc7e0
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 95 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions assets_gui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"
common = { path = "../common" }
geom = { path = "../geom" }
engine = { path = "../engine" }
egui-inspect = { path = "../egui-inspect" }
log = { version = "0.4.11", features=["max_level_info", "release_max_level_info"] }
inline_tweak = "1.0.8"
egui_dock = "0.6.3"
Expand Down
152 changes: 150 additions & 2 deletions assets_gui/src/gui.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use common::descriptions::{BuildingGen, CompanyKind};
use egui::{Color32, Ui};
use egui_dock::{DockArea, NodeIndex, Style, TabStyle, Tree};
use egui_inspect::{Inspect, InspectArgs};

use engine::meshload::MeshProperties;
use engine::wgpu::RenderPass;
use engine::{Drawable, GfxContext, Mesh, SpriteBatch};
use geom::Matrix4;
use geom::{Matrix4, Vec2};

use crate::companies::Companies;
use crate::State;
Expand Down Expand Up @@ -87,8 +89,154 @@ fn properties(state: &mut State, ui: &mut Ui) {
Inspected::None => {}
Inspected::Company(i) => {
let comp = &mut state.gui.companies.companies[i];
ui.label(&comp.name);
text_inp(ui, "name", &mut comp.name);

let mut selected = match comp.kind {
CompanyKind::Store => 0,
CompanyKind::Factory { .. } => 1,
CompanyKind::Network => 2,
};

if egui::ComboBox::new("company_kind", "company kind")
.show_index(ui, &mut selected, 3, |idx| match idx {
0 => "Store",
1 => "Factory",
2 => "Network",
_ => unreachable!(),
})
.changed()
{
match selected {
0 => comp.kind = CompanyKind::Store,
1 => comp.kind = CompanyKind::Factory { n_trucks: 1 },
2 => comp.kind = CompanyKind::Network,
_ => unreachable!(),
}
}

match &mut comp.kind {
CompanyKind::Store | CompanyKind::Network => {}
CompanyKind::Factory { n_trucks } => {
inspect(ui, "n_trucks", n_trucks);
}
}

let mut selected = match comp.bgen {
BuildingGen::House => unreachable!(),
BuildingGen::Farm => 0,
BuildingGen::CenteredDoor { .. } => 1,
BuildingGen::NoWalkway { .. } => 2,
};
if egui::ComboBox::new("bgen_kind", "bgen kind")
.show_index(ui, &mut selected, 3, |idx| match idx {
0 => "Farm",
1 => "Centered door",
2 => "No walkway",
_ => unreachable!(),
})
.changed()
{
match selected {
0 => comp.bgen = BuildingGen::Farm,
1 => {
comp.bgen = BuildingGen::CenteredDoor {
vertical_factor: 1.0,
}
}
2 => {
comp.bgen = BuildingGen::NoWalkway {
door_pos: Vec2::ZERO,
}
}
_ => unreachable!(),
}
}

match &mut comp.bgen {
BuildingGen::House | BuildingGen::Farm => {}
BuildingGen::CenteredDoor { vertical_factor } => {
inspect(ui, "vertical factor", vertical_factor);
}
BuildingGen::NoWalkway { door_pos } => {
inspect(ui, "door_pos", door_pos);
}
}

ui.add_space(5.0);
ui.label("Recipe");
inspect(ui, "complexity", &mut comp.recipe.complexity);
inspect(
ui,
"storage_multiplier",
&mut comp.recipe.storage_multiplier,
);
ui.label("consumption");
ui.indent("consumption", |ui| {
for (name, amount) in comp.recipe.consumption.iter_mut() {
inspect_item(ui, name, amount);
}
});

ui.label("production");
ui.indent("production", |ui| {
for (name, amount) in comp.recipe.production.iter_mut() {
inspect_item(ui, name, amount);
}
});

inspect(ui, "n_workers", &mut comp.n_workers);
inspect(ui, "size", &mut comp.size);
text_inp(ui, "asset_location", &mut comp.asset_location);
inspect(ui, "price", &mut comp.price);

ui_opt(ui, "zone", &mut comp.zone, |ui, zone| {
ui.indent("zone", |ui| {
inspect(ui, "floor", &mut zone.floor);
inspect(ui, "filler", &mut zone.filler);
inspect(ui, "price_per_area", &mut zone.price_per_area);
});
});
}
}
}

fn inspect_item(ui: &mut Ui, name: &mut String, amount: &mut i32) {
ui.horizontal(|ui| {
ui.label(&*name);
ui.add(egui::DragValue::new(amount));
});
}

fn inspect<T: Inspect<T>>(ui: &mut Ui, label: &'static str, x: &mut T) -> bool {
<T as Inspect<T>>::render_mut(x, label, ui, &InspectArgs::default())
}

fn text_inp(ui: &mut Ui, label: &'static str, v: &mut String) {
ui.horizontal(|ui| {
ui.label(label);
ui.text_edit_singleline(v);
});
}

fn ui_opt<T: Default>(
ui: &mut Ui,
label: &'static str,
v: &mut Option<T>,
f: impl FnOnce(&mut Ui, &mut T),
) {
ui.horizontal(|ui| {
let mut is_some = v.is_some();
ui.checkbox(&mut is_some, label);
if is_some != v.is_some() {
if is_some {
*v = Some(Default::default());
} else {
*v = None;
}
}
});
if let Some(v) = v {
f(ui, v);
}
}

Expand Down
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ miniz_oxide = "0.7"
rustc-hash = "1.1.0"
inline_tweak = {version = "1.0.8"}
log-panics = { version = "2.0.0", features=["with-backtrace"] }
egui-inspect = { path = "../egui-inspect" }
47 changes: 39 additions & 8 deletions common/src/descriptions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use egui_inspect::debug_inspect_impl;
use geom::Vec2;
use serde::{Deserialize, Serialize};

Expand All @@ -9,21 +10,40 @@ pub struct RecipeDescription {
pub storage_multiplier: i32,
}

#[derive(Serialize, Deserialize)]
pub struct BuildingGenDescription {
pub kind: String,
pub vertical_factor: Option<f32>,
pub door_pos: Option<Vec2>,
#[derive(Copy, Clone, Serialize, Deserialize, Debug)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum CompanyKind {
// Buyers come to get their goods
Store,
// Buyers get their goods delivered to them
Factory { n_trucks: u32 },
// Buyers get their goods instantly delivered, useful for things like electricity/water/..
Network,
}

debug_inspect_impl!(CompanyKind);

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum BuildingGen {
House,
Farm,
CenteredDoor {
vertical_factor: f32, // 1.0 means that the door is at the bottom, just on the street
},
NoWalkway {
door_pos: Vec2, // door_pos is relative to the center of the building
},
}

#[derive(Serialize, Deserialize)]
pub struct GoodsCompanyDescriptionJSON {
pub name: String,
pub bgen: BuildingGenDescription,
pub kind: String,
pub bgen: BuildingGen,
#[serde(flatten)]
pub kind: CompanyKind,
pub recipe: RecipeDescription,
pub n_workers: i32,
pub n_trucks: Option<u32>,
pub size: f32,
pub asset_location: String,
pub price: i64,
Expand All @@ -41,3 +61,14 @@ pub struct ZoneDescription {
#[serde(default)]
pub randomize_filler: bool,
}

impl Default for ZoneDescription {
fn default() -> Self {
Self {
floor: "".to_string(),
filler: "".to_string(),
price_per_area: 100,
randomize_filler: false,
}
}
}
28 changes: 28 additions & 0 deletions egui-inspect/src/impls/i64.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use super::{Inspect, InspectArgs};

impl Inspect<i64> for i64 {
fn render(data: &Self, label: &'static str, ui: &mut egui::Ui, _args: &InspectArgs) {
// Values are consistent
let mut cp = *data;
ui.horizontal(|ui| {
ui.add(egui::DragValue::new(&mut cp));
ui.label(label);
});
}

fn render_mut(
data: &mut Self,
label: &'static str,
ui: &mut egui::Ui,
args: &InspectArgs,
) -> bool {
let before = *data;
ui.horizontal(|ui| {
ui.label(label);
ui.add(egui::DragValue::new(data).clamp_range(
args.min_value.unwrap_or(f32::MIN)..=args.max_value.unwrap_or(f32::MAX),
));
});
before != *data
}
}
1 change: 1 addition & 0 deletions egui-inspect/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod f32;
mod f64;
pub mod geometry;
mod i32;
mod i64;
mod option;
mod str;
mod string;
Expand Down
3 changes: 2 additions & 1 deletion native_app/src/gui/topgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::gui::windows::GUIWindows;
use crate::gui::{ErrorTooltip, PotentialCommands, RoadBuildResource, Tool, UiTextures};
use crate::inputmap::{InputAction, InputMap};
use crate::uiworld::{SaveLoadState, UiWorld};
use common::descriptions::BuildingGen;
use common::saveload::Encoder;
use egui::{
Align2, Color32, Context, Frame, Id, LayerId, Response, RichText, Rounding, Stroke, Style, Ui,
Expand All @@ -20,7 +21,7 @@ use serde::{Deserialize, Serialize};
use simulation::economy::{Government, Item, ItemRegistry, Money};
use simulation::engine_interaction::WorldCommand;
use simulation::map::{
BuildingGen, BuildingKind, LanePatternBuilder, LightPolicy, MapProject, TurnPolicy, Zone,
BuildingKind, LanePatternBuilder, LightPolicy, MapProject, TurnPolicy, Zone,
};
use simulation::souls::goods_company::GoodsCompanyRegistry;
use simulation::utils::time::{GameTime, SECONDS_PER_HOUR};
Expand Down
5 changes: 3 additions & 2 deletions simulation/src/engine_interaction.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::time::Instant;

use common::descriptions::BuildingGen;
use serde::{Deserialize, Serialize};

use geom::{vec3, Vec2, OBB};
Expand All @@ -9,8 +10,8 @@ use WorldCommand::*;
use crate::economy::Government;
use crate::map::procgen::{load_parismap, load_testfield};
use crate::map::{
BuildingGen, BuildingID, BuildingKind, IntersectionID, LaneID, LanePattern, LanePatternBuilder,
LightPolicy, LotID, Map, MapProject, ProjectKind, RoadID, Terrain, TurnPolicy, Zone,
BuildingID, BuildingKind, IntersectionID, LaneID, LanePattern, LanePatternBuilder, LightPolicy,
LotID, Map, MapProject, ProjectKind, RoadID, Terrain, TurnPolicy, Zone,
};
use crate::map_dynamic::{BuildingInfos, ParkingManagement};
use crate::multiplayer::chat::Message;
Expand Down
5 changes: 2 additions & 3 deletions simulation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,8 @@ const START_COMMANDS: &str = r#"
},
"kind": "RailFreightStation",
"gen": {
"NoWalkway": {
"door_pos": 0
}
"kind": "no_walkway",
"door_pos": 0
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions simulation/src/map/map.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::map::serializing::SerializedMap;
use crate::map::{
Building, BuildingGen, BuildingID, BuildingKind, Intersection, IntersectionID, Lane, LaneID,
LaneKind, LanePattern, Lot, LotID, LotKind, MapSubscriber, MapSubscribers, ParkingSpotID,
ParkingSpots, ProjectFilter, ProjectKind, Road, RoadID, RoadSegmentKind, SpatialMap, Terrain,
UpdateType, Zone,
Building, BuildingID, BuildingKind, Intersection, IntersectionID, Lane, LaneID, LaneKind,
LanePattern, Lot, LotID, LotKind, MapSubscriber, MapSubscribers, ParkingSpotID, ParkingSpots,
ProjectFilter, ProjectKind, Road, RoadID, RoadSegmentKind, SpatialMap, Terrain, UpdateType,
Zone,
};
use common::descriptions::BuildingGen;
use geom::OBB;
use geom::{Spline3, Vec2, Vec3};
use ordered_float::OrderedFloat;
Expand Down
13 changes: 1 addition & 12 deletions simulation/src/map/objects/building.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::map::procgen::{gen_exterior_farm, gen_exterior_house, ColoredMesh};
use crate::map::{Buildings, LanePattern, SpatialMap, Terrain};
use crate::souls::goods_company::GoodsCompanyID;
use common::descriptions::BuildingGen;
use egui_inspect::debug_inspect_impl;
use geom::{Color, Polygon, Vec2, Vec3, OBB};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -37,18 +38,6 @@ impl BuildingKind {
}
}

#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub enum BuildingGen {
House,
Farm,
CenteredDoor {
vertical_factor: f32, // 1.0 means that the door is at the bottom, just on the street
},
NoWalkway {
door_pos: Vec2, // door_pos is relative to the center of the building
},
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StraightRoadGen {
pub from: Vec3,
Expand Down
Loading

0 comments on commit 40fc7e0

Please sign in to comment.