summaryrefslogtreecommitdiff
path: root/src/displays.adb
blob: e7d241b53fa8bf5ba6e42c878d926d7b55954041 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194


with

    FLTK.Screen,
    FLTK.Event,
    Misc;


package body Displays is


    package WD renames FLTK.Widgets.Groups.Windows.Double;
    package B renames FLTK.Widgets.Boxes;




    function Create
           (X, Y, W, H : in Integer;
            Text       : in String)
        return Display
    is
        My_Width  : Integer := Misc.Max (Message_Box_Width, W);
        My_Height : Integer := Misc.Max (Message_Box_Height + Stat_Box_Height, H);
    begin
        return This : Display :=
                   (WD.Double_Window'(WD.Forge.Create (X, Y, My_Width, My_Height, Text)) with

                    Message_Box => B.Box'(B.Forge.Create
                        (0, 0, Message_Box_Width, Message_Box_Height, "")),
                    Level_Box => B.Box'(B.Forge.Create
                        (0, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, "")),
                    Move_Box => B.Box'(B.Forge.Create
                        (Stat_Box_Width, Message_Box_Height, Stat_Box_Width, Stat_Box_Height, "")),
                    Current_Grid => null,
                    Key_Func => null,
                    Mouse_Func => null) do

            This.Add (This.Message_Box);
            This.Add (This.Level_Box);
            This.Add (This.Move_Box);
            This.Message_Box.Set_Label_Size (Text_Size);
            This.Level_Box.Set_Label_Size (Text_Size);
            This.Move_Box.Set_Label_Size (Text_Size);

            This.Set_Icon (Logo);
        end return;
    end Create;


    function Create
           (W, H : in Integer)
        return Display is
    begin
        return Create (0, 0, W, H, "Sokoban");
    end Create;


    function Create
        return Display is
    begin
        return Create (0, 0, Message_Box_Width, Message_Box_Height, "Sokoban");
    end Create;




    procedure Set_Grid
           (This : in out Display;
            To   : in out Grids.Grid) is
    begin
        if This.Current_Grid /= null then
            This.Remove (This.Current_Grid.all);
        end if;
        This.Current_Grid := To'Unchecked_Access;
        This.Add (To);
        This.Ensure_Correct_Size;
    end Set_Grid;


    procedure Adjust_Grid
           (This       : in out Display;
            Cols, Rows : in     Natural) is
    begin
        This.Current_Grid.Set_Cols (Cols);
        This.Current_Grid.Set_Rows (Rows);
        This.Ensure_Correct_Size;
    end Adjust_Grid;


    procedure Ensure_Correct_Size
           (This : in out Display)
    is
        New_Width : Integer :=
            Misc.Max (Message_Box_Width, This.Current_Grid.Get_W);
        New_Height : Integer :=
            Message_Box_Height + This.Current_Grid.Get_H + Stat_Box_Height;
        Grid_X : Integer :=
            Misc.Max (0, (Message_Box_Width - This.Current_Grid.Get_W) / 2);
    begin
        This.Current_Grid.Reposition (Grid_X, 0);
        This.Message_Box.Resize (New_Width, This.Message_Box.Get_H);
        This.Message_Box.Reposition (This.Message_Box.Get_X, This.Current_Grid.Get_H);
        This.Level_Box.Resize (New_Width / 2, This.Level_Box.Get_H);
        This.Level_Box.Reposition (0, New_Height - Stat_Box_Height);
        This.Move_Box.Resize (New_Width / 2, This.Move_Box.Get_H);
        This.Move_Box.Reposition (New_Width / 2, New_Height - Stat_Box_Height);
        This.Resize (New_Width, New_Height);
    end Ensure_Correct_Size;


    procedure Centre_On_Screen
           (This : in out Display)
    is
        use FLTK.Screen;
    begin
        This.Reposition
           (Get_X + (Get_W - This.Get_W) / 2,
            Get_Y + (Get_H - This.Get_H) / 2);
    end Centre_On_Screen;




    procedure Set_Message
           (This : in out Display;
            Msg  : in     String) is
    begin
        This.Message_Box.Set_Label (Msg);
    end Set_Message;


    procedure Set_Level_Number
           (This : in out Display;
            To   : in     Natural) is
    begin
        This.Level_Box.Set_Label ("Level:" & Natural'Image (To));
    end Set_Level_Number;


    procedure Set_Move_Number
           (This : in out Display;
            To   : in     Natural) is
    begin
        This.Move_Box.Set_Label ("Moves:" & Natural'Image (To));
    end Set_Move_Number;




    procedure Set_Keyboard_Callback
           (This : in out Display;
            Func : in     Keyboard_Callback) is
    begin
        This.Key_Func := Func;
    end Set_Keyboard_Callback;


    procedure Set_Mouse_Callback
           (This : in out Display;
            Func : in     Mouse_Callback) is
    begin
        This.Mouse_Func := Func;
    end Set_Mouse_Callback;




    function Handle
           (This  : in out Display;
            Event : in     FLTK.Event_Kind)
        return FLTK.Event_Outcome
    is
        use type FLTK.Event_Kind, FLTK.Event_Outcome;
    begin
        if  This.Key_Func /= null and then Event = FLTK.Keydown and then
            This.Key_Func (FLTK.Press (FLTK.Event.Last_Key)) = FLTK.Handled
        then
            return FLTK.Handled;

        elsif This.Mouse_Func /= null and then Event = FLTK.Release and then
            This.Mouse_Func (FLTK.Event.Mouse_X, FLTK.Event.Mouse_Y) = FLTK.Handled
        then
            return FLTK.Handled;

        else
            return WD.Double_Window (This).Handle (Event);
        end if;
    end Handle;


end Displays;