瀏覽代碼

Simple input events in player

Ivan Safrin 13 年之前
父節點
當前提交
a13feb3801
共有 2 個文件被更改,包括 77 次插入0 次删除
  1. 14 0
      Bindings/Contents/LUA/API/defaults.lua
  2. 63 0
      Player/Contents/Source/PolycodePlayer.cpp

+ 14 - 0
Bindings/Contents/LUA/API/defaults.lua

@@ -44,6 +44,20 @@ function delete(c)
 	c:__delete()
 end
 
+function onKeyDown(key)
+end
+
+function onKeyUp(key)
+end
+
+function onMouseDown(button, x,y)
+end
+
+function onMouseUp(button, x,y)
+end
+
+function onMouseMove(x,y)
+end
 
 function Update(e)
 end

+ 63 - 0
Player/Contents/Source/PolycodePlayer.cpp

@@ -599,6 +599,12 @@ void PolycodePlayer::loadFile(const char *fileName) {
 	}
 	createCore();
 
+	core->getInput()->addEventListener(this, InputEvent::EVENT_KEYDOWN);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_KEYUP);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEMOVE);
+	core->getInput()->addEventListener(this, InputEvent::EVENT_MOUSEUP);
+					
 	if(nameString == "") {
 		return;
 	}
@@ -681,6 +687,7 @@ PolycodePlayer::~PolycodePlayer() {
 
 void PolycodePlayer::handleEvent(Event *event) {	
 
+
 	if(event->getDispatcher() == debuggerTimer) {
 		runFile(fullPath);
 		debuggerTimer->Pause(true);
@@ -727,6 +734,62 @@ void PolycodePlayer::handleEvent(Event *event) {
 			break;		
 		}
 	}
+	
+	if(event->getDispatcher() == core->getInput()) {
+		InputEvent *inputEvent = (InputEvent*) event;
+		switch(event->getEventCode()) {
+			case InputEvent::EVENT_KEYDOWN:
+			{
+				if(L && !crashed) {
+					lua_getfield(L, LUA_GLOBALSINDEX, "onKeyDown");
+					lua_pushinteger(L, inputEvent->keyCode());
+					lua_pcall(L, 1,0,errH);					
+				}
+			}
+			break;
+			case InputEvent::EVENT_KEYUP:
+			{
+				if(L && !crashed) {
+					lua_getfield(L, LUA_GLOBALSINDEX, "onKeyUp");
+					lua_pushinteger(L, inputEvent->keyCode());
+					lua_pcall(L, 1,0,errH);					
+				}
+			}
+			break;
+			case InputEvent::EVENT_MOUSEDOWN:
+			{
+				if(L && !crashed) {
+					lua_getfield(L, LUA_GLOBALSINDEX, "onMouseDown");
+					lua_pushinteger(L, inputEvent->mouseButton);
+					lua_pushnumber(L, inputEvent->mousePosition.x);
+					lua_pushnumber(L, inputEvent->mousePosition.y);					
+					lua_pcall(L, 3,0,errH);					
+				}
+			}
+			break;	
+			case InputEvent::EVENT_MOUSEUP:
+			{
+				if(L && !crashed) {
+					lua_getfield(L, LUA_GLOBALSINDEX, "onMouseUp");
+					lua_pushinteger(L, inputEvent->mouseButton);
+					lua_pushnumber(L, inputEvent->mousePosition.x);
+					lua_pushnumber(L, inputEvent->mousePosition.y);					
+					lua_pcall(L, 3,0,errH);					
+				}
+			}
+			break;	
+			case InputEvent::EVENT_MOUSEMOVE:
+			{
+				if(L && !crashed) {
+					lua_getfield(L, LUA_GLOBALSINDEX, "onMouseMove");
+					lua_pushnumber(L, inputEvent->mousePosition.x);
+					lua_pushnumber(L, inputEvent->mousePosition.y);					
+					lua_pcall(L, 2,0,errH);					
+				}
+			}
+			break;																			
+		}
+	}
 }