Sunday, October 18, 2009

Let's go Full Screen with our Flex apps,it is so easy to do.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/08/07/creating-full-screen-flex-applications/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" applicationComplete="init(event)">

<mx:Script>
<![CDATA[
import flash.display.StageDisplayState;

private function init(evt:Event):void {
/* Set up full screen handler. */
Application.application.stage.addEventListener(FullScreenEvent.FULL_SCREEN, fullScreenHandler);
dispState = Application.application.stage.displayState;
}

private function fullScreenHandler(evt:FullScreenEvent):void {
dispState = Application.application.stage.displayState + " (fullScreen=" + evt.fullScreen.toString() + ")";
if (evt.fullScreen) {
/* Do something specific here if we switched to full screen mode. */
} else {
/* Do something specific here if we switched to normal mode. */
}
}

private function toggleFullScreen():void {
try {
switch (Application.application.stage.displayState) {
case StageDisplayState.FULL_SCREEN:
/* If already in full screen mode, switch to normal mode. */
Application.application.stage.displayState = StageDisplayState.NORMAL;
break;
default:
/* If not in full screen mode, switch to full screen mode. */
Application.application.stage.displayState = StageDisplayState.FULL_SCREEN;
break;
}
} catch (err:SecurityError) {
// ignore
}
}
]]>
</mx:Script>

<mx:String id="dispState" />

<mx:Label text="width={Application.application.width}" />
<mx:Label text="height={Application.application.height}" />
<mx:Label text="displayState={dispState}" />

<mx:Button label="Toggle fullscreen" click="toggleFullScreen()" />

</mx:Application>

Wednesday, October 14, 2009

Flex Icons for Alert

The example uses the mx_internal namespace to access a couple of internal properties (descriptions taken from the documented source code):

alertForm — The internal AlertForm object that contains the text, icon, and buttons of the Alert control.
buttons — An Array that contains any Buttons appearing in the Alert control.

Since this example uses the mx_internal namespace, you can’t always depend on this behavior to work in future versions of the Flex SDK. Use at your own risk.


<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

layout="vertical"
verticalAlign="middle"
creationComplete="showAlert();"
backgroundColor="white" >

<mx:Script>
<![CDATA[

import mx.controls.Alert;
import mx.events.CloseEvent;

[Bindable]
[Embed(source='assets/error.png')]
private var Icon:Class;

[Bindable]

[Embed(source='assets/tick.png')]
private var TickIcon:Class;

[Bindable]
[Embed(source='assets/cross.png')]
private var CrossIcon:Class;


private var a:Alert;

private function showAlert():void {
/* Set button width so it is large enough to accomodate
an icon and default button labels. */

Alert.buttonWidth = 100;

var titleText:String = "WARNING";
var messageText:String = "Are you sure you would like to erase the Internet?\\n\\nPress OK to continue, or Cancel to abort.";

/* Display the Alert, show the OK and Cancel buttons,
and show an icon represented by the Icon binding. */
a = Alert.show(messageText, titleText, Alert.OK | Alert.CANCEL, null, doClose, Icon);


/* Get a reference to the Alert control's internal
buttons array. */
var buttonArray:Array = a.mx_internal::alertForm.mx_internal::buttons;

/* Set the first button to the TickIcon class, and the

second icon to the CrossIcon class. */
buttonArray[0].setStyle("icon", TickIcon);
buttonArray[1].setStyle("icon", CrossIcon);

progressBar.visible = false;

}

private function doClose(evt:CloseEvent):void {
if (evt.detail == Alert.OK) {
progressBar.visible = true;
} else if (evt.detail == Alert.CANCEL) {

// do nothing.
}
}
]]>
</mx:Script>

<mx:Button label="Launch Alert"
click="showAlert();" />


<mx:ProgressBar id="progressBar"
label="Deleting..."
indeterminate="true"
visible="false" />

</mx:Application>

Tuesday, October 13, 2009

Javascript String Replace All


To replace all the %26 to '&' (Un Escape)
str.replace(/%26/g,'&');

To replace all '+' symbol to 'Add'
str.replace(/+/g,'Add');


Extracting an image(jpeg or png) from a raw ByteArray in actionscript / flex

This is a question I see asked fairly regularly, yet it seems to be a hard one to google for and get an easy answer.

In my current main project I have a need to pre load a large amount of images as quickly as possible, and yet kill any existing preloads immediately should circumstances change. Some casting around lead me to the StreamLoader class which offers all I needed, with the proviso that the result is a raw ByteArray.

So, I needed a means to convert that ByteArray to a bitmap or BitmapData.

Some digging around lead me to the fact that the Loader class will do that for a ByteArray that is read from a jpeg or png file.

The code looked something like this for an existing ByteArray in a variable named “bytes” :

var loader:Loader = new Loader();

loader.loadBytes(bytes);

Now, the bitmap *will* be available in the loaders “content” property, but do not assume that just because you gave it a preloaded bytearray that it will be *immediately* available. Or you will be sad and confused, as was I :)

No, the bitmap will only be available once the loader.contentLoaderInfo complete event fires.

So, add an event listener for the complete event :

loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);

Then get the bitmap from the loader at that point in time :

private function loaderCompleteHandler(event:Event)

{

var loader:Loader = event.target as Loader;
var bmp:Bitmap = Bitmap(loader.content);

}

Note that I’ve omitted all the usual removing of event listeners, unloading loaders etc for the sake of brevity.

So there you go, that’s how it’s done. Now I know the answer it makes perfect sense, but initially it did seem to work not quite as I expected it to.

Since then I’ve had occasion to use this a couple more times, such as getting a server side processed image from .NET via fluorine remoting services, and in AIR (grabbing images stored as ByteArray blobs in a sqlite database).

Reference:http://ifeedme.com/blog/?p=5