|
| 1 | +use std::collections::HashSet; |
| 2 | + |
| 3 | +use crate::sbc::command_system::command::Command; |
| 4 | +use crate::sbc::heightmap::commands::set_heightmap_brush_command::SetHeightmapBrushCommand; |
| 5 | +use crate::sbc::heightmap::commands::terrain_level_command::{ |
| 6 | + Opts as LevelOpts, TerrainLevelCommand, |
| 7 | +}; |
| 8 | +use crate::sbc::heightmap::commands::terrain_shape_modify_command::{ |
| 9 | + Opts as ShapeOpts, TerrainShapeModifyCommand, |
| 10 | +}; |
| 11 | +use crate::sbc::heightmap::commands::terrain_smooth_command::{ |
| 12 | + Opts as SmoothOpts, TerrainSmoothCommand, |
| 13 | +}; |
| 14 | +use crate::sbc::states::shapes::{brush_opts, load_shape}; |
| 15 | +use crate::sbc::states::state::StateContext; |
| 16 | +use crate::sbc::states::{BrushButton, BrushSettings, BrushStamp, MapBrush}; |
| 17 | + |
| 18 | +pub(crate) static SHAPE_MODIFY: ShapeModify = ShapeModify; |
| 19 | +pub(crate) static LEVEL: Level = Level; |
| 20 | +pub(crate) static SMOOTH: Smooth = Smooth; |
| 21 | + |
| 22 | +pub(crate) struct ShapeModify; |
| 23 | +pub(crate) struct Level; |
| 24 | +pub(crate) struct Smooth; |
| 25 | + |
| 26 | +fn centre(stamp: BrushStamp) -> (f32, f32) { |
| 27 | + (stamp.x + stamp.size / 2.0, stamp.z + stamp.size / 2.0) |
| 28 | +} |
| 29 | + |
| 30 | +fn signed_strength(brush: &BrushSettings, button: BrushButton) -> f32 { |
| 31 | + if button.is_secondary() { |
| 32 | + -brush.strength |
| 33 | + } else { |
| 34 | + brush.strength |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub(crate) fn prepare_pattern( |
| 39 | + brush: &BrushSettings, |
| 40 | + uploaded: &mut HashSet<String>, |
| 41 | + ctx: &mut StateContext, |
| 42 | +) -> bool { |
| 43 | + let Some(pattern) = brush.pattern_texture.as_deref() else { |
| 44 | + return false; |
| 45 | + }; |
| 46 | + if uploaded.contains(pattern) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + let Some(shape) = load_shape(ctx.interface, pattern) else { |
| 50 | + log::warn!("heightmap brush pattern {pattern} could not be loaded"); |
| 51 | + return false; |
| 52 | + }; |
| 53 | + ctx.command(Box::new(SetHeightmapBrushCommand::new(brush_opts( |
| 54 | + pattern, &shape, |
| 55 | + )))); |
| 56 | + uploaded.insert(pattern.to_string()); |
| 57 | + true |
| 58 | +} |
| 59 | + |
| 60 | +impl MapBrush for ShapeModify { |
| 61 | + fn name(&self) -> &'static str { |
| 62 | + "terrain-shape-modify" |
| 63 | + } |
| 64 | + |
| 65 | + fn prepare( |
| 66 | + &self, |
| 67 | + brush: &BrushSettings, |
| 68 | + uploaded: &mut HashSet<String>, |
| 69 | + ctx: &mut StateContext, |
| 70 | + ) -> bool { |
| 71 | + prepare_pattern(brush, uploaded, ctx) |
| 72 | + } |
| 73 | + |
| 74 | + fn command( |
| 75 | + &self, |
| 76 | + brush: &BrushSettings, |
| 77 | + stamp: BrushStamp, |
| 78 | + button: BrushButton, |
| 79 | + ) -> Box<dyn Command> { |
| 80 | + let (x, z) = centre(stamp); |
| 81 | + Box::new(TerrainShapeModifyCommand::new(ShapeOpts { |
| 82 | + rotation: stamp.rotation, |
| 83 | + x, |
| 84 | + z, |
| 85 | + shape_name: brush.pattern_texture.clone().unwrap_or_default(), |
| 86 | + strength: signed_strength(brush, button), |
| 87 | + size: stamp.size, |
| 88 | + })) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl MapBrush for Level { |
| 93 | + fn name(&self) -> &'static str { |
| 94 | + "terrain-level" |
| 95 | + } |
| 96 | + |
| 97 | + fn prepare( |
| 98 | + &self, |
| 99 | + brush: &BrushSettings, |
| 100 | + uploaded: &mut HashSet<String>, |
| 101 | + ctx: &mut StateContext, |
| 102 | + ) -> bool { |
| 103 | + prepare_pattern(brush, uploaded, ctx) |
| 104 | + } |
| 105 | + |
| 106 | + fn consume_press(&self, brush: &mut BrushSettings, button: BrushButton, height: f32) -> bool { |
| 107 | + if button.is_secondary() { |
| 108 | + brush.set_height(height); |
| 109 | + true |
| 110 | + } else { |
| 111 | + false |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + fn command( |
| 116 | + &self, |
| 117 | + brush: &BrushSettings, |
| 118 | + stamp: BrushStamp, |
| 119 | + button: BrushButton, |
| 120 | + ) -> Box<dyn Command> { |
| 121 | + let (x, z) = centre(stamp); |
| 122 | + Box::new(TerrainLevelCommand::new(LevelOpts { |
| 123 | + rotation: stamp.rotation, |
| 124 | + x, |
| 125 | + z, |
| 126 | + shape_name: brush.pattern_texture.clone().unwrap_or_default(), |
| 127 | + strength: signed_strength(brush, button), |
| 128 | + size: stamp.size, |
| 129 | + height: brush.height, |
| 130 | + apply_dir_id: brush.apply_dir.id(), |
| 131 | + })) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +impl MapBrush for Smooth { |
| 136 | + fn name(&self) -> &'static str { |
| 137 | + "terrain-smooth" |
| 138 | + } |
| 139 | + |
| 140 | + fn prepare( |
| 141 | + &self, |
| 142 | + brush: &BrushSettings, |
| 143 | + uploaded: &mut HashSet<String>, |
| 144 | + ctx: &mut StateContext, |
| 145 | + ) -> bool { |
| 146 | + prepare_pattern(brush, uploaded, ctx) |
| 147 | + } |
| 148 | + |
| 149 | + fn command( |
| 150 | + &self, |
| 151 | + brush: &BrushSettings, |
| 152 | + stamp: BrushStamp, |
| 153 | + button: BrushButton, |
| 154 | + ) -> Box<dyn Command> { |
| 155 | + let (x, z) = centre(stamp); |
| 156 | + let strength = signed_strength(brush, button).abs(); |
| 157 | + let sigma = (strength.sqrt().sqrt() / 2.0).clamp(0.20, 1.5); |
| 158 | + Box::new(TerrainSmoothCommand::new(SmoothOpts { |
| 159 | + rotation: stamp.rotation, |
| 160 | + x, |
| 161 | + z, |
| 162 | + shape_name: brush.pattern_texture.clone().unwrap_or_default(), |
| 163 | + strength, |
| 164 | + size: stamp.size, |
| 165 | + sigma, |
| 166 | + })) |
| 167 | + } |
| 168 | +} |
0 commit comments