example.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. #include "oxygine-framework.h"
  2. #include "test.h"
  3. #include "ae/AEMovie.h"
  4. #include "AEMovieWork.h"
  5. #include "ox/ZipFileSystem.hpp"
  6. //#include "MovieSprite.h"
  7. using namespace oxygine;
  8. //#define MULTIWINDOW 1
  9. #if MULTIWINDOW
  10. spStage stage2;
  11. #endif
  12. AEMovieResource movieRes;
  13. extern std::string aeProject;
  14. extern std::string aeCurrent;
  15. extern Vector2 aeWorkArea;
  16. DECLARE_SMART(Preview, spPreview);
  17. namespace test
  18. {
  19. bool AeMasksDisabled = false;
  20. bool AeDontDraw = false;
  21. int AeHalfFillrate = 0;
  22. }
  23. class Preview : public Test
  24. {
  25. public:
  26. static spPreview instance;
  27. spAEMovieWork movie;
  28. spColorRectSprite _cr;
  29. spColorRectSprite _cr2;
  30. bool _looped = false;
  31. static bool _bb;
  32. static bool _show_all;
  33. float _allscale = 1.0f;
  34. Preview(const string &id)
  35. {
  36. addButton("play_loop", "play loop");
  37. addButton("play_once", "play once");
  38. addButton("interrupt", "interrupt");
  39. addButton("pause", "pause");
  40. addButton("resume", "resume");
  41. addButton("wa", "ae work area");
  42. addButton("wireframe", "wireframe");
  43. //addButton("int_off", "interpolation off");
  44. //addButton("int_on", "interpolation on");
  45. addButton("show_all", _show_all ? "show all: on" : "show all: off");
  46. addButton("bounding_box", _bb ? "bounding box: on" : "bounding box: off");
  47. addButton("overdraw", "show overdraw");
  48. addButton("mask", "disable mask");
  49. _color = Color::Red;
  50. _txtColor = Color::White;
  51. //()
  52. Test::toggle t[2] = { {"use work area", 1}, {"no work area", 2} };
  53. //addToggle("wa", t, 2);
  54. addClickListener([=](Event*) {
  55. });
  56. addEventListener(TouchEvent::WHEEL_DIR, [=] (Event* ev) {
  57. TouchEvent *te = safeCast<TouchEvent*>(ev);
  58. int b = movie->getWireframeMode();
  59. if (te->wheelDirection.y < 0)
  60. {
  61. b += 1;
  62. }
  63. if (te->wheelDirection.y > 0)
  64. {
  65. b -= 1;
  66. }
  67. if (b < -1)
  68. b = movie->getLastBatches();
  69. if (b > 0)
  70. {
  71. b = b % (movie->getLastBatches() + 1 );
  72. }
  73. movie->setWireframeMode(b);
  74. });
  75. set(id);
  76. }
  77. void toggleClicked(string id, const toggle* data)
  78. {
  79. }
  80. void clicked(string id)
  81. {
  82. size_t p = id.find(':');
  83. if (p != id.npos)
  84. {
  85. string ev = id.substr(p + 1);
  86. movie->playEvent(ev.c_str(), true);
  87. }
  88. if (id == "wa")
  89. {
  90. movie->playArea(aeWorkArea.x * 1000.0f, aeWorkArea.y * 1000.0f, true);
  91. }
  92. if (id == "play_loop")
  93. {
  94. movie->play(true);
  95. }
  96. if (id == "play_once")
  97. {
  98. movie->play(false);
  99. }
  100. if (id == "pause")
  101. {
  102. movie->pause();
  103. }
  104. if (id == "resume")
  105. {
  106. movie->resume();
  107. }
  108. if (id == "wireframe")
  109. {
  110. }
  111. if (id == "mask")
  112. {
  113. test::AeMasksDisabled = !test::AeMasksDisabled;
  114. updateText("mask", test::AeMasksDisabled ? "enable mask" : "disable mask");
  115. }
  116. if (id == "interrupt")
  117. {
  118. movie->interrupt();
  119. }
  120. if (id == "int_on")
  121. {
  122. // movie->setInterpolation(true);
  123. }
  124. if (id == "int_off")
  125. {
  126. // movie->setInterpolation(false);
  127. }
  128. if (id == "bounding_box") {
  129. _bb = !_bb;
  130. updateText("bounding_box", _bb ? "bounding box: on" : "bounding box: off");
  131. }
  132. if (id == "overdraw")
  133. {
  134. movie->setShowOverdraw(!movie->getShowOverdraw());
  135. updateText("overdraw", !movie->getShowOverdraw() ? "show overdraw" : "hide overdraw");
  136. }
  137. if (id == "show_all")
  138. {
  139. _show_all = !_show_all;
  140. updateText("show_all", _show_all ? "show all: on" : "show all: off");
  141. }
  142. }
  143. void update(const UpdateState& us)
  144. {
  145. Test::update(us);
  146. if (!movie)
  147. return;
  148. char str[255];
  149. safe_sprintf(str, "wireframe: %d / %d", movie->getWireframeMode(), movie->getLastBatches());
  150. updateText("wireframe", str);
  151. if (key::wasPressed(SDL_SCANCODE_SPACE))
  152. {
  153. if (!movie->isPaused())
  154. {
  155. movie->pause();
  156. notify("pause");
  157. }
  158. else
  159. {
  160. movie->resume();
  161. notify("resume");
  162. }
  163. }
  164. float sx, sy;
  165. float scale = 1.0f;
  166. if (_bb)
  167. {
  168. RectF rc;
  169. if (movie->computeAllTimelineBounds(rc))
  170. {
  171. Vector2 off = movie->getSize() * 0.5f;
  172. Matrix m = movie->getParent()->computeGlobalTransform().toMatrix();
  173. Vector4 p0 = m.transformVec4(Vector4(rc.getLeft() - off.x, rc.getTop() - off.y, 0.0f, 1.0f));
  174. Vector4 p1 = m.transformVec4(Vector4(rc.getRight() - off.x, rc.getBottom() - off.y, 0.0f, 1.0f));
  175. sx = std::max(fabs(p0.x), fabs(p1.x))*1.1f;
  176. sy = std::max(fabs(p0.y), fabs(p1.y))*1.1f;
  177. sx = (0.5f * getStage()->getWidth()) / sx;
  178. sy = (0.5f * getStage()->getHeight()) / sy;
  179. scale = std::min(sx, sy);
  180. if (_show_all)
  181. {
  182. scale = std::min(scale, _allscale);
  183. }
  184. else
  185. {
  186. sx = getStage()->getWidth() / movie->getWidth();
  187. sy = getStage()->getHeight() / movie->getHeight();
  188. scale = std::min(scale, std::min(sx, sy));
  189. }
  190. movie->setScale(std::min(scale, 1.0f));
  191. movie->setPosition(getStage()->getSize() / 2 - movie->getScaledSize() / 2);
  192. m = movie->computeGlobalTransform().toMatrix();
  193. Vector4 pp0 = m.transformVec4(Vector4(rc.getLeft(), rc.getTop(), 0.0f, 1.0f));
  194. Vector4 pp1 = m.transformVec4(Vector4(rc.getRight(), rc.getBottom(), 0.0f, 1.0f));
  195. _cr2->setPosition(Vector2(pp0.x - 2.0f, pp0.y - 2.0f));
  196. _cr2->setSize(Vector2(pp1.x - pp0.x + 4.0f, pp1.y - pp0.y + 4.0f));
  197. _cr2->setVisible(true);
  198. _cr->setPosition(Vector2(pp0.x, pp0.y));
  199. _cr->setSize(Vector2(pp1.x - pp0.x, pp1.y - pp0.y));
  200. _cr->setVisible(true);
  201. }
  202. }
  203. else
  204. {
  205. if (_show_all)
  206. {
  207. movie->setScale(_allscale);
  208. }
  209. else
  210. {
  211. sx = getStage()->getWidth() / movie->getWidth();
  212. sy = getStage()->getHeight() / movie->getHeight();
  213. movie->setScale(std::min(1.0f, std::min(sx, sy)) * 0.9f);
  214. }
  215. movie->setPosition(getStage()->getSize() / 2 - movie->getScaledSize() / 2);
  216. _cr->setVisible(false);
  217. _cr2->setVisible(false);
  218. }
  219. }
  220. void set(const string &id)
  221. {
  222. if (movie)
  223. movie->detach();
  224. _cr2 = new ColorRectSprite;
  225. _cr2->setColor(Color(255, 0, 0, 255));
  226. _cr2->attachTo(this);
  227. _cr2->setPriority(-3);
  228. _cr2->setVisible(false);
  229. _cr = new ColorRectSprite;
  230. _cr->setColor(Color(0,0,0,255));
  231. _cr->attachTo(this);
  232. _cr->setPriority(-2);
  233. _cr->setVisible(false);
  234. movie = AEMovieWork::createWork(movieRes, id);
  235. if (!movie)
  236. return;
  237. movie->attachTo(this);
  238. movie->setPriority(-1);
  239. movie->setAnchor(0.0f, 0.0f);
  240. movie->play(true);
  241. float sx, sy, scale;
  242. RectF rc;
  243. if (movie->calcMinMaxForAllFrames(rc))
  244. {
  245. Vector2 off = movie->getSize() * 0.5f;
  246. Matrix m = movie->getParent()->computeGlobalTransform().toMatrix();
  247. Vector4 p0 = m.transformVec4(Vector4(rc.getLeft() - off.x, rc.getTop() - off.y, 0.0f, 1.0f));
  248. Vector4 p1 = m.transformVec4(Vector4(rc.getRight() - off.x, rc.getBottom() - off.y, 0.0f, 1.0f));
  249. sx = std::max(fabs(p0.x), fabs(p1.x))*1.1f;
  250. sy = std::max(fabs(p0.y), fabs(p1.y))*1.1f;
  251. sx = (0.5f * getStage()->getWidth()) / sx;
  252. sy = (0.5f * getStage()->getHeight()) / sy;
  253. scale = std::min(sx, sy);
  254. sx = getStage()->getWidth() / movie->getWidth();
  255. sy = getStage()->getHeight() / movie->getHeight();
  256. scale = std::min(scale, std::min(sx, sy));
  257. _allscale = std::min(scale, 1.0f);
  258. }
  259. if (_show_all)
  260. {
  261. movie->setScale(_allscale);
  262. }
  263. else
  264. {
  265. sx = getStage()->getWidth() / movie->getWidth();
  266. sy = getStage()->getHeight() / movie->getHeight();
  267. movie->setScale(std::min(1.0f, std::min(sx, sy)) * 0.9f);
  268. }
  269. movie->setPosition(getStage()->getSize() / 2 - movie->getScaledSize() / 2);
  270. movie->addEventListener(Event::COMPLETE, [=](Event*) {
  271. notify("COMPLETE");
  272. });
  273. int events = ae_get_movie_composition_data_event_count(movie->getAEData());
  274. for (int i = 0; i < events; ++i)
  275. {
  276. const char *name = ae_get_movie_composition_data_event_name(movie->getAEData(), i);
  277. addButton("event:" + string(name), name);
  278. }
  279. }
  280. };
  281. bool Preview::_bb = false;
  282. bool Preview::_show_all = false;
  283. spPreview Preview::instance;
  284. class TestActor: public Test
  285. {
  286. public:
  287. TestActor()
  288. {
  289. _x = 90;//getStage()->getWidth()/2.0f;
  290. _y = 80;
  291. if (!movieRes.movieData)
  292. return;
  293. int num = ae_get_movie_composition_data_count(movieRes.movieData);
  294. for (int i = 0; i < num; ++i)
  295. {
  296. const aeMovieCompositionData *comp = ae_get_movie_composition_data_by_index(movieRes.movieData, i);
  297. bool main = ae_is_movie_composition_data_master(comp);
  298. _color = main ? Color(Color::White) : Color(Color::Gray);
  299. _txtColor = main ? Color(Color::Blue) : Color(Color::Gray);
  300. const char *name = ae_get_movie_composition_data_name(comp);
  301. addButton(name, name);
  302. }
  303. if (!aeCurrent.empty())
  304. {
  305. getStage()->addTween(TweenDummy(), 1)->addDoneCallback([=](Event*) {
  306. clicked(aeCurrent);
  307. });
  308. }
  309. }
  310. void showTest(spActor actor)
  311. {
  312. spStage stage = getStage();
  313. #if MULTIWINDOW
  314. stage = stage2;
  315. #else
  316. setVisible(false);
  317. #endif
  318. stage->addChild(actor);
  319. }
  320. void clicked(string id)
  321. {
  322. Preview::instance = new Preview(id);
  323. setVisible(false);
  324. Preview::instance->attachTo(getStage());
  325. }
  326. };
  327. void example_preinit()
  328. {
  329. }
  330. oxygine::file::ZipFileSystem zfs;
  331. void example_init()
  332. {
  333. Test::init();
  334. key::init();
  335. AEMovieResource::initLibrary();
  336. bool isZip = path::extractFileExt(aeProject) == "zip";
  337. if (isZip)
  338. {
  339. if (file::exists(aeProject))
  340. zfs.add(aeProject.c_str());
  341. file::fs().mount(&zfs);
  342. movieRes.load("project", ep_show_warning);
  343. }
  344. else
  345. {
  346. movieRes.load(aeProject, ep_show_warning);
  347. }
  348. Test::instance = new TestActor;
  349. getStage()->addChild(Test::instance);
  350. //Preview::instance = new Preview;
  351. //Initialize http requests
  352. HttpRequestTask::init();
  353. #if MULTIWINDOW
  354. SDL_Window* window2 = SDL_CreateWindow("Second Oxygine Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, getStage()->getWidth(), getStage()->getHeight(), SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
  355. stage2 = new Stage(false);
  356. stage2->setSize(getStage()->getSize());
  357. stage2->associateWithWindow(window2);
  358. #endif
  359. }
  360. void example_update()
  361. {
  362. AEMovieResource::updateLibrary();
  363. #if MULTIWINDOW
  364. stage2->update();
  365. SDL_Window* wnd = stage2->getAssociatedWindow();
  366. if (core::beginRendering(wnd))
  367. {
  368. Color clearColor(32, 32, 32, 255);
  369. Rect viewport(Point(0, 0), core::getDisplaySize());
  370. //render all actors. Actor::render would be called also for all children
  371. stage2->render(clearColor, viewport);
  372. core::swapDisplayBuffers(wnd);
  373. }
  374. #endif
  375. }
  376. void example_destroy()
  377. {
  378. key::release();
  379. if (Preview::instance)
  380. Preview::instance->detach();
  381. Preview::instance = 0;
  382. movieRes.clear();
  383. Test::free();
  384. HttpRequestTask::release();
  385. AEMovieResource::freeLibrary();
  386. }