MmgCfgFileEntry
MmgEvent
MmgEventHandler
Other Classes: MmgCfgFileEntry
The MmgCfgFileEntry class is used to hold data when reading and writing config files. It’s a small class, but I think it’s important to review it because class config automation is such a useful feature when setting up game screens. The MmgCfgFileEntry class is designed to hold numeric or string data in the form of key-value pairs loaded from a special text file, a class config file.
Enumerations
There is one enumeration that is associated with the MmgCfgFileEntry class, the CfgEntryType enumeration. It has three different values, TYPE_DOUBLE, TYPE_STRING, and NONE. The first two values are used to indicate the type of data stored in the config entry. The NONE value is used to indicate an uninitialized state.
Class Fields
MmgCfgFileEntry Class Fields 1
The first entry is an instance of the CfgEntryType enumeration we just reviewed. It is used to keep track of the type of data that is stored in this object. The next two class fields, number and str, are used to hold the actual data read in from the class config file. The last field is the name field. It’s used to store the key string associated with the line of data in the class config file.
Support Method Details
MmgCfgFileEntry Support Method Details 1
The first of the two entries for us to review is an API-to-string method. We’ve come across this before, so I won’t spend more time on it here. The compare method is a framework comparison method used to sort the config file entries before writing them.
Main Method Details
MmgCfgFileEntry Main Method Details 1
The first main method is a simple constructor that takes no arguments. This is very useful for quickly instantiating an object and then configuring it later on. The second constructor is a specialized constructor that takes an MmgCfgFileEntry as an argument and uses it to create a new, unique, class instance.
The Clone method is used to make a new unique copy of the class. It uses the specialized constructor to do this. The last entry in the preceding list is the ApiEquals method, which is an API-level comparison method. That wraps up the main methods I wanted to review. Let’s take a look at the class in action!
Demonstration: MmgCfgFileEntry Class
MmgCfgFileEntry Class Demonstration 1
The code snippet listed in the preceding is from the LoadResources method of the SplashScreen class from the MmgTestSpace library. The first line of code demonstrates loading a class config file. On lines 2–5, we can see how to access the data from a config file entry. Note that we know ahead of time what type of data to expect for the given key. On line 4, the display time field is set based on the config file entry’s numeric value.
The next block of example code on lines 7–12 demonstrates using a config file entry object to retrieve a string value. Notice on line 9 the config file entry object is located by its key and the str field is used. That completes the demonstration code I wanted to review for this class.
Other Classes: MmgEvent
The MmgEvent class and its counterpart, the MmgEventHandler class, create a generic event handling system that you can use to send and receive events. We’ll cover the MmgEventHandler class after this class review. The MmgEvent class has a simple implementation. The only thing I should mention is that there are some preset event IDs for handling menu navigation.
Static Class Members
MmgEvent Static Class Members 1
The static class fields of the MmgEvent class are preset event IDs meant to represent keyboard or gamepad input. An input can be mapped to one of these static IDs indicating directional pad input, enter or space bar input, or back or escape input.
It’s important to note that the event ID is really only important in the context of the event and the event handler. You could choose to use the preset IDs to indicate something entirely different than a keyboard event. It’s really up to you.
Class Fields
MmgEvent Class Fields 1
The MmgEvent class supports different associations between events and event handlers. The first field can be used to assign a parent event handler. You can ignore this or use it to create parent-child events. The next class field is the message field. This can optionally be used to add information about the event or event payload data if the string data type is useful. The next two class fields, id and type, are used to identify and respond to the event when being processed by the event handler. The id and type combination should be unique for a given event and event handler setup.
The targetHandler field is the handler that receives the event when it triggers. The extra field is an event payload that can be used to store objects and data associated with the event. Last but not least, we have the prevEvent field, which is an instance of the MmgEvent class. This field can be used to store a reference to a previous event. You can use this feature to create chains of events.
Support Method Details
MmgEvent Support Method Details 1
The support methods listed in the preceding are just simple get and set methods for the various class fields. I won’t cover them in any detail here, but make sure to look over them and make sure you understand them.
Main Method Details
MmgEvent Main Method Details 1
The first method in the preceding listing is the class constructor. It takes an argument for each of the class’ pertinent fields. Note that you don’t need to provide an object for each argument if you don’t plan to use that field. For instance, the ParentHandler argument can be set to null if you don’t have an event hierarchy.
The next two main methods for us to look at are the Fire and ApiToString methods. The Fire method is called when the MmgEvent should trigger. If the targetHandler is defined, then it’s called, and the current MmgEvent object is passed as an argument. The ApiToString method returns a string representation of the event.
Demonstration: MmgEvent Class
MmgEvent Class Demonstration 1
MmgEvent Class Demonstration 2
In the second snippet of example code, we can see how to properly trigger the click screen event of the MmgScrollVert class. In response to receiving a mouse click event, the clickScreen field, if defined, will be fired. Notice the use of the extra field to store information about the event. In this case, we are sending the screen click coordinates, as an MmgVector2 object, along with the click event.
Other Classes: MmgEventHandler
The MmgEventHandler is an interface that a class can implement so that it can handle certain MmgEvent objects. The MmgEventHandler class is implemented as an interface so that any class can implement it and register to handle events.
Class Review
MmgEventHandler Class Review 1
The MmgEventHandler interface defines one method signature, MmgHandleEvent. When a class implements the interface, it must define the MmgHandleEvent method and in doing so can receive registered events. It’s really that simple.
Demonstration: MmgEventHandler Class
MmgEventHandler Class Demonstration 1
The first snippet of code listed in the preceding shows the SetEventHandler method of the MmgScrollVert class. This method is used to register event handlers for the different events supported by the class. Notice that each event supported by the class has its event handler set, lines 2–4. The next snippet of code is from the ScreenTestMmgScrollVert class’ LoadResources method from the MmgTestSpace library. In this code block, a new instance of the MmgScrollVert class is initialized. Notice that on line 5 the event handler for the scroll pane widget is set to the game screen.
That brings us to the last piece of the puzzle – the event handler method from the game screen class, ScreenTestMmgScrollVert. On line 2, we check to see if the event received is an event we can process by looking at the id and type fields of the received object. If we can process the event, an MmgVector2 object is cast from the extra field, line 3. In this case, we use the click information to update an MmgFont instance’s text.
Chapter Conclusion
MmgCfgFileEntry: A class that models a line of data in a class config file. This class is used to store data from a class config file line and use it to set up a game screen.
MmgEvent: A general event class that provides basic event support to the API.
MmgEventHandler: An event handler interface. This interface is used to set up an event handler to receive registered events. When used in combination with the MmgEvent class, the two classes constitute a simple event system.
There is subtle power in these classes. Take a minute to think about the event system configuration. Notice how general it is. Like most of the functionality we’re building up to support the game engine, we have a generalized event system that you can use in your next game. When working with events, be sure not to abuse them. Think about using events when one class needs to notify another class about something that happened. Use that as a guide when implementing events.