gnoga_2.1.2_5f127c56/components/pixi/src/gnoga-gui-plugin-pixi-text.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
------------------------------------------------------------------------------
--                                                                          --
--                   GNOGA - The GNU Omnificent GUI for Ada                 --
--                                                                          --
--            G N O G A . G U I . P L U G I N . P I X I . T E X T           --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
--                                                                          --
--                     Copyright (C) 2017 Pascal Pignard                    --
--                                                                          --
--  This library is free software;  you can redistribute it and/or modify   --
--  it under terms of the  GNU General Public License  as published by the  --
--  Free Software  Foundation;  either version 3,  or (at your  option) any --
--  later version. This library is distributed in the hope that it will be  --
--  useful, but WITHOUT ANY WARRANTY;  without even the implied warranty of --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                    --
--                                                                          --
--  As a special exception under Section 7 of GPL version 3, you are        --
--  granted additional permissions described in the GCC Runtime Library     --
--  Exception, version 3.1, as published by the Free Software Foundation.   --
--                                                                          --
--  You should have received a copy of the GNU General Public License and   --
--  a copy of the GCC Runtime Library Exception along with this program;    --
--  see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see   --
--  <http://www.gnu.org/licenses/>.                                         --
--                                                                          --
--  As a special exception, if other files instantiate generics from this   --
--  unit, or you link this unit with other files to produce an executable,  --
--  this  unit  does not  by itself cause  the resulting executable to be   --
--  covered by the GNU General Public License. This exception does not      --
--  however invalidate any other reasons why the executable file might be   --
--  covered by the  GNU Public License.                                     --
--                                                                          --
--  For more information please go to http://www.gnoga.com                  --
------------------------------------------------------------------------------

with Ada.Numerics.Elementary_Functions;
with Gnoga.Server.Connection;

package body Gnoga.Gui.Plugin.Pixi.Text is

   ------------
   -- Create --
   ------------

   procedure Create
     (Text        : in out Text_Type;
      Parent      : in out Container_Type'Class;
      Message     : in     String;
      Row, Column : in     Integer)
   is
      Text_ID : constant String := Gnoga.Server.Connection.New_GID;
   begin
      Text.ID (Text_ID, Gnoga.Types.Gnoga_ID);
      Text.Connection_ID (Parent.Connection_ID);
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text_ID & "'] = new PIXI.Text('" & Escape_Quotes (Message) & "');");
      Text.Locate (Row, Column);
      Text.Motion (0.0, 0.0);
      Text.Acceleration (0.0, 0.0);
      Text.Rotation_Velocity (0.0);
      Text.Rotation_Acceleration (0.0);
      Text.Parent (Parent);
   end Create;

   ------------
   -- Locate --
   ------------

   procedure Locate
     (Text        : in out Text_Type;
      Row, Column : in     Integer)
   is
   begin
      Text.Property ("x", Column);
      Text.Property ("y", Row);
   end Locate;

   --------------
   -- Position --
   --------------

   procedure Position
     (Text        : in     Text_Type;
      Row, Column :    out Integer)
   is
   begin
      Row    := Text.Property ("y");
      Column := Text.Property ("x");
   end Position;

   ---------
   -- Row --
   ---------

   function Row
     (Text : in Text_Type)
      return Integer
   is
      V : constant Float := Text.Property ("y");
   begin
      return Integer (V);
   end Row;

   ------------
   -- Column --
   ------------

   function Column
     (Text : in Text_Type)
      return Integer
   is
      V : constant Float := Text.Property ("x");
   begin
      return Integer (V);
   end Column;

   ------------
   -- Motion --
   ------------

   procedure Motion
     (Text                          : in out Text_Type;
      Row_Velocity, Column_Velocity : in     Velocity_Type)
   is
   begin
      Text.Property ("gnoga_vx", Column_Velocity / Frame_Rate);
      Text.Property ("gnoga_vy", Row_Velocity / Frame_Rate);
   end Motion;

   ------------------
   -- Row_Velocity --
   ------------------

   function Row_Velocity
     (Text : in Text_Type)
      return Velocity_Type
   is
   begin
      return Text.Property ("gnoga_vy") * Frame_Rate;
   end Row_Velocity;

   ---------------------
   -- Column_Velocity --
   ---------------------

   function Column_Velocity
     (Text : in Text_Type)
      return Velocity_Type
   is
   begin
      return Text.Property ("gnoga_vx") * Frame_Rate;
   end Column_Velocity;

   ------------------
   -- Acceleration --
   ------------------

   procedure Acceleration
     (Text                                  : in out Text_Type;
      Row_Acceleration, Column_Acceleration : in     Acceleration_Type)
   is
   begin
      Text.Property ("gnoga_ax", Column_Acceleration / Frame_Rate / Frame_Rate);
      Text.Property ("gnoga_ay", Row_Acceleration / Frame_Rate / Frame_Rate);
   end Acceleration;

   ----------------------
   -- Row_Acceleration --
   ----------------------

   function Row_Acceleration
     (Text : in Text_Type)
      return Acceleration_Type
   is
   begin
      return Text.Property ("gnoga_ay") * (Frame_Rate * Frame_Rate);
   end Row_Acceleration;

   -------------------------
   -- Column_Acceleration --
   -------------------------

   function Column_Acceleration
     (Text : in Text_Type)
      return Acceleration_Type
   is

   begin
      return Text.Property ("gnoga_ax") * (Frame_Rate * Frame_Rate);
   end Column_Acceleration;

   -----------
   -- Alpha --
   -----------

   procedure Alpha
     (Text  : in out Text_Type;
      Value : in     Gnoga.Types.Alpha_Type)
   is
   begin
      Text.Property ("alpha", Float (Value));
   end Alpha;

   -----------
   -- Alpha --
   -----------

   function Alpha
     (Text : in Text_Type)
      return Gnoga.Types.Alpha_Type
   is
   begin
      return Gnoga.Types.Alpha_Type (Float'(Text.Property ("alpha")));
   end Alpha;

   ------------
   -- Anchor --
   ------------

   procedure Anchor
     (Text        : in out Text_Type;
      Row, Column : in     Gnoga.Types.Frational_Range_Type)
   is
      function Image is new UXStrings.Conversions.Fixed_Point_Image (Gnoga.Types.Frational_Range_Type);
   begin
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text.ID & "'].anchor = {x:" & Image (Column) & ",y:" & Image (Row) & "};");
   end Anchor;

   ----------------
   -- Row_Anchor --
   ----------------

   function Row_Anchor
     (Text : in Text_Type)
      return Gnoga.Types.Frational_Range_Type
   is
   begin
      return Gnoga.Types.Frational_Range_Type (Float'(Text.Execute ("anchor.y")));
   end Row_Anchor;

   -------------------
   -- Column_Anchor --
   -------------------

   function Column_Anchor
     (Text : in Text_Type)
      return Gnoga.Types.Frational_Range_Type
   is
   begin
      return Gnoga.Types.Frational_Range_Type (Float'(Text.Execute ("anchor.x")));
   end Column_Anchor;

   ----------------
   -- Blend_Mode --
   ----------------

   procedure Blend_Mode
     (Text  : in out Text_Type;
      Value : in     Blend_Modes_Type)
   is
      function Image is new UXStrings.Conversions.Scalar_Image (Blend_Modes_Type);
   begin
      Text.Property ("blendMode", Image (Value));
   end Blend_Mode;

   ----------------
   -- Blend_Mode --
   ----------------

   function Blend_Mode
     (Text : in Text_Type)
      return Blend_Modes_Type
   is
      function Value is new UXStrings.Conversions.Scalar_Value (Blend_Modes_Type);
   begin
      return Value (Text.Property ("blendMode"));
   end Blend_Mode;

   -------------
   -- Message --
   -------------

   procedure Message
     (Text  : in out Text_Type;
      Value : in     String)
   is
   begin
      Text.Property ("text", Value);
   end Message;

   -------------
   -- Message --
   -------------

   function Message
     (Text : in Text_Type)
      return String
   is
   begin
      return Text.Property ("text");
   end Message;

   -----------
   -- Pivot --
   -----------

   procedure Pivot
     (Text        : in out Text_Type;
      Row, Column : in     Integer)
   is
   begin
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text.ID & "'].pivot = {x:" & Image (Column) & ",y:" & Image (Row) & "};");
   end Pivot;

   ---------------
   -- Row_Pivot --
   ---------------

   function Row_Pivot
     (Text : in Text_Type)
      return Integer
   is
   begin
      return Text.Execute ("pivot.x");
   end Row_Pivot;

   ------------------
   -- Column_Pivot --
   ------------------

   function Column_Pivot
     (Text : in Text_Type)
      return Integer
   is
   begin
      return Text.Execute ("pivot.y");
   end Column_Pivot;

   --------------
   -- Rotation --
   --------------

   procedure Rotation
     (Text  : in out Text_Type;
      Value : in     Integer)
   is
   begin
      Text.Property ("rotation", Float (Value) * Ada.Numerics.Pi / 180.0);
   end Rotation;

   --------------
   -- Rotation --
   --------------

   function Rotation
     (Text : in Text_Type)
      return Integer
   is
   begin
      return Integer (Float'(Text.Property ("rotation")) * 180.0 / Ada.Numerics.Pi);
   end Rotation;

   -----------------------
   -- Rotation_Velocity --
   -----------------------

   procedure Rotation_Velocity
     (Text  : in out Text_Type;
      Value :        Velocity_Type)
   is
   begin
      Text.Property ("gnoga_vr", Value / Frame_Rate * Ada.Numerics.Pi / 180.0);
   end Rotation_Velocity;

   -----------------------
   -- Rotation_Velocity --
   -----------------------

   function Rotation_Velocity
     (Text : in Text_Type)
      return Velocity_Type
   is
   begin
      return Float'(Text.Property ("gnoga_vr")) * Frame_Rate * 180.0 / Ada.Numerics.Pi;
   end Rotation_Velocity;

   -----------------------
   -- Rotation_Acceleration --
   -----------------------

   procedure Rotation_Acceleration
     (Text  : in out Text_Type;
      Value :        Acceleration_Type)
   is
   begin
      Text.Property ("gnoga_ar", Value / Frame_Rate / Frame_Rate * Ada.Numerics.Pi / 180.0);
   end Rotation_Acceleration;

   -----------------------
   -- Rotation_Acceleration --
   -----------------------

   function Rotation_Acceleration
     (Text : in Text_Type)
      return Acceleration_Type
   is
   begin
      return Float'(Text.Property ("gnoga_ar")) * Frame_Rate * Frame_Rate * 180.0 / Ada.Numerics.Pi;
   end Rotation_Acceleration;

   -----------
   -- Width --
   -----------

   overriding procedure Width
     (Text  : in out Text_Type;
      Value : in     Integer)
   is
   begin
      Text.Property ("width", Value);
   end Width;

   -----------
   -- Width --
   -----------

   overriding function Width
     (Text : in Text_Type)
      return Integer
   is
   begin
      return Text.Property ("width");
   end Width;

   ------------
   -- Height --
   ------------

   overriding procedure Height
     (Text  : in out Text_Type;
      Value : in     Integer)
   is
   begin
      Text.Property ("height", Value);
   end Height;

   ------------
   -- Height --
   ------------

   overriding function Height
     (Text : in Text_Type)
      return Integer
   is
   begin
      return Text.Property ("height");
   end Height;

   -----------
   -- Scale --
   -----------

   procedure Scale
     (Text        : in out Text_Type;
      Row, Column : in     Positive)
   is
   begin
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text.ID & "'].scale = {x:" & Image (Column) & ",y:" & Image (Row) & "};");
   end Scale;

   ---------------
   -- Row_Scale --
   ---------------

   function Row_Scale
     (Text : in Text_Type)
      return Positive
   is
   begin
      return Text.Execute ("scale.y");
   end Row_Scale;

   ------------------
   -- Column_Scale --
   ------------------

   function Column_Scale
     (Text : in Text_Type)
      return Positive
   is
   begin
      return Text.Execute ("scale.x");
   end Column_Scale;

   ----------
   -- Skew --
   ----------

   procedure Skew
     (Text        : in out Text_Type;
      Row, Column : in     Positive)
   is
   begin
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text.ID & "'].skew = {x:" & Image (Column) & ",y:" & Image (Row) & "};");
   end Skew;

   --------------
   -- Row_Skew --
   --------------

   function Row_Skew
     (Text : in Text_Type)
      return Positive
   is
   begin
      return Text.Execute ("skew.y");
   end Row_Skew;

   -----------------
   -- Column_Skew --
   -----------------

   function Column_Skew
     (Text : in Text_Type)
      return Positive
   is
   begin
      return Text.Execute ("skew.x");
   end Column_Skew;

   ---------------
   -- Set_Style --
   ---------------

   procedure Set_Style
     (Text  : in out Text_Type;
      Value : in     Style_Type'Class)
   is
   begin
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Text.ID & "'].style = gnoga['" & Value.ID & "'];");
   end Set_Style;

   ---------------
   -- Get_Style --
   ---------------

   procedure Get_Style
     (Text  : in     Text_Type;
      Value : in out Style_Type'Class)
   is
      Style_ID : constant String := Gnoga.Server.Connection.New_GID;
   begin
      Value.ID (Style_ID, Gnoga.Types.Gnoga_ID);
      Value.Connection_ID (Text.Connection_ID);
      Gnoga.Server.Connection.Execute_Script
        (Text.Connection_ID, "gnoga['" & Style_ID & "'] = gnoga['" & Text.ID & "'].style;");
   end Get_Style;

   ----------
   -- Tint --
   ----------

   procedure Tint
     (Text  : in out Text_Type;
      Value : in     Natural)
   is
   begin
      Text.Property ("tint", Value);
   end Tint;

   ----------
   -- Tint --
   ----------

   function Tint
     (Text : in Text_Type)
      return Natural
   is
   begin
      return Text.Property ("tint");
   end Tint;

   -------------
   -- Visible --
   -------------

   procedure Visible
     (Text  : in out Text_Type;
      Value : in     Boolean)
   is
   begin
      Text.Property ("visible", Value);
   end Visible;

   -------------
   -- Visible --
   -------------

   function Visible
     (Text : in Text_Type)
      return Boolean
   is
   begin
      return Text.Property ("visible");
   end Visible;

   -----------------
   -- Coincidence --
   -----------------

   function Coincidence
     (Text1, Text2 : in Text_Type;
      Tolerance    : in Natural)
      return Boolean
   is
   begin
      return Distance (Text1, Text2) <= Tolerance;
   end Coincidence;

   -----------------
   -- Coincidence --
   -----------------

   function Coincidence
     (Text        : in Text_Type;
      Row, Column : in Integer;
      Tolerance   : in Natural)
      return Boolean
   is
   begin
      return Distance (Text, Row, Column) <= Tolerance;
   end Coincidence;

   --------------
   -- Distance --
   --------------

   function Distance
     (Text1, Text2 : in Text_Type)
      return Natural
   is
   begin
      return
        Natural
          (Ada.Numerics.Elementary_Functions.Sqrt
             (Float (Text2.Row - Text1.Row)**2 + Float (Text2.Column - Text1.Column)**2));
   end Distance;

   --------------
   -- Distance --
   --------------

   function Distance
     (Text        : in Text_Type;
      Row, Column : in Integer)
      return Natural
   is
   begin
      return
        Natural (Ada.Numerics.Elementary_Functions.Sqrt (Float (Text.Row - Row)**2 + Float (Text.Column - Column)**2));
   end Distance;

   ------------
   -- Delete --
   ------------

   procedure Delete
     (Text   : in out Text_Type;
      Parent : in out Container_Type'Class)
   is
   begin
      Parent.Remove_Child (Text);
   end Delete;

   ----------------
   -- Delete_All --
   ----------------

   procedure Delete_All (Parent : in out Container_Type'Class) is
   begin
      Gnoga.Server.Connection.Execute_Script
        (Parent.Connection_ID,
         "var gnoga_list = [];" & " for (var gnoga_text of gnoga['" & Parent.ID &
         "'].children) if (gnoga_text instanceof PIXI.Text) gnoga_list.push(gnoga_text);" &
         " for (var gnoga_text of gnoga_list)" & " gnoga['" & Parent.ID & "'].removeChild(gnoga_text);");
   end Delete_All;

end Gnoga.Gui.Plugin.Pixi.Text;