Adding New Shapes and Behaviors

An ABC manual consists of hyperlinked pages, each page of which consists of a collection of figures. The figures can have both a shape and behavior. Examples of shapes include polygons, arcs, rectangles and axes. Examples of behaviors include click, drag, and link. The shapes and behaviors in the default version of ABC provide many of the capabilities designers want in the development of a hypermedia manual.

However, there will be many applications which could make use of specialized shapes or behaviors. For example, the regular polygon shape was not originally in ABC but was designed by a student for use in a lesson. In addition to providing needed capabilities, creating a new shape saves development time and provides a consistent interface. In this chapter I will discuss how to develop a new shape and behavior. Be sure to review Graphics if you have not already done so.

Developing a new Shape

Suppose you wanted to create a regular polygon shape called RegPoly. Given n sides (3 or greater), RegPoly will draw a closed polygon of n equal length sides at the given position and size.

Creating the Shape Type

To create the RegPoly type you would follow these steps:
  1. Select Run/Type Manager from the Main programmer interface
  2. Select Type/New from the Type Manager
  3. Enter the following values in the Type/New dialogbox:
  4. Click on Apply to create
If you need to add new parts later, select Type/Edit in the Type Manager to change these values.
Note: Since RegPoly already exists in your version, don't attempt to create it again. However, you can review the code which is referenced below or edit it in ABC.

Adding Methods

Suggestion: A good way to develop a new type is to edit a method of a similar, existing type, use Method/New in the Type Manager to create the new method and copy the code from the old to the new method.
The responsibilities of a shape is to draw itself, get and retrieve user input data from its dialogbox (which you will create later), to resize itself on request and perform any other functions which are unique to the shape.

The primary role of the RegPoly shape is to compute a regular closed polygon with n sides which can be circumscribed in a square. This recomputation is done in resizeWidth:resizeHeight:. The width and height of the square is chosen to be the minimum of the supplied width and height. Then the vertices of the regular polygon are computed and put into the endpoints array. Drawing is done using drawIn: of Polygon.

To keep the user from being able to move or edit the vertices, drawHandlesIn:with: is overridden to display only the move and resize handles. Also, computeExtent is overridden since there is no reason to recompute the width and height. For polygons, computeExtent determines the maximum width and height of the polygon based on all its endpoints.

When a regular polygon is added to a page, the Page Manager will issue a "RegPoly new" message to create a new polygon. The new method is supplied to make the default polygon a closed pentagon.

Getting and Retrieving Settings

Whenever the Page Manager is active and the user clicks on a figure, the figure becomes selected. When the user clicks on Settings, the Page Manager will look for a dialogbox in the folder Widgets with the name of the shape type followed by "db". For example, if the selected figure were a RegPoly shape, the dialogbox named "regpolydb" would be found and displayed. Then getValues: would be called which would set the widgets in regpolydb to the proper values. Similarly, when the Apply button is clicked in regpolydb, the data from the dialogbox is copied back to the shape using setValues:.

When creating a new shape, you will need to create a new dialogbox in the Widgets folder and give it the proper name as described above.

Suggestion: Copy a dialogbox for a similar shape and then edit the copy, adding and deleting widgets as necessary.
See the section on Editing a View in Designing an Application to learn how to modify a dialogbox. Remember to name all of your widgets and use those names in getValues: and setValues:. Also be sure to click on the dialogbox and name it with the same name as you used to refer to it in the widgets folder.

Creating the Shape Bitmap

The Page Manager determines whether to make a shape available by finding a bitmap of the same name in the Bitmaps folder. Most shape types have bitmaps but types like Shape and Line were not intended to have visual counterparts and so have no associated bitmaps. Thus, you must create a bitmap if you want to make it available in the Page Manager.

To create a bitmap use the Bitmap Manager. Make it a 16x16 bitmap which is the default size. Make sure you store it in the Bitmaps folder.

Making the Shape Available

The first time you click on the Edit button in the Manual Manager, the Page Manager is started. For efficiency, the Page Manager looks for an array called /etc/shapes which contains the types of shapes to display. If it is not there, it will create the array and determine all the shapes which should be displayed. In order to make your new shape accessible to the Page Manager, delete the /etc/shapes array and then start the Manual Manager. The bitmap should appear in the Shapes window.

Making a New Behavior

The process for making a new behavior is similar to making a shape. Create a new subtype of Behavior and add methods similar to those in an existing behavior. Build a dialogbox to hold the values which should be edited and name it the same as the type, concatenated with "db". No bitmap is required for a behavior.

It will become available the next time you use the Page Manager.