~ubuntu-branches/ubuntu/hoary/kdeaddons/hoary

« back to all changes in this revision

Viewing changes to noatun-plugins/tippercanoe/ui.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ben Burton
  • Date: 2002-04-01 16:42:29 UTC
  • Revision ID: james.westby@ubuntu.com-20020401164229-oxc5htazpgzyqygi
Tags: upstream-2.2.2
ImportĀ upstreamĀ versionĀ 2.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "font.h"
 
2
#include "icons.h"
 
3
#include "syna.h"
 
4
#include <math.h>
 
5
#include <stdlib.h>
 
6
#include <kconfig.h>
 
7
 
 
8
#define output core->output()
 
9
 
 
10
static void putChar(unsigned char character,int x,int y,int red,int blue)
 
11
{
 
12
        unsigned short *ptr = ((unsigned short *)output) + x + y*core->outWidth;
 
13
        unsigned short  put = (blue<<8)+red;
 
14
        int i,j;
 
15
        for(i=0;i<8;i++,ptr += core->outWidth-8)
 
16
                for(j=0;j<8;j++,ptr++)
 
17
                        if (font[character*8+i] & (128>>j))
 
18
                                *ptr = put;
 
19
}
 
20
 
 
21
void Interface::putString(char *string,int x,int y,int red,int blue)
 
22
{
 
23
        if (x < 0 || y < 0 || y >= core->outHeight-8)
 
24
                return;
 
25
        for(;*string && x <= core->outWidth-8;string++,x+=8)
 
26
                putChar((unsigned char)(*string),x,y,red,blue);
 
27
}
 
28
 
 
29
void UIObject::changed()
 
30
{
 
31
}
 
32
 
 
33
struct Button : public UIObject
 
34
{
 
35
        int icon;
 
36
        char hotKey;
 
37
 
 
38
        bool passive, bright;
 
39
 
 
40
        Button(int _am,int _vm,double _x,double _y,
 
41
                        double _size,int _icon,char _key = 0, 
 
42
                        bool _passive = false, bool _bright = false)
 
43
        {
 
44
                activeMask = _am; visibleMask = _vm;
 
45
                x = _x; y = _y; width = height = _size;
 
46
                icon = _icon; hotKey = _key; passive = _passive; bright = _bright;
 
47
        }
 
48
 
 
49
        int go(bool mouseDown,bool mouseClick,bool mouseOver,
 
50
                        double _x, double _y, double scale, char &_hotKey, int &action)
 
51
        {
 
52
                (void)mouseDown;
 
53
                (void)_x;
 
54
                (void)_y;
 
55
                core->polygonEngine.icon(
 
56
                                Icons[icon],
 
57
                                (bright ? 0x0202 : 0x0100),
 
58
                                x*scale,y*scale,width*scale,height*scale);
 
59
 
 
60
                if (mouseOver && !passive)
 
61
                        core->polygonEngine.icon(
 
62
                                        Icons[icon],
 
63
                                        0x0002,
 
64
                                        (x-IconWidths[icon]*width/2)*scale,
 
65
                                        (y-height/2)*scale,width*scale*2,height*scale*2);
 
66
 
 
67
                if (mouseOver && mouseClick && !passive)
 
68
                        action = icon;
 
69
 
 
70
                if (mouseOver && !passive && hotKey)
 
71
                        _hotKey = hotKey;
 
72
 
 
73
                return 0;
 
74
        }
 
75
 
 
76
        void handleKey(char key, int &action)
 
77
        {
 
78
                if (key == hotKey && !passive)
 
79
                        action = icon;
 
80
        }
 
81
};
 
82
#define BarWidth 0.1
 
83
struct SliderBar : public UIObject
 
84
{
 
85
        double *value;
 
86
        char leftKey, rightKey;
 
87
 
 
88
        typedef void (Core::*Callback)(double v);
 
89
        Callback callback;
 
90
 
 
91
        SliderBar(int _am,int _vm,double _x,double _y,
 
92
                        double _width,double _height, double *_value,
 
93
                        Callback _callback, char _leftKey, char _rightKey)
 
94
        {
 
95
                activeMask = _am; visibleMask = _vm;
 
96
                x = _x; y = _y; width = _width; height = _height;
 
97
                value = _value; callback = _callback;
 
98
                leftKey = _leftKey; rightKey = _rightKey;
 
99
        }
 
100
 
 
101
        int go(bool mouseDown,bool mouseClick,bool mouseOver,
 
102
                        double _x, double _y, double scale, char &_hotKey, int &action)
 
103
        {
 
104
                (void)mouseClick;
 
105
                (void)_y;
 
106
                (void)action;
 
107
                core->polygonEngine.icon(
 
108
                        Icons[Bar],
 
109
                        0x0100,
 
110
                        x*scale,y*scale,width*scale,height*scale);
 
111
                core->polygonEngine.icon(
 
112
                        Icons[Slider],
 
113
                        0x0200,
 
114
                        (x+*value*width-IconWidths[Slider]*height/2)*scale,
 
115
                        y*scale,height*scale,height*scale);
 
116
 
 
117
                if (mouseOver)
 
118
                {
 
119
                        double newValue = (_x)/(width);
 
120
                        if (newValue < 0.0) newValue = 0.0;
 
121
                        if (newValue > 1.0) newValue = 1.0;
 
122
 
 
123
                        core->polygonEngine.icon(
 
124
                                        Icons[Selector],
 
125
                                        0x0002,
 
126
                                        (x+newValue*width-IconWidths[Selector]*height/2)*scale,
 
127
                                        y*scale,height*scale,height*scale);
 
128
 
 
129
                        if (mouseDown)
 
130
                        {
 
131
                                *value = newValue;
 
132
 
 
133
                                if (callback)
 
134
                                        (core->*callback)(*value);
 
135
                                changed();
 
136
                        }
 
137
 
 
138
                        if (mouseOver)
 
139
                                _hotKey = (newValue < *value ? leftKey : rightKey);
 
140
                }
 
141
 
 
142
                return 0;
 
143
        }
 
144
        void handleKey(char key, int &action)
 
145
        {
 
146
                (void)action;
 
147
                if (key == leftKey || key == rightKey)
 
148
                {
 
149
                        if (key == leftKey)
 
150
                        {
 
151
                                if (*value == 0.0) return;
 
152
                                *value -= 0.05;
 
153
                                if (*value < 0.0) *value = 0.0;
 
154
                        }
 
155
                        else
 
156
                        {
 
157
                                if (*value == 1.0) return;
 
158
                                *value += 0.05;
 
159
                                if (*value > 1.0) *value = 1.0;
 
160
                        }
 
161
 
 
162
                        if (callback)
 
163
                                (core->*callback)(*value);
 
164
                        changed();
 
165
                }
 
166
        }
 
167
};
 
168
#undef BarWidth
 
169
 
 
170
 
 
171
struct PopperUpper : public UIObject
 
172
{
 
173
        int maskAdd;
 
174
 
 
175
        PopperUpper(int _am,int _vm,double _x,double _y,
 
176
                        double _width,double _height, int _maskAdd)
 
177
        {
 
178
                activeMask = _am; visibleMask = _vm; 
 
179
                x = _x; y = _y; width = _width; height = _height;
 
180
                maskAdd = _maskAdd;
 
181
        }
 
182
 
 
183
        int go(bool mouseDown,bool mouseClick, bool mouseOver,
 
184
                        double _x, double _y, double scale, char &_hotKey, int &action)
 
185
        {
 
186
                (void)mouseDown;
 
187
                (void)mouseClick;
 
188
                (void)_x;
 
189
                (void)_y;
 
190
                (void)_hotKey;
 
191
                (void)action;
 
192
                
 
193
                core->polygonEngine.icon(
 
194
                                Icons[Box],
 
195
                                0x0200,
 
196
                                x*scale,y*scale,width*scale,height*scale);
 
197
 
 
198
                return mouseOver ? maskAdd : 0;
 
199
        }
 
200
 
 
201
        void handleKey(char key, int &action)
 
202
        {
 
203
                (void)key;
 
204
                (void)action;
 
205
        }
 
206
};
 
207
 
 
208
void Interface::setupPalette()
 
209
{
 
210
#define BOUND(x) ((x) > 255 ? 255 : (x))
 
211
#define PEAKIFY(x) int(BOUND((x) - (x)*(255-(x))/255/2))
 
212
#define MAX(x,y) ((x) > (y) ? (x) : (y))
 
213
        int i;
 
214
        unsigned char palette[768];
 
215
 
 
216
        double scale, fgRed, fgGreen, fgBlue, bgRed, bgGreen, bgBlue;
 
217
        fgRed = core->fgRedSlider;
 
218
        fgGreen = core->fgGreenSlider;
 
219
        fgBlue = 1.0 - MAX(core->fgRedSlider,core->fgGreenSlider);
 
220
        scale = MAX(MAX(fgRed,fgGreen),fgBlue);
 
221
        fgRed /= scale;
 
222
        fgGreen /= scale;
 
223
        fgBlue /= scale;
 
224
 
 
225
        bgRed = core->bgRedSlider;
 
226
        bgGreen = core->bgGreenSlider;
 
227
        bgBlue = 1.0 - MAX(core->bgRedSlider, core->bgGreenSlider);
 
228
        scale = MAX(MAX(bgRed, bgGreen), bgBlue);
 
229
        bgRed /= scale;
 
230
        bgGreen /= scale;
 
231
        bgBlue /= scale;
 
232
 
 
233
        for(i=0;i<256;i++)
 
234
        {
 
235
                int f = i&15, b = i/16;
 
236
                palette[i*3+0] = PEAKIFY(b*bgRed*16+f*fgRed*16);
 
237
                palette[i*3+1] = PEAKIFY(b*bgGreen*16+f*fgGreen*16);
 
238
                palette[i*3+2] = PEAKIFY(b*bgBlue*16+f*fgBlue*16);
 
239
        }
 
240
        core->screen->setPalette(palette);
 
241
#undef BOUND
 
242
#undef PEAKIFY
 
243
#undef MAX
 
244
}
 
245
 
 
246
//Visible mask
 
247
#define ALL 1
 
248
#define BUTTONBAR 2
 
249
#define TRACKBAR 4
 
250
#define DIALBAR 8
 
251
#define VOLUMEBAR 16
 
252
 
 
253
//Active mask
 
254
//#define ALL 1
 
255
#define PLAYING 2
 
256
#define PAUSED 4
 
257
#define STOPPED 8
 
258
#define NOCD 32 
 
259
#define OPEN 64 
 
260
 
 
261
 
 
262
// TODO Lay things out with parents and a stack, like QT
 
263
Interface::Interface()
 
264
{
 
265
        static const float IconSize=0.2;
 
266
        static const float SliderSize=0.125;
 
267
 
 
268
        {
 
269
                KConfig config("noatun/synaescope", false, false, "data");
 
270
                core->fadeMode=(SymbolID)config.readNumEntry("mode", (int)Stars);
 
271
                core->pointsAreDiamonds=config.readBoolEntry("diamonds", false);
 
272
                core->brightnessTwiddler=config.readDoubleNumEntry("brightness", .4);
 
273
                core->starSize=config.readDoubleNumEntry("starsize", .1);
 
274
                core->fgRedSlider=config.readDoubleNumEntry("FGRed", 0.0);
 
275
                core->fgGreenSlider=config.readDoubleNumEntry("FGgreen", 1.0);
 
276
                core->bgRedSlider=config.readDoubleNumEntry("BGRed", 0.0);
 
277
                core->bgGreenSlider=config.readDoubleNumEntry("BGGreen", 0.0);
 
278
        }
 
279
        
 
280
        uiObjects.setAutoDelete(true);
 
281
 
 
282
        double x,y;
 
283
        
 
284
        //addUI(new Button(ALL,0.025,0.525,IconSize, 0, 'x'));
 
285
//      addUI(new PopperUpper(ALL,ALL,0,0,0.25,0.25, BUTTONBAR));
 
286
//      addUI(stateButton = new Button(ALL,ALL,0.05,0.025,IconSize, 0, 0, true, false));
 
287
 
 
288
        addUI(new PopperUpper(ALL,BUTTONBAR,x=0.25,y=0,1.375,0.25, BUTTONBAR));
 
289
//      x += 0.1; y += 0.025;
 
290
 
 
291
//      addUI(new PopperUpper(PLAYING|PAUSED|STOPPED, ALL,0,0.25,0.25,0.25, TRACKBAR));
 
292
//      addUI(new PopperUpper(PLAYING|PAUSED|STOPPED, TRACKBAR,x=0.25,y=0.25,1.0,0.625, TRACKBAR));
 
293
//      x += 0.1; y += 0.1;
 
294
 
 
295
        addUI(new PopperUpper(ALL,ALL,0,0.0,0.25,0.25, DIALBAR));
 
296
        addUI(new Button(ALL,ALL,0.075,0.05,IconSize, Bulb, 0, true, false));
 
297
 
 
298
        addUI(new PopperUpper(ALL,DIALBAR,x=0.25,y=0.0,1.25,1.0, DIALBAR));
 
299
        x += 0.05; y += 0.025;
 
300
 
 
301
        addUI(starsButton = new Button(ALL,DIALBAR,x,y,IconSize, Stars, 'd'));
 
302
        addUI(waveButton = new Button(ALL,DIALBAR,x+IconSize,y,IconSize, Wave, 'f'));
 
303
        addUI(flameButton = new Button(ALL,DIALBAR,x+IconSize*2.5,y,IconSize, Flame, 'g'));
 
304
 
 
305
        addUI(starButton = new Button(ALL,DIALBAR,x+IconSize*3.5,y,IconSize, Star, 'h'));
 
306
        addUI(diamondButton = new Button(ALL,DIALBAR,x+IconSize*4.5,y,IconSize, Diamond, 'j'));
 
307
 
 
308
        y += IconSize*1.3;
 
309
 
 
310
        addUI(new Button(ALL,DIALBAR,x,y-0.05,IconSize, Bulb, 0, true));
 
311
        addUI(new SliderBar(ALL,DIALBAR,
 
312
                x+IconSize,y, 0.75, SliderSize, &core->brightnessTwiddler, /*&Core::setBrightness,*/0, 'z', 'x'));
 
313
 
 
314
        addUI(new Button(ALL,DIALBAR,x,y+SliderSize*1,IconSize, Size, 'x', true));
 
315
        addUI(new SliderBar(ALL,DIALBAR,
 
316
                x+IconSize,y+SliderSize, 0.75, SliderSize, &core->starSize, &Core::setStarSize, 'c','v'));
 
317
 
 
318
        addUI(new Button(ALL,DIALBAR,x+0.5,y+SliderSize*2-0.025,IconSize, FgColor, 0, true));
 
319
        addUI(new SliderBar(ALL,DIALBAR,
 
320
                x,y+SliderSize*2, 0.45, SliderSize, &(core->fgRedSlider), &Core::setupPalette, 'b','n'));
 
321
        addUI(new SliderBar(ALL,DIALBAR,
 
322
                                x+0.5+SliderSize,y+SliderSize*2, 0.45, SliderSize, &core->fgGreenSlider, &Core::setupPalette, 'm',','));
 
323
 
 
324
        addUI(new Button(ALL,DIALBAR,x+0.5,y+SliderSize*3,IconSize, BgColor, 0, true));
 
325
        addUI(new SliderBar(ALL,DIALBAR,
 
326
                                x,y+SliderSize*3, 0.45, SliderSize, &core->bgRedSlider, &Core::setupPalette, 'B','N'));
 
327
        addUI(new SliderBar(ALL,DIALBAR,
 
328
                                x+0.5+SliderSize,y+SliderSize*3, 0.45, SliderSize, &core->bgGreenSlider, &Core::setupPalette, 'M','<'));
 
329
 
 
330
        x += 0.1;// y += 0.0625;
 
331
        //static double value = 0.5;
 
332
        //addUI(new SliderBar(ALL,0,0.75,1.0,0.25,&value)); 
 
333
 
 
334
        //addUI(new Button(BUTTONBAR,x,y,IconSize, 1, 'x'));
 
335
        //addUI(new Button(BUTTONBAR,x += IconSize,y,IconSize, 2, 'x'));
 
336
        //addUI(new Button(BUTTONBAR,x += IconSize,y,IconSize, 3, 'x'));
 
337
 
 
338
        visibleMask = ALL;
 
339
        mouseX = -1;
 
340
        mouseY = -1;
 
341
        lastY = -1;
 
342
        lastY = -1;
 
343
        countDown = 0;
 
344
        mouseButtons = 0;
 
345
 
 
346
        syncToState();
 
347
        
 
348
}
 
349
 
 
350
Interface::~Interface()
 
351
{
 
352
        KConfig config("noatun/synaescope", false, false, "data");
 
353
        config.writeEntry("mode",core->fadeMode);
 
354
        config.writeEntry("diamonds", core->pointsAreDiamonds);
 
355
        config.writeEntry("brightness", core->brightnessTwiddler);
 
356
        config.writeEntry("starsize", core->starSize);
 
357
        config.writeEntry("FGRed", core->fgRedSlider);
 
358
        config.writeEntry("FGgreen", core->fgGreenSlider);
 
359
        config.writeEntry("BGRed", core->bgRedSlider);
 
360
        config.writeEntry("BGGreen", core->bgGreenSlider);
 
361
}
 
362
 
 
363
void Interface::addUI(UIObject *obj)
 
364
{
 
365
        uiObjects.append(obj);
 
366
}
 
367
 
 
368
void Interface::syncToState()
 
369
{
 
370
        starsButton->bright = (core->fadeMode == Stars); 
 
371
        flameButton->bright = (core->fadeMode == Flame); 
 
372
        waveButton->bright = (core->fadeMode == Wave);
 
373
 
 
374
        starButton->bright = !core->pointsAreDiamonds;
 
375
        diamondButton->bright = core->pointsAreDiamonds;
 
376
 
 
377
        setupPalette();
 
378
}
 
379
 
 
380
void Interface::changeState(int transitionSymbol)
 
381
{
 
382
        if (transitionSymbol < 0)
 
383
        {
 
384
                return ;
 
385
        }
 
386
 
 
387
        int retVal = 0;
 
388
        switch(transitionSymbol)
 
389
        {
 
390
                case Flame :
 
391
                        starsButton->bright = false;
 
392
                        flameButton->bright = true;
 
393
                        waveButton->bright = false;
 
394
                        core->fadeMode = Flame;
 
395
                        core->setStarSize(core->starSize);
 
396
                        break;
 
397
                case Wave :
 
398
                        starsButton->bright = false;
 
399
                        flameButton->bright = false;
 
400
                        waveButton->bright = true;
 
401
                        core->fadeMode = Wave;
 
402
                        core->setStarSize(core->starSize);
 
403
                        break;
 
404
                case Stars :
 
405
                        starsButton->bright = true;
 
406
                        flameButton->bright = false;
 
407
                        waveButton->bright = false;
 
408
                        core->fadeMode = Stars;
 
409
                        core->setStarSize(core->starSize);
 
410
                        break;
 
411
 
 
412
                case Star :
 
413
                        core->pointsAreDiamonds = false;
 
414
                        starButton->bright = true;
 
415
                        diamondButton->bright = false;
 
416
                        break;
 
417
                case Diamond :
 
418
                        core->pointsAreDiamonds = true;
 
419
                        starButton->bright = false;
 
420
                        diamondButton->bright = true;
 
421
                        break;
 
422
 
 
423
                case Exit  : 
 
424
                        retVal = 1; break;
 
425
        }
 
426
//      return retVal;
 
427
}
 
428
 
 
429
bool Interface::go()
 
430
{
 
431
        int newVisibleMask = ALL;
 
432
        char keyHit;
 
433
        int action = NotASymbol;
 
434
        int oldButtons = mouseButtons;
 
435
 
 
436
        core->screen->sizeUpdate();
 
437
        if (!core->screen->inputUpdate(mouseX,mouseY,mouseButtons,keyHit))
 
438
                return false;
 
439
 
 
440
        bool mouseClick = (mouseButtons && !oldButtons);
 
441
 
 
442
        if ((mouseX != lastX || mouseY != lastY) && 
 
443
                        lastX > 0 && lastY > 0 && 
 
444
                        lastX < core->outWidth && lastY < core->outHeight)
 
445
                countDown = 40;
 
446
 
 
447
        int activeMask = ALL;
 
448
 
 
449
        if (countDown)
 
450
        {
 
451
                countDown--;
 
452
 
 
453
                double scale = 
 
454
                        (core->outWidth*0.625 < core->outHeight ? core->outWidth*0.625 : core->outHeight);
 
455
                double scaledX = mouseX / scale;
 
456
                double scaledY = mouseY / scale;
 
457
 
 
458
                char hotKey = 0;
 
459
 
 
460
                core->polygonEngine.clear();
 
461
 
 
462
//              stateButton->icon = core->state;
 
463
 
 
464
 
 
465
                for (UIObject *i=uiObjects.first(); i!=0; i = uiObjects.next())
 
466
                {
 
467
                        if ((i->visibleMask & visibleMask) && (i->activeMask & activeMask))
 
468
                                newVisibleMask |= i->go(mouseButtons,mouseClick,
 
469
                                                (scaledX >= i->x &&
 
470
                                                 scaledY >= i->y &&
 
471
                                                 scaledX < i->x+i->width &&
 
472
                                                 scaledY < i->y+i->height),
 
473
                                                scaledX - i->x,
 
474
                                                scaledY - i->y,
 
475
                                                scale,
 
476
                                                hotKey,
 
477
                                                action);
 
478
                }
 
479
 
 
480
                visibleMask = newVisibleMask;  
 
481
                if (visibleMask != 1)
 
482
                        countDown = 20;
 
483
 
 
484
                core->polygonEngine.icon(Icons[Pointer],0x0303,mouseX,mouseY,50,50);
 
485
 
 
486
                core->polygonEngine.apply(core->outputBmp.data);
 
487
 
 
488
                char hint[2] = " ";
 
489
                hint[0] = hotKey;
 
490
                putString(hint,mouseX+6,mouseY+7,0,0);
 
491
        }
 
492
 
 
493
        if (keyHit)
 
494
                for(UIObject *i=uiObjects.first(); i!=0; i = uiObjects.next()) 
 
495
                        if (i->activeMask & activeMask)
 
496
                                i->handleKey(keyHit,action);
 
497
 
 
498
 
 
499
        lastX = mouseX;
 
500
        lastY = mouseY;
 
501
 
 
502
        changeState(action);
 
503
 
 
504
        return true;
 
505
}
 
506
 
 
507
#undef output