From a6d6fb1a1e6be0d055598ae2b64b800d5b3a445f Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Wed, 11 Jul 2012 16:29:30 +0700 Subject: [PATCH 1/9] CPGeometry moved to Foundation --- AppKit/AppKit.j | 1 - {AppKit => Foundation}/CPGeometry.j | 0 Foundation/Foundation.j | 1 + Tests/{AppKit => Foundation}/CPGeometryTest.j | 0 4 files changed, 1 insertion(+), 1 deletion(-) rename {AppKit => Foundation}/CPGeometry.j (100%) rename Tests/{AppKit => Foundation}/CPGeometryTest.j (100%) diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index c48b219d3..783ca68eb 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -60,7 +60,6 @@ @import "CPFlashView.j" @import "CPFont.j" @import "CPFontManager.j" -@import "CPGeometry.j" @import "CPGradient.j" @import "CPGraphics.j" @import "CPImage.j" diff --git a/AppKit/CPGeometry.j b/Foundation/CPGeometry.j similarity index 100% rename from AppKit/CPGeometry.j rename to Foundation/CPGeometry.j diff --git a/Foundation/Foundation.j b/Foundation/Foundation.j index d26bfbb1f..422501616 100755 --- a/Foundation/Foundation.j +++ b/Foundation/Foundation.j @@ -36,6 +36,7 @@ @import "CPException.j" @import "CPExpression.j" @import "CPFormatter.j" +@import "CPGeometry.j" @import "CPIndexSet.j" @import "CPIndexPath.j" @import "CPInvocation.j" diff --git a/Tests/AppKit/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j similarity index 100% rename from Tests/AppKit/CPGeometryTest.j rename to Tests/Foundation/CPGeometryTest.j From 2905cd1586b10831e24c3b2a8d5cd893223b6dc0 Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Thu, 12 Jul 2012 00:19:10 +0700 Subject: [PATCH 2/9] Linked methods from CPGeometry to CGGeometry + added unit tests --- Foundation/CPGeometry.j | 92 ++++--------------------------- Tests/Foundation/CPGeometryTest.j | 52 ++++++++++++++++- 2 files changed, 61 insertions(+), 83 deletions(-) diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index 7e7fa2079..e98ba26d8 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -33,22 +33,21 @@ CPMakePoint = CGPointMake; CPMakeSize = CGSizeMake; CPMakeRect = CGRectMake; +CPPointCreateCopy = CGPointMakeCopy; + +CPPointEqualToPoint = CGPointEqualToPoint; +CPRectEqualToRect = CGRectEqualToRect; + +CPRectIsEmpty = CGRectIsEmpty; + +CPRectContainsRect = CGRectContainsRect; +CPRectIntersection = CGRectIntersection; + /*! @addtogroup appkit @{ */ -/*! - Creates a copy of a specified point and returns the copy - @group CGPoint - @param the point to be copied - @return CGPoint the copy of the provided CGPoint - */ -function CPPointCreateCopy(aPoint) -{ - return { x: aPoint.x, y: aPoint.y }; -} - /*! Makes a CGPoint object out of two numbers provided as arguments @group CGPoint @@ -86,26 +85,6 @@ function CPRectIntegral(aRect) alert("CPRectIntegral unimplemented"); } -/*! - Returns the intersection of the two provided rectangles as a new rectangle - @group CGRect - @param lhsRect the first rectangle used for calculation - @param rhsRect the second rectangle used for calculation - @return CGRect the intersection of the two rectangles -*/ -function CPRectIntersection(lhsRect, rhsRect) -{ - var intersection = CPRectMake( - MAX(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), - MAX(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), - 0, 0); - - intersection.size.width = MIN(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)) - CPRectGetMinX(intersection); - intersection.size.height = MIN(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)) - CPRectGetMinY(intersection); - - return CPRectIsEmpty(intersection) ? CPRectMakeZero() : intersection; -} - /*! Creates a copy of the provided rectangle @group CGRect @@ -226,45 +205,6 @@ function CPRectContainsPoint(aRect, aPoint) aPoint.y < CPRectGetMaxY(aRect); } -/*! - Returns a \c BOOL indicating whether CGRect \c possibleOuter - contains CGRect \c possibleInner. - @group CGRect - @param possibleOuter the CGRect to test if \c possibleInner is inside of - @param possibleInner the CGRect to test if it fits inside \c possibleOuter. - @return BOOL \c YES if \c possibleInner fits inside \c possibleOuter. -*/ -function CPRectContainsRect(possibleOuter, possibleInner) -{ - return CGRectContainsRect(possibleOuter, possibleInner); -} - -/*! - Tests whether the two CGPoints are equal to each other by comparing their - \c x and \c y members. - @group @CGPoint - @param lhsPoint the first CGPoint to check - @param rhsPoint the second CGPoint to check - @return BOOL \c YES if the two points have the same x's, and the same y's. -*/ -function CPPointEqualToPoint(lhsPoint, rhsPoint) -{ - return lhsPoint.x == rhsPoint.x && lhsPoint.y == rhsPoint.y; -} - -/*! - Test whether the two CGRects have the same origin and size - @group CGRect - @param lhsRect the first CGRect to compare - @param rhsRect the second CGRect to compare - @return BOOL \c YES if the two rectangles have the same origin and size. \c NO, otherwise. -*/ -function CPRectEqualToRect(lhsRect, rhsRect) -{ - return CPPointEqualToPoint(lhsRect.origin, rhsRect.origin) && - CPSizeEqualToSize(lhsRect.size, rhsRect.size); -} - /*! @group CGRect @param aRect a CGRect @@ -357,18 +297,6 @@ function CPRectIntersectsRect(lhsRect, rhsRect) return !CPRectIsEmpty(CPRectIntersection(lhsRect, rhsRect)); } -/*! - Returns \c YES if the CGRect has no area. - The test is performed by checking if the width and height are both zero. - @group CGRect - @param aRect the CGRect to test - @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. -*/ -function CPRectIsEmpty(aRect) -{ - return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; -} - /*! Returns \c YES if the CGRect has no area. The test is performed by checking if the width and height are both zero. diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index 50f91828e..7eb30c800 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -1,4 +1,4 @@ -@import +@import @implementation CPGeometryTest : OJTestCase { @@ -14,4 +14,54 @@ [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 1, 1))]; } +-(void)testCPPointEqualToPoint +{ + [self assertTrue:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(0, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(0, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 0), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 0))]; +} + +-(void)testCPRectEqualToRect +{ + [self assertTrue:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(50, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(50, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 50, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 50, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 50, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 50), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 50))]; +} + +-(void)textCPRectIsEmpty +{ + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsEmpty(CGRectMake(0, 0, 10, 10))]; +} + +-(void)testCPRectIntersection +{ + var lhsRect = CGRectMake(0, 0, 20, 20), + rhsRect = CGRectMake(10, 10, 20, 20), + intersectionResultRect = CGRectMake(10, 10, 10, 10); + + [self assertTrue:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; + intersectionResultRect.origin.x = 30; + intersectionResultRect.origin.y = 30; + [self assertFalse:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; +} + +-(void)testCPPointCreateCopy +{ + var point = CGPointMake(1, 1); + [self assertTrue:CPPointEqualToPoint(CPPointCreateCopy(point),point)]; + [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; +} + @end From d6b9daf7ca637be965347fe2585b4f5a2a02c7b5 Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Thu, 12 Jul 2012 02:47:12 +0700 Subject: [PATCH 3/9] Fixed documentation & spaces --- Foundation/CPGeometry.j | 70 +++++++++++++++++++++++++++++++ Tests/Foundation/CPGeometryTest.j | 68 +++++++++++++++--------------- 2 files changed, 104 insertions(+), 34 deletions(-) diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index e98ba26d8..234190a59 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -29,18 +29,88 @@ CPMaxXEdge = 2; CPMaxYEdge = 3; // FIXME: the rest! +/*! + Makes a CGPoint object out of two numbers provided as arguments + @group CGPoint + @param x the x-coordinate of the CGPoint + @param y the y-coordinate of the CGPoint + @return CGPoint a CGPoint with an X and Y coordinate equal to the function arguments +*/ CPMakePoint = CGPointMake; + +/*! + Creates and returns a new CGSize object from the provided dimensions. + @group CGSize + @param width the width for the new CGSize + @param height the height for the new CGSize + @return CGSize the new CGSize +*/ CPMakeSize = CGSizeMake; + +/*! + Returns a CGRect made of the specified arguments + @group CGRect + @param x the x-coordinate of the rectangle's origin + @param y the y-coordinate of the rectangle's origin + @param width the width of the new rectangle + @param height the height of the new rectangle + @return CGRect the new rectangle +*/ CPMakeRect = CGRectMake; +/*! + Creates a copy of a specified point and returns the copy + @group CGPoint + @param the point to be copied + @return CGPoint the copy of the provided CGPoint +*/ CPPointCreateCopy = CGPointMakeCopy; +/*! + Tests whether the two CGPoints are equal to each other by comparing their + \c x and \c y members. + @group @CGPoint + @param lhsPoint the first CGPoint to check + @param rhsPoint the second CGPoint to check + @return BOOL \c YES if the two points have the same x's, and the same y's. +*/ CPPointEqualToPoint = CGPointEqualToPoint; + +/*! + Test whether the two CGRects have the same origin and size + @group CGRect + @param lhsRect the first CGRect to compare + @param rhsRect the second CGRect to compare + @return BOOL \c YES if the two rectangles have the same origin and size. \c NO, otherwise. +*/ CPRectEqualToRect = CGRectEqualToRect; +/*! + Returns \c YES if the CGRect has no area. + The test is performed by checking if the width and height are both zero. + @group CGRect + @param aRect the CGRect to test + @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. +*/ CPRectIsEmpty = CGRectIsEmpty; +/*! + Returns a \c BOOL indicating whether CGRect \c possibleOuter + contains CGRect \c possibleInner. + @group CGRect + @param possibleOuter the CGRect to test if \c possibleInner is inside of + @param possibleInner the CGRect to test if it fits inside \c possibleOuter. + @return BOOL \c YES if \c possibleInner fits inside \c possibleOuter. +*/ CPRectContainsRect = CGRectContainsRect; + +/*! + Returns the intersection of the two provided rectangles as a new rectangle + @group CGRect + @param lhsRect the first rectangle used for calculation + @param rhsRect the second rectangle used for calculation + @return CGRect the intersection of the two rectangles +*/ CPRectIntersection = CGRectIntersection; /*! diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index 7eb30c800..92ab7b1c1 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -14,54 +14,54 @@ [self assertFalse:CPRectContainsRect(CGRectMake(0, 0, 500, 500), CGRectMake(500, 500, 1, 1))]; } --(void)testCPPointEqualToPoint +- (void)testCPPointEqualToPoint { - [self assertTrue:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))]; - [self assertFalse:CPPointEqualToPoint(CGPointMake(0, 10), CGPointMake(10, 10))]; - [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(0, 10))]; - [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 0), CGPointMake(10, 10))]; - [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 0))]; + [self assertTrue:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(0, 10), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(0, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 0), CGPointMake(10, 10))]; + [self assertFalse:CPPointEqualToPoint(CGPointMake(10, 10), CGPointMake(10, 0))]; } --(void)testCPRectEqualToRect +- (void)testCPRectEqualToRect { - [self assertTrue:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(50, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(50, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 50, 100, 100), CGRectMake(0, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 50, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 50, 100), CGRectMake(0, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 50), CGRectMake(0, 0, 100, 100))]; - [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 50))]; + [self assertTrue:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(50, 0, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(50, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 50, 100, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 50, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 50, 100), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 50, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 50), CGRectMake(0, 0, 100, 100))]; + [self assertFalse:CPRectEqualToRect(CGRectMake(0, 0, 100, 100), CGRectMake(0, 0, 100, 50))]; } --(void)textCPRectIsEmpty +- (void)textCPRectIsEmpty { - [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 0, 10))]; - [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, 0))]; - [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, -10, 10))]; - [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, -10))]; - [self assertFalse:CPRectIsEmpty(CGRectMake(0, 0, 10, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsEmpty(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsEmpty(CGRectMake(0, 0, 10, 10))]; } --(void)testCPRectIntersection +- (void)testCPRectIntersection { - var lhsRect = CGRectMake(0, 0, 20, 20), - rhsRect = CGRectMake(10, 10, 20, 20), - intersectionResultRect = CGRectMake(10, 10, 10, 10); + var lhsRect = CGRectMake(0, 0, 20, 20), + rhsRect = CGRectMake(10, 10, 20, 20), + intersectionResultRect = CGRectMake(10, 10, 10, 10); - [self assertTrue:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; - intersectionResultRect.origin.x = 30; - intersectionResultRect.origin.y = 30; - [self assertFalse:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; + [self assertTrue:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; + intersectionResultRect.origin.x = 30; + intersectionResultRect.origin.y = 30; + [self assertFalse:CPRectEqualToRect(CPRectIntersection(lhsRect, rhsRect), intersectionResultRect)]; } --(void)testCPPointCreateCopy +- (void)testCPPointCreateCopy { - var point = CGPointMake(1, 1); - [self assertTrue:CPPointEqualToPoint(CPPointCreateCopy(point),point)]; - [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; + var point = CGPointMake(1, 1); + [self assertTrue:CPPointEqualToPoint(CPPointCreateCopy(point),point)]; + [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; } @end From 28b48edf02bde7170ef791235f30aa5d9568e81c Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Fri, 13 Jul 2012 01:52:06 +0700 Subject: [PATCH 4/9] Completed aliases and tests --- Foundation/CPGeometry.j | 217 +++++--------------------- Tests/Foundation/CPGeometryTest.j | 250 ++++++++++++++++++++++++++++++ 2 files changed, 285 insertions(+), 182 deletions(-) diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index 234190a59..2bb19a4c2 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -20,15 +20,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -@import "CGGeometry.j" +@import - -CPMinXEdge = 0; -CPMinYEdge = 1; -CPMaxXEdge = 2; -CPMaxYEdge = 3; - -// FIXME: the rest! /*! Makes a CGPoint object out of two numbers provided as arguments @group CGPoint @@ -113,22 +106,14 @@ CPRectContainsRect = CGRectContainsRect; */ CPRectIntersection = CGRectIntersection; -/*! - @addtogroup appkit - @{ -*/ - /*! Makes a CGPoint object out of two numbers provided as arguments @group CGPoint @param x the x-coordinate of the CGPoint @param y the y-coordinate of the CGPoint @return CGPoint a CGPoint with an X and Y coordinate equal to the function arguments - */ -function CPPointMake(x, y) -{ - return { x: x, y: y }; -} +*/ +CPPointMake = CGPointMake; /*! Makes a CGRect with an origin and size equal to \c aRect less the \c dX/dY insets specified. @@ -137,11 +122,7 @@ function CPPointMake(x, y) @group CGRect @return CGRect a rectangle like \c aRect with an inset */ -function CPRectInset(aRect, dX, dY) -{ - return CPRectMake( aRect.origin.x + dX, aRect.origin.y + dY, - aRect.size.width - 2 * dX, aRect.size.height - 2 * dY); -} +CPRectInset = CGRectInset; /*! @group CGRect @@ -149,11 +130,7 @@ function CPRectInset(aRect, dX, dY) @return void @deprecated */ -function CPRectIntegral(aRect) -{ - // FIXME!!! - alert("CPRectIntegral unimplemented"); -} +CPRectIntegral = CGRectIntegral; /*! Creates a copy of the provided rectangle @@ -161,10 +138,7 @@ function CPRectIntegral(aRect) @param aRect the CGRect that will be copied @return CGRect the rectangle copy */ -function CPRectCreateCopy(aRect) -{ - return { origin: CPPointCreateCopy(aRect.origin), size: CPSizeCreateCopy(aRect.size) }; -} +CPRectCreateCopy = CGRectCreateCopy; /*! Returns a CGRect made of the specified arguments @@ -175,10 +149,7 @@ function CPRectCreateCopy(aRect) @param height the height of the new rectangle @return CGRect the new rectangle */ -function CPRectMake(x, y, width, height) -{ - return { origin: CPPointMake(x, y), size: CPSizeMake(width, height) }; -} +CPRectMake = CGRectMake; /*! Creates a new rectangle with its origin offset by \c dX and \c dY. @@ -188,36 +159,14 @@ function CPRectMake(x, y, width, height) @param dY the amount added to the y-size of the new rectangle @return CGRect the new rectangle with modified size */ -function CPRectOffset(aRect, dX, dY) -{ - return CPRectMake(aRect.origin.x + dX, aRect.origin.y + dY, aRect.size.width, aRect.size.height); -} +CPRectOffset = CGRectOffset; /*! @group CGRect @param aRect a CGRect @return CGRect */ -function CPRectStandardize(aRect) -{ - var width = CPRectGetWidth(aRect), - height = CPRectGetHeight(aRect), - standardized = CPRectCreateCopy(aRect); - - if (width < 0.0) - { - standardized.origin.x += width; - standardized.size.width = -width; - } - - if (height < 0.0) - { - standardized.origin.y += height; - standardized.size.height = -height; - } - - return standardized; -} +CPRectStandardize = CGRectStandardize; /*! Returns the smallest rectangle that can contain the two argument \c CGRects. @@ -226,15 +175,7 @@ function CPRectStandardize(aRect) @param rhsRect the second CGRect to use for the union calculation @return CGRect the union rectangle */ -function CPRectUnion(lhsRect, rhsRect) -{ - var minX = MIN(CPRectGetMinX(lhsRect), CPRectGetMinX(rhsRect)), - minY = MIN(CPRectGetMinY(lhsRect), CPRectGetMinY(rhsRect)), - maxX = MAX(CPRectGetMaxX(lhsRect), CPRectGetMaxX(rhsRect)), - maxY = MAX(CPRectGetMaxY(lhsRect), CPRectGetMaxY(rhsRect)); - - return CPRectMake(minX, minY, maxX - minX, maxY - minY); -} +CPRectUnion = CGRectUnion; /*! Creates and returns a copy of the provided CGSize @@ -242,10 +183,7 @@ function CPRectUnion(lhsRect, rhsRect) @param aSize the CGSize to copy @return CGSize the copy of the CGSize */ -function CPSizeCreateCopy(aSize) -{ - return { width: aSize.width, height: aSize.height }; -} +CPSizeCreateCopy = CGSizeCreateCopy; /*! Creates and returns a new CGSize object from the provided dimensions. @@ -254,10 +192,7 @@ function CPSizeCreateCopy(aSize) @param height the height for the new CGSize @return CGSize the new CGSize */ -function CPSizeMake(width, height) -{ - return { width: width, height: height }; -} +CPSizeMake = CGSizeMake; /*! Returns \c YES if the CGRect, \c aRect, contains @@ -267,93 +202,63 @@ function CPSizeMake(width, height) @group CGRect @return BOOL \c YES if the rectangle contains the point, \c NO otherwise. */ -function CPRectContainsPoint(aRect, aPoint) -{ - return aPoint.x >= CPRectGetMinX(aRect) && - aPoint.y >= CPRectGetMinY(aRect) && - aPoint.x < CPRectGetMaxX(aRect) && - aPoint.y < CPRectGetMaxY(aRect); -} +CPRectContainsPoint = CGRectContainsPoint; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetHeight(aRect) -{ - return aRect.size.height; -} +CPRectGetHeight = CGRectGetHeight; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetMaxX(aRect) -{ - return aRect.origin.x + aRect.size.width; -} +CPRectGetMaxX = CGRectGetMaxX; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetMaxY(aRect) -{ - return aRect.origin.y + aRect.size.height; -} +CPRectGetMaxY = CGRectGetMaxY; /*! @group CGRect @param aRect a CGRect @return float */ -function CPRectGetMidX(aRect) -{ - return aRect.origin.x + (aRect.size.width) / 2.0; -} +CPRectGetMidX = CGRectGetMidX; /*! @group CGRect @param aRect a CGRect @return float */ -function CPRectGetMidY(aRect) -{ - return aRect.origin.y + (aRect.size.height) / 2.0; -} +CPRectGetMidY = CGRectGetMidY; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetMinX(aRect) -{ - return aRect.origin.x; -} +CPRectGetMinX = CGRectGetMinX; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetMinY(aRect) -{ - return aRect.origin.y; -} +CPRectGetMinY = CGRectGetMinY; /*! @group CGRect @param aRect a CGRect @return int */ -function CPRectGetWidth(aRect) -{ - return aRect.size.width; -} +CPRectGetWidth = CGRectGetWidth; /*! Returns \c YES if the two rectangles intersect @@ -362,10 +267,7 @@ function CPRectGetWidth(aRect) @param rhsRect the second CGRect @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. */ -function CPRectIntersectsRect(lhsRect, rhsRect) -{ - return !CPRectIsEmpty(CPRectIntersection(lhsRect, rhsRect)); -} +CPRectIntersectsRect = CGRectIntersectsRect; /*! Returns \c YES if the CGRect has no area. @@ -373,10 +275,7 @@ function CPRectIntersectsRect(lhsRect, rhsRect) @group CGRect @return BOOL \c YES if the CGRect has no area, and \c NO, otherwise. */ -function CPRectIsNull(aRect) -{ - return aRect.size.width <= 0.0 || aRect.size.height <= 0.0; -} +CPRectIsNull = CGRectIsNull; /*! Creates two rectangles -- slice and rem -- from inRect, by dividing inRect @@ -387,10 +286,7 @@ function CPRectIsNull(aRect) @group CGRect */ -function CPDivideRect(inRect, slice, rem, amount, edge) -{ - CGRectDivide(inRect, slice, rem, amount, edge); -} +CPDivideRect = CGRectDivide; /*! Returns \c YES if the two CGSizes are identical. @@ -399,10 +295,7 @@ function CPDivideRect(inRect, slice, rem, amount, edge) @param rhsSize the second CGSize to compare @return BOOL \c YES if the two sizes are identical. \c NO, otherwise. */ -function CPSizeEqualToSize(lhsSize, rhsSize) -{ - return lhsSize.width == rhsSize.width && lhsSize.height == rhsSize.height; -} +CPSizeEqualToSize = CGSizeEqualToSize; /*! Returns a human readable string of the provided CGPoint. @@ -410,10 +303,7 @@ function CPSizeEqualToSize(lhsSize, rhsSize) @param aPoint the point to represent @return CGPoint a string representation of the CGPoint */ -function CPStringFromPoint(aPoint) -{ - return "{" + aPoint.x + ", " + aPoint.y + "}"; -} +CPStringFromPoint = CGStringFromPoint; /*! Returns a human readable string of the provided CGSize. @@ -421,10 +311,7 @@ function CPStringFromPoint(aPoint) @param aSize the size to represent @return CGSize a string representation of the CGSize */ -function CPStringFromSize(aSize) -{ - return "{" + aSize.width + ", " + aSize.height + "}"; -} +CPStringFromSize = CGStringFromSize; /*! Returns a human readable string of the provided CGRect. @@ -432,10 +319,7 @@ function CPStringFromSize(aSize) @param aRect the rectangle to represent @return CGString the string representation of the rectangle */ -function CPStringFromRect(aRect) -{ - return "{" + CPStringFromPoint(aRect.origin) + ", " + CPStringFromSize(aRect.size) + "}"; -} +CPStringFromRect = CGStringFromRect; /*! Returns a CGPoint from a string with a comma separated pair of integers. @@ -443,12 +327,7 @@ function CPStringFromRect(aRect) @param aString a string containing two comma separated integers @return CGPoint the point object created from the string */ -function CPPointFromString(aString) -{ - var comma = aString.indexOf(','); - - return { x:parseFloat(aString.substr(1, comma - 1), 10), y:parseFloat(aString.substring(comma + 1, aString.length), 10) }; -} +CPPointFromString = CGPointFromString; /*! Returns a CGSize from a string containing a pair of comma separated integers. @@ -456,12 +335,7 @@ function CPPointFromString(aString) @param aString a string containing two comma separated integers @return CGSize the size object created from the string */ -function CPSizeFromString(aString) -{ - var comma = aString.indexOf(','); - - return { width:parseFloat(aString.substr(1, comma - 1), 10), height:parseFloat(aString.substring(comma + 1, aString.length), 10) }; -} +CPSizeFromString = CGSizeFromString; /*! Returns a CGRect created from a string. @@ -469,53 +343,32 @@ function CPSizeFromString(aString) @param aString a string in the form generated by \c CPStringFromRect @return CGRect the rectangle created from the string */ -function CPRectFromString(aString) -{ - var comma = aString.indexOf(',', aString.indexOf(',') + 1); - - return { origin:CPPointFromString(aString.substr(1, comma - 1)), size:CPSizeFromString(aString.substring(comma + 2, aString.length)) }; -} +CPRectFromString = CGRectFromString; /*! @group CGPoint @param anEvent @return CGPoint */ -function CPPointFromEvent(anEvent) -{ - return CPPointMake(anEvent.clientX, anEvent.clientY, 0); -} +CPPointFromEvent = CGPointFromEvent; /*! Returns a zero sized CGSize. @group CGSize @return CGSize a size object with zeros for \c width and \c height */ -function CPSizeMakeZero() -{ - return CPSizeMake(0, 0); -} +CPSizeMakeZero = CGSizeMakeZero; /*! Returns a rectangle at origin \c (0,0) and size of \c (0,0). @group CGRect @return CGRect a zeroed out CGRect */ -function CPRectMakeZero() -{ - return CPRectMake(0, 0, 0, 0); -} +CPRectMakeZero = CGRectMakeZero; /*! Returns a point located at \c (0, 0). @group CGPoint @return CGPoint a point located at \c (0, 0) */ -function CPPointMakeZero() -{ - return CPPointMake(0, 0, 0); -} - -/*! - @} -*/ +CPPointMakeZero = CGPointMakeZero; diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index 92ab7b1c1..d08d9dddf 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -64,4 +64,254 @@ [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; } +- (void)testCPPointMake +{ + var testPoint = CPPointMake(1,2); + [self assert:1 equals:testPoint.x message:"point x coordinate failed"]; + [self assert:2 equals:testPoint.y message:"point y coordinate failed"]; +} + +- (void)testCPRectInset +{ + var testRect = CGRectMake(0, 0, 100, 100); + [self assertTrue:CGRectEqualToRect(CGRectMake(10, 10, 80, 80), CPRectInset(testRect, 10, 10))]; +} + +- (void)testCPRectIntegral +{ + var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); + [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; +} + +- (void)testCPRectCreateCopy +{ + var rect = CGRectMake(0, 0, 100, 100); + [self assertTrue:CGRectEqualToRect(rect, CPRectCreateCopy(rect))]; +} + +- (void)testCPRectMake +{ + var rect = CPRectMake(10, 20, 30, 40); + [self assert:10 equals:rect.origin.x message:"Rect x coordinate failed"]; + [self assert:20 equals:rect.origin.y message:"Rect x coordinate failed"]; + [self assert:30 equals:rect.size.width message:"Rect width failed"]; + [self assert:40 equals:rect.size.height message:"Rect height failed"]; +} + +- (void)testCPRectOffset +{ + var initialRect = CGRectMake(10, 10, 100, 100), + offsetRect = CGRectMake(20, 20, 100, 100); + [self assertTrue:CGRectEqualToRect(offsetRect, CPRectOffset(initialRect, 10, 10))]; + [self assertFalse:CGRectEqualToRect(initialRect, CPRectOffset(initialRect, 10, 10))]; +} + +- (void)testCPRectStandardize +{ + var initialRect = CGRectMake(10, 10, -10, -10), + standardizedRect = CGRectMake(0, 0, 10, 10); + [self assertTrue:CGRectEqualToRect(standardizedRect,CPRectStandardize(initialRect))]; + [self assertFalse:CGRectEqualToRect(initialRect,CPRectStandardize(initialRect))]; +} + +- (void)testCPRectUnion +{ + var firstRect = CGRectMake(0, 0, 10, 10), + secondRect = CGRectMake(20, 20, 10, 10), + unitedRect = CGRectMake(0, 0, 30, 30); + [self assertTrue:CGRectEqualToRect(unitedRect, CPRectUnion(firstRect, secondRect))]; + [self assertFalse:CGRectEqualToRect(firstRect, CPRectUnion(firstRect, secondRect))]; +} + +- (void)testCPSizeCreateCopy +{ + var initialSize = CGSizeMake(100, 200); + [self assertTrue:CGSizeEqualToSize(initialSize, CPSizeCreateCopy(initialSize))]; + [self assertFalse:CGSizeEqualToSize(CGSizeMake(100, 10), CPSizeCreateCopy(initialSize))]; +} + +- (void)testCPSizeMake +{ + var size = CPSizeMake(10, 20); + [self assert:10 equals:size.width message:"Size width failed"]; + [self assert:20 equals:size.height message:"Size height failed"]; +} + +- (void)testCPRectContainsPoint +{ + var rect = CGRectMake(10, 10, 10, 10); + [self assertTrue:CPRectContainsPoint(rect, CGPointMake(15, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(15, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 5))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(5, 25))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(15, 25))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 15))]; + [self assertFalse:CPRectContainsPoint(rect, CGPointMake(25, 25))]; +} + +- (void)testCPRectGetHeight +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assert:40 equals:CPRectGetHeight(rect)]; +} + +- (void)testCPRectGetMaxX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:50 equals:CPRectGetMaxX(rect)]; +} + +- (void)testCPRectGetMaxY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:100 equals:CPRectGetMaxY(rect)]; +} + +- (void)testCPRectGetMidX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:30 equals:CPRectGetMidX(rect)]; +} + +- (void)testCPRectGetMidY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:60 equals:CPRectGetMidY(rect)]; +} + +- (void)testCPRectGetMinX +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:10 equals:CPRectGetMinX(rect)]; +} + +- (void)testCPRectGetMinY +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:20 equals:CPRectGetMinY(rect)]; +} + +- (void)testCPRectGetWidth +{ + var rect = CGRectMake(10, 20, 40, 80); + [self assert:40 equals:CPRectGetWidth(rect)]; +} + +- (void)testCPRectIntersectsRect +{ + var firstRect = CGRectMake(10, 10, 10, 10), + secondRect = CGRectMake(15, 15, 10, 10); + [self assertTrue:CPRectIntersectsRect(firstRect, secondRect)]; + secondRect.origin = CGPointMake(25, 25); + [self assertFalse:CPRectIntersectsRect(firstRect, secondRect)]; +} + +- (void)testCPRectIsNull +{ + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 0, 10))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 10, 0))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, -10, 10))]; + [self assertTrue:CPRectIsNull(CGRectMake(0, 0, 10, -10))]; + [self assertFalse:CPRectIsNull(CGRectMake(0, 0, 10, 10))]; +} + +- (void)testCPDivideRect +{ + var initialRect = CGRectMake(0, 0, 10, 10), + slice = CGRectMake(0, 0, 0, 0), + rem = CGRectMake(0, 0, 0, 0); + + CPDivideRect(initialRect, slice, rem, 3, CGMinXEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 0, 3, 10))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(3, 0, 7, 10))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMinYEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 0, 10, 3))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 3, 10, 7))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMaxXEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(7, 0, 3, 10))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 0, 7, 10))]; + + CPDivideRect(initialRect, slice, rem, 3, CGMaxYEdge); + [self assertTrue:CGRectEqualToRect(slice, CGRectMake(0, 7, 10, 3))]; + [self assertTrue:CGRectEqualToRect(rem, CGRectMake(0, 0, 10, 7))]; +} + +- (void)testCPSizeEqualToSize +{ + [self assertTrue:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(0, 10), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(0, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 0), CGSizeMake(10, 10))]; + [self assertFalse:CPSizeEqualToSize(CGSizeMake(10, 10), CGSizeMake(10, 0))]; +} + +- (void)testCPStringFromPoint +{ + var point = CGPointMake(0, 1); + [self assert:"{0, 1}" equals: CPStringFromPoint(point)]; +} + +- (void)testCPStringFromSize +{ + var size = CGSizeMake(10, 20); + [self assert:"{10, 20}" equals: CPStringFromSize(size)]; +} + +- (void)testCPStringFromRect +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assert:"{{10, 20}, {30, 40}}" equals:CPStringFromRect(rect)]; +} + +- (void)testCPPointFromString +{ + var point = CGPointMake(0,1); + [self assertTrue:CGPointEqualToPoint(point , CPPointFromString("{0, 1}"))]; +} + +- (void)testCPSizeFromString +{ + var size = CGSizeMake(10, 20); + [self assertTrue:CGSizeEqualToSize(size, CPSizeFromString("{10, 20}"))]; +} + +- (void)testCPRectFromString +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assertTrue:CGRectEqualToRect(rect, CPRectFromString("{{10, 20}, {30, 40}}"))]; +} + +- (void)testCPSizeMakeZero +{ + var size = CGSizeMake(10, 20); + [self assertTrue:CGSizeEqualToSize(CGSizeMake(0, 0), CPSizeMakeZero(size))]; +} + +- (void)testCPRectMakeZero +{ + var rect = CGRectMake(10, 20, 30, 40); + [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 0, 0), CPRectMakeZero(rect))]; +} + +- (void)testCPPointMakeZero +{ + var point = CGPointMake(10, 20); + [self assertTrue:CGPointEqualToPoint(CGPointMake(0, 0), CPPointMakeZero(0, 0))]; +} + +- (void)testCPPointFromEvent +{ + var anEvent = [CPEvent mouseEventWithType:CPLeftMouseDownMask location:CGPointMake(5.0, 5.0) modifierFlags:0 + timestamp:0 windowNumber:0 context:nil eventNumber:0 clickCount:1 pressure:1]; + anEvent.clientX = 1.0; + anEvent.clientY = 2.0; + var point = CPPointFromEvent(anEvent); + [self assert:1.0 equals:point.x message:"Point x coordinate failed"]; + [self assert:2.0 equals:point.y message:"Point y coordinate failed"]; +} + @end From 028965e7088fede898fc56999a297cdbbf294c84 Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Fri, 13 Jul 2012 03:43:12 +0700 Subject: [PATCH 5/9] mistakes corrected --- Foundation/CPGeometry.j | 5 +++++ Tests/Foundation/CPGeometryTest.j | 36 +++++++++++++++++++------------ 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index 2bb19a4c2..fc05cbe75 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -21,6 +21,11 @@ */ @import +        +CPMinXEdge = 0; +CPMinYEdge = 1; +CPMaxXEdge = 2; +CPMaxYEdge = 3; /*! Makes a CGPoint object out of two numbers provided as arguments diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index d08d9dddf..ce4c2ea12 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -59,14 +59,16 @@ - (void)testCPPointCreateCopy { - var point = CGPointMake(1, 1); - [self assertTrue:CPPointEqualToPoint(CPPointCreateCopy(point),point)]; - [self assertFalse:CPPointEqualToPoint(CPPointCreateCopy(point), CGPointMake(0,1))]; + var point = CGPointMake(1, 1), + copiedPoint = CPPointCreateCopy(point); + [self assertTrue:CPPointEqualToPoint(point, copiedPoint)]; + point.x = 10; + [self assertFalse:CPPointEqualToPoint(point, copiedPoint)]; } - (void)testCPPointMake { - var testPoint = CPPointMake(1,2); + var testPoint = CPPointMake(1, 2); [self assert:1 equals:testPoint.x message:"point x coordinate failed"]; [self assert:2 equals:testPoint.y message:"point y coordinate failed"]; } @@ -77,16 +79,19 @@ [self assertTrue:CGRectEqualToRect(CGRectMake(10, 10, 80, 80), CPRectInset(testRect, 10, 10))]; } -- (void)testCPRectIntegral -{ - var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); - [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; -} +// - (void)testCPRectIntegral +// { +// var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); +// [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; +// } - (void)testCPRectCreateCopy { - var rect = CGRectMake(0, 0, 100, 100); - [self assertTrue:CGRectEqualToRect(rect, CPRectCreateCopy(rect))]; + var rect = CGRectMake(0, 0, 100, 100), + copiedRect = CPRectCreateCopy(rect); + [self assertTrue:CGRectEqualToRect(rect, copiedRect)]; + rect.origin.x = 10; + [self assertFalse:CGRectEqualToRect(rect, copiedRect)]; } - (void)testCPRectMake @@ -125,9 +130,12 @@ - (void)testCPSizeCreateCopy { - var initialSize = CGSizeMake(100, 200); - [self assertTrue:CGSizeEqualToSize(initialSize, CPSizeCreateCopy(initialSize))]; - [self assertFalse:CGSizeEqualToSize(CGSizeMake(100, 10), CPSizeCreateCopy(initialSize))]; + var initialSize = CGSizeMake(100, 200), + copiedSize = CPSizeCreateCopy(initialSize); + [self assertTrue:CGSizeEqualToSize(initialSize, copiedSize)]; + // [self assertFalse:CGSizeEqualToSize(CGSizeMake(100, 10), CPSizeCreateCopy(initialSize))]; + initialSize.width = 10; + [self assertFalse:CGSizeEqualToSize(initialSize, copiedSize)]; } - (void)testCPSizeMake From ff074294eea9a7291ccaa24e81770bde00b2d551 Mon Sep 17 00:00:00 2001 From: Vladimir Shevchenko Date: Fri, 13 Jul 2012 03:47:34 +0700 Subject: [PATCH 6/9] mistakes corrected --- Tests/Foundation/CPGeometryTest.j | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Tests/Foundation/CPGeometryTest.j b/Tests/Foundation/CPGeometryTest.j index ce4c2ea12..2aa9fb909 100644 --- a/Tests/Foundation/CPGeometryTest.j +++ b/Tests/Foundation/CPGeometryTest.j @@ -79,11 +79,11 @@ [self assertTrue:CGRectEqualToRect(CGRectMake(10, 10, 80, 80), CPRectInset(testRect, 10, 10))]; } -// - (void)testCPRectIntegral -// { -// var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); -// [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; -// } +- (void)testCPRectIntegral +{ + var rect = CGRectMake(0.1, 0.2, 10.3, 10.4); + [self assertTrue:CGRectEqualToRect(CGRectMake(0, 0, 11, 11), CPRectIntegral(rect))]; +} - (void)testCPRectCreateCopy { @@ -133,7 +133,6 @@ var initialSize = CGSizeMake(100, 200), copiedSize = CPSizeCreateCopy(initialSize); [self assertTrue:CGSizeEqualToSize(initialSize, copiedSize)]; - // [self assertFalse:CGSizeEqualToSize(CGSizeMake(100, 10), CPSizeCreateCopy(initialSize))]; initialSize.width = 10; [self assertFalse:CGSizeEqualToSize(initialSize, copiedSize)]; } From ae4b7cb58e75d2666fa8b50ce688cc7ae40cbfe7 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 13 Jul 2012 01:26:14 +0100 Subject: [PATCH 7/9] Sort imports. --- AppKit/AppKit.j | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index 783ca68eb..39204cc56 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -20,6 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +@import "_CPToolTip.j" @import "CALayer.j" @import "CPAccordionView.j" @import "CPAlert.j" @@ -36,10 +37,10 @@ @import "CPCibBindingConnector.j" @import "CPCibConnector.j" @import "CPCibControlConnector.j" +@import "CPCibHelpConnector.j" @import "CPCibLoading.j" @import "CPCibOutletConnector.j" @import "CPCibRuntimeAttributesConnector.j" -@import "CPCibHelpConnector.j" @import "CPClipView.j" @import "CPCollectionView.j" @import "CPCollectionViewItem.j" @@ -98,13 +99,12 @@ @import "CPTokenField.j" @import "CPToolbar.j" @import "CPToolbarItem.j" -@import "_CPToolTip.j" @import "CPTreeNode.j" @import "CPUserDefaultsController.j" @import "CPView.j" @import "CPViewAnimation.j" @import "CPViewController.j" @import "CPWebView.j" -@import "CPWorkspace.j" @import "CPWindow.j" @import "CPWindowController.j" +@import "CPWorkspace.j" From b9b98a39bb14dde4c2d80e03c98ee1333116ff0f Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 13 Jul 2012 01:28:38 +0100 Subject: [PATCH 8/9] Avoid Foundation depending on AppKit. CPGeometry.j and CGGeometry.j now share their implementation in two private API classes in the lower layer Foundation. --- AppKit/AppKit.j | 1 + AppKit/CoreGraphics/CGAffineTransform.h | 2 +- AppKit/CoreGraphics/CGGeometry.j | 303 +--------------- Foundation/CPGeometry.j | 4 +- Foundation/Jakefile | 8 +- .../CGGeometry.h => Foundation/_CGGeometry.h | 8 +- Foundation/_CGGeometry.j | 326 ++++++++++++++++++ 7 files changed, 347 insertions(+), 305 deletions(-) rename AppKit/CoreGraphics/CGGeometry.h => Foundation/_CGGeometry.h (91%) create mode 100644 Foundation/_CGGeometry.j diff --git a/AppKit/AppKit.j b/AppKit/AppKit.j index 39204cc56..27f3a3001 100644 --- a/AppKit/AppKit.j +++ b/AppKit/AppKit.j @@ -22,6 +22,7 @@ @import "_CPToolTip.j" @import "CALayer.j" +@import "CGGeometry.j" @import "CPAccordionView.j" @import "CPAlert.j" @import "CPAnimation.j" diff --git a/AppKit/CoreGraphics/CGAffineTransform.h b/AppKit/CoreGraphics/CGAffineTransform.h index 1b3fc9266..4eea12e67 100644 --- a/AppKit/CoreGraphics/CGAffineTransform.h +++ b/AppKit/CoreGraphics/CGAffineTransform.h @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "CGGeometry.h" +#include "../../Foundation/_CGGeometry.h" #define _CGAffineTransformMake(a_, b_, c_, d_, tx_, ty_) { a:a_, b:b_, c:c_, d:d_, tx:tx_, ty:ty_ } #define _CGAffineTransformMakeIdentity() _CGAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0) diff --git a/AppKit/CoreGraphics/CGGeometry.j b/AppKit/CoreGraphics/CGGeometry.j index 957662a00..bdefc9bd6 100644 --- a/AppKit/CoreGraphics/CGGeometry.j +++ b/AppKit/CoreGraphics/CGGeometry.j @@ -2,8 +2,8 @@ * CGGeometry.j * AppKit * - * Created by Francisco Tolmasky. - * Copyright 2008, 280 North, Inc. + * Created by Alexander Ljungberg. + * Copyright 2012, SlevenBits Ltd. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,301 +20,4 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#define _function(inline) function inline { return _##inline; } - -_function(CGPointMake(x, y)) -_function(CGPointMakeZero()) -_function(CGPointMakeCopy(aPoint)) -_function(CGPointCreateCopy(aPoint)) - -_function(CGPointEqualToPoint(lhsPoint, rhsPoint)) -_function(CGStringFromPoint(aPoint)) - -_function(CGSizeMake(width, height)) -_function(CGSizeMakeZero()) -_function(CGSizeMakeCopy(aSize)) -_function(CGSizeCreateCopy(aSize)) - -_function(CGSizeEqualToSize(lhsSize, rhsSize)) -_function(CGStringFromSize(aSize)) - -_function(CGRectMake(x, y, width, height)) -_function(CGRectMakeZero()) -_function(CGRectMakeCopy(aRect)) -_function(CGRectCreateCopy(aRect)) - -_function(CGRectEqualToRect(lhsRect, rhsRect)) -_function(CGStringFromRect(aRect)) - -_function(CGRectOffset(aRect, dX, dY)) -_function(CGRectInset(aRect, dX, dY)) - -_function(CGRectGetHeight(aRect)) -_function(CGRectGetMaxX(aRect)) -_function(CGRectGetMaxY(aRect)) -_function(CGRectGetMidX(aRect)) -_function(CGRectGetMidY(aRect)) -_function(CGRectGetMinX(aRect)) -_function(CGRectGetMinY(aRect)) -_function(CGRectGetWidth(aRect)) - -_function(CGRectIsEmpty(aRect)) -_function(CGRectIsNull(aRect)) - -_function(CGRectContainsPoint(aRect, aPoint)) - -_function(CGInsetMake(top, right, bottom, left)) -_function(CGInsetMakeZero()) -_function(CGInsetMakeCopy(anInset)) -_function(CGInsetIsEmpty(anInset)) -_function(CGInsetEqualToInset(lhsInset, rhsInset)) - -CGMinXEdge = 0; -CGMinYEdge = 1; -CGMaxXEdge = 2; -CGMaxYEdge = 3; - -CGRectNull = _CGRectMake(Infinity, Infinity, 0.0, 0.0); - -/*! - @addtogroup appkit - @{ -*/ - -/*! - Creates two rectangles -- slice and rem -- from inRect, by dividing inRect - with a line that's parallel to the side of inRect specified by edge. - The size of slice is determined by amount, which specifies the distance from edge. - - slice and rem must not be NULL, must not be the same object, and must not be the - same object as inRect. - - @group CGRect -*/ -function CGRectDivide(inRect, slice, rem, amount, edge) -{ - slice.origin = _CGPointMakeCopy(inRect.origin); - slice.size = _CGSizeMakeCopy(inRect.size); - rem.origin = _CGPointMakeCopy(inRect.origin); - rem.size = _CGSizeMakeCopy(inRect.size); - - switch (edge) - { - case CGMinXEdge: - slice.size.width = amount; - rem.origin.x += amount; - rem.size.width -= amount; - break; - - case CGMaxXEdge: - slice.origin.x = _CGRectGetMaxX(slice) - amount; - slice.size.width = amount; - rem.size.width -= amount; - break; - - case CGMinYEdge: - slice.size.height = amount; - rem.origin.y += amount; - rem.size.height -= amount; - break; - - case CGMaxYEdge: - slice.origin.y = _CGRectGetMaxY(slice) - amount; - slice.size.height = amount; - rem.size.height -= amount; - } -} - -/*! - Returns a \c BOOL indicating whether CGRect \c lhsRect - contains CGRect \c rhsRect. - @group CGRect - @param lhsRect the CGRect to test if \c rhsRect is inside of - @param rhsRect the CGRect to test if it fits inside \c lhsRect. - @return BOOL \c YES if \c rhsRect fits inside \c lhsRect. -*/ -function CGRectContainsRect(lhsRect, rhsRect) -{ - var union = CGRectUnion(lhsRect, rhsRect); - - return _CGRectEqualToRect(union, lhsRect); -} - -/*! - Returns \c YES if the two rectangles intersect - @group CGRect - @param lhsRect the first CGRect - @param rhsRect the second CGRect - @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. -*/ -function CGRectIntersectsRect(lhsRect, rhsRect) -{ - var intersection = CGRectIntersection(lhsRect, rhsRect); - - return !_CGRectIsEmpty(intersection); -} - -/*! - Makes the origin and size of a CGRect all integers. Specifically, by making - the southwest corner the origin (rounded down), and the northeast corner a CGSize (rounded up). - @param aRect the rectangle to operate on - @return CGRect the modified rectangle (same as the input) - @group CGRect -*/ -function CGRectIntegral(aRect) -{ - aRect = CGRectStandardize(aRect); - - // Store these out separately, if not the GetMaxes will return incorrect values. - var x = FLOOR(_CGRectGetMinX(aRect)), - y = FLOOR(_CGRectGetMinY(aRect)); - - aRect.size.width = CEIL(_CGRectGetMaxX(aRect)) - x; - aRect.size.height = CEIL(_CGRectGetMaxY(aRect)) - y; - - aRect.origin.x = x; - aRect.origin.y = y; - - return aRect; -} - -/*! - Returns the intersection of the two provided rectangles as a new rectangle. - @param lhsRect the first rectangle used for calculation - @param rhsRect the second rectangle used for calculation - @return CGRect the intersection of the two rectangles - @group CGRect -*/ -function CGRectIntersection(lhsRect, rhsRect) -{ - var intersection = _CGRectMake( - MAX(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), - MAX(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), - 0, 0); - - intersection.size.width = MIN(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)) - _CGRectGetMinX(intersection); - intersection.size.height = MIN(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)) - _CGRectGetMinY(intersection); - - return _CGRectIsEmpty(intersection) ? _CGRectMakeZero() : intersection; -} - -/* - -*/ -function CGRectStandardize(aRect) -{ - var width = _CGRectGetWidth(aRect), - height = _CGRectGetHeight(aRect), - standardized = _CGRectMakeCopy(aRect); - - if (width < 0.0) - { - standardized.origin.x += width; - standardized.size.width = -width; - } - - if (height < 0.0) - { - standardized.origin.y += height; - standardized.size.height = -height; - } - - return standardized; -} - -function CGRectUnion(lhsRect, rhsRect) -{ - var lhsRectIsNull = !lhsRect || lhsRect === CGRectNull, - rhsRectIsNull = !rhsRect || rhsRect === CGRectNull; - - if (lhsRectIsNull) - return rhsRectIsNull ? CGRectNull : rhsRect; - - if (rhsRectIsNull) - return lhsRectIsNull ? CGRectNull : lhsRect; - - var minX = MIN(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), - minY = MIN(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), - maxX = MAX(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)), - maxY = MAX(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)); - - return _CGRectMake(minX, minY, maxX - minX, maxY - minY); -} - -/*! - Returns the specified rectangle inset by the given CGInset as a new rectangle. - @param aRect the rect to inset - @param anInset a CGInset to inset by - @return CGRect aRect inset by anInset -*/ -_function(CGRectInsetByInset(aRect, anInset)); - -function CGPointFromString(aString) -{ - var comma = aString.indexOf(','); - - return { x:parseFloat(aString.substr(1, comma - 1)), y:parseFloat(aString.substring(comma + 1, aString.length)) }; -} - -function CGSizeFromString(aString) -{ - var comma = aString.indexOf(','); - - return { width:parseFloat(aString.substr(1, comma - 1)), height:parseFloat(aString.substring(comma + 1, aString.length)) }; -} - -function CGRectFromString(aString) -{ - var comma = aString.indexOf(',', aString.indexOf(',') + 1); - - return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) }; -} - -function CGPointFromEvent(anEvent) -{ - return _CGPointMake(anEvent.clientX, anEvent.clientY); -} - -/*! - Combines two insets by adding their individual elements and returns the result. - - @group CGInset -*/ -function CGInsetUnion(lhsInset, rhsInset) -{ - return _CGInsetMake(lhsInset.top + rhsInset.top, - lhsInset.right + rhsInset.right, - lhsInset.bottom + rhsInset.bottom, - lhsInset.left + rhsInset.left); -} - -/*! - Subtract one inset from another by subtracting their individual elements and returns the result. - - @group CGInset -*/ -function CGInsetDifference(lhsInset, rhsInset) -{ - return _CGInsetMake(lhsInset.top - rhsInset.top, - lhsInset.right - rhsInset.right, - lhsInset.bottom - rhsInset.bottom, - lhsInset.left - rhsInset.left); -} - -function CGInsetFromString(aString) -{ - var numbers = aString.substr(1, aString.length - 2).split(','); - - return _CGInsetMake(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]), parseFloat(numbers[3])); -} - -CGInsetFromCPString = CGInsetFromString; - -function CPStringFromCGInset(anInset) -{ - return '{' + anInset.top + ", " + anInset.left + ", " + anInset.bottom + ", " + anInset.right + '}'; -} - -/*! - @} -*/ +@import diff --git a/Foundation/CPGeometry.j b/Foundation/CPGeometry.j index fc05cbe75..ba3c249f4 100644 --- a/Foundation/CPGeometry.j +++ b/Foundation/CPGeometry.j @@ -1,6 +1,6 @@ /* * CPGeometry.j - * AppKit + * Foundation * * Created by Francisco Tolmasky. * Copyright 2008, 280 North, Inc. @@ -20,7 +20,7 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -@import +@import "_CGGeometry.j"         CPMinXEdge = 0; CPMinYEdge = 1; diff --git a/Foundation/Jakefile b/Foundation/Jakefile index 90a5bdba9..20d218c9e 100644 --- a/Foundation/Jakefile +++ b/Foundation/Jakefile @@ -42,7 +42,13 @@ foundationTask = framework ("Foundation", function(foundationTask) foundationTask.setInfoPlistPath("Info.plist"); foundationTask.setEnvironments(require("objective-j/jake/environment").ObjJ); - var INCLUDES = "--include \"../AppKit/Platform/Platform.h\" --include \"Ref.h\""; + // Grab all the .h's and just include them in each file. + var INCLUDES = new FileList("**/*.h").map(function(aFilename) + { + return "--include \"" + aFilename + "\""; + }).join(" "); + + INCLUDES = "--include \"../AppKit/Platform/Platform.h\" " + INCLUDES; if ($CONFIGURATION === "Release") foundationTask.setCompilerFlags("-O " + INCLUDES); diff --git a/AppKit/CoreGraphics/CGGeometry.h b/Foundation/_CGGeometry.h similarity index 91% rename from AppKit/CoreGraphics/CGGeometry.h rename to Foundation/_CGGeometry.h index f66a82d32..5ea60e41c 100644 --- a/AppKit/CoreGraphics/CGGeometry.h +++ b/Foundation/_CGGeometry.h @@ -1,6 +1,6 @@ /* * CGGeometry.h - * AppKit + * Foundation * * Created by Francisco Tolmasky. * Copyright 2008, 280 North, Inc. @@ -20,6 +20,12 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +/* + +CGGeometry is not a part of Foundation. The reason _CGGeometry.h exists, and is in Foundation, is that CPGeometry and CGGeometry both use the same code so the shared basis needs to be in the lowest layer. If for some reason Cappuccino was ever reimplemented such that CPRect !== CGRect etc, this class could be removed and CPGeometry.j and CGGeometry.j updated with relevant functions without breaking any client code. + +*/ + #define _CGPointMake(x_, y_) { x:x_, y:y_ } #define _CGPointMakeCopy(aPoint) _CGPointMake(aPoint.x, aPoint.y) #define _CGPointMakeZero() _CGPointMake(0.0, 0.0) diff --git a/Foundation/_CGGeometry.j b/Foundation/_CGGeometry.j new file mode 100644 index 000000000..bbb116d9b --- /dev/null +++ b/Foundation/_CGGeometry.j @@ -0,0 +1,326 @@ +/* + * _CGGeometry.j + * Foundation + * + * Created by Francisco Tolmasky. + * Copyright 2008, 280 North, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + +CGGeometry is not a part of Foundation. The reason _CGGeometry.j exists, and is in Foundation, is that CPGeometry and CGGeometry both use the same code so the shared basis needs to be in the lowest layer. If for some reason Cappuccino was ever reimplemented such that CPRect !== CGRect etc, this class could be removed and CPGeometry.j and CGGeometry.j updated with relevant functions without breaking any client code. + +*/ + +#define _function(inline) function inline { return _##inline; } + +_function(CGPointMake(x, y)) +_function(CGPointMakeZero()) +_function(CGPointMakeCopy(aPoint)) +_function(CGPointCreateCopy(aPoint)) + +_function(CGPointEqualToPoint(lhsPoint, rhsPoint)) +_function(CGStringFromPoint(aPoint)) + +_function(CGSizeMake(width, height)) +_function(CGSizeMakeZero()) +_function(CGSizeMakeCopy(aSize)) +_function(CGSizeCreateCopy(aSize)) + +_function(CGSizeEqualToSize(lhsSize, rhsSize)) +_function(CGStringFromSize(aSize)) + +_function(CGRectMake(x, y, width, height)) +_function(CGRectMakeZero()) +_function(CGRectMakeCopy(aRect)) +_function(CGRectCreateCopy(aRect)) + +_function(CGRectEqualToRect(lhsRect, rhsRect)) +_function(CGStringFromRect(aRect)) + +_function(CGRectOffset(aRect, dX, dY)) +_function(CGRectInset(aRect, dX, dY)) + +_function(CGRectGetHeight(aRect)) +_function(CGRectGetMaxX(aRect)) +_function(CGRectGetMaxY(aRect)) +_function(CGRectGetMidX(aRect)) +_function(CGRectGetMidY(aRect)) +_function(CGRectGetMinX(aRect)) +_function(CGRectGetMinY(aRect)) +_function(CGRectGetWidth(aRect)) + +_function(CGRectIsEmpty(aRect)) +_function(CGRectIsNull(aRect)) + +_function(CGRectContainsPoint(aRect, aPoint)) + +_function(CGInsetMake(top, right, bottom, left)) +_function(CGInsetMakeZero()) +_function(CGInsetMakeCopy(anInset)) +_function(CGInsetIsEmpty(anInset)) +_function(CGInsetEqualToInset(lhsInset, rhsInset)) + +CGMinXEdge = 0; +CGMinYEdge = 1; +CGMaxXEdge = 2; +CGMaxYEdge = 3; + +CGRectNull = _CGRectMake(Infinity, Infinity, 0.0, 0.0); + +/*! + @addtogroup appkit + @{ +*/ + +/*! + Creates two rectangles -- slice and rem -- from inRect, by dividing inRect + with a line that's parallel to the side of inRect specified by edge. + The size of slice is determined by amount, which specifies the distance from edge. + + slice and rem must not be NULL, must not be the same object, and must not be the + same object as inRect. + + @group CGRect +*/ +function CGRectDivide(inRect, slice, rem, amount, edge) +{ + slice.origin = _CGPointMakeCopy(inRect.origin); + slice.size = _CGSizeMakeCopy(inRect.size); + rem.origin = _CGPointMakeCopy(inRect.origin); + rem.size = _CGSizeMakeCopy(inRect.size); + + switch (edge) + { + case CGMinXEdge: + slice.size.width = amount; + rem.origin.x += amount; + rem.size.width -= amount; + break; + + case CGMaxXEdge: + slice.origin.x = _CGRectGetMaxX(slice) - amount; + slice.size.width = amount; + rem.size.width -= amount; + break; + + case CGMinYEdge: + slice.size.height = amount; + rem.origin.y += amount; + rem.size.height -= amount; + break; + + case CGMaxYEdge: + slice.origin.y = _CGRectGetMaxY(slice) - amount; + slice.size.height = amount; + rem.size.height -= amount; + } +} + +/*! + Returns a \c BOOL indicating whether CGRect \c lhsRect + contains CGRect \c rhsRect. + @group CGRect + @param lhsRect the CGRect to test if \c rhsRect is inside of + @param rhsRect the CGRect to test if it fits inside \c lhsRect. + @return BOOL \c YES if \c rhsRect fits inside \c lhsRect. +*/ +function CGRectContainsRect(lhsRect, rhsRect) +{ + var union = CGRectUnion(lhsRect, rhsRect); + + return _CGRectEqualToRect(union, lhsRect); +} + +/*! + Returns \c YES if the two rectangles intersect + @group CGRect + @param lhsRect the first CGRect + @param rhsRect the second CGRect + @return BOOL \c YES if the two rectangles have any common spaces, and \c NO, otherwise. +*/ +function CGRectIntersectsRect(lhsRect, rhsRect) +{ + var intersection = CGRectIntersection(lhsRect, rhsRect); + + return !_CGRectIsEmpty(intersection); +} + +/*! + Makes the origin and size of a CGRect all integers. Specifically, by making + the southwest corner the origin (rounded down), and the northeast corner a CGSize (rounded up). + @param aRect the rectangle to operate on + @return CGRect the modified rectangle (same as the input) + @group CGRect +*/ +function CGRectIntegral(aRect) +{ + aRect = CGRectStandardize(aRect); + + // Store these out separately, if not the GetMaxes will return incorrect values. + var x = FLOOR(_CGRectGetMinX(aRect)), + y = FLOOR(_CGRectGetMinY(aRect)); + + aRect.size.width = CEIL(_CGRectGetMaxX(aRect)) - x; + aRect.size.height = CEIL(_CGRectGetMaxY(aRect)) - y; + + aRect.origin.x = x; + aRect.origin.y = y; + + return aRect; +} + +/*! + Returns the intersection of the two provided rectangles as a new rectangle. + @param lhsRect the first rectangle used for calculation + @param rhsRect the second rectangle used for calculation + @return CGRect the intersection of the two rectangles + @group CGRect +*/ +function CGRectIntersection(lhsRect, rhsRect) +{ + var intersection = _CGRectMake( + MAX(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), + MAX(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), + 0, 0); + + intersection.size.width = MIN(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)) - _CGRectGetMinX(intersection); + intersection.size.height = MIN(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)) - _CGRectGetMinY(intersection); + + return _CGRectIsEmpty(intersection) ? _CGRectMakeZero() : intersection; +} + +/* + +*/ +function CGRectStandardize(aRect) +{ + var width = _CGRectGetWidth(aRect), + height = _CGRectGetHeight(aRect), + standardized = _CGRectMakeCopy(aRect); + + if (width < 0.0) + { + standardized.origin.x += width; + standardized.size.width = -width; + } + + if (height < 0.0) + { + standardized.origin.y += height; + standardized.size.height = -height; + } + + return standardized; +} + +function CGRectUnion(lhsRect, rhsRect) +{ + var lhsRectIsNull = !lhsRect || lhsRect === CGRectNull, + rhsRectIsNull = !rhsRect || rhsRect === CGRectNull; + + if (lhsRectIsNull) + return rhsRectIsNull ? CGRectNull : rhsRect; + + if (rhsRectIsNull) + return lhsRectIsNull ? CGRectNull : lhsRect; + + var minX = MIN(_CGRectGetMinX(lhsRect), _CGRectGetMinX(rhsRect)), + minY = MIN(_CGRectGetMinY(lhsRect), _CGRectGetMinY(rhsRect)), + maxX = MAX(_CGRectGetMaxX(lhsRect), _CGRectGetMaxX(rhsRect)), + maxY = MAX(_CGRectGetMaxY(lhsRect), _CGRectGetMaxY(rhsRect)); + + return _CGRectMake(minX, minY, maxX - minX, maxY - minY); +} + +/*! + Returns the specified rectangle inset by the given CGInset as a new rectangle. + @param aRect the rect to inset + @param anInset a CGInset to inset by + @return CGRect aRect inset by anInset +*/ +_function(CGRectInsetByInset(aRect, anInset)); + +function CGPointFromString(aString) +{ + var comma = aString.indexOf(','); + + return { x:parseFloat(aString.substr(1, comma - 1)), y:parseFloat(aString.substring(comma + 1, aString.length)) }; +} + +function CGSizeFromString(aString) +{ + var comma = aString.indexOf(','); + + return { width:parseFloat(aString.substr(1, comma - 1)), height:parseFloat(aString.substring(comma + 1, aString.length)) }; +} + +function CGRectFromString(aString) +{ + var comma = aString.indexOf(',', aString.indexOf(',') + 1); + + return { origin:CGPointFromString(aString.substr(1, comma - 1)), size:CGSizeFromString(aString.substring(comma + 2, aString.length)) }; +} + +function CGPointFromEvent(anEvent) +{ + return _CGPointMake(anEvent.clientX, anEvent.clientY); +} + +/*! + Combines two insets by adding their individual elements and returns the result. + + @group CGInset +*/ +function CGInsetUnion(lhsInset, rhsInset) +{ + return _CGInsetMake(lhsInset.top + rhsInset.top, + lhsInset.right + rhsInset.right, + lhsInset.bottom + rhsInset.bottom, + lhsInset.left + rhsInset.left); +} + +/*! + Subtract one inset from another by subtracting their individual elements and returns the result. + + @group CGInset +*/ +function CGInsetDifference(lhsInset, rhsInset) +{ + return _CGInsetMake(lhsInset.top - rhsInset.top, + lhsInset.right - rhsInset.right, + lhsInset.bottom - rhsInset.bottom, + lhsInset.left - rhsInset.left); +} + +function CGInsetFromString(aString) +{ + var numbers = aString.substr(1, aString.length - 2).split(','); + + return _CGInsetMake(parseFloat(numbers[0]), parseFloat(numbers[1]), parseFloat(numbers[2]), parseFloat(numbers[3])); +} + +CGInsetFromCPString = CGInsetFromString; + +function CPStringFromCGInset(anInset) +{ + return '{' + anInset.top + ", " + anInset.left + ", " + anInset.bottom + ", " + anInset.right + '}'; +} + +/*! + @} +*/ From a4ae36929b182fdbf0375ed4c81bd388729eb279 Mon Sep 17 00:00:00 2001 From: Alexander Ljungberg Date: Fri, 13 Jul 2012 01:29:01 +0100 Subject: [PATCH 9/9] Fix CPGeometry.j import. CPGeometry.j is now in Foundation. --- AppKit/CPImage.j | 3 +-- AppKit/CPPopUpButton.j | 3 ++- AppKit/CPView.j | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/AppKit/CPImage.j b/AppKit/CPImage.j index d87c0b5d4..f19991f7a 100644 --- a/AppKit/CPImage.j +++ b/AppKit/CPImage.j @@ -21,13 +21,12 @@ */ @import +@import @import @import @import @import -@import "CPGeometry.j" - CPImageLoadStatusInitialized = 0; CPImageLoadStatusLoading = 1; diff --git a/AppKit/CPPopUpButton.j b/AppKit/CPPopUpButton.j index cbc011db0..43d5ac3dc 100644 --- a/AppKit/CPPopUpButton.j +++ b/AppKit/CPPopUpButton.j @@ -20,8 +20,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +@import + @import "CPButton.j" -@import "CPGeometry.j" @import "CPMenu.j" @import "CPMenuItem.j" diff --git a/AppKit/CPView.j b/AppKit/CPView.j index eede974cc..8d4db509f 100644 --- a/AppKit/CPView.j +++ b/AppKit/CPView.j @@ -21,14 +21,14 @@ */ @import +@import @import @import @import "CGAffineTransform.j" -@import "CGGeometry.j" + @import "CPColor.j" -@import "CPGeometry.j" @import "CPGraphicsContext.j" @import "CPResponder.j" @import "CPTheme.j"