Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

11991 Posts in 1587 Topics- by 3507 Members - Latest Member: PienueDut

26. May 2012, 12:45:53 pm
Xith3D CommunityXith3D InternalsDeveloper discussion (Moderators: Marvin Fröhlich, 'n ddrylliog)FirstPersonInputHandler
Pages: [1]
Print
Author Topic: FirstPersonInputHandler  (Read 987 times)
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« on: 26. October 2009, 08:39:10 pm »

Hi everyone,

I know it's been a long time since I was around, but I'm back -- at least every now and then.  I am setting up some test programs to submit for research grants exploring various data visualizations.  It looks pretty promising so maybe I'll be around a lot more often.

Now the reason I post: I set up a FirstPersonInputHandler and tried to extend it meaningfully.  It seems this is rather difficult so I'd like to open up a discussion about it.  First, is anyone seriously using it or is it primarily used for unit tests?  If it is being used seriously, how is everyone extending it?  It seems to me there are several hard-coded dependencies between FirstPersonInputHandler and the FPIH* classes.  This means the best way to extend it right now is to copy-paste the whole set of classes which is clearly an anti-pattern.

I tried making it generic even going so far as the use of
Code:
FirstPersonInputHandler<T extends Enum<T> & InputAction> extends InputHandler<T>
This showed promise; however, the number of touched classes started to explode.

I think I should be able to do something like this:
Code:
        this.firstPersonInputHandler = new FirstPersonInputHandler( xithRoot.getView(), xithRoot.getCanvas(), 1.0f, 1.0f, false, 1.0f );
        this.firstPersonInputHandler.getBindingsManager().unbindAll();
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.Q, FPIHInputAction.TURN_LEFT );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.W, FPIHInputAction.WALK_FORWARD );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.E, FPIHInputAction.TURN_RIGHT );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.R, FPIHInputAction.AIM_UP );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.A, FPIHInputAction.STRAFE_LEFT );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.S, FPIHInputAction.WALK_BACKWARD );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.D, FPIHInputAction.STRAFE_RIGHT );
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.F, FPIHInputAction.AIM_DOWN );
        /*
         * This next line is what I cannot do today.
         */
        this.firstPersonInputHandler.getBindingsManager().bind( Keys.G, MyInputAction.RESET_ANGLE );

The problems arise from Java's handling of generics and enums in combination with each other -- especially the inability for one enum to extend another.  If FirstPersonInputHandler and FPIHInputStatesManager used "if...else if" instance checks instead for objects implementing InputAction, we could extend the class meaningfully.  Right now, the classes use switch statements based on enum constants.

Presumably the switch statements are used because enum table lookups are faster than "if...else if" blocks; however, it might just be for clarity.  Regardless, I would like to open the idea of the Command pattern (also known as / very similar to State or Functor patterns).  This would require a few hours refactoring (or a lot more by the uninitiated) of the classes in that part of the system but it would be clear, extensible, and even faster than switch statements.

If I missed some key part of the API that let's me achieve the goal, just tell me that too.
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
Marvin Fröhlich
Xith Lord
Administrator
Guru
*****
Offline Offline

Posts: 4381


May the 4th, be with you...


View Profile
« Reply #1 on: 26. October 2009, 09:56:12 pm »

I have converted FPIHInputAction to a regular class to make it extendable. You should now be able to do, what you need.

Yes, I used an enum for speed reasons. Though this is not important for input at all and for a small number of items if/else if is even faster than a switch. So we should be fine here.

Marvin
Logged
horati
Global Moderator
Getting respectable
*****
Offline Offline

Posts: 393


View Profile
« Reply #2 on: 26. October 2009, 11:04:37 pm »

Awesome, thanks!

It looked as if there might be potential errors if I tried that approach since I wasn't sure where all the dependencies were.
Logged

Kevin
"It may not seem like a big deal, but ignorance of character encoding issues leads to insidious code rot akin to y2k."
http://stackoverflow.com/users/3474/sylvarking
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic