Section 4

Better Cursor Management

One of the things I don't like about the current implementation of our LaserGame morph is that the cursor arrow gets in the way when your moving around over a mirror cell. The hint arrows draw themselves on the cell but in some situations it's difficult to see them because the mirror cell is relatively small and the cursor/arrow is almost the same size.

Doing something to improve the cursor visibility while it's over the mirror cell and while these hint arrows are visible would be a good thing.

We're going to create another form. It's a simple form and will provide a simple cross-hair. We will use this new cross-hair form as our cursor while hovering over the mirror cell hint drawings.

Add some new class methods to the LaserGameForms class.

drawCrossHair
    "LaserGameForms initializeCachedForms"
    | form pen startPoint endPoint line lineLength inset |
    lineLength := 10.
    form := Form extent: CellRenderer cellExtent depth: 32.
    form fillColor: Color transparent.
    pen := Form extent: 1@1 depth: 1.
    pen fillColor: Color black.
    inset := ((form width - lineLength) // 2)@((form height - lineLength) // 2).
    startPoint := (inset x)@(form height // 2).
    endPoint := (form width - (inset x))@(form height // 2).
    line := Line from: startPoint to: endPoint withForm: pen.
    line displayOn: form.
    startPoint := (form width // 2)@(inset y).
    endPoint := (form width // 2)@(form height - (inset y)).
    line := Line from: startPoint to: endPoint withForm: pen.
    line displayOn: form.
    ^form

The class method #drawCrossHair creates a small form with a small black cross hair in its center. We will now modify the #initializeCachedForms class method to use this new drawing method to create the #crossHair cached form.

initializeCachedForms
    "LaserGameForms initializeCachedForms"
    | form |
    CachedForms := Dictionary new.
    form := self arrowFormFromPointsArray: self northArrowPoints.
    CachedForms at: #north put: form.
    form := self arrowFormFromPointsArray: self eastArrowPoints.
    CachedForms at: #east put: form.
    form := self arrowFormFromPointsArray: self southArrowPoints.
    CachedForms at: #south put: form.
    form := self arrowFormFromPointsArray: self westArrowPoints.
    CachedForms at: #west put: form.
    form := self drawCounterClockwiseArrow.
    CachedForms at: #counterClockwise put: form.
    form := self drawClockwiseArrow.
    CachedForms at: #clockwise put: form.
    form := self drawCrossHair.
    CachedForms at: #crossHair put: form.

Next we add a class method to answer the cached cross-hair form.

crossHair
    CachedForms isNil ifTrue: [self initializeCachedForms].
    ^CachedForms at: #crossHair

We're going to add one more class method to the LaserGameForms class. This is an #initialize class method. Here's why this is important. As we extend the code in the #initializeCachedForms method, remember that we need to be certain to re-initialize those cached forms otherwise our new forms will not be present in the cached dictionary. The trick is that when we load a new version of code, either via a file-in of a Change Set or a project load from Monticello, we need to cause these cached forms to get re-initialized. When a package loads into memory all the classes loaded have any #initialize class method executed right after the load or install. In this way we can ensure that our cached forms are reset if the new version of code were to change the form definitions in any way. Here's our new #initialize class method.

initialize
    super initialize.
    LaserGameForms initializeCachedForms

Our next step is to figure out how to use this new cross-hair form.

Index Page Next Page

Copyright © 2007, 2008, 2009, 2010 Stephan B Wessels    stevewessels@me.com