To add functonality to our application we must do some actions (cd, dvd
player) after selecting a menu. We can do this by adding own "states" to
the application. In our case, "states" are the implementation of a
functionality. The MMBoxApplication Class always knows which state is
active and so it always knows where it should send the key event so that
only one state has the input to any time.
How we create a new state? - This can be done by writing a new class
that is derived from MMBoxState, this looks like that:
class MyState : public MMBoxState {
public:
Result Up( MMBoxApplication* );
Result Down( MMBoxApplication* );
Result Left( MMBoxApplication* );
Result Right( MMBoxApplication* );
Result Return( MMBoxApplication* );
Result Back( MMBoxApplication* );
Result initialize( MMBoxApplication* );
Result uinitialize( MMBoxApplication* );
};
|
Here we must implement what should happen if we get a key up, down, etc.
message by overwriting the corresponding methods. Each method has got a
pointer to the MMBoxApplication, so that we can save global data there
if needed or access the Multiple Osd Node to draw text or icons.
There a two special methods. The first one is called "initialize", it is
called at the beginning always when we switch to the given state, it
could be compared with the constructor of a class. The second one is
called "uinitialize" and it is called before we switch to another state.
It could be compared with the deconstructor of a class.
At last we must tell the MMBoxApplication that it should use our new
state with the "registerState" method:
app->registerState( "mystateID", new MyState );
|
This method needs a string to identify the class and a pointer to a
MMBoxState. Note: all registered states will be deleted automatically,
that means no delete MyState is needed at the end of the program.
How we see above, each state has got a string identifier. So we can
simply switch between states by calling the changeState method with the
new id:
changeState( app, newID )
|