AlignUtil – align objects easily



1 Comment

  • Share/Bookmark







Recently i had to align tons of UIComponents with scale and even rotation defined. And so, i decided to write a small static class that would really facilitate my life. I called it AlignUtil. That’s it for now. All links are listed bellow. Hope it helps someone. Cheers ^_^

ps: btw i decided to switch my devs license to MIT, so from now on u r free to use the source codes for whatever u want purpose.

Online demo
Download the AlignUtil class

And here’s the source code (in case u r 2 lazy to download the archive):


////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 Julius Loa | jloa@chargedweb.com
//  All Rights Reserved.
//  license:	MIT {http://www.opensource.org/licenses/mit-license.php}
//  notice: 	just keep the header plz
//
////////////////////////////////////////////////////////////////////////////////

package com.chargedweb.utils
{
	import flash.display.DisplayObject;
	import flash.geom.Rectangle;
	import flash.geom.Point;

	/**
	 * <p>AlignUtil class - vertical/horizontal align DisplayObjects</p>
	 * @author			Julius Loa aka jloa, jloa@chargedweb.com
	 * @availability 	flash/flex, as3
	 * @version 		1.0
	 *
	 * <p>Class usage:</p>
	 * @example Flex sample:
	 * <listing version="3.0">
	 * import com.chargedweb.utils.AlignUtil;
	 *
	 * var btn:Button = new Button();
	 * btn.x = 100;
	 * btn.y = 100;
	 * btn.width = 100;
	 * btn.height = 30;
	 * btn.rotation = 30;
	 * addChild(btn);
	 *
	 * AlignUtil.setAlign(AlignUtil.H_CENTER, btn, this);
	 * AlignUtil.setAlign(AlignUtil.V_MIDDLE, btn, this);
	 * </listing>
	 */
	public class AlignUtil
	{
		/** Horizontal left alignment **/
		public static const H_LEFT:String = "horizontalLeft";
		/** Horizontal center alignment **/
		public static const H_CENTER:String = "horizontalCenter";
		/** Horizontal right alignment **/
		public static const H_RIGHT:String = "horizontalRight";

		/** Vertical top alignment **/
		public static const V_TOP:String = "verticalTop";
		/** Vertical middle alignment **/
		public static const V_MIDDLE:String = "verticalMiddle";
		/** Vertical bottom alignment **/
		public static const V_BOTTOM:String = "verticalBottom";

		/**
		 * Applies a specified alignment to the target DisplayObject
		 * @param	align:String			alignment mode (see the public constants defined above)
		 * @param	target:DisplayObject	target DisplayObject to align (according to the set alignment mode) @see flash.display.DisplayObject
		 * @param	parent:DisplayObject	the parent DisplayObject of the target one @see flash.display.DisplayObject
		 * @return	nothing
		 */
		public static function setAlign(align:String, target:DisplayObject, parent:DisplayObject):void
		{
			var a:String = align; var t:DisplayObject = target; var p:DisplayObject = parent;
			var b:Rectangle = t.transform.pixelBounds;
			var bp:Point = p.globalToLocal(new Point(b.x, b.y));
			b.x = bp.x; b.y = bp.y;
			if(a == H_LEFT) t.x = (t.x > b.x) ? t.x - b.x : 0;
			if(a == H_CENTER) t.x = int((p.width - b.width)/2 + t.x - b.x);
			if(a == H_RIGHT) t.x = (t.x > b.x + b.width) ? p.width : p.width - (b.x + b.width - t.x);
			if(a == V_TOP) t.y = (t.y > b.y) ? t.y - b.y : 0;
			if(a == V_MIDDLE) t.y = int((p.height - b.height)/2 + t.y - b.y);
			if(a == V_BOTTOM) t.y = (t.y > b.y + b.height) ? p.height : p.height - (b.y + b.height - t.y);
		}
	}
}

js tooltip



No Comments

  • Share/Bookmark




+




Well, i’m not a js-developer actually, however i had to write a small tooltip in js 2day and i thought that some of you would probably find it usefull. Won’t post the source code here this time, but the demo and source archive are still available @see the links below.

  • Lightweight
  • Supports basic fade effects
  • Easy to use
  • Uses external png graphics
  • Supports html
  • Works in all browsers

Check out the online demo or grab the source files

ps: thx DryIcons for the icon.

csv, xls and utf8 fail



2 Comments

  • Share/Bookmark




In case u need to save utf8 data to a csv or xls file, mind that those formats do not support utf8 encoding.
Say, u’ve got to save a cyrillic text to a csv file:


// A sample cyrillic, bulgarian text, for exmaple
var utf8String:String = "Аз съм българче. Обичам наште планини зелени...";

// Try to save the file in utf8 charset and then
// open it with Microsoft Excel
var file:FileReference = new FileReference();
file.save(utf8String, "utf8_test.csv");

And here’s the result:

But don’t hurry to google for an as3 charset encoding library, as there’s a simple way to solve this issue:


// A sample cyrillic, bulgarian text, for exmaple
var utf8String:String = "Аз съм българче. Обичам наште планини зелени...";

// csv, xls appear to work only with the ascii
// and windows-like charsets, so as the string is
// cyrillic, we'll use the cyrillic (Windows) charset 'windows-1251'
// which codename's 'x-cp1251' according to the adobe's as3 reference.
var bytes:ByteArray = new ByteArray();
bytes.writeMultiByte(utf8String, "x-cp1251");

var file:FileReference = new FileReference();
file.save(bytes, "cp1251_test.csv");

Now we got the expected result:


Really simple, but still there might be someone who’d need it.

ResourceMonitor v2 = ResourceMonitorUtil [updated to 2.1]



8 Comments

  • Share/Bookmark




UPD: i’ve updated the class to version 2.1, fixed some bugs, added new features, switched the license to MIT !


yellow – current fps
cyan – current memory usage
red – max memory usage graph

As lots of you asked me to add some new features like the font color changing, performance increasement etc, i decided to make a new version of the ResourceMonitor class. I’ve added a few new features, made the code more ammm more readable (well, it’s an os class, so probably u’d like to edit smth there) and i’ve even made a documentation this time :)
And i also decided to rename it to ResourceMonitorUtil.

Class usage also has shorten.

Flex:


import com.chargedweb.utils.ResourceMonitorUtil;

rawChildren.addChild(new ResourceMonitorUtil());

Flash:


import com.chargedweb.utils.ResourceMonitorUtil;

addChild(new ResourceMonitorUtil());

Blah-blah-blah, just let me download the ResourceMonitorUtil source code already

Read more

The com.chargedweb.* package, sort of



No Comments

  • Share/Bookmark




I’ve decided to make a sort of a mini package containing some handy utils (but not only) classes in one place.

Includes such classes like: VectorUtil, ResourceMonitor, MatrixUtil, LoaderUtil, Cache, VimeoPlayer, SkypeStatusIcon etc.

Download the com.chargedweb.* package

UPD1: now with docs

UPD2:

- added AlignUtil class (more info here)

ps: i wish i had some free time to collect all my classes in one huge package :(

Fast brightness, contrast, saturation



5 Comments

  • Share/Bookmark




Sorry for the late update. I had lots of work & devs recently and there’re still more to go.
But, still i’ve found some spare time to post this little (hope useful) utility class.


////////////////////////////////////////////////////////////////////////////////
//
//  Copyright 2010 Julius Loa | jloa@chargedweb.com
//  All Rights Reserved.
//  license: GNU {http://www.opensource.org/licenses/gpl-2.0.php}
//  notice: just keep the header plz
//
////////////////////////////////////////////////////////////////////////////////

package com.chargedweb.utils
{
	import flash.filters.ColorMatrixFilter;

	/**
	 * Matrix utility class
	 * @version 1.0
	 * so far added:
	 * - brightness
	 * - contrast
	 * - saturation
	 */
	public class MatrixUtil
	{
		/**
		 * sets brightness value available are -100 ~ 100 @default is 0
		 * @param 		value:int	brightness value
		 * @return		ColorMatrixFilter
		 */
		public static function setBrightness(value:Number):ColorMatrixFilter
		{
			value = value*(255/250);

			var m:Array = new Array();
    		m = m.concat([1, 0, 0, 0, value]);	// red
   		 	m = m.concat([0, 1, 0, 0, value]);	// green
   		 	m = m.concat([0, 0, 1, 0, value]);	// blue
    		m = m.concat([0, 0, 0, 1, 0]);		// alpha

    		return new ColorMatrixFilter(m);
		}

		/**
		 * sets contrast value available are -100 ~ 100 @default is 0
		 * @param 		value:int	contrast value
		 * @return		ColorMatrixFilter
		 */
		public static function setContrast(value:Number):ColorMatrixFilter
		{
			value /= 100;
			var s: Number = value + 1;
    		var o : Number = 128 * (1 - s);

			var m:Array = new Array();
			m = m.concat([s, 0, 0, 0, o]);	// red
			m = m.concat([0, s, 0, 0, o]);	// green
			m = m.concat([0, 0, s, 0, o]);	// blue
			m = m.concat([0, 0, 0, 1, 0]);	// alpha

			return new ColorMatrixFilter(m);
		}

		/**
		 * sets saturation value available are -100 ~ 100 @default is 0
		 * @param 		value:int	saturation value
		 * @return		ColorMatrixFilter
		 */
		public static function setSaturation(value:Number):ColorMatrixFilter
		{
			const lumaR:Number = 0.212671;
    		const lumaG:Number = 0.71516;
    		const lumaB:Number = 0.072169;

			var v:Number = (value/100) + 1;
			var i:Number = (1 - v);
   		 	var r:Number = (i * lumaR);
    		var g:Number = (i * lumaG);
    		var b:Number = (i * lumaB);

			var m:Array = new Array();
			m = m.concat([(r + v), g, b, 0, 0]);	// red
			m = m.concat([r, (g + v), b, 0, 0]);	// green
			m = m.concat([r, g, (b + v), 0, 0]);	// blue
			m = m.concat([0, 0, 0, 1, 0]);			// alpha

 			return new ColorMatrixFilter(m);
		}
	}
}

And the sample usage code:


import com.chargedweb.utils.MatrixUtil;

var bmp:Bitmap = new Bitmap(new Image(0,0));
addChild(bmp);
bmp.filters = [MatrixUtil.setBrightness(20),
			   MatrixUtil.setContrast(20),
			   MatrixUtil.setSaturation(-100)];

Fast black’n'white trick



1 Comment

  • Share/Bookmark




Sometimes i need a fast black’n'white saturation method and this one’s the shortest i presume.
I can’t remember whose this method originally was, but it’s nice.


private var rc:Number = 1/3, gc:Number = 1/3, bc:Number = 1/3;
private var sf:ColorMatrixFilter = new ColorMatrixFilter([rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, rc, gc, bc, 0, 0, 0, 0, 0, 1, 0]);

private function blackAndWhite(target:UIComponent, enabled:Boolean):void
{
   if(enabled) target.filters = [sf];
   else target.filters = [];
}

Flex CursorManager vs jloa



No Comments

  • Share/Bookmark




So there i was, trying to build my latest application which involved run-time cursor loading/usage.
I knew that the flex sdk had that static CursorManager class (i.e. mx.core.CursorManager), but as i looked up the manual – there’s no loadIcon() method, only setters that require class references which is sooooo unhandy if u deal with run-time loading assets (like i do).
So, i got myself some coffee, opened the eclipse and after some time spend – eureka!
I came to this slight solution which besides is elementary. :)

Read more

Skype status icon class



No Comments

  • Share/Bookmark






Today i had some free time to make a skype status icon class (don’t know who’d need that).
Anyways, it’s a simple class that connects to the skype service, retrieves the status code for a specified account name and displays the status icon. All parameters customizable. I’ve even created a sample page with a php proxy (see the archive)

Download SkypeStatusIcon sources

Flex Button icon load failed?



No Comments

  • Share/Bookmark




The only thing why flex sucks is that components can’t actually load icons (Button class for example) i thought.

Yes, but that was until i found Ben Stucki‘s slight solution to this issue.
He wrote a nice utility class called IconUtility which creates BitmapAssets at run-time. This class is huge.
Sample button icon load up code:


var myBtn:Button = new Button();

myBtn.label = "press me";
myBtn.setStyle("icon", IconUtility.getClass(myBtn, "assets/my_icon.png"));
addChild(myBtn);

IconUtility Component for Dynamic Run-Time Icons

Download: IconUtility Class (302 kb)

Older Entries