changeset 18:40a7c9618ade

* new function which lists instances in a rect
author spq@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 07 Jul 2008 01:40:57 +0000
parents ae46cee19e76
children 485f4c6dd9fc
files engine/core/view/camera.cpp engine/core/view/camera.h engine/core/view/camera.i
diffstat 3 files changed, 48 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/engine/core/view/camera.cpp	Mon Jul 07 00:27:59 2008 +0000
+++ b/engine/core/view/camera.cpp	Mon Jul 07 01:40:57 2008 +0000
@@ -323,6 +323,45 @@
 		}
 	}
 	
+	void Camera::getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances) {
+		instances.clear();
+		const std::vector<Instance*>& layer_instances = m_layer_to_instances[&layer];
+		std::vector<Instance*>::const_iterator instance_it = layer_instances.end();
+		while (instance_it != layer_instances.begin()) {
+			--instance_it;
+			Instance* i = (*instance_it);
+			InstanceVisual* visual = i->getVisual<InstanceVisual>();
+			InstanceVisualCacheItem& vc = visual->getCacheItem(this);
+			if ((vc.dimensions.intersects(screen_rect))) {
+				assert(vc.image);
+				Uint8 r, g, b, a;
+				for(int xx = screen_rect.x; xx < screen_rect.x + screen_rect.w; xx++) {
+					for(int yy = screen_rect.y; yy < screen_rect.y + screen_rect.h; yy++) {
+						int x = xx - vc.dimensions.x;
+						int y = yy - vc.dimensions.y;
+						if (m_zoom != 1.0) {
+							double fx = static_cast<double>(x);
+							double fy = static_cast<double>(y);
+							double fow = static_cast<double>(vc.image->getWidth());
+							double foh = static_cast<double>(vc.image->getHeight());
+							double fsw = static_cast<double>(vc.dimensions.w);
+							double fsh = static_cast<double>(vc.dimensions.h);
+							x = static_cast<int>(round(fx / fsw * fow));
+							y = static_cast<int>(round(fy / fsh * foh));
+						}
+						vc.image->getPixelRGBA(x, y, &r, &b, &g, &a);
+						// instance is hit with mouse if not totally transparent
+						if (a != 0) {
+							instances.push_back(i);
+							goto found_non_transparent_pixel;
+						}
+					}
+				}
+				found_non_transparent_pixel:;
+			}
+		}
+	}
+	
 	void Camera::getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates) {
 		instances.clear();
 		const std::vector<Instance*>& layer_instances = m_layer_to_instances[loc.getLayer()];
--- a/engine/core/view/camera.h	Mon Jul 07 00:27:59 2008 +0000
+++ b/engine/core/view/camera.h	Mon Jul 07 01:40:57 2008 +0000
@@ -219,6 +219,14 @@
 		 * @param instances list of instances that is filled based on hit test results
 		 */
 		void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances);
+		
+		/** Returns instances that match given screen coordinate
+		 * @param screen_point1 top left screen coordinates to be used for hit search
+		 * @param screen_point2 right bottom screen coordinates to be used for hit search
+		 * @param layer layer to use for search
+		 * @param instances list of instances that is filled based on hit test results
+		 */
+		void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances);
 
 		/** Returns instances that match given location. Instances are sorted based on camera view, so that "topmost"
 		 * instance is first in returned list
--- a/engine/core/view/camera.i	Mon Jul 07 00:27:59 2008 +0000
+++ b/engine/core/view/camera.i	Mon Jul 07 01:40:57 2008 +0000
@@ -37,6 +37,7 @@
 		bool isEnabled();
 		
 		void getMatchingInstances(ScreenPoint screen_coords, Layer& layer, std::list<Instance*>& instances);
+		void getMatchingInstances(Rect screen_rect, Layer& layer, std::list<Instance*>& instances);
 		void getMatchingInstances(Location& loc, std::list<Instance*>& instances, bool use_exactcoordinates=false);
 		RendererBase* getRenderer(const std::string& name);
 		void resetRenderers();