summaryrefslogtreecommitdiffhomepage
path: root/Stars45/Button.cpp
blob: 8c7e474e224df59a0e9687f120fc13ff231233ff (plain)
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
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    Button class
*/

#include "MemDebug.h"
#include "Button.h"
#include "Video.h"
#include "Bitmap.h"
#include "Font.h"
#include "Sound.h"
#include "DataLoader.h"

// +--------------------------------------------------------------------+

static Sound* button_sound       = 0;
static Sound* click_sound        = 0;
static Sound* swish_sound        = 0;
static Sound* chirp_sound        = 0;
static Sound* accept_sound       = 0;
static Sound* reject_sound       = 0;
static Sound* confirm_sound      = 0;
static Sound* list_select_sound  = 0;
static Sound* list_scroll_sound  = 0;
static Sound* list_drop_sound    = 0;
static Sound* combo_open_sound   = 0;
static Sound* combo_close_sound  = 0;
static Sound* combo_hilite_sound = 0;
static Sound* combo_select_sound = 0;
static Sound* menu_open_sound    = 0;
static Sound* menu_close_sound   = 0;
static Sound* menu_select_sound  = 0;
static Sound* menu_hilite_sound  = 0;

static int    gui_volume   = 0;

// +--------------------------------------------------------------------+

Button::Button(Screen* s, int ax, int ay, int aw, int ah, DWORD aid)
    : ActiveWindow(s, ax, ay, aw, ah, aid)
{
    animated       = true;
    bevel_width    = 5;
    border         = false;
    button_state   = 0;
    drop_shadow    = false;
    sticky         = false;
    picture_loc    = 1;
    captured       = false;
    pre_state      = 0;
    text_align     = DT_CENTER;

    standard_image    = 0;
    activated_image   = 0;
    transition_image  = 0;

    char buf[32];
    sprintf_s(buf, "Button %d", id); //-V576
    desc = buf;
}

Button::Button(ActiveWindow* p, int ax, int ay, int aw, int ah, DWORD aid)
    : ActiveWindow(p->GetScreen(), ax, ay, aw, ah, aid, 0, p)
{
    animated       = true;
    bevel_width    = 5;
    border         = false;
    button_state   = 0;
    drop_shadow    = false;
    sticky         = false;
    picture_loc    = 1;
    captured       = false;
    pre_state      = 0;
    text_align     = DT_CENTER;

    standard_image    = 0;
    activated_image   = 0;
    transition_image  = 0;

    char buf[32];
    sprintf_s(buf, "Button %d", id); //-V576
    desc = buf;
}

// +--------------------------------------------------------------------+

Button::~Button()
{
}

// +--------------------------------------------------------------------+

static void LoadInterfaceSound(DataLoader* loader, const char* wave, Sound*& s)
{
    loader->LoadSound(wave, s, 0, true);   // optional sound effect

    if (s)
    s->SetFlags(s->GetFlags() | Sound::INTERFACE);
}

void
Button::Initialize()
{
    DataLoader* loader = DataLoader::GetLoader();
    loader->SetDataPath("Sounds/");

    LoadInterfaceSound(loader, "button.wav",        button_sound);
    LoadInterfaceSound(loader, "click.wav",         click_sound);
    LoadInterfaceSound(loader, "swish.wav",         swish_sound);
    LoadInterfaceSound(loader, "chirp.wav",         chirp_sound);
    LoadInterfaceSound(loader, "accept.wav",        accept_sound);
    LoadInterfaceSound(loader, "reject.wav",        reject_sound);
    LoadInterfaceSound(loader, "confirm.wav",       confirm_sound);
    LoadInterfaceSound(loader, "list_select.wav",   list_select_sound);
    LoadInterfaceSound(loader, "list_scroll.wav",   list_scroll_sound);
    LoadInterfaceSound(loader, "list_drop.wav",     list_drop_sound);
    LoadInterfaceSound(loader, "combo_open.wav",    combo_open_sound);
    LoadInterfaceSound(loader, "combo_close.wav",   combo_close_sound);
    LoadInterfaceSound(loader, "combo_hilite.wav",  combo_hilite_sound);
    LoadInterfaceSound(loader, "combo_select.wav",  combo_select_sound);
    LoadInterfaceSound(loader, "menu_open.wav",     menu_open_sound);
    LoadInterfaceSound(loader, "menu_close.wav",    menu_close_sound);
    LoadInterfaceSound(loader, "menu_select.wav",   menu_select_sound);
    LoadInterfaceSound(loader, "menu_hilite.wav",   menu_hilite_sound);

    loader->SetDataPath(0);
}

// +--------------------------------------------------------------------+

void
Button::Close()
{
    delete button_sound;
    delete click_sound;
    delete swish_sound;
    delete chirp_sound;
    delete accept_sound;
    delete reject_sound;
    delete confirm_sound;
    delete list_select_sound;
    delete list_scroll_sound;
    delete list_drop_sound;
    delete combo_open_sound;
    delete combo_close_sound;
    delete combo_hilite_sound;
    delete combo_select_sound;
    delete menu_open_sound;
    delete menu_close_sound;
    delete menu_select_sound;
    delete menu_hilite_sound;
}

// +--------------------------------------------------------------------+

void
Button::Draw()
{
    if (!IsShown()) return;

    int x = 0;
    int y = 0;
    int w = rect.w;
    int h = rect.h;
    int img_w = picture.Width();
    int img_h = picture.Height();

    float old_alpha = alpha;

    if (!enabled)
    SetAlpha(0.35);

    Rect btn_rect(x,y,w,h);

    if (!transparent) {
        if (standard_image) {
            if (!enabled) {
                texture = standard_image;
            }

            else {
                switch (button_state) {
                case -1:
                    texture = activated_image;
                    break;

                default:
                case 0:
                    texture = standard_image;
                    break;

                case 1:
                    if (sticky)
                    texture = activated_image;
                    else
                    texture = transition_image;
                    break;

                case 2:
                    texture = transition_image;
                    break;
                }
            }

            if (!texture)
            texture = standard_image;

            DrawTextureGrid();
        }

        else {
            FillRect(0, 0, w, h, ShadeColor(back_color, 1.0));
            DrawStyleRect(0, 0, w, h, style);
        }
    }

    // draw the picture (if any)
    if (picture.Width()) {
        Rect irect = CalcPictureRect();
        DrawImage(&picture, irect);
    }

    // draw text here:
    if (font && text.length()) {
        Rect label_rect = CalcLabelRect(img_w,img_h);
        int  vert_space = label_rect.h;
        int  horz_space = label_rect.w;
        int  align      = DT_WORDBREAK | text_align;

        DrawText(text.data(), 0, label_rect, DT_CALCRECT | align);
        vert_space = (vert_space - label_rect.h)/2;

        label_rect.w = horz_space;

        if (vert_space > 0)
        label_rect.y += vert_space;

        if (animated && button_state > 0) {
            label_rect.x += button_state;
            label_rect.y += button_state;
        }

        if (drop_shadow) {
            label_rect.x++;
            label_rect.y++;

            font->SetColor(back_color);
            DrawText(text.data(), text.length(), label_rect, align);

            label_rect.x--;
            label_rect.y--;
        }

        font->SetColor(fore_color);
        DrawText(text.data(), text.length(), label_rect, align);
    }

    if (!enabled)
    SetAlpha(old_alpha);
}

Rect Button::CalcLabelRect(int img_w, int img_h)
{
    // fit the text in the bevel:
    Rect label_rect;
    label_rect.x = 0;
    label_rect.y = 0;
    label_rect.w = rect.w;
    label_rect.h = rect.h;

    if (text_align == DT_LEFT)
    label_rect.Deflate(bevel_width + 8, bevel_width + 1);
    else
    label_rect.Deflate(bevel_width + 1, bevel_width + 1);

    // and around the picture, if any:
    if (img_h != 0)
    {
        switch (picture_loc)
        {
        default:
        case 0:  // the four corner positions
        case 2:  // and the center position
        case 4:  // don't affect the text position
        case 6:
        case 8:
            break;

        case 1:  // north
            label_rect.y += img_h;
            label_rect.h -= img_h;
            break;

        case 3:  // west
            label_rect.x += img_w;
            label_rect.w -= img_w;
            break;

        case 5:  // east
            label_rect.w -= img_w;
            break;

        case 7:  // south
            label_rect.h -= img_h;
            break;
        }
    }

    return label_rect;
}

// +--------------------------------------------------------------------+

Rect
Button::CalcPictureRect()
{
    int w     = rect.w;
    int h     = rect.h;
    int img_w = picture.Width();
    int img_h = picture.Height();

    if (img_h > h) img_h = h-2;
    if (img_w > w) img_w = w-2;

    int img_x_offset = bevel_width;
    int img_y_offset = bevel_width;

    switch (picture_loc)
    {
    default:
        // TOP ROW:
    case  0: break;

    case  1: img_x_offset = (w/2-img_w/2);
        break;

    case  2: img_x_offset = w - img_w - bevel_width;
        break;

        // MIDDLE ROW:
    case  3: img_y_offset = (h/2-img_h/2);
        break;
    case  4: img_x_offset = (w/2-img_w/2);
        img_y_offset = (h/2-img_h/2);
        break;
    case  5: img_x_offset = w - img_w - bevel_width;
        img_y_offset = (h/2-img_h/2);
        break;

        // BOTTOM ROW:
    case  6:
        img_y_offset = h - img_h - bevel_width;
        break;
    case  7: img_x_offset = (w/2-img_w/2);
        img_y_offset = h - img_h - bevel_width;
        break;
    case  8: img_x_offset = w - img_w - bevel_width;
        img_y_offset = h - img_h - bevel_width;
        break;
    }

    Rect img_rect;
    img_rect.x = img_x_offset;
    img_rect.y = img_y_offset;

    if (animated && button_state > 0) {
        img_rect.x += button_state;
        img_rect.y += button_state;
    }

    img_rect.w = img_w;
    img_rect.h = img_h;

    return img_rect;
}

// +--------------------------------------------------------------------+

void
Button::DrawImage(Bitmap* bmp, const Rect& irect)
{
    if (bmp) {
        DrawBitmap(irect.x,
        irect.y,
        irect.x + irect.w,
        irect.y + irect.h,
        bmp,
        Video::BLEND_ALPHA);
    }
}

// +--------------------------------------------------------------------+

int Button::OnMouseMove(int x, int y)
{
    bool dirty = false;

    if (captured)
    {
        ActiveWindow* test = GetCapture();

        if (test != this)
        {
            captured = false;
            button_state = pre_state;
            dirty = true;
        }

        else if (sticky)
        {
            if (button_state == 2)
            {
                if (!rect.Contains(x,y))
                {
                    button_state = pre_state;
                    dirty = true;
                }
            }
            else
            {
                if (rect.Contains(x,y))
                {
                    button_state = 2;
                    dirty = true;
                }
            }
        }
        else
        {
            if (button_state == 1)
            {
                if (!rect.Contains(x,y))
                {
                    button_state = 0;
                    dirty = true;
                }
            }
            else
            {
                if (rect.Contains(x,y))
                {
                    button_state = 1;
                    dirty = true;
                }
            }
        }
    }

    return ActiveWindow::OnMouseMove(x,y);
}

int Button::OnLButtonDown(int x, int y)
{
    if (!captured)
    captured = SetCapture();

    if (sticky)
    button_state = 2;
    else
    button_state = 1;

    return ActiveWindow::OnLButtonDown(x,y);
}

int Button::OnLButtonUp(int x, int y)
{
    if (captured) {
        ReleaseCapture();
        captured = false;
    }

    button_state = pre_state;
    return ActiveWindow::OnLButtonUp(x,y);
}

void Button::SetVolume(int vol)
{
    if (vol >= -10000 && vol <= 0)
    gui_volume = vol;
}

void Button::PlaySound(int n)
{
    Sound* sound = 0;

    switch (n) {
    default:
    case SND_BUTTON:        if (button_sound)       sound = button_sound->Duplicate();        break;
    case SND_CLICK:         if (click_sound)        sound = click_sound->Duplicate();         break;
    case SND_SWISH:         if (swish_sound)        sound = swish_sound->Duplicate();         break;
    case SND_CHIRP:         if (chirp_sound)        sound = chirp_sound->Duplicate();         break;
    case SND_ACCEPT:        if (accept_sound)       sound = accept_sound->Duplicate();        break;
    case SND_REJECT:        if (reject_sound)       sound = reject_sound->Duplicate();        break;
    case SND_CONFIRM:       if (confirm_sound)      sound = confirm_sound->Duplicate();       break;
    case SND_LIST_SELECT:   if (list_select_sound)  sound = list_select_sound->Duplicate();   break;
    case SND_LIST_SCROLL:   if (list_scroll_sound)  sound = list_scroll_sound->Duplicate();   break;
    case SND_LIST_DROP:     if (list_drop_sound)    sound = list_drop_sound->Duplicate();     break;
    case SND_COMBO_OPEN:    if (combo_open_sound)   sound = combo_open_sound->Duplicate();    break;
    case SND_COMBO_CLOSE:   if (combo_close_sound)  sound = combo_close_sound->Duplicate();   break;
    case SND_COMBO_HILITE:  if (combo_hilite_sound) sound = combo_hilite_sound->Duplicate();  break;
    case SND_COMBO_SELECT:  if (combo_select_sound) sound = combo_select_sound->Duplicate();  break;
    case SND_MENU_OPEN:     if (menu_open_sound)    sound = menu_open_sound->Duplicate();     break;
    case SND_MENU_CLOSE:    if (menu_close_sound)   sound = menu_close_sound->Duplicate();    break;
    case SND_MENU_SELECT:   if (menu_select_sound)  sound = menu_select_sound->Duplicate();   break;
    case SND_MENU_HILITE:   if (menu_hilite_sound)  sound = menu_hilite_sound->Duplicate();   break;
    }

    if (sound) {
        sound->SetVolume(gui_volume);
        sound->Play();
    }
}

int Button::OnClick()
{
    PlaySound(SND_BUTTON);

    if (sticky)
    button_state = !pre_state;

    pre_state = button_state;

    return ActiveWindow::OnClick();
}

int Button::OnMouseEnter(int mx, int my)
{
    if (button_state >= 0)
    pre_state = button_state;

    if (button_state == 0)
    button_state = -1;

    if (IsEnabled() && IsShown())
    PlaySound(SND_SWISH);

    return ActiveWindow::OnMouseEnter(mx, my);
}

int Button::OnMouseExit(int mx, int my)
{
    if (button_state == -1)
    button_state = pre_state;

    return ActiveWindow::OnMouseExit(mx, my);
}

// +--------------------------------------------------------------------+

void Button::SetStandardImage(Bitmap* img)
{
    standard_image = img;
    texture        = standard_image;
}

void Button::SetActivatedImage(Bitmap* img)
{
    activated_image = img;
}

void Button::SetTransitionImage(Bitmap* img)
{
    transition_image = img;
}

// +--------------------------------------------------------------------+

short Button::GetBevelWidth()
{
    return bevel_width;
}

void Button::SetBevelWidth(short nNewValue)
{
    if (nNewValue < 0) nNewValue = 0;
    if (nNewValue > rect.w/2) nNewValue = rect.w/2;
    bevel_width = nNewValue;
}

bool  Button::GetBorder()
{
    return border;
}

void  Button::SetBorder(bool bNewValue)
{
    border = bNewValue;
}

Color  Button::GetBorderColor()
{
    return border_color;
}

void  Button::SetBorderColor(Color newValue)
{
    border_color = newValue;
}

Color  Button::GetActiveColor()
{
    return active_color;
}

void  Button::SetActiveColor(Color newValue)
{
    active_color = newValue;
}

bool  Button::GetAnimated()
{
    return animated;
}

void  Button::SetAnimated(bool bNewValue)
{
    animated = bNewValue;
}

bool  Button::GetDropShadow()
{
    return drop_shadow;
}

void  Button::SetDropShadow(bool bNewValue)
{
    drop_shadow = bNewValue;
}

// +--------------------------------------------------------------------+

short Button::GetButtonState()
{
    return button_state;
}

void  Button::SetButtonState(short n)
{
    if (button_state != n && n >= -2 && n <= 2) {
        button_state = n;
        pre_state    = n;
    }
}

void  Button::GetPicture(Bitmap& img)
{
    img.CopyBitmap(picture);
}

void  Button::SetPicture(const Bitmap& img)
{
    picture.CopyBitmap(img);
    picture.AutoMask();
}

short Button::GetPictureLocation()
{
    return picture_loc;
}

void  Button::SetPictureLocation(short n)
{
    if (picture_loc != n && n >= 0 && n <= 8) {
        picture_loc = n;
    }
}

bool  Button::GetSticky()
{
    return sticky;
}

void  Button::SetSticky(bool n)
{
    if (sticky != n)
    sticky = n;
}