Archive for the 'Components' category

Using MinimalComps’ Scrollbar

Dec 20 2010 Published by under ActionScript, Components

Send to Kindle

The scrollbar component in minimalcomps was never really meant to be used as a standalone component. This is not to say that it can’t be, it’s just that the interface doesn’t really have the same simplicity and ease of use as the other components. However, a few people have asked about it, so here’s an example of how to use it.

First we’ll need something to scroll. We’ll create a sprite with a bunch of random lines and give it a scrollrect so that it has more content than it can show.

var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xeeeeee);
sprite.graphics.drawRect(0, 0, 100, 500);
sprite.graphics.endFill();
sprite.graphics.lineStyle(0);
sprite.graphics.lineStyle(0);
for(var i:int = 0; i < 100; i++)
{
	sprite.graphics.lineTo(Math.random() * 100, Math.random() * 500);
}
sprite.scrollRect = new Rectangle(0, 0, 100, 100);
addChild(sprite);

I'm omitting all the imports. If you're attempting this, I assume you know how to import classes. If not, stop now and learn some AS3. :)

OK, now we'll create our scrollbar and put it just to the right of the sprite, and give it an event handler for the scroll event.

var scrollbar:VScrollBar = new VScrollBar(this, 100, 0, onScroll);
function onScroll(event:Event):void
{
}

This should look right, but won't do anything. We need to do 4 things to make it functional:

1. Set the thumb percent - i.e. how big the sliding button is, as a percentage of how big it could possibly be. By default it's 1.0, meaning it fills the whole area. You'll need to calculate this yourself, and it's generally the visible area of the thing you're scrolling, divided by the total size of it. In our case, we can see 100 pixels of the sprite, but it's height is 500. So we can say:

scrollbar.setThumbPercent(100 / sprite.height);

2. Set the maximum value of the slider. This is the most you want the object to scroll. Again, this takes come calculation. Generally, it's the total size minus the visible area. The sprite is 500 pixels high and we can see 100 pixels. So 500 - 100 = 400.

scrollbar.maximum = sprite.height - 100;

3. Customize how much you want the scrollbar to move when you click on the buttons or the back. The lineSize property controls how much it will move when you click on the up or down buttons, and the pageSize controls how much it moves when you click on the back. Generally lineSize is fine at 1, but you can bump it up to make things scroll more. And pageSize is usually the same as the visual area, so 100 in this case. In some cases you might want to make pageSize a bit less, so you have some overlap. This is up to you.

scrollbar.pageSize = 100;
scrollbar.lineSize = 1;

4. Finally, you have to implement the event handler for the scroll event to make use of the new scroll value. In this case, we're changing the scrollrect to make it's top value equal the value of the scrollbar:

function onScroll(event:Event):void
{
	sprite.scrollRect = new Rectangle(0, scrollbar.value, 100, 100);
}

See what I mean? Not exactly rocket science, but not exactly "minimal" either. But there you have it. If you need to use it, go for it. Here's all the code together:

import flash.display.Sprite;
import flash.geom.Rectangle;
import com.bit101.components.VScrollBar;
import flash.events.Event;

var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xeeeeee);
sprite.graphics.drawRect(0, 0, 100, 500);
sprite.graphics.endFill();
sprite.graphics.lineStyle(0);
for(var i:int = 0; i < 100; i++)
{
	sprite.graphics.lineTo(Math.random() * 100, Math.random() * 500);
}
sprite.scrollRect = new Rectangle(0, 0, 100, 100);
addChild(sprite);

var scrollbar:VScrollBar = new VScrollBar(this, 100, 0, onScroll);
scrollbar.setThumbPercent(100 / sprite.height);
scrollbar.maximum = sprite.height - 100;
scrollbar.pageSize = 100;
scrollbar.lineSize = 1;

function onScroll(event:Event):void
{
	sprite.scrollRect = new Rectangle(0, scrollbar.value, 100, 100);
}
Send to Kindle

5 responses so far

MinimalComps 0.9.6

Nov 07 2010 Published by under ActionScript, Components, Flash

Send to Kindle

It’s been a while, but I finally got around to doing some work on MinimalComps. I went through all the issues that people had entered in Google Code. Some were older and already handled. Some were requests for new features, which I’ve noted, but am not acting on just now. Several I could not reproduce and closed. But if you entered one of those and are still seeing an issue and can give reproduceable steps for it, please reopen it with those steps. And then there were a fair amount of real bugs. Many of these were related to the List and ComboBox controls. These wound up showing up several issues in lower level controls, down to PushButton. I think I have them pretty well cleaned up.

So, no new features, but you should find List and ComboBox work much better now. You can get the URL to the SVN repository, or download the SWC or the zipped source here: http://code.google.com/p/minimalcomps/

A couple of other things I want to note. First I want to acknowledge that the ComboBox is misnamed. It should be a Dropdown. A ComboBox COMBINES an editable field with a dropdown list. I’m not sure the best way to handle this. I’m thinking of just changing the name to Dropdown and then creating an empty ComboBox class that extends Dropdown just to ensure I don’t break existing stuff. Does that seem like a decent fix?

The other issue to address is that several people have been bugging me to move the repository over to GitHub. I’ve personally used Git and got to like it, but despite the zeal that converts express for it, I think SVN is a much more popular method of source control. Pretty much anyone these days knows how to use SVN, either by command line or via some client. Git does have a serious learning curve, even for those who have used SVN or CVS. A lot of people have not made the jump yet. I don’t want to limit people’s access to the source and I don’t want to try to maintain two repositories. So for the near future, I’m sticking with Google Code SVN.

Send to Kindle

10 responses so far

A few MinimalComp updates

Apr 16 2010 Published by under ActionScript, Components, Flash

Send to Kindle

Addressed all reported bugs and added a few graphical goodies.

Get Adobe Flash player

First is grids. The Panel now has a few new properties. Panel.showGrid turns on or off a grid drawn in the background. Panel.gridColor and Panel.gridSize let you control the color of the grid lines and how far apart they are. Grids also apply to the chart classes and work the same way.

Next is alternating rows for Lists and ComboBoxes. These now have an alternatingRow property which is false by default. Set it to true and every other row will be colored differently. You also have alternateColor, which along with defaultColor allows you to set the colors of the rows.

Finally, I didn’t like the way the Window’s title bar was inset. So it is no longer inset. And it now has a property called grips. This is a Shape object. It’s invisible by default, but if you set Window.grips.visible to true, you’ll see lines there that give it a bit of a tactile sense to the bar for dragging. I left it as a shape so if you want you can draw your own graphics in there if you don’t like the lines. Be warned though, it scales according to the width of the window, the size of the label, and whether or not there is a close button. I’m open for suggestions on how to make that all better.

Send to Kindle

15 responses so far

MinimalComps: New SWC, zipped source, updated site

Apr 11 2010 Published by under Components

Send to Kindle

Backlogged on this, but I got the new SWC and zipped source uploaded, including the new RangeSlider component (HRangeSlider and VRangeSlider, actually). Has a bunch of other bug fixes in there as well.

Also updated the site, as it had a nasty bug in the WheelMenu that caused a run time error, as several as you reported. This was fixed a while back, just needed to update the site. And the site now shows the RangeSlider as well.

Send to Kindle

5 responses so far

MinimalComps: RangeSlider

Apr 02 2010 Published by under ActionScript, Components

Send to Kindle

I know I’m supposed to stop making new components and clean things up for 1.0, but this got in my head and I had to bang it out. It’s basically a slider with two handles. You get a lowValue and a highValue. Good for specifying a range with a low and high boundary. I thought it was pretty important to have labels for the two values, but wasn’t sure the best way to do it. Finally came up with these sliding labels that match the position of each handle. They can be always on, always off, or just show up when you move the handles. You can also specify the position of the label.

Get Adobe Flash player

Shown here is the HRangeSlider. There’s also a VRangeSlider which works about as you’d suspect.

The code is checked into SVN. Will update the site, SWC, docs, and code download later, probably tomorrow.

Send to Kindle

10 responses so far

Minimal NumericStepper

Mar 27 2010 Published by under ActionScript, Components

Send to Kindle

I wasn’t planning on doing this before 1.0, but I needed one and put a couple of buttons and an input text together for the project I’m doing. Then I needed another one elsewhere in the project. So I extracted what I made, cleaned it up and here you go. :)

Get Adobe Flash player

The buttons are a bit different than other Numeric Steppers, but I kind of like them. You have max, min, value, step, labelPrecision, and of course a CHANGE event.

Enjoy

http://www.minimalcomps.com

Send to Kindle

11 responses so far

Custom List Items for MinimalComps List and ComboBox

Mar 26 2010 Published by under Components, Flash

Send to Kindle

I checked in some changes to List and ComboBox that allow you to create custom list items. Here’s an example:

Get Adobe Flash player

This is enabled through the listItemClass property on List and ComboBox. In it, you pass in a class that must extend the ListItem class. Here’s the code for this example:

package
{
	import com.bit101.components.*;

	import flash.display.Sprite;
	import flash.events.Event;

	public class Playground extends Sprite
	{
		private var list:List;
		private var label:Label;

		public function Playground()
		{
			Component.initStage(stage);

			label = new Label(this, 120, 10);

			list = new List(this, 10, 10);
			list.listItemClass = CheckListItem;
			for(var i:int = 0; i < 20; i++)
			{
				list.addItem({label:"item " + i, checked:i % 2 == 0});
			}
			list.addEventListener(Event.SELECT, onSelect);
		}

		protected function onSelect(event:Event):void
		{
			label.text = list.selectedItem.label + ". checked: " + list.selectedItem.checked;
		}
	}
}

Here, I'm saying list.listItemClass = CheckListItem; We'll see the CheckListItem class shortly. As I add items, I give each one a label property and a checked property, which is true or false. And when an item is selected, I read these properties and assign them to a label.

So here's my custom list item class:

package
{
	import com.bit101.components.CheckBox;
	import com.bit101.components.ListItem;

	import flash.display.DisplayObjectContainer;
	import flash.events.Event;

	public class CheckListItem extends ListItem
	{
		protected var _checkBox:CheckBox;

		public function CheckListItem(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0, data:Object = null)
		{
			super(parent, xpos, ypos, data);
		}

		protected override function addChildren():void
		{
			super.addChildren();
			_checkBox = new CheckBox(this, 5, 5, "", onCheck);
			_label.visible = false
		}

		public override function draw():void
		{
			super.draw();
			if(_data is String)
			{
				_checkBox.label = _data as String;
			}
			else if(_data.label is String)
			{
				_checkBox.label = _data.label;
			}
			else
			{
				_checkBox.label = _data.toString();
			}
			if(_data.checked != null)
			{
				_checkBox.selected = _data.checked;
			}
		}

		protected function onCheck(event:Event):void
		{
			_data.checked = _checkBox.selected;
		}
	}
}

In the addChildren method, I create a CheckBox and turn the existing Label invisible.

In the draw method, I assign the _data.label value to the CheckBox and use _data.checked to set whether it is checked or not. As you can see, there is some additional testing for various possible label values that I basically just copied over from ListItem. When the CheckBox is clicked, it sets _data.checked to its own checked state.

Since data is typed as a generic object, you can pass any property to it. For instance, if you want to display an icon or image, you might pass in an icon class or a url and use that within your list item class to instantiate or load a bitmap and display it.

One thing to note is that any display objects you create in the list item may become the target of mouse click events. In checking for a list selection, the target must be the list item itself, not a child of the list item. For example, notice that when you click the CheckBox, it checks/unchecks the CheckBox, but does not select the list item. You have to click somewhere off the CheckBox to select the item. So if you have any display objects that may be capturing click events, you can set them to mouseEnabled = false, and maybe mouseChildren = false, to have them ignore clicks. The click target will then be the list item itself instead of the child. If you have children that need to be interactive, like the CheckBox in this example, make sure you leave enough space to allow for a selection-causing click as well.

Send to Kindle

9 responses so far

MinimalComps Fixes

Mar 25 2010 Published by under ActionScript, Components

Send to Kindle

As I mentioned the other day, I’m going to spend the next couple of weeks addressing any bugs or enhancements to the components. In the last few days, here are some of the things I’ve added and fixed:

- exposed numVisibleItems on ComboBox
- fix for stage click to close ComboBox
- exposed items array for List and ComboBox
- more accurate leap year calculation in Calendar
- Sliders now dispatch a change event when you set their values programmatically
- HBox and VBox now calculate their own width and height based on their contents, so you can now nest these
- Knob has 3 modes of reacting to the mouse: vertical, horizontal, and rotate
- fixed some visual jumpiness that occurred when resizing components
- made internal event listeners use weak references
- on text components, if text is set to null, it will be changed to an empty string rather than throwing an error

If you have any other bugs or enhancements, please feel free to submit them: http://code.google.com/p/minimalcomps/issues/list. I’m paying close attention to this list and addressing each item as soon as possible. Again, I’m not adding any new components in the near future, just addressing bugs and enhancement requests to existing ones.

Send to Kindle

No responses yet

And finally, Minimal ComboBox

Mar 23 2010 Published by under ActionScript, Components, Flash

Send to Kindle

Get Adobe Flash player

Has most of the same properties / methods as List. And adds:

openPosition – “top”, “bottom”, or ComboBox.TOP, ComboBox.BOTTOM. Defaults to bottom.
defaultLabel – what shows if nothing is selected. Default to an empty string.

As mentioned yesterday, this is the last component I am going to add for a while. I’m going to concentrate on cleaning things up, fixing any bugs that come up, considering any needed enhancements to existing components, and then calling it 1.0.

http://www.minimalcomps.com

Send to Kindle

12 responses so far

Font issue in MinimalComps

Mar 22 2010 Published by under Components, Flash

Send to Kindle

It was brought to my attention today that there was a possible issue in the MinimalComps regarding font embedding. If you were to compile the component source with the Flex 4 SDK, no labels on any components would show up. If you use the precompiled SWC, you’d be fine, as this was compiled with the 3.4 SDK. But if you were using the zipped source checked out the source with SVN and compiled under SDK 4.x, you’d hit this problem.

With a quick bit of research, I learned that with Flex 4, there is a new type of embedding for fonts. Traditional font embedding is called DefineFont3. The new stuff is called DefineFont4. Let’s call them DF3 and DF4 for short here. It turns out that the new text components in Flex 4 require DF4. Yet, the old fashioned TextField requires DF3. Guess what the default is? Yes, DF4. This means that using the exact same syntax for embedding will now make embedded fonts in TextFields not work. The solution is, when embedding a font, to tell it to NOT use DF4. The original syntax for this was to add cff="false" to the Embed tag. The current syntax is embedAsCFF="false". So, in that sense, this is an easy fix.

With FlashBuilder 4 and Flex 4 being officially released today, I decided to upgrade the SWC to use the Flex 4 SDK for compiling. I changed the Embed tag as above. So now, compiling in FlashBuilder 4 or otherwise using the 4.x SDK should work fine. Of course, this creates the opposite problem. If you are compiling in Flex Builder 3 or a 3.x SDK, it will not recognize embedAsCFF in the Embed tag and your code will not compile. I looked into conditional compiling, but as far as I can tell, that’s not going to be a real solution in this case unless I can inject a compiler definition. But since I’m just distributing source, not a build system, I don’t see a way to do that.

So the two choices are:

A. Don’t use embedAsCFF and compile the SWC under 3.x.
B. Use embedAsCFF and compile the SWC under 4.x.

Either way, anyone using the other SDK will have a problem. However, if I go with A, the people using 4.x will not see the font show up, but will get no useful error of any kind. If I go with B, the people using 3.x will get a compiler error pointing them to the Embed statement in Component.as. That is much more useful.

So, if you look at Component.as, you will now see this:

    // NOTE: Flex 4 introduces DefineFont4, which is used by default and does not work in native text fields.
    // Use the embedAsCFF="false" param to switch back to DefineFont4. In earlier Flex 4 SDKs this was cff="false".
    // So if you are using the Flex 3.x sdk compiler, switch the embed statment below to expose the correct version.

    // Flex 4.x sdk:
    [Embed(source="/assets/pf_ronda_seven.ttf", embedAsCFF="false", fontName="PF Ronda Seven", mimeType="application/x-font")]
    // Flex 3.x sdk:
//  [Embed(source="/assets/pf_ronda_seven.ttf", fontName="PF Ronda Seven", mimeType="application/x-font")]
    private var Ronda:Class;

So, if you are using 4.x, all should be well. If you are using 3.x, you’ll get an error which should take you right to this exact point. Hopefully you’ll be able to figure things out from my detailed explanation and fix it.

Again, if you are using the precompiled SWC, it shouldn’t matter what SDK you are using, at the SWC is all wrapped up in its own little package. This will only be a problem if you are linking to the source classes themselves.

I know this whole thing presented a problem for Adobe, but I feel they broke some fundamental rules here. They broke a commonly used API. If you have function foo() that makes ThingX’s, you don’t just suddenly change it so it makes ThingY’s instead, and add a new parameter that you can use to force it to make ThingX’s again. You either make a foo2() function or allow foo to take an optional parameter to force it to make the new ThingY’s. You don’t break existing code. Anyway, that’s what we’re stuck with, and I did the best I could with it.

Send to Kindle

18 responses so far

« Newer posts Older posts »