MultiView (0.1 Alpha)
Small component which may act as ViewStack’s little brother.
Component acts somewhat like ViewStack, but it has following differences:
- It accepts DisplayObjects (not only mx.core.Container instances)
- It accepts IFactory instances.
And since it’s very young component, it missing some features like: exceptions and event dispatching, but it’s coming.
Example:
0
1
2
3
package kii.fx.controls
{
import flash.display.DisplayObject;
import mx.core.UIComponent;
import mx.core.mx_internal;
use namespace mx_internal;
import mx.controls.Label
import mx.containers.ViewStack
import mx.core.IFactory;
import kii.events.MultiViewEvent;
/**
* Class acts as Light Version of view stack.
* @author David
* @usage
MXML Example:
ActionScript Example:
(addChild(new MultiView()) as MultiView).views = [ClassFactory(List), ClassFactory(MyCustomClass)];
*/
public class MultiView extends UIComponent
{
/**
* @private
*/
protected var _views:Array = null;
/**
* @private
*/
protected var _selectedView:DisplayObject = null;
/**
* @private
*/
protected var _selectedIndex:int = -1;
/**
* @private
*/
protected var _resizeContent:Boolean = false;
/**
* @private
*/
protected var _resizeToContent:Boolean = false;
/**
* @private
*/
protected var proposedSelectedIndex:int = -1;
/**
* Creates new MultiView instance.
*/
public function MultiView()
{
super();
}
/**
* Indicates whenever MultiView instance should be resized to match content size.
*/
[Inspectable(category=”General”)]
[Bindable]
public function set resizeToContent(value:Boolean):void
{
if(value == _resizeToContent) return;
_resizeToContent = value;
_resizeContent = !value;
invalidateSize();
}
public function get resizeToContent():Boolean
{
return _resizeToContent;
}
/**
* Indicates whenever content should be resized to match MultiView instance size.
*/
[Inspectable(category=”General”)]
[Bindable]
public function set resizeContent(value:Boolean):void
{
if(value == _resizeContent) return;
_resizeContent = value;
_resizeToContent = !value;
invalidateSize();
}
public function get resizeContent():Boolean
{
return _resizeContent;
}
/**
* Collection of either DisplayObject or IFactory instances.
*/
[Inspectable(category=”General”)]
[Bindable]
public function set views(value:Array):void
{
_views = value;
}
public function get views():Array
{
if(_views == null) _views = new Array();
return _views;
}
/**
* Indicates currently selected index.
*/
[Inspectable(category=”General”)]
[Bindable]
public function set selectedIndex(value:int):void
{
if(value == _selectedIndex)
return;
proposedSelectedIndex = value;
invalidateProperties();
}
public function get selectedIndex():int
{
return _selectedIndex;
}
/**
* Reference to currently selected view.
*/
public function get selectedView():DisplayObject
{
return _selectedView;
}
/**
* Adds view to collection.
* @param view DisplayObject or IFactory instance.
* @return Ammount of views.
*/
public function addView(view:Object):int
{
return views.push(view);
}
/**
* @inheritDoc
*/
override protected function commitProperties():void
{
super.commitProperties();
if(proposedSelectedIndex != -1)
{
commitSelectedIndex(proposedSelectedIndex);
proposedSelectedIndex = -1;
}
}
/**
* @private
*/
protected function commitSelectedIndex(index:int):void
{
if(_selectedView != null)
{
removeChild(_selectedView);
_selectedView = null;
}
if(views == null)
return;
if(_views[index] is IFactory)
_views[index] = (_views[index] as IFactory).newInstance();
_selectedView = addChild(_views[index] as DisplayObject);
dispatchEvent(new MultiViewEvent(MultiViewEvent.VIEW_CHANGE, _selectedView, index));
_selectedIndex = index;
proccessResizeContent();
}
/**
* @private
*/
protected function proccessResizeContent()
{
if(_selectedView != null)
{
if(_resizeContent)
{
trace(’processResize:content’);
_selectedView.width = width;
_selectedView.height = height;
}
else if(_resizeToContent)
{
width = _selectedView.width;
height = _selectedView.height;
}
}
}
/**
* @inheritDocs
*/
override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
trace(’updateDisplayList’);
super.updateDisplayList(unscaledWidth, unscaledHeight);
proccessResizeContent();
}
}
}
November 9th, 2007 at 12:18 pm
Nice, but why wouldn’t one just put whatever one would like to use in a Viewstack inside a HBox?
November 9th, 2007 at 7:10 pm
Call me minimalist =) I wanted to created something that consumes very little user resources.
Following class can
a) Hold factories instead of actual children, and create children only when needed.
b) It does not need extra Container instance per item.
November 25th, 2007 at 3:04 pm
Very nice, Im a minimalist as well :-)
can’t wait for the event dispatching….
thanks
Curtis