2
0

PolycodeFrame.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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. topBarBg->processInputEvents = true;
  410. topBarBg->blockMouseInput = true;
  411. addChild(topBarBg);
  412. logo = new ScreenImage("Images/barlogo.png");
  413. addChild(logo);
  414. playButton = new UIImageButton("Images/play_button.png");
  415. addChild(playButton);
  416. playButton->setPosition(10,4);
  417. stopButton = new UIImageButton("Images/stop_button.png");
  418. addChild(stopButton);
  419. stopButton->setPosition(10,4);
  420. currentProjectTitle = new ScreenLabel("", 32, "section");
  421. addChild(currentProjectTitle);
  422. currentProjectTitle->color.a = 0.4;
  423. currentProjectTitle->setPosition(70, 0);
  424. currentFileSelector = new UIComboBox(globalMenu, 300);
  425. currentFileSelector->addEventListener(this, UIEvent::CHANGE_EVENT);
  426. addChild(currentFileSelector);
  427. resizer = new ScreenImage("Images/corner_resize.png");
  428. addChild(resizer);
  429. resizer->setColor(0,0,0,0.4);
  430. modalBlocker = new ScreenShape(ScreenShape::SHAPE_RECT, 10,10);
  431. modalBlocker->setColor(0,0,0,0.4);
  432. modalBlocker->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  433. modalBlocker->enabled = false;
  434. modalBlocker->blockMouseInput = true;
  435. modalBlocker->processInputEvents = true;
  436. addChild(modalBlocker);
  437. assetBrowser = new AssetBrowser();
  438. assetBrowser->visible = false;
  439. newProjectWindow = new NewProjectWindow();
  440. newProjectWindow->visible = false;
  441. exampleBrowserWindow = new ExampleBrowserWindow();
  442. exampleBrowserWindow->visible = false;
  443. newFileWindow = new NewFileWindow();
  444. newFileWindow->visible = false;
  445. exportProjectWindow = new ExportProjectWindow();
  446. exportProjectWindow->visible = false;
  447. textInputPopup = new TextInputPopup();
  448. textInputPopup->visible = false;
  449. yesNoPopup = new YesNoPopup();
  450. yesNoPopup->visible = false;
  451. aboutWindow = new UIWindow("", 800, 440);
  452. aboutWindow->closeOnEscape = true;
  453. ScreenImage *aboutImage = new ScreenImage("Images/about.png");
  454. aboutWindow->addChild(aboutImage);
  455. aboutImage->setPosition(20, 40);
  456. aboutWindow->visible = false;
  457. aboutOKButton = new UIButton("OK", 100);
  458. aboutWindow->addChild(aboutOKButton);
  459. aboutOKButton->setPosition(700, 420);
  460. aboutOKButton->addEventListener(this, UIEvent::CLICK_EVENT);
  461. ScreenLabel *versionLabel = new ScreenLabel("version 0.8.2", 12, "mono");
  462. aboutWindow->addChild(versionLabel);
  463. versionLabel->setPosition(20, 430);
  464. versionLabel->color.a = 0.4;
  465. isDragging = false;
  466. dragLabel = new ScreenLabel("NONE", 11, "sans");
  467. dragLabel->setPosition(0,-15);
  468. dragEntity = new ScreenEntity();
  469. dragEntity->addChild(dragLabel);
  470. addChild(dragEntity);
  471. dragEntity->visible = false;
  472. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
  473. CoreServices::getInstance()->getCore()->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
  474. curveEditor = new CurveEditor();
  475. addChild(curveEditor);
  476. curveEditor->setPosition(200,100);
  477. curveEditor->visible = false;
  478. curveEditor->enabled = false;
  479. globalColorPicker = new UIColorPicker();
  480. globalColorPicker->setPosition(300,300);
  481. addChild(globalColorPicker);
  482. modalRoot = new UIElement();
  483. addChild(modalRoot);
  484. fileDialogBlocker = new ScreenShape(ScreenShape::SHAPE_RECT, 100, 100);
  485. fileDialogBlocker->setPositionMode(ScreenEntity::POSITION_TOPLEFT);
  486. addChild(fileDialogBlocker);
  487. fileDialogBlocker->setColor(0.0, 0.0, 0.0, 0.5);
  488. fileDialogBlocker->processInputEvents = true;
  489. fileDialogBlocker->blockMouseInput = true;
  490. fileDialogBlocker->visible = false;
  491. fileDialogBlocker->enabled = false;
  492. fileBrowserRoot = new UIElement();
  493. addChild(fileBrowserRoot);
  494. fileDialog = NULL;
  495. }
  496. void PolycodeFrame::showFileBrowser(String baseDir, bool foldersOnly, std::vector<String> extensions, bool allowMultiple) {
  497. if(fileDialog)
  498. delete fileDialog;
  499. fileDialog = new UIFileDialog(baseDir, foldersOnly, extensions, allowMultiple);
  500. fileDialog->addEventListener(this, UIEvent::CANCEL_EVENT);
  501. fileDialog->addEventListener(this, UIEvent::OK_EVENT);
  502. fileBrowserRoot->addChild(fileDialog);
  503. fileDialog->setPosition(100,100);
  504. fileDialogBlocker->visible = true;
  505. fileDialogBlocker->enabled = true;
  506. }
  507. void PolycodeFrame::showCurveEditor() {
  508. curveEditor->visible = true;
  509. curveEditor->enabled = true;
  510. }
  511. void PolycodeFrame::showModal(UIWindow *modalChild) {
  512. modalBlocker->enabled = true;
  513. this->modalChild = modalChild;
  514. modalRoot->addChild(modalChild);
  515. modalChild->showWindow();
  516. modalChild->addEventListener(this, UIEvent::CLOSE_EVENT);
  517. Resize(frameSizeX, frameSizeY);
  518. if(modalChild == yesNoPopup) {
  519. yesNoPopup->focusChild(yesNoPopup->okButton);
  520. }
  521. }
  522. PolycodeProjectBrowser *PolycodeFrame::getProjectBrowser() {
  523. return projectBrowser;
  524. }
  525. void PolycodeFrame::removeEditor(PolycodeEditor *editor) {
  526. for(int i=0; i < editors.size(); i++) {
  527. if(editors[i] == editor) {
  528. editors.erase(editors.begin()+i);
  529. editorHolder->removeChild(editor);
  530. if(editor == editorHolder->currentEditor) {
  531. editorHolder->currentEditor = NULL;
  532. }
  533. return;
  534. }
  535. }
  536. }
  537. void PolycodeFrame::addEditor(PolycodeEditor *editor) {
  538. editors.push_back(editor);
  539. editorHolder->addChild(editor);
  540. editor->enabled = false;
  541. }
  542. void PolycodeFrame::showEditor(PolycodeEditor *editor) {
  543. if(editorHolder->currentEditor) {
  544. editorHolder->currentEditor->enabled = false;
  545. editorHolder->currentEditor = NULL;
  546. }
  547. editorHolder->currentEditor = editor;
  548. editorHolder->currentEditor->enabled = true;
  549. editorHolder->currentEditor->Activate();
  550. Resize(frameSizeX, frameSizeY);
  551. }
  552. void PolycodeFrame::hideModal() {
  553. if(modalChild) {
  554. modalRoot->removeChild(modalChild);
  555. modalChild->removeEventListener(this, UIEvent::CLOSE_EVENT);
  556. modalChild->hideWindow();
  557. modalChild = NULL;
  558. }
  559. modalBlocker->enabled = false;
  560. }
  561. void PolycodeFrame::Update() {
  562. if(willHideModal) {
  563. hideModal();
  564. willHideModal = false;
  565. }
  566. }
  567. void PolycodeFrame::showAssetBrowser(std::vector<String> extensions) {
  568. if(!projectManager->getActiveProject()) {
  569. return;
  570. }
  571. assetBrowser->setProject(projectManager->getActiveProject());
  572. assetBrowser->setExtensions(extensions);
  573. showModal(assetBrowser);
  574. }
  575. void PolycodeFrame::handleEvent(Event *event) {
  576. if(event->getDispatcher() == currentFileSelector && event->getEventType() == "UIEvent") {
  577. PolycodeEditor *editor = editorManager->openEditors[currentFileSelector->getSelectedIndex()];
  578. editorManager->setCurrentEditor(editor, false);
  579. showEditor(editor);
  580. }
  581. if(event->getDispatcher() == editorManager) {
  582. currentFileSelector->clearItems();
  583. for(int i=0; i < editorManager->openEditors.size(); i++) {
  584. OSFileEntry entry(editorManager->openEditors[i]->getFilePath(), OSFileEntry::TYPE_FILE);
  585. if(editorManager->openEditors[i]->hasChanges()) {
  586. currentFileSelector->addComboItem("* " +entry.name);
  587. } else {
  588. currentFileSelector->addComboItem(entry.name);
  589. }
  590. if(editorManager->getCurrentEditor() == editorManager->openEditors[i]) {
  591. currentFileSelector->setSelectedIndex(i);
  592. }
  593. }
  594. }
  595. if(event->getDispatcher() == projectManager) {
  596. if(projectManager->getActiveProject()) {
  597. currentProjectTitle->setText(projectManager->getActiveProject()->getProjectName());
  598. }
  599. }
  600. if(event->getDispatcher() == aboutOKButton && event->getEventType() == "UIEvent") {
  601. hideModal();
  602. }
  603. if(event->getDispatcher() == fileDialog && event->getEventType() == "UIEvent") {
  604. fileBrowserRoot->removeChild(fileDialog);
  605. fileDialogBlocker->visible = false;
  606. fileDialogBlocker->enabled = false;
  607. }
  608. if(event->getDispatcher() == CoreServices::getInstance()->getCore()->getInput()) {
  609. switch(event->getEventCode()) {
  610. case InputEvent::EVENT_MOUSEUP:
  611. if(isDragging) {
  612. if(editorHolder->currentEditor) {
  613. InputEvent *inputEvent = (InputEvent*) event;
  614. Number posX = inputEvent->mousePosition.x;
  615. Number posY = inputEvent->mousePosition.y;
  616. editorHolder->currentEditor->handleDroppedFile(draggedFile, posX, posY);
  617. }
  618. }
  619. isDragging = false;
  620. dragEntity->visible = false;
  621. break;
  622. case InputEvent::EVENT_MOUSEMOVE:
  623. if(isDragging) {
  624. dragEntity->setPosition(((InputEvent*)event)->mousePosition);
  625. }
  626. break;
  627. }
  628. }
  629. if(event->getDispatcher() == projectBrowser->treeContainer->getRootNode()) {
  630. switch (event->getEventCode()) {
  631. case UITreeEvent::DRAG_START_EVENT:
  632. {
  633. UITreeEvent *treeEvent = (UITreeEvent*) event;
  634. BrowserUserData *data = (BrowserUserData*)treeEvent->selection->getUserData();
  635. draggedFile = data->fileEntry;
  636. dragLabel->setText(data->fileEntry.name);
  637. dragEntity->visible = true;
  638. isDragging = true;
  639. // printf("START DRAG: %s\n", data->fileEntry.name.c_str());
  640. }
  641. break;
  642. }
  643. }
  644. if(event->getDispatcher() == modalChild) {
  645. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLOSE_EVENT) {
  646. willHideModal = true;
  647. }
  648. } else {
  649. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT && event->getDispatcher() == newProjectButton) {
  650. newProjectWindow->ResetForm();
  651. showModal(newProjectWindow);
  652. }
  653. if(event->getEventType() == "UIEvent" && event->getEventCode() == UIEvent::CLICK_EVENT && event->getDispatcher() == examplesButton) {
  654. newProjectWindow->ResetForm();
  655. showModal(exampleBrowserWindow);
  656. }
  657. }
  658. }
  659. void PolycodeFrame::Resize(int x, int y) {
  660. frameSizeX = x;
  661. frameSizeY = y;
  662. welcomeEntity->setPosition((x-welcomeImage->getWidth()) / 2,
  663. (y-welcomeImage->getHeight()) / 2);
  664. topBarBg->setShapeSize(x, 45);
  665. logo->setPosition(x-logo->getWidth()-2, 2);
  666. resizer->setPosition(x-resizer->getWidth()-1, y-resizer->getHeight()-1);
  667. mainSizer->Resize(x,y-45);
  668. modalBlocker->setShapeSize(x, y);
  669. fileDialogBlocker->setShapeSize(x, y);
  670. currentFileSelector->setPosition(x-350, 11);
  671. if(this->modalChild) {
  672. modalChild->setPosition((x-modalChild->getWidth())/2.0f, (y-modalChild->getHeight())/2.0f);
  673. }
  674. }
  675. PolycodeFrame::~PolycodeFrame() {
  676. }