mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-05-31 20:00:48 +00:00
* Source/NSGradient.m: Rearrange code to avoid duplicate
saveGraphicsState calls. git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@37407 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
803c50651d
commit
935c45f00e
2 changed files with 105 additions and 78 deletions
|
@ -1,3 +1,8 @@
|
||||||
|
2013-11-24 Fred Kiefer <FredKiefer@gmx.de>
|
||||||
|
|
||||||
|
* Source/NSGradient.m: Rearrange code to avoid duplicate
|
||||||
|
saveGraphicsState calls.
|
||||||
|
|
||||||
2013-11-24 Fred Kiefer <FredKiefer@gmx.de>
|
2013-11-24 Fred Kiefer <FredKiefer@gmx.de>
|
||||||
|
|
||||||
* Source/NSBitmapImageRep+GIF.m: Add define missing in libgif 5.0.
|
* Source/NSBitmapImageRep+GIF.m: Add define missing in libgif 5.0.
|
||||||
|
|
|
@ -42,6 +42,12 @@
|
||||||
#define PI 3.1415926535897932384626434
|
#define PI 3.1415926535897932384626434
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@interface NSGradient (Private)
|
||||||
|
- (void) _drawInRect: (NSRect)rect angle: (CGFloat)angle;
|
||||||
|
- (void) _drawInRect: (NSRect)rect
|
||||||
|
relativeCenterPosition: (NSPoint)relativeCenterPoint;
|
||||||
|
@end
|
||||||
|
|
||||||
@implementation NSGradient
|
@implementation NSGradient
|
||||||
|
|
||||||
- (NSColorSpace *) colorSpace;
|
- (NSColorSpace *) colorSpace;
|
||||||
|
@ -79,7 +85,7 @@
|
||||||
|
|
||||||
[currentContext saveGraphicsState];
|
[currentContext saveGraphicsState];
|
||||||
[path addClip];
|
[path addClip];
|
||||||
[self drawInRect: [path bounds] angle: angle];
|
[self _drawInRect: [path bounds] angle: angle];
|
||||||
[currentContext restoreGraphicsState];
|
[currentContext restoreGraphicsState];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,102 +96,28 @@
|
||||||
|
|
||||||
[currentContext saveGraphicsState];
|
[currentContext saveGraphicsState];
|
||||||
[path addClip];
|
[path addClip];
|
||||||
[self drawInRect: [path bounds] relativeCenterPosition: relativeCenterPoint];
|
[self _drawInRect: [path bounds] relativeCenterPosition: relativeCenterPoint];
|
||||||
[currentContext restoreGraphicsState];
|
[currentContext restoreGraphicsState];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) drawInRect: (NSRect)rect angle: (CGFloat)angle
|
- (void) drawInRect: (NSRect)rect angle: (CGFloat)angle
|
||||||
{
|
{
|
||||||
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
|
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
|
||||||
NSPoint startPoint;
|
|
||||||
NSPoint endPoint;
|
|
||||||
float rad;
|
|
||||||
float length;
|
|
||||||
|
|
||||||
// Normalize to 0.0 <= angle <= 360.0
|
|
||||||
while (angle < 0.0)
|
|
||||||
{
|
|
||||||
angle += 360.0;
|
|
||||||
}
|
|
||||||
while (angle > 360.0)
|
|
||||||
{
|
|
||||||
angle -= 360.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (angle < 90.0)
|
|
||||||
{
|
|
||||||
startPoint = NSMakePoint(NSMinX(rect), NSMinY(rect));
|
|
||||||
}
|
|
||||||
else if (angle < 180.0)
|
|
||||||
{
|
|
||||||
startPoint = NSMakePoint(NSMaxX(rect), NSMinY(rect));
|
|
||||||
}
|
|
||||||
else if (angle < 270.0)
|
|
||||||
{
|
|
||||||
startPoint = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
startPoint = NSMakePoint(NSMinX(rect), NSMaxY(rect));
|
|
||||||
}
|
|
||||||
rad = PI * angle / 180;
|
|
||||||
length = abs(NSWidth(rect) * cos(rad) + NSHeight(rect) * sin(rad));
|
|
||||||
endPoint = NSMakePoint(startPoint.x + length * cos(rad),
|
|
||||||
startPoint.y + length * sin(rad));
|
|
||||||
|
|
||||||
[currentContext saveGraphicsState];
|
[currentContext saveGraphicsState];
|
||||||
[NSBezierPath clipRect: rect];
|
[NSBezierPath clipRect: rect];
|
||||||
[self drawFromPoint: startPoint
|
[self _drawInRect: rect angle: angle];
|
||||||
toPoint: endPoint
|
|
||||||
options: 0];
|
|
||||||
[currentContext restoreGraphicsState];
|
[currentContext restoreGraphicsState];
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline float sqr(float a)
|
|
||||||
{
|
|
||||||
return a * a;
|
|
||||||
}
|
|
||||||
|
|
||||||
static inline float euclidian_distance(NSPoint start, NSPoint end)
|
|
||||||
{
|
|
||||||
return sqrt(sqr(end.x - start.x) + sqr(end.y - start.y));
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void) drawInRect: (NSRect)rect
|
- (void) drawInRect: (NSRect)rect
|
||||||
relativeCenterPosition: (NSPoint)relativeCenterPoint
|
relativeCenterPosition: (NSPoint)relativeCenterPoint
|
||||||
{
|
{
|
||||||
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
|
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
|
||||||
NSPoint startCenter;
|
|
||||||
NSPoint endCenter;
|
|
||||||
CGFloat endRadius;
|
|
||||||
CGFloat distance;
|
|
||||||
|
|
||||||
NSAssert(relativeCenterPoint.x >= 0.0 && relativeCenterPoint.x <= 1.0, @"NSGradient invalid relative center point");
|
|
||||||
NSAssert(relativeCenterPoint.y >= 0.0 && relativeCenterPoint.y <= 1.0, @"NSGradient invalid relative center point");
|
|
||||||
startCenter = NSMakePoint(NSMidX(rect), NSMidY(rect));
|
|
||||||
endCenter = NSMakePoint(startCenter.x + rect.size.width * relativeCenterPoint.x,
|
|
||||||
startCenter.y + rect.size.height * relativeCenterPoint.y);
|
|
||||||
endRadius = 0.0;
|
|
||||||
distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMinY(rect)));
|
|
||||||
if (endRadius < distance)
|
|
||||||
endRadius = distance;
|
|
||||||
distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMinY(rect)));
|
|
||||||
if (endRadius < distance)
|
|
||||||
endRadius = distance;
|
|
||||||
distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMaxY(rect)));
|
|
||||||
if (endRadius < distance)
|
|
||||||
endRadius = distance;
|
|
||||||
distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMaxY(rect)));
|
|
||||||
if (endRadius < distance)
|
|
||||||
endRadius = distance;
|
|
||||||
|
|
||||||
[currentContext saveGraphicsState];
|
[currentContext saveGraphicsState];
|
||||||
[NSBezierPath clipRect: rect];
|
[NSBezierPath clipRect: rect];
|
||||||
[self drawFromCenter: startCenter
|
[self _drawInRect: rect relativeCenterPosition: relativeCenterPoint];
|
||||||
radius: 0.0
|
|
||||||
toCenter: endCenter
|
|
||||||
radius: endRadius
|
|
||||||
options: 0];
|
|
||||||
[currentContext restoreGraphicsState];
|
[currentContext restoreGraphicsState];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -365,3 +297,93 @@ relativeCenterPosition: (NSPoint)relativeCenterPoint
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@implementation NSGradient (Private)
|
||||||
|
|
||||||
|
- (void) _drawInRect: (NSRect)rect angle: (CGFloat)angle
|
||||||
|
{
|
||||||
|
NSPoint startPoint;
|
||||||
|
NSPoint endPoint;
|
||||||
|
float rad;
|
||||||
|
float length;
|
||||||
|
|
||||||
|
// Normalize to 0.0 <= angle <= 360.0
|
||||||
|
while (angle < 0.0)
|
||||||
|
{
|
||||||
|
angle += 360.0;
|
||||||
|
}
|
||||||
|
while (angle > 360.0)
|
||||||
|
{
|
||||||
|
angle -= 360.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (angle < 90.0)
|
||||||
|
{
|
||||||
|
startPoint = NSMakePoint(NSMinX(rect), NSMinY(rect));
|
||||||
|
}
|
||||||
|
else if (angle < 180.0)
|
||||||
|
{
|
||||||
|
startPoint = NSMakePoint(NSMaxX(rect), NSMinY(rect));
|
||||||
|
}
|
||||||
|
else if (angle < 270.0)
|
||||||
|
{
|
||||||
|
startPoint = NSMakePoint(NSMaxX(rect), NSMaxY(rect));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
startPoint = NSMakePoint(NSMinX(rect), NSMaxY(rect));
|
||||||
|
}
|
||||||
|
rad = PI * angle / 180;
|
||||||
|
length = abs(NSWidth(rect) * cos(rad) + NSHeight(rect) * sin(rad));
|
||||||
|
endPoint = NSMakePoint(startPoint.x + length * cos(rad),
|
||||||
|
startPoint.y + length * sin(rad));
|
||||||
|
|
||||||
|
[self drawFromPoint: startPoint
|
||||||
|
toPoint: endPoint
|
||||||
|
options: 0];
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline float sqr(float a)
|
||||||
|
{
|
||||||
|
return a * a;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline float euclidian_distance(NSPoint start, NSPoint end)
|
||||||
|
{
|
||||||
|
return sqrt(sqr(end.x - start.x) + sqr(end.y - start.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void) _drawInRect: (NSRect)rect
|
||||||
|
relativeCenterPosition: (NSPoint)relativeCenterPoint
|
||||||
|
{
|
||||||
|
NSPoint startCenter;
|
||||||
|
NSPoint endCenter;
|
||||||
|
CGFloat endRadius;
|
||||||
|
CGFloat distance;
|
||||||
|
|
||||||
|
NSAssert(relativeCenterPoint.x >= 0.0 && relativeCenterPoint.x <= 1.0, @"NSGradient invalid relative center point");
|
||||||
|
NSAssert(relativeCenterPoint.y >= 0.0 && relativeCenterPoint.y <= 1.0, @"NSGradient invalid relative center point");
|
||||||
|
startCenter = NSMakePoint(NSMidX(rect), NSMidY(rect));
|
||||||
|
endCenter = NSMakePoint(startCenter.x + rect.size.width * relativeCenterPoint.x,
|
||||||
|
startCenter.y + rect.size.height * relativeCenterPoint.y);
|
||||||
|
endRadius = 0.0;
|
||||||
|
distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMinY(rect)));
|
||||||
|
if (endRadius < distance)
|
||||||
|
endRadius = distance;
|
||||||
|
distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMinY(rect)));
|
||||||
|
if (endRadius < distance)
|
||||||
|
endRadius = distance;
|
||||||
|
distance = euclidian_distance(endCenter, NSMakePoint(NSMinX(rect), NSMaxY(rect)));
|
||||||
|
if (endRadius < distance)
|
||||||
|
endRadius = distance;
|
||||||
|
distance = euclidian_distance(endCenter, NSMakePoint(NSMaxX(rect), NSMaxY(rect)));
|
||||||
|
if (endRadius < distance)
|
||||||
|
endRadius = distance;
|
||||||
|
|
||||||
|
[self drawFromCenter: startCenter
|
||||||
|
radius: 0.0
|
||||||
|
toCenter: endCenter
|
||||||
|
radius: endRadius
|
||||||
|
options: 0];
|
||||||
|
}
|
||||||
|
@end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue