generated from GarandPLG/rust-flake-template
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:
@@ -5,23 +5,40 @@ use clap::ValueEnum;
|
|||||||
pub struct Offset {
|
pub struct Offset {
|
||||||
value: usize,
|
value: usize,
|
||||||
max: usize,
|
max: usize,
|
||||||
|
value_initiated: bool,
|
||||||
|
max_initiated: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Offset {
|
impl Offset {
|
||||||
pub fn new() -> Self {
|
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 {
|
pub fn get_value(&self) -> usize {
|
||||||
self.value
|
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) {
|
pub fn set_max(&mut self, max: usize) {
|
||||||
if self.max != 0 {
|
if self.max_initiated {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.max = max;
|
self.max = max;
|
||||||
|
self.max_initiated = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&mut self) {
|
pub fn next(&mut self) {
|
||||||
|
|||||||
@@ -19,8 +19,8 @@ pub struct BoardWidget<'a> {
|
|||||||
|
|
||||||
impl<'a> BoardWidget<'a> {
|
impl<'a> BoardWidget<'a> {
|
||||||
pub fn new(app: &'a mut App, area_width: u16, area_height: u16) -> Self {
|
pub fn new(app: &'a mut App, area_width: u16, area_height: u16) -> Self {
|
||||||
const CELL_HIGHT: u16 = 5; // 4
|
const CELL_HIGHT: u16 = 5;
|
||||||
const CELL_WIDTH: u16 = 9; // 7
|
const CELL_WIDTH: u16 = 9;
|
||||||
|
|
||||||
let rows: u16 = area_height / CELL_HIGHT;
|
let rows: u16 = area_height / CELL_HIGHT;
|
||||||
let cols: u16 = area_width / CELL_WIDTH;
|
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.horizontal_offset.set_max(h_max_offset);
|
||||||
app.states.skirmish.vertical_offset.set_max(v_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 {
|
Self {
|
||||||
map_width: app.states.skirmish.map_width as usize,
|
map_width: app.states.skirmish.map_width as usize,
|
||||||
cell_width: CELL_WIDTH,
|
cell_width: CELL_WIDTH,
|
||||||
|
|||||||
Reference in New Issue
Block a user