uxsampletest.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include"evasapp.h"
  19. #include "lottieview.h"
  20. #include <memory>
  21. #include<vector>
  22. #include<string>
  23. class UxSampleTest
  24. {
  25. public:
  26. UxSampleTest(EvasApp *app, bool renderMode) {
  27. mApp = app;
  28. mRenderMode = renderMode;
  29. mResourceList = EvasApp::jsonFiles(std::string(DEMO_DIR) + "UXSample_1920x1080/");
  30. mRepeatMode = LottieView::RepeatMode::Restart;
  31. }
  32. void showPrev() {
  33. if (mResourceList.empty()) return;
  34. mCurIndex--;
  35. if (mCurIndex < 0)
  36. mCurIndex = mResourceList.size() - 1;
  37. show();
  38. }
  39. void showNext() {
  40. if (mResourceList.empty()) return;
  41. mCurIndex++;
  42. if (mCurIndex >= int(mResourceList.size()))
  43. mCurIndex = 0;
  44. show();
  45. }
  46. void resize() {
  47. if (mView) {
  48. mView->setSize(mApp->width(), mApp->height());
  49. }
  50. }
  51. private:
  52. void show() {
  53. mView = std::make_unique<LottieView>(mApp->evas(), Strategy::renderCAsync);
  54. mView->setFilePath(mResourceList[mCurIndex].c_str());
  55. mView->setPos(0, 0);
  56. mView->setSize(mApp->width(), mApp->height());
  57. mView->show();
  58. mView->play();
  59. mView->loop(true);
  60. mView->setRepeatMode(mRepeatMode);
  61. }
  62. public:
  63. EvasApp *mApp;
  64. bool mRenderMode = false;
  65. int mCurIndex = -1;
  66. std::vector<std::string> mResourceList;
  67. std::unique_ptr<LottieView> mView;
  68. LottieView::RepeatMode mRepeatMode;
  69. };
  70. static void
  71. onExitCb(void *data, void */*extra*/)
  72. {
  73. UxSampleTest *view = (UxSampleTest *)data;
  74. delete view;
  75. }
  76. static void
  77. onKeyCb(void *data, void *extra)
  78. {
  79. UxSampleTest *view = (UxSampleTest *)data;
  80. char *keyname = (char *)extra;
  81. if (!strcmp(keyname, "Right") || !strcmp(keyname, "n")) {
  82. view->showNext();
  83. } else if (!strcmp(keyname, "Left") || !strcmp(keyname, "p")) {
  84. view->showPrev();
  85. } else if (!strcmp(keyname,"r")) {
  86. if (view->mRepeatMode == LottieView::RepeatMode::Restart) {
  87. view->mRepeatMode = LottieView::RepeatMode::Reverse;
  88. } else
  89. view->mRepeatMode = LottieView::RepeatMode::Restart;
  90. if (view->mView)
  91. view->mView->setRepeatMode(view->mRepeatMode);
  92. }
  93. }
  94. static void
  95. onResizeCb(void *data, void */*extra*/)
  96. {
  97. UxSampleTest *view = (UxSampleTest *)data;
  98. view->resize();
  99. }
  100. int
  101. main(int argc, char **argv)
  102. {
  103. EvasApp *app = new EvasApp(800, 800);
  104. app->setup();
  105. bool renderMode = true;
  106. if (argc > 1) {
  107. if (!strcmp(argv[1],"--disable-render"))
  108. renderMode = false;
  109. }
  110. UxSampleTest *view = new UxSampleTest(app, renderMode);
  111. view->showNext();
  112. app->addExitCb(onExitCb, view);
  113. app->addKeyCb(onKeyCb, view);
  114. app->addResizeCb(onResizeCb, view);
  115. app->run();
  116. delete app;
  117. return 0;
  118. }