A quick way to know the source of scroll bar? - actionscript-3

Whenever I use a lot of nesting inside mxml components(including many states) with quite a few Vboxs and Other containers, I always get confused when I see a scrollbar appearing on screen, especially with datagrid inside it(I always want to show scroll bar in datagrid and not on the parent container, for which I usually set the height and width of datagrid smaller than its parent container at run time).
My question is, how could I possibly know (QUICKLY), using debugger, that which component is the source of scroll bar that I see on screen (if there are more than one, then some propoerty of compnent must change when I scroll it up or down).
Thanks.

I realise that this answer isn't using the debugger directly. I mean it as an idea for a simple tool really.
I had a quick go at putting together a simple app that's function is to report what display object is dispatching a mouse wheel event. It does not matter to the app if there is a scrollbar or not, but I guess you could adjust it to your needs. Its a quick start really, here's the code...
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" minWidth="955" minHeight="600"
creationComplete="init()">
<mx:HBox id="HBoxWithScrollbar" width="600" height="500">
<mx:HBox width="800" height="800">
</mx:HBox>
</mx:HBox>
<mx:TextArea id="record" height="300" width="600"/>
<mx:Script>
<![CDATA[
private function init():void{
record.text = 'Scroll Record\n';
this.addEventListener(MouseEvent.MOUSE_WHEEL, recordObject);
for each (var obj:DisplayObject in this.getChildren()){
obj.addEventListener(MouseEvent.MOUSE_WHEEL, recordObject);
}
}
protected function recordObject(event:MouseEvent):void{
record.text += (event.target as DisplayObject).toString() + '\n';
}
]]>
</mx:Script>
</mx:Application>
The important thing here really is to see that you can pick up the mouse wheel event at the top level, because it bubbles by default, and isn't cancelable.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/InteractiveObject.html#event:mouseWheel
Once you've got hold of that event, you've got options.
This was built using version 3.6 of the Flex SDK, but it wouldn't take much to build a 4.x version. I am simply displaying "toString()" value of the target display object, but that could be any attribute you want. You'll probably want to put in some error handling for the loop adding events, and also in the event handler. As I said, its just a start, and I hope it helps.

Related

Sorting List without propagating event for resetting scrollbar

I have a spark List and using ArrayCollection as dataprovider for list. Initially when the list is populated sorting is done , but i need to add items to list quite frequently as they update on server. After i add item i have to re-sort the list. After applying sort and calling refresh() scrollbar resets itself to top.
I am looking for a way so that i can add items to list and keep sorting the list without resetting the scrollbar to top position.
I have read similar question earlier but in that question most users have said to store the VerticalScrollPostion in a variable and apply that VerticalScrollPosition back after the sorting is done. The problem with this is there is a jerk in scrollbar , as it goes to top after refresh and then scrolls back to position as applied. So this solution is not possible as items are added very frequently as it will make the scrollbar going up and down very frequently and annoying the users. Also does't look nice n professional
I need a way so that scrollbar stays in it place even after sorting is done.
Also i want to keep using useVirtualLayout=true for the List.
I was also trying to extent ArrayCollection but could't find a way to solve my problem.
Below is simple example(extracted from my main application) to illustrate the problem. Just Scroll to bottom of list and then press the Sort button(which will sort the list) and the scrollbar will jump to top position. I want scrollbar to stay at its position even after pressing Sort button:
<?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="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.collections.SortField;
import spark.collections.Sort;
[Bindable] protected var ar_c:ArrayCollection;
protected var dgArray:Array=new Array();
protected function application1_creationCompleteHandler(event:FlexEvent):void
{
for(var i:int=0;i<20;i++)
dgArray.splice(-1,0,{label:"h"+i});
ar_c= new ArrayCollection(dgArray);
}
protected function sort_list():void{
var sort:spark.collections.Sort=new Sort();
sort.fields=[new SortField("label",false,true)];
ar_c.sort=sort;
ar_c.refresh();
}
]]>
</fx:Script>
<s:List x="42" y="41" id="mylist" width="199" height="253" dataProvider="{ar_c}" useVirtualLayout="true"></s:List>
<s:Button x="295" y="40" label="Sort" click="sort_list();"/>
</s:Application>
I think a sort is applied once and then everything added to the ArrayCollection later on is sorted on. You only have to add the sort once, and then call refresh once to apply the sort.

process facebook feeds dynamically in adobe flash builder

I am trying to process the user feeds retrieved from facebook by dynamically adding them to the flash app, and i need the feed to be displayed in a box with its publisher name and picture beside it (as normal)..
the question is how could i display them aligned under each other if i don't know their heights? is there a way to just add them under each other without specifying the height?
like in html adding divs under each other?
and other question how to add a scroll bar if the content exceeds the flash window?
iam new to as3 & flash so any advice will help.. thanks..
my code:
protected function getFeedsHandler(result:Object,fail:Object):void
{
if(result)
{
var i:Number;
for(i=0;i<10;i++)
if(result[i])
{
var fdLbl= new Label();
addElementAt(fdLbl,1);
fdLbl.text=result[i].message;
fdLbl.x=20;
fdLbl.y=(i+2)*100;
fdLbl.width=400;
var fdImg= new Image();
addElementAt(fdImg,1);
fdImg.source=FacebookDesktop.getImageUrl(result[i].from.id,"small");
fdImg.x=20;
fdImg.y=(i+2)*80;
fdImg.width=400;
var nameLbl= new Label();
addElementAt(nameLbl,1);
nameLbl.text=result[i].from.name;
nameLbl.x=20;
nameLbl.y=(i+2)*90;
}
}
the interface:
<s:Button id="loginoutBtn" right="10" top="10" label="Log out"
click.loggedin="logout(event)"
label.loggedout="Log in" click.loggedout="login(event)"/>
<s:Form includeIn="loggedin" left="70" top="10">
<s:FormItem label="User">
<s:Label id="nameLbl" text=""/>
</s:FormItem>
<s:FormItem label="birthday">
<s:Label id="brthday"/>
</s:FormItem>
<s:FormItem label="feeds">
<s:Image id="feedImg"/>
<s:Label id="feedLbl" x="0"/>
<s:Label id="statusLbl" width="405"/>
</s:FormItem>
</s:Form>
<s:Image id="userImg" includeIn="loggedin" left="10" top="10" width="50"/>
So it looks like you're using Flex and specifically you're using the spark components. In using Spark there's a couple of options on how you would achieve this, one option is to put the component you made above into a VGroup. The VGroup vertically stacks visual components, it has a property called gap you can set to add or remove spacing between the components (number of pixels between each object nested into the VGroup). The addition of a s:Scroller component wrapping the VGroup will give you the scroll bars, essentially the way this works is you set the size on the s:Scroller to the size you want to visible (can be a percentage like 100% width/height of the view/container), then you set no explicit size on the VGroup, the VGroup will expand to have a height equal to the height of all the components nested in it plus any padding plus the gap space, the scroller will take care of figuring out how big the scroll tab button should be and adjust the scrollRect on the VGroup for you, essentially it's going to work like magic :). This would look something like the following:
<s:Scroller width="100%" height="100%">
<s:VGroup id="loadedContentVGroup">
<yourpackage:YourCustomComponent/>
</s:VGroup>
</s:Scroller>
If you don't constrain the scroller's height and width it will not work as it doesn't have a defined region to draw in and will just become as large as it's children (the nested components). In all likelihood you would add elements to the VGroup in AS3 so you'd probably want to give it an ID.
Your other option is to use a spark List and then set your visual component as the itemRenderer. For each data element in the data provider for the list (some sort of collection class, ArrayCollection, XMLCollection etc.) it will set an element of the array as the "data" property on your visual component. You would then use bindings to the data property in your visual component to make sure if the renderer is re-used/recycled the elements within it update automatically. Here's an example of that:
<s:List dataProvider="{new ArrayCollection([{name:'Shaun', birthday:'04-28-1983', imageSource:'someImage.png'}])}"
itemRenderer="views.YourCustomComponent"
width="100%"/>
In your case the dataProvider for the list could be assigned in the result handler code you have above, it appears result is an array so you can just wrap it in an ArrayCollection like myList.dataProvider = new ArrayCollection(result);
Here's a potential view definition for a single entry in the list (the itemRenderer class used above)
[views.YourCustomComponent]
<?xml version="1.0" encoding="utf-8"?>
<s:VGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
implements="mx.core.IDataRenderer">
<fx:Script>
<![CDATA[
private var _data:Object;
[Bindable(eventName="dataChanged")]
public function get data():Object
{
return _data;
}
public function set data(value:Object):void
{
if(_data == value)
return;
_data = value;
dispatchEvent(new Event("dataChanged"));
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label text="{data.name}"/>
<s:Label text="{data.birthday}"/>
<s:Image source="{data.imageSource}"/>
</s:VGroup>
There are certainly caveats and alternatives to these answers but this will hopefully get you going in the right direction, if you're doing this for mobile development be sure to check out the recommendations with regard to creating renderers for mobile, the idea here is to reduce the processing needed for creating and re-using renderers on mobile as much as possible to lighten the load on the CPU and memory.
One other thing to note it appears your mixing defining the view in AS3 and MXML, while this is technically okay I find it to be a bit confusing myself. I would normally define the entire layout in MXML, or if need be entirely in AS3. If defined in MXML and a component needs to be conditionally hidden by code I would just toggle the visible/includeInLayout properties. I suppose this is just a preference but it seems it could get hairy determining what is causing particular layouts to occur when you have to look between the two to piece it together.
References to the AS3 reference for classes used:
http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/spark/components/VGroup.html
http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/spark/components/Scroller.html
http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/spark/components/List.html
Details on custom item renderers http://help.adobe.com/en_US/flex/using/WS03d33b8076db57b9-23c04461124bbeca597-8000.html <-- references Jeffry Houser a regular on SO with the name www.flextras.com

What is the best way to create a slide effect on a canvas?

I need to create a slide left and slide right effect. This will be used on a panel when it is added to stage.
Also, the canvas I want to use the effect on has its height determined by the "top" and "bottom" properties, I heard this may cause issues when applying such an effect.
You might want to check out something called TweenMax: http://www.greensock.com/tweenmax/ It greatly simplifies animation of many things, and it has lots of options which you would expect such as ease in/out, onTween events/callback, on complete callback etc.
I am not 100% sure what you wan to do exactly, but in theory you will want to start your canvas off the screen, and then do something like this:
TweenMax.to(theCanvas, 1.0, {x:0});
This line of code will tell TweenMax (via the static function "to") to start a tween (animation). The target of the animation is theCanvas, the duration of the animation is 1.0 seconds, and the property being animated is x. In this case .x is animated from where ever it current is "to" 0. If you wrote:
TweenMax.to(theCanvas, 1.0, {x:100});
Then the canvas would animate from where is currently is to .x = 100. Note that if you specify a string for the 'value'
TweenMax.to(theCanvas, 1.0, {x:"100"});
this is treated as a delta - so the canvas will not end at x = 100, instead it will end at x = originalX + 100.
As far as the height of the canvas is concerned, it should be ok to constrain the height with top and bottom style settings, but I am not 100% sure of this... you will want to explicitly define the width of the canvas however, as you will need to explicitly(absolutely) define the x position.
EDIT
Here is an example, I think it covers the basics. Note that the slider canvas has its height defined with top and bottom, and there are no issues.
<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas
width="500" height="500"
xmlns:mx="http://www.adobe.com/2006/mxml"
>
<mx:Script>
<![CDATA[
import com.greensock.TweenMax;
private function slideIn():void
{
TweenMax.to(theSlider, 1.0, {x:50});
}
private function slideOut():void
{
TweenMax.to(theSlider, 1.0, {x:300});
}
]]>
</mx:Script>
<mx:Button label="Slide In" click="{slideIn()}"/>
<mx:Button x="80" label="Slide Out" click="{slideOut()}"/>
<mx:Canvas id="theWindow"
width="300" height="300"
verticalCenter="0" horizontalCenter="0"
borderColor="red" borderThickness="1" borderStyle="solid"
verticalScrollPolicy="off" horizontalScrollPolicy="off"
>
<mx:Canvas id="theSlider"
width="200"
x="-200"
top="10" bottom="10"
backgroundColor="green" backgroundAlpha="1.0"
verticalScrollPolicy="off" horizontalScrollPolicy="off"
/>
</mx:Canvas>
</mx:Canvas>
Here's something you can start with that I made a while back, this is the Flex3 version moving it over to 4 I've changed the base class to Group and been able to make some further optimization with regard to the initial creation of the components on the "sliding canvas". View source is enabled in the right click menu on the export: http://www.shaunhusain.com/ImageSlider/ (don't be confused by the name it allows you to add whatever UIComponent to it I just started out with images). I'm not immediately aware of any issues with defining it's size via top and bottom properties but it might require tweaking, if you run into issues post those as questions.

RichTextEditor with image

I need to put in a Rich Text Editor which should be able to insert a picture and my user needs to control the position of placing the image anywhere in the editor without damaging the text already entered.It will be okay if user can at least move the image around the editor. Can anyone help me, have some example?
Any help would be great..
many thanks
You can easily enter images in a RichTextEditor with the following code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
[Bindable]
public var Str:String = '<img src="assets/allBtn_hover.png"/>'
]]>
</mx:Script>
<mx:RichTextEditor id="rte" x="33" y="27" title="Title"
htmlText="{Str}">
</mx:RichTextEditor>
<mx:Text x="397" y="27" text="{Str}" width="270" height="238"/>
</mx:Application>
But moving the image around , is something which might be problematic.
With the above , it might be possible to move the image up and down by placing the wrapped text image at different positions inside the RTE, but incase you want to give absolute control , Id suggest creating a new image object, with absolute layout and allow user to move it around.
But that would involve writing a lot of code , to create the logic of hiding the image, showing part of the image, when a scroll happens and the image is not supposed to be shown on the screen.

resizing list components upon dataProvider change

I have a component that contains a list that implements a custom renderer. I use this component as a tooltip, keeping one in memory and just altering the databindings as necessary.
Something like this where TTComponent is a class that extends Canvas:
<s:TTComponent>
<s:BorderContainer>
<s:List id='lstItems' dataProvider="{data.Items}" width="50%" borderVisible="false" contentBackgroundColor="#222222">
<s:layout>
<s:VerticalLayout useVirtualLayout="false" requestedMinRowCount="1"/>
</s:layout>
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer>
<s:HGroup>
<s:Label text="{data.Value}" paddingLeft="6" />
<s:Label text="{data.Name}" />
</s:HGroup>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
</s:BorderContainer>
</s:TTComponent>
Now, the driver code looks like this for the mouse over:
if (tt == null) {
tt = new TTComponent();
}
tt.data = data;
PopUpManager.addPopUp(tt, this);
And, of course, the mouse out:
PopUpManager.removePopUp(tt);
Now, what happens is when data containing many items is set to tt.data, the list resizes itself larger and displays fine. However, after this, if data containing few items is set to tt.data, the component momentarily displays larger than it needs to be, then resizes itself smaller.
What I'd like to do is have the component resize itself before it displays, so I don't see the resize on screen. Any idea on how to accomplish this?
First, I would compel you to consider creating the component once and just showing / hiding it / moving it when needed rather than using pop up manager. That's how I would typically do something similar to what you're attempting. It will perform better and probably fix this issue on its own. Just a thought.
Secondly, after you rebind new data to it, try these :
Tell it needs to resize :
tt.invalidateDisplayList();
Also, you might need to tell it you changed the data by refreshing the data if an ArrayCollection:
data.refresh();
Let me know if that doesn't do the trick.