|
Comments
Did you read today's front page stories & breaking news?
SYS-CON.TV
|
Director Multiple Undo
Power and flexibility
By: James Newton
Jan. 5, 2004 12:00 AM
Application users change their minds and make mistakes. The undo item in the Edit menu has been a standard feature in all applications since the dawn of menu-driven applications. undo buttons in toolbars are more recent. Most modern applications provide a multiple undo facility. This requires a second redo entry in the Edit menu, and a second button on the toolbar. Director MX is used by many developers to create professional-strength applications. This article shows you how to provide a multiple undo button set for a simple Paint program in Director MX. This includes undo and redo buttons, each with a popup menu that allows you to undo or redo a whole sequence of changes with one action. By the end of the article, beginner users of Director will be able to create a simple paint program with undo and redo buttons for the paint action. Users with a limited knowledge of Lingo will be able to extend the multiple undo feature to reset the choice of brush color. Intermediate users should be able to customize the Undo scripts for their own projects, and advanced users will have found a number of subtle tricks with standard Lingo features. Test-Driving the Multiple Undo Feature
![]() Try selecting a different brush or the eraser tool, and using that on your painting. (Note that a double-click on the eraser will erase the entire painting.) The exact same script members are used to undo any of these actions. All the script members you require are available in the tutorial download (see below). Now try changing the brush color by clicking on the color palette. Notice that the color swatch behind the eyedropper tool changes. Click on undo: the original color reappears. This action requires a different undo script, which you will have to create yourself by modifying a copy of an existing script. All You Need To Get Started If you already have Director 8.0 or later, you don't even need to download Director MX. Building the Paint Application To build the Paint application with its Multiple Undo feature, you simply need to drag the appropriate behaviors onto the appropriate sprites. Accept the default parameters except where noted in Table 1.
![]() That's it. You're ready to run your movie and test the Multiple Undo and Redo buttons. Feel free to add more brush shapes or to modify the Stage layout. How the Undo Feature Works property action -- #undo | #redo The action property is set when you drop the behavior on the sprite and choose Undo or Redo from the popup menu in the Behavior Parameters dialog. The Undo Broker is a movie script. In fact, it's somewhat more than a movie script, but I'll leave the discussion of its complexities until later. All you need to know for now is that the Undo Broker contains a record of the last undoable action that the user made. This record is saved as an instance of the Paint Line parent script, which contains instructions on how to undo the paint action and redo it again. When you tell the Undo Broker to Undo(), the Undo Broker forwards the message to the instance of the Paint Line script, which actually does all the work. Let's first look at what an instance is, then look at what work it needs to carry out in order to undo the painting of a line.
Parent Scripts and Instances You can breed as many offspring from a parent script as you like, and give them their different characteristics from the moment of their birth. They will all obey the same commands. Or to use Lingo terms: you can create as many instances of the script as you like, and provide them with property values in the new() handler. An instance is in fact an address in the computer's memory that stores a number of property names and the values that are associated with them. When you give a command to an instance of a parent script, the command handler "knows" which property values are stored by that instance at that particular address in memory. Depending on the values it holds, each instance will thus do something different, by following the same rules. Undoing a Paint Line Action
Given these starting points, undoing the paint action is simply a matter of swapping the current image inside the dirtied rectangle for the image that was there before. on undo(me) -------------------------- Redoing the change is just as simple: on redo(me) ------------------ Creating a Customized Undo Script To make the Paint_SetBrushColor() action undoable, you need to tell the Undo Broker what the color was before and after the change, and which script will handle undoing the change (see Code III). What purpose does the isUndo parameter serve? And how does the Select Color Undo script work? The answer to both of these questions is in the extracts from the Select Color Undo script shown in Code IV. The undo action simply calls the same Paint_SetBrushColor() handler you saw above. Since it passes a value of TRUE to isUndo, no new instance of the Select Color Undo script is created. As you can see, the principles are simple. You define the before and after state in the new() handler, and you provide a means of switching from one to the other (and vice versa) in the undo() and redo() handlers. I have removed the six crucial lines from the Show Brush Color behavior, and another six lines from the Select Color Undo behavior. All you need to do to make the feature work is to type in the lines shown in Code III. If you have followed the logic this far, you should be able to create your own Undo parent scripts for other undoable actions that you might need in your projects. Chained Lists and Upside-Down Ancestors An instance of the Undo Step script adopts an instance of the Paint Line or the Select Color Undo script as its ancestor. The Undo Step instances provide the links that chain the multiple undo actions together. Each instance contains a pointer to the previous undoable action. When an action is undone, its Undo Step instance becomes the first in a chain of actions to redo. Normally, ancestors are used the other way around: the generic script is the ancestor of the specific script. Here, I make the specific script (Select Color Undo, for instance) the ancestor of the generic Undo Step. The idea is to make the specific scripts as simple as possible. There is no need, for instance, to declare an ancestor property. By inverting standard practice, I make it easier for you to create your own undo action scripts. Brokers, or Self-Instantiating Movie Scripts The Undo Broker thus provides a movie-wide feature without using any global variables. You can add it to any of your movies without needing to worry about clashes with the names of your global variables. Not only that, but the action of the Undo Broker is limited to the movie itself. If you have several document windows open in your Paint application, each with its own Undo Broker, each paint document will perform its own undo actions independently.
A Professional-Strength Version The final version of this movie is CanvasPro.dir. This uses the OSControl xtra to provide platform-specific buttons, and to provide button menus that allow you to choose how many steps to undo or redo at a time. The behaviors have been adapted to work with OSControl sprites, but you can use them on your own buttons if you prefer. The behaviors in the CanvasPro.dir movie are more robust and end-user friendly than the ones you have been working with. To encourage you to use them in your own projects, they are organized into two separate external casts, which you can add to the Libs folder alongside your copy of Director. When you next open the Library Palette, the new behaviors will be there for you to use. Conclusion Reader Feedback: Page 1 of 1
Your Feedback
Latest Cloud Developer Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
|
SYS-CON Featured Whitepapers
Most Read This Week
Breaking Cloud Computing News
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||