db2c9bf564f15813095601d46cae202e7318a12f
[platform/core/uifw/lottie-player.git] / example / rlottiePlayer / rlottiePlayer.cpp
1 // rlottiePlayer.cpp : Defines the entry point for the application.
2 //
3
4
5 #include "framework.h"
6 #include "rlottiePlayer.h"
7 using namespace Gdiplus;
8
9 #define MAX_LOADSTRING 100
10
11 // Global Variables:
12 HINSTANCE hInst;                                // current instance
13 WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
14 WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name
15 HWND mainWindow;                                    // Main Window Instance
16 HWND hTextFileToBeOpened;                               // openDialog file path
17 HWND hBtnPlay;
18 HWND hSliderPlay, hSliderCanvasResize;
19 UINT curFrame = 0;
20 RlottieBitmap anim;                                          // rendered Animation Bitmap
21 RECT animRect, backRect;
22 Gdiplus::Color backColor(255, 255, 255, 255);
23 Gdiplus::Color borderColor(255, 0, 0, 0);
24 bool isBackgroundChanged = false;
25
26 // Forward declarations of functions included in this code module:
27 ATOM                MyRegisterClass(HINSTANCE hInstance);
28 BOOL                InitInstance(HINSTANCE, int);
29 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
30 INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
31 void openJSONFileDialog(HWND);
32 void initUIControl(HWND);
33 void dlgUICommand(HWND, WPARAM);
34 void resizeCanvas(HWND, int);
35 void changeBackgroundColor(int r, int g, int b);
36
37 // Animation Rendering Functions
38 void draw(HDC);
39 Bitmap* CreateBitmap(void* data, unsigned int width, unsigned int height);
40 void renderAnimation(UINT);
41
42 int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
43     _In_opt_ HINSTANCE hPrevInstance,
44     _In_ LPWSTR    lpCmdLine,
45     _In_ int       nCmdShow)
46 {
47     UNREFERENCED_PARAMETER(hPrevInstance);
48     UNREFERENCED_PARAMETER(lpCmdLine);
49
50     // initialize Gdiplus
51     Gdiplus::GdiplusStartupInput gdiplusStartUpInput;
52     ULONG_PTR gdiplustoken;
53     Gdiplus::GdiplusStartup(&gdiplustoken, &gdiplusStartUpInput, nullptr);
54
55     // Initialize global strings
56     LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
57     LoadStringW(hInstance, IDC_RLOTTIEPLAYER, szWindowClass, MAX_LOADSTRING);
58     MyRegisterClass(hInstance);
59
60     // Perform application initialization:
61     if (!InitInstance(hInstance, nCmdShow))
62     {
63         return FALSE;
64     }
65
66     HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RLOTTIEPLAYER));
67
68     MSG msg;
69
70     // Main message loop:
71     while (GetMessage(&msg, nullptr, 0, 0))
72     {
73         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
74         {
75             TranslateMessage(&msg);
76             DispatchMessage(&msg);
77         }
78     }
79
80     Gdiplus::GdiplusShutdown(gdiplustoken);
81     return (int)msg.wParam;
82 }
83
84
85
86 //
87 //  FUNCTION: MyRegisterClass()
88 //
89 //  PURPOSE: Registers the window class.
90 //
91 ATOM MyRegisterClass(HINSTANCE hInstance)
92 {
93     WNDCLASSEXW wcex;
94
95     wcex.cbSize = sizeof(WNDCLASSEX);
96
97     wcex.style = CS_HREDRAW | CS_VREDRAW;
98     wcex.lpfnWndProc = WndProc;
99     wcex.cbClsExtra = 0;
100     wcex.cbWndExtra = 0;
101     wcex.hInstance = hInstance;
102     wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RLOTTIEPLAYER));
103     wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
104     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
105     wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_RLOTTIEPLAYER);
106     wcex.lpszClassName = szWindowClass;
107     wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
108
109     return RegisterClassExW(&wcex);
110 }
111
112 //
113 //   FUNCTION: InitInstance(HINSTANCE, int)
114 //
115 //   PURPOSE: Saves instance handle and creates main window
116 //
117 //   COMMENTS:
118 //
119 //        In this function, we save the instance handle in a global variable and
120 //        create and display the main program window.
121 //
122 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
123 {
124     hInst = hInstance; // Store instance handle in our global variable
125
126     DWORD dwStyle = WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
127     mainWindow = CreateWindowEx(0, szWindowClass, szTitle, dwStyle,
128         CW_USEDEFAULT, CW_USEDEFAULT, WND_WIDTH, WND_HEIGHT, nullptr, nullptr, hInstance, nullptr);
129
130     if (!mainWindow)
131     {
132         return FALSE;
133     }
134
135     ShowWindow(mainWindow, nCmdShow);
136     UpdateWindow(mainWindow);
137     SetMenu(mainWindow, NULL);
138
139     return TRUE;
140 }
141
142 //
143 //  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
144 //
145 //  PURPOSE: Processes messages for the main window.
146 //
147 //  WM_COMMAND  - process the application menu
148 //  WM_PAINT    - Paint the main window
149 //  WM_DESTROY  - post a quit message and return
150 //
151 //
152 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
153 {
154     static bool isplay = false;
155     int wmId = LOWORD(wParam);
156
157     switch (message)
158     {
159     case WM_CREATE:
160     {
161         initUIControl(hWnd);
162         initAnimation(BMP_MAX_LEN, BMP_MAX_LEN);
163         break;
164     }
165     case WM_TIMER:
166     {
167         switch (wmId)
168         {
169         case TIMER_PLAY_ANIM:
170         {
171             renderAnimation(curFrame + 1);
172             SendMessage(hSliderPlay, TBM_SETPOS, TRUE, curFrame);
173             break;
174         }
175         default:
176             break;
177         }
178         break;
179     }
180     case WM_COMMAND:
181     {
182         // Parse the menu selections:
183         switch (wmId)
184         {
185         case IDM_ABOUT:
186             DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
187             break;
188
189         case IDM_EXIT:
190             DestroyWindow(hWnd);
191             break;
192
193         case BTN_BROWSE:
194             openJSONFileDialog(hWnd);
195             break;
196
197         case BTN_PLAY:
198         {
199             LPWSTR textBtnPlay;
200             USES_CONVERSION;
201             if (isplay)
202             {
203                 isplay = false;
204                 textBtnPlay = A2W("Play");
205                 KillTimer(hWnd, TIMER_PLAY_ANIM);
206             }
207             else
208             {
209                 isplay = true;
210                 textBtnPlay = A2W("Pause");
211                 SetTimer(hWnd, TIMER_PLAY_ANIM, 10, NULL);
212             }
213             SetWindowText(hBtnPlay, textBtnPlay);
214             break;
215         }
216
217         case WM_DROPFILES:
218             break;
219         case BTN_WHITE:
220             changeBackgroundColor(1, 1, 1);
221             break;
222         case BTN_BLACK:
223             changeBackgroundColor(0, 0, 0);
224             break;
225         case BTN_RED:
226             changeBackgroundColor(1, 0, 0);
227             break;
228         case BTN_GREEN:
229             changeBackgroundColor(0, 1, 0);
230             break;
231         case BTN_BLUE:
232             changeBackgroundColor(0, 0, 1);
233             break;
234
235         default:
236             return DefWindowProc(hWnd, message, wParam, lParam);
237         }
238     }
239     break;
240
241     case WM_HSCROLL:
242     {
243         if ((lParam != 0) && (reinterpret_cast<HWND>(lParam) == hSliderPlay))
244         {
245             UINT frameNum = SendDlgItemMessage(hWnd, SLIDER_PLAY, TBM_GETPOS, 0, 0);
246             renderAnimation(frameNum);
247         }
248         else if ((lParam != 0) && (reinterpret_cast<HWND>(lParam) == hSliderCanvasResize))
249         {
250             static int curSize = anim.width / RESIZE_LENGTH;
251             int newSize = SendDlgItemMessage(hWnd, SLIDER_CANVAS_RESIZE, TBM_GETPOS, 0, 0);
252             resizeCanvas(hWnd, (curSize - newSize) * RESIZE_LENGTH);
253             curSize = newSize;
254         }
255         break;
256     }
257
258     case WM_PAINT:
259     {
260         PAINTSTRUCT ps;
261         HDC hdc = BeginPaint(hWnd, &ps);
262         draw(hdc);
263         EndPaint(hWnd, &ps);
264     }
265     break;
266     case WM_CTLCOLORSTATIC:
267     {
268         static HBRUSH hBrushEdit = CreateSolidBrush(RGB(255, 255, 255));
269         return (LRESULT)hBrushEdit;
270     }
271
272     case WM_DESTROY:
273         PostQuitMessage(0);
274         break;
275
276     default:
277         return DefWindowProc(hWnd, message, wParam, lParam);
278     }
279     return 0;
280 }
281
282 // Message handler for about box.
283 INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
284 {
285     UNREFERENCED_PARAMETER(lParam);
286     switch (message)
287     {
288     case WM_INITDIALOG:
289         return (INT_PTR)TRUE;
290
291     case WM_COMMAND:
292         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
293         {
294             EndDialog(hDlg, LOWORD(wParam));
295             return (INT_PTR)TRUE;
296         }
297         break;
298     }
299     return (INT_PTR)FALSE;
300 }
301
302 void openJSONFileDialog(HWND hDlg)
303 {
304     OPENFILENAME ofn;       // common dialog box structure
305     TCHAR szFile[260] = { 0 };       // if using TCHAR macros
306
307     // Initialize OPENFILENAME
308     ZeroMemory(&ofn, sizeof(ofn));
309     ofn.lStructSize = sizeof(ofn);
310     ofn.hwndOwner = hDlg;
311     ofn.lpstrFile = szFile;
312     ofn.nMaxFile = sizeof(szFile);
313     ofn.lpstrFilter = _T("JSON\0*.json\0");
314     ofn.nFilterIndex = 1;
315     ofn.lpstrFileTitle = NULL;
316     ofn.nMaxFileTitle = 0;
317     ofn.lpstrInitialDir = NULL;
318     ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
319
320     if (GetOpenFileName(&ofn))
321     {
322         SetWindowText(hTextFileToBeOpened, ofn.lpstrFile);
323         // LPWSTR(w_char*) -> LPSTR(char*)
324         USES_CONVERSION;
325         LPSTR path = W2A(ofn.lpstrFile);
326
327         setAnimation(path, BMP_MAX_LEN, BMP_MAX_LEN);
328         // init play slider
329         SendMessage(hSliderPlay, TBM_SETRANGE, FALSE, MAKELPARAM(0, getTotalFrame()));
330         SendMessage(hSliderPlay, TBM_SETPOS, TRUE, 0);
331         renderAnimation(0);
332     }
333 }
334
335 void draw(HDC hdc)
336 {
337     Graphics gf(hdc);
338     int half_interval = UI_INTERVAL / 2;
339
340     // background
341     SolidBrush brush(backColor);
342     int back_y = half_interval + BTN_HEIGHT;
343     int back_height = back_y + BMP_MAX_LEN + UI_INTERVAL;
344     if (isBackgroundChanged)
345     {
346         isBackgroundChanged = false;
347         gf.FillRectangle(&brush, 0, back_y, WND_WIDTH, back_height);
348     }
349
350     // image borderline
351     Pen pen(borderColor);
352     gf.DrawRectangle(&pen, anim.x - half_interval, anim.y - half_interval, anim.width + half_interval * 2, anim.height + half_interval * 2);
353
354     // image
355     if (anim.image != NULL)
356     {
357         gf.DrawImage(anim.image, anim.x, anim.y, anim.width, anim.height);
358     }
359 }
360
361 Bitmap* CreateBitmap(void* data, unsigned int width, unsigned int height)
362 {
363     BITMAPINFO Info;
364     memset(&Info, 0, sizeof(Info));
365
366     Info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
367     Info.bmiHeader.biWidth = 500;
368     Info.bmiHeader.biHeight = 500;
369     Info.bmiHeader.biPlanes = 1;
370     Info.bmiHeader.biBitCount = 32;
371     Info.bmiHeader.biCompression = BI_RGB;
372     Info.bmiHeader.biSizeImage = 0;  //(((32 * width + 31) & ~31) / 8) * height;
373
374     return new Gdiplus::Bitmap(&Info, data);
375 }
376
377 void renderAnimation(UINT frameNum)
378 {
379     if (isAnimNULL()) return;
380     if (anim.image != NULL) delete anim.image;
381
382     curFrame = frameNum % getTotalFrame();
383
384     // render
385     UINT* resRender = renderRLottieAnimation(curFrame);
386     anim.image = CreateBitmap(resRender, BMP_MAX_LEN, BMP_MAX_LEN);
387     anim.image->RotateFlip(RotateNoneFlipY);
388     // call WM_PAINT message
389     InvalidateRect(mainWindow, &animRect, FALSE);
390 }
391
392 void initUIControl(HWND hWnd)
393 {
394     int half_ui_interval = UI_INTERVAL / 2;
395
396     // button browse
397     int browse_x = UI_INTERVAL;
398     int browse_y = half_ui_interval;
399     CreateWindow(L"button", L"Browse", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
400         browse_x, browse_y, BTN_WIDTH, BTN_HEIGHT, hWnd, (HMENU)BTN_BROWSE, hInst, NULL);
401
402     // textbox FilePath
403     int textFile_x = browse_x + BTN_WIDTH + UI_INTERVAL;
404     int textFile_y = browse_y;
405     hTextFileToBeOpened = CreateWindowEx(0, L"static", L"No file selected.", WS_CHILD | WS_VISIBLE | ES_LEFT,
406         textFile_x, textFile_y, WND_WIDTH * 0.6, TEXT_HEIGHT, hWnd, (HMENU)TEXT_FILENAME, hInst, 0);
407
408     // image
409     anim.x = WND_WIDTH / 4;
410     anim.y = browse_y + BTN_HEIGHT + UI_INTERVAL * 2;
411     anim.width = BMP_MAX_LEN;
412     anim.height = anim.width;
413
414     // animating range
415     SetRect(&animRect,
416         anim.x - UI_INTERVAL,
417         anim.y - UI_INTERVAL,
418         anim.x + anim.width + UI_INTERVAL * 2,
419         anim.y + anim.height + UI_INTERVAL * 2
420     );
421
422     // background range
423     SetRect(&backRect,
424         0,
425         anim.y - UI_INTERVAL,
426         WND_WIDTH,
427         anim.y + anim.height + UI_INTERVAL * 2);
428
429     // text Background Color
430     int textBC_x = WND_WIDTH / 20;
431     int textBC_y = anim.y + anim.height + UI_INTERVAL * 2;
432     CreateWindowEx(0, L"static", L"Background Color", WS_CHILD | WS_VISIBLE | ES_LEFT,
433         textBC_x, textBC_y, 120, TEXT_HEIGHT, hWnd, (HMENU)TEXT_FILENAME, hInst, 0);
434
435     // radio button
436     // white
437     int white_x = WND_WIDTH / 20;
438     int white_y = textBC_y + TEXT_HEIGHT + half_ui_interval;
439     CreateWindowEx(0, L"button", L"White", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
440         white_x, white_y, RDOBTN_WIDTH, RDOBTN_HEIGHT, hWnd, (HMENU)BTN_WHITE, hInst, NULL);
441
442     // black
443     int black_x = white_x + RDOBTN_WIDTH + half_ui_interval;
444     int black_y = white_y;
445     CreateWindowEx(0, L"button", L"Black", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
446         black_x, black_y, RDOBTN_WIDTH, RDOBTN_HEIGHT, hWnd, (HMENU)BTN_BLACK, hInst, NULL);
447
448     // red
449     int red_x = black_x + RDOBTN_WIDTH + half_ui_interval;
450     int red_y = white_y;
451     CreateWindowEx(0, L"button", L"Red", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
452         red_x, red_y, RDOBTN_WIDTH, RDOBTN_HEIGHT, hWnd, (HMENU)BTN_RED, hInst, NULL);
453
454     // green
455     int green_x = red_x + RDOBTN_WIDTH + half_ui_interval;
456     int green_y = white_y;
457     CreateWindowEx(0, L"button", L"Green", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
458         green_x, green_y, RDOBTN_WIDTH, RDOBTN_HEIGHT, hWnd, (HMENU)BTN_GREEN, hInst, NULL);
459
460     // blue
461     int blue_x = green_x + RDOBTN_WIDTH + half_ui_interval;
462     int blue_y = white_y;
463     CreateWindowEx(0, L"button", L"Blue", WS_VISIBLE | WS_CHILD | BS_AUTORADIOBUTTON,
464         blue_x, blue_y, RDOBTN_WIDTH, RDOBTN_HEIGHT, hWnd, (HMENU)BTN_BLUE, hInst, NULL);
465
466     CheckRadioButton(hWnd, BTN_WHITE, BTN_BLUE, BTN_WHITE);
467
468     // text Canvas Resize
469     int textCR_x = WND_WIDTH / 2;
470     int textCR_y = textBC_y;
471     CreateWindowEx(0, L"static", L"Canvas Resize", WS_CHILD | WS_VISIBLE | ES_LEFT,
472         textCR_x, textCR_y, 120, TEXT_HEIGHT, hWnd, (HMENU)TEXT_FILENAME, hInst, 0);
473
474     // slider Canvas Resize
475     int sliderCR_x = textCR_x;
476     int sliderCR_y = textCR_y + TEXT_HEIGHT + half_ui_interval;
477     hSliderCanvasResize = CreateWindowExW(0, TRACKBAR_CLASSW, NULL, WS_CHILD | WS_VISIBLE | TBS_FIXEDLENGTH | TBS_NOTICKS,
478         sliderCR_x, sliderCR_y, WND_WIDTH * 0.2, SLIDER_HEIGHT, hWnd, (HMENU)SLIDER_CANVAS_RESIZE, hInst, NULL);
479
480     // init resize slider
481     UINT sizeSlider = anim.width / RESIZE_LENGTH;
482     SendMessage(hSliderCanvasResize, TBM_SETRANGE, FALSE, MAKELPARAM(0, sizeSlider));
483     SendMessage(hSliderCanvasResize, TBM_SETPOS, TRUE, sizeSlider);
484
485     // button play
486     int btnPlay_x = WND_WIDTH / 10;
487     int btnPlay_y = red_y + RDOBTN_HEIGHT + UI_INTERVAL * 2;
488     hBtnPlay = CreateWindow(L"button", L"Play", WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
489         btnPlay_x, btnPlay_y, BTN_WIDTH, BTN_HEIGHT, hWnd, (HMENU)BTN_PLAY, hInst, NULL);
490
491     // slider play
492     int sliderPlay_x = btnPlay_x + BTN_WIDTH + UI_INTERVAL;
493     int sliderPlay_y = btnPlay_y;
494     hSliderPlay = CreateWindowExW(0, TRACKBAR_CLASSW, NULL, WS_CHILD | WS_VISIBLE | TBS_FIXEDLENGTH | TBS_NOTICKS,
495         sliderPlay_x, sliderPlay_y, WND_WIDTH * 0.6, SLIDER_HEIGHT, hWnd, (HMENU)SLIDER_PLAY, hInst, NULL);
496 }
497
498 void resizeCanvas(HWND hWnd, int resizeValue)
499 {
500     isBackgroundChanged = true;
501     anim.x += resizeValue / 2;
502     anim.y += resizeValue / 2;
503     anim.width -= resizeValue;
504     anim.height -= resizeValue;
505     InvalidateRect(hWnd, &animRect, TRUE);
506 }
507
508 void changeBackgroundColor(int r, int g, int b)
509 {
510     isBackgroundChanged = true;
511     backColor = Gdiplus::Color(r * 255, g * 255, b * 255);
512     if (r + g + b == 0) borderColor = Gdiplus::Color(255, 255, 255);
513     else borderColor = Gdiplus::Color(0, 0, 0);
514     setAnimationColor(r, g, b);
515     renderAnimation(curFrame);
516     InvalidateRect(mainWindow, &backRect, FALSE);
517 }