Add init flags to Offset and set vertical offset

Add `value_initiated` and `max_initiated` flags to prevent
repeated assignments. Provide `set_initial_value` for one‑time
initialisation and use it to centre the vertical offset when
creating the board widget. Remove stale comments on cell size constants.
This commit is contained in:
2026-03-28 14:35:05 +01:00
parent ba0028b7a7
commit edf491457e
2 changed files with 28 additions and 4 deletions
+19 -2
View File
@@ -5,23 +5,40 @@ use clap::ValueEnum;
pub struct Offset {
value: usize,
max: usize,
value_initiated: bool,
max_initiated: bool,
}
impl Offset {
pub fn new() -> Self {
Self { value: 0, max: 0 }
Self {
value: 0,
max: 0,
value_initiated: false,
max_initiated: false,
}
}
pub fn get_value(&self) -> usize {
self.value
}
pub fn set_initial_value(&mut self, value: usize) {
if self.value_initiated {
return;
}
self.value = value;
self.value_initiated = true;
}
pub fn set_max(&mut self, max: usize) {
if self.max != 0 {
if self.max_initiated {
return;
}
self.max = max;
self.max_initiated = true;
}
pub fn next(&mut self) {
+9 -2
View File
@@ -19,8 +19,8 @@ pub struct BoardWidget<'a> {
impl<'a> BoardWidget<'a> {
pub fn new(app: &'a mut App, area_width: u16, area_height: u16) -> Self {
const CELL_HIGHT: u16 = 5; // 4
const CELL_WIDTH: u16 = 9; // 7
const CELL_HIGHT: u16 = 5;
const CELL_WIDTH: u16 = 9;
let rows: u16 = area_height / CELL_HIGHT;
let cols: u16 = area_width / CELL_WIDTH;
@@ -40,6 +40,13 @@ impl<'a> BoardWidget<'a> {
app.states.skirmish.horizontal_offset.set_max(h_max_offset);
app.states.skirmish.vertical_offset.set_max(v_max_offset);
if v_max_offset > 0 {
app.states
.skirmish
.vertical_offset
.set_initial_value((app.states.skirmish.map_height - rows as usize) / 2);
}
Self {
map_width: app.states.skirmish.map_width as usize,
cell_width: CELL_WIDTH,