* Added a static Draw() method.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@39146 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2010-10-25 20:17:06 +00:00
parent 04e916ccb5
commit 44fe7b9f4f
2 changed files with 69 additions and 32 deletions
+55 -26
View File
@@ -25,7 +25,7 @@ RadioView::RadioView(BRect frame, const char* name, int32 resizingMode)
fPercent(0), fPercent(0),
fPulse(NULL), fPulse(NULL),
fPhase(0), fPhase(0),
fMax(7) fMax(DefaultMax())
{ {
} }
@@ -87,6 +87,31 @@ RadioView::StopPulsing()
} }
/*static*/ void
RadioView::Draw(BView* view, BRect rect, int32 percent, int32 maxCount)
{
view->PushState();
BPoint center;
int32 count;
float step;
_Compute(rect, center, count, maxCount, step);
for (int32 i = 0; i < count; i++) {
_SetColor(view, percent, 0, i, count);
_DrawBow(view, i, center, count, step);
}
view->PopState();
}
/*static*/ int32
RadioView::DefaultMax()
{
return 7;
}
void void
RadioView::AttachedToWindow() RadioView::AttachedToWindow()
{ {
@@ -128,14 +153,14 @@ RadioView::Draw(BRect updateRect)
BPoint center; BPoint center;
int32 count; int32 count;
float step; float step;
_Compute(Bounds(), center, count, step); _Compute(Bounds(), center, count, fMax, step);
for (int32 i = 0; i < count; i++) { for (int32 i = 0; i < count; i++) {
_SetColor(i, count); _SetColor(this, fPercent, fPhase, i, count);
if (step == kMinStep && _IsDisabled(i, count)) if (step == kMinStep && _IsDisabled(fPercent, i, count))
continue; continue;
_DrawBow(i, center, count, step); _DrawBow(this, i, center, count, step);
} }
} }
@@ -157,7 +182,7 @@ RadioView::_RestartPulsing()
BPoint center; BPoint center;
int32 count; int32 count;
float step; float step;
_Compute(Bounds(), center, count, step); _Compute(Bounds(), center, count, fMax, step);
BMessage message(kMsgPulse); BMessage message(kMsgPulse);
fPulse = new BMessageRunner(this, &message, (bigtime_t)(kMinPulseInterval fPulse = new BMessageRunner(this, &message, (bigtime_t)(kMinPulseInterval
@@ -165,54 +190,58 @@ RadioView::_RestartPulsing()
} }
void /*static*/ void
RadioView::_Compute(const BRect& bounds, BPoint& center, int32& count, RadioView::_Compute(const BRect& bounds, BPoint& center, int32& count,
float& step) const int32 max, float& step)
{ {
center.Set(roundf(bounds.Width() / 2), bounds.bottom); center.Set(roundf(bounds.Width() / 2), bounds.bottom);
float size = min_c(center.x * 3 / 2, center.y); float size = min_c(center.x * 3 / 2, center.y);
step = floorf(size / fMax); step = floorf(size / max);
if (step < kMinStep) { if (step < kMinStep) {
count = (int32)(size / kMinStep); count = (int32)(size / kMinStep);
step = kMinStep; step = kMinStep;
} else } else
count = fMax; count = max;
center.x += bounds.left;
} }
void /*static*/ void
RadioView::_DrawBow(int32 index, const BPoint& center, int32 count, float step) RadioView::_DrawBow(BView* view, int32 index, const BPoint& center,
int32 count, float step)
{ {
float radius = step * index + 1; float radius = step * index + 1;
if (step < 4) if (step < 4)
SetPenSize(step / 2); view->SetPenSize(step / 2);
else else
SetPenSize(step * 2 / 3); view->SetPenSize(step * 2 / 3);
SetLineMode(B_ROUND_CAP, B_ROUND_JOIN); view->SetLineMode(B_ROUND_CAP, B_ROUND_JOIN);
StrokeArc(center, radius, radius, 50, 80); view->StrokeArc(center, radius, radius, 50, 80);
} }
void /*static*/ void
RadioView::_SetColor(int32 index, int32 count) RadioView::_SetColor(BView* view, int32 percent, int32 phase, int32 index,
int32 count)
{ {
if (_IsDisabled(index, count)) { if (_IsDisabled(percent, index, count)) {
// disabled // disabled
SetHighColor(tint_color(ViewColor(), B_DARKEN_1_TINT)); view->SetHighColor(tint_color(view->LowColor(), B_DARKEN_1_TINT));
} else if (fPhase == 0 || fPhase % count != index) { } else if (phase == 0 || phase % count != index) {
// enabled // enabled
SetHighColor(tint_color(ViewColor(), B_DARKEN_3_TINT)); view->SetHighColor(tint_color(view->LowColor(), B_DARKEN_3_TINT));
} else { } else {
// pulsing // pulsing
SetHighColor(tint_color(ViewColor(), B_DARKEN_2_TINT)); view->SetHighColor(tint_color(view->LowColor(), B_DARKEN_2_TINT));
} }
} }
bool /*static*/ bool
RadioView::_IsDisabled(int32 index, int32 count) const RadioView::_IsDisabled(int32 percent, int32 index, int32 count)
{ {
return fPercent < 100 * index / count; return percent < 100 * index / count;
} }
+14 -6
View File
@@ -26,6 +26,10 @@ public:
bool IsPulsing() const bool IsPulsing() const
{ return fPulse != NULL; } { return fPulse != NULL; }
static void Draw(BView* view, BRect rect, int32 percent,
int32 count);
static int32 DefaultMax();
protected: protected:
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void DetachedFromWindow(); virtual void DetachedFromWindow();
@@ -36,12 +40,16 @@ protected:
private: private:
void _RestartPulsing(); void _RestartPulsing();
void _Compute(const BRect& bounds, BPoint& center,
int32& count, float& step) const; static void _Compute(const BRect& bounds, BPoint& center,
void _DrawBow(int32 index, const BPoint& center, int32& count, int32 max, float& step);
int32 count, float step); static void _DrawBow(BView* view, int32 index,
void _SetColor(int32 index, int32 count); const BPoint& center, int32 count,
bool _IsDisabled(int32 index, int32 count) const; float step);
static void _SetColor(BView* view, int32 percent,
int32 phase, int32 index, int32 count);
static bool _IsDisabled(int32 percent, int32 index,
int32 count);
private: private:
int32 fPercent; int32 fPercent;