How do I position a element when that container is scaled? - actionscript-3

I have a component that I'm trying to position and it works fine when the parent is not scaled but when it is scaled the positioning is off.
How do I get the correct position?
I will try to provide some example code since it is part of a larger application but it's basically the following:
<Group id="MainGroup" x="10" y="10">
<Group id="SubGroup" x="10 y="10">
<Button id="original" x="10" y="10">
</Group>
</Group>
<Button id="clone">
Then I use the code from this question or similar code to find out how to position the second button that is outside of the scaled containers.
It looks like the code I'm using works so there must be something else going on. Here is updated test code:
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" >
<fx:Script>
<![CDATA[
import com.flexcapacitor.utils.DisplayObjectUtils;
protected function button1_clickHandler(event:MouseEvent):void
{
var point:Point = DisplayObjectUtils.getDistanceBetweenDisplayObjects(originalButton, cloneButton);
cloneButton.x = point.x;
cloneButton.y = point.y;
}
]]>
</fx:Script>
<s:Group x="10" y="10" scaleX="2" scaleY="2">
<s:Group x="10" y="10">
<s:Button id="originalButton" x="10" y="10" label="Button"/>
</s:Group>
</s:Group>
<s:Button id="cloneButton" label="position me" click="button1_clickHandler(event)"/>
</s:WindowedApplication>

Related

FileSystemTree itemRenderer

I've been trying to do a couple of things and I couldn't.
I've been trying to skin a mx:FileSystemTree, to show de data horizontally, I think it's almost impossible, anyone has an idea how to make it?
The second thing I'm trying is to add an itemRenderer to the FileSystemTree wanting to add a pair of buttons(add and delete, which I already had working). Everything I tried so far is giving the same error:
TypeError: Error #1034: Type Coercion failed: cannot convert SKins::FileSystemItemRenderer#971c479 to mx.controls.listClasses.IListItemRenderer.
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
width="750" height="500">
<s:layout>
<s:VerticalLayout/>
</s:layout>
<s:VGroup width="607" height="134">
<s:HGroup width="607" height="68">
<s:Button label="Button"/>
<s:Button label="Button"/>
</s:HGroup>
<s:TextInput width="600"/>
</s:VGroup>
<mx:HDividedBox>
<mx:FileSystemTree id="tree" width="615" height="480" allowMultipleSelection="true"
borderVisible="false" defaultLeafIcon="#Embed('../assets/folder.png')"
directory="{new File(raiz)}" dragEnabled="true" dropEnabled="true"
folderClosedIcon="#Embed('../assets/folder.png')"
folderOpenIcon="#Embed('../assets/openFolder.png')" fontSize="45"
fontStyle="normal" fontWeight="normal" iconFunction="selectIcon"
itemRenderer="SKins.FileSystemItemRenderer" showIcons="true"
textAlign="left" textDecoration="none" verticalAlign="middle"/>
</mx:HDividedBox>
</s:WindowedApplication>
That's the code which is going to show the tree.
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="true">
<s:Label text="{data}"/>
</s:ItemRenderer>
I have tried with a simple renderer receiving the error #1034.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2010/04/08/creating-an-alternating-item-renderer-in-a-spark-list-control-in-flex-4/ -->
<s:MXTreeItemRenderer name="IListItemRenderer"
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:HGroup id="hGr" left="4" right="4" top="4" bottom="4">
<s:RichText id="lbl" text="({itemIndex}) {data.label}" width="100%" />
<mx:Image id="img" source="{data.img}" toolTip="{data.img}" width="100" height="67" />
</s:HGroup>
</s:MXTreeItemRenderer>
I also have tried another renderer, but with the same results.
Not sure about showing data horizontally, but your 2nd point about itemrenderer is working fine for me. Here is the working code
<?xml version="1.0" encoding="utf-8"?>
<s:MXTreeItemRenderer
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:HGroup id="hGr" left="4" right="4" top="4" bottom="4">
<s:Rect id="indentationSpacer"
width="{treeListData.indent}" height="22"
alpha="0">
<s:fill>
<s:SolidColor color="0xFFFFFF" />
</s:fill>
</s:Rect>
<s:Group id="disclosureGroup">
<s:BitmapImage source="{treeListData.disclosureIcon}"
width="16" height="16"
visible="{treeListData.hasChildren}" />
</s:Group>
<s:BitmapImage source="{treeListData.icon}"
width="16" height="16"/>
<s:RichText id="lbl" text="{data.nativePath}" toolTip="{data.nativePath}" width="100%" maxDisplayedLines="1" />
<s:Button label="Button"/>
<s:Button label="Button"/>
</s:HGroup>

Flex - How to do calculation in Flash Builder

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">
<fx:Script>
<![CDATA[
protected function calculate_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:actionContent>
<s:Button label="Back" click="navigator.pushView(MainHomeView)" styleName="back"/>
</s:actionContent>
<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="mywieght" x="216" y="40" width="228" prompt="0.0kg" textAlign="right"/>
<s:TextInput id="myheight" x="216" y="119" width="228" prompt="0.0cm" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
fontStyle="italic"/>
<s:Label id="myresult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
verticalCenter="99"/>
</s:View>
this is the gui for a BMI calculator. i dont really have the basic of using flash builder. can anyone teach me how to use the data input by user inside the textinput and then use it for calculation and display it? thanks
First you assign a click handler to your button so your calculate_clickHandler method will be called on button click:
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36" fontStyle="italic" click="calculate_clickHandler(event)"/>
Then you do your calculations and pass your result to the myresult Label:
protected function calculate_clickHandler(event:MouseEvent):void
{
var height:Number = Number(myheight.text);
var weight:Number = Number(mywieght.text); // you have a typo here, wiegth instead of weight ;)
myresult.text = height * weight;
}
And you probably want a popView() on back button click. This will remove the current view and go back to your last view. push() will add another view:
<s:Button label="Back" click="navigator.popView()" styleName="back"/>
Try this:
<?xml version="1.0"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark">
<fx:Script>
<![CDATA[
protected function calculate_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
const myWeightInKG:Number = Number(myWeight.text);
const myHeightInMeter:Number = Number(myHeight.text) * 0.01;
const BMI:Number = myWeightInKG / (myHeightInMeter * myHeightInMeter);
const BMI_withOneDecimalPlace:Number = int(BMI * 10)/10;
myResult.text = BMI_withOneDecimalPlace.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label x="33" y="61" fontSize="30" text="Weight(kg) :"/>
<s:Label x="34" y="140" fontSize="30" text="Height(cm) :"/>
<s:TextInput id="myWeight" x="216" y="40" width="228" prompt="Weight in Kg(e.g. 56)" textAlign="right"/>
<s:TextInput id="myHeight" x="216" y="119" width="228" prompt="Weight in cm(e.g. 157)" textAlign="right"/>
<s:Button id="calculation" x="31" y="260" width="413" label="Calculate" fontSize="36"
fontStyle="italic" click="calculate_clickHandler(event)"/>
<s:Label id="myResult" left="31" right="36" height="146" fontSize="72" fontStyle="normal"
fontWeight="bold" text="0.0" textAlign="center" verticalAlign="middle"
verticalCenter="99"/>
</s:Application>

Why does this non-descriptive error happen when I try to build this mxml file?

I get the following error when trying to build a simple MXML file:
Build halted with errors (fcsh).
Here is the MXML:
<?xml version="1.0" encoding="utf-8"?>
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<s:Label id="titleLabel" x="50" y="50" fontFamily="Arial" fontWeight="bold" text="My first Flex 4 application"/>
<fx:Script>
<![CDATA[
public function example() {
speakTextInput.text = "This is an example of horrible code!";
}
]]>
</fx:Script>
<s:HGroup x="50" y="70" width="80%">
<s:Button id="speakButton" label="Say Something!" />
<s:TextInput id="speakTextInput" width="100%" text="" />
</s:HGroup>
</s:Application>
Maybe your compiler doesn't like functions without type declaration for return value. Try change to:
public function example():void

Spark container not hiding contents outside of its body

This is what what my screen looks like:
It is composed like this :
First container <s:HGroup>(900 X 100)--top black area
Second container <s:Group> (900 X 475)--middle white area
Third container <s:HGroup>--(900 X 100)--bottom black area
If the project were done with Flex 3, the middle area would be an <mx:Canvas>.
Now suppose I have one BorderContainer(125 X 475) and name it middleContainerChild. It is located on the right side of the middle area. When I set its y postion to -middleContainerChild.height, it should be located at y = -475, outside of the container's body. And as you can see in the image above, it has been placed there.
But other than with <mx:Canvas>, the image still shows, even though it is no longer within the <s:Group>s body, and it is rendered "on top" of the <s:HGroup>.
See the image below for more clarification:
If I use <mx:Canvas>, it is hidden away properly, but if I use a Spark container (not only a group, but any Spark container), it remains visible.
Has anyone else had this problem?
Read a bit about clipAndEnableScrolling property of GroupBase class.
Regards.
As 2DH gave me the hint, i have prepared this sample,
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="900" minHeight="672">
<fx:Declarations>
<s:Move
id="moveUp"
yFrom="0"
yTo="-475"
target="{helpWindow}"/>
<s:Move
id="moveDown"
yFrom="-475"
yTo="0"
target="{helpWindow}"/>
</fx:Declarations>
<fx:Script>
<![CDATA[
private function buttonUp_clickHandler(event:MouseEvent):void
{
moveUp.play();
}
private function buttonDown_clickHandler(event:MouseEvent):void
{
moveDown.play();
}
]]>
</fx:Script>
<s:layout>
<s:VerticalLayout gap="0"/>
</s:layout>
<s:BorderContainer
backgroundColor="#000000"
height="100"
width="100%"/>
<s:Group
height="475"
width="100%"
clipAndEnableScrolling="true">
<s:VGroup
left="0"
top="0">
<s:Button
label="Play Effect UP"
click="buttonUp_clickHandler(event)"/>
<s:Button
label="Play Effect DOWN"
click="buttonDown_clickHandler(event)"/>
</s:VGroup>
<s:BorderContainer
id="helpWindow"
backgroundColor="#CCCCCC"
y="{-helpWindow.height}"
right="0"
height="475"
width="125"
/>
</s:Group>
<s:BorderContainer
backgroundColor="#000000"
height="100"
width="100%"/>
</s:Application>
so now i have set my center container's clipAndEnableScrolling to true, and problem solved
Thanks to both the stack members:)

How do I set the dataProvider for an <s:List> component to be an XML file?

I've got the latest Beta of Adobe Flash Builder 4.
I want to use a <s:List> component, and specify the dataProvider as being an XML file.
However, after loads of research (including looking at doc links off labs.adobe.com), I still can't figure out how to do it.
The XML file will look something like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<imageList>
<image location="path/to/file1.jpg" />
<image location="path/to/file2.jpg" />
<image location="path/to/file3.jpg" />
</imageList>
if you want to display images in a list, you should load the xml with a URLLoader , store it in a bindable variable and assign that as data provider to your list. the list should use a mx:itemrenderer where you can customize your view as you wish.
Actual code goes someting like this
<fx:Script>
<![CDATA[
import mx.collections.XMLListCollection;
import mx.collections.IList;
import mx.controls.Image;
private var loader : URLLoader = new URLLoader();
[Bindable]
private var xml : XMLList;
private function init() : void
{
this.loader.addEventListener(Event.COMPLETE, onComplete);
this.loader.load(new URLRequest("data.xml"));
}
private function onComplete(evt : Event) :void
{
this.loader.removeEventListener(Event.COMPLETE, onComplete);
this.xml = new XML(this.loader.data).image;
}
]]>
</fx:Script>
<mx:List id="list" width="200" height="500" dataProvider="{xml}">
<mx:itemRenderer>
<fx:Component>
<mx:Image width="200" height="200" source="{data.#location}" />
</fx:Component>
</mx:itemRenderer>
</mx:List>
If you want to load the XML file over the network you can do:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo">
<fx:Declarations>
<mx:HTTPService id="srv" url="http://ws.jamesward.com/images.xml"/>
</fx:Declarations>
<s:applicationComplete>
srv.send();
</s:applicationComplete>
<s:List dataProvider="{srv.lastResult.images.image}" width="100%" height="100%">
<s:itemRenderer>
<fx:Component>
<mx:Image source="{data.source}"/>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:Application>
You need to use the XMLListCollection class.
<s:List dataProvider="{new XMLListCollection(data.image)}" labelField="#location"/>
My original aim was to have an XML file external to the SWF that my client could maintain themselves, and thus they would have control over the images displayed.
The first answer I posted was not quite the solution I was after: using fx:XML means that the contents of the XML file is actually compiled into the SWF, and hence is not alterable after compilation.
So I've implemented James' solution.
The XML file looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<images>
<image source="path/to/image1.jpg" />
<image source="path/to/image2.jpg" />
<image source="path/to/image3.jpg" />
<image source="path/to/image4.jpg" />
</images>
MXML:
<?xml version="1.0" encoding="utf-8"?>
<s:Group
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
>
<fx:Script>
<![CDATA[
import mx.events.FlexEvent;
protected function lstImages_creationCompleteHandler(event : FlexEvent) : void
{
dpHttpService.send();
}
]]>
</fx:Script>
<fx:Declarations>
<mx:HTTPService
id="dpHttpService"
url="images.xml"
/>
</fx:Declarations>
<s:List
id="lstImages"
dataProvider="{dpHttpService.lastResult.images.image}"
width="100%"
itemRenderer="path.to.render.ImageRenderer"
skinClass="path.to.skins.ListSkin"
>
<s:layout>
<s:HorizontalLayout gap="2" />
</s:layout>
</s:List>
</s:Group>
And in the image renderer, I refer to the data like this:
<mx:Image
id="imgRendered"
source="{data.source}"
/>
The really useful thing about this solution is that I can also put full http:// references to images that exist on another site if I want to (remembering crossdomain.xml, of course).
In fact, the images.xml file can exist on another server.
Well, I found one solution.
The MXML will look like this:
<fx:Declarations>
<fx:XML
id="dpXml"
source="path/to/images.xml"
/>
<mx:XMLListCollection
id="dpXmlListCollection"
source="{dpXml.image}"
/>
</fx:Declarations>
<s:List
id="lstImages"
dataProvider="{dpXmlListCollection}"
itemRenderer="path.to.render.ImageRenderer"
skinClass="path.to.skins.ListSkin"
>
<s:layout>
<s:HorizontalLayout gap="2" />
</s:layout>
</s:List>
And images.xml like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<images>
<image>
<location>path/to/image1.jpg</location>
</image>
<image>
<location>path/to/image2.jpg</location>
</image>
<image>
<location>path/to/image3.jpg</location>
</image>
</images>
Many thanks for all your responses!
Matt