Placing MOIS code in a separate class

Floppy

19-09-2009 02:33:52

Hi,

I am pretty new to this so be gentle =)

I want to place my MOIS code in a separate class to avoid messy code. I have looked at the example "Using MOIS" on the wiki and noticed that the KeyPressed/Released function is passed directly to the keylistener;


inputKeyboard.KeyPressed += new MOIS.KeyListener.KeyPressedHandler(KeyPressed);
inputKeyboard.KeyReleased += new MOIS.KeyListener.KeyReleasedHandler(KeyReleased);


I do not want to act upon the input in this class, only provide the user of the class with some handy functions for getting mouse positions and what keys are pressed etc.. I am thinking that I want my KeyPressed function to return a bool telling the caller whether the button passed to it is pressed or not. With the current setup this is not possible? As I understand it I have to iterate through the listener inside the KeyPressed function to see if the key passed to it was pressed or not, then just return?

/Thanks in advance

EDIT:

Let me put it like this.. In my main class, I am initializing my IOHandler object and passing it *this*(the main class/object) so that I have access to all the member variables and can modify them when I handle all the input inside the IOHandler object. Is this good programming? Shouldn't this be done outside the IOHandler, in the main object?

smiley80

21-09-2009 17:28:42

You could do something like this:
public class MoisInput
{
private MOIS.Keyboard inputKeyboard;
private MOIS.InputManager inputManager;
private MOIS.Mouse inputMouse;

public MoisInput(RenderWindow window)
{
MOIS.ParamList pl = new MOIS.ParamList();
IntPtr windowHnd;
window.GetCustomAttribute("WINDOW", out windowHnd);
pl.Insert("WINDOW", windowHnd.ToString());

this.inputManager = MOIS.InputManager.CreateInputSystem(pl);

this.inputKeyboard = (MOIS.Keyboard)this.inputManager.CreateInputObject(MOIS.Type.OISKeyboard, true);
this.inputMouse = (MOIS.Mouse)this.inputManager.CreateInputObject(MOIS.Type.OISMouse, true);
MOIS.MouseState_NativePtr state = this.inputMouse.MouseState;
state.width = (int)window.Width;
state.height = (int)window.Height;
}

public bool IsMouseButtonDown(MOIS.MouseButtonID mouseButton)
{
this.inputMouse.Capture();
return this.inputMouse.MouseState.ButtonDown(mouseButton);
}

public System.Drawing.Point GetMousePosition()
{
this.inputMouse.Capture();
MOIS.MouseState_NativePtr state = this.inputMouse.MouseState;
return new System.Drawing.Point(state.X.abs, state.Y.abs);
}

public Collection<MOIS.KeyCode> GetPressedKeys()
{
this.inputKeyboard.Capture();
Collection<MOIS.KeyCode> col = new Collection<MOIS.KeyCode>();
foreach (MOIS.KeyCode kc in Enum.GetValues(typeof(MOIS.KeyCode)))
{
if (this.inputKeyboard.IsKeyDown(kc))
{
col.Add(kc);
}
}

return col;
}

public bool IsKeyDown(MOIS.KeyCode keyCode)
{
return this.GetPressedKeys().Contains(keyCode);
}
}