Guest User

Adding UIComponents and interactive graphics to a TextFlow

a guest
Oct 17th, 2016
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MXML 31.91 KB | None | 0 0
  1. The following code uses the cursor manager to show a hand cursor when over inline images. There are some a few lines of references to local classes you will need to remove to enable this example code to run.  
  2.  
  3.     <?xml version="1.0" encoding="utf-8"?>
  4.     <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
  5.                            xmlns:s="library://ns.adobe.com/flex/spark"
  6.                            xmlns:mx="library://ns.adobe.com/flex/mx"
  7.                            xmlns:utils="com.flexcapacitor.utils.*"
  8.                            xmlns:local="*"
  9.                            
  10.                            frameRate="10"
  11.                            width="1000" height="550"
  12.                            applicationComplete="init()"
  13.                            >
  14.        
  15.         <fx:Script>
  16.             <![CDATA[
  17.                 import com.flexcapacitor.utils.CursorUtils;
  18.                 import com.flexcapacitor.utils.DisplayObjectUtils;
  19.                
  20.                 import mx.collections.ArrayList;
  21.                 import mx.core.UIComponent;
  22.                
  23.                 import spark.components.Button;
  24.                 import spark.components.ComboBox;
  25.                 import spark.components.Group;
  26.                 import spark.components.TileGroup;
  27.                 import spark.utils.TextFlowUtil;
  28.                
  29.                 import flashx.textLayout.container.ContainerController;
  30.                 import flashx.textLayout.edit.IEditManager;
  31.                 import flashx.textLayout.edit.SelectionState;
  32.                 import flashx.textLayout.elements.InlineGraphicElement;
  33.                 import flashx.textLayout.elements.TextFlow;
  34.                 import flashx.textLayout.formats.TextLayoutFormat;
  35.                
  36.                 static private const simpleText:String = "<TextFlow xmlns='http://ns.adobe.com/textLayout/2008'>"
  37.                     + "<p styleName='center'><span typeName='a'>There are many </span><span styleName='italic'>such</span><span> lime-kilns </span><a href='http://www.google.com' typeName='a'><span>links</span></a><span> in that tract of country, for the purpose of burning the white marble which composes a large part of the substance of the hills. Some of them, built years ago, and long deserted, with weeds growing in the vacant round of the interior, which is open to the sky, and grass and wild-flowers rooting themselves into the chinks of the stones, look already like relics of antiquity, and may yet be overspread with the lichens of centuries to come. Others, where the lime-burner still feeds his daily and nightlong fire, afford points of interest to the wanderer among the hills, who seats himself on a log of wood or a fragment of marble, to hold a chat with the solitary man. It is a lonesome, and, when the character is inclined to thought, may be an intensely thoughtful occupation; as it proved in the case of Ethan Brand, who had mused to such strange purpose, in days gone by, while the fire in this very kiln was burning.</span></p>"
  38.                     + "<br/><p><span>The man who now watched the </span><span id='bold'>fire</span><span> was of a </span><span typeName='foo'>different</span><span> order, and troubled himself with no thoughts save the very few that were requisite to his business. At frequent intervals, he flung back the clashing weight of the iron door, and, turning his face from the insufferable glare, thrust in huge logs of oak, or stirred the immense brands with a long pole. Within the furnace were seen the curling and riotous flames, and the burning marble, almost molten with the intensity of heat; while without, the reflection of the fire quivered on the dark intricacy of the surrounding forest, and showed in the foreground a bright and ruddy little picture of the hut, the spring beside its door, the athletic and coal-begrimed figure of the lime-burner, and the half-frightened child, shrinking into the protection of his father's shadow. And when again the iron door was closed, then reappeared the tender light of the half-full moon, which vainly strove to trace out the indistinct shapes of the neighboring mountains; and, in the upper sky, there was a flitting congregation of clouds, still faintly tinged with the rosy sunset, though thus far down into the valley the sunshine had vanished long and long ago.</span></p>"
  39.                     + "</TextFlow>";
  40.                
  41.                 private function init():void {
  42.                     // set it into the editor
  43.                     resetContent();
  44.                    
  45.                     var floatOptions:Array = ["none","left","right","start","end"];
  46.                     float.dataProvider = new ArrayList(floatOptions);
  47.                     float.selectedIndex = 0;
  48.                    
  49.                     if (mouseCursorData==null) {
  50.                         cursorID = "handCursor";
  51.                         mouseCursorData = CursorUtils.createMouseCursorData(HandPointer, nativeMouseCursorX, nativeMouseCursorY);
  52.                         Mouse.registerCursor(cursorID, mouseCursorData);
  53.     //                  cursorID = MouseCursor.HAND;
  54.                     }
  55.                 }
  56.                
  57.                 public var inlineGraphicElementsDictionary:Dictionary = new Dictionary(true);
  58.                 public var displayObjectsDictionary:Dictionary = new Dictionary(true);
  59.                 public var componentsDictionary:Dictionary = new Dictionary(true);
  60.                 public var index:int;
  61.                 public var editorSubGroup:Group;
  62.                 public var grandparentComponent:UIComponent;
  63.                 public var positioningGroups:Array = [];
  64.    
  65.                 private var editManager:IEditManager;
  66.                
  67.                 /**
  68.                  * Insert an image
  69.                  *
  70.                  * source is either
  71.                  *    a String interpreted as a URI,
  72.                  *    a Class interpreted as the class of an Embed DisplayObject,
  73.                  *    a DisplayObject instance or
  74.                  *    a URLRequest.
  75.                  * width, height is a number or percent
  76.                  * options - the float to assign (String value, none for inline with text, left/right/start/end for float)
  77.                  */
  78.                 public function insertImage(source:Object, width:Object = null, height:Object = null, options:String= null, operationState:SelectionState = null):InlineGraphicElement {
  79.                     var inlineGraphicElement:InlineGraphicElement;
  80.                     var currentFormat:TextLayoutFormat;
  81.                     var selectionStart:int;
  82.                     var selectionEnd:int;
  83.                     var loader:Loader;
  84.                     var displayObject:DisplayObject;
  85.                     var textFlow:TextFlow;
  86.                     var uicomponent:UIComponent;
  87.                     var componentContainer:Sprite;
  88.                     var sprite:Sprite;
  89.                    
  90.                     if (editor.selectionActivePosition==-1) {
  91.                         editor.selectRange(0, 0);
  92.                     }
  93.                    
  94.                     if (editor && editor.textFlow) {
  95.                         textFlow = editor.textFlow;
  96.                     }
  97.                    
  98.                     if (textFlow.interactionManager) {
  99.                         editManager = textFlow.interactionManager as IEditManager;
  100.                     }
  101.                    
  102.                     if (editor && textFlow && editManager) {
  103.                        
  104.                         if (editor.selectionActivePosition==-1) {
  105.                             editManager.selectFirstPosition();
  106.                         }
  107.                        
  108.                         //textFlow.flowComposer.addController(new ContainerController(new Sprite(), 500, 100));
  109.                        
  110.                         var containerController:ContainerController = textFlow.flowComposer.getControllerAt(0);
  111.                         var container:DisplayObjectContainer = containerController.container; // rich text editor component
  112.                         //textFlow.flowComposer.numControllers;
  113.                        
  114.                         if (source is UIComponent) {
  115.                             var containerSprite:Sprite;
  116.                             var nestComponent:Boolean;
  117.                             var addToSubGroup:Boolean = false;
  118.                             var addToSubSubGroup:Boolean = true;
  119.                             var crazyGroup1:Group;
  120.                             var crazyGroup2:Group;
  121.                             var optimizedTest:Boolean;
  122.                             var parentToComponent:UIComponent;
  123.                            
  124.                             uicomponent = source as UIComponent;
  125.                            
  126.                             if (!nestComponent) {
  127.                                
  128.                                
  129.                                 if (addToSubSubGroup) {
  130.                                    
  131.                                     if (optimizedTest) {
  132.                                         // this is flakey (delete does not remove elements after a whiel
  133.                                         if (addToSubSubGroup && grandparentComponent==null) {
  134.                                             grandparentComponent = new UIComponent();
  135.                                             editor.addChild(grandparentComponent);
  136.                                         }
  137.                                        
  138.                                         parentToComponent = new UIComponent();
  139.                                        
  140.                                         parentToComponent.addChild(uicomponent);
  141.                                         grandparentComponent.addChild(parentToComponent);
  142.                                        
  143.                                         inlineGraphicElement = editManager.insertInlineGraphic(parentToComponent, uicomponent.width, uicomponent.height, options, operationState);
  144.                                     }
  145.                                     else {
  146.                                         // this works mostly except after reseting the textflow or using
  147.                                         // anything but float none
  148.                                         var localParentToComponent:UIComponent = new UIComponent();
  149.                                         var localGrandparent:UIComponent = new UIComponent();
  150.                                        
  151.                                         localParentToComponent.addChild(uicomponent);
  152.                                         localGrandparent.addChild(localParentToComponent);
  153.                                         positioningGroups.push(localGrandparent);
  154.                                         editor.addChild(localGrandparent);
  155.                                        
  156.                                         inlineGraphicElement = editManager.insertInlineGraphic(localParentToComponent, uicomponent.width, uicomponent.height, options, operationState);
  157.                                        
  158.                                     }
  159.                                    
  160.                                     /* THIS WORKS BEST
  161.                                     var parentToComponent:UIComponent = new UIComponent();
  162.                                     var grandparent:UIComponent = new UIComponent();
  163.                                    
  164.                                     parentToComponent.addChild(uicomponent);
  165.                                     grandparent.addChild(parentToComponent);
  166.                                     editor.addChild(grandparent);
  167.                                    
  168.                                     inlineGraphicElement = editManager.insertInlineGraphic(parentToComponent, uicomponent.width, uicomponent.height, options, operationState);
  169.                                    
  170.                                    
  171.                                     crazyGroup1 = new Group();
  172.                                     crazyGroup2 = new Group();
  173.                                     crazyGroup1.addElement(uicomponent);
  174.                                     crazyGroup2.addElement(crazyGroup1);
  175.                                     editor.addChild(crazyGroup2);
  176.                                    
  177.                                     inlineGraphicElement = editManager.insertInlineGraphic(crazyGroup1, uicomponent.width, uicomponent.height, options, operationState);
  178.                                     */
  179.                                    
  180.                                     /*
  181.                                     var parentContainer:Sprite = new Sprite();
  182.                                     var grandparentContainer:Sprite = new Sprite();
  183.                                     parentContainer.addChild(uicomponent);
  184.                                     grandparentContainer.addChild(parentContainer);
  185.                                     editor.addChild(grandparentContainer);
  186.                                    
  187.                                     inlineGraphicElement = editManager.insertInlineGraphic(parentContainer, uicomponent.width, uicomponent.height, options, operationState);
  188.                                    
  189.                                     var parentContainer:SpriteVisualElement = new SpriteVisualElement();
  190.                                     var grandparentContainer:SpriteVisualElement = new SpriteVisualElement();
  191.                                     parentContainer.addChild(uicomponent);
  192.                                     grandparentContainer.addChild(parentContainer);
  193.                                     editor.addChild(grandparentContainer);
  194.                                    
  195.                                     inlineGraphicElement = editManager.insertInlineGraphic(parentContainer, uicomponent.width, uicomponent.height, options, operationState);
  196.                                    
  197.                                     */
  198.                                 }
  199.                                
  200.                                 else if (addToSubGroup) {
  201.                                    
  202.                                     if (editorSubGroup==null) {
  203.                                         editorSubGroup = new Group();
  204.                                         editor.addChild(editorSubGroup);
  205.                                     }
  206.                                    
  207.                                     editorSubGroup.addElement(uicomponent);
  208.                                     inlineGraphicElement = editManager.insertInlineGraphic(editorSubGroup, uicomponent.width, uicomponent.height, options, operationState);
  209.                                 }
  210.                                 else {
  211.                                     addElement(uicomponent);
  212.                                     inlineGraphicElement = editManager.insertInlineGraphic(uicomponent, uicomponent.width, uicomponent.height, options, operationState);
  213.                                 }
  214.                                
  215.                             }
  216.                             else {
  217.                                 containerSprite = new Sprite();
  218.                                 inlineGraphicElement = editManager.insertInlineGraphic(containerSprite, uicomponent.width, uicomponent.height, options, operationState);
  219.                                 sprite = inlineGraphicElement.graphic as Sprite;
  220.                                
  221.                                 if (sprite) {
  222.                                     //addElement(uicomponent);
  223.                                     //editor.addChild(uicomponent);
  224.                                     //sprite.parent.addChild(uicomponent);
  225.                                 }
  226.                             }
  227.                            
  228.                            
  229.                             if (uicomponent) {
  230.                                 uicomponent.validateNow();
  231.                             }
  232.                            
  233.                             componentsDictionary[inlineGraphicElement] = uicomponent;
  234.                         }
  235.                         else {
  236.                             inlineGraphicElement = editManager.insertInlineGraphic(source, null, null, options, operationState);
  237.                         }
  238.                        
  239.                         if (inlineGraphicElement==null) {
  240.                            
  241.                             if (editor.selectionActivePosition==-1) {
  242.                                 trace("You need to have a set an active position");
  243.                                 return null;
  244.                             }
  245.                            
  246.                             trace("Inline graphic was not created and null");
  247.                             return null;
  248.                         }
  249.                        
  250.                         //inlineGraphicElement.status = InlineGraphicElementStatus.LOADING;
  251.                        
  252.                         displayObject = inlineGraphicElement.graphic as DisplayObject;
  253.                         loader = inlineGraphicElement.graphic as Loader;
  254.                         sprite = inlineGraphicElement.graphic as Sprite;
  255.                         uicomponent = inlineGraphicElement.graphic as UIComponent;
  256.                        
  257.                         if (loader) {
  258.                             loader.contentLoaderInfo.addEventListener(Event.INIT, inlineGraphicElementLoader_complete);
  259.                             loader.contentLoaderInfo.addEventListener(Event.COMPLETE, inlineGraphicElementLoader_complete);
  260.                             loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, inlineGraphicElementLoader_complete);
  261.                             loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, inlineGraphicElementLoader_complete);
  262.                             loader.contentLoaderInfo.addEventListener(Event.OPEN, inlineGraphicElementLoader_complete);
  263.                             loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, inlineGraphicElementLoader_complete);
  264.                             loader.contentLoaderInfo.addEventListener(Event.UNLOAD, inlineGraphicElementLoader_complete);
  265.                            
  266.                             loader.addEventListener(Event.ADDED, inlineGraphicElementLoader_complete);
  267.                             loader.addEventListener(Event.ADDED_TO_STAGE, inlineGraphicElementLoader_complete);
  268.                             loader.addEventListener(MouseEvent.CLICK, inlineGraphicElementClickHandler);
  269.                             //loader.addEventListener(MouseEvent.MOUSE_OUT, inlineGraphicElementMouseOut);
  270.                             loader.addEventListener(MouseEvent.MOUSE_MOVE, cursorObject_mouseMove);
  271.                             loader.addEventListener(MouseEvent.ROLL_OVER, cursorObject_rollOver);
  272.                             loader.addEventListener(MouseEvent.ROLL_OUT, cursorObject_rollOut);
  273.                             //loader.addEventListener(MouseEvent.ROLL_OUT, inlineGraphicElementMouseOut);
  274.                            
  275.                             //loader.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, uncaughtErrorHandler);
  276.                             displayObject = loader.parent; // may be null
  277.                            
  278.                             inlineGraphicElementsDictionary[loader] = inlineGraphicElement;
  279.                             if (displayObject) {
  280.                                 displayObjectsDictionary[displayObject] = inlineGraphicElement;
  281.                             }
  282.                         }
  283.                         else if (displayObject) {
  284.                             if (uicomponent) {
  285.                                 uicomponent.validateNow();
  286.                             }
  287.                             if (displayObject is Shape) {
  288.                                 //addElement(button);
  289.                                 //editor.addChild(button);
  290.                                 //sprite = displayObject.parent as Sprite;
  291.                                 //sprite.addChild(button);
  292.                                 //removeElement(button);
  293.                             }
  294.                            
  295.                             inlineGraphicElementsDictionary[displayObject] = inlineGraphicElement;
  296.                         }
  297.                        
  298.                         editManager.updateAllControllers();
  299.                     }
  300.                    
  301.                     return inlineGraphicElement;
  302.                 }
  303.                
  304.                 /**
  305.                  * Handle inline graphics loaded
  306.                  * */
  307.                 public function inlineGraphicElementLoader_complete(event:Event):void {
  308.                     var inlineGraphicElement:InlineGraphicElement;
  309.                     var displayObject:DisplayObject;
  310.                     var loaderInfo:LoaderInfo;
  311.                     var loader:Loader;
  312.                     var sprite:Sprite;
  313.                     var bitmap:Bitmap;
  314.                     var actualWidth:int;
  315.                     var actualHeight:int;
  316.                     var graphicStatus:String;
  317.                     var currentTarget:Object;
  318.                     var eventType:String;
  319.                     var drawGraphics:Boolean;
  320.                    
  321.                     currentTarget = event.currentTarget;
  322.                     eventType = event.type;
  323.                    
  324.                     trace("\n" + currentTarget);
  325.                     trace("Event: " +  event.type);
  326.                    
  327.                    
  328.                     if (currentTarget is LoaderInfo) {
  329.                         loader = currentTarget.loader as Loader;
  330.                     }
  331.                     else if (currentTarget is Loader) {
  332.                         loader = currentTarget as Loader;
  333.                     }
  334.                    
  335.                     displayObject = loader ? loader.parent : null;
  336.                     sprite = displayObject ? displayObject as Sprite : null;
  337.                    
  338.                    
  339.                     // show loading icon???
  340.                     if (eventType==Event.OPEN && sprite) {
  341.                         /*
  342.                         var busyIndicator:BusyCursor = new BusyCursor();
  343.                         var button:Button = new Button();
  344.                         button.label="Test";
  345.                         button.width = 100;
  346.                         button.height = 22;
  347.                         sprite.width = button.width;
  348.                         sprite.height = button.height;
  349.                         loader.width = button.width;
  350.                         loader.height = button.height;
  351.                         button.validateNow();
  352.                         //sprite.addChild(button);
  353.                        
  354.                         //loader.addChild(button);
  355.                         sprite.addChild(busyIndicator);
  356.                         */
  357.                     }
  358.                    
  359.                     inlineGraphicElement = inlineGraphicElementsDictionary[loader];
  360.                    
  361.                     // add to dictionary since the display object may not have been created yet
  362.                     if (displayObject && inlineGraphicElement && displayObjectsDictionary[displayObject]==null) {
  363.                         displayObjectsDictionary[displayObject] = inlineGraphicElement;
  364.                     }
  365.                    
  366.                     graphicStatus = inlineGraphicElement ? inlineGraphicElement.status : "inline graphic element not found";
  367.                    
  368.                     trace("Graphic status: " + graphicStatus);
  369.                    
  370.                     if (eventType==IOErrorEvent.IO_ERROR) {
  371.                         //sprite.removeChildren();loader.removeChildren();
  372.                     }
  373.                    
  374.                     if (eventType!=Event.ADDED_TO_STAGE) {
  375.                         return;
  376.                     }
  377.                    
  378.                     if (loader.content) {
  379.                         bitmap = loader.content as Bitmap;
  380.                        
  381.                         if (bitmap) {
  382.                             actualWidth = bitmap.width;
  383.                             actualHeight = bitmap.height;
  384.                         }
  385.                     }
  386.                    
  387.                     if (sprite) {
  388.                         sprite.buttonMode = true;
  389.                        
  390.                         if (drawGraphics) {
  391.                             if (sprite.width==0 && actualWidth) {
  392.                                 sprite.width = actualWidth;
  393.                                 sprite.height = actualHeight;
  394.                             }
  395.                            
  396.                             sprite.graphics.beginFill(0xff0000, .8);
  397.                             sprite.graphics.drawCircle(0,0, 10);
  398.                             sprite.graphics.endFill();
  399.                         }
  400.                     }
  401.                    
  402.                     if (displayObject) {
  403.                         displayObject.addEventListener(MouseEvent.CLICK, inlineGraphicElementClickHandler, false, 0, true);
  404.                         //displayObject.addEventListener(MouseEvent.CLICK, highPriorityInlineGraphicElementClickHandler, true, EventPriority.CURSOR_MANAGEMENT, true);
  405.                     }
  406.                    
  407.                 }
  408.                
  409.                 protected function inlineGraphicElementClickHandler(event:Event):void {
  410.                     trace ("Clicked: " + event.currentTarget);
  411.                     var inlineGraphicElement:InlineGraphicElement;
  412.                     var currentTarget:Object = event.currentTarget;
  413.                     var sprite:Sprite = currentTarget as Sprite;
  414.                     var target:Object = event.target;
  415.                     var loader:Loader = target as Loader;
  416.                     var object:Object;
  417.                     var startPosition:int;
  418.                    
  419.                     inlineGraphicElement = inlineGraphicElementsDictionary[currentTarget];
  420.                    
  421.                     if (inlineGraphicElement==null) {
  422.                         inlineGraphicElement = displayObjectsDictionary[currentTarget];
  423.                     }
  424.                    
  425.                     if (inlineGraphicElement==null) {
  426.                         object = currentTarget;
  427.                     }
  428.                    
  429.                    
  430.                     while (inlineGraphicElement==null) {
  431.                        
  432.                         inlineGraphicElement = inlineGraphicElementsDictionary[object];
  433.                        
  434.                         if (inlineGraphicElement==null && object && "parent" in object) {
  435.                             object = object.parent;
  436.                         }
  437.                         else {
  438.                             break;
  439.                         }
  440.                     }
  441.                    
  442.                     if (inlineGraphicElement!=null) {
  443.                         startPosition = inlineGraphicElement.getAbsoluteStart();
  444.                        
  445.                         editor.selectRange(startPosition, startPosition+1);
  446.                     }
  447.                     else {
  448.                         trace("could not find element");
  449.                     }
  450.                    
  451.                 }
  452.                
  453.                
  454.                 protected function highPriorityInlineGraphicElementClickHandler(event:Event):void {
  455.                     trace ("high Priority Clicked");
  456.                 }
  457.                
  458.                 private function uncaughtErrorHandler(event:UncaughtErrorEvent):void {
  459.                     trace ("Error");
  460.                    
  461.                     if (event.error is Error) {
  462.                         var error:Error = event.error as Error;
  463.                         // do something with the error
  464.                     }
  465.                     else if (event.error is ErrorEvent) {
  466.                         var errorEvent:ErrorEvent = event.error as ErrorEvent;
  467.                         // do something with the error
  468.                     }
  469.                     else {
  470.                         // a non-Error, non-ErrorEvent type was thrown and uncaught
  471.                     }
  472.                    
  473.                    
  474.                 }
  475.                
  476.                
  477.                 public var PointerHand:Class = LibraryToolAssets.PointerHand;
  478.                 public var HandPointer:Class = LibraryToolAssets.Button32;
  479.                 public var mouseCursorData:MouseCursorData;
  480.                 public var nativeMouseCursorX:int = 12;
  481.                 public var nativeMouseCursorY:int = 7;
  482.                 public var cursorManagerX:int = -6;
  483.                 public var cursorManagerY:int = 0;
  484.                 public var useCursorManager:Boolean = false;
  485.                 public var useCustomCursor:Boolean = true;
  486.                 private var cursorID:String;
  487.                 private var currentCursorIndex:int;
  488.                 public var hideCursorWithDefaultMouse:Boolean = true;
  489.                
  490.                 protected function cursorObject_mouseMove(event:MouseEvent):void {
  491.                    
  492.                     trace("\n"+event.type);
  493.                    
  494.                     if (useCursorManager) {
  495.                         // when using flex cursor manager the cursor is updated
  496.                         // at the framerate of the application. this makes it look
  497.                         // sluggish. calling updateAfterEvent forces a redraw
  498.                         event.updateAfterEvent();
  499.                     }
  500.                    
  501.                 }
  502.                
  503.                 protected function cursorObject_rollOver(event:Event):void
  504.                 {
  505.                    
  506.                     trace("\n"+event.type);
  507.                    
  508.                     if (useCursorManager) {
  509.                         trace("using flex cursor manager");
  510.                         currentCursorIndex = cursorManager.setCursor(PointerHand, 2, cursorManagerX, cursorManagerY);
  511.                     }
  512.                     else {
  513.                        
  514.                         if (useCustomCursor) {
  515.                             trace("Assigning cursor named " + cursorID + "");
  516.                             Mouse.cursor = cursorID;
  517.                         }
  518.                         else {
  519.                             trace("Assigning MouseCursor.BUTTON");
  520.                            
  521.                             if (hideCursorWithDefaultMouse) {
  522.                                 cursorManager.hideCursor();
  523.                             }
  524.                            
  525.                             Mouse.cursor = MouseCursor.BUTTON;
  526.                         }
  527.                     }
  528.                    
  529.                     //Mouse.cursor = MouseCursor.AUTO;
  530.                     //cursorManager.setBusyCursor();
  531.                 }
  532.                
  533.                 protected function cursorObject_rollOut(event:MouseEvent):void
  534.                 {
  535.                     trace(event.type);
  536.                     removeCustomCursor();
  537.                 }
  538.                
  539.                 public function removeCustomCursor():void {
  540.                    
  541.                     if (useCursorManager) {
  542.                         trace("removing flex cursor " + currentCursorIndex);
  543.                         cursorManager.removeCursor(currentCursorIndex);
  544.                         cursorManager.removeAllCursors();
  545.                     }
  546.                     else {
  547.                         if (hideCursorWithDefaultMouse) {
  548.                             cursorManager.showCursor();
  549.                         }
  550.                        
  551.                         trace("reseting to MouseCursor.AUTO");
  552.                         Mouse.cursor = MouseCursor.AUTO;
  553.                     }
  554.                 }
  555.                
  556.                 protected function switchCursorModes_clickHandler(event:MouseEvent):void {
  557.                     var selected:Boolean = switchCursorModesButton.selected;
  558.                    
  559.                     removeCustomCursor();
  560.                    
  561.                     if (useCursorManager) {
  562.                         useCursorManager = false;
  563.                         switchCursorModesButton.label = "Mouse Cursor";
  564.                     }
  565.                     else {
  566.                         switchCursorModesButton.label = "Cursor Manager";
  567.                         useCursorManager = true;
  568.                     }
  569.                    
  570.                 }
  571.                
  572.                 protected function insertImage_clickHandler(event:MouseEvent):void
  573.                 {
  574.                     var url:String = "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
  575.                     insertImage(url, null, null, float.selectedItem);
  576.                 }
  577.                
  578.                 protected function insertButton_clickHandler(event:MouseEvent):void
  579.                 {
  580.                     var button:Button;
  581.                     button = new Button();
  582.                     button.width = 100;
  583.                     button.height = 24;
  584.                     button.label = "Flex" + ++index;
  585.                     button.addEventListener(MouseEvent.CLICK, function(event:*):void {trace("button click");});
  586.                    
  587.                     insertImage(button, null, null, float.selectedItem);
  588.                 }
  589.                
  590.                 protected function insertComboBox_clickHandler(event:MouseEvent):void
  591.                 {
  592.                     var combo:ComboBox;
  593.                     combo = new ComboBox();
  594.                     combo.width = 100;
  595.                     combo.height = 24;
  596.                     combo.addEventListener(Event.CHANGE, function(event:*):void {trace("combo box change");});
  597.                    
  598.                     insertImage(combo, null, null, float.selectedItem);
  599.                 }
  600.                
  601.                 protected function insertGroup_clickHandler(event:MouseEvent):void
  602.                 {
  603.                     var combo:ComboBox;
  604.                     combo = new ComboBox();
  605.                     combo.width = 100;
  606.                     combo.height = 24;
  607.                     combo.addEventListener(Event.CHANGE, function(event:*):void {trace("combo box change");});
  608.                    
  609.                     var button:Button;
  610.                     button = new Button();
  611.                     button.width = 100;
  612.                     button.height = 24;
  613.                     button.label = "Flex" + ++index;
  614.                     button.addEventListener(MouseEvent.CLICK, function(event:*):void {trace("button click");});
  615.                    
  616.                     var group:TileGroup;
  617.                     group = new TileGroup();
  618.                     group.width = 100;
  619.                     group.height = 100;
  620.                     group.addEventListener(MouseEvent.CLICK, function(event:*):void {trace("group click");});
  621.                    
  622.                     group.addElement(button);
  623.                     group.addElement(combo);
  624.                     group.addElement(button);
  625.                    
  626.                     insertImage(group, null, null, float.selectedItem);
  627.                 }
  628.                
  629.                
  630.                 protected function listImages_clickHandler(event:MouseEvent):void
  631.                 {
  632.                     var images:Array = [];
  633.                     var textFlow:TextFlow = editor.textFlow;
  634.                     images = textFlow.getElementsByTypeName("img");
  635.                     var inlineGraphic:InlineGraphicElement;
  636.                    
  637.                     trace("\ninline graphics:\n" + images);
  638.                    
  639.                     for (var i:int;i<images.length;i++) {
  640.                         inlineGraphic = images[i] as InlineGraphicElement;
  641.                        
  642.                         var zeroPoint:Point = new Point();
  643.                         var graphicPoint:Point = inlineGraphic.graphic.localToGlobal(zeroPoint);
  644.                         var uicomponent:DisplayObject = componentsDictionary[inlineGraphic] ;
  645.                         var containerPoint:Point = editor.localToGlobal(zeroPoint);
  646.                         var difference:Point = containerPoint.subtract(graphicPoint);
  647.                        
  648.                         if (uicomponent) {
  649.                             var uicomponentPoint:Point = uicomponent.localToGlobal(zeroPoint);
  650.                             var componentDifference:Point = containerPoint.subtract(uicomponentPoint);
  651.                             if ("label" in uicomponent) {
  652.                                 trace(uicomponent["label"]);
  653.                             }
  654.                         }
  655.                        
  656.                         trace("graphic y " + graphicPoint.y);
  657.                         trace("container y " + containerPoint.y);
  658.                         trace("img.graphic y " + inlineGraphic.graphic.y);
  659.                         trace("difference y " + difference.y);
  660.                        
  661.                         if (uicomponent) {
  662.                             trace("uicomponent y " + uicomponent.y);
  663.                             trace("component difference y " + componentDifference.y);
  664.                         }
  665.                        
  666.                         trace("graphic x " + graphicPoint.x);
  667.                         trace("container x " + containerPoint.x);
  668.                         trace("img.graphic x " + inlineGraphic.graphic.x);
  669.                         trace("difference x " + difference.x);
  670.                        
  671.                         if (uicomponent) {
  672.                             trace("uicomponent x " + uicomponent.x);
  673.                             trace("component difference x " + componentDifference.x);
  674.                         }
  675.                     }
  676.                 }
  677.                
  678.                 protected function editableToggle_clickHandler(event:MouseEvent):void
  679.                 {
  680.                     var editable:Boolean = editableToggle.selected;
  681.                     editor.editable = editable;
  682.                    
  683.                 }
  684.                
  685.                 protected function selectableToggle_clickHandler(event:MouseEvent):void
  686.                 {
  687.                     var selectable:Boolean = selectableToggle.selected;
  688.                     editor.selectable = selectable;
  689.                    
  690.                 }
  691.                
  692.                 protected function resetContent():void
  693.                 {
  694.                     var textFlow:TextFlow;
  695.                    
  696.                     TextFlow.defaultConfiguration.unfocusedSelectionFormat = TextFlow.defaultConfiguration.focusedSelectionFormat;
  697.                     TextFlow.defaultConfiguration.inactiveSelectionFormat = TextFlow.defaultConfiguration.focusedSelectionFormat;
  698.                    
  699.                     textFlow = TextFlowUtil.importFromString(simpleText);
  700.                    
  701.                     // set it into the editor
  702.                     editor.textFlow = textFlow;
  703.                    
  704.                     if (positioningGroups && positioningGroups.length) {
  705.                        
  706.                         for (var i:int;i<positioningGroups.length;i++) {
  707.                             var component:UIComponent = positioningGroups.pop();
  708.                             component.parent ? component.parent.removeChild(component):0;
  709.                             trace("Removing components");
  710.                         }
  711.                     }
  712.                    
  713.                     if (editor.selectionActivePosition==-1) {
  714.                         editor.selectRange(0, 0);
  715.                     }
  716.                     editor.selectRange(0, 0);
  717.                 }
  718.                
  719.                 protected function showDisplayList_clickHandler(event:MouseEvent):void
  720.                 {
  721.                     DisplayObjectUtils.walkDownDisplayList(this, showDisplayFunction);
  722.                    
  723.                 }
  724.                
  725.                 public function showDisplayFunction(object:DisplayObject):void {
  726.                    
  727.                     object.y = 5;
  728.                     object.x = 5;
  729.                     object.z = 5;
  730.                 }
  731.             ]]>
  732.         </fx:Script>
  733.        
  734.         <fx:Declarations>
  735.             <utils:MiniInspector />
  736.         </fx:Declarations>
  737.    
  738.         <s:HGroup top="20" horizontalCenter="0">
  739.             <s:Button label="Insert Image"
  740.                       click="insertImage_clickHandler(event)"/>
  741.            
  742.             <s:Button label="Insert Button"
  743.                       click="insertButton_clickHandler(event)"/>
  744.            
  745.             <s:Button label="Insert Combobox"
  746.                       click="insertComboBox_clickHandler(event)"/>
  747.            
  748.             <s:Button label="Insert Group"
  749.                       click="insertGroup_clickHandler(event)"/>
  750.            
  751.         </s:HGroup>
  752.        
  753.         <s:HGroup  top="20" left="10">
  754.             <s:Button label="List Images"
  755.                       click="listImages_clickHandler(event)"/>
  756.             <s:Button label="3D View"
  757.                       click="showDisplayList_clickHandler(event)"/>
  758.         </s:HGroup>
  759.        
  760.         <s:DropDownList id="float" right="20" top="20" selectedIndex="0" >
  761.         </s:DropDownList>
  762.        
  763.         <s:RichEditableText id="editor" text="Some rich text"
  764.                             top="100" left="100" right="100"
  765.                             selectionHighlighting="whenActive"
  766.                             />
  767.         <!--<s:RichEditableText id="editor" text="Some rich text"
  768.                             top="100" left="100" right="100"
  769.                             />-->
  770.        
  771.        
  772.         <s:HGroup bottom="20" left="20" >
  773.             <s:ToggleButton id="editableToggle"
  774.                             label="Editable"
  775.                             selected="true"
  776.                             click="editableToggle_clickHandler(event)"/>
  777.             <s:ToggleButton id="selectableToggle"
  778.                             label="Selectable"
  779.                             selected="true"
  780.                             click="selectableToggle_clickHandler(event)"/>
  781.         </s:HGroup>
  782.        
  783.         <s:HGroup bottom="20" right="20" gap="10">
  784.             <s:ToggleButton id="switchCursorModesButton" label="Mouse Cursor"
  785.                       click="switchCursorModes_clickHandler(event)"/>
  786.            
  787.             <s:Button id="modesButton" label="Cursor test area"
  788.                       rollOver="cursorObject_rollOver(event)"
  789.                       rollOut="cursorObject_rollOut(event)"/>
  790.            
  791.             <s:Button id="nativeHandButton" label="MouseCursor.Button"
  792.                       rollOver="Mouse.cursor = MouseCursor.BUTTON"
  793.                       rollOut="Mouse.cursor = MouseCursor.AUTO"/>
  794.         </s:HGroup>
  795.        
  796.        
  797.         <s:Button label="Reset TextFlow" bottom="20" horizontalCenter="0"
  798.                   click="resetContent()"/>
  799.     </s:WindowedApplication>
Advertisement
Add Comment
Please, Sign In to add comment