[Bf-blender-cvs] [e6aa464] master: BLI_rect: add BLI_rctf_clamp

Campbell Barton noreply at git.blender.org
Fri Oct 16 15:40:09 CEST 2015


Commit: e6aa4647573fc97a78ebc84bae426b18db139384
Author: Campbell Barton
Date:   Sat Oct 17 00:03:29 2015 +1100
Branches: master
https://developer.blender.org/rBe6aa4647573fc97a78ebc84bae426b18db139384

BLI_rect: add BLI_rctf_clamp

Clamp one rect within another.

This is done inline in the UI code, which gets verbose.

===================================================================

M	source/blender/blenlib/BLI_rect.h
M	source/blender/blenlib/intern/rct.c

===================================================================

diff --git a/source/blender/blenlib/BLI_rect.h b/source/blender/blenlib/BLI_rect.h
index a98931d..59bf364 100644
--- a/source/blender/blenlib/BLI_rect.h
+++ b/source/blender/blenlib/BLI_rect.h
@@ -66,6 +66,8 @@ void BLI_rctf_interp(struct rctf *rect, const struct rctf *rect_a, const struct
 //void BLI_rcti_interp(struct rctf *rect, struct rctf *rect_a, struct rctf *rect_b, float fac);
 bool BLI_rctf_clamp_pt_v(const struct rctf *rect, float xy[2]);
 bool BLI_rcti_clamp_pt_v(const struct rcti *rect, int xy[2]);
+bool BLI_rctf_clamp(struct rctf *rect, const struct rctf *rect_bounds, float r_xy[2]);
+bool BLI_rcti_clamp(struct rcti *rect, const struct rcti *rect_bounds, int r_xy[2]);
 bool BLI_rctf_compare(const struct rctf *rect_a, const struct rctf *rect_b, const float limit);
 bool BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b);
 bool BLI_rctf_isect(const struct rctf *src1, const struct rctf *src2, struct rctf *dest);
diff --git a/source/blender/blenlib/intern/rct.c b/source/blender/blenlib/intern/rct.c
index 9f14df2..600ae6b 100644
--- a/source/blender/blenlib/intern/rct.c
+++ b/source/blender/blenlib/intern/rct.c
@@ -494,6 +494,95 @@ bool BLI_rcti_clamp_pt_v(const rcti *rect, int xy[2])
 	return changed;
 }
 
+/**
+ * Clamp \a rect within \a rect_bounds, setting \a r_xy to the offset.
+ *
+ * \return true if a change is made.
+ */
+bool BLI_rctf_clamp(rctf *rect, const rctf *rect_bounds, float r_xy[2])
+{
+	bool changed = false;
+
+	r_xy[0] = 0.0f;
+	r_xy[1] = 0.0f;
+
+	if (rect->xmin < rect_bounds->xmin) {
+		float ofs = rect_bounds->xmin - rect->xmin;
+		rect->xmin += r_xy[0];
+		rect->xmax += r_xy[0];
+		r_xy[0] += ofs;
+		changed = true;
+	}
+
+	if (rect->xmax > rect_bounds->xmax) {
+		float ofs = rect_bounds->xmax - rect->xmax;
+		rect->xmin += ofs;
+		rect->xmax += ofs;
+		r_xy[0] += ofs;
+		changed = true;
+	}
+
+	if (rect->ymin < rect_bounds->ymin) {
+		float ofs = rect_bounds->ymin - rect->ymin;
+		rect->ymin += r_xy[1];
+		rect->ymax += r_xy[1];
+		r_xy[1] += ofs;
+		changed = true;
+	}
+
+	if (rect->ymax > rect_bounds->ymax) {
+		float ofs = rect_bounds->ymax - rect->ymax;
+		rect->ymin += ofs;
+		rect->ymax += ofs;
+		r_xy[1] += ofs;
+		changed = true;
+	}
+
+	return changed;
+}
+
+bool BLI_rcti_clamp(rcti *rect, const rcti *rect_bounds, int r_xy[2])
+{
+	bool changed = false;
+
+	r_xy[0] = 0;
+	r_xy[1] = 0;
+
+	if (rect->xmin < rect_bounds->xmin) {
+		int ofs = rect_bounds->xmin - rect->xmin;
+		rect->xmin += r_xy[0];
+		rect->xmax += r_xy[0];
+		r_xy[0] += ofs;
+		changed = true;
+	}
+
+	if (rect->xmax > rect_bounds->xmax) {
+		int ofs = rect_bounds->xmax - rect->xmax;
+		rect->xmin += ofs;
+		rect->xmax += ofs;
+		r_xy[0] += ofs;
+		changed = true;
+	}
+
+	if (rect->ymin < rect_bounds->ymin) {
+		int ofs = rect_bounds->ymin - rect->ymin;
+		rect->ymin += r_xy[1];
+		rect->ymax += r_xy[1];
+		r_xy[1] += ofs;
+		changed = true;
+	}
+
+	if (rect->ymax > rect_bounds->ymax) {
+		int ofs = rect_bounds->ymax - rect->ymax;
+		rect->ymin += ofs;
+		rect->ymax += ofs;
+		r_xy[1] += ofs;
+		changed = true;
+	}
+
+	return changed;
+}
+
 bool BLI_rctf_compare(const rctf *rect_a, const rctf *rect_b, const float limit)
 {
 	if (fabsf(rect_a->xmin - rect_b->xmin) < limit)




More information about the Bf-blender-cvs mailing list