summaryrefslogtreecommitdiffhomepage
path: root/StarsEx/Slider.cpp
blob: 2970ac6ac83268d95dda31b121783eead6905cf5 (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
/*  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
    ========
    Slider/Gauge ActiveWindow class
*/

#include "Slider.h"
#include "Video.h"
#include "Font.h"

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

Slider::Slider(ActiveWindow* p, int ax, int ay, int aw, int ah, DWORD aid)
: ActiveWindow(p->GetScreen(), ax, ay, aw, ah, aid, 0, p)
{
    captured       = false;
    dragging       = false;

    border_color   = Color::Black;
    fill_color     = Color::DarkBlue;

    active         = true;
    border         = true;
    num_leds       = 1;
    orientation    = 0;

    range_min      = 0;
    range_max      = 100;
    step_size      = 10;
    show_thumb     = 0;
    thumb_size     = -1;

    nvalues        = 1;
    ZeroMemory(value, sizeof(value));

    marker[0]      = -1;
    marker[1]      = -1;

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

Slider::Slider(Screen* s, int ax, int ay, int aw, int ah, DWORD aid)
: ActiveWindow(s, ax, ay, aw, ah, aid)
{
    captured       = false;
    dragging       = false;

    border_color   = Color::Black;
    fill_color     = Color::DarkBlue;

    active         = true;
    border         = true;
    num_leds       = 1;
    orientation    = 0;

    range_min      = 0;
    range_max      = 100;
    step_size      = 10;
    show_thumb     = 0;
    thumb_size     = -1;

    nvalues        = 1;
    ZeroMemory(value, sizeof(value));

    marker[0]      = -1;
    marker[1]      = -1;

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

Slider::~Slider()
{
}

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

void
Slider::Draw()
{
    int x = 0;
    int y = 0;
    int w = rect.w;
    int h = rect.h;

    if (w < 1 || h < 1 || !shown)
    return;

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

    // draw the border:
    if (border) {
        Color oc = ShadeColor(border_color, 1);
        DrawRect(0,0,w-1,h-1,oc);
        DrawRect(1,1,w-2,h-2,oc);

        ctrl_rect.Deflate(2,2);
    }

    // draw the bevel:
    FillRect(ctrl_rect, back_color);
    DrawStyleRect(ctrl_rect, WIN_SUNK_FRAME);

    // HORIZONTAL
    if (orientation == 0) {
        // draw the leds:
        int led_width  = ((w - 6) / (num_leds)) - 1;
        int led_height = ((h - 5) / (nvalues))  - 1;

        if (nvalues < 2) {
            int fill_width  = (int) ((double)(w-4) * FractionalValue());
            int num_lit     = fill_width / (led_width+1);

            Color fc = ShadeColor(fill_color, 1);

            if (num_leds == 1) {
                FillRect(2,2,2+fill_width,h-3,fc);
            }
            else {
                int x0 = 2;

                for (int i = 0; i < num_lit; i++) {
                    FillRect(x0,2,x0+led_width,h-3,fc);
                    x0 += led_width + 1;
                }
            }

            // draw the thumb:
            if (thumb_size) {
                if (thumb_size < 0) thumb_size = h;

                thumb_pos = 2+fill_width;

                Rect thumb_rect(thumb_pos-thumb_size/2, 0, thumb_size, h);

                if (thumb_rect.x < 0)
                thumb_rect.x = 0;

                else if (thumb_rect.x > w-thumb_size)
                thumb_rect.x = w-thumb_size;

                if (show_thumb) {
                    FillRect(thumb_rect, back_color);
                    DrawStyleRect(thumb_rect, WIN_RAISED_FRAME);
                }
            }
        }

        else {
            Color fc = ShadeColor(fill_color, 1);

            int y0 = 3;

            for (int i = 0; i < nvalues; i++) {
                int fill_width  = (int) ((double)(w-6) * FractionalValue(i));
                FillRect(3,y0,3+fill_width,y0+led_height,fc);

                y0 += led_height+1;
            }
        }

        // draw the markers:
        if (marker[0] >= 0) {
            int   m = marker[0];
            Color c = ShadeColor(base_color, 1.6);    // full highlight
            DrawLine(m-3, 1, m+4,  1, c);
            c = ShadeColor(base_color, 1.3);          // soft highlight
            DrawLine(m-3, 2, m-3,  4, c);
            DrawLine(m-2, 4, m-2,  6, c);
            DrawLine(m-1, 6, m-1,  8, c);
            DrawLine(m  , 8, m  , 10, c);
            c = base_color;                           // body
            DrawLine(m-2, 2, m-2,  4, c);
            DrawLine(m-1, 2, m-1,  6, c);
            DrawLine(m  , 2, m  ,  8, c);
            DrawLine(m+1, 2, m+1,  6, c);
            DrawLine(m+2, 2, m+2,  4, c);
            c = ShadeColor(base_color, 0.5);          // shadow
            DrawLine(m+1, 6, m+1,  8, c);
            DrawLine(m+2, 4, m+2,  6, c);
            DrawLine(m+3, 2, m+3,  4, c);
        }

        if (marker[1] >= 0) {
            int   m = marker[0];
            Color c = ShadeColor(base_color, 0.5);    // shadow
            DrawLine(m-3, h-2, m+4, h-2, c);
            DrawLine(m+1, h-6, m+1, h-8, c);
            DrawLine(m+2, h-4, m+2, h-6, c);
            DrawLine(m+3, h-2, m+3, h-4, c);
            c = ShadeColor(base_color, 1.3);          // soft highlight
            DrawLine(m-3, h-2, m-3, h-4, c);
            DrawLine(m-2, h-4, m-2, h-6, c);
            DrawLine(m-1, h-6, m-1, h-8, c);
            DrawLine(m  , h-8, m  , h-10, c);
            c = base_color;                           // body
            DrawLine(m-2, h-2, m-2, h-4, c);
            DrawLine(m-1, h-2, m-1, h-6, c);
            DrawLine(m  , h-2, m  , h-8, c);
            DrawLine(m+1, h-2, m+1, h-6, c);
            DrawLine(m+2, h-2, m+2, h-4, c);
        }
    }

    // VERTICAL
    else {
        // draw the leds:
        int led_width  = ((w - 5) / (nvalues))  - 1;

        if (num_leds > 1) {
        }
        else {
            if (nvalues < 2) {
                led_width  = w - 4;
                int led_height = (int) ((double)(h-4) * FractionalValue());

                Color fc = ShadeColor(fill_color, 1);
                FillRect(2, h-2-led_height, 2+led_width, h-2, fc);

                // draw the thumb:
                if (thumb_size) {
                    if (thumb_size < 0) thumb_size = w;

                    thumb_pos = h-2-led_height;

                    Rect thumb_rect(0, thumb_pos-(thumb_size/2), w, thumb_size);

                    if (thumb_rect.y < 0)
                    thumb_rect.y = 0;

                    else if (thumb_rect.y > h-thumb_size)
                    thumb_rect.y = h-thumb_size;

                    if (show_thumb) {
                        FillRect(thumb_rect, back_color);
                        DrawStyleRect(thumb_rect, WIN_RAISED_FRAME);
                    }
                }
            }

            else {
                Color fc = ShadeColor(fill_color, 1);

                int x0 = 3;

                for (int i = 0; i < nvalues; i++) {
                    int led_height = (int) ((double)(h-6) * FractionalValue(i));
                    FillRect(x0,h-3-led_height,x0+led_width,h-3,fc);

                    x0 += led_width+1;
                }
            }
        }
    }
}

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

bool Slider::GetActive()
{
    return active;
}

void  Slider::SetActive(bool bNewValue)
{
    active = bNewValue;
}

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

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

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

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

Color Slider::GetFillColor()
{
    return fill_color;
}

void  Slider::SetFillColor(Color cNewValue)
{
    fill_color = cNewValue;
}

int   Slider::GetNumLeds()
{
    return num_leds;
}

void  Slider::SetNumLeds(int nNewValue)
{
    if (nNewValue >= 0) {
        num_leds = nNewValue;
    }
}

int   Slider::GetOrientation()
{
    return orientation;
}

void  Slider::SetOrientation(int nNewValue)
{
    if (nNewValue == 0 || nNewValue == 1) {
        orientation = nNewValue;
    }
}

int   Slider::GetRangeMin()
{
    return range_min;
}

void  Slider::SetRangeMin(int nNewValue)
{
    range_min = nNewValue;
}

int   Slider::GetRangeMax()
{
    return range_max;
}

void  Slider::SetRangeMax(int nNewValue)
{
    range_max = nNewValue;
}

int   Slider::GetStepSize()
{
    return step_size;
}

void  Slider::SetStepSize(int nNewValue)
{
    if (nNewValue < range_max - range_min)
    step_size = nNewValue;
}

bool  Slider::GetShowThumb()
{
    return show_thumb?true:false;
}

void  Slider::SetShowThumb(bool bNewValue)
{
    show_thumb = bNewValue;
}

int   Slider::GetThumbSize()
{
    return thumb_size;
}

void  Slider::SetThumbSize(int nNewValue)
{
    if (nNewValue < range_max - range_min)
    thumb_size = nNewValue;
}

int   Slider::NumValues()
{
    return nvalues;
}

int   Slider::GetValue(int index)
{
    if (index >= 0 && index < nvalues)
    return value[index];

    return 0;
}

void  Slider::SetValue(int nNewValue, int index)
{
    if (index >= 0 && index < MAX_VAL) {
        value[index] = nNewValue;

        if (index >= nvalues)
        nvalues = index+1;
    }
}

void  Slider::SetMarker(int nNewValue, int index)
{
    if (index >= 0 && index < 2) {
        marker[index] = nNewValue;
    }
}

double Slider::FractionalValue(int index)
{
    if (index >= 0 && index < nvalues)
    return ((double) (value[index]-range_min)) / ((double) (range_max-range_min));

    return 0;
}

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

void  Slider::StepUp(int index)
{
    if (index >= 0 && index < nvalues) {
        value[index] += step_size;

        if (value[index] > range_max)
        value[index] = range_max;
    }
}

void  Slider::StepDown(int index)
{
    if (index >= 0 && index < nvalues) {
        value[index] -= step_size;

        if (value[index] < range_min)
        value[index] = range_min;
    }
}

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

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

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

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

        else if (dragging)
        {
            mouse_x = x - rect.x;
            if (mouse_x < 0) mouse_x = 0;
            else if (mouse_x > rect.w) mouse_x = rect.w;

            mouse_y = rect.h - (y - rect.y);
            if (mouse_y < 0) mouse_y = 0;
            else if (mouse_y > rect.h) mouse_y = rect.h;

            // HORIZONTAL
            if (orientation == 0) {
                SetValue((int) ((double) mouse_x/rect.w * (range_max-range_min) + range_min));
            }

            // VERTICAL
            else {
                SetValue((int) ((double) mouse_y/rect.h * (range_max-range_min) + range_min));
            }

            dirty = true;
        }
    }

    if (dirty)
    OnClick();

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

int Slider::OnLButtonDown(int x, int y)
{
    if (!active)
    return 0;

    if (!captured)
    captured = SetCapture();

    mouse_x = x - rect.x;
    mouse_y = y - rect.y;

    // HORIZONTAL
    if (orientation == 0) {
        if (mouse_x < thumb_pos-thumb_size/2) {
            StepDown();
        }

        else if (mouse_x > thumb_pos+thumb_size/2) {
            StepUp();
        }

        else {
            dragging = true;
        }
    }

    // VERTICAL
    else {
        if (mouse_y < thumb_pos-thumb_size/2) {
            StepUp();
        }

        else if (mouse_y > thumb_pos+thumb_size/2) {
            StepDown();
        }

        else {
            dragging = true;
        }
    }

    if (!dragging)
    OnClick();

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

int Slider::OnLButtonUp(int x, int y)
{
    if (!active)
    return 0;

    if (captured) {
        ReleaseCapture();
        captured = 0;
        dragging = false;
    }

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

int Slider::OnClick()
{
    return ActiveWindow::OnClick();
}