Versione finale relazione
[toni-reis.git] / src / simulator-car-gui.adb
blob5c4d4cf4e080089649f4d2ce437b00abd79a9c23
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;
6 -- include GTK
7 with Gdk.Threads;
8 with Gtk.Main;
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
23 VBox1 : Gtk_Box;
24 HBox : Gtk_Box;
25 Frame : Gtk_Frame;
26 Sep : Gtk_Separator;
27 Table : Gtk_Table;
28 TableBox : Gtk_Table;
29 align : Gtk_Alignment;
30 Label : Gtk_Label;
31 LabelId : Gtk_Label;
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;
40 b : Boolean := False;
41 begin
42 -- Inizializzo GTK e la finestra principale
43 Gdk.Threads.G_Init;
44 Gdk.Threads.Init;
45 Gtk.Main.Init;
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:");
53 Gtk_New(ValId, "--");
54 Gtk_New(LabelName, "Name:");
55 Gtk_New(ValName);
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");
97 Gtk_New (Scroller);
98 Set_Policy (Scroller, Policy_Automatic, Policy_Always);
99 Gtk_New (Console);
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);
108 Add(align, LabelId);
109 Attach(Table,align, 0, 1, 0, 1, Fill, Fill, 5, 5);
110 Gtk_New(align, 0.0, 0.5, 0.0, 0.0);
111 Add(align, ValId);
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);
117 Add(align, ValName);
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);
135 Add(align, ValFuel);
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);
159 Add(align, Label);
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);
166 Add(align, Label);
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);
172 Add(align, ButtBox);
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);
176 Add(align, 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);
184 Add(align, 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);
202 Add(Window, VBox1);
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
217 Show_All (Window);
219 -- Metto in attesa l'applicazione nel loop GTK
220 Gdk.Threads.Enter;
221 Gtk.Main.Main;
222 Gdk.Threads.Leave;
224 Put_Line ("GUI: terminato Gtk.Main.Main di auto. Termino l'applicazione.");
225 end Init_Car_Gui;
227 procedure Print(str : String) is
228 Iter : Gtk_Text_Iter;
229 Mark : Gtk_Text_Mark;
230 begin
231 Gdk.Threads.Enter;
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);
237 Gdk.Threads.Leave;
238 end Print;
240 procedure Print_NoLock(str : String) is
241 Iter : Gtk_Text_Iter;
242 Mark : Gtk_Text_Mark;
243 begin
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);
249 end Print_NoLock;
251 procedure SetWinTitle(str : String) is
252 begin
253 Set_Title(Window, str);
254 end SetWinTitle;
256 -- Callback per l'avvio dell'auto
257 procedure Start_Car (Widget : access Gtk_Widget_Record'Class)
259 -- name deve essere lungo 15
260 name :String :=" ";
261 pragma Unreferenced (Widget);
262 Success : Boolean;
263 tyres : Tires_T;
264 begin
265 if (Get_Active_Text(ValTyres) = "Slick" ) then
266 tyres := Slick;
267 else
268 tyres := Rain;
269 end if;
271 for i in 1 .. Get_Text(ValName)'Length loop
272 name(i) := Get_Text(ValName)(i);
273 end loop;
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);
283 else
284 Print_NoLock("ERROR: impossibile iscriversi alla gara!");
285 end if;
286 end Start_Car;
288 procedure PitStop (Widget : access Gtk_Widget_Record'Class)
290 pragma Unreferenced (Widget);
291 tyres : Tires_T;
292 begin
293 Print_NoLock("GUI: Richiesto PitStop. Parametri: "&Get_Active_Text(ValTyresBox)&","&Integer(Get_Value(ValFuelBox))'Img&".");
294 if (Get_Active_Text(ValTyresBox) = "Slick" ) then
295 tyres := Slick;
296 else
297 tyres := Rain;
298 end if;
299 Simulator.Car.CallForPitStop(CarFuel_T(Get_Value(ValFuelBox)),tyres );
300 end PitStop;
302 procedure SetUsage(fuel : Float; tyres : Float)
304 strf : String := "-----";
305 strt : String := "-----";
306 begin
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);
310 end SetUsage;
312 procedure Retire_Car (Widget : access Gtk_Widget_Record'Class)
314 pragma Unreferenced (Widget);
315 begin
316 Print_NoLock ("GUI: Richiesto ritiro dell'auto.");
317 Simulator.Car.Kill;
318 end Retire_Car;
319 end Simulator.Car.Gui;