2
0

PolycodeFrame.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /*
  2. Copyright (C) 2012 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolycodeFrame.h"
  20. UIColorPicker *globalColorPicker;
  21. PolycodeFrame *globalFrame;
  22. extern UIGlobalMenu *globalMenu;
  23. EditPoint::EditPoint(BezierPoint *point, unsigned int type) : ScreenEntity() {
  24. this->point = point;
  25. this->type = type;
  26. processInputEvents = true;
  27. draggingPoint = NULL;
  28. dragging = false;
  29. controlHandle1 = new ScreenImage("Images/bezier_handle.png");
  30. controlHandle1->setPositionMode(ScreenEntity::POSITION_CENTER);
  31. controlHandle1->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  32. controlHandle1->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  33. controlHandle1->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  34. controlHandle1->processInputEvents = true;
  35. controlHandle1->setWidth(30);
  36. controlHandle1->setHeight(30);
  37. addChild(controlHandle1);
  38. controlHandle2 = new ScreenImage("Images/bezier_handle.png");
  39. controlHandle2->setPositionMode(ScreenEntity::POSITION_CENTER);
  40. controlHandle2->processInputEvents = true;
  41. controlHandle2->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  42. controlHandle2->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  43. controlHandle2->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  44. controlHandle2->setWidth(30);
  45. controlHandle2->setHeight(30);
  46. addChild(controlHandle2);
  47. pointHandle = new ScreenImage("Images/bezier_point.png");
  48. pointHandle->processInputEvents = true;
  49. pointHandle->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  50. pointHandle->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  51. pointHandle->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);
  52. pointHandle->setPositionMode(ScreenEntity::POSITION_CENTER);
  53. pointHandle->setWidth(30);
  54. pointHandle->setHeight(30);
  55. if(type == TYPE_START_POINT) {
  56. controlHandle1->visible = false;
  57. controlHandle1->enabled = false;
  58. connectorLine1 = NULL;
  59. } else {
  60. connectorLine1 = new ScreenLine(pointHandle, controlHandle1);
  61. addChild(connectorLine1);
  62. connectorLine1->setColorInt(39, 212, 255, 128);
  63. connectorLine1->setLineWidth(2.0);
  64. connectorLine1->lineSmooth = true;
  65. }
  66. if(type == TYPE_END_POINT) {
  67. controlHandle2->visible = false;
  68. controlHandle2->enabled = false;
  69. connectorLine2 = NULL;
  70. } else {
  71. connectorLine2 = new ScreenLine(pointHandle, controlHandle2);
  72. addChild(connectorLine2);
  73. connectorLine2->setColorInt(39, 212, 255, 128);
  74. connectorLine2->setLineWidth(2.0);
  75. connectorLine2->lineSmooth = true;
  76. }
  77. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  78. addChild(pointHandle);
  79. updatePosition();
  80. }
  81. void EditPoint::setMode(unsigned int mode) {
  82. this->mode = mode;
  83. }
  84. void EditPoint::updateCurvePoint() {
  85. point->p1.x = controlHandle1->getPosition2D().x/610;
  86. point->p1.y = controlHandle1->getPosition2D().y/-254;
  87. point->p2.x = pointHandle->getPosition2D().x/610;
  88. point->p2.y = pointHandle->getPosition2D().y/-254;
  89. point->p3.x = controlHandle2->getPosition2D().x/610;
  90. point->p3.y = controlHandle2->getPosition2D().y/-254;
  91. }
  92. void EditPoint::handleEvent(Event *event) {
  93. if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  94. switch(event->getEventCode()) {
  95. case InputEvent::EVENT_MOUSEMOVE:
  96. if(dragging) {
  97. if(draggingPoint) {
  98. Vector2 newPosition = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  99. Vector2 translateAmt = Vector2(basePosition.x - newPosition.x, basePosition.y - newPosition.y);
  100. if(type != TYPE_POINT && draggingPoint == pointHandle) {
  101. // don't let drag start and end control points
  102. translateAmt.x = 0.0;
  103. }
  104. draggingPoint->setPosition(basePointPosition.x - translateAmt.x, basePointPosition.y - translateAmt.y);
  105. if(draggingPoint == pointHandle) {
  106. controlHandle1->setPosition(baseControl1.x - translateAmt.x, baseControl1.y - translateAmt.y);
  107. controlHandle2->setPosition(baseControl2.x - translateAmt.x, baseControl2.y - translateAmt.y);
  108. }
  109. limitPoint(pointHandle);
  110. limitPoint(controlHandle1);
  111. limitPoint(controlHandle2);
  112. updateCurvePoint();
  113. dispatchEvent(new Event(), Event::CHANGE_EVENT);
  114. }
  115. }
  116. break;
  117. }
  118. }
  119. if(event->getDispatcher() == pointHandle || event->getDispatcher() == controlHandle1 || event->getDispatcher() == controlHandle2) {
  120. switch(event->getEventCode()) {
  121. case InputEvent::EVENT_MOUSEDOWN:
  122. if(mode == CurveEditor::MODE_SELECT) {
  123. draggingPoint = (ScreenImage*)event->getDispatcher();
  124. dragging = true;
  125. basePosition = CoreServices::getInstance()->getCore()->getInput()->getMousePosition();
  126. basePointPosition = draggingPoint->getPosition2D();
  127. baseControl1 = controlHandle1->getPosition2D();
  128. baseControl2 = controlHandle2->getPosition2D();
  129. }
  130. if(mode == CurveEditor::MODE_REMOVE) {
  131. if(type == TYPE_POINT) {
  132. dispatchEvent(new Event(), Event::CANCEL_EVENT);
  133. }
  134. }
  135. break;
  136. case InputEvent::EVENT_MOUSEUP:
  137. case InputEvent::EVENT_MOUSEUP_OUTSIDE:
  138. dragging = false;
  139. draggingPoint = NULL;
  140. break;
  141. }
  142. }
  143. }
  144. void EditPoint::limitPoint(ScreenImage *point) {
  145. if(point->position.x < 0.0)
  146. point->position.x = 0.0;
  147. if(point->position.x > 610.0)
  148. point->position.x = 610.0;
  149. if(point->position.y > 0.0)
  150. point->position.y = 0.0;
  151. if(point->position.y < -254.0)
  152. point->position.y = -254.0;
  153. }
  154. void EditPoint::updatePosition() {
  155. pointHandle->setPosition(610.0*point->p2.x, -254*point->p2.y, 0.0);
  156. controlHandle1->setPosition(610.0*point->p1.x, -254*point->p1.y, 0.0);
  157. controlHandle2->setPosition(610.0*point->p3.x, -254*point->p3.y, 0.0);
  158. }
  159. EditPoint::~EditPoint() {
  160. }
  161. EditCurve::EditCurve(BezierCurve *targetCurve, Color curveColor) : UIElement() {
  162. this->targetCurve = targetCurve;
  163. poly = new Polycode::Polygon();
  164. for(int i=0; i < CURVE_SIZE; i++) {
  165. poly->addVertex(0.0, 0.0, 0.0);
  166. }
  167. visMesh = new ScreenMesh(Mesh::LINE_STRIP_MESH);
  168. visMesh->getMesh()->addPolygon(poly);
  169. visMesh->lineSmooth = true;
  170. visMesh->lineWidth = 2.0;
  171. addChild(visMesh);
  172. visMesh->setPosition(0, 254);
  173. visMesh->color = curveColor;
  174. pointsBase = new UIElement();
  175. addChild(pointsBase);
  176. pointToRemove = NULL;
  177. updateCurve();
  178. updatePoints();
  179. Deactivate();
  180. }
  181. void EditCurve::updatePoints() {
  182. for(int i=0; i < points.size(); i++) {
  183. pointsBase->removeChild(points[i]);
  184. //delete points[i];
  185. }
  186. points.clear();
  187. for(int i=0; i < targetCurve->getNumControlPoints(); i++) {
  188. unsigned int type = EditPoint::TYPE_POINT;
  189. if(i == 0)
  190. type = EditPoint::TYPE_START_POINT;
  191. if(i == targetCurve->getNumControlPoints()-1)
  192. type = EditPoint::TYPE_END_POINT;
  193. EditPoint *point = new EditPoint(targetCurve->getControlPoint(i), type);
  194. point->setMode(mode);
  195. point->addEventListener(this, Event::CHANGE_EVENT);
  196. point->addEventListener(this, Event::CANCEL_EVENT);
  197. pointsBase->addChild(point);
  198. points.push_back(point);
  199. point->setPosition(0, 254);
  200. }
  201. }
  202. void EditCurve::setMode(unsigned int mode) {
  203. this->mode = mode;
  204. for(int i=0; i < points.size(); i++) {
  205. points[i]->setMode(mode);
  206. }
  207. }
  208. void EditCurve::Activate() {
  209. pointsBase->visible = true;
  210. pointsBase->enabled = true;
  211. visMesh->color.a = 0.7;
  212. }
  213. void EditCurve::Deactivate() {
  214. pointsBase->visible = false;
  215. pointsBase->enabled = false;
  216. visMesh->color.a = 0.4;
  217. }
  218. void EditCurve::Update() {
  219. if(pointToRemove) {
  220. targetCurve->removePoint(pointToRemove->point);
  221. updatePoints();
  222. updateCurve();
  223. pointToRemove = NULL;
  224. }
  225. }
  226. void EditCurve::handleEvent(Event *event) {
  227. if(event->getEventCode() == Event::CHANGE_EVENT) {
  228. updateCurve();
  229. }
  230. if(event->getEventCode() == Event::CANCEL_EVENT) {
  231. for(int i=0; i < points.size(); i++) {
  232. if(event->getDispatcher() == points[i]) {
  233. pointToRemove = points[i];
  234. break;
  235. }
  236. }
  237. }
  238. }
  239. void EditCurve::updateCurve() {
  240. targetCurve->recalculateDistances();
  241. targetCurve->rebuildBuffers();
  242. Number interval = 610.0/CURVE_SIZE;
  243. Number normInterval = 1.0/CURVE_SIZE;
  244. interval += interval/CURVE_SIZE;
  245. normInterval += normInterval/CURVE_SIZE;
  246. for(int i=0; i < CURVE_SIZE; i++) {
  247. poly->getVertex(i)->set(targetCurve->getPointAt(normInterval * i).x * 610, targetCurve->getPointAt(normInterval * i).y * -254.0, 0.0);
  248. }
  249. visMesh->getMesh()->arrayDirtyMap[RenderDataArray::VERTEX_DATA_ARRAY] = true;
  250. }
  251. EditCurve::~EditCurve() {
  252. }
  253. CurveEditor::CurveEditor() : UIWindow("", 750, 300) {
  254. closeOnEscape = true;
  255. bg = new ScreenImage("Images/curve_editor_bg.png");
  256. addChild(bg);
  257. bg->setPosition(160, 63);
  258. bg->processInputEvents = true;
  259. bg->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
  260. selectorImage = new ScreenImage("Images/ScreenEditor/selector.png");
  261. selectorImage->setColor(0.0, 0.0, 0.0, 0.3);
  262. addChild(selectorImage);
  263. selectButton = new UIImageButton("Images/ScreenEditor/arrow.png");
  264. addChild(selectButton);
  265. selectButton->addEventListener(this, UIEvent::CLICK_EVENT);
  266. selectButton->setPosition(170, 33);
  267. addButton = new UIImageButton("Images/arrow_add.png");
  268. addChild(addButton);
  269. addButton->addEventListener(this, UIEvent::CLICK_EVENT);
  270. addButton->setPosition(170 + 32, 33);
  271. removeButton = new UIImageButton("Images/arrow_remove.png");
  272. addChild(removeButton);
  273. removeButton->addEventListener(this, UIEvent::CLICK_EVENT);
  274. removeButton->setPosition(170 + 64, 33);
  275. selectorImage->setPosition(selectButton->getPosition().x - 4, selectButton->getPosition().y - 4);
  276. selectedCurve = NULL;
  277. setMode(0);
  278. treeContainer = new UITreeContainer("boxIcon.png", L"Curves", 145, 280);
  279. treeContainer->getRootNode()->toggleCollapsed();
  280. treeContainer->getRootNode()->addEventListener(this, UITreeEvent::SELECTED_EVENT);
  281. treeContainer->setPosition(12, 33);
  282. treeContainer->getRootNode()->setUserData(NULL);
  283. addChild(treeContainer);
  284. }
  285. void CurveEditor::onClose() {
  286. visible = false;
  287. enabled = false;
  288. }
  289. void CurveEditor::clearCurves() {
  290. selectedCurve = NULL;
  291. treeContainer->getRootNode()->clearTree();
  292. for(int i=0; i < curves.size(); i++) {
  293. removeChild(curves[i]);
  294. delete curves[i];
  295. }
  296. curves.clear();
  297. }
  298. void CurveEditor::addCurve(String name, BezierCurve *curve, Color curveColor) {
  299. UITree *newNode = treeContainer->getRootNode()->addTreeChild("Images/curve_icon.png", name);
  300. EditCurve *editCurve = new EditCurve(curve, curveColor);
  301. addChild(editCurve);
  302. editCurve->setPosition(160, 63);
  303. curves.push_back(editCurve);
  304. newNode->setUserData((void*) editCurve);
  305. }
  306. void CurveEditor::setMode(unsigned int mode) {
  307. this->mode = mode;
  308. if(selectedCurve) {
  309. selectedCurve->setMode(mode);
  310. }
  311. }
  312. void CurveEditor::handleEvent(Event *event) {
  313. if(mode == MODE_ADD) {
  314. if(event->getDispatcher() == bg) {
  315. if(event->getEventCode() == InputEvent::EVENT_MOUSEDOWN) {
  316. InputEvent *inputEvent = (InputEvent*)event;
  317. if(selectedCurve) {
  318. Vector2 pos = inputEvent->mousePosition;
  319. pos.x = pos.x/610.0;
  320. pos.y = 1.0-(pos.y/254.0);
  321. BezierCurve *targetCurve = selectedCurve->targetCurve;
  322. bool done = false;
  323. for(int i=0; i < targetCurve->getNumControlPoints(); i++) {
  324. if(pos.x < targetCurve->getControlPoint(i)->p2.x && !done) {
  325. targetCurve->insertPoint = targetCurve->getControlPoint(i);
  326. done = true;
  327. }
  328. }
  329. targetCurve->addControlPoint2dWithHandles(pos.x-0.1, pos.y, pos.x, pos.y, pos.x + 0.1, pos.y);
  330. selectedCurve->updatePoints();
  331. selectedCurve->updateCurve();
  332. }
  333. }
  334. }
  335. }
  336. if(event->getDispatcher() == selectButton) {
  337. selectorImage->setPosition(selectButton->getPosition().x - 4, selectButton->getPosition().y - 4);
  338. setMode(0);
  339. }
  340. if(event->getDispatcher() == addButton) {
  341. selectorImage->setPosition(addButton->getPosition().x - 4, addButton->getPosition().y - 4);
  342. setMode(1);
  343. }
  344. if(event->getDispatcher() == removeButton) {
  345. selectorImage->setPosition(removeButton->getPosition().x - 4, removeButton->getPosition().y - 4);
  346. setMode(2);
  347. }
  348. if(event->getDispatcher() == treeContainer->getRootNode()) {
  349. if(event->getEventCode() == UITreeEvent::SELECTED_EVENT){
  350. EditCurve *curve = (EditCurve *)treeContainer->getRootNode()->getSelectedNode()->getUserData();
  351. if(selectedCurve) {
  352. selectedCurve->Deactivate();
  353. }
  354. selectedCurve = curve;
  355. if(curve) {
  356. curve->Activate();
  357. curve->setMode(mode);
  358. }
  359. }
  360. }
  361. UIWindow::handleEvent(event);
  362. }
  363. CurveEditor::~CurveEditor() {
  364. }
  365. EditorHolder::EditorHolder() : UIElement() {
  366. currentEditor = NULL;
  367. }
  368. EditorHolder::~EditorHolder() {
  369. }
  370. void EditorHolder::Resize(Number width, Number height) {
  371. if(currentEditor) {
  372. currentEditor->Resize(width, height);
  373. }
  374. }
  375. PolycodeFrame::PolycodeFrame() : ScreenEntity() {
  376. globalFrame = this;
  377. processInputEvents = true;
  378. willHideModal = false;
  379. modalChild = NULL;
  380. welcomeEntity = new ScreenEntity();
  381. welcomeEntity->processInputEvents = true;
  382. addChild(welcomeEntity);
  383. welcomeImage = new ScreenImage("Images/welcome.png");
  384. welcomeEntity->addChild(welcomeImage);
  385. welcomeEntity->snapToPixels = true;
  386. newProjectButton = new UIButton("Create A New Project!", 220);
  387. newProjectButton->setPosition(230,80);
  388. newProjectButton->addEventListener(this, UIEvent::CLICK_EVENT);
  389. examplesButton = new UIButton("Browse Example Projects!", 220);
  390. examplesButton->setPosition(460,80);
  391. examplesButton->addEventListener(this, UIEvent::CLICK_EVENT);
  392. welcomeEntity->addChild(newProjectButton);
  393. welcomeEntity->addChild(examplesButton);
  394. mainSizer = new UIHSizer(100,100,200,true);
  395. mainSizer->setPosition(0, 45);
  396. addChild(mainSizer);
  397. consoleSizer = new UIVSizer(100,100,200, false);
  398. mainSizer->addRightChild(consoleSizer);
  399. projectBrowser = new PolycodeProjectBrowser();
  400. mainSizer->addLeftChild(projectBrowser);
  401. editorHolder = new EditorHolder();
  402. consoleSizer->addTopChild(editorHolder);
  403. console = new PolycodeConsole();
  404. consoleSizer->addBottomChild(console);
  405. projectBrowser->treeContainer->getRootNode()->addEventListener(this, UITreeEvent::DRAG_START_EVENT);
  406. topBarBg = new ScreenShape(ScreenShape::SHAPE_RECT, 2,2);
  407. topBarBg->setColorInt(21, 18, 17, 255);
  408. topBarBg->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  409. addChild(topBarBg);
  410. logo = new ScreenImage("Images/barlogo.png");
  411. addChild(logo);
  412. playButton = new UIImageButton("Images/play_button.png");
  413. addChild(playButton);
  414. playButton->setPosition(10,4);
  415. stopButton = new UIImageButton("Images/stop_button.png");
  416. addChild(stopButton);
  417. stopButton->setPosition(10,4);
  418. resizer = new ScreenImage("Images/corner_resize.png");
  419. addChild(resizer);
  420. resizer->setColor(0,0,0,0.4);
  421. modalBlocker = new ScreenShape(ScreenShape::SHAPE_RECT, 10,10);
  422. modalBlocker->setColor(0,0,0,0.4);
  423. modalBlocker->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  424. modalBlocker->enabled = false;
  425. modalBlocker->blockMouseInput = true;
  426. modalBlocker->processInputEvents = true;
  427. addChild(modalBlocker);
  428. assetBrowser = new AssetBrowser();
  429. assetBrowser->visible = false;
  430. newProjectWindow = new NewProjectWindow();
  431. newProjectWindow->visible = false;
  432. exampleBrowserWindow = new ExampleBrowserWindow();
  433. exampleBrowserWindow->visible = false;
  434. newFileWindow = new NewFileWindow();
  435. newFileWindow->visible = false;
  436. exportProjectWindow = new ExportProjectWindow();
  437. exportProjectWindow->visible = false;
  438. textInputPopup = new TextInputPopup();
  439. textInputPopup->visible = false;
  440. yesNoPopup = new YesNoPopup();
  441. yesNoPopup->visible = false;
  442. isDragging = false;
  443. dragLabel = new ScreenLabel("NONE", 11, "sans");
  444. dragLabel->setPosition(0,-15);
  445. dragEntity = new ScreenEntity();
  446. dragEntity->addChild(dragLabel);
  447. addChild(dragEntity);
  448. dragEntity->visible = false;
  449. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  450. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  451. curveEditor = new CurveEditor();
  452. addChild(curveEditor);
  453. curveEditor->setPosition(200,100);
  454. curveEditor->visible = false;
  455. curveEditor->enabled = false;
  456. globalColorPicker = new UIColorPicker();
  457. globalColorPicker->setPosition(300,300);
  458. addChild(globalColorPicker);
  459. modalRoot = new UIElement();
  460. addChild(modalRoot);
  461. fileDialogBlocker = new ScreenShape(ScreenShape::SHAPE_RECT, 100, 100);
  462. fileDialogBlocker->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  463. addChild(fileDialogBlocker);
  464. fileDialogBlocker->setColor(0.0, 0.0, 0.0, 0.5);
  465. fileDialogBlocker->processInputEvents = true;
  466. fileDialogBlocker->blockMouseInput = true;
  467. fileDialogBlocker->visible = false;
  468. fileDialogBlocker->enabled = false;
  469. fileBrowserRoot = new UIElement();
  470. addChild(fileBrowserRoot);
  471. fileDialog = NULL;
  472. }
  473. void PolycodeFrame::showFileBrowser(String baseDir, bool foldersOnly, std::vector<String> extensions, bool allowMultiple) {
  474. if(fileDialog)
  475. delete fileDialog;
  476. fileDialog = new UIFileDialog(baseDir, foldersOnly, extensions, allowMultiple);
  477. fileDialog->addEventListener(this, UIEvent::CANCEL_EVENT);
  478. fileDialog->addEventListener(this, UIEvent::OK_EVENT);
  479. fileBrowserRoot->addChild(fileDialog);
  480. fileDialog->setPosition(100,100);
  481. fileDialogBlocker->visible = true;
  482. fileDialogBlocker->enabled = true;
  483. }
  484. void PolycodeFrame::showCurveEditor() {
  485. curveEditor->visible = true;
  486. curveEditor->enabled = true;
  487. }
  488. void PolycodeFrame::showModal(UIWindow *modalChild) {
  489. modalBlocker->enabled = true;
  490. this->modalChild = modalChild;
  491. modalRoot->addChild(modalChild);
  492. modalChild->showWindow();
  493. modalChild->addEventListener(this, UIEvent::CLOSE_EVENT);
  494. Resize(frameSizeX, frameSizeY);
  495. }
  496. PolycodeProjectBrowser *PolycodeFrame::getProjectBrowser() {
  497. return projectBrowser;
  498. }
  499. void PolycodeFrame::addEditor(PolycodeEditor *editor) {
  500. editors.push_back(editor);
  501. editorHolder->addChild(editor);
  502. editor->enabled = false;
  503. }
  504. void PolycodeFrame::showEditor(PolycodeEditor *editor) {
  505. if(editorHolder->currentEditor) {
  506. editorHolder->currentEditor->enabled = false;
  507. editorHolder->currentEditor = NULL;
  508. }
  509. editorHolder->currentEditor = editor;
  510. editorHolder->currentEditor->enabled = true;
  511. editorHolder->currentEditor->Activate();
  512. Resize(frameSizeX, frameSizeY);
  513. }
  514. void PolycodeFrame::hideModal() {
  515. if(modalChild) {
  516. modalRoot->removeChild(modalChild);
  517. modalChild->removeEventListener(this, UIEvent::CLOSE_EVENT);
  518. modalChild->hideWindow();
  519. modalChild = NULL;
  520. }
  521. modalBlocker->enabled = false;
  522. }
  523. void PolycodeFrame::Update() {
  524. if(willHideModal) {
  525. hideModal();
  526. willHideModal = false;
  527. }
  528. }
  529. void PolycodeFrame::showAssetBrowser(std::vector<String> extensions) {
  530. if(!projectManager->getActiveProject()) {
  531. return;
  532. }
  533. assetBrowser->setProject(projectManager->getActiveProject());
  534. assetBrowser->setExtensions(extensions);
  535. showModal(assetBrowser);
  536. }
  537. void PolycodeFrame::handleEvent(Event *event) {
  538. if(event->getDispatcher() == fileDialog && event->getEventType() == "UIEvent") {
  539. fileBrowserRoot->removeChild(fileDialog);
  540. fileDialogBlocker->visible = false;
  541. fileDialogBlocker->enabled = false;
  542. }
  543. if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  544. switch(event->getEventCode()) {
  545. case InputEvent::EVENT_MOUSEUP:
  546. if(isDragging) {
  547. if(editorHolder->currentEditor) {
  548. InputEvent *inputEvent = (InputEvent*) event;
  549. Number posX = inputEvent->mousePosition.x;
  550. Number posY = inputEvent->mousePosition.y;
  551. editorHolder->currentEditor->handleDroppedFile(draggedFile, posX, posY);
  552. }
  553. }
  554. isDragging = false;
  555. dragEntity->visible = false;
  556. break;
  557. case InputEvent::EVENT_MOUSEMOVE:
  558. if(isDragging) {
  559. dragEntity->setPosition(((InputEvent*)event)->mousePosition);
  560. }
  561. break;
  562. }
  563. }
  564. if(event->getDispatcher() == projectBrowser->treeContainer->getRootNode()) {
  565. switch (event->getEventCode()) {
  566. case UITreeEvent::DRAG_START_EVENT:
  567. {
  568. UITreeEvent *treeEvent = (UITreeEvent*) event;
  569. BrowserUserData *data = (BrowserUserData*)treeEvent->selection->getUserData();
  570. draggedFile = data->fileEntry;
  571. dragLabel->setText(data->fileEntry.name);
  572. dragEntity->visible = true;
  573. isDragging = true;
  574. // printf("START DRAG: %s\n", data->fileEntry.name.c_str());
  575. }
  576. break;
  577. }
  578. }
  579. if(event->getDispatcher() == modalChild) {
  580. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLOSE_EVENT) {
  581. willHideModal = true;
  582. }
  583. } else {
  584. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT && event->getDispatcher() == newProjectButton) {
  585. newProjectWindow->ResetForm();
  586. showModal(newProjectWindow);
  587. }
  588. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT && event->getDispatcher() == examplesButton) {
  589. newProjectWindow->ResetForm();
  590. showModal(exampleBrowserWindow);
  591. }
  592. }
  593. }
  594. void PolycodeFrame::Resize(int x, int y) {
  595. frameSizeX = x;
  596. frameSizeY = y;
  597. welcomeEntity->setPosition((x-welcomeImage->getWidth()) / 2,
  598. (y-welcomeImage->getHeight()) / 2);
  599. topBarBg->setShapeSize(x, 45);
  600. logo->setPosition(x-logo->getWidth()-2, 2);
  601. resizer->setPosition(x-resizer->getWidth()-1, y-resizer->getHeight()-1);
  602. mainSizer->Resize(x,y-45);
  603. modalBlocker->setShapeSize(x, y);
  604. fileDialogBlocker->setShapeSize(x, y);
  605. if(this->modalChild) {
  606. modalChild->setPosition((x-modalChild->getWidth())/2.0f, (y-modalChild->getHeight())/2.0f);
  607. }
  608. }
  609. PolycodeFrame::~PolycodeFrame() {
  610. }