Skip to content

Commit

Permalink
???
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianEberius committed Apr 1, 2010
1 parent a9cacfe commit ec96599
Show file tree
Hide file tree
Showing 5 changed files with 726 additions and 191 deletions.
53 changes: 26 additions & 27 deletions AsyncBGDrawOperation.m
Expand Up @@ -47,61 +47,60 @@ - (void)setPartToDraw:(NSRect)part andRangeObject:(NSValue*)range
partToDraw = part;
rangeObject = range;
}
- (void)main
- (void)main
{
if ([self isCancelled])
if ([self isCancelled])
return;
[self partialBackgroundDraw];
}

- (void)partialBackgroundDraw
{
[[minimapView drawLock] lock];
if ([self checkCancelled])
if ([self checkCancelled])
return;
NSImage* image = [minimapView theImage];

NSRect tvBounds = [[minimapView textView] bounds];
int gutterSize = [minimapView gutterSize];
int gutterSize = [minimapView gutterSize];
float scaleFactor = tvBounds.size.height / [image size].height;
NSRect rectToRedraw = NSMakeRect(gutterSize,
partToDraw.origin.y*scaleFactor,
NSRect rectToRedraw = NSMakeRect(gutterSize,
partToDraw.origin.y*scaleFactor,
tvBounds.size.width - gutterSize,
partToDraw.size.height*scaleFactor);
NSImage* drawnPart = [[minimapView textView] snapshotByDrawingInRect:rectToRedraw];
if ([self checkCancelled])

if ([self checkCancelled])
return;

[image lockFocus];
[drawnPart drawInRect:partToDraw
fromRect:rectToRedraw
fromRect:rectToRedraw
operation:NSCompositeSourceOver fraction:1.0];
[image unlockFocus];

// NSRect visRect = [minimapView getVisiblePartOfMinimap];
// int p1 = partToDraw.origin.y;
// int p2 = partToDraw.origin.y+partToDraw.size.height;
// int l1 = visRect.origin.y;
// int l2 = visRect.origin.y+visRect.size.height;
// if ((p1>=l1 && p1<=l2) || (p2>=l1 && p2 <= l2)) {
if ([self checkCancelled])

if ([self checkCancelled])
return;
[minimapView performSelectorOnMainThread:@selector(smallRefresh) withObject:NULL waitUntilDone:NO];

// }
if ([self checkCancelled])
NSRect visRect = [minimapView getVisiblePartOfMinimap];
int p1 = partToDraw.origin.y;
int p2 = partToDraw.origin.y+partToDraw.size.height;
int l1 = visRect.origin.y;
int l2 = visRect.origin.y+visRect.size.height;
if ((p1>=l1 && p1<=l2) || (p2>=l1 && p2 <= l2)) {
[minimapView performSelectorOnMainThread:@selector(smallRefresh) withObject:NULL waitUntilDone:NO];
}
if ([self checkCancelled])
return;
[updater performSelectorOnMainThread:@selector(rangeWasRedrawn:) withObject:rangeObject waitUntilDone:YES];

[[minimapView drawLock] unlock];
}

- (BOOL)checkCancelled
{
if ([self isCancelled]) {
[[minimapView drawLock] unlock];
return YES;
return YES;
}
return NO;
}
Expand All @@ -115,8 +114,8 @@ - (NSRect)scaleRect:(NSRect)rect withFactor:(float)factor
*/
- (NSBitmapImageRep*)cropImageRep:(NSBitmapImageRep*)rep ToRect:(NSRect)rect {
CGImageRef cgImg = CGImageCreateWithImageInRect([rep CGImage], NSRectToCGRect(rect)); NSBitmapImageRep *result = [[NSBitmapImageRep alloc] initWithCGImage:cgImg];
CGImageRelease(cgImg);

CGImageRelease(cgImg);
return [result autorelease];
}

Expand Down
11 changes: 6 additions & 5 deletions BackgroundUpdater.m
Expand Up @@ -33,9 +33,10 @@ - (void) dealloc
- (void)startRedrawInBackground
{
NSRect visRect = [minimapView getVisiblePartOfMinimap];
[operationQueue cancelAllOperations];
[operationQueue setSuspended:YES];
for (NSValue* val in dirtyRegions) {
NSRange range = [val rangeValue];
NSRange range = [val rangeValue];
NSRect rectToDraw = NSMakeRect(visRect.origin.x, range.location-1, visRect.size.width, range.length+1);
AsyncBGDrawOperation* op = [[[AsyncBGDrawOperation alloc] initWithMinimapView:minimapView andUpdater:self] autorelease];
[op setPartToDraw:rectToDraw andRangeObject:(NSValue*)val];
Expand All @@ -53,7 +54,7 @@ - (void)setDirtyExceptForVisiblePart
{
NSImage* image = [minimapView theImage];
NSRect visRect = [minimapView getVisiblePartOfMinimap];

int i = visRect.origin.y+visRect.size.height;
int t = visRect.origin.y;
BOOL goUp = TRUE;
Expand All @@ -64,7 +65,7 @@ - (void)setDirtyExceptForVisiblePart
if ((i+length) > [image size].height) {
length = [image size].height - i;
}
NSRange range = NSMakeRange(i, length+1);
NSRange range = NSMakeRange(i, length+1);
[self setRangeDirty:range];
i=i+50;
if (i>[image size].height)
Expand All @@ -87,7 +88,7 @@ - (void)setDirtyExceptForVisiblePart
- (void)setRangeDirty:(NSRange)range
{
NSValue* val = [NSValue valueWithRange:range];
for (NSValue* v in dirtyRegions)
for (NSValue* v in dirtyRegions)
if ([v isEqualToValue:val]) {
return;
}
Expand All @@ -101,7 +102,7 @@ - (void)addDirtyRegions:(NSArray *)regions

- (void)setCompleteImageDirty
{
NSImage* image = [minimapView theImage];
NSImage* image = [minimapView theImage];
int i;
for (i=0;i<[image size].height;i=i+50) {
int length = 50;
Expand Down
60 changes: 31 additions & 29 deletions MinimapView.m
Expand Up @@ -72,7 +72,7 @@ - (void)dealloc
#pragma mark drawing routines

- (void)drawRect:(NSRect)rect
{
{
if (firstDraw) {
[windowController updateTrailingSpace];
firstDraw = NO;
Expand All @@ -82,13 +82,14 @@ - (void)drawRect:(NSRect)rect
//Defer first drawing by small interval ... don't know a better way to wait till the tv is fully initialized
NSTimer* t = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(firstRefresh) userInfo:nil repeats:NO];
[self setTimer:t];
return;
}

int numLines = [self getNumberOfLines];
float ppl = [self bounds].size.height / numLines;
float scaleUpThreshold = [[NSUserDefaults standardUserDefaults] floatForKey:@"Minimap_scaleUpThreshold"];
NSRect drawTo = [self bounds];
if (refreshAll)
if (refreshAll)
{
AsyncDrawOperation* op;
if (ppl < scaleUpThreshold)
Expand Down Expand Up @@ -127,26 +128,26 @@ - (void)drawRect:(NSRect)rect
[self drawVisRect:drawTo];
}

- (NSRect)updateVisiblePartOfImage
- (NSRect)updateVisiblePartOfImage
{
NSRect bounds = [self bounds];
int numLines = [self getNumberOfLines];

NSSize imgSize = [theImage size];
NSSize imgSize = [theImage size];
NSRect tvBounds = [textView bounds];
int scaleUpTo = [[NSUserDefaults standardUserDefaults] integerForKey:@"Minimap_scaleUpTo"];
float visiblePercentage = bounds.size.height / (numLines*scaleUpTo);
[self setViewableRangeScaling:(1/visiblePercentage)];


NSScrollView* sv = (NSScrollView*)[[textView superview] superview];
float percentage = [[sv verticalScroller] floatValue];
float scaleFactor = [theImage size].height / tvBounds.size.height;

float scaleFactor = [theImage size].height / tvBounds.size.height;
float middle = (percentage*(tvBounds.size.height-[textView visibleRect].size.height) + [textView visibleRect].size.height/2);
float mysteriousHeight = ((visiblePercentage * tvBounds.size.height) - [textView visibleRect].size.height);
float begin = middle - (percentage*mysteriousHeight) - ([textView visibleRect].size.height/2);

NSRect visRect = NSMakeRect(0,
begin*scaleFactor,
[theImage size].width,
Expand Down Expand Up @@ -209,13 +210,13 @@ - (void)drawVisRect:(NSRect)drawTo
{
visRectPos = (viewableRange.location*pixelPerLine);
}

float drawToScaling = drawTo.size.height / bounds.size.height;
NSRect visibleHighlightRect = NSMakeRect(0,
visRectPos*drawToScaling,
drawTo.size.width-1,
visRectHeight*drawToScaling);

[NSGraphicsContext saveGraphicsState];
[[NSColor colorWithCalibratedRed:0.549 green:0.756 blue:1 alpha:0.9] set];
[NSBezierPath setDefaultLineWidth:1];
Expand Down Expand Up @@ -245,28 +246,28 @@ - (void)mouseDown:(NSEvent *)theEvent
/* Ignore any other kind of event. */
break;
}

};
return;
}

- (void)scrollWheel:(NSEvent *)theEvent
{
NSRect tvBounds = [textView bounds];
float scaleFactor = [theImage size].height / tvBounds.size.height;
float scaleFactor = [theImage size].height / tvBounds.size.height;

NSRect newVisRect = visiblePartOfImage;
if (visRectPosBeforeScrolling == -1)
visRectPosBeforeScrolling = visiblePartOfImage.origin.y;

float newBegin = (newVisRect.origin.y / scaleFactor) + ([theEvent deltaY]/scaleFactor)*(-5);

if (newBegin < 0)
newBegin = 0;
float lowerBound = tvBounds.size.height-(newVisRect.size.height/scaleFactor);
if (newBegin > lowerBound)
newBegin = lowerBound;

newVisRect.origin.y = newBegin*scaleFactor;
[self setMinimapLinesStart:(newBegin/tvBounds.size.height)*[self getNumberOfLines]];
visiblePartOfImage = newVisRect;
Expand All @@ -281,7 +282,7 @@ - (BOOL)isFlipped
{
return YES;
}
- (BOOL)isOpaque
- (BOOL)isOpaque
{
return YES;
}
Expand All @@ -292,20 +293,21 @@ - (BOOL)isOpaque
- (void)refreshDisplay {
refreshAll = TRUE;
visRectPosBeforeScrolling = -1;

[self setNeedsDisplayInRect:[self visibleRect]];
}
- (void)firstRefresh {
refreshAll = TRUE;
visRectPosBeforeScrolling = -1;
[self updateGutterSize];
[updater setDirtyExceptForVisiblePart];
[self setNeedsDisplayInRect:[self visibleRect]];}

- (void)refreshViewableRange{
[self updateVisiblePartOfImage];
visRectPosBeforeScrolling = -1;
[self setNeedsDisplayInRect:[self visibleRect]];

NSTimer* old_timer = [self timer];
if (old_timer != NULL && [old_timer isValid]) {
[old_timer invalidate];
Expand Down Expand Up @@ -370,31 +372,31 @@ - (NSColor*)currentBackgroundColor

- (int)gutterSize
{
// lazy initialization...
// lazy initialization...
if (gutterSize == -1)
{
[self updateGutterSize];
}
}
return gutterSize;
}

#pragma mark drawOperation-api

- (void)asyncDrawFinished: (NSImage*) bitmap
- (void)asyncDrawFinished: (NSImage*) bitmap
{
[bitmap retain];
[theImage release];
theImage = bitmap;
if (minimapIsScrollable) {
[updater setDirtyExceptForVisiblePart];

[updater startRedrawInBackground];
}

if (minimapIsScrollable)
pixelPerLine = [[NSUserDefaults standardUserDefaults] floatForKey:@"Minimap_scaleUpTo"];
else
else
pixelPerLine = [self bounds].size.height / [self getNumberOfLines];

[self setNeedsDisplay:YES];
}

Expand All @@ -409,7 +411,7 @@ - (void)setMinimapLinesStart:(int)start
- (int)getNumberOfLines
{
unsigned int lineHeight = [textView lineHeight];

float h = [textView bounds].size.height;
int totalLines = round(h/lineHeight);
return totalLines;
Expand Down

0 comments on commit ec96599

Please sign in to comment.