Graphics Reference
In-Depth Information
Listing 5.2 shows the
CApplicationView
class supporting the two main func-
tionalities of a view component: coordinate space transformation and initial-
ization for redraw. As discussed earlier, the controller is responsible for call-
ing the
HardwareToModelXform()
function to communicate user input points to
the model component. The
CUIDrawArea
class is introduced to encapsulate the
highly API-dependent device initialization and drawing procedures.
5.2.3
The Controllers
We can improve the solution of Listing 1.7 to better support the specified function-
ality of the ball-shooting program. Recall that the application window depicted in
Figure I.2 has two distinct regions for interpreting events: the upper application
drawing area where mouse button events are associated with defining/selecting the
HeroBall
and the lower GUI element area where mouse button events on the GUI
elements have different meanings (e.g., mouse button events on the slider bars
generate
SliderBarChange
events). We also notice that the upper application
drawing area is the exact same area where the
CApplicationView
must direct
the drawings of the
CApplicationModel
state. Listing 5.3 introduces two types
of controller classes: a
CViewController
and the
CMainAppController
. Each
controller class is dedicated to receiving input events from the corresponding re-
gion on the application window. The
CViewController
creates a
CApplication
View
during initialization such that the view can be tightly paired for drawing of
the
CApplicationModel
state in the same area. In addition, the
CViewControl
ler
class also defines the appropriate mouse event service routines to support the
interaction with the
HeroBall
.The
CMainAppController
is meant to contain
GUI elements for interacting with the application state.
The bottom of Listing 5.3 illustrates that the GUI API
MainEventLoop()
will still call the
SystemInitialization()
function to initialize the application.
In this case, we create one instance each of
CViewController
and
CMainApp
Controller
.The
CViewController
is initialized to monitor mouse button events
in the drawing area of the application window (e.g., left mouse button click to de-
fine
HeroBall
), whereas the
CMainAppController
is initialized to monitor the
GUI element state changes (e.g., left mouse button dragging of a slider bar). No-
tice that the service of the timer event is global to the entire application and should
be defined in only one of the controllers (either one will do).
In practice, the GUI API
MainEventLoop()
dispatches
events to the con-
trollers based on the
context
of the event. The context of an event is typically
defined by the location of the mouse pointer or the current
focus
of the GUI
Search WWH ::
Custom Search