Pencil.gaming callbacks

March 24, 2016 ยท View on GitHub

public static GlfwMouseButtonFun mouseCallback;
public static GlfwKeyFun keyCallback;


protected virtual void Setup(){

	// Setup mouse callbacks:
	mouseCallback += HandleMouseClick;
	Glfw.SetMouseButtonCallback(mouseCallback);

	keyCallback += HandleKeyDown;
	Glfw.SetKeyCallback (keyCallback);

// this is my window resizing code
	Glfw.SetWindowSizeCallback ((int width, int height) => {
		Screen.viewSize = new Size (width, height);
		needsResizing = true;
	});

}

public  void HandleKeyDown(Key key, Keystate state){
	if (state == Keystate.Press && key == (Key) 'Z') {
		UpdateTestState (testState - 1);
	}
	if (state == Keystate.Press && key == (Key) 'X') {
		UpdateTestState (testState + 1);
	}
	if (state == Keystate.Press && key == (Key) 'K') {
		UpdateSkyboxState (skyboxState - 1);
	}
	if (state == Keystate.Press && key == (Key) 'L') {
		UpdateSkyboxState (skyboxState + 1);
	}
	if (state == Keystate.Press && key == (Key) 'J') {
		UpdateSkyboxState (0);
	}
}

public void HandleMouseClick(MouseButton mouseButton, Keystate state){//,int mouseButton, int buttonDown){
	
	int x = 0;
	int y = 0;

	Glfw.GetMousePos(out x, out y);
	// screenSpace = new Vector4(x, y, 0, 1);
	
	//My math to flip the Y
	Vector3 worldSpace = UnProject(new Vector3(x,Screen.viewSize.Height - y,0), renderQueue.modelview, renderQueue.projection, new float[]{0, 0, Screen.x, Screen.y});
	Vector3 uiSpace = UnProject(new Vector3(x,Screen.viewSize.Height - y,0), uiRenderQueue.modelview, uiRenderQueue.projection, new float[]{0, 0, Screen.x, Screen.y});

	if(state == Keystate.Release){

// Debug.Log ("button: " + mouseButton + " up at " + screenSpace.ToString() + " -> " + worldSpace.ToString());

// call your own helper functions if you want MouseUpHelper (uiSpace.Xy, worldSpace, mouseButton); }else{ // Debug.Log ("button: " + mouseButton + " down at " + screenSpace.ToString() + " -> " + worldSpace.ToString()); MouseDownHelper (uiSpace.Xy, worldSpace, mouseButton); previousMouseDown = GetMousePos (); } }