From the Couch 3 – DrawControl Part 2

In the last installment of From the Couch we created DrawControl – a simple drawing application. In this installment we’ll add selection and deletion of drawing items.

The basic idea is that you should be able to click on a drawing item (e.g. a rectangle) to “select” it. Or, alternately, you should be able to choose a rectangular portion of the screen in order to select all the items within that rectangle. As is fairly standard with drawing programs, we’ll indicate selected items by drawing “handles” on them. For now, these handles simply mark selected items, later, they will be used to resize items.

The first step is to add a new drawing tool (tracker) for selection. After refactoring DrawRectTracker so we can inherit most of its behavior, here is the resulting DrawSelectTracker:

The argument to New is a reference to the canvas rather than the item to be created as with the other trackers. Point and Rect simply delegate the work to the canvas and return False to tell DrawRectTracker that no new item was created. Rect uses Min and Max to ensure the points are the top left and bottom right. This is not done by DrawRectTracker because for in some cases the direction the rectangle was drawn is significant.

Since the order of .items is from “bottom” to “top”, and we want to select the top-most item when we click, SelectPoint loops through the items in reverse order (i.e. from top to bottom), stopping as soon as it finds an item. On the other hand, SelectRect selects all the matching points, so it can simply use foreach.

This code now depends on items implementing Contains(x, y) and Within(x1, y1, x2, y2) to determine if an item contains a point, or if it is within a rectangle. Rather than simply repetitively implementing these methods for all items, we’ll create default versions in CanvasItem:

In CanvasControl, after painting each item, we then paint the handles for selected items. This ensures that the handles will be painted on top and won’t be hidden by other items. Again, we’ll provide a default implementation in CanvasItem:

We draw the handles with the exclusive-or mode so we can see them regardless of the background.

Rather than hardwire the positions of the handles, we call an iterator method to loop through the handles. This type ofiterator method is common in Smalltalk and Ruby. We supply a default implementation in CanvasItem that puts handles at the corners and middle of the sides of the bounding rectangle:

This can be overridden by specific items if we want different handles. For example, CanvasLine just puts handles at the ends of the line:

ForeachHandle(handle)
{
handle(.x1, .y1)
handle(.x2, .y2)
}

Here is the result (all items selected):

The only problem is that we can’t do anything with the selected items! Since moving and resizing are a little too involved for this installment, we’ll just implement delete, via the delete key. The first step is to catch the keyboard message in DrawCanvasControl:

KEYUP(wParam)
{
if (wParam is VK.DELETE)
.DeleteSelected()
return 0
}

The only catch is that in order to receive keyboard messages the DrawCanvasControl has to have the focus, which will often be on the ChooseList instead. To remedy this, we can add a SetFocus to the mouse down handler:

One last point – because we added new required methods for canvas items, CanvasStockObject will no longer work properly. We could implement the required methods and simply delegate to the wrapped item, but a better solution is to simply delegate all unrecognized methods to the item. This way, we don’t have to worry about future new methods.

Default(@args)
{
return .item[args[0]](@+1args)
}

(This idiom is covered in the Idioms section in the Appendix of the Users Manual.)

If you want to try this code, you can download drawlib2.zip, extract drawlib.su, load it from the command line with:

suneido -load drawlib

Use the library from LibView, and then run DrawControl().

Note: This drawlib includes new versions of the Canvas and Draw code that is included in the April 22 release of stdlib. This new version of the code will be included in the next release.

Here are a few ideas for enhancements. I’m sure you can think of additional ones:

Add Select All (CTRL+A)

Support multiple selection with SHIFT or CTRL clicking. Hint: Use GetKeyState.

Allow “grouping” and “ungrouping” of multiple selected items. A set of items that has been “grouped” becomes a single composite item that can be selected, moved, etc. Hint: Create a CanvasGroup canvas item with a bounding box that covers all the included items. To group, replace the individual items with a CanvasGroup. To ungroup, replace the CanvasGroup with the individual items.

Improve Contains and Within for non-rectangular items such as ellipse and line. Hint: Windows has “region” functions that can be used with shapes like ellipse. For lines dust off your high school geometry to figure out the distance from a point to a line.

Implement a tracker to allow drawing polygons. Either implement BoundingRect or, more accurate versions of Contains and Within. Don’t forget to implement ForeachHandle to put handles at the vertices of the polygon.