Jelajahi Sumber

Fixed ScreenEntity mouse events

Ivan Safrin 13 tahun lalu
induk
melakukan
0490cc51b4

+ 4 - 4
Core/Contents/Include/PolyScreenEntity.h

@@ -85,10 +85,10 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		Number getRotation() const;
 			
 		bool _onMouseDown(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
-		bool _onMouseUp(Number x, Number y, int mouseButton, int timestamp);
-		void _onMouseMove(Number x, Number y, int timestamp);
-		void _onMouseWheelUp(Number x, Number y, int timestamp);
-		void _onMouseWheelDown(Number x, Number y, int timestamp);
+		bool _onMouseUp(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust = Vector2(0,0));
+		void _onMouseMove(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
+		void _onMouseWheelUp(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
+		void _onMouseWheelDown(Number x, Number y, int timestamp, Vector2 parentAdjust = Vector2(0,0));
 	
 		virtual void onMouseDown(Number x, Number y){}
 		virtual void onMouseUp(Number x, Number y){}

+ 180 - 70
Core/Contents/Source/PolyScreenEntity.cpp

@@ -245,10 +245,18 @@ void ScreenEntity::clearDragLimits() {
 	dragLimits = NULL;
 }
 
-void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp) {
+void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp, Vector2 parentAdjust) {
 
 	if(isDragged) {
-		setPosition(x-dragOffsetX,y-dragOffsetY);
+	
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);				
+				
+		if(parentEntity) {
+			Matrix4 inverse = parentEntity->getConcatenatedMatrix().inverse();
+			localCoordinate = inverse * localCoordinate;
+		}
+	
+		setPosition(localCoordinate.x-dragOffsetX,localCoordinate.y-dragOffsetY);
 		if(dragLimits) {
 			if(position.x < dragLimits->x)
 				position.x = dragLimits->x;
@@ -261,39 +269,89 @@ void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp) {
 		}
 	}
 	
-	xmouse = x;
-	ymouse = y;
-
-	onMouseMove(x,y);
+	
+	bool doTest = true;	
+	
+	if(hasMask) {
+		if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+			doTest = false;
+		}	
+	}
+	
+	if(doTest) {
 	if(processInputEvents && enabled) {
-		if(hitTest(x,y)) {
-			dispatchEvent(new InputEvent(Vector2(x,y), timestamp), InputEvent::EVENT_MOUSEMOVE);
-			if(!mouseOver) {
-				dispatchEvent(new InputEvent(Vector2(x,y), timestamp), InputEvent::EVENT_MOUSEOVER);
+	if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+	
+	
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);		
+		Matrix4 inverse = getConcatenatedMatrix().inverse();
+		localCoordinate = inverse * localCoordinate;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.x += hitwidth/2.0;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.y += hitheight/2.0;
+
+
+		
+		onMouseMove(localCoordinate.x,localCoordinate.y);
+		xmouse = localCoordinate.x;
+		ymouse = localCoordinate.y;
+		
+		dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEMOVE);
+		
+		if(!mouseOver) {
+				dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEOVER);
 				mouseOver = true;
-			}
-		} else {
-			if(mouseOver) {
-				dispatchEvent(new InputEvent(Vector2(x,y), timestamp), InputEvent::EVENT_MOUSEOUT);
-				mouseOver = false;
-			}
 		}
+			
+	} else {
+		if(mouseOver) {
+		
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);		
+		Matrix4 inverse = getConcatenatedMatrix().inverse();
+		localCoordinate = inverse * localCoordinate;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.x += hitwidth/2.0;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.y += hitheight/2.0;
+		
+		
+			dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEOUT);
+			mouseOver = false;
+		}		
+	}	
 	}
 	
 	if(enabled) {
-		for(int i=0;i<children.size();i++) {
-			((ScreenEntity*)children[i])->_onMouseMove(x,y, timestamp);
+		for(int i=children.size()-1;i>=0;i--) {			
+			Vector2 adjust = parentAdjust;
+			if(positionMode == POSITION_TOPLEFT)
+				adjust += Vector2(width/2.0, height/2.0);
+			((ScreenEntity*)children[i])->_onMouseMove(x,y, timestamp, adjust);
+			if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
+				if(((ScreenEntity*)children[i])->hitTest(x,y))
+				   break;
+			}
 		}
 	}
+	}		
 }
 
-bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp) {
+bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp, Vector2 parentAdjust) {
 	bool retVal = false;
 	
-	if(processInputEvents && enabled) {	
-	if(hitTest(x,y)) {
+	bool doTest = true;	
 	
-		Vector3 localCoordinate = Vector3(x,y,0);
+	if(hasMask) {
+		if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+			doTest = false;
+		}	
+	}
+	
+	if(doTest) {
+	if(processInputEvents && enabled) {
+	if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
 		
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
@@ -301,16 +359,16 @@ bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp
 			localCoordinate.x += hitwidth/2.0;
 		if(positionMode == POSITION_TOPLEFT)
 			localCoordinate.y += hitheight/2.0;
-	
-		onMouseUp(localCoordinate.x,localCoordinate.y);
+
 		
-		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y), timestamp);		
-		inputEvent->mouseButton = mouseButton;		
+		onMouseUp(localCoordinate.x,localCoordinate.y);		
+		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);		
+		inputEvent->mouseButton = mouseButton;
 		dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP);
-		retVal = true;		
-	} else {
 		
-		Vector3 localCoordinate = Vector3(x,y,0);
+		retVal = true;
+	} else {
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
 		
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
@@ -318,76 +376,128 @@ bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp
 			localCoordinate.x += hitwidth/2.0;
 		if(positionMode == POSITION_TOPLEFT)
 			localCoordinate.y += hitheight/2.0;
-	
+
 		
-		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y), timestamp);		
+		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);		
 		inputEvent->mouseButton = mouseButton;
-		
-		dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP_OUTSIDE);
+		dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEUP_OUTSIDE);	
 	}
 	}
 	if(enabled) {
-		Vector2 adjust;
-		if(positionMode == POSITION_TOPLEFT)
-			adjust = Vector2(width/2.0, height/2.0);
-	
-		for(int i=0;i<children.size();i++) {
-			((ScreenEntity*)children[i])->_onMouseUp(x+adjust.x,y+adjust.y, mouseButton, timestamp);
+		for(int i=children.size()-1;i>=0;i--) {			
+			Vector2 adjust = parentAdjust;
+			if(positionMode == POSITION_TOPLEFT)
+				adjust += Vector2(width/2.0, height/2.0);
+			((ScreenEntity*)children[i])->_onMouseUp(x,y, mouseButton, timestamp, adjust);;
+			if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
+				if(((ScreenEntity*)children[i])->hitTest(x,y))
+				   break;
+			}
 		}
 	}
+	}		
+	
 	return retVal;
 }
 
-void ScreenEntity::_onMouseWheelUp(Number x, Number y, int timestamp) {
-	bool doTest = true;
+void ScreenEntity::_onMouseWheelUp(Number x, Number y, int timestamp, Vector2 parentAdjust) {
+	bool retVal = false;
+	
+	bool doTest = true;	
 	
 	if(hasMask) {
-		if(!((ScreenEntity*)maskEntity)->hitTest(x,y)) {
+		if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
 			doTest = false;
 		}	
 	}
 	
 	if(doTest) {
-		if(hitTest(x,y) && enabled) {
-			onMouseWheelUp(x,y);
-			dispatchEvent(new InputEvent(Vector2(x,y), timestamp), InputEvent::EVENT_MOUSEWHEEL_UP);
-		}
-		if(enabled) {
-			for(int i=children.size()-1;i>=0;i--) {				
-				((ScreenEntity*)children[i])->_onMouseWheelUp(x,y, timestamp);
-				if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
-					if(((ScreenEntity*)children[i])->hitTest(x,y))
-						break;
-				}
+	if(processInputEvents && enabled) {
+	if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
+		
+		Matrix4 inverse = getConcatenatedMatrix().inverse();
+		localCoordinate = inverse * localCoordinate;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.x += hitwidth/2.0;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.y += hitheight/2.0;
+
+		
+		onMouseWheelUp(localCoordinate.x,localCoordinate.y);
+		
+		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);		
+		dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_UP);
+		
+		retVal = true;
+	}
+	}
+	
+	if(enabled) {
+		for(int i=children.size()-1;i>=0;i--) {			
+			Vector2 adjust = parentAdjust;
+			if(positionMode == POSITION_TOPLEFT)
+				adjust += Vector2(width/2.0, height/2.0);
+			((ScreenEntity*)children[i])->_onMouseWheelUp(x,y, timestamp, adjust);;
+			if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
+				if(((ScreenEntity*)children[i])->hitTest(x,y))
+				   break;
 			}
 		}
-	}	
+	}
+	}		
+	
+	return retVal;
 }
 
-void ScreenEntity::_onMouseWheelDown(Number x, Number y, int timestamp) {
-	bool doTest = true;
+void ScreenEntity::_onMouseWheelDown(Number x, Number y, int timestamp, Vector2 parentAdjust) {
+	bool retVal = false;
+	
+	bool doTest = true;	
 	
 	if(hasMask) {
-		if(!((ScreenEntity*)maskEntity)->hitTest(x,y)) {
+		if(!((ScreenEntity*)maskEntity)->hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
 			doTest = false;
 		}	
 	}
 	
 	if(doTest) {
-		if(hitTest(x,y) && enabled) {
-			onMouseWheelDown(x,y);
-			dispatchEvent(new InputEvent(Vector2(x,y), timestamp), InputEvent::EVENT_MOUSEWHEEL_DOWN);
-		}
-		if(enabled) {
-			for(int i=children.size()-1;i>=0;i--) {				
-				((ScreenEntity*)children[i])->_onMouseWheelDown(x,y, timestamp);
-				if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
-					if(((ScreenEntity*)children[i])->hitTest(x,y))
-						break;
-				}
+	if(processInputEvents && enabled) {
+	if(hitTest(x+parentAdjust.x,y+parentAdjust.y)) {
+		Vector3 localCoordinate = Vector3(x+(parentAdjust.x*2.0),y+(parentAdjust.y*2.0),0);
+		
+		Matrix4 inverse = getConcatenatedMatrix().inverse();
+		localCoordinate = inverse * localCoordinate;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.x += hitwidth/2.0;
+		if(positionMode == POSITION_TOPLEFT)
+			localCoordinate.y += hitheight/2.0;
+
+		
+		onMouseWheelDown(localCoordinate.x,localCoordinate.y);
+		
+		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);		
+		dispatchEvent(inputEvent, InputEvent::EVENT_MOUSEWHEEL_DOWN);
+		
+		retVal = true;
+	}
+	}
+	
+	if(enabled) {
+		for(int i=children.size()-1;i>=0;i--) {			
+			Vector2 adjust = parentAdjust;
+			if(positionMode == POSITION_TOPLEFT)
+				adjust += Vector2(width/2.0, height/2.0);
+			((ScreenEntity*)children[i])->_onMouseWheelDown(x,y, timestamp, adjust);;
+			if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
+				if(((ScreenEntity*)children[i])->hitTest(x,y))
+				   break;
 			}
 		}
-	}	
+	}
+	}		
+	
+	return retVal;	
 }
 
 
@@ -436,7 +546,7 @@ bool ScreenEntity::_onMouseDown(Number x, Number y, int mouseButton, int timesta
 			Vector2 adjust = parentAdjust;
 			if(positionMode == POSITION_TOPLEFT)
 				adjust += Vector2(width/2.0, height/2.0);
-			((ScreenEntity*)children[i])->_onMouseDown(x,y, mouseButton, timestamp, adjust);
+			((ScreenEntity*)children[i])->_onMouseDown(x,y, mouseButton, timestamp, adjust);;
 			if(((ScreenEntity*)children[i])->blockMouseInput && ((ScreenEntity*)children[i])->enabled) {
 				if(((ScreenEntity*)children[i])->hitTest(x,y))
 				   break;

+ 2 - 0
IDE/Build/Mac OS X/Polycode.xcodeproj/project.pbxproj

@@ -435,6 +435,7 @@
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Core/include\"",
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Core/Dependencies/include\"",
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Modules/include\"",
+					/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/OpenAL.framework/Headers,
 				);
 				INFOPLIST_FILE = "Polycode-Info.plist";
 				INSTALL_PATH = "$(HOME)/Applications";
@@ -472,6 +473,7 @@
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Core/include\"",
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Core/Dependencies/include\"",
 					"\"$(SRCROOT)/../../../Release/Darwin/Framework/Modules/include\"",
+					/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/OpenAL.framework/Headers,
 				);
 				INFOPLIST_FILE = "Polycode-Info.plist";
 				INSTALL_PATH = "$(HOME)/Applications";

+ 7 - 0
Modules/Contents/UI/Source/PolyUIButton.cpp

@@ -58,6 +58,7 @@ UIButton::UIButton(String text, Number width, Number height) : ScreenEntity() {
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOVER);
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
+	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEUP_OUTSIDE);	
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
 	pressedDown = false;
 	
@@ -71,6 +72,8 @@ UIButton::UIButton(String text, Number width, Number height) : ScreenEntity() {
 	this->height = height;
 	focusable = true;
 	
+	buttonRect->processInputEvents = true;
+	
 }
 
 void UIButton::Update() {
@@ -97,6 +100,10 @@ void UIButton::handleEvent(Event *event) {
 				buttonRect->setPosition(0,0);				
 				pressedDown = false;
 			break;
+			case InputEvent::EVENT_MOUSEUP_OUTSIDE:
+				buttonLabel->setPosition(labelXPos,labelYPos);
+				buttonRect->setPosition(0,0);
+			break;			
 			case InputEvent::EVENT_MOUSEUP:
 				buttonLabel->setPosition(labelXPos,labelYPos);
 				buttonRect->setPosition(0,0);

+ 1 - 0
Modules/Contents/UI/Source/PolyUIImageButton.cpp

@@ -43,6 +43,7 @@ UIImageButton::UIImageButton(String imageName) : ScreenEntity() {
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEOUT);
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEUP);
 	buttonRect->addEventListener(this, InputEvent::EVENT_MOUSEDOWN);
+	buttonRect->processInputEvents = true;
 	pressedDown = false;
 	
 	width = buttonRect->getWidth();