tcp: Add APICall trace entry and move TRACEs into locked parts.
[haiku.git] / src / add-ons / screen_savers / icons / IconDisplay.cpp
blobcc32a85aa718aba40db47d92dfe0cb6e7eaa8f58
1 /*
2 * Copyright 2009 Vincent Duvert, vincent.duvert@free.fr
3 * Copyright 2014 Haiku, Inc. All rights reserved.
5 * Distributed under the terms of the MIT License.
7 * Authors:
8 * Vincent Duvert, vincent.duvert@free.fr
9 * John Scipione, jscipione@gmail.com
13 #include "IconDisplay.h"
15 #include <stdlib.h>
17 #include <Bitmap.h>
18 #include <IconUtils.h>
19 #include <View.h>
21 #include "VectorIcon.h"
24 #define RAND_BETWEEN(a, b) ((rand() % ((b) - (a) + 1) + (a)))
25 #define SHOW_TICKS_MIN 20
26 #define SHOW_TICKS_MAX 50
27 #define STAY_TICKS_MIN 50
28 #define STAY_TICKS_MAX 100
29 #define HIDE_TICKS_MIN 20
30 #define HIDE_TICKS_MAX 50
33 IconDisplay::IconDisplay()
35 fIsRunning(false),
36 fBitmap(NULL)
41 IconDisplay::~IconDisplay()
43 delete fBitmap;
47 void
48 IconDisplay::Run(vector_icon* icon, BRect frame)
50 delete fBitmap;
52 fBitmap = new BBitmap(BRect(0, 0, frame.Width(), frame.Height()), 0,
53 B_RGBA32);
54 if (BIconUtils::GetVectorIcon(icon->data, icon->size, fBitmap) != B_OK)
55 return;
57 fState = 0;
58 fTicks = 0;
59 fDelay = RAND_BETWEEN(SHOW_TICKS_MIN, SHOW_TICKS_MAX);
60 fFrame = frame;
62 fIsRunning = true;
66 void
67 IconDisplay::ClearOn(BView* view)
69 if (!fIsRunning || fState == 2)
70 return;
72 view->FillRect(fFrame);
76 void
77 IconDisplay::DrawOn(BView* view, uint32 delta)
79 if (!fIsRunning)
80 return;
82 fTicks += delta;
84 rgb_color backColor = view->HighColor();
86 switch (fState) {
87 case 0:
88 // progressive showing
89 if (fTicks < fDelay)
90 backColor.alpha = (fTicks * 255) / fDelay;
91 else
92 fState++;
94 break;
96 case 1:
97 // completed showing
98 backColor.alpha = 255;
99 fTicks = 0;
100 fDelay = RAND_BETWEEN(STAY_TICKS_MIN, STAY_TICKS_MAX);
101 fState++;
102 break;
104 case 2:
105 // waiting
106 if (fTicks < fDelay)
107 return;
109 fTicks = 0;
110 backColor.alpha = 255;
111 fDelay = RAND_BETWEEN(HIDE_TICKS_MIN, HIDE_TICKS_MAX);
112 fState++;
113 return;
114 break;
116 case 3:
117 // progressive hiding
118 if (fTicks < fDelay) {
119 backColor.alpha = 255 - (fTicks * 255) / fDelay;
120 } else {
121 backColor.alpha = 0;
122 fState++;
124 break;
126 default:
127 // finished
128 fIsRunning = false;
129 return;
130 break;
133 view->SetHighColor(backColor);
134 view->DrawBitmap(fBitmap, BPoint(fFrame.left, fFrame.top));
135 backColor.alpha = 255;
136 view->SetHighColor(backColor);