orka_b455160b/orka_awt/example/src/example.adb

  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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
with Ada.Exceptions;
with Ada.Real_Time;
with Ada.Text_IO;

with AWT.Clipboard;
with AWT.Drag_And_Drop;
with AWT.Inputs.Gamepads;
with AWT.Monitors;
with AWT.Windows;

with Orka.Contexts.AWT;
with Orka.Debug;
with Orka.Strings;
with Orka.Resources.Locations.Directories;

with Package_Test;

procedure Example is
   use Ada.Text_IO;

   Index : Positive := 1;
   Monitor_Events, Last_Monitor_Events : Positive := 1;
   Border_Size : constant := 50;
   Should_Be_Visible : Boolean := True;
   Visible_Index : Positive := 1;

   Print_Axes_And_Triggers : constant Boolean := False;

   subtype Normalized is AWT.Inputs.Gamepads.Normalized;

   Color      : AWT.Inputs.Gamepads.RGB_Color := (others => 0.0);
   Brightness : Normalized := 0.0;

   type Extra_Info_Kind is (Info_Motion, Info_Battery, Info_LED);

   Extra_Info : Extra_Info_Kind := Extra_Info_Kind'First;

   Visible_Index_Count : constant := 100;

   type Test_Listener is new AWT.Monitors.Monitor_Event_Listener with null record;

   overriding procedure On_Connect    (Object : Test_Listener; Monitor : AWT.Monitors.Monitor_Ptr);
   overriding procedure On_Disconnect (Object : Test_Listener; Monitor : AWT.Monitors.Monitor_Ptr);

   overriding
   procedure On_Connect
     (Object  : Test_Listener;
      Monitor : AWT.Monitors.Monitor_Ptr) is
   begin
      Put_Line ("connected:");
      Monitor.Log_Information;
      Monitor_Events := Monitor_Events + 1;
   end On_Connect;

   overriding
   procedure On_Disconnect
     (Object  : Test_Listener;
      Monitor : AWT.Monitors.Monitor_Ptr) is
   begin
      Put_Line ("disconnected:");
      Monitor.Log_Information;
      Monitor_Events := Monitor_Events + 1;
   end On_Disconnect;

   Unused_Monitor_Listener : Test_Listener;

   type Print_Gamepad_Listener is new AWT.Inputs.Gamepads.Gamepad_Event_Listener with null record;

   overriding
   procedure On_Connect
     (Object  : Print_Gamepad_Listener;
      Gamepad : AWT.Inputs.Gamepads.Gamepad_Ptr);

   overriding
   procedure On_Disconnect
     (Object  : Print_Gamepad_Listener;
      Gamepad : AWT.Inputs.Gamepads.Gamepad_Ptr);

   overriding
   procedure On_Connect
     (Object  : Print_Gamepad_Listener;
      Gamepad : AWT.Inputs.Gamepads.Gamepad_Ptr) is
   begin
      Gamepad.Log_Information;
   end On_Connect;

   overriding
   procedure On_Disconnect
     (Object  : Print_Gamepad_Listener;
      Gamepad : AWT.Inputs.Gamepads.Gamepad_Ptr) is
   begin
      Gamepad.Log_Information;
   end On_Disconnect;

   Unused_Print_Gamepad_Listener : Print_Gamepad_Listener;

   Effect_1 : constant AWT.Inputs.Gamepads.Effect :=
     AWT.Inputs.Gamepads.Rumble_Effect (0.15, 0.0, 0.7, 1.0);

   Effect_2 : constant AWT.Inputs.Gamepads.Effect :=
     AWT.Inputs.Gamepads.Periodic_Effect (8.0, 0.0, 0.8, 3.0, 2.0);

   Location_Data : constant Orka.Resources.Locations.Location_Ptr :=
     (Orka.Resources.Locations.Directories.Create_Location ("./"));

   use all type AWT.Inputs.Gamepads.Connection_Kind;
begin
   Put_Line ("Initializing...");
   AWT.Initialize;
   Put_Line ("Initialized");

   for Monitor of AWT.Monitors.Monitors loop
      Monitor.Log_Information;
   end loop;

   if Location_Data.Exists ("gamecontrollerdb.txt") then
      AWT.Inputs.Gamepads.Set_Mappings
        (Orka.Resources.Convert (Location_Data.Read_Data ("gamecontrollerdb.txt").Get));
      Put_Line ("Mappings added");
   end if;
   AWT.Inputs.Gamepads.Initialize;

   AWT.Inputs.Gamepads.Poll (DT => 0.0);
   for Gamepad of AWT.Inputs.Gamepads.Gamepads loop
      Gamepad.Log_Information;
   end loop;

   declare
      Next_Cursor : AWT.Inputs.Cursors.Pointer_Cursor :=
        AWT.Inputs.Cursors.Pointer_Cursor'First;

      use Ada.Real_Time;

      Interval : constant Duration := To_Duration (Microseconds (16_667));
      Flip_Size : Boolean := False;

      Context : constant Orka.Contexts.Surface_Context'Class :=
        Orka.Contexts.AWT.Create_Context
          (Version => (4, 2),
           Flags   => (Debug | Robust => True, others => False));

      Window : Package_Test.Test_Window := Package_Test.Create_Window
        (Context, 600, 400, Visible => True, Transparent => True, Title => "init test");
      AWT_Window : AWT.Windows.Window'Class renames AWT.Windows.Window'Class (Window);

      task Render_Task is
         entry Start_Rendering;
      end Render_Task;

      task body Render_Task is
      begin
         Put_Line ("Render task waiting to get started...");
         accept Start_Rendering;
         Put_Line ("Render task started");
         Context.Make_Current (Window);
         Put_Line ("Window made current on context");

         Orka.Debug.Set_Log_Messages (Enable => True);
         Put_Line ("Context version: " & Orka.Contexts.Image (Context.Version));

         Window.Post_Initialize;

         Put_Line ("Rendering...");
         while not Window.Should_Close loop
            delay until Clock + Microseconds (15000);
            Window.Render;
         end loop;
         Put_Line ("Rendering done");
         Context.Make_Not_Current;
         Put_Line ("Render task, context made not current");
      exception
         when E : others =>
            Put_Line ("Error render task: " & Ada.Exceptions.Exception_Information (E));
            Context.Make_Not_Current;
            raise;
      end Render_Task;

      task Poll_Joysticks;

      task body Poll_Joysticks is
         Poll_Interval : constant Time_Span := Milliseconds (4);

         Next_Time : Time := Clock + Poll_Interval;
      begin
         Put_Line ("Polling joysticks...");
         loop
            exit when Window.Should_Close;

            AWT.Inputs.Gamepads.Poll (DT => 0.0);

            delay until Next_Time;
            Next_Time := Next_Time + Poll_Interval;
         end loop;
         Put_Line ("Polling done");
      exception
         when E : others =>
            Put_Line ("Error joystick task: " & Ada.Exceptions.Exception_Information (E));
            raise;
      end Poll_Joysticks;
   begin
      Context.Make_Not_Current;
      Put_Line ("Context made not current in main task");
      Render_Task.Start_Rendering;
      Put_Line ("Render task started by main task");

      Window.Set_Margin (Border_Size);
      Put_Line ("Starting event loop...");
      while not Window.Should_Close loop
         AWT.Process_Events (Interval);
         Index := Index + 1;

         select
            Window.Drag_And_Drop_Signal.Wait;

            declare
               Result : constant String := AWT.Drag_And_Drop.Get;
            begin
               Put_Line ("value: '" & Orka.Strings.Strip_Line_Term (Result) & "'");
               AWT.Drag_And_Drop.Finish (AWT.Inputs.Copy);
            end;
         else
            null;
         end select;

         if not Should_Be_Visible  then
            Put_Line (Positive'Image (Visible_Index + Visible_Index_Count) & Index'Image);
            if Index > Visible_Index + Visible_Index_Count then
               Should_Be_Visible := True;
               AWT_Window.Set_Title ("visible! " & Visible_Index'Image);
               AWT_Window.Set_Visible (True);
               Put_Line ("window visible");
            end if;
         end if;

         if Monitor_Events /= Last_Monitor_Events then
            Put_Line ("Monitor count: " & Natural'Image (AWT.Monitors.Monitors'Length));
            Last_Monitor_Events := Monitor_Events;
         end if;

         declare
            Pointer : constant AWT.Inputs.Pointer_State := AWT_Window.State;

--            use all type AWT.Inputs.Button_State;
--            use all type AWT.Inputs.Dimension;
            use all type AWT.Inputs.Pointer_Button;
            use all type AWT.Inputs.Pointer_Mode;
            use type AWT.Inputs.Cursors.Pointer_Cursor;
         begin
--            if False then
--               Put_Line ("focused:   " & Pointer.Focused'Image);
--               Put_Line ("scrolling: " & Pointer.Scrolling'Image);
--               Put_Line ("buttons:");
--               Put_Line ("    left: " & Pointer.Buttons (Left)'Image);
--               Put_Line ("  middle: " & Pointer.Buttons (Middle)'Image);
--               Put_Line ("   right: " & Pointer.Buttons (Right)'Image);
--               Put_Line ("mode: " & Pointer.Mode'Image);
--               Put_Line ("position: " & Pointer.Position (X)'Image & Pointer.Position (Y)'Image);
--               Put_Line ("relative: " & Pointer.Relative (X)'Image & Pointer.Relative (Y)'Image);
--               Put_Line ("scroll: " & Pointer.Scroll (X)'Image & Pointer.Scroll (Y)'Image);
--            end if;

            if Pointer.Pressed (Left) then
               Next_Cursor :=
                 (if Next_Cursor = AWT.Inputs.Cursors.Pointer_Cursor'Last then
                    AWT.Inputs.Cursors.Pointer_Cursor'First
                  else
                    AWT.Inputs.Cursors.Pointer_Cursor'Succ (Next_Cursor));
               AWT_Window.Set_Pointer_Cursor (Next_Cursor);
            end if;

            if Pointer.Pressed (Right) and Pointer.Mode /= Locked then
               AWT_Window.Set_Pointer_Mode (Locked);
            elsif Pointer.Released (Right) and Pointer.Mode = Locked then
               AWT_Window.Set_Pointer_Mode (Visible);
            end if;
         end;

         declare
            Keyboard : constant AWT.Inputs.Keyboard_State := AWT_Window.State;

--            use type AWT.Inputs.Keyboard_Modifiers;
            use all type AWT.Inputs.Button_State;
            use all type AWT.Inputs.Keyboard_Button;
         begin
            if Keyboard.Pressed (Key_Escape) then
               Window.Close;
            end if;

            if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_C) then
               declare
                  Value : constant String := "foobar" & Index'Image;
               begin
                  AWT.Clipboard.Set (Value);
                  Put_Line ("Set clipboard: '" & Value & "'");
               end;
            end if;

            if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_V) then
               Put_Line ("Get clipboard: '" & AWT.Clipboard.Get & "'");
            end if;

--            Put_Line ("focused: " & Keyboard.Focused'Image);
--            Put_Line ("repeat:");
--            Put_Line ("  rate:  " & Keyboard.Repeat_Rate'Image);
--            Put_Line ("  delay: " & Keyboard.Repeat_Delay'Image);
--            if False and Keyboard.Modifiers /= Last_Keyboard.Modifiers then
--               Put_Line ("Shift:       " & Keyboard.Modifiers.Shift'Image);
--               Put_Line ("Caps_Lock:   " & Keyboard.Modifiers.Caps_Lock'Image);
--               Put_Line ("Ctrl:        " & Keyboard.Modifiers.Ctrl'Image);
--               Put_Line ("Alt:         " & Keyboard.Modifiers.Alt'Image);
--               Put_Line ("Num_Lock:    " & Keyboard.Modifiers.Num_Lock'Image);
--               Put_Line ("Logo:        " & Keyboard.Modifiers.Logo'Image);
--            end if;

            declare
               use all type AWT.Windows.Size_Mode;
            begin
               if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_F) then
                  if AWT_Window.State.Mode = Fullscreen then
                     AWT_Window.Set_Size_Mode (Default);
                  else
                     AWT_Window.Set_Size_Mode (Fullscreen);
                  end if;
               end if;

               if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_M) then
                  if AWT_Window.State.Mode = Maximized then
                     AWT_Window.Set_Size_Mode (Default);
                  else
                     AWT_Window.Set_Size_Mode (Maximized);
                  end if;
               end if;

               if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_S) then
                  Flip_Size := not Flip_Size;
                  if Flip_Size then
                     AWT_Window.Set_Size (1280, 720);
                  else
                     AWT_Window.Set_Size (600, 400);
                  end if;
               end if;

               if Keyboard.Modifiers.Ctrl and Keyboard.Pressed (Key_H) then
                  Should_Be_Visible := False;
                  Visible_Index := Index;
                  AWT_Window.Set_Visible (False);
                  Put_Line ("window hidden");
               end if;
            end;

            declare
               Gamepads : constant AWT.Inputs.Gamepads.Gamepad_Array :=
                 AWT.Inputs.Gamepads.Gamepads;

               Title : AWT.SU.Unbounded_String;
            begin
               if Gamepads'Length > 0 then
                  declare
                     Gamepad : AWT.Inputs.Gamepads.Gamepad_Ptr renames Gamepads (1);

                     State   : constant AWT.Inputs.Gamepads.Gamepad_State := Gamepad.State;
                     Motion  : constant AWT.Inputs.Gamepads.Motion_State  := Gamepad.State;
                     Battery : constant AWT.Inputs.Gamepads.Battery_State := Gamepad.State;
                     LED     : constant AWT.Inputs.Gamepads.LED_State     := Gamepad.State;

                     use all type AWT.Inputs.Gamepads.Gamepad_Button;
                     use all type AWT.Inputs.Gamepads.Gamepad_Trigger;
                     use all type AWT.Inputs.Gamepads.Color_Kind;
                     use type Normalized;

                     Show_Extra_Info : constant Boolean := State.Buttons (Center_Right) /= Pressed;
                  begin
                     AWT.SU.Append (Title,
                       "serial: " & Gamepad.Serial_Number &
                       " connected? " & Gamepad.Connection'Image &
                       " (" & Natural'Image (Gamepads'Length) & " gamepads) ");

                     if State.Pressed (Shoulder_Right) then
                        Gamepad.Play_Effect (Effect_1);
                     elsif State.Released (Shoulder_Right) then
                        Gamepad.Cancel_Effect (Effect_1);
                     end if;

                     if State.Pressed (Shoulder_Left) then
                        Gamepad.Play_Effect (Effect_2);
                     elsif State.Released (Shoulder_Left) then
                        Gamepad.Cancel_Effect (Effect_2);
                     end if;

                     if State.Pressed (Center_Right) then
                        Extra_Info := (if Extra_Info = Extra_Info_Kind'Last then
                                         Extra_Info_Kind'First
                                       else
                                         Extra_Info_Kind'Succ (Extra_Info));
                     end if;

                     --  Toggle LED red when pressing button B
                     if State.Pressed (Action_Right) then
                        Color (Red) := 1.0 - Color (Red);
                     end if;

                     --  Toggle LED green when pressing button Y
                     if State.Pressed (Action_Up) then
                        Color (Green) := 1.0 - Color (Green);
                     end if;

                     --  Toggle LED blue when pressing button A
                     if State.Pressed (Action_Down) then
                        Color (Blue) := 1.0 - Color (Blue);
                     end if;

                     --  Set brightness with left trigger while holding button X
                     if State.Buttons (Action_Left) = Pressed then
                        Brightness := Normalized (State.Triggers (Trigger_Left));
                     end if;

                     Gamepad.Set_LED (Brightness, Color);

                     for Button in State.Buttons'Range loop
                        if State.Pressed (Button) then
--                           pragma Assert (State.Buttons (Button) = Pressed);
                           Put_Line ("pressed " & Button'Image);
                        end if;

                        if State.Released (Button) then
--                           pragma Assert (State.Buttons (Button) = Released);
                           Put_Line ("released " & Button'Image);
                        end if;

                        if State.Buttons (Button) = Pressed then
                           AWT.SU.Append (Title, " " & Button'Image);
                        end if;
                     end loop;

                     if Show_Extra_Info then
                        if Extra_Info = Info_Motion and Motion.Is_Present then
                           AWT.SU.Append (Title, "motion:");
                           for Axis in Motion.Axes'Range loop
                              AWT.SU.Append (Title, " " & Motion.Axes (Axis)'Image);
                           end loop;
                        end if;

                        if Extra_Info = Info_Battery and Battery.Is_Present then
                           AWT.SU.Append (Title, "battery: " & Battery.Capacity'Image &
                             " (" & Battery.Status'Image & ")");
                        end if;

                        if Extra_Info = Info_LED and LED.Is_Present then
                           AWT.SU.Append (Title, "L: " & LED.Brightness'Image & " color: " &
                             LED.Color (Red)'Image &
                             LED.Color (Green)'Image &
                             LED.Color (Blue)'Image);
                        end if;
                     end if;

                     if Print_Axes_And_Triggers then
                        for Axis in State.Axes'Range loop
                           Put (Axis'Image & ": " & State.Axes (Axis)'Image & " ");
                        end loop;
                        for Trigger in State.Triggers'Range loop
                           Put (Trigger'Image & ": " & State.Triggers (Trigger)'Image & " ");
                        end loop;
                        New_Line;
                     end if;
                  end;
               end if;

               if Gamepads'Length > 0 then
                  AWT_Window.Set_Title (AWT.SU.To_String (Title));
               else
                  AWT_Window.Set_Title ("No gamepads");
               end if;
            end;
         end;
      end loop;
      Put_Line ("Exited event loop");
   exception
      when E : others =>
         Put_Line ("Error in main task: " & Ada.Exceptions.Exception_Information (E));
         raise;
   end;
end Example;