2 * Copyright 2010, Stephan Aßmus <superstippi@gmx.de>.
3 * Distributed under the terms of the MIT License.
7 #include "PlayPauseButton.h"
9 #include <GradientLinear.h>
10 #include <LayoutUtils.h>
14 static const rgb_color kGreen
= (rgb_color
){ 116, 224, 0, 255 };
18 PlayPauseButton::PlayPauseButton(const char* name
,
19 BShape
* playSymbolShape
, BShape
* pauseSymbolShape
, BMessage
* message
,
22 SymbolButton(name
, NULL
, message
, borders
),
23 fPlaySymbol(playSymbolShape
),
24 fPauseSymbol(pauseSymbolShape
),
25 fPlaybackState(kStopped
)
30 PlayPauseButton::~PlayPauseButton()
38 PlayPauseButton::Draw(BRect updateRect
)
40 SymbolButton::Draw(updateRect
);
42 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
43 rgb_color active
= (rgb_color
){ 116, 224, 0, 255 };
46 if (Value() == B_CONTROL_ON
)
47 base
= tint_color(base
, (B_DARKEN_4_TINT
+ B_DARKEN_MAX_TINT
) / 2);
49 base
= tint_color(base
, B_DARKEN_4_TINT
);
51 if (Value() == B_CONTROL_ON
)
52 base
= tint_color(base
, B_DARKEN_2_TINT
);
54 base
= tint_color(base
, B_DARKEN_1_TINT
);
56 BRect
bounds(Bounds());
57 BRect pauseBounds
= fPauseSymbol
->Bounds();
58 BRect playBounds
= fPlaySymbol
->Bounds();
61 float spacing
= pauseBounds
.Height() / 4;
62 offset
.x
= (bounds
.left
+ bounds
.right
) / 2;
63 offset
.y
= (bounds
.top
+ bounds
.bottom
) / 2;
64 offset
.x
-= (pauseBounds
.Width() + playBounds
.Width() + spacing
) / 2;
65 offset
.y
-= pauseBounds
.Height() / 2;
66 offset
.x
= floorf(offset
.x
- playBounds
.left
+ 0.5);
67 offset
.y
= ceilf(offset
.y
- pauseBounds
.top
);
68 if (Value() == B_CONTROL_ON
) {
73 bool playActive
= IsEnabled()
74 && ((fPlaybackState
== kPlaying
&& Value() == B_CONTROL_OFF
)
75 || (fPlaybackState
== kPaused
&& Value() == B_CONTROL_ON
));
76 bool pauseActive
= IsEnabled()
77 && ((fPlaybackState
== kPaused
&& Value() == B_CONTROL_OFF
)
78 || (fPlaybackState
== kPlaying
&& Value() == B_CONTROL_ON
));
81 BGradientLinear gradient
;
83 gradient
.AddColor(active
, 0);
84 gradient
.AddColor(tint_color(active
, B_LIGHTEN_1_TINT
), 255);
86 gradient
.AddColor(tint_color(base
, B_DARKEN_1_TINT
), 0);
87 gradient
.AddColor(base
, 255);
89 gradient
.SetStart(offset
);
90 offset
.y
+= playBounds
.Height();
91 gradient
.SetEnd(offset
);
92 FillShape(fPlaySymbol
, gradient
);
94 SetHighColor(tint_color(active
, B_DARKEN_3_TINT
));
96 StrokeShape(fPlaySymbol
);
99 offset
.y
-= playBounds
.Height();
100 offset
.x
+= ceilf(playBounds
.Width() + spacing
);
102 gradient
.MakeEmpty();
104 gradient
.AddColor(active
, 0);
105 gradient
.AddColor(tint_color(active
, B_LIGHTEN_1_TINT
), 255);
107 gradient
.AddColor(tint_color(base
, B_DARKEN_1_TINT
), 0);
108 gradient
.AddColor(base
, 255);
110 gradient
.SetStart(offset
);
111 offset
.y
+= playBounds
.Height();
112 gradient
.SetEnd(offset
);
113 FillShape(fPauseSymbol
, gradient
);
115 SetHighColor(tint_color(active
, B_DARKEN_3_TINT
));
117 StrokeShape(fPauseSymbol
);
123 PlayPauseButton::MinSize()
125 if (fPauseSymbol
== NULL
|| fPlaySymbol
== NULL
)
126 return BButton::MinSize();
130 (fPlaySymbol
->Bounds().Width() + fPauseSymbol
->Bounds().Width())
132 size
.height
= ceilf(fPauseSymbol
->Bounds().Height() * 2.5f
);
133 return BLayoutUtils::ComposeSize(ExplicitMinSize(), size
);
138 PlayPauseButton::MaxSize()
140 BSize
size(MinSize());
141 size
.width
= ceilf(size
.width
* 1.5f
);
142 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), size
);
147 PlayPauseButton::SetPlaying()
149 _SetPlaybackState(kPlaying
);
154 PlayPauseButton::SetPaused()
156 _SetPlaybackState(kPaused
);
161 PlayPauseButton::SetStopped()
163 _SetPlaybackState(kStopped
);
168 PlayPauseButton::SetSymbols(BShape
* playSymbolShape
, BShape
* pauseSymbolShape
)
170 BSize oldSize
= MinSize();
173 fPlaySymbol
= playSymbolShape
;
175 fPauseSymbol
= pauseSymbolShape
;
177 if (MinSize() != oldSize
)
185 PlayPauseButton::_SetPlaybackState(uint32 state
)
187 if (fPlaybackState
== state
)
189 fPlaybackState
= state
;