11 fTracking(TRACKING_NONE
),
12 fStartPoint(-1.0, -1.0),
13 fEndPoint(-1.0, -1.0),
14 fColor((rgb_color
){ 0, 0, 0, 255 }),
15 fDrawingMode(B_OP_COPY
),
28 State::Init(rgb_color color
, drawing_mode mode
, bool fill
, float penSize
)
38 State::MouseDown(BPoint where
)
40 where
.x
= floorf(where
.x
+ 0.5);
41 where
.y
= floorf(where
.y
+ 0.5);
43 if (_HitTest(where
, fStartPoint
)) {
44 fTracking
= TRACKING_START
;
45 fClickOffset
= fStartPoint
- where
;
46 } else if (_HitTest(where
, fEndPoint
)) {
47 fTracking
= TRACKING_END
;
48 fClickOffset
= fEndPoint
- where
;
50 fTracking
= TRACKING_END
;
51 fStartPoint
= fEndPoint
= where
;
52 fClickOffset
.Set(0.0, 0.0);
60 fTracking
= TRACKING_NONE
;
65 State::MouseMoved(BPoint where
)
67 where
.x
= floorf(where
.x
+ 0.5);
68 where
.y
= floorf(where
.y
+ 0.5);
70 if (fTracking
== TRACKING_START
) {
71 fStartPoint
= where
+ fClickOffset
;
73 } else if (fTracking
== TRACKING_END
) {
74 fEndPoint
= where
+ fClickOffset
;
81 State::SetColor(rgb_color color
)
88 State::SetDrawingMode(drawing_mode mode
)
95 State::SetFill(bool fill
)
102 State::SetPenSize(float penSize
)
109 State::SetEditing(bool editing
)
116 State::Bounds() const
119 BRect r
= _ValidRect();
120 float inset
= -2.0; // for the dots
121 if (!SupportsFill() || !fFill
) {
122 inset
= min_c(inset
, -ceilf(fPenSize
/ 2.0));
124 r
.InsetBy(inset
, inset
);
127 return BRect(0.0, 0.0, -1.0, -1.0);
132 State::Draw(BView
* view
) const
134 if (fValid
&& fEditing
) {
135 _RenderDot(view
, fStartPoint
);
136 _RenderDot(view
, fEndPoint
);
142 State::_ValidRect() const
144 return BRect(min_c(fStartPoint
.x
, fEndPoint
.x
),
145 min_c(fStartPoint
.y
, fEndPoint
.y
),
146 max_c(fStartPoint
.x
, fEndPoint
.x
),
147 max_c(fStartPoint
.y
, fEndPoint
.y
));
152 State::_RenderDot(BView
* view
, BPoint where
) const
154 view
->SetHighColor(0, 0, 0, 255);
155 view
->SetPenSize(1.0);
156 view
->SetDrawingMode(B_OP_COPY
);
157 BRect
r(where
, where
);
158 r
.InsetBy(-2.0, -2.0);
160 view
->SetHighColor(255, 255, 255, 255);
167 State::_AdjustViewState(BView
* view
) const
169 view
->SetDrawingMode(fDrawingMode
);
170 view
->SetHighColor(fColor
);
172 if (!SupportsFill() || !fFill
)
173 view
->SetPenSize(fPenSize
);
178 State::_HitTest(BPoint where
, BPoint point
) const
180 BRect
r(point
, point
);
181 r
.InsetBy(-8.0, -8.0);
182 return r
.Contains(where
);
186 class LineState
: public State
{
191 virtual void Draw(BView
* view
) const
194 _AdjustViewState(view
);
195 view
->StrokeLine(fStartPoint
, fEndPoint
);
199 virtual bool SupportsFill() const
206 class RectState
: public State
{
211 virtual void Draw(BView
* view
) const
214 _AdjustViewState(view
);
216 view
->FillRect(_ValidRect());
218 view
->StrokeRect(_ValidRect());
225 class RoundRectState
: public State
{
230 virtual void Draw(BView
* view
) const
233 _AdjustViewState(view
);
234 BRect r
= _ValidRect();
235 float radius
= min_c(r
.Width() / 3.0, r
.Height() / 3.0);
237 view
->FillRoundRect(r
, radius
, radius
);
239 view
->StrokeRoundRect(r
, radius
, radius
);
246 class EllipseState
: public State
{
251 virtual void Draw(BView
* view
) const
254 _AdjustViewState(view
);
256 view
->FillEllipse(_ValidRect());
258 view
->StrokeEllipse(_ValidRect());
266 State::StateFor(int32 objectType
, rgb_color color
, drawing_mode mode
,
267 bool fill
, float penSize
)
270 switch (objectType
) {
272 state
= new LineState();
275 state
= new RectState();
277 case OBJECT_ROUND_RECT
:
278 state
= new RoundRectState();
281 state
= new EllipseState();
287 state
->Init(color
, mode
, fill
, penSize
);