The use of invalidating and drawing is important when you have several factors that determine how an object is displayed on the screen, and those factors can change frequently and at any time, but you only want to recalculate the display of that object once per frame (screen render).
You're always going to have invalidate, draw, and drawNow functions. draw() will be your protected method that recalculates your display, drawNow() will be your public access to the draw() that immediately calls draw(), and invalidate will mark your GUI as needing to be recalculated which in turn will call draw() on the next render cycle.
If your GUI has a more complicated need for redrawing, such as having more than one draw methods where each has their own reasons for invalidation. Then you should refer to the way Flash does their UI. Go to
(flash install dir)\en\Configuration\Component Source\ActionScript 3.0\User Interface\fl\core\UIComponent.as
In this file you can see there is an invalidate function that takes a parameter for invalidation type.
public function invalidate(property:String=InvalidationType.ALL, callLater:Boolean=true):void {
This way you can control what gets redrawn based on what was invalidated.
Now your options here are either having your class extend UIComponent, or copy out the drawing and validation methods to your class.
However, if your GUI is much simpler, then you might not want the overhead of UIComponent, or the complexity of that approach to draw/invalidate.
My approach has been to use my IDraw interface which has the methods:
invalidate and drawNow
And then have the invalidate method call draw, but limited to only once per render cycle. (Usually one frame)
Interface:
package nbilyk.gui.drawing { public interface IDraw { function drawNow():void; function invalidate():void; } }
Implementation:
import nbilyk.utils.FunctionLimiter; // IDraw methods. function draw():void {} public function drawNow():void { draw(); } public function invalidate():void { var allowPass:Boolean = FunctionLimiter.limit(invalidate); if (!allowPass) return; draw(); }
An example using the FunctionLimiter Class
Thanks for reading!
| Attachment | Size |
|---|---|
| FunctionLimiter.as | 3.75 KB |