Game Development Reference
In-Depth Information
// nunchuck buttons
if ( nc->btns != 0 || nc->btns_released != 0 ) {
int Z_TR01 = (nc->btns == 3) || (nc->btns_released == 3); /* Old Z=1, New(tr01) Z=(3)ALL */
if (IS_JUST_PRESSED(nc, NUNCHUK_BUTTON_C) && !Z_TR01) jni_send_btn_press(id,"C");
if (IS_JUST_PRESSED(nc, NUNCHUK_BUTTON_Z)) jni_send_btn_press(id,"Z");
if (IS_RELEASED(nc, NUNCHUK_BUTTON_C) && !Z_TR01) jni_send_btn_release(id,"C");
if (IS_RELEASED(nc, NUNCHUK_BUTTON_Z)) jni_send_btn_release(id,"Z");
}
// joystick
jni_send_event(id,
"EVT_TYPE=NUNCHUK|ROLL=%.2f|PITCH=%.2f|YAW=%.2f|JANGLE=%.2f|JMAGNITU DE=%.2f"
, nc->orient.roll, nc->orient.pitch, nc->orient.yaw, nc->js.ang, nc->js.mag);
}
}
The event handler in Listing 8-11 first checks if the core buttons are pressed or released. If
so, it sends the event information thru the JNI callback system back to Java by invoking
jni_send_event(id, "EVT_TYPE=BTNPRESS|BTN=%s", btn);
For example, when A is pressed, the string
"EVT_TYPE=BTNPRESS|BTN=A"
will be sent to
the
OnMessage()
method of the Wiimote Java class. This class will then parse the event
and dispatch the required information to any listeners that implement the
IController.
IControllerListener
interface. In your case, the Android app will log the event in the text
box of the app GUI.
The same thing happens when buttons are pressed/released on the Nunchuk. When the
joystick is moved, the string sent will be
"EVT_TYPE=NUNCHUK|ROLL=%.2f|PITCH=%.2f|YAW=%.2f|JANGLE=%.2f|JMAGNITUDE=%.2f"
This string tells the Wiimote class a Nunchuk joystick event has occurred along with
information such as roll, pitch, yaw, angle, and magnitude values. Finally, the last pieces of
the puzzle are the two methods used by the callback system to send messages back to the
Wiimote Java class. Let's take a look.
Callback Methods
Listing 8-12 shows the methods used to send event information back to the Wiimote class.
They are critical; without them the Wiimote class won't receive anything when an event occurs
in
WiiC
. These callbacks are the two simple functions:
jni_send_message
and
jni_send_event
.
Both reuse the function
jni_send_str
, which is in charge of doing the heavy lifting.
jni_send_str
takes three arguments:
1.
An integer (return code) that can be used to send a code to the
OnMessage
Java method or a Wiimote number to the
OnEvent
method
of the Wiimote class.
2.
The second argument is the method name to invoke within Wiimote
(either
OnMessage
or
OnEvent
).