Quellcode durchsuchen

hitwidth/hitheight (now a Rectangle hit instead of two Numbers) now works correctly with ScreenMesh. If you change the Mesh after creating the ScreenMesh you will need to call an updateHitBox() function.

mcc vor 13 Jahren
Ursprung
Commit
7c2adedb49

+ 6 - 5
Core/Contents/Include/PolyScreenEntity.h

@@ -124,13 +124,13 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		* Sets the width of the screen entity.
 		* @param w New height value.
 		*/									
-		void setWidth(Number w) { width = w; hitwidth = w; }
+		void setWidth(Number w) { width = w; hit.w = w; hit.x = -w/2; }
 		
 		/**
 		* Sets the height of the screen entity.
 		* @param h New height value.
 		*/									
-		void setHeight(Number h) { height = h; hitheight = h; }
+		void setHeight(Number h) { height = h; hit.h = h; hit.y = -h/2; }
 	
 		virtual void onGainFocus(){}
 		virtual void onLoseFocus(){}		
@@ -175,7 +175,9 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		bool snapToPixels;
 		bool processInputEvents;
 
-		Vector2 getHitbox();
+		Rectangle getHitbox();
+		void setHitbox(Number width, Number height);
+		void setHitbox(Number width, Number height, Number left, Number top);
 
 	protected:
 	
@@ -190,8 +192,7 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		Number width;
 		Number height;
 
-		Number hitwidth;
-		Number hitheight;
+		Rectangle hit;
 		
 		Number xmouse;
 		Number ymouse;

+ 6 - 1
Core/Contents/Include/PolyScreenMesh.h

@@ -95,7 +95,12 @@ namespace Polycode {
 			* If true, will delete its Mesh upon destruction. (defaults to true)
 			*/ 			
 			bool ownsMesh;
-			
+		
+			/**
+			 * Updates hit.width, hit.height to coordinates of mesh.
+			 */
+			void updateHitBox();
+		
 		protected:
 		
 			Mesh *mesh;

+ 38 - 28
Core/Contents/Source/PolyScreenEntity.cpp

@@ -35,8 +35,7 @@ ScreenEntity::ScreenEntity() : Entity(), EventDispatcher() {
 	color = Color(1.0f,1.0f,1.0f,1.0f);
 	width = 1;
 	height = 1;
-	hitwidth = 1;
-	hitheight = 1;
+	setHitbox(1, 1);
 	backfaceCulled = false;
 	positionMode = POSITION_TOPLEFT;
 	mouseOver = false;
@@ -192,25 +191,23 @@ bool ScreenEntity::hitTest(const Number x, const Number y) const {
 	Polygon testPoly;
 	
 	Matrix4 transformMatrix = getConcatenatedMatrix();
-	
-	v = Vector3(-hitwidth/2.0, -hitheight/2.0,0);
-	v = transformMatrix * v;	
+	v = Vector3(hit.x, hit.y, 0);
+	v = transformMatrix * v;
 	testPoly.addVertex(v.x, v.y, 0.0);
 	
-	v = Vector3(hitwidth/2.0, -hitheight/2.0,0);
-	v = transformMatrix * v;	
+	v = Vector3(hit.x+hit.w, hit.y, 0);
+	v = transformMatrix * v;
 	testPoly.addVertex(v.x, v.y, 0.0);
 
-	v = Vector3(hitwidth/2.0, hitheight/2.0,0);
-	v = transformMatrix * v;	
+	v = Vector3(hit.x+hit.w, hit.y+hit.h, 0);
+	v = transformMatrix * v;
 	testPoly.addVertex(v.x, v.y, 0.0);
 
-	v = Vector3(-hitwidth/2.0,hitheight/2.0,0);
-	v = transformMatrix * v;	
+	v = Vector3(hit.x,hit.y+hit.h, 0);
+	v = transformMatrix * v;
 	testPoly.addVertex(v.x, v.y, 0.0);
 		
 	return isPointInsidePolygon2D(&testPoly, Vector2(x,y));
-	
 }
 
 void ScreenEntity::setPositionMode(int newPositionMode) {
@@ -249,8 +246,21 @@ void ScreenEntity::clearDragLimits() {
 	dragLimits = NULL;
 }
 
-Vector2 ScreenEntity::getHitbox() {
-	return Vector2(hitwidth, hitheight);
+Rectangle ScreenEntity::getHitbox() {
+	return hit;
+}
+
+void ScreenEntity::setHitbox(Number width, Number height) {
+	hit.w = width;
+	hit.h = height;
+	hit.x = -width/2;
+	hit.y = -height/2;
+}
+void ScreenEntity::setHitbox(Number width, Number height, Number left, Number top) {
+	hit.w = width;
+	hit.h = height;
+	hit.x = left;
+	hit.y = top;
 }
 
 Matrix4 ScreenEntity::getScreenConcatenatedMatrix() {
@@ -309,9 +319,9 @@ void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp, Vector2 paren
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 
 		
@@ -333,9 +343,9 @@ void ScreenEntity::_onMouseMove(Number x, Number y, int timestamp, Vector2 paren
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 		
 		
 			dispatchEvent(new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp), InputEvent::EVENT_MOUSEOUT);
@@ -378,9 +388,9 @@ bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 		
 		onMouseUp(localCoordinate.x,localCoordinate.y);		
@@ -395,9 +405,9 @@ bool ScreenEntity::_onMouseUp(Number x, Number y, int mouseButton, int timestamp
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 		
 		InputEvent *inputEvent = new InputEvent(Vector2(localCoordinate.x,localCoordinate.y)-parentAdjust, timestamp);		
@@ -441,9 +451,9 @@ void ScreenEntity::_onMouseWheelUp(Number x, Number y, int timestamp, Vector2 pa
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 		
 		onMouseWheelUp(localCoordinate.x,localCoordinate.y);
@@ -489,9 +499,9 @@ void ScreenEntity::_onMouseWheelDown(Number x, Number y, int timestamp, Vector2
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 		
 		onMouseWheelDown(localCoordinate.x,localCoordinate.y);
@@ -539,9 +549,9 @@ bool ScreenEntity::_onMouseDown(Number x, Number y, int mouseButton, int timesta
 		Matrix4 inverse = getConcatenatedMatrix().inverse();
 		localCoordinate = inverse * localCoordinate;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.x += hitwidth/2.0;
+			localCoordinate.x += hit.w/2.0;
 		if(positionMode == POSITION_TOPLEFT)
-			localCoordinate.y += hitheight/2.0;
+			localCoordinate.y += hit.h/2.0;
 
 		
 		onMouseDown(localCoordinate.x,localCoordinate.y);

+ 1 - 2
Core/Contents/Source/PolyScreenImage.cpp

@@ -65,8 +65,7 @@ void ScreenImage::setImageCoordinates(Number x, Number y, Number width, Number h
 	
 	this->width = width;
 	this->height = height;
-	hitwidth = width;
-	hitheight = height;
+	setHitbox(width, height);
 	Number whalf = floor(width/2.0f);
 	Number hhalf = floor(height/2.0f);	
 		

+ 24 - 0
Core/Contents/Source/PolyScreenMesh.cpp

@@ -33,6 +33,7 @@ ScreenMesh::ScreenMesh(Mesh *mesh) : ScreenEntity(), texture(NULL) {
 	lineSmooth = false;
 	lineWidth = 1.0;
 	ownsMesh = true;
+	updateHitBox();
 }
 
 ScreenMesh::ScreenMesh(const String& fileName) : ScreenEntity(), texture(NULL) {
@@ -90,3 +91,26 @@ void ScreenMesh::Render() {
 	renderer->pushDataArrayForMesh(mesh, RenderDataArray::TEXCOORD_DATA_ARRAY);	
 	renderer->drawArrays(mesh->getMeshType());
 }
+
+void ScreenMesh::updateHitBox() {
+	Number xmin, ymin, xmax, ymax;
+	bool any = false;
+	for(int c = 0; c < mesh->getPolygonCount(); c++) {
+		Polygon *poly = mesh->getPolygon(c);
+		for(int d = 0; d < poly->getVertexCount(); d++) {
+			Vertex *v = poly->getVertex(d);
+			if (any) {
+				xmin = MIN(v->x, xmin);
+				ymin = MIN(v->y, ymin);
+				xmax = MAX(v->x, xmax);
+				ymax = MAX(v->y, ymax);
+			} else {
+				xmin = v->x; xmax = v->x;
+				ymin = v->y; ymax = v->y;
+				any = true;
+			}
+		}
+	}
+	
+	setHitbox(xmax-xmin, ymax-ymin, xmin, ymin);
+}

+ 3 - 5
Core/Contents/Source/PolyScreenShape.cpp

@@ -34,9 +34,8 @@ ScreenShape::ScreenShape(int shapeType, Number option1, Number option2, Number o
 	this->shapeType = shapeType;
 	width = option1;
 	height = option2;
-
-	hitwidth = width;
-	hitheight = height;
+	
+	setHitbox(width, height);
 
 	this->option1 = option1;
 	this->option2 = option2;
@@ -93,8 +92,7 @@ void ScreenShape::setShapeSize(Number newWidth, Number newHeight) {
 	width = newWidth;
 	height = newHeight;
 	
-	hitwidth = width;
-	hitheight = height;	
+	setHitbox(width, height);
 	
 	Number whalf = floor(width/2.0f);
 	Number hhalf = floor(height/2.0f);

+ 2 - 4
Modules/Contents/UI/Source/PolyUIBox.cpp

@@ -30,8 +30,7 @@ UIBox::UIBox(String imageFile, Number t, Number r, Number b, Number l, Number bo
 	
 	width=boxWidth;
 	height = boxHeight;
-	hitwidth = boxWidth;
-	hitheight = boxHeight;
+	setHitbox(boxWidth, boxHeight);
 	
 	tlImage = new ScreenImage(imageFile);
 	tlImage->setImageCoordinates(0,0,l,t);
@@ -106,8 +105,7 @@ void UIBox::resizeBox(Number newWidth, Number newHeight) {
 	
 	width=newWidth;
 	height = newHeight;
-	hitwidth = newWidth;
-	hitheight = newHeight;
+	setHitbox(newWidth, newHeight);
 	
 	this->rebuildTransformMatrix();
 }

+ 2 - 4
Modules/Contents/UI/Source/PolyUIScrollContainer.cpp

@@ -42,8 +42,7 @@ UIScrollContainer::UIScrollContainer(ScreenEntity *scrolledEntity, bool hScroll,
 	
 	this->width = width;
 	this->height = height;
-	this->hitwidth = width;
-	this->hitheight = height;
+	setHitbox(width, height);
 	
 	Number uiScrollPanePadding = conf->getNumericValue("Polycode", "uiScrollPanePadding");			
 	
@@ -84,8 +83,7 @@ UIScrollContainer::UIScrollContainer(ScreenEntity *scrolledEntity, bool hScroll,
 void UIScrollContainer::Resize(int x, int y) {
 	width = x;
 	height = y;
-	hitwidth = width;
-	hitheight = height;
+	setHitbox(width, height);
 	
 	maskShape->setShapeSize(x, y);
 	vScrollBar->Resize(y);

+ 1 - 2
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -124,8 +124,7 @@ UITextInput::UITextInput(bool multiLine, Number width, Number height) : ScreenEn
 	focusable = true;
 	this->width = width;
 	this->height = rectHeight;
-	hitwidth = width;
-	hitheight = rectHeight;
+	setHitbox(width, rectHeight);
 	
 	updateCaretPosition();
 }

+ 1 - 2
Modules/Contents/UI/Source/PolyUITree.cpp

@@ -259,8 +259,7 @@ void UITree::refreshTree() {
 	}
 	height = treeHeight + cellHeight;
 	width = treeWidth;
-	hitwidth = width;
-	hitheight = height;
+	setHitbox(width, height);
 	
 	selection->visible = selected;
 	dispatchEvent(new UITreeEvent(), UITreeEvent::NEED_REFRESH_EVENT);	

+ 3 - 6
Modules/Contents/UI/Source/PolyUITreeContainer.cpp

@@ -58,8 +58,7 @@ UITreeContainer::UITreeContainer(String icon, String text, Number treeWidth, Num
 	
 	width = treeWidth;
 	height = treeHeight;
-	hitwidth = width;
-	hitheight = height;
+	setHitbox(width, height);
 }
 
 void UITreeContainer::Resize(int x, int y) {
@@ -68,10 +67,8 @@ void UITreeContainer::Resize(int x, int y) {
 	mainContainer->setPositionY(0);
 
 //	width = x;
-//	height = y;
-	hitwidth = x;
-	hitheight = y;
-
+	//	height = y;
+	setHitbox(x, y);
 }
 
 void UITreeContainer::handleEvent(Event *event) {

+ 2 - 4
Modules/Contents/UI/Source/PolyUIVScrollBar.cpp

@@ -84,14 +84,12 @@ UIVScrollBar::UIVScrollBar(Number width, Number height, Number initialRatio) : S
 	this->height = height;
 	this->width = width;	
 	
-	this->hitwidth = width;
-	this->hitheight = height;	
+	setHitbox(width, height);
 }
 
 void UIVScrollBar::Resize(int newHeight) {
 	bgBox->resizeBox(width, newHeight);
-	this->height = newHeight;
-	this->hitheight = newHeight;
+	setHeight(newHeight);
 	dragRectHeight = height-(padding*2)-scrollHandleHeight;	
 	handleBox->setDragLimits(Rectangle(padding,padding,width-(padding*2)-(width-(padding*2)), dragRectHeight));	
 }

+ 1 - 2
Modules/Contents/UI/Source/PolyUIWindow.cpp

@@ -80,8 +80,7 @@ UIWindow::UIWindow(String windowName, Number width, Number height) : ScreenEntit
 	
 	this->width = width;
 	this->height = height;
-	this->hitwidth = width;
-	this->hitheight = height;
+	setHitbox(width, height);
 	
 	focusable = true;
 	blockMouseInput = true;