1 with Ada
.Text_IO
; use Ada
.Text_IO
;
2 with Ada
.Float_Text_IO
; use Ada
.Float_Text_IO
;
3 with Simulator
.Car
; use Simulator
.Car
;
4 with Simulator
.Gui
; use Simulator
.Gui
;
9 with Gtk
.Scrolled_Window
; use Gtk
.Scrolled_Window
;
10 with Gtk
.Frame
; use Gtk
.Frame
;
11 with Gtk
.Separator
; use Gtk
.Separator
;
12 with Gtk
.Enums
; use Gtk
.Enums
;
13 with Gtk
.Alignment
; use Gtk
.Alignment
;
14 with Gtk
.Box
; use Gtk
.Box
;
15 with Gtk
.Table
; use Gtk
.Table
;
16 with Gtk
.Text_Buffer
; Use Gtk
.Text_Buffer
;
17 with Gtk
.Text_Iter
; use Gtk
.Text_Iter
;
18 with Gtk
.Text_Mark
; use Gtk
.Text_Mark
;
20 package body Simulator
.Car
.Gui
is
22 procedure Init_Car_Gui
is
29 align
: Gtk_Alignment
;
32 LabelName
: Gtk_Label
;
33 LabelTyres
: Gtk_Label
;
34 LabelPerformance
: Gtk_Label
;
35 LabelFuel
: Gtk_Label
;
36 LabelFuelLimit
: Gtk_Label
;
37 LabelTyresConsum
: Gtk_Label
;
38 LabelTyresConsumLimit
: Gtk_Label
;
39 Scroller
: Gtk_Scrolled_Window
;
42 -- Inizializzo GTK e la finestra principale
46 Gtk
.Window
.Gtk_New
(Window
);
47 -- Bordo della finestra e bottone centrale
48 Gtk
.Window
.Set_Border_Width
(Window
, 5);
49 Set_Size_Request
(Window
, 470, 500);
51 Gtk_New
(Table
, 8, 2, False);
52 Gtk_New
(LabelId
, "Id:");
54 Gtk_New
(LabelName
, "Name:");
56 Set_Max_Length
(ValName
, 15);
57 Set_Text
(ValName
, "pilot");
58 Gtk_New
(LabelTyres
, "Tyres type:");
59 Gtk_New_Text
(ValTyres
);
60 Append_Text
(ValTyres
, "Slick");
61 Append_Text
(ValTyres
, "Rain");
62 Set_Active
(ValTyres
, 0);
63 Gtk_New
(LabelPerformance
, "Performance:");
64 Gtk_New_Hscale
(ValPerformance
, 1.0, 100.0, 1.0);
65 Set_Size_Request
(ValPerformance
, 150);
66 Gtk_New
(LabelFuel
, "Fuel:");
67 Gtk_New_Hscale
(ValFuel
, 0.0, 150.0, 1.0);
68 Set_Value
(ValFuel
, 50.0);
69 Set_Size_Request
(ValFuel
, 150);
70 Gtk_New
(LabelFuelLimit
, "Fuel Limit:");
71 Gtk_New_Hscale
(ValFuelLimit
, 0.0, 150.0, 1.0);
72 Set_Value
(ValFuelLimit
, 10.0);
73 Set_Size_Request
(ValFuel
, 150);
74 Gtk_New
(LabelTyresConsum
, "Tyres Consumption:");
75 Gtk_New_Hscale
(ValTyresConsum
, 0.0, 1.0, 0.01);
76 Set_Size_Request
(ValTyresConsum
, 150);
77 Set_Sensitive
(ValTyresConsum
, False);
78 Gtk_New
(LabelTyresConsumLimit
, "Tyres Limit:");
79 Gtk_New_Hscale
(ValTyresConsumLimit
, 0.0, 1.0, 0.01);
80 Set_Value
(ValTyresConsumLimit
, 0.9);
81 Set_Size_Request
(ValTyresConsumLimit
, 150);
83 Gtk_New_Text
(ValTyresBox
);
84 Append_Text
(ValTyresBox
, "Slick");
85 Append_Text
(ValTyresBox
, "Rain");
86 Set_Active
(ValTyresBox
, 0);
87 Gtk_New
(ValFuelBox
, 0.0, 150.0, 1.0);
88 Set_Value
(ValFuelBox
, 30.0);
89 Gtk_New
(ButtBox
, "Force PitStop");
90 Set_Sensitive
(ButtBox
, False);
91 Gtk_New
(LabelUsage
, "<b>Lap usage</b>:" & ASCII
.LF
&" Fuel: --" & ASCII
.LF
& " Tyres: --");
92 Set_Use_Markup
(LabelUsage
, true);
93 Gtk_New
(ButtKill
, "Retire Car");
95 Gtk_New
(Button
, "Start Car");
98 Set_Policy
(Scroller
, Policy_Automatic
, Policy_Always
);
100 Set_Editable
(Console
, False);
101 Set_Cursor_Visible
(Console
, False);
102 Set_Overwrite
(Console
, False);
103 Set_Wrap_Mode
(Console
, Wrap_Word
);
105 -- impacchetto nel layout
106 Gtk_New_Vbox
(VBox1
, False, 5);
107 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
109 Attach
(Table
,align
, 0, 1, 0, 1, Fill
, Fill
, 5, 5);
110 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
112 Attach
(Table
,align
, 1, 2, 0, 1, Fill
, Fill
, 5, 5);
113 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
114 Add
(align
, LabelName
);
115 Attach
(Table
,align
, 0, 1, 1, 2, Fill
, Fill
, 5, 5);
116 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
118 Attach
(Table
,align
, 1, 2, 1, 2, Fill
, Fill
, 5, 5);
119 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
120 Add
(align
, LabelTyres
);
121 Attach
(Table
, align
, 0, 1, 2, 3, Fill
, Fill
, 5, 5);
122 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
123 Add
(align
, ValTyres
);
124 Attach
(Table
,align
, 1, 2, 2, 3, Fill
, Fill
, 5, 5);
125 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
126 Add
(align
, LabelPerformance
);
127 Attach
(Table
, align
, 0, 1, 3, 4, Fill
, Fill
, 5, 5);
128 Gtk_New
(align
, 0.0, 0.5, 1.0, 0.0);
129 Add
(align
, ValPerformance
);
130 Attach
(Table
,align
, 1, 2, 3, 4, Fill
, Fill
, 5, 5);
131 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
132 Add
(align
, LabelFuel
);
133 Attach
(Table
, align
, 0, 1, 4, 5, Fill
, Fill
, 5, 5);
134 Gtk_New
(align
, 0.0, 0.5, 1.0, 0.0);
136 Attach
(Table
,align
, 1, 2, 4, 5, Fill
, Fill
, 5, 5);
137 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
138 Add
(align
, LabelFuelLimit
);
139 Attach
(Table
, align
, 0, 1, 5, 6, Fill
, Fill
, 5, 5);
140 Gtk_New
(align
, 0.0, 0.5, 1.0, 0.0);
141 Add
(align
, ValFuelLimit
);
142 Attach
(Table
,align
, 1, 2, 5, 6, Fill
, Fill
, 5, 5);
143 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
144 Add
(align
, LabelTyresConsum
);
145 Attach
(Table
, align
, 0, 1, 6, 7, Fill
, Fill
, 5, 5);
146 Gtk_New
(align
, 0.0, 0.5, 1.0, 0.0);
147 Add
(align
, ValTyresConsum
);
148 Attach
(Table
,align
, 1, 2, 6, 7, Fill
, Fill
, 5, 5);
149 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
150 Add
(align
, LabelTyresConsumLimit
);
151 Attach
(Table
, align
, 0, 1, 7, 8, Fill
, Fill
, 5, 5);
152 Gtk_New
(align
, 0.0, 0.5, 1.0, 0.0);
153 Add
(align
, ValTyresConsumLimit
);
154 Attach
(Table
,align
, 1, 2, 7, 8, Fill
, Fill
, 5, 5);
156 Gtk_New
(TableBox
, 7, 2, False);
157 Gtk_New
(Label
, "Tyres:");
158 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
160 Attach
(TableBox
, align
, 0, 1, 0, 1, Fill
, Fill
, 5, 5);
161 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
162 Add
(align
, ValTyresBox
);
163 Attach
(TableBox
, align
, 1, 2, 0, 1, Fill
, Fill
, 5, 5);
164 Gtk_New
(Label
, "Fuel:");
165 Gtk_New
(align
, 1.0, 0.5, 0.0, 0.0);
167 Attach
(TableBox
, align
, 0, 1, 1, 2, Fill
, Fill
, 5, 5);
168 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
169 Add
(align
, ValFuelBox
);
170 Attach
(TableBox
, align
, 1, 2, 1, 2, Fill
, Fill
, 5, 5);
171 Gtk_New
(align
, 0.5, 0.5, 0.0, 0.0);
173 Attach
(TableBox
, align
, 0, 2, 2, 3, Fill
, Fill
, 5, 5);
174 Gtk_New
(align
, 0.5, 0.5, 0.0, 0.0);
175 Gtk_New_Hseparator
(Sep
);
177 Set_Size_Request
(Sep
, 50);
178 Attach
(TableBox
, align
, 0, 2, 3, 4, Fill
, Fill
, 5, 5);
179 Gtk_New
(align
, 0.0, 0.5, 0.0, 0.0);
180 Add
(align
, LabelUsage
);
181 Attach
(TableBox
, align
, 0, 2, 4, 5, Fill
, Fill
, 5, 5);
182 Gtk_New
(align
, 0.5, 0.5, 0.0, 0.0);
183 Gtk_New_Hseparator
(Sep
);
185 Set_Size_Request
(Sep
, 50);
186 Attach
(TableBox
, align
, 0, 2, 5, 6, Fill
, Fill
, 5, 5);
187 Gtk_New
(align
, 0.5, 0.8, 0.0, 0.0);
188 Add
(align
, ButtKill
);
189 Attach
(TableBox
, align
, 0, 2, 6, 7, Fill
, Expand
, 5, 5);
191 Gtk_New
(Frame
, "Console");
192 Add
(Frame
, TableBox
);
193 Gtk_New_Hbox
(HBox
, False, 5);
194 Pack_Start
(HBox
, Table
, True, True, 5);
195 Pack_Start
(HBox
, Frame
, True, True, 5);
197 Pack_Start
(VBox1
, HBox
, False, True, 5);
198 Pack_Start
(VBox1
, Button
, False, True, 5);
199 Add
(Scroller
, Console
);
200 Pack_Start
(VBox1
, Scroller
, True, True, 5);
204 -- Connessione segnali
205 -- collego il segnale di chiusura del Window Manager alla funzione Delete_Event
206 Return_Handlers
.Connect
(Window
, "delete_event", Return_Handlers
.To_Marshaller
(Delete_Event
'Access));
207 -- callback chiamato quando si invoca "destroy" su Window, o quando
208 -- la funzione Delete_Event ritorna false
209 Handlers
.Connect
(Window
, "destroy", Handlers
.To_Marshaller
(Destroy
'Access));
210 -- callback del tasto start
211 Handlers
.Connect
(Button
, "clicked", Handlers
.To_Marshaller
(Start_Car
'Access));
212 -- callback del tasto PitStop
213 Handlers
.Connect
(ButtBox
, "clicked", Handlers
.To_Marshaller
(PitStop
'Access));
214 -- callback del tasto Retire_Car
215 Handlers
.Connect
(ButtKill
, "clicked", Handlers
.To_Marshaller
(Retire_Car
'Access));
216 -- Rendo tutto visibile
219 -- Metto in attesa l'applicazione nel loop GTK
224 Put_Line
("GUI: terminato Gtk.Main.Main di auto. Termino l'applicazione.");
227 procedure Print
(str
: String) is
228 Iter
: Gtk_Text_Iter
;
229 Mark
: Gtk_Text_Mark
;
232 Get_End_Iter
(Get_Buffer
(Console
), Iter
);
233 Insert
(Get_Buffer
(Console
), Iter
, str
& ASCII
.LF
);
234 Get_End_Iter
(Get_Buffer
(Console
), Iter
);
235 Mark
:= Create_Mark
(Buffer
=>Get_Buffer
(Console
), Where
=>Iter
);
236 Scroll_To_Mark
(Console
, Mark
);
240 procedure Print_NoLock
(str
: String) is
241 Iter
: Gtk_Text_Iter
;
242 Mark
: Gtk_Text_Mark
;
244 Get_End_Iter
(Get_Buffer
(Console
), Iter
);
245 Insert
(Get_Buffer
(Console
), Iter
, str
& ASCII
.LF
);
246 Get_End_Iter
(Get_Buffer
(Console
), Iter
);
247 Mark
:= Create_Mark
(Buffer
=>Get_Buffer
(Console
), Where
=>Iter
);
248 Scroll_To_Mark
(Console
, Mark
);
251 procedure SetWinTitle
(str
: String) is
253 Set_Title
(Window
, str
);
256 -- Callback per l'avvio dell'auto
257 procedure Start_Car
(Widget
: access Gtk_Widget_Record
'Class)
259 -- name deve essere lungo 15
261 pragma Unreferenced
(Widget
);
265 if (Get_Active_Text
(ValTyres
) = "Slick" ) then
271 for i
in 1 .. Get_Text
(ValName
)'Length loop
272 name
(i
) := Get_Text
(ValName
)(i
);
274 Success
:= Simulator
.Car
.Init
(tyres
, 650,CarFuel_T
(Get_Value
(ValFuel
)), CarPerformance_T
(Get_Value
(ValPerformance
)),name
);
275 if( Success
= True) then
276 -- disabilito gli elementi grafici di inizializzazione
277 Set_Sensitive
(ValName
, False);
278 Set_Sensitive
(ValTyres
, False);
279 Set_Sensitive
(ValFuel
, False);
280 Set_Sensitive
(ValPerformance
, False);
281 Set_Sensitive
(Button
, False);
282 Set_Sensitive
(ButtBox
, True);
284 Print_NoLock
("ERROR: impossibile iscriversi alla gara!");
288 procedure PitStop
(Widget
: access Gtk_Widget_Record
'Class)
290 pragma Unreferenced
(Widget
);
293 Print_NoLock
("GUI: Richiesto PitStop. Parametri: "&Get_Active_Text
(ValTyresBox
)&","&Integer(Get_Value
(ValFuelBox
))'Img&".");
294 if (Get_Active_Text
(ValTyresBox
) = "Slick" ) then
299 Simulator
.Car
.CallForPitStop
(CarFuel_T
(Get_Value
(ValFuelBox
)),tyres
);
302 procedure SetUsage
(fuel
: Float; tyres
: Float)
304 strf
: String := "-----";
305 strt
: String := "-----";
307 Put
(strf
, fuel
, Aft
=> 2, Exp
=> 0);
308 Put
(strt
, tyres
, Aft
=> 2, Exp
=> 0);
309 Set_Label
(LabelUsage
, "<b>Lap usage</b>:" & ASCII
.LF
&" Fuel: "& strf
& ASCII
.LF
& " Tyres: " & strt
);
312 procedure Retire_Car
(Widget
: access Gtk_Widget_Record
'Class)
314 pragma Unreferenced
(Widget
);
316 Print_NoLock
("GUI: Richiesto ritiro dell'auto.");
319 end Simulator
.Car
.Gui
;