add comment storage to boards

This commit is contained in:
Crispy 2025-03-28 23:06:27 +01:00
parent 0b9f41cbf6
commit 5c48b531f6
13 changed files with 219 additions and 113 deletions

View file

@ -1,4 +1,5 @@
use raylib::prelude::*;
use serde::{Deserialize, Serialize};
use super::{tile::*, Pos, PosInt};
use crate::{
@ -6,8 +7,9 @@ use crate::{
util::{draw_scaled_texture, Textures},
};
#[derive(Debug, Clone, PartialEq)]
pub struct Board {
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(into = "String", from = "String")]
pub struct Grid {
tiles: Vec<Tile>,
width: usize,
height: usize,
@ -21,7 +23,7 @@ pub struct ResizeDeltas {
pub y_neg: usize,
}
impl Board {
impl Grid {
pub fn parse(source: &str) -> Self {
let mut rows = Vec::new();
@ -48,7 +50,7 @@ impl Board {
}
}
pub fn serialize(&self) -> String {
pub fn to_str(&self) -> String {
let mut out = String::new();
for y in 0..self.height {
for x in 0..self.width {
@ -125,7 +127,7 @@ impl Board {
}
}
pub fn paste_board(&mut self, pos: Pos, source: &Board) {
pub fn paste_grid(&mut self, pos: Pos, source: &Grid) {
for x in 0..source.width() {
for y in 0..source.height() {
let offset = (x, y).into();
@ -136,8 +138,8 @@ impl Board {
}
}
pub fn get_rect(&self, pos: Pos, width: usize, height: usize) -> Board {
let mut out = Board::new_empty(width, height);
pub fn get_rect(&self, pos: Pos, width: usize, height: usize) -> Grid {
let mut out = Grid::new_empty(width, height);
for x in 0..width {
for y in 0..height {
let offset = (x, y).into();
@ -152,27 +154,27 @@ impl Board {
pub fn grow(&mut self, deltas: &ResizeDeltas) {
let new_width = self.width + deltas.x_neg + deltas.x_pos;
let new_height = self.height + deltas.y_neg + deltas.y_pos;
let mut new_board = Board::new_empty(new_width, new_height);
let mut new_grid = Grid::new_empty(new_width, new_height);
for x in 0..self.width {
for y in 0..self.height {
let tile = self.get_unchecked((x, y).into());
new_board.set((x + deltas.x_neg, y + deltas.y_neg).into(), tile);
new_grid.set((x + deltas.x_neg, y + deltas.y_neg).into(), tile);
}
}
*self = new_board;
*self = new_grid;
}
pub fn shrink(&mut self, deltas: &ResizeDeltas) {
let new_width = self.width - deltas.x_neg - deltas.x_pos;
let new_height = self.height - deltas.y_neg - deltas.y_pos;
let mut new_board = Board::new_empty(new_width, new_height);
let mut new_grid = Grid::new_empty(new_width, new_height);
for x in 0..new_width {
for y in 0..new_height {
let tile = self.get_unchecked((x + deltas.x_neg, y + deltas.y_neg).into());
new_board.set((x, y).into(), tile);
new_grid.set((x, y).into(), tile);
}
}
*self = new_board;
*self = new_grid;
}
pub fn width(&self) -> usize {
@ -231,3 +233,15 @@ impl Board {
}
}
}
impl From<String> for Grid {
fn from(value: String) -> Self {
Self::parse(&value)
}
}
impl From<Grid> for String {
fn from(val: Grid) -> String {
val.to_str()
}
}

View file

@ -1,10 +1,11 @@
use std::ops::Add;
use raylib::prelude::*;
use serde::{Deserialize, Serialize};
pub type PosInt = i16;
#[derive(Debug, Default, Clone, Copy, PartialEq)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub struct Pos {
pub x: PosInt,
pub y: PosInt,