diff options
Diffstat (limited to 'src/grids.adb')
-rw-r--r-- | src/grids.adb | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/grids.adb b/src/grids.adb new file mode 100644 index 0000000..64f0c34 --- /dev/null +++ b/src/grids.adb @@ -0,0 +1,117 @@ + + +package body Grids is + + + function Create + (X, Y, W, H : in Integer; + Text : in String) + return Grid is + begin + return This : Grid := + (FLTK.Widgets.Widget'(FLTK.Widgets.Create (X, Y, W, H, Text)) with + Cells => Square_Vector_Vectors.Empty_Vector, + Rows => 0, Cols => 0); + end Create; + + + + + function Create + (X, Y : in Integer) + return Grid is + begin + return Create (X, Y, 0, 0, "Sokoban Grid"); + end Create; + + + + + procedure Draw + (This : in out Grid) is + begin + for Y in Integer range 1 .. This.Rows loop + for X in Integer range 1 .. This.Cols loop + This.Cells.Reference (X).Reference (Y).Draw + (This.Get_X + (X - 1) * Step, + This.Get_Y + (Y - 1) * Step); + end loop; + end loop; + end Draw; + + + + + procedure Set_Cols + (This : in out Grid; + To : in Natural) is + begin + This.Cells.Set_Length (Ada.Containers.Count_Type (To)); + for X in Integer range This.Cols + 1 .. To loop + This.Cells.Replace_Element (X, Square_Vectors.Empty_Vector); + This.Cells.Reference (X).Set_Length (Ada.Containers.Count_Type (This.Rows)); + for Y in Integer range 1 .. This.Rows loop + This.Cells.Reference (X).Replace_Element (Y, Squares.Void); + end loop; + end loop; + This.Cols := To; + This.Resize (Step * This.Cols, This.Get_H); + end Set_Cols; + + + + + procedure Set_Rows + (This : in out Grid; + To : in Natural) is + begin + for X in Integer range 1 .. This.Cols loop + This.Cells.Reference (X).Set_Length (Ada.Containers.Count_Type (To)); + for Y in Integer range This.Rows + 1 .. To loop + This.Cells.Reference (X).Replace_Element (Y, Squares.Void); + end loop; + end loop; + This.Rows := To; + This.Resize (This.Get_W, Step * This.Rows); + end Set_Rows; + + + + + function In_Bounds + (This : in Grid; + X, Y : in Integer) + return Boolean is + begin + return X in 1 .. This.Cols and Y in 1 .. This.Rows; + end In_Bounds; + + + + + function Get_Square + (This : in Grid; + X, Y : in Integer) + return Squares.Square is + begin + if not This.In_Bounds (X, Y) then + return Squares.Void; + else + return This.Cells.Element (X).Element (Y); + end if; + end Get_Square; + + + + + procedure Set_Square + (This : in out Grid; + X, Y : in Integer; + Item : in Squares.Square) is + begin + This.Cells.Reference (X).Reference (Y) := Item; + end Set_Square; + + +end Grids; + |