summaryrefslogtreecommitdiff
path: root/src/grids.adb
blob: 12f1ae4ce7b45045dc6a1c000c4c10e9a1b42725 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131


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.Forge.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 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;




    procedure Pixel_To_Colrow
           (This : in     Grid;
            X, Y : in     Integer;
            C, R :    out Integer) is
    begin
        C := X / Step + 1;
        R := Y / Step + 1;
    end Pixel_To_Colrow;


    procedure Colrow_To_Pixel
           (This : in     Grid;
            C, R : in     Integer;
            X, Y :    out Integer) is
    begin
        X := (C - 1) * Step;
        Y := (R - 1) * Step;
    end Colrow_To_Pixel;




    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;


end Grids;