3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.tizenopensource.org/license
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
18 #include "DrawingBoard.h"
20 using namespace Osp::App;
21 using namespace Osp::Base;
22 using namespace Osp::Base::Runtime;
23 using namespace Osp::Base::Utility;
24 using namespace Osp::Graphics;
25 using namespace Osp::Media;
26 using namespace Osp::System;
27 using namespace Osp::Ui;
28 using namespace Osp::Ui::Controls;
31 #define RED32(color32) static_cast<byte>(0x00F8 & ((color32) >> 16))
32 #define GREEN32(color32) static_cast<byte>(0x00FC & ((color32) >> 8))
33 #define BLUE32(color32) static_cast<byte>(0x00F8 & (color32))
35 #define RGB888ToCOLOR16(red, green, blue) (static_cast<unsigned short>((static_cast<byte>((blue) >> 3)) | (static_cast<unsigned short>(static_cast<byte>((green) >> 2)) << 5)) | ((static_cast<unsigned short>(static_cast<byte>((red) >> 3)) << 11)))
36 #define COLOR32ToCOLOR16(color32) RGB888ToCOLOR16(RED32(color32), GREEN32(color32), BLUE32(color32))
39 const int REPEAT_COUNT = 100;
40 const int TIME_OUT = 10;
41 const int MAX_LINE_WIDTH = 10;
42 const int MAX_FONT_SIZE = 50;
46 : public Osp::Ui::Controls::Form
49 DrawingForm(DrawingBoard* pApp)
54 virtual ~DrawingForm(void)
59 virtual result OnDraw(void)
77 Painter(const Control& control)
80 _pCanvas = control.GetCanvasN();
81 _pCanvas->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
85 _bounds = _pCanvas->GetBounds();
93 virtual bool Draw(void) = 0;
105 PixelPainter(const Control& control)
110 virtual bool Draw(void)
112 if (REPEAT_COUNT < _count++)
117 byte red = Math::Rand() % 0xFF;
118 byte green = Math::Rand() % 0xFF;
119 byte blue = Math::Rand() % 0xFF;
120 byte alpha = Math::Rand() % 0xFF;
122 Color color(red, green, blue, alpha);
124 _pCanvas->SetForegroundColor(color);
126 int x = (Math::Rand() % (_bounds.width -2)) + 1;
127 int y = (Math::Rand() % (_bounds.height-2)) + 1;
129 _pCanvas->SetPixel(Point(x - 1, y - 1));
130 _pCanvas->SetPixel(Point(x + 1, y - 1));
131 _pCanvas->SetPixel(Point(x, y - 1));
132 _pCanvas->SetPixel(Point(x, y));
133 _pCanvas->SetPixel(Point(x - 1, y + 1));
134 _pCanvas->SetPixel(Point(x, y + 1));
135 _pCanvas->SetPixel(Point(x + 1, y + 1));
148 LinePainter(const Control& control)
155 if (REPEAT_COUNT < _count++)
160 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
162 byte red = Math::Rand() % 0xFF;
163 byte green = Math::Rand() % 0xFF;
164 byte blue = Math::Rand() % 0xFF;
165 byte alpha = Math::Rand() % 0xFF;
167 Color color(red, green, blue, alpha);
168 _pCanvas->SetForegroundColor(color);
170 Point point1(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
171 Point point2(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
173 _pCanvas->SetLineWidth(lineWidth);
174 _pCanvas->DrawLine(point1, point2);
183 class TrianglePainter
187 TrianglePainter(const Control& control)
194 if (REPEAT_COUNT < _count++)
199 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
201 _pCanvas->SetLineWidth(lineWidth);
203 byte red = Math::Rand() % 0xFF;
204 byte green = Math::Rand() % 0xFF;
205 byte blue = Math::Rand() % 0xFF;
206 byte alpha = Math::Rand() % 0xFF;
208 Color color(red, green, blue, alpha);
210 _pCanvas->SetForegroundColor(color);
212 Point point1(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
213 Point point2(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
214 Point point3(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
216 _pCanvas->DrawTriangle(point1, point2, point3);
218 point1.x = Math::Rand() % _bounds.width;
219 point1.y = Math::Rand() % _bounds.height;
220 point2.x = Math::Rand() % _bounds.width;
221 point2.y = Math::Rand() % _bounds.height;
222 point3.x = Math::Rand() % _bounds.width;
223 point3.y = Math::Rand() % _bounds.height;
225 _pCanvas->FillTriangle(color, point1, point2, point3);
234 class RectanglePainter
238 RectanglePainter(const Control& control)
245 if (REPEAT_COUNT < _count++)
250 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
252 _pCanvas->SetLineWidth(lineWidth);
254 byte red = Math::Rand() % 0xFF;
255 byte green = Math::Rand() % 0xFF;
256 byte blue = Math::Rand() % 0xFF;
257 byte alpha = Math::Rand() % 0xFF;
259 Color color(red, green, blue, alpha);
261 _pCanvas->SetForegroundColor(color);
263 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
265 _pCanvas->DrawRectangle(rect);
267 rect.x = Math::Rand() % _bounds.width;
268 rect.y = Math::Rand() % _bounds.height;
269 rect.width = Math::Rand() % _bounds.width;
270 rect.height = Math::Rand() % _bounds.height;
272 _pCanvas->FillRectangle(color, rect);
281 class RoundRectanglePainter
285 RoundRectanglePainter(const Control& control)
292 if (REPEAT_COUNT < _count++)
297 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
299 _pCanvas->SetLineWidth(lineWidth);
301 byte red = Math::Rand() % 0xFF;
302 byte green = Math::Rand() % 0xFF;
303 byte blue = Math::Rand() % 0xFF;
304 byte alpha = Math::Rand() % 0xFF;
306 Color color(red, green, blue, alpha);
308 _pCanvas->SetForegroundColor(color);
310 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width + 1, Math::Rand() % _bounds.height + 1);
311 Dimension round(rect.width / MAX_LINE_WIDTH, rect.height / MAX_LINE_WIDTH);
313 _pCanvas->DrawRoundRectangle(rect, round);
315 rect.x = Math::Rand() % _bounds.width;
316 rect.y = Math::Rand() % _bounds.height;
317 rect.width = Math::Rand() % _bounds.width;
318 rect.height = Math::Rand() % _bounds.height;
320 round.width = rect.width / MAX_LINE_WIDTH;
321 round.height = rect.height / MAX_LINE_WIDTH;
323 _pCanvas->FillRoundRectangle(color, rect, round);
336 EllipsePainter(const Control& control)
343 if (REPEAT_COUNT < _count++)
348 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
350 _pCanvas->SetLineWidth(lineWidth);
352 byte red = Math::Rand() % 0xFF;
353 byte green = Math::Rand() % 0xFF;
354 byte blue = Math::Rand() % 0xFF;
355 byte alpha = Math::Rand() % 0xFF;
357 Color color(red, green, blue, alpha);
359 _pCanvas->SetForegroundColor(color);
361 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
363 _pCanvas->DrawEllipse(rect);
365 rect.x = Math::Rand() % _bounds.width;
366 rect.y = Math::Rand() % _bounds.height;
367 rect.width = Math::Rand() % _bounds.width;
368 rect.height = Math::Rand() % _bounds.height;
370 _pCanvas->FillEllipse(color, rect);
383 ArcPainter(const Control& control)
390 if (REPEAT_COUNT < _count++)
395 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
397 _pCanvas->SetLineWidth(lineWidth);
399 byte red = Math::Rand() % 0xFF;
400 byte green = Math::Rand() % 0xFF;
401 byte blue = Math::Rand() % 0xFF;
402 byte alpha = Math::Rand() % 0xFF;
404 Color color(red, green, blue, alpha);
405 _pCanvas->SetForegroundColor(color);
407 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
409 int startAngle = Math::Rand() % 360;
410 int endAngle = Math::Rand() % 360;
412 ArcStyle arcStyle = ARC_STYLE_ONLY;
414 switch (Math::Rand() % 3)
417 arcStyle = ARC_STYLE_ONLY;
420 arcStyle = ARC_STYLE_CHORD;
423 arcStyle = ARC_STYLE_PIE;
427 _pCanvas->DrawArc(rect, startAngle, endAngle, arcStyle);
436 class PolylinePainter
440 PolylinePainter(const Control& control)
443 for (int i = 0; i < 6; i++)
445 __pointList.Add(*(new (std::nothrow) Point));
449 virtual ~PolylinePainter()
451 __pointList.RemoveAll(true);
456 if (REPEAT_COUNT < _count++)
461 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
463 _pCanvas->SetLineWidth(lineWidth);
465 byte red = Math::Rand() % 0xFF;
466 byte green = Math::Rand() % 0xFF;
467 byte blue = Math::Rand() % 0xFF;
468 byte alpha = Math::Rand() % 0xFF;
470 Color color(red, green, blue, alpha);
472 _pCanvas->SetForegroundColor(color);
474 for (int i = 0; i < __pointList.GetCount(); i++)
476 Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
480 pPoint->x = Math::Rand() % _bounds.width;
481 pPoint->y = Math::Rand() % _bounds.height;
485 _pCanvas->DrawPolyline(__pointList);
493 Osp::Base::Collection::ArrayList __pointList;
501 PolygonPainter(const Control& control)
504 for (int i = 0; i < 6; i++)
506 __pointList.Add(*(new (std::nothrow) Point));
510 virtual ~PolygonPainter()
512 __pointList.RemoveAll(true);
517 if (REPEAT_COUNT < _count++)
522 int lineWidth = (Math::Rand() % MAX_LINE_WIDTH) + 1;
524 _pCanvas->SetLineWidth(lineWidth);
526 byte red = Math::Rand() % 0xFF;
527 byte green = Math::Rand() % 0xFF;
528 byte blue = Math::Rand() % 0xFF;
529 byte alpha = Math::Rand() % 0xFF;
531 Color color(red, green, blue, alpha);
533 _pCanvas->SetForegroundColor(color);
535 for (int i = 0; i < __pointList.GetCount(); i++)
537 Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
541 pPoint->x = Math::Rand() % _bounds.width;
542 pPoint->y = Math::Rand() % _bounds.height;
546 _pCanvas->DrawPolygon(__pointList);
548 for (int i = 0; i < __pointList.GetCount(); i++)
550 Point* pPoint = dynamic_cast<Point*>(__pointList.GetAt(i));
554 pPoint->x = Math::Rand() % _bounds.width;
555 pPoint->y = Math::Rand() % _bounds.height;
559 _pCanvas->FillPolygon(color, __pointList);
567 Osp::Base::Collection::ArrayList __pointList;
575 TextPainter(const Control& control)
582 if (REPEAT_COUNT < _count++)
587 int fontStyle = FONT_STYLE_PLAIN;
589 switch (Math::Rand() % 3)
592 fontStyle = FONT_STYLE_PLAIN;
595 fontStyle = FONT_STYLE_BOLD;
598 fontStyle = FONT_STYLE_ITALIC;
602 int fontSize = Math::Rand() % MAX_FONT_SIZE;
611 result r = font.Construct(fontStyle, fontSize);
615 AppLog("Fails to construct font.\n");
619 _pCanvas->SetFont(font);
621 byte red = Math::Rand() % 0xFF;
622 byte green = Math::Rand() % 0xFF;
623 byte blue = Math::Rand() % 0xFF;
624 byte alpha = Math::Rand() % 0xFF;
626 Color color(red, green, blue, alpha);
628 _pCanvas->SetForegroundColor(color);
630 Point point(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
632 Osp::Base::String text(L"DrawingBoard Sample");
634 _pCanvas->DrawText(point, text);
647 BitmapPainter(const Control& control)
654 imageDecoder.Construct();
656 __pBitmap16 = imageDecoder.DecodeN(App::GetInstance()->GetAppRootPath() + L"res/tizen.png", BITMAP_PIXEL_FORMAT_RGB565);
657 __pBitmap32 = imageDecoder.DecodeN(App::GetInstance()->GetAppRootPath() + L"res/tizen.png", BITMAP_PIXEL_FORMAT_ARGB8888);
660 virtual ~BitmapPainter()
668 if (REPEAT_COUNT < _count++)
673 Point pos(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
675 _pCanvas->DrawBitmap(pos, *__pBitmap16);
677 pos.x = Math::Rand() % _bounds.width;
678 pos.y = Math::Rand() % _bounds.height;
680 _pCanvas->DrawBitmap(pos, *__pBitmap32);
682 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, (Math::Rand() % (_bounds.width - 1)) + 1, (Math::Rand() % (_bounds.height- 1)) + 1);
684 _pCanvas->DrawBitmap(rect, *__pBitmap16);
686 rect.x = Math::Rand() % _bounds.width;
687 rect.y = Math::Rand() % _bounds.height;
688 rect.width = (Math::Rand() % (_bounds.width - 1)) + 1;
689 rect.height = (Math::Rand() % (_bounds.height- 1)) + 1;
691 _pCanvas->DrawBitmap(rect, *__pBitmap32);
693 rect.x = Math::Rand() % __pBitmap16->GetWidth();
694 rect.y = Math::Rand() % __pBitmap16->GetHeight();
695 rect.width = (Math::Rand() % (__pBitmap16->GetWidth() - 1)) + 1;
696 rect.height = (Math::Rand() % (__pBitmap16->GetHeight() - 1)) + 1;
698 if (rect.x + rect.width > __pBitmap16->GetWidth())
700 rect.width = __pBitmap16->GetWidth() - rect.x;
703 if (rect.y + rect.height > __pBitmap16->GetHeight())
705 rect.height = __pBitmap16->GetHeight() - rect.y;
708 Rectangle destRect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, (Math::Rand() % (_bounds.width - 1)) + 1, (Math::Rand() % (_bounds.height- 1)) + 1);
710 _pCanvas->DrawBitmap(destRect, *__pBitmap16, rect);
712 rect.x = Math::Rand() % __pBitmap32->GetWidth();
713 rect.y = Math::Rand() % __pBitmap32->GetHeight();
714 rect.width = (Math::Rand() % (__pBitmap32->GetWidth() - 1)) + 1;
715 rect.height = (Math::Rand() % (__pBitmap32->GetHeight()- 1)) + 1;
717 if (rect.x + rect.width > __pBitmap32->GetWidth())
719 rect.width = __pBitmap32->GetWidth() - rect.x;
722 if (rect.y + rect.height > __pBitmap32->GetHeight())
724 rect.height = __pBitmap32->GetHeight() - rect.y;
727 destRect.x = Math::Rand() % _bounds.width;
728 destRect.y = Math::Rand() % _bounds.height;
729 destRect.width = (Math::Rand() % (_bounds.width - 1)) + 1;
730 destRect.height = (Math::Rand() % (_bounds.height- 1)) + 1;
732 _pCanvas->DrawBitmap(destRect, *__pBitmap32, rect);
745 class DirectBufferPainter
749 DirectBufferPainter(const Control& control)
756 if (REPEAT_COUNT < _count++)
761 byte red = Math::Rand() % 0xFF;
762 byte green = Math::Rand() % 0xFF;
763 byte blue = Math::Rand() % 0xFF;
764 byte alpha = Math::Rand() % 0xFF;
766 Color color(red, green, blue, alpha);
768 _pCanvas->SetForegroundColor(color);
770 Rectangle rect(Math::Rand() % _bounds.width, Math::Rand() % _bounds.height, Math::Rand() % _bounds.width, Math::Rand() % _bounds.height);
772 FillRectangle(*_pCanvas, color, rect);
779 void FillRectangle(Canvas& canvas, const Color &color, const Rectangle &rect)
781 BufferInfo bufferInfo;
797 unsigned short color16;
798 unsigned short* pBuffer16;
799 unsigned long* pBuffer32;
812 _pCanvas->Lock(bufferInfo);
817 height = rect.height;
820 y2 = y1 + height - 1;
824 bufferX2 = bufferX1 + bufferInfo.width - 1;
825 bufferY2 = bufferY1 + bufferInfo.height - 1;
827 if (x1 > bufferX2 || x2 < bufferX1)
832 if (y1 > bufferY2 || y2 < bufferY1)
858 height = y2 - y1 + 1;
860 bufferWidth = bufferInfo.width;
862 if (bufferInfo.bitsPerPixel == 16)
865 color16 = static_cast<unsigned short>(COLOR32ToCOLOR16(color.GetRGB32()));
866 pBuffer16 = static_cast<unsigned short*>(bufferInfo.pPixels);
868 alphaLevel = 100 - static_cast<int>(static_cast<float>(color.GetAlpha() / 2.56));
869 srcLevel = alphaLevel;
870 destLevel = 100 - alphaLevel;
872 srcR = static_cast<byte>(color16 >> 11);
873 srcG = static_cast<byte>((color16 >> 6) & 0x001F);
874 srcB = static_cast<byte>(color16 & 0x001F);
876 for (y = y1; y < y1 + height; y++)
878 for (x = x1; x < x1 + width; x++)
880 offset = bufferWidth * y + x;
881 destR = static_cast<byte>(pBuffer16[offset] >> 11);
882 destG = static_cast<byte>((pBuffer16[offset] >> 6) & 0x001F);
883 destB = static_cast<byte>(pBuffer16[offset] & 0x001F);
885 r = (destR * destLevel + srcR * srcLevel) / 100;
886 g = (destG * destLevel + srcG * srcLevel) / 100;
887 b = (destB * destLevel + srcB * srcLevel) / 100;
889 destR = static_cast<byte>(r);
890 destG = static_cast<byte>(g);
891 destB = static_cast<byte>(b);
892 pBuffer16[offset] = static_cast<unsigned short>((destR << 11) | (destG << 6) | (destB));
896 else if (bufferInfo.bitsPerPixel == 32)
898 pBuffer32 = (unsigned long*)bufferInfo.pPixels;
900 for (y = y1; y < y1 + height; y++)
902 for (x = x1; x < x1 + width; x++)
904 offset = bufferWidth * y + x;
905 pBuffer32[offset] = color.GetRGB32();
915 class ColorTablePainter
919 ColorTablePainter(const Control& control)
934 if (__y >= _bounds.height)
939 Color color(__red, __green, __blue, __alpha);
941 _pCanvas->FillRectangle(color, Rectangle(__x, __y, __width, __height));
950 if (__red >= 0xFF && __green < 0xFF)
955 if (__green >= 0xFF && __blue < 0xFF)
962 if (__x >= _bounds.width)
983 DrawingBoard::DrawingBoard()
992 DrawingBoard::~DrawingBoard()
998 DrawingBoard::CreateInstance(void)
1000 // You can create the instance through another constructor.
1001 return new (std::nothrow) DrawingBoard();
1005 DrawingBoard::OnAppInitializing(AppRegistry& appRegistry)
1007 Frame* pAppFrame = new (std::nothrow) Frame();
1009 pAppFrame->Construct();
1011 this->AddFrame(*pAppFrame);
1013 result r = E_SUCCESS;
1015 __pForm = new (std::nothrow) DrawingForm(this);
1016 TryCatch(__pForm != null, , "Failed to allocate memory.");
1018 r = __pForm->Construct(FORM_STYLE_NORMAL);
1019 TryCatch(!IsFailed(r), , "__pForm->Construct(FORM_STYLE_NORMAL) failed.");
1021 __pForm->SetBackgroundColor(Color::GetColor(COLOR_ID_BLACK));
1022 r = this->GetAppFrame()->GetFrame()->AddControl(*__pForm);
1023 TryCatch(!IsFailed(r), , "__pForm->Construct(FORM_STYLE_NORMAL) failed.");
1025 __pTimer = new (std::nothrow) Timer;
1026 TryCatch(__pTimer != null, , "Failed to allocate memory.");
1028 r = __pTimer->Construct(*this);
1029 TryCatch(!IsFailed(r), , "Failed to construct timer.");
1037 if (__pTimer != null)
1050 DrawingBoard::OnAppTerminating(AppRegistry& appRegistry, bool forcedTermination)
1052 if (__pTimer != null)
1060 if (__pPainter != null)
1071 DrawingBoard::OnForeground(void)
1073 if (__pTimer != null)
1075 __pTimer->Start(TIME_OUT);
1081 DrawingBoard::OnBackground(void)
1083 if (__pTimer != null)
1088 if (__pForm != null)
1090 Canvas* pCanvas = __pForm->GetCanvasN();
1092 pCanvas->SetBackgroundColor(Color(0x00000000));
1103 DrawingBoard::OnLowMemory(void)
1109 DrawingBoard::OnBatteryLevelChanged(BatteryLevel batteryLevel)
1115 DrawingBoard::OnTimerExpired(Timer& timer)
1117 if (__pTimer == null)
1122 if (__pPainter == null)
1128 bool result = __pPainter->Draw();
1136 __pTimer->Start(TIME_OUT);
1140 DrawingBoard::ChangePainter()
1142 if (__pForm == null)
1150 switch (__scenarioIndex)
1153 __pPainter = new (std::nothrow) PixelPainter(*__pForm);
1156 __pPainter = new (std::nothrow) LinePainter(*__pForm);
1159 __pPainter = new (std::nothrow) TrianglePainter(*__pForm);
1162 __pPainter = new (std::nothrow) RectanglePainter(*__pForm);
1165 __pPainter = new (std::nothrow) RoundRectanglePainter(*__pForm);
1168 __pPainter = new (std::nothrow) EllipsePainter(*__pForm);
1171 __pPainter = new (std::nothrow) ArcPainter(*__pForm);
1174 __pPainter = new (std::nothrow) TextPainter(*__pForm);
1177 __pPainter = new (std::nothrow) BitmapPainter(*__pForm);
1180 __pPainter = new (std::nothrow) DirectBufferPainter(*__pForm);
1183 __pPainter = new (std::nothrow) ColorTablePainter(*__pForm);
1186 __pPainter = new (std::nothrow) PolylinePainter(*__pForm);
1189 __pPainter = new (std::nothrow) PolygonPainter(*__pForm);
1192 __scenarioIndex = 0;
1198 if (__scenarioIndex > 12)
1200 __scenarioIndex = 0;
1205 DrawingBoard::Draw(void)
1207 if (__pPainter != null)
1214 DrawingBoard::OnScreenOn (void)
1219 DrawingBoard::OnScreenOff (void)