OBO_Accordion: Another Scripted UI “Widget”

Long ago, I created a package for handling “Complex Buttons” – that is buttons that remain selected until another button in the same “group” is selected. In a nutshell, the developer creates a class that describes visual states of his button and implements the interface IComplexButton. These classes are wrapped inside a ComplexButton instance. Several of these ComplexButton instances are then passed to a Menu class (which is finally added to the display list) which handles all mouse interaction (rollovers, dragovers, clicks, release outsides, etc) and maintains button state. For more information and some downloads, check out this old post over at Kirupa.com.

Why do I blather on about all this, you ask? Well, when I was trying to create an Accordion widget to use in my ASDoc Gui (post before this), I thought this little Complex Button “framework” (for lack of better term) would be just the way to go. Well, I was right and I was wrong. It turns out that the way I had written the ComplexButton class it would not work for an accordion (for starters, it disables mouse communication with its children – something you definitely look for in an accordion. It just went downhill from there.). Well after an hour or two of mucking about with the ComplexButton class, I finally just copied and pasted the whole schmeer into a new .as file, deleted what I didn’t want and added what I did. This is a horrible horrible coding practice. It should have been extended, encapsulated, or something. Instead I now have two nearly identical classes with nearly identical function - ComplexButton and ComplexAccordionButton - residing in the same package. Ouch. Lesson learned: ALWAYS design with extension in mind. It’s not just something you read in all the hot titles.

Anyway, that coding architecture faux pas aside, I now have a very nice Accordion widget added to my ui package that I shared earlier. A straight out of the box, no frills version looks something like this:

And with a simple dose of customization, you could make something like this:

Both of these examples come from this simple script (just uncomment/recomment the lines for the basic or fancy accordion):

package {
 
	import com.onebyonedesign.ui.OBO_Accordion;
	import flash.display.Sprite;
	import flash.filters.DropShadowFilter;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
 
	/**
	* Example usage of OBO_Accordion class
	* @author Devon O.
	*/
 
	public class AccordionTest extends Sprite {
 
		private var _accordion:OBO_Accordion;
 
		public function AccordionTest():void {
			var content1:Sprite = createContent();
			var content2:Sprite = createContent();
			var content3:Sprite = createContent();
 
			var accordionInfo:Array = new Array( { label:"Content One", content:content1 }, { label:"Content Two", content:content2 }, { label:"Content Three", content:content3 } );
 
			// basic accordion
			_accordion = new OBO_Accordion(accordionInfo);
 
			// fancy pants accordion
			//_accordion = new OBO_Accordion(accordionInfo, 200, 350, 20, 0x4B4B4B, 0x292929, "_typewriter", 11, 0xFFFFFF, true);
			//_accordion.filters = [new DropShadowFilter()];
 
			_accordion.x = int(stage.stageWidth * .5 - _accordion.width * .5);
			_accordion.y = int(stage.stageHeight * .5 - _accordion.height * .5);
 
			addChild(_accordion);
 
			// getItemAt() method allows access to Accordion content
			_accordion.getItemAt(0).getChildByName("textField").text = "This is text added after accordion creation.";
		}
 
		/**
		 * Just creates a randomly colored sprite as test content
		 */
		private function createContent():Sprite {
			var content:Sprite = new Sprite();
			content.graphics.beginFill(Math.random() * 0xFFFFFF);
			content.graphics.drawRect(0, 0, 350, 200);
			content.graphics.endFill();
 
			var tf:TextField = new TextField();
			tf.autoSize = TextFieldAutoSize.LEFT;
			tf.x = tf.y = 25;
			tf.name = "textField";
			tf.text = "This is text added during content creation.";
 
			content.addChild(tf);
 
			return content;
		}
	}
}

If interested, you can download the new ui package here. It includes all the classes from the last post, plus the accordion, plus the complex button package, plus documentation.

Incidentally, the Accordion requires Tweener for its animation, but you’re welcome to re-write the AccordionSection.as file to use whatever tweening package you’re most comfortable with. Me, I like Tweener.

Hope this may help some folks out…

10 Comments »

  1. Dave Gillem says:

    Nice work, thanks for the download!

  2. Skye says:

    Howdy, Devon. Nice job, once again, with the OBO components. I’m using the accordion here and was wondering if you could provide me with some direction on how to go about having all the panels initially closed instead of the first one starting opened. I don’t need a detailed tutorial, but perhaps you could point me in the right direction or pass along some general thoughts on how you’d approach that.

    I’ve been digging through the source but haven’t yet discovered a solution. Your insight is appreciated.

    Cheers!

  3. Devon O. says:

    Hey Skye,

    In the OBO_Accordion.as class file, in the init() method, comment out the line _menu.selected=”0″; and see if that works for you. In any case, thank you for the comments..

    d.

  4. Skye says:

    How embarrassingly obvious. :)

    Many thanks, Devon. If you pass me your email I’ll send you a link off-blog to how I’m using it on a site in-progress. You might like a couple of the tiny visual mods I made.

    Best,

    Skye

  5. Devon O. says:

    I’d definitely like to see it. You can just use webmaster AT onebyonedesign DOT com to send a link. I’m glad that helped out…

  6. Jim McClure says:

    Really nice work Devon O! I’m sorry this is a little off piste for this post but I couldn’t find your original post on ComplexButtons.
    I’m trying to create an accordion menu which is almost exactly the same as this except it uses XML to create the buttons. To separate Main from Sub I’ve labeled them in the name property. Now all I need to do is manage their various rollover states using your ComplexButton class. This should be simple right!? : ) but I’m having trouble getting it to work. The sub button event triggers the main button event. I feel like I’m nearly there but would you mind helping me over the last hurdle?

  7. Devon O. says:

    Hey jim, without seeing any script, it looks as though you need to set something’s mouseEnabled or mouseChildren property to false. Just kind of a shot in the dark, but that’s what the symptoms sound like…

  8. Jim McClure says:

    Hey Devon O, Thanks for your reply. Yeah I tried setting mouseChildren = true it was set to false in the ComplexButton Class, this does help activate rollover conditions on the subMenus but it doesn’t manage the selected status. It seems like the mask I’ve used to reveal the subMenus which are attached to a parent Main Menu clip is catching both Main & Sub mouseEvents. I’ve tried setting the mask to mouseEnabled = false but it has no effect. I think it’s inheriting this property from the parent clip. I’ve also been trying to name each clip Main & Sub then filtering events accordingly in your class but I’m finding this mask still catches my mouseevents. hmm, I’m stumped! I’m happy to send you the code but there is a lot of it! I dunno if it would be easier for you to try a similar test at your end?

  9. James says:

    Is this available in AS2 and/or an code implementation example available to integrate it?

  10. Devon O. says:

    Hey James,

    It’s not available in AS2, but there is a usage example in the asdocs. http://www.onebyonedesign.com/flash/ui/docs/

RSS feed for comments on this post. / TrackBack URI

Leave a Reply

Devon O. Wolfgang

Technical Reviewer of “The Essential Guide to Flash CS4 AIR Development”

Contributing Author of “Flash AS3 for Interactive Agencies”

Senior Flash Engineer PopCap Games, International Ltd.

Portfolio

Santabot: A Unity3D Flash Game


All right, so a Christmas game like “Santabot vs. The Flying Saucers from Mars” may be a day late[...]

Magnify – a jQuery Plugin


Let me begin by saying right up front, I have not given up on Flash[...]

It’s a Starling Halloween


Getting some practice for the upcoming Zombie Apocalypse[...]

Getting Started with Proscenium

So I had a chance this weekend to sit down and play around with Adobe’s new 3D framework for Stage3D, Proscenium, and thought I’d share a few of the results (a word of caution, there are no preloaders for any of the examples and may load a bit slowly). The first shows some reflections and [...]

Particle Editor for Starling Framework

An in-browser particle editor for the Starling 2D Framework for Flash Player 11.

So Long and Thanks for all the Flash on the Beach

So, another Flash on the Beach has just has just drawn to a close[...]

Game Development Tips from the Trenches of PopCap

Well, this is a post that’s a bit overdue, but, thanks to a well timed bank holiday, I finally had the opportunity to sit down and type up what I’ve been meaning to for some time now…

Old Skool Demoscene FX as 3D Textures

Many moons ago, I got the idea to create some demoscene plane deformation effects in Flash based on the formulas found here: http://www.iquilezles.org/www/articles/deform/deform.htm. I posted my less than desired results up on wonderfl. Thankfully, fellow wonderfl user, Hasufel, forked my attempt and optimized the hell out of it coming up with this. Well, today, for no [...]

Making The Gaming Scene (A Change in Careers)

And for my second blog post of the day, a much more personal note. After about three years of working with, what I would consider as objectively as possible, the best digital agency in Ireland, vStream Digital Media, I have made the immense career decision to leave the agency world and enter the arena of [...]

Feeling Lucky?

Images to dice, kick ascii style [...]

Adventures in Playbook Land

Adventures in Playbook Land


Now that the ordeal is over, I thought I’d take the time to sit down and share my account of what it was like to develop a Blackberry Playbook application using the Adobe Flex SDK[...]

Flash

Draw it for Me

So many ideas – so little time….

Kinect Application Running in Dublin

So, on Friday I just wrapped up our latest project at vStream Digital Media, a Kinect powered flipbook that lets users flip through the hand written notebooks of Philip Lynott of Thin Lizzy. The app uses OpenNI, runs in Adobe AIR and is currently on display at a pretty bitchin’ Phil Lynott exhibition running in [...]

Beach Ball Kinect Party

So we finally got a Kinect camera hooked up to a pc at work and, while it doesn’t seem to be legal to use it for commercial projects (but, hey, I’m no lawyer), the boss asked me to get it figured out and come up with some ideas just in case it would be feasible [...]

Facebook and Flash – A Book Review

Now, I should begin by saying I absolutely hate building Facebook applications. And I build a lot of them at work. Every time I get the word from above that we’re doing another FB app, I just groan – both inwardly and out. It’s become a running joke of the office. Why do I dislike [...]

Multitouch Fluid Dynamics with AIR for Android and RTMFP

The other day I was having some fun playing around with Eugene Zatepyakin’s (aka @inspirit) FluidSolverHD (Actionscript port of C++ fluid dynamics library, MSAFluid. Or is MSAFluid, the processing/java port of the C++ library? In any case it’s a very cool fluid dynamics thingamabob – the HD version using Alchemy). After a bit of tinkering, [...]