Bladeren bron

Made pixel snapping an (overridable) per-screne setting, added addChild to Scene and addEntity to Screen for consistency

Ivan Safrin 13 jaren geleden
bovenliggende
commit
18dd1f1c44

+ 1 - 1
Core/Contents/Include/PolyEntity.h

@@ -132,7 +132,7 @@ namespace Polycode {
 			/**
 			* @see addChild()
 			*/		
-			void addEntity(Entity *newChild);
+			virtual void addEntity(Entity *newChild);
 
 			/**
 			* Adds another entity as a child. The children inherit the parent's transforms.

+ 6 - 0
Core/Contents/Include/PolyScene.h

@@ -60,6 +60,12 @@ namespace Polycode {
 		* @param entity New entity to add.
 		*/
 		void addEntity(SceneEntity *entity);
+
+		/**
+		* Adds a new SceneEntity to the scene
+		* @param entity New entity to add.
+		*/
+		void addChild(SceneEntity *entity);
 		
 		/**
 		* Removes a SceneEntity from the scene

+ 12 - 0
Core/Contents/Include/PolyScreen.h

@@ -54,6 +54,13 @@ namespace Polycode {
 		* @return Returns the same entity for convenience.
 		*/		
 		ScreenEntity* addChild(ScreenEntity *newEntity);
+
+		/**
+		* Adds a ScreenEntity to the 2d rendering pipeline.
+		* @param newEntity Entity to add.
+		* @return Returns the same entity for convenience.
+		*/		
+		ScreenEntity* addEntity(ScreenEntity *newEntity);
 		
 		/**
 		* Removes a ScreenEntity from the screen's render list.
@@ -159,6 +166,11 @@ namespace Polycode {
 		*/
 		bool ownsChildren;		
 		
+		/**
+		* If true, children snap to pixels by default. You can still change it per entity. Defaults to false.
+		*/		
+		bool snapToPixelsByDefault;
+		
 	protected:
 		
 		bool useNormalizedCoordinates;

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

@@ -44,6 +44,9 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		ScreenEntity();
 		virtual ~ScreenEntity();
 		
+		
+		void addEntity(Entity *newChild);
+		
 		/**
 		* Set 2d position.
 		* @param x Horizontal position.
@@ -155,6 +158,8 @@ class _PolyExport ScreenEntity : public Entity, public EventDispatcher {
 		void setDragLimits(Rectangle rect);
 		void clearDragLimits();
 		
+		void setDefaultScreenOptions(bool snapToPixels);
+		
 		void focusChild(ScreenEntity *child);
 		void focusNextChild();
 	

+ 4 - 0
Core/Contents/Source/PolyScene.cpp

@@ -142,6 +142,10 @@ void Scene::addEntity(SceneEntity *entity) {
 	entities.push_back(entity);
 }
 
+void Scene::addChild(SceneEntity *entity) {
+	addEntity(entity);
+}
+
 void Scene::removeEntity(SceneEntity *entity) {
 	for(int i=0; i < entities.size(); i++) {
 		if(entities[i] == entity) {

+ 8 - 1
Core/Contents/Source/PolyScreen.cpp

@@ -49,6 +49,7 @@ Screen::Screen() : EventDispatcher() {
 	addChild(rootEntity);
 	processTouchEventsAsMouse = false;
 	ownsChildren = false;
+	snapToPixelsByDefault = false;
 }
 
 Screen::~Screen() {
@@ -261,7 +262,7 @@ bool Screen::hasFilterShader() const {
 	return _hasFilterShader;
 }
 
-ScreenEntity* Screen::addChild(ScreenEntity *newEntity) {
+ScreenEntity* Screen::addEntity(ScreenEntity *newEntity) {
 	if(!newEntity)
 		return NULL;
 	children.push_back(newEntity);
@@ -271,10 +272,16 @@ ScreenEntity* Screen::addChild(ScreenEntity *newEntity) {
 	newEntity->addEventListener(this, ScreenEvent::ENTITY_MOVE_DOWN);
 	newEntity->addEventListener(this, ScreenEvent::ENTITY_MOVE_UP);
 	newEntity->zindex = getHighestZIndex()+1;
+	newEntity->setDefaultScreenOptions(snapToPixelsByDefault);
+	
 	sortChildren();
 	return newEntity;
 }
 
+ScreenEntity* Screen::addChild(ScreenEntity *newEntity) {
+	addEntity(newEntity);
+}
+
 ScreenEntity* Screen::removeChild(ScreenEntity *entityToRemove) {
 	if(!entityToRemove)
 		return NULL;

+ 19 - 2
Core/Contents/Source/PolyScreenEntity.cpp

@@ -67,6 +67,18 @@ ScreenEntity::ScreenEntity() : Entity(), EventDispatcher() {
 	
 }
 
+void ScreenEntity::addEntity(Entity *newChild) {
+	((ScreenEntity*)newChild)->setDefaultScreenOptions(snapToPixels);
+	Entity::addEntity(newChild);
+}
+		
+void ScreenEntity::setDefaultScreenOptions(bool snapToPixels) {
+	this->snapToPixels = snapToPixels;
+		for(int i=0; i < children.size(); i++) {
+			((ScreenEntity*)children[i])->setDefaultScreenOptions(snapToPixels);
+		}
+}
+
 void ScreenEntity::focusNextChild() {
 	int j = 0;
 	if(focusedChild) {
@@ -621,6 +633,11 @@ Matrix4 ScreenEntity::buildPositionMatrix() {
 }
 
 void ScreenEntity::adjustMatrixForChildren() {
-	if(positionMode == POSITION_TOPLEFT)
-		renderer->translate2D(-floor(width/2.0f), -floor(height/2.0f));	
+	if(positionMode == POSITION_TOPLEFT) {
+		if(snapToPixels) {
+			renderer->translate2D(-floor(width/2.0f), -floor(height/2.0f));			
+		} else {
+			renderer->translate2D(-width/2.0f, -height/2.0f);	
+		}
+	}
 }

+ 1 - 0
IDE/Contents/Source/PolycodeIDEApp.cpp

@@ -44,6 +44,7 @@ PolycodeIDEApp::PolycodeIDEApp(PolycodeView *view) : EventDispatcher() {
 	printf("creating font editor\n"); 
 	
 	Screen *screen = new Screen();	
+	screen->snapToPixelsByDefault = true;
 	
 	editorManager = new PolycodeEditorManager();
 	

+ 3 - 0
Modules/Contents/UI/Source/PolyUITextInput.cpp

@@ -286,6 +286,9 @@ void UITextInput::deleteSelection() {
 
 void UITextInput::Resize(int x, int y) {
 	inputRect->resizeBox(x, y);
+	this->width = x;
+	this->height = y;	
+	setHitbox(x,y);
 }
 
 int UITextInput::insertLine(bool after) {