xref: /haiku/src/add-ons/screen_savers/icons/IconDisplay.cpp (revision 1294543de9ac0eff000eaea1b18368c36435d08e)
1 /*
2 	Copyright 2009 Vincent Duvert, vincent.duvert@free.fr
3 	All rights reserved. Distributed under the terms of the MIT License.
4 */
5 
6 
7 #include "IconDisplay.h"
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include <Bitmap.h>
13 #include <IconUtils.h>
14 
15 
16 #define RAND_BETWEEN(a, b) ((rand() % ((b) - (a) + 1) + (a)))
17 #define SHOW_TICKS_MIN 20
18 #define SHOW_TICKS_MAX 50
19 #define STAY_TICKS_MIN 50
20 #define STAY_TICKS_MAX 100
21 #define HIDE_TICKS_MIN 20
22 #define HIDE_TICKS_MAX 50
23 
24 
25 IconDisplay::IconDisplay()
26 	:
27 	fIsRunning(false),
28 	fBitmap(NULL)
29 {
30 }
31 
32 
33 IconDisplay::~IconDisplay()
34 {
35 	delete fBitmap;
36 }
37 
38 
39 void
40 IconDisplay::Run(VectorIcon* icon, BRect frame)
41 {
42 	delete fBitmap;
43 
44 	fBitmap = new BBitmap(BRect(0, 0, frame.Width(), frame.Height()), 0,
45 		B_RGBA32);
46 	BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap);
47 
48 	fState = 0;
49 	fTicks = 0;
50 	fDelay = RAND_BETWEEN(SHOW_TICKS_MIN, SHOW_TICKS_MAX);
51 	fFrame = frame;
52 
53 	fIsRunning = true;
54 }
55 
56 
57 void
58 IconDisplay::ClearOn(BView* view)
59 {
60 	if (!fIsRunning || fState == 2)
61 		return;
62 
63 	view->FillRect(fFrame);
64 }
65 
66 
67 void
68 IconDisplay::DrawOn(BView* view, uint32 delta)
69 {
70 	if (!fIsRunning)
71 		return;
72 
73 	fTicks += delta;
74 
75 	rgb_color backColor = view->HighColor();
76 
77 	switch (fState) {
78 		case 0:
79 			// Progressive showing
80 			if (fTicks < fDelay)
81 				backColor.alpha = (fTicks * 255) / fDelay;
82 			else
83 				fState++;
84 			break;
85 
86 		case 1:
87 			// Completed showing
88 			backColor.alpha = 255;
89 			fTicks = 0;
90 			fDelay = RAND_BETWEEN(STAY_TICKS_MIN, STAY_TICKS_MAX);
91 			fState++;
92 			break;
93 
94 		case 2:
95 			// Waiting
96 			if (fTicks < fDelay)
97 				return;
98 			fTicks = 0;
99 			backColor.alpha = 255;
100 			fDelay = RAND_BETWEEN(HIDE_TICKS_MIN, HIDE_TICKS_MAX);
101 			fState++;
102 			return;
103 			break;
104 
105 		case 3:
106 			// Progressive hiding
107 			if (fTicks < fDelay) {
108 				backColor.alpha = 255 - (fTicks * 255) / fDelay;
109 			} else {
110 				backColor.alpha = 0;
111 				fState++;
112 			}
113 			break;
114 
115 		default:
116 			// Finished
117 			fIsRunning = false;
118 			return;
119 			break;
120 	};
121 
122 	view->SetHighColor(backColor);
123 	view->DrawBitmap(fBitmap, BPoint(fFrame.left, fFrame.top));
124 	backColor.alpha = 255;
125 	view->SetHighColor(backColor);
126 }
127