GEOS SDK TechDocs
|
|
5.2 What TicTac Does
|
5.4 TicTacBoard Specifics
The TicTac sample application is coded in two files: The first, tictac.gp , is the geode parameters file. The other, tictac.goc , contains all the code for objects in the application. The geode parameters file is similar to other .gp files and is not discussed in this section.
The application uses two different object trees, one for its generic UI and one for the game board and game pieces. The first consists of generic UI objects and the second of objects subclassed off
VisClass
and
VisContentClass
. The two object trees are diagrammed and described below.
The TicTac application begins with two standard generic objects common to all applications: GenApplication and GenPrimary. These two objects are described in detail in other sections and are not covered here, though their definitions are shown in TicTacApp and TicTacPrimary .
The primary object, TicTacPrimary, has two children. One, TicTacGameMenu, implements the game menu; the other, TicTacView, provides the window through with the user can interact with the visible tree.
Code Display 6-1 TicTacApp and TicTacPrimary
@start AppResource;
/* The AppResource resource block contains the TicTacApp object only. This
* object is in its own resource for performance purposes. */
@object GenApplicationClass TicTacApp = {
GI_visMoniker = list { TicTacTextMoniker }
GI_comp = TicTacPrimary;
gcnList(MANUFACTURER_ID_GEOWORKS, GAGCNLT_WINDOWS) = TicTacPrimary;
}
@visMoniker TicTacTextMoniker = "TicTacToe";
@end AppResource
@start Interface;
/* This is the Primary window of the application. It is not minimizable
* (since no icon is defined for it). It has two children: The View
* object and the Menu object. */
@object GenPrimaryClass TicTacPrimary = {
GI_comp = TicTacView, TicTacGameMenu;
ATTR_GEN_DISPLAY_NOT_MINIMIZABLE;
HINT_SIZE_WINDOW_AS_DESIRED;
}
This object is a standard GenInteraction object set up as a menu. The code for the entire menu (including the TicTacNewTrigger) is shown in The TicTac Game Menu and is heavily commented as to how each attribute is used.
The New Game trigger may be invoked at any time except when a piece is being moved. When a piece is being moved, that piece object has the "mouse grab," meaning that it will receive all mouse and pointer events until it releases the grab. When an object has the mouse grab, no mouse events may be sent to another object, and therefore the menu object can not be clicked on while the piece has the grab.
When the user presses the New Game trigger, the trigger sends its message,
MSG_TICTAC_NEW_GAME
, to the TicTacBoard object. The TicTacBoard object is the top object in the visible tree and, upon receipt of this message, resets the game board and notifies all the piece objects of the reset. This process is described below in TicTacBoard Specifics
.
Code Display 6-2 The TicTac Game Menu
/* The TicTacGameMenu object is the only menu of this application. Its only child * and only menu entry is the TicTacNewTrigger object. */
@start Interface; /* In the same resource block as TicTacPrimary. */
@object GenInteractionClass TicTacGameMenu = {
GI_visMoniker = "Game"; /* The name of the menu. */
GI_comp = @TicTacNewTrigger; /* The only menu item. */
GII_visibility = GIV_POPUP; /* This attribute indicates that this
* interaction is a menu rather than
* a dialog. */
}
@object GenTriggerClass TicTacNewTrigger = {
GI_visMoniker = "New Game"; /* The name of the menu item. */
GTI_destination = @TicTacBoard; /* The object to receive the "New Game"
* message: the game board object. */
GTI_actionMsg = MSG_TICTAC_NEW_GAME; /* The message to be sent when the trigger
* is pressed. */
}
@end Interface /* End of the Interface resource block */
The TicTacView object is a standard GenView set up to run the TicTacBoard object. The code for TicTacView is shown in The TicTacView Object and is heavily commented to show what each of the view's attributes is used for.
TicTacView's content object is TicTacBoard, the game board object. This means that any appropriate input events as well as all messages sent out by the view will be passed directly to the TicTacBoard object. The sizing attributes and the fact that the view is not marked GVDA_SCROLLABLE in either dimension makes sure the view sizes exactly to the game board's bounds.
A GenView object is necessary in every case where a visible object tree is used. The view not only displays the visible tree but also handles all clipping, scaling, scrolling, and sizing if any is desired (none is used in TicTac). It also takes and passes on any appropriate mouse or keyboard input events. Additionally, it interacts directly with its content object (in this case TicTacBoard) to determine proper geometry and sizing behavior of the content and the view.
This view also provides the background color of the game board, dark blue.
Code Display 6-3 The TicTacView Object
/* This object provides the window through which the user interacts with the
* visible object tree. This object communicates with the game board object (a
* subclass of VisContentClass) to coordinate drawing, clipping, sizing, and
* even input handling. */
@start Interface; /* In the same resource block as TicTacPrimary. */
@object GenViewClass TicTacView = {
GVI_content = @TicTacBoard; /* The content object of this view is the
* TicTacBoard object, the root object of the
* visible object tree. */
GVI_color = { C_BLUE, 0, 0, 0 }; /* The background color of this view
* should be dark blue. */
/* The horizontal attributes of this view set it to the same
* size as the game board, and the view is not scrollable. */
GVI_horizAttrs = @default | GVDA_NO_LARGER_THAN_CONTENT | GVDA_NO_SMALLER_THAN_CONTENT;
/* The vertical attributes of this view set it to the same size
* as the game board, and the view is not scrollable. */
GVI_vertAttrs = @default | GVDA_NO_LARGER_THAN_CONTENT
| GVDA_NO_SMALLER_THAN_CONTENT
| GVDA_KEEP_ASPECT_RATIO;
/* The user won't need to type anything, so there's no need for
* a floating keyboard. */
ATTR_GEN_VIEW_DOES_NOT_ACCEPT_TEXT_INPUT;
}
@end Interface /* End of the Interface resource block */
The visible tree contains eleven objects. One acts as TicTacView's content and is of
TicTacBoardClass
, a subclass of
VisContentClass
. The other ten are all game pieces of class
TicTacPieceClass
, a subclass of
VisClass
. Both the class definitions and the object definitions are given in TicTacBoardClass and TicTacPieceClass
.
All eleven of the visible objects remain on the screen and in the view during their entire existence. The game board and all its pieces are shown in the figure above; this illustration represents the basic configuration of the game board when the application first starts or when the user presses the "New Game" trigger in the "Game" menu.
The TicTacBoard object draws the border around the board and makes sure the view window sizes itself to the same size as the board. The board is 180 points (2.5 inches) in height and 270 points (3.75 inches) in width; these numbers are stored as the constants BOARD_HEIGHT and BOARD_WIDTH. TicTacBoard also draws the playing field--this consists of the four white lines on the left side of the game board. TicTacBoard's other main function is to ensure that all the children (game pieces) behave properly; it makes sure the child's bounds are on the game board when the piece is moved, and it notifies the game pieces when they must draw themselves due to a view exposure. Finally, TicTacBoard receives the "New Game" message from the Game menu; it then redraws the game board and notifies each of the game pieces that they should return to their initial locations.
Each of the game piece objects knows about its location and status. Each piece knows its initial location, current location, and proposed location (during a move). Every game piece is an instance of
TicTacPieceClass
. This class is shown in TicTacBoardClass and TicTacPieceClass
; it contains a number of instance data fields for these locations. It also has an instance data field indicating what type of piece (i.e. "box" or "ring") the object is.
The "box" objects are designated by having the value TTPT_BOX in their
TTP_pieceType
fields; the "ring" objects have TTPT_RING in that field. Both types of objects act and react in the same way to various events; the only difference is in their shape and color.
Code Display 6-4 TicTacBoardClass and TicTacPieceClass
/* The TicTacPieceTypes enumerated type lists the different types of game pieces a * particular piece object can be. In this game, a piece is either a "box" (gray * square) or a "ring" (light gray circle). */
typedef ByteEnum TicTacPieceTypes; #define TTPT_BOX 0 #define TTPT_RING 1
/*********************************************************************** * TicTacBoardClass * This class is a subclass of VisContentClass and provides the game board * for this application. It also manages all the children (piece objects). * Because it is a subclass of VisContentClass, it inherits all the instance * data fields and messages of that class. ***********************************************************************/
@class TicTacBoardClass, VisContentClass; /* this class is a subclass * of VisContentClass */
/* Message definitions for this class */
@message void MSG_TICTAC_NEW_GAME();
/* This message is sent by the New Game trigger in the Game menu
* when the user wants to reset the game. It is sent directly to
* the game board object and causes the board object first to
* send the "new game" message to each of its children and then
* to redraw the game board. */
@message Boolean MSG_TICTAC_VALIDATE_BOUNDS(word bottom, word right, word top, word left); /* This message is sent by a game piece that is being moved by the * user and is about to be set down. The four parameters are the * proposed new bounds of the moved piece; if they are within the * game board's limit, this message returns TRUE. If they are at * all outside the game board, this message returns FALSE. */
@endc
/* Declare the class in memory so the method table will be built. */ @classdecl TicTacBoardClass;
/*********************************************************************** * TicTacPieceClass * This class is a subclass of VisClass and provides all the functions * necessary for a game piece in this game. Because it is a subclass of * VisClass, it inherits all the instance data fields and messages of * that class. ***********************************************************************/
@class TicTacPieceClass, VisClass; /* this class is a subclass * of VisClass */
/* The instance data fields of this class: */
@instance TicTacPieceTypes TTP_pieceType;
/* TTP_pieceType defines whether the object of this class is
* a "box" or a "ring." */
@instance int TTP_vertPos; /* TTP_vertPos indicates the current y position of * the piece. This does not indicate the piece's actual * bounds but rather where its moving outline appears. */
@instance int TTP_horizPos; /* TTP_horizPos indicates the current x position of * the piece. This does not indicate the piece's actual * bounds but rather where its moving outline appears. */
@instance int TTP_origVertPos; /* TTP_origVertPos indicates the y position where this * piece should return when the New Game trigger is pushed * and the piece goes back to its original location. */
@instance int TTP_origHorizPos; /* TTP_origHorizPos indicates the x position where this * piece should return when the New Game trigger is pushed * and the piece goes back to its original location. */
@instance Boolean TTP_dragging; /* A flag indicating whether the user is in the process of dragging * the game piece around the board. */
/* Message definitions unique to this class. */
@message void MSG_PIECE_NEW_GAME();
/* This message notifies the piece object that the user has pushed *
* the New Game trigger and that the piece should return to its *
* original position on the board (the TTP_orig(Horiz/Vert)Pos *
* fields). */
@endc
/* Declare the class in memory so the method table will be built. */ @classdecl TicTacPieceClass;
GEOS SDK TechDocs
|
|
5.2 What TicTac Does
|
5.4 TicTacBoard Specifics