* Copyright 2003-2009, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Authors:
* Jonas SundstrΓΆm, jonas@kirilla.com
*/
#include "ZipOMaticActivity.h"
#include <ControlLook.h>
#include <stdio.h>
Activity::Activity(const char* name)
:
BView(name, B_WILL_DRAW | B_FRAME_EVENTS),
fIsRunning(false),
fBitmap(NULL),
fSpinSpeed(0.15),
fColors(NULL),
fNumColors(0),
fScrollOffset(0.0),
fStripeWidth(0.0),
fNumStripes(0)
{
_InactiveColors();
SetExplicitMinSize(BSize(17, 17));
SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 17));
};
Activity::~Activity()
{
delete fBitmap;
delete[] fColors;
}
void
Activity::AllAttached()
{
_CreateBitmap();
FrameResized(Bounds().Width(), Bounds().Height());
}
void
Activity::Start()
{
fIsRunning = true;
_ActiveColors();
Window()->SetPulseRate(100000);
SetFlags(Flags() | B_PULSE_NEEDED);
Invalidate();
}
void
Activity::Pause()
{
Window()->SetPulseRate(500000);
SetFlags(Flags() & (~B_PULSE_NEEDED));
Invalidate();
}
void
Activity::Stop()
{
fIsRunning = false;
_InactiveColors();
Window()->SetPulseRate(500000);
SetFlags(Flags() & (~B_PULSE_NEEDED));
Invalidate();
}
bool
Activity::IsRunning()
{
return fIsRunning;
}
void
Activity::Pulse()
{
fScrollOffset += fStripeWidth / (1.0f / fSpinSpeed);
if (fScrollOffset >= fStripeWidth * fNumColors) {
fScrollOffset = 0;
}
Invalidate();
}
void
Activity::SetColors(const rgb_color* colors, uint32 numColors)
{
delete[] fColors;
rgb_color* colorsCopy = new rgb_color[numColors];
for (uint32 i = 0; i < numColors; i++)
colorsCopy[i] = colors[i];
fColors = colorsCopy;
fNumColors = numColors;
}
void
Activity::Draw(BRect rect)
{
BRect viewRect = Bounds();
BRect bitmapRect = fBitmap->Bounds();
if (bitmapRect != viewRect) {
delete fBitmap;
_CreateBitmap();
}
_DrawOnBitmap(IsRunning());
SetDrawingMode(B_OP_COPY);
DrawBitmap(fBitmap);
}
void
Activity::_DrawOnBitmap(bool running)
{
if (fBitmap->Lock()) {
BRect bounds = fBitmap->Bounds();
fBitmapView->SetDrawingMode(B_OP_COPY);
float position = -fStripeWidth * (fNumColors + 0.5) + fScrollOffset;
BRect innerFrame = bounds;
innerFrame.InsetBy(-2, -2);
be_control_look->DrawStatusBar(fBitmapView, innerFrame, innerFrame,
ui_color(B_PANEL_BACKGROUND_COLOR),
running ? ui_color(B_STATUS_BAR_COLOR)
: ui_color(B_PANEL_BACKGROUND_COLOR),
bounds.Width());
fBitmapView->SetDrawingMode(B_OP_ALPHA);
uint32 colorIndex = 0;
for (uint32 i = 0; i < fNumStripes; i++) {
fBitmapView->SetHighColor(fColors[colorIndex]);
colorIndex++;
if (colorIndex >= fNumColors)
colorIndex = 0;
BRect stripeFrame = fStripe.Frame();
fStripe.MapTo(stripeFrame,
stripeFrame.OffsetToCopy(position, 0.0));
fBitmapView->FillPolygon(&fStripe);
position += fStripeWidth;
}
fBitmapView->SetDrawingMode(B_OP_COPY);
be_control_look->DrawTextControlBorder(fBitmapView, bounds, bounds,
ui_color(B_PANEL_BACKGROUND_COLOR), B_PLAIN_BORDER);
fBitmapView->Sync();
fBitmap->Unlock();
}
}
void
Activity::_CreateBitmap(void)
{
BRect rect = Bounds();
fBitmap = new BBitmap(rect, B_RGBA32, true);
fBitmapView = new BView(Bounds(), "buffer", B_FOLLOW_NONE, 0);
fBitmap->AddChild(fBitmapView);
}
void
Activity::FrameResized(float width, float height)
{
delete fBitmap;
_CreateBitmap();
fStripeWidth = (width / (fIsRunning ? 4 : 6)) + 5;
if (fStripeWidth > 200)
fStripeWidth = 200;
BPoint stripePoints[4];
stripePoints[0].Set(fStripeWidth * 0.5, 0.0);
stripePoints[1].Set(fStripeWidth * 1.5, 0.0);
stripePoints[2].Set(fStripeWidth, height);
stripePoints[3].Set(0.0, height);
fStripe = BPolygon(stripePoints, 4);
fNumStripes = (int32)ceilf((width) / fStripeWidth) + 1 + fNumColors;
Invalidate();
}
void
Activity::_ActiveColors()
{
rgb_color defaultColors[2];
rgb_color otherColor = tint_color(ui_color(B_STATUS_BAR_COLOR), 1.3);
otherColor.alpha = 50;
defaultColors[0] = otherColor;
defaultColors[1] = B_TRANSPARENT_COLOR;
SetColors(defaultColors, 2);
}
void
Activity::_InactiveColors()
{
rgb_color defaultColors[2];
rgb_color otherColor = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), 1.7);
otherColor.alpha = 50;
defaultColors[0] = otherColor;
defaultColors[1] = B_TRANSPARENT_COLOR;
SetColors(defaultColors, 2);
}