© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2021
V. G. BruscaIntroduction to Video Game Engine Developmenthttps://doi.org/10.1007/978-1-4842-7039-4_4

4. Other Classes

Victor G Brusca1  
(1)
Edison, NJ, USA
 
In this chapter, you’ll review the “other” classes of the MmgBase API. These are classes that don’t quite fit into your typical categories, so we gave them their own. We’ll look at the following classes during this review:
  • 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

The MmgCfgFileEntry has a few descriptive fields I’ve listed in the following.
public CfgEntryType cfgType = CfgEntryType.NONE;
public Double number;
public String str;
public String name;
Listing 4-1

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

Due to the simple nature of the class, there aren’t any get and set support methods. That gives us only two methods to review.
public String ApiToString() { ... }
public int compare(MmgCfgFileEntry o1, MmgCfgFileEntry o2) { ... }
Listing 4-2

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

The MmgCfgFileEntry class has some standard main methods for us to review.
public MmgCfgFileEntry() { ... }
public MmgCfgFileEntry(MmgCfgFileEntry obj) { ... }
public MmgCfgFileEntry Clone() { ... }
public boolean ApiEquals(MmgCfgFileEntry obj) { ... }
Listing 4-3

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

The snippet of example code listed in the following shows the MmgCfgFileEntry class in use as part of a class config implementation.
01 classConfig = MmgHelper.ReadClassConfigFile(GameSettings.CLASS_CONFIG_DIR + "screen_splash.txt");
...
02 key = "splashScreenDisplayTimeMs";
03 if(classConfig.containsKey(key)) {
04 super.SetDisplayTime(classConfig.get(key).number.intValue());
05 }
06
07 key = "bmpLogo";
08 if(classConfig.containsKey(key)) {
09     file = classConfig.get(key).str;
10 } else {
11     file = "logo_large.jpg";
12 }
Listing 4-4

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

The MmgEvent class has a number of class fields that are default static values for different events. These presets were mainly designed to be used with a menu system allowing the user to move around to the different menu options.
public static int EVENT_ID_UP = 0;
public static int EVENT_ID_DOWN = 1;
public static int EVENT_ID_LEFT = 2;
public static int EVENT_ID_RIGHT = 3;
public static int EVENT_ID_ENTER = 4;
public static int EVENT_ID_SPACE = 5;
public static int EVENT_ID_BACK = 6;
public static int EVENT_ID_ESC = 7;
Listing 4-5

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

The class fields listed in the following define the payload for the event and the handler for the event. The MmgEvent class is implemented in a general way allowing you the flexibility to use it however you want.
private MmgEventHandler parentHandler;
private String message;
private int id;
private int type;
private MmgEventHandler targetHandler;
private Object extra;
private MmgEvent prevEvent;
Listing 4-6

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

The MmgEvent class has the following support methods.
public MmgEvent GetPrevEvent() { ... }
public void SetPrevEvent(MmgEvent p) { ... }
public void SetParentEventHandler(MmgEventHandler e) { ... }
public MmgEventHandler GetParentEventHandler() { ... }
public void SetTargetEventHandler(MmgEventHandler e) { ... }
public MmgEventHandler GetTargetEventHandler() { ... }
public String GetMessage() { ... }
public void SetMessage(String s) { ... }
public int GetEventId() { ... }
public void SetEventId(int s) { ... }
public int GetEventType() { ... }
public void SetEventType(int s) { ... }
public Object GetExtra() { ... }
public void SetExtra(Object obj) { ... }
Listing 4-7

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

The MmgEvent class has a few main methods for us to look at.
public MmgEvent(MmgEventHandler ParentHandler, String Msg, int Id, int Type, MmgEventHandler TargetHandler, Object Ex) { ... }
public void Fire() { ... }
public String ApiToString() { ... }
Listing 4-8

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

As a demonstration of the MmgEvent class, we’ll take a look at creating and firing an event.
1 private MmgEvent clickScreen = new MmgEvent(null, "vert_click_screen", MmgScrollVert.SCROLL_VERT_CLICK_EVENT_ID, MmgScrollVert.SCROLL_VERT_CLICK_EVENT_TYPE, null, null);
Listing 4-9

MmgEvent Class Demonstration 1

The first line of example code shows us how to instantiate a new MmgEvent object. This line of code is from the class field section of the MmgScrollVert class, in the MmgBase package, or namespace in C#. Notice that the parent handler, target handler, and previous event fields are all set to null. In this example, we are prepping the event with the correct id and type, but we aren’t specifying an event handler just yet. In this case, the event handler is set a little later on in the configuration of the MmgScrollVert class.
1 if(clickScreen != null) {
2     clickScreen.SetExtra(new MmgVector2(x, y));
3     clickScreen.Fire();
4 }
Listing 4-10

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

Because the MmgEventHandler interface does not have the same features as a regular class, we’ll just print out the code and talk about it directly.
1 public interface MmgEventHandler {
2     public void MmgHandleEvent(MmgEvent e);
3 }
Listing 4-11

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

For this class’ demonstration, we’ll take a look at three snippets of code that show you how to set up an MmgEvent and handler.
//MmgScrollVert
01 public void SetEventHandler(MmgEventHandler e) {
02     clickScreen.SetTargetEventHandler(e);
03     clickUp.SetTargetEventHandler(e);
04     clickDown.SetTargetEventHandler(e);
05 }
//ScreenTestMmgScrollVert.LoadResources
01 scrollVert = new MmgScrollVert(vPort, sPane, sBarColor, sBarSldrColor, sBarWidth, sBarSldrHeight, interval);
02 scrollVert.SetIsVisible(true);
03 scrollVert.SetWidth(sWidth + scrollVert.GetScrollBarWidth());
04 scrollVert.SetHeight(sHeight);
05 scrollVert.SetEventHandler(this);
//ScreenTestMmgScrollVert
01 public void MmgHandleEvent(MmgEvent e) {
02     if(e.GetEventId() == MmgScrollVert.SCROLL_VERT_CLICK_EVENT_ID || e.GetEventId() == MmgScrollHor.SCROLL_HOR_CLICK_EVENT_ID || e.GetEventId() == MmgScrollHorVert.SCROLL_BOTH_CLICK_EVENT_ID) {
03         MmgVector2 v2 = (MmgVector2)e.GetExtra();
04         event.SetText("Event: Id: " + e.GetEventId() + " Type: " + e.GetEventType() + " Pos: " + v2.ApiToString() + " Msg: " + e.GetMessage() + " " + System.currentTimeMillis());
05
06     } else {
07         event.SetText("Event: Id: " + e.GetEventId() + " Type: " + e.GetEventType() + " Msg: " + e.GetMessage() + " " + System.currentTimeMillis());
08
09     }
10
11     MmgHelper.CenterHor(event);
12 }
Listing 4-12

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

In this chapter, we completed a review of the “other” classes. The classes we covered are very useful, and I’ll summarize them as follows:
  • 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.154.208