PageRenderTime 98ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/Editeur1.as

https://gitlab.com/sakisan/editeur-aaaah
ActionScript | 653 lines | 545 code | 85 blank | 23 comment | 111 complexity | cc543f0b51c7575c11ce6de596a2431c MD5 | raw file
  1. package
  2. {
  3. import blocks.*;
  4. import drawing.*;
  5. import editeur.*;
  6. import flash.display.Bitmap;
  7. import flash.display.BitmapData;
  8. import flash.display.DisplayObject;
  9. import flash.display.Graphics;
  10. import flash.display.InteractiveObject;
  11. import flash.display.MovieClip;
  12. import flash.display.Sprite;
  13. import flash.events.Event;
  14. import flash.events.KeyboardEvent;
  15. import flash.events.MouseEvent;
  16. import flash.events.TimerEvent;
  17. import flash.geom.Matrix;
  18. import flash.geom.Point;
  19. import flash.geom.Rectangle;
  20. import flash.net.URLRequest;
  21. import flash.net.URLRequestHeader;
  22. import flash.net.URLRequestMethod;
  23. import flash.net.navigateToURL;
  24. import flash.text.TextField;
  25. import flash.text.TextFieldType;
  26. import flash.text.engine.SpaceJustifier;
  27. import flash.ui.Keyboard;
  28. import flash.utils.ByteArray;
  29. import flash.utils.Timer;
  30. import flash.utils.setTimeout;
  31. import interactivity.Communication;
  32. import org.osmf.events.TimeEvent;
  33. import properties.*;
  34. import support.*;
  35. [SWF(width="900", height="700", frameRate="29", backgroundColor="#303036")]
  36. public class Editeur1 extends Sprite
  37. {
  38. private var _drawfield:Drawboard;
  39. private var _mySelection:Selection;
  40. private var keepSelection:Selection;
  41. private var factory:ShapeDrawerFactory;
  42. private var _drawingPanel:DrawingPanel;
  43. private var editPanel:EditPanel;
  44. private var selectionPanel:SelectionPanel;
  45. private var timeChanger:TimeChanger;
  46. private var options:OptionsPanel;
  47. private var codeRenderer:CodeRenderer;
  48. private var selectionCode:TextField;
  49. private var masker:MovieClip;
  50. private var history:History;
  51. public function Editeur1()
  52. {
  53. this.x = Properties.editeurDefaultX;
  54. this.y = Properties.editeurDefaultY;
  55. Getter.editer = this;
  56. Getter.initialized = false;
  57. drawfield = new Drawboard();
  58. addChild(drawfield);
  59. Getter.drawboard = drawfield;
  60. drawingPanel = new DrawingPanel(810, 10);
  61. addChild(drawingPanel);
  62. State.state = "Dessin";
  63. _mySelection = new Selection(drawfield);
  64. keepSelection = new Selection(drawfield);
  65. history = new History();
  66. editPanel = new EditPanel();
  67. addChild(editPanel);
  68. timeChanger = new TimeChanger(810, 240);
  69. addChild(timeChanger);
  70. Getter.timeChanger = timeChanger;
  71. setupCode();
  72. options = new OptionsPanel();
  73. addEventListener(MouseEvent.MOUSE_DOWN, clickHandler);
  74. addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
  75. addEventListener(MouseEvent.MOUSE_MOVE, updateMouseCoordinates);
  76. doubleClickEnabled = true;
  77. addEventListener(MouseEvent.DOUBLE_CLICK, selectionON);
  78. var timer:Timer = new Timer(500);
  79. timer.start();
  80. timer.addEventListener(TimerEvent.TIMER ,updateHistory);
  81. setChildIndex(drawingPanel, numChildren-1);
  82. startDrawing();
  83. showDrawingPanel();
  84. // var com:Communication = new Communication();
  85. // addChild(com); //testing purposes --> migrate that textbox to Getter.as
  86. // Getter.communication = com;
  87. Getter.initialized = true;
  88. }
  89. //SETUP
  90. private function setupCode():void
  91. {
  92. codeRenderer = new CodeRenderer(drawfield);
  93. addChild(codeRenderer);
  94. codeRenderer.addEventListener(KeyboardEvent.KEY_UP, keyUpLoadCode);
  95. Getter.codeRenderer = codeRenderer;
  96. selectionCode = new TextField;
  97. selectionCode.width = 180;
  98. selectionCode.height = 40;
  99. selectionCode.x = 00;
  100. selectionCode.y = 50;
  101. selectionCode.wordWrap = true;
  102. selectionCode.mouseEnabled = false;
  103. addEventListener(Event.ADDED_TO_STAGE, addBox);
  104. function addBox(e:Event):void
  105. {
  106. stage.addChild(selectionCode);
  107. }
  108. }
  109. public function startDrawing():void
  110. {
  111. codeRenderer.mouseChildren = false;
  112. editPanel.mouseChildren = false;
  113. selectionCode.mouseEnabled = false;
  114. drawfield.drawHitAreaRectangle(0x000000);
  115. setChildIndex(drawfield, numChildren - 1);
  116. }
  117. public function stopDrawing():void
  118. {
  119. codeRenderer.mouseChildren = true;
  120. editPanel.mouseChildren = true;
  121. selectionCode.mouseEnabled = true;
  122. // selectionON();
  123. setChildIndex(drawfield, 0);
  124. }
  125. public function hideDrawingPanel():void
  126. {
  127. setChildIndex(drawingPanel, 0);
  128. }
  129. public function showDrawingPanel():void
  130. {
  131. setChildIndex(drawingPanel, numChildren - 1);
  132. }
  133. public function resetSize(e:Event = null):void
  134. {
  135. drawfield.scaleX = 1;
  136. drawfield.scaleY = 1;
  137. drawfield.x = 0;
  138. drawfield.y = 0;
  139. }
  140. private function reselectForms(step:int):void
  141. {
  142. if(step == 1){
  143. keepSelection.empty();
  144. keepSelection.addGroup(mySelection);
  145. }
  146. //code below is to keep non changing forms of the selection in the selection
  147. else if(step == 2){
  148. mySelection.empty();
  149. var sit:GroupIterator = keepSelection.iterator;
  150. while(sit.hasNext())
  151. {
  152. var it:ElementsIterator = drawfield.iterator;
  153. var shape:Shape = sit.next();
  154. while(it.hasNext())
  155. {
  156. var element:Element = it.next();
  157. if(element is Shape){
  158. var newShape:Shape = Shape(element);
  159. if(shape != null && shape.equals(newShape)){
  160. mySelection.add(newShape);
  161. shape = null;
  162. }
  163. }
  164. else if(element is Group){
  165. var git:GroupIterator = Group(element).iterator;
  166. while(git.hasNext())
  167. {
  168. var newShape2:Shape = git.next();
  169. if(shape != null && shape.equals(newShape2)){
  170. mySelection.add(newShape2);
  171. shape = null;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. showSelectionPanelOrNot();
  179. }
  180. public function jumpInTime():void
  181. {
  182. TimeTraveller.moveElements(drawfield.elements);
  183. }
  184. public function addToSelection(shape:Shape):void
  185. {
  186. mySelection.add(shape);
  187. }
  188. //EventListeners
  189. private function clickHandler(event:MouseEvent):void
  190. {
  191. if(State.state == "Dessin" ){
  192. if(drawfield.drawing()){
  193. startDrawing();
  194. }
  195. }
  196. if(State.state == "Perspective")
  197. {
  198. drawfield.startDrag();
  199. }
  200. if(event.target is NewGraphics){
  201. var shape:Shape = Shape(event.target.parent);
  202. if(State.state == "Selection"){
  203. if(mySelection.containsShape(shape))
  204. mySelection.remove(shape);
  205. else
  206. if(shape.group != null && !shape.hasGroupMemberInSelection)
  207. mySelection.addGroup( shape.group )
  208. else
  209. mySelection.add( shape );
  210. }
  211. if(State.state == "Gomme"){
  212. if(shape.group != null){
  213. Group(shape.group).erase();
  214. }
  215. else{
  216. shape.erase();
  217. }
  218. }
  219. if(State.state == "Deplacement"){
  220. if(shape.group != null){
  221. shape.group.startDrag();
  222. mySelection.addGroup(shape.group);
  223. }
  224. else{
  225. shape.startDrag();
  226. mySelection.add(shape);
  227. }
  228. }
  229. }
  230. }
  231. private function mouseUpHandler(e:MouseEvent):void
  232. {
  233. if(State.state == "Deplacement"){
  234. mySelection.updatePositions();
  235. mySelection.stopDrag();
  236. mySelection.empty();
  237. }
  238. if(State.state == "Perspective")
  239. {
  240. drawfield.stopDrag();
  241. }
  242. //update methods
  243. if(!codeRenderer.contains(DisplayObject(e.target)))
  244. updateCode(e);
  245. showSelectionPanelOrNot();
  246. updateCounter();
  247. }
  248. private function updateMouseCoordinates(e:MouseEvent):void
  249. {
  250. selectionCode.text ="("+Getter.round(Getter.stageToDrawfieldX(e.stageX))+","+Getter.round(Getter.stageToDrawfieldY(e.stageY))+")";
  251. }
  252. public function eraseON(e:Event):void
  253. {
  254. drawfield.drawer = ShapeDrawerFactory.createDrawer("Gomme");
  255. }
  256. public function selectionON(e:Event = null):void
  257. {
  258. drawfield.drawer = ShapeDrawerFactory.createDrawer("Null","Selection");
  259. }
  260. public function moveON(e:Event = null):void
  261. {
  262. drawfield.drawer = ShapeDrawerFactory.createDrawer("Null","Perspective");
  263. }
  264. public function zoneSelectionON(e:Event):void
  265. {
  266. drawfield.drawer = ShapeDrawerFactory.createDrawer("SelectionRectangle");
  267. }
  268. public function groupSelection(e:Event):void
  269. {
  270. selectionON(e);
  271. if(mySelection.shapes.length != 0){
  272. drawfield.createGroupFromSelection(mySelection);
  273. }
  274. mySelection.empty();
  275. }
  276. public function degroupSelection(e:Event):void
  277. {
  278. // var group:Group = new Group(null,mySelection);
  279. // degroupGroup(group);
  280. if(mySelection.shapes.length > 1){
  281. var referenceTable:Array = new Array();
  282. var groupTable:Array = new Array();
  283. var ungroupedForms:Array = new Array();
  284. if(mySelection.containsOneElement()
  285. && Shape(mySelection.shapes[0]).group != null){ //overkill? just leave it
  286. degroupGroup(Shape(mySelection.shapes[0]).group);
  287. }
  288. else{
  289. var it:GroupIterator = mySelection.iterator;
  290. while(it.hasNext()){
  291. var shape:Shape = it.next();
  292. if(shape != null){
  293. if(shape.group !=null){
  294. if(referenceTable.indexOf(shape.group) != -1){
  295. Group(groupTable[referenceTable.indexOf(shape.group)]).add(shape);}
  296. else {
  297. var group:Group = new Group(drawfield);
  298. if(shape.group.myTranslation != null)
  299. group.myTranslation = shape.group.myTranslation.clone();
  300. if(shape.group.myRotation != null)
  301. group.myRotation = shape.group.myRotation.clone();
  302. group.add(shape);
  303. groupTable.push(group);
  304. referenceTable.push(shape.group);
  305. }
  306. }
  307. }
  308. }
  309. }
  310. for each(var g:Group in groupTable){
  311. degroupGroup(g);
  312. }
  313. }
  314. }
  315. public function degroupGroup(group:Group):void
  316. {
  317. if(group !=null){
  318. var translation:Translation = group.myTranslation;
  319. var rotation:Rotation = group.myRotation;
  320. var iter:GroupIterator = group.iterator;
  321. while(iter.hasNext())
  322. {
  323. var shape:Shape = iter.next();
  324. mySelection.add(shape);
  325. groupSelection(null);
  326. //a group is made for each shape in selection
  327. //now because a group consisting of only one shape
  328. // is immediatly dissolved, all shapes are degrouped
  329. shape.myTranslation = translation;
  330. shape.myRotation = rotation;
  331. }
  332. }
  333. }
  334. public function dragON(e:MouseEvent):void
  335. {
  336. drawfield.drawer = ShapeDrawerFactory.createDrawer("Null","Deplacement");
  337. }
  338. public function duplicateSelection(e:Event):void
  339. {
  340. reselectForms(1);
  341. if(mySelection.shapes.length > 1){
  342. var referenceTable:Array = new Array();
  343. var groupTable:Array = new Array();
  344. var ungroupedForms:Array = new Array();
  345. if(mySelection.containsOneElement()
  346. && Shape(mySelection.shapes[0]).group != null) //overkill? just leave it
  347. {
  348. var copySelection:Group = Group(Shape(mySelection.shapes[0]).group.clone());
  349. copySelection.redraw();
  350. groupTable.push(copySelection);
  351. }
  352. else{
  353. var it:GroupIterator = mySelection.iterator;
  354. while(it.hasNext()){
  355. var shape:Shape = it.next();
  356. if(shape != null){
  357. var copy:Shape = Shape(shape.clone());
  358. copy.redraw();
  359. if(shape.group !=null){
  360. if(referenceTable.indexOf(shape.group) != -1){
  361. Group(groupTable[referenceTable.indexOf(shape.group)]).add(copy);}
  362. else {
  363. var group:Group = new Group(drawfield);
  364. group.add(copy);
  365. groupTable.push(group);
  366. referenceTable.push(shape.group);
  367. if(shape.group.myTranslation != null)
  368. group.myTranslation = shape.group.myTranslation.clone();
  369. if(shape.group.myRotation != null)
  370. group.myRotation = shape.group.myRotation.clone();
  371. }
  372. }
  373. else{
  374. ungroupedForms.push(copy);
  375. }
  376. }
  377. }
  378. }
  379. for each(var s:Shape in ungroupedForms)
  380. drawfield.addElement(s);
  381. for each(var g:Group in groupTable){
  382. drawfield.addElement(g);
  383. }
  384. }
  385. else if(mySelection.shapes.length == 1){
  386. Shape(mySelection.shapes[0]);
  387. var copyy:Shape = Shape(Shape(mySelection.shapes[0]).clone());
  388. copyy.redraw();
  389. drawfield.addElement(copyy);
  390. }
  391. codeRenderer.updateCode(drawfield.elements);
  392. reselectForms(2);
  393. loadCode();
  394. }
  395. public function emptyMap(e:Event):void
  396. {
  397. drawfield.elements = new Array;
  398. codeRenderer.updateCode(drawfield.elements);
  399. loadCode();
  400. drawfield.drawer = ShapeDrawerFactory.createDrawer("Gomme");
  401. }
  402. private function keyUpLoadCode(e:KeyboardEvent):void
  403. {
  404. if(e.keyCode == Keyboard.ENTER){
  405. loadCode();
  406. codeRenderer.updateCode(drawfield.elements);
  407. }
  408. drawfield.drawer = ShapeDrawerFactory.createDrawer("Null","Selection");
  409. }
  410. public function updateCode(e:Event = null):void
  411. {
  412. codeRenderer.updateCode(drawfield.elements);
  413. // try{
  414. // Getter.communication.commitRevision(codeRenderer.code);
  415. // }
  416. // catch(e:Error)
  417. // {
  418. //
  419. // }
  420. }
  421. private function updateHistory(e:Event):void
  422. {
  423. history.push(codeRenderer.code);
  424. }
  425. public function groupAll(event:Event):void
  426. {
  427. mySelection.empty();
  428. if(drawfield.elements.length > 1){
  429. var it:ElementsIterator = new ElementsIterator(drawfield.elements);
  430. while(it.hasNext())
  431. {
  432. var e:Element = it.next();
  433. if(e is Shape){
  434. mySelection.add(Shape(e));
  435. }
  436. else if(e is Group){
  437. for each (var s:Shape in Group(e).shapes)
  438. mySelection.add(s);
  439. }
  440. }
  441. groupSelection(event);
  442. // selectionCode.appendText(theGroup.getCode());
  443. }
  444. else if (drawfield.elements.length == 1){
  445. if(drawfield.elements[0] is Group)
  446. {
  447. var group:Group = drawfield.elements[0];
  448. degroupGroup(group);
  449. }
  450. }
  451. }
  452. public function deleteSelection(e:Event):void
  453. {
  454. var drawer:ShapeDrawer = drawfield.drawer;
  455. drawer.terminate();
  456. var selection:Selection = new Selection(null);
  457. selection.addGroup(mySelection);
  458. drawfield.drawer = ShapeDrawerFactory.createDrawer("Gomme");
  459. var it:GroupIterator = selection.iterator;
  460. while(it.hasNext())
  461. it.next().erase();
  462. if(drawer is SelectionRectangleDrawer)
  463. drawfield.drawer = drawer;
  464. else
  465. drawfield.drawer = ShapeDrawerFactory.createDrawer("Null","Selection");
  466. }
  467. public function turnON(e:MouseEvent):void
  468. {
  469. if(mySelection.containsOneElement()){
  470. drawfield.drawer = ShapeDrawerFactory.createDrawer("Tournage");
  471. drawfield.drawer.clickHandler(e);
  472. }
  473. }
  474. public function mouseWheelZoom(e:MouseEvent):void
  475. {
  476. var m:Matrix = drawfield.transform.matrix;
  477. m.scale( 1+e.delta/Properties.zoomPrecision, 1+e.delta/Properties.zoomPrecision);
  478. m.translate(e.stageX*(-e.delta/Properties.zoomPrecision), e.stageY*(-e.delta/Properties.zoomPrecision));
  479. drawfield.transform.matrix = m;
  480. }
  481. public function loadPrevious(e:Event):void
  482. {
  483. loadCode(history.previous());
  484. }
  485. public function loadNext(e:Event):void
  486. {
  487. loadCode(history.next());
  488. }
  489. //UPDATE METHODS
  490. public function loadCode(code:String = null):void
  491. {
  492. reselectForms(1);
  493. var parser:CodeParser = codeRenderer.parser;
  494. var map:Array = parser.parseMap( code == null ? codeRenderer.code : code);
  495. drawfield.elements = map;
  496. codeRenderer.code = parser.read();
  497. history.push(parser.read());
  498. reselectForms(2);
  499. }
  500. private function updateCounter():void
  501. {
  502. drawingPanel.counter.text = ""+drawfield.shapeCounter+"/120";
  503. }
  504. private function showSelectionPanelOrNot():void
  505. {
  506. if(State.state == "Selection" && mySelection.containsOneElement())
  507. {
  508. if(selectionPanel == null || (selectionPanel != null && selectionPanel.parent == null)){
  509. selectionPanel = new SelectionPanel(mySelection, 200, 408);
  510. addChildAt(selectionPanel,1);
  511. selectionPanel.addEventListener(KeyboardEvent.KEY_UP, updateCode);
  512. }
  513. }
  514. else try{
  515. if(selectionPanel != null)
  516. removeChild(selectionPanel);
  517. }catch(e:ArgumentError){
  518. }
  519. }
  520. public function showOptions(e:Event):void
  521. {
  522. addChild(options);
  523. }
  524. public function hideOptions(e:Event):void
  525. {
  526. if(contains(options))
  527. removeChild(options);
  528. }
  529. // GETTERS & SETTERS
  530. public function get drawfield():Drawboard
  531. {
  532. return _drawfield;
  533. }
  534. public function set drawfield(value:Drawboard):void
  535. {
  536. _drawfield = value;
  537. }
  538. public function set state(value:String):void
  539. {
  540. if(value != "Selection" && value != "Tournage")
  541. {
  542. mySelection.empty();
  543. }
  544. if(value != "Dessin"){
  545. drawingPanel.unselect();
  546. }
  547. if(value != "Perspective")
  548. {
  549. this.stopDrag();
  550. }
  551. State.state = value;
  552. var color:uint = State.color;
  553. drawfield.drawHitAreaRectangle(color);
  554. }
  555. public function get drawingPanel():DrawingPanel
  556. {
  557. return _drawingPanel;
  558. }
  559. public function set drawingPanel(value:DrawingPanel):void
  560. {
  561. _drawingPanel = value;
  562. }
  563. public function get mySelection():Selection
  564. {
  565. return _mySelection;
  566. }
  567. }
  568. }