5 #include <Application.h>
10 #include <MessageQueue.h>
16 #include "ObjectView.h"
19 #undef B_TRANSLATION_CONTEXT
20 #define B_TRANSLATION_CONTEXT "Playground"
23 const unsigned char kMoveCursor
[] = { 16, 1, 8, 8,
24 0x01, 0x80, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10,
25 0x1e, 0x78, 0x2a, 0x54, 0x4e, 0x72, 0x80, 0x01,
26 0x80, 0x01, 0x4e, 0x72, 0x2a, 0x54, 0x1e, 0x78,
27 0x08, 0x10, 0x04, 0x20, 0x02, 0x40, 0x01, 0x80,
29 0x01, 0x80, 0x03, 0xc0, 0x07, 0xe0, 0x0f, 0xf0,
30 0x1f, 0xf8, 0x3b, 0xdc, 0x7f, 0xfe, 0xff, 0xff,
31 0xff, 0xff, 0x7f, 0xfe, 0x3b, 0xdc, 0x1f, 0xf8,
32 0x0f, 0xf0, 0x07, 0xe0, 0x03, 0xc0, 0x01, 0x80 };
34 const unsigned char kGrabCursor
[] = { 16, 1, 8, 9,
35 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
36 0x0d, 0xb0, 0x12, 0x4c, 0x10, 0x0a, 0x08, 0x02,
37 0x18, 0x02, 0x20, 0x02, 0x20, 0x02, 0x20, 0x04,
38 0x10, 0x04, 0x08, 0x08, 0x04, 0x08, 0x04, 0x08,
40 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
41 0x0d, 0xb0, 0x1f, 0xfc, 0x1f, 0xfe, 0x0f, 0xfe,
42 0x1f, 0xfe, 0x3f, 0xfe, 0x3f, 0xfe, 0x3f, 0xfc,
43 0x1f, 0xfc, 0x0f, 0xf8, 0x07, 0xf8, 0x07, 0xf8 };
47 ObjectView::ObjectView(BRect frame
, const char* name
,
48 uint32 resizeFlags
, uint32 flags
)
49 : BView(frame
, name
, resizeFlags
, flags
),
51 fObjectType(OBJECT_LINE
),
53 fColor((rgb_color
){ 0, 80, 255, 100 }),
54 fDrawingMode(B_OP_ALPHA
),
58 fInitiatingDrag(false),
59 fLastMousePos(0.0, 0.0)
62 SetLowColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR
), B_LIGHTEN_1_TINT
));
66 // font.SetFamilyAndStyle("Bitstream Vera Serif", "Roman");
67 // font.SetSize(20.0);
68 //// font.SetRotation(6.0);
69 // SetFont(&font, B_FONT_FAMILY_AND_STYLE | B_FONT_ROTATION | B_FONT_SIZE);
71 // State* state = State::StateFor(OBJECT_ROUND_RECT, fColor, B_OP_COPY,
73 // state->MouseDown(BPoint(15, 15));
74 // state->MouseMoved(BPoint(255, 305));
79 SetBlendingMode(B_CONSTANT_ALPHA
, B_ALPHA_OVERLAY
);
83 ObjectView::~ObjectView()
89 ObjectView::AttachedToWindow()
91 SetViewColor(B_TRANSPARENT_COLOR
);
96 ObjectView::DetachedFromWindow()
102 ObjectView::Draw(BRect updateRect
)
104 FillRect(updateRect
, B_SOLID_LOW
);
106 // SetHighColor(0, 0, 0);
107 // for (float i = 10; i < 200; i += 4.25) {
108 // StrokeLine(BPoint(i, 10), BPoint(i, 200));
109 // StrokeLine(BPoint(10, i), BPoint(200, i));
112 // SetHighColor(0, 0, 50);
113 // for (float i = 10; i < 200; i += 8.25) {
114 // FillRect(BRect(i, 10 + 230, i + 2, 200 + 230));
115 // FillRect(BRect(10, i + 230, 200, i + 2 + 230));
118 // rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR);
119 // rgb_color shadow = tint_color(noTint, B_DARKEN_2_TINT);
120 // rgb_color light = tint_color(noTint, B_LIGHTEN_MAX_TINT);
124 /* BeginLineArray(4);
125 AddLine(BPoint(r.left, r.top),
126 BPoint(r.right, r.top), shadow);
127 AddLine(BPoint(r.right, r.top + 1),
128 BPoint(r.right, r.bottom), light);
129 AddLine(BPoint(r.right - 1, r.bottom),
130 BPoint(r.left, r.bottom), light);
131 AddLine(BPoint(r.left, r.bottom - 1),
132 BPoint(r.left, r.top + 1), shadow);
136 // SetHighColor(200, 200, 200, 255);
137 //// BRect rect(0.0, 0.0, 10.0, 11.0);
138 // int32 counter = 0;
140 //// while (rect.top < r.bottom) {
141 //// while (rect.left < r.right) {
142 //// region.Include(rect);
143 //// rect.OffsetBy(rect.Width() + 2, 0.0);
147 //// rect.OffsetTo(0.0, rect.top + rect.Height() + 2);
149 // BRect rect(0.0, 0.0, 0.0, 10.0);
150 // for (int32 i = 0; i < 100; i++) {
151 // region.Include(rect);
152 // rect.OffsetBy(rect.Width() + 1, 1.0);
155 //bigtime_t now = system_time();
156 // FillRegion(®ion);
158 //printf("rendering %ld small rects (region: %ld): %lld\n", counter,
159 //region.CountRects(), system_time() - now);
161 SetDrawingMode(B_OP_OVER
);
162 SetHighColor(255, 0, 0, 128);
164 const char* message
= B_TRANSLATE("Click and drag to draw an object");
165 float width
= StringWidth(message
);
167 BPoint
p((r
.Width() - width
) / 2.0, r
.Height() / 2.0);
172 //bigtime_t now = system_time();
177 // font.SetFalseBoldWidth(1.0);
178 // SetFont(&font, B_FONT_FALSE_BOLD_WIDTH);
179 // SetHighColor(0, 0, 0);
180 // DrawString(message, p);
182 // font.SetFalseBoldWidth(0.0);
183 // SetFont(&font, B_FONT_FALSE_BOLD_WIDTH);
184 // SetHighColor(255, 0, 0);
185 //#endif // __HAIKU__
187 DrawString(message
, p
);
190 //printf("Drawing Text: %lld\n", system_time() - now);
192 // r.OffsetTo(B_ORIGIN);
194 //now = system_time();
195 // int32 rectCount = 20;
196 // BeginLineArray(4 * rectCount);
197 // for (int32 i = 0; i < rectCount; i++) {
200 // AddLine(BPoint(r.left, r.top),
201 // BPoint(r.right, r.top), shadow);
202 // AddLine(BPoint(r.right, r.top + 1),
203 // BPoint(r.right, r.bottom), light);
204 // AddLine(BPoint(r.right - 1, r.bottom),
205 // BPoint(r.left, r.bottom), light);
206 // AddLine(BPoint(r.left, r.bottom - 1),
207 // BPoint(r.left, r.top + 1), shadow);
211 //printf("Drawing Lines: %lld\n", system_time() - now);
215 //bigtime_t start = system_time();
216 //SetDrawingMode(B_OP_ALPHA);
217 //SetBlendingMode(B_CONSTANT_ALPHA, B_ALPHA_OVERLAY);
218 //SetHighColor(0, 0, 255, 128);
219 //FillRect(BRect(0, 0, 250, 300));
221 //printf("Alpha Fill: %lld\n", system_time() - start);
223 for (int32 i
= 0; State
* state
= (State
*)fStateList
.ItemAt(i
); i
++)
226 //printf("State: %lld\n", system_time() - start);
231 ObjectView::MouseDown(BPoint where
)
237 // BMessageQueue* queue = Window()->MessageQueue();
240 // for (int32 i = 0; (msg = queue->FindMessage(i)); i++) {
241 // if (msg->what == B_MOUSE_MOVED)
244 // printf("B_MOUSE_MOVED count before GetMouse(): %ld\n", count);
246 // GetMouse(&where, &buttons);
249 // for (int32 i = 0; (msg = queue->FindMessage(i)); i++) {
250 // if (msg->what == B_MOUSE_MOVED)
253 // printf("B_MOUSE_MOVED count after 1st GetMouse(): %ld\n", count);
255 // GetMouse(&where, &buttons);
258 // for (int32 i = 0; (msg = queue->FindMessage(i)); i++) {
259 // if (msg->what == B_MOUSE_MOVED)
262 // printf("B_MOUSE_MOVED count after 2nd GetMouse(): %ld\n", count);
265 //be_app->HideCursor();
267 Window()->CurrentMessage()->FindInt32("buttons", (int32
*)&buttons
);
268 Window()->CurrentMessage()->FindInt32("clicks", &clicks
);
269 //printf("ObjectView::MouseDown() - clicks: %ld\n", clicks);
270 fInitiatingDrag
= buttons
& B_SECONDARY_MOUSE_BUTTON
;
271 fScrolling
= !fInitiatingDrag
&& (buttons
& B_TERTIARY_MOUSE_BUTTON
);
273 SetMouseEventMask(B_POINTER_EVENTS
, B_LOCK_WINDOW_FOCUS
);
275 if (fScrolling
|| fInitiatingDrag
) {
276 fLastMousePos
= where
;
279 AddObject(State::StateFor(fObjectType
, fColor
, fDrawingMode
,
283 fState
->MouseDown(where
);
290 ObjectView::MouseUp(BPoint where
)
292 //be_app->ShowCursor();
305 ObjectView::MouseMoved(BPoint where
, uint32 transit
,
306 const BMessage
* dragMessage
)
308 // BRect dirty(where, where);
309 // dirty.InsetBy(-10, -10);
310 // Invalidate(dirty);
313 //printf("ObjectView::MouseMoved(BPoint(%.1f, %.1f)) - DRAG MESSAGE\n", where.x, where.y);
314 //Window()->CurrentMessage()->PrintToStream();
316 //printf("ObjectView::MouseMoved(BPoint(%.1f, %.1f))\n", where.x, where.y);
320 BCursor
cursor(kGrabCursor
);
321 SetViewCursor(&cursor
);
323 BPoint offset
= fLastMousePos
- where
;
324 ScrollBy(offset
.x
, offset
.y
);
325 fLastMousePos
= where
+ offset
;
326 } else if (fInitiatingDrag
) {
327 BPoint offset
= fLastMousePos
- where
;
328 if (sqrtf(offset
.x
* offset
.x
+ offset
.y
* offset
.y
) > 5.0) {
329 BMessage
newDragMessage('drag');
330 BBitmap
* dragBitmap
= new BBitmap(BRect(0, 0, 40, 40), B_RGBA32
,
332 if (dragBitmap
->Lock()) {
333 BView
* helper
= new BView(dragBitmap
->Bounds(),
334 "offscreen view", B_FOLLOW_ALL
, B_WILL_DRAW
);
335 dragBitmap
->AddChild(helper
);
336 helper
->SetDrawingMode(B_OP_ALPHA
);
337 helper
->SetBlendingMode(B_CONSTANT_ALPHA
, B_ALPHA_COMPOSITE
);
339 BRect
r(helper
->Bounds());
340 helper
->SetHighColor(0, 0, 0, 128);
341 helper
->StrokeRect(r
);
343 helper
->SetHighColor(200, 200, 200, 100);
347 helper
->SetHighColor(0, 0, 0, 255);
348 const char* text
= B_TRANSLATE("Test");
349 float pos
= (r
.Width() - helper
->StringWidth(text
)) / 2;
350 helper
->DrawString(text
, BPoint(pos
, 25));
354 DragMessage(&newDragMessage
, dragBitmap
, B_OP_ALPHA
, B_ORIGIN
,
356 fInitiatingDrag
= false;
359 BCursor
cursor(kMoveCursor
);
360 SetViewCursor(&cursor
);
362 if (fState
&& fState
->IsTracking()) {
363 BRect before
= fState
->Bounds();
365 fState
->MouseMoved(where
);
367 BRect after
= fState
->Bounds();
368 BRect
invalid(before
| after
);
377 ObjectView::MessageReceived(BMessage
* message
)
379 switch (message
->what
) {
381 printf("ObjectView::MessageReceived() - received drag message\n");
384 BView::MessageReceived(message
);
391 ObjectView::SetState(State
* state
)
393 if (fState
!= state
) {
395 fState
->SetEditing(false);
396 Invalidate(fState
->Bounds());
402 fState
->SetEditing(true);
403 Invalidate(fState
->Bounds());
410 ObjectView::SetObjectType(int32 type
)
412 if (type
!= fObjectType
) {
420 ObjectView::AddObject(State
* state
)
423 fStateList
.AddItem((void*)state
);
425 BMessage
message(MSG_OBJECT_ADDED
);
426 message
.AddPointer("object", state
);
427 Window()->PostMessage(&message
);
435 ObjectView::RemoveObject(State
* state
)
437 if (state
&& fStateList
.RemoveItem((void*)state
)) {
441 Invalidate(state
->Bounds());
443 Window()->PostMessage(MSG_OBJECT_COUNT_CHANGED
);
451 ObjectView::CountObjects() const
453 return fStateList
.CountItems();
458 ObjectView::MakeEmpty()
460 for (int32 i
= 0; State
* state
= (State
*)fStateList
.ItemAt(i
); i
++)
462 fStateList
.MakeEmpty();
464 Window()->PostMessage(MSG_OBJECT_COUNT_CHANGED
);
473 ObjectView::SetStateColor(rgb_color color
)
475 if (color
.red
!= fColor
.red
||
476 color
.green
!= fColor
.green
||
477 color
.blue
!= fColor
.blue
||
478 color
.alpha
!= fColor
.alpha
) {
483 fState
->SetColor(fColor
);
484 Invalidate(fState
->Bounds());
489 // SetStateDrawingMode
491 ObjectView::SetStateDrawingMode(drawing_mode mode
)
493 if (fDrawingMode
!= mode
) {
497 fState
->SetDrawingMode(fDrawingMode
);
498 Invalidate(fState
->Bounds());
505 ObjectView::SetStateFill(bool fill
)
511 BRect before
= fState
->Bounds();
513 fState
->SetFill(fFill
);
515 BRect after
= fState
->Bounds();
516 BRect
invalid(before
| after
);
524 ObjectView::SetStatePenSize(float penSize
)
526 if (fPenSize
!= penSize
) {
530 BRect before
= fState
->Bounds();
532 fState
->SetPenSize(fPenSize
);
534 BRect after
= fState
->Bounds();
535 BRect
invalid(before
| after
);