Graphics Reference
In-Depth Information
5.2.1
The Model
The model is the application state and thus this is the core of our program. When
describing approaches to designing an event-driven program in Section 1.6, the
first two points mentioned were:
1. define the application state;
2. describe how a user changes this application state.
These two points are the guidelines for designing the model component. In
an object-oriented environment, the model component can be implemented as
classes, and the state of the application can be implemented as instance variables,
with “how a user changes this application state” implemented as methods of the
classes.
Listing 5.1 shows that the instance variables representing the state are typi-
cally private to the model component. The
CBall
class is basically a circle that
can travel based on a defined velocity. Listing 5.1 also shows the four categories
of methods that a typical model component must support.
1.
Application state inquiries.
These are functions that return the applica-
tion state. These functions are important for maintaining up-to-date GUI
elements (e.g., echo number of balls on screen).
2.
Application state changes from user events.
These are functions that
change the application state according to a user's input actions. Notice
that the function names should reflect the functionality (e.g.,
CreateHero
Ball()
) and not the user event actions (e.g.,
ServiceLMBDown()
). It is
common for a group of functions to support a defined finite state transition.
For example,
CreateHeroBall()
,
ResizeHeroBall()
,and
AddHeroBall
ToWorld()
implement the finite state diagram of Figure 2.3.
3.
Application state changes from application (timer) events.
This is a
function that updates the application state resulting from purposeful and
usually synchronous application timer events. For the ball-shooting pro-
gram, we update all of the velocities, displace the balls' positions by the
updated velocities, and compute ball-to-ball collisions, as well as remove
off-screen balls.
4.
Application state visualization.
This is a function that knows how to draw
the application state (e.g., drawing the necessary number of circles at the
corresponding positions). It is expected that a view component will initial-
ize appropriate regions on the application window, set up transformations,
and invoke this function to draw into the initialized region.
Search WWH ::
Custom Search