aboutsummaryrefslogtreecommitdiffstats
path: root/src/menu/UMenuButton.pas
blob: 509c140b3f6866faecc3f70729ebe4dbe6e3aef5 (plain) (blame)
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
{* UltraStar Deluxe - Karaoke Game
 *
 * UltraStar Deluxe is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program 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.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *}

unit UMenuButton;

interface

{$IFDEF FPC}
  {$MODE Delphi}
{$ENDIF}

{$I switches.inc}

uses
  TextGL,
  UTexture,
  gl,
  UMenuText,
  SDL,
  UMenuInteract;

type
  CButton = class of TButton;

  TButton = class
    protected
      SelectBool:   boolean;

      FadeProgress: real;
      FadeLastTick: cardinal;

      DeSelectW,
      DeSelectH,
      PosX,
      PosY:         real;


    public
      Text:                      array of TText;
      Texture:                   TTexture; // Button Screen position and size
      Texture2:                  TTexture; // second texture only used for fading full resolution covers

      Colorized:                 boolean;
      DeSelectTexture:           TTexture; // texture for colorized hack

      FadeTex:                   TTexture; //Texture for beautiful fading
      FadeTexPos:                byte;     //Pos of the FadeTexture (0: Top, 1: Left, 2: Bottom, 3: Right)

      DeselectType:              integer;  // not used yet
      Visible:                   boolean;

      Reflection:                boolean;
      Reflectionspacing,
      DeSelectReflectionspacing: real;

      Fade,
      FadeText:                  boolean;

      Selectable:                boolean;

      //Number of the Parent Collection, 0 if in no Collection
      Parent:  byte;

      SelectColR,
      SelectColG,
      SelectColB,
      SelectInt,
      SelectTInt:   real;
      //Fade Mod
      SelectW:      real;
      SelectH:      real;

      DeselectColR,
      DeselectColG,
      DeselectColB,
      DeselectInt,
      DeselectTInt: real;

      procedure SetY(Value: real);
      procedure SetX(Value: real);
      procedure SetW(Value: real);
      procedure SetH(Value: real);

      procedure SetSelect(Value: boolean); virtual;
      property X: real read PosX write SetX;
      property Y: real read PosY write SetY;
      property Z: real read Texture.z write Texture.z;
      property W: real read DeSelectW write SetW;
      property H: real read DeSelectH write SetH;
      property Selected: boolean read SelectBool write SetSelect;

      procedure   Draw; virtual;

      constructor Create(); overload;
      constructor Create(Textura: TTexture); overload;
      constructor Create(Textura, DSTexture: TTexture); overload;
      destructor  Destroy; override;

      function GetMouseOverArea: TMouseOverRect;
  end;

implementation

uses 
  SysUtils,
  UDisplay,
  UDrawTexture;

procedure TButton.SetX(Value: real);
{var
  dx:   real;
  T:    integer;    // text}
begin
  {dY := Value - Texture.y;

  Texture.X := Value;

  for T := 0 to High(Text) do
    Text[T].X := Text[T].X + dY;}

  PosX := Value;
  if (FadeTex.TexNum = 0) then
    Texture.X := Value;

end;

procedure TButton.SetY(Value: real);
{var
  dY: real;
  T:  integer;    // text}
begin
  {dY := Value - PosY;


  for T := 0 to High(Text) do
    Text[T].Y := Text[T].Y + dY;}

  PosY := Value;
  if (FadeTex.TexNum = 0) then
    Texture.y := Value;
end;

procedure TButton.SetW(Value: real);
begin
  if SelectW = DeSelectW then
    SelectW := Value;

  DeSelectW := Value;

  if not Fade then
  begin
    if SelectBool then
      Texture.W := SelectW
    else
      Texture.W := DeSelectW;
  end;
end;

procedure TButton.SetH(Value: real);
begin
  if SelectH = DeSelectH then
    SelectH := Value;

  DeSelectH := Value;

  if not Fade then
  begin
    if SelectBool then
      Texture.H := SelectH
    else
      Texture.H := DeSelectH;
  end;
end;

procedure TButton.SetSelect(Value : boolean);
var
  T: integer;
begin
  SelectBool := Value;

  if (Value) then
  begin
    Texture.ColR  := SelectColR;
    Texture.ColG  := SelectColG;
    Texture.ColB  := SelectColB;
    Texture.Int   := SelectInt;

    Texture2.ColR := SelectColR;
    Texture2.ColG := SelectColG;
    Texture2.ColB := SelectColB;
    Texture2.Int  := SelectInt;

    for T := 0 to High(Text) do
      Text[T].Int := SelectTInt;

    //Fade Mod
    if Fade then
    begin
      if (FadeProgress <= 0) then
        FadeProgress := 0.125;
    end
    else
    begin
      Texture.W := SelectW;
      Texture.H := SelectH;
    end;
  end
  else
  begin
    Texture.ColR  := DeselectColR;
    Texture.ColG  := DeselectColG;
    Texture.ColB  := DeselectColB;
    Texture.Int   := DeselectInt;

    Texture2.ColR := DeselectColR;
    Texture2.ColG := DeselectColG;
    Texture2.ColB := DeselectColB;
    Texture2.Int  := DeselectInt;

    for T := 0 to High(Text) do
      Text[T].Int := DeselectTInt;

    //Fade Mod
    if Fade then
    begin
      if (FadeProgress >= 1) then
        FadeProgress := 0.875;
    end
    else
    begin
      Texture.W := DeSelectW;
      Texture.H := DeSelectH;
    end;
  end;
end;

// ***** Public methods ****** //

procedure TButton.Draw;
var
  T:       integer;
  Tick:    cardinal;
  Spacing: real;
begin
  if Visible then
  begin
    //Fade Mod
    T:=0;
    if Fade then
    begin
      if (FadeProgress < 1) and (FadeProgress > 0) then
      begin
        Tick := SDL_GetTicks() div 16;
        if (Tick <> FadeLastTick) then
        begin
          FadeLastTick := Tick;

          if SelectBool then
            FadeProgress := FadeProgress + 0.125
          else
            FadeProgress := FadeProgress - 0.125;

          if (FadeText) then
          begin
            for T := 0 to high(Text) do
            begin
              Text[T].MoveX := (SelectW - DeSelectW) * FadeProgress;
              Text[T].MoveY := (SelectH - DeSelectH) * FadeProgress;
            end;
          end;

        end;
      end;

      //Method without Fade Texture
      if (FadeTex.TexNum = 0) then
      begin
        Texture.W         := DeSelectW + (SelectW - DeSelectW) * FadeProgress;
        Texture.H         := DeSelectH + (SelectH - DeSelectH) * FadeProgress;
        DeSelectTexture.W := Texture.W;
        DeSelectTexture.H := Texture.H;
      end
      else //method with Fade Texture
      begin
        Texture.W         := DeSelectW;
        Texture.H         := DeSelectH;
        DeSelectTexture.W := Texture.W;
        DeSelectTexture.H := Texture.H;

        FadeTex.ColR      := Texture.ColR;
        FadeTex.ColG      := Texture.ColG;
        FadeTex.ColB      := Texture.ColB;
        FadeTex.Int       := Texture.Int;

        FadeTex.Z         := Texture.Z;

        FadeTex.Alpha     := Texture.Alpha;
        FadeTex.TexX1     := 0;
        FadeTex.TexX2     := 1;
        FadeTex.TexY1     := 0;
        FadeTex.TexY2     := 1;

        case FadeTexPos of
          0: //FadeTex on Top
            begin
              //Standard Texture
              Texture.X := PosX;
              Texture.Y := PosY + (SelectH - DeSelectH) * FadeProgress;
              DeSelectTexture.X := Texture.X;
              DeSelectTexture.Y := Texture.Y;
              //Fade Tex
              FadeTex.X := PosX;
              FadeTex.Y := PosY;
              FadeTex.W := Texture.W;
              FadeTex.H := (SelectH - DeSelectH) * FadeProgress;
              FadeTex.ScaleW := Texture.ScaleW;
              //Some Hack that Fixes a little Space between both Textures
              FadeTex.TexY2 := 0.9;
            end;
          1: //FadeTex on Left
            begin
              //Standard Texture
              Texture.X := PosX + (SelectW - DeSelectW) * FadeProgress;
              Texture.Y := PosY;
              DeSelectTexture.X := Texture.X;
              DeSelectTexture.Y := Texture.Y;
              //Fade Tex
              FadeTex.X := PosX;
              FadeTex.Y := PosY;
              FadeTex.H := Texture.H;
              FadeTex.W := (SelectW - DeSelectW) * FadeProgress;
              FadeTex.ScaleH := Texture.ScaleH;
              //Some Hack that Fixes a little Space between both Textures
              FadeTex.TexX2 := 0.9;
            end;
          2: //FadeTex on Bottom
            begin
              //Standard Texture
              Texture.X := PosX;
              Texture.Y := PosY;
              DeSelectTexture.X := Texture.X;
              DeSelectTexture.Y := Texture.Y;
              //Fade Tex
              FadeTex.X := PosX;
              FadeTex.Y := PosY  + (SelectH - DeSelectH) * FadeProgress;;
              FadeTex.W := Texture.W;
              FadeTex.H := (SelectH - DeSelectH) * FadeProgress;
              FadeTex.ScaleW := Texture.ScaleW;
              //Some Hack that Fixes a little Space between both Textures
              FadeTex.TexY1 := 0.1;
            end;
          3: //FadeTex on Right
            begin
              //Standard Texture
              Texture.X := PosX;
              Texture.Y := PosY;
              DeSelectTexture.X := Texture.X;
              DeSelectTexture.Y := Texture.Y;
              //Fade Tex
              FadeTex.X := PosX + (SelectW - DeSelectW) * FadeProgress;
              FadeTex.Y := PosY;
              FadeTex.H := Texture.H;
              FadeTex.W := (SelectW - DeSelectW) * FadeProgress;
              FadeTex.ScaleH := Texture.ScaleH;
              //Some Hack that Fixes a little Space between both Textures
              FadeTex.TexX1 := 0.1;
            end;
        end;
      end;
    end
    else if (FadeText) then
    begin
      Text[T].MoveX := (SelectW - DeSelectW);
      Text[T].MoveY := (SelectH - DeSelectH);
    end;

    if SelectBool or (FadeProgress > 0) or not Colorized then
      DrawTexture(Texture)
    else
    begin
      DeSelectTexture.X := Texture.X;
      DeSelectTexture.Y := Texture.Y;
      DeSelectTexture.H := Texture.H;
      DeSelectTexture.W := Texture.W;
      DrawTexture(DeSelectTexture);
    end;

    //Draw FadeTex
    if (FadeTex.TexNum > 0) then
      DrawTexture(FadeTex);

    if Texture2.Alpha > 0 then
    begin
      Texture2.ScaleW := Texture.ScaleW;
      Texture2.ScaleH := Texture.ScaleH;

      Texture2.X := Texture.X;
      Texture2.Y := Texture.Y;
      Texture2.W := Texture.W;
      Texture2.H := Texture.H;

      Texture2.ColR := Texture.ColR;
      Texture2.ColG := Texture.ColG;
      Texture2.ColB := Texture.ColB;
      Texture2.Int := Texture.Int;

      Texture2.Z := Texture.Z;

      DrawTexture(Texture2);
    end;

    //Reflection Mod
    if (Reflection) then // Draw Reflections
    begin
      if (FadeProgress <> 0) and (FadeProgress <> 1) then
      begin
        Spacing := DeSelectReflectionspacing - (DeSelectReflectionspacing - Reflectionspacing) * FadeProgress;
      end
      else if SelectBool then
        Spacing := Reflectionspacing
      else
        Spacing := DeSelectReflectionspacing;

      if SelectBool or not Colorized then
      with Texture do
      begin
        //Bind Tex and GL Attributes
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);

        glDepthRange(0, 10);
        glDepthFunc(GL_LEQUAL);
        glEnable(GL_DEPTH_TEST);

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBindTexture(GL_TEXTURE_2D, TexNum);

        //Draw
        glBegin(GL_QUADS);//Top Left
          glColor4f(ColR * Int, ColG * Int, ColB * Int, Alpha-0.3);
          glTexCoord2f(TexX1*TexW, TexY2*TexH);
          glVertex3f(x, y+h*scaleH+ Spacing, z);

          //Bottom Left
          glColor4f(ColR * Int, ColG * Int, ColB * Int, 0);
          glTexCoord2f(TexX1*TexW, TexY1+TexH*0.5);
          glVertex3f(x, y+h*scaleH + h*scaleH/2 + Spacing, z);


          //Bottom Right
          glColor4f(ColR * Int, ColG * Int, ColB * Int, 0);
          glTexCoord2f(TexX2*TexW, TexY1+TexH*0.5);
          glVertex3f(x+w*scaleW, y+h*scaleH + h*scaleH/2 + Spacing, z);

          //Top Right
          glColor4f(ColR * Int, ColG * Int, ColB * Int, Alpha-0.3);
          glTexCoord2f(TexX2*TexW, TexY2*TexH);
          glVertex3f(x+w*scaleW, y+h*scaleH + Spacing, z);
        glEnd;

        glDisable(GL_TEXTURE_2D);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_BLEND);
      end
      else
      with DeSelectTexture do
      begin
        //Bind Tex and GL Attributes
        glEnable(GL_TEXTURE_2D);
        glEnable(GL_BLEND);

        glDepthRange(0, 10);
        glDepthFunc(GL_LEQUAL);
        glEnable(GL_DEPTH_TEST);

        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glBindTexture(GL_TEXTURE_2D, TexNum);

        //Draw
        glBegin(GL_QUADS);//Top Left
          glColor4f(ColR * Int, ColG * Int, ColB * Int, Alpha-0.3);
          glTexCoord2f(TexX1*TexW, TexY2*TexH);
          glVertex3f(x, y+h*scaleH+ Spacing, z);

          //Bottom Left
          glColor4f(ColR * Int, ColG * Int, ColB * Int, 0);
          glTexCoord2f(TexX1*TexW, TexY1+TexH*0.5);
          glVertex3f(x, y+h*scaleH + h*scaleH/2 + Spacing, z);

          //Bottom Right
          glColor4f(ColR * Int, ColG * Int, ColB * Int, 0);
          glTexCoord2f(TexX2*TexW, TexY1+TexH*0.5);
          glVertex3f(x+w*scaleW, y+h*scaleH + h*scaleH/2 + Spacing, z);

          //Top Right
          glColor4f(ColR * Int, ColG * Int, ColB * Int, Alpha-0.3);
          glTexCoord2f(TexX2*TexW, TexY2*TexH);
          glVertex3f(x+w*scaleW, y+h*scaleH + Spacing, z);
        glEnd;

        glDisable(GL_TEXTURE_2D);
        glDisable(GL_DEPTH_TEST);
        glDisable(GL_BLEND);
      end;
    end;

    for T := 0 to High(Text) do
    begin
      Text[T].Draw;
    end;
  end;
end;

function TButton.GetMouseOverArea: TMouseOverRect;
begin
  if (Display.Cursor_HiddenByScreen = false) then
  begin
    if (FadeTex.TexNum = 0) then
    begin
      Result.X := Texture.X;
      Result.Y := Texture.Y;
      Result.W := Texture.W;
      Result.H := Texture.H;
    end
    else
    begin
      case FadeTexPos of
        0: begin // fade tex on top
          Result.X := Texture.X;
          Result.Y := FadeTex.Y;
          Result.W := Texture.W;
          Result.H := FadeTex.H + Texture.H;
        end;

        1: begin // fade tex on left side
          Result.X := FadeTex.X;
          Result.Y := Texture.Y;
          Result.W := FadeTex.W + Texture.W;
          Result.H := Texture.H;
        end;

        2: begin // fade tex on bottom
          Result.X := Texture.X;
          Result.Y := Texture.Y;
          Result.W := Texture.W;
          Result.H := FadeTex.H + Texture.H;
        end;

        3: begin // fade tex on right side
          Result.X := Texture.X;
          Result.Y := Texture.Y;
          Result.W := FadeTex.W + Texture.W;
          Result.H := Texture.H;
        end;
      end;
    end;
  end;
end;


destructor TButton.Destroy;
begin
  inherited;
end;

constructor TButton.Create();
begin
  inherited Create;
  // We initialize all to 0, nil or false
  Visible        := true;
  SelectBool     := false;
  DeselectType   := 0;
  Selectable     := true;
  Reflection     := false;
  Colorized      := false;

  SelectColR     := 1;
  SelectColG     := 1;
  SelectColB     := 1;
  SelectInt      := 1;
  SelectTInt     := 1;

  DeselectColR   := 1;
  DeselectColG   := 1;
  DeselectColB   := 1;
  DeselectInt    := 0.5;
  DeselectTInt   := 1;

  Fade           := false;
  FadeTex.TexNum := 0;
  FadeProgress   := 0;
  FadeText       := false;
  SelectW        := DeSelectW;
  SelectH        := DeSelectH;

  PosX           := 0;
  PosY           := 0;

  Parent         := 0;
end;

constructor TButton.Create(Textura: TTexture);
begin
  Create();
  Texture         := Textura;
  DeSelectTexture := Textura;
  Texture.ColR    := 0;
  Texture.ColG    := 0.5;
  Texture.ColB    := 0;
  Texture.Int     := 1;
  Colorized       := false;
end;

// Button has the texture-type "colorized"
// Two textures are generated, one with Col the other one with DCol
// Check UMenu.pas line 680 to see the call ( AddButton() )
constructor TButton.Create(Textura, DSTexture: TTexture);
begin
  Create();
  Texture         := Textura;
  DeSelectTexture := DSTexture;
  Texture.ColR    := 1;
  Texture.ColG    := 1;
  Texture.ColB    := 1;
  Texture.Int     := 1;
  Colorized       := true;
end;

end.