summaryrefslogtreecommitdiff
path: root/tikzit/src/common/Grid.m
blob: f597a4a6953e3b8368a45a6daad199caebe0a431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright 2011  Alex Merry <dev@randomguy3.me.uk>
 * Copyright 2010  Chris Heunen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#import "Grid.h"
#import "util.h"

@implementation Grid

+ (Grid*) gridWithSpacing:(float)sp subdivisions:(int)subs transformer:(Transformer*)t {
	return [[[self alloc] initWithSpacing:sp subdivisions:subs transformer:t] autorelease];
}

- (id) initWithSpacing:(float)sp subdivisions:(int)subs transformer:(Transformer*)t {
	self = [super init];

	if (self) {
		transformer = [t retain];
		spacing = sp;
		cellSubdivisions = subs;
	}

	return self;
}

- (id) copyWithZone:(NSZone*)zone {
	return [[Grid allocWithZone:zone]
			initWithSpacing:spacing
			   subdivisions:cellSubdivisions
				transformer:transformer];
}

- (void) dealloc {
	[transformer release];
	[super dealloc];
}

- (int) cellSubdivisions {
	return cellSubdivisions;
}

- (void) setCellSubdivisions:(int)count {
	cellSubdivisions = count;
}

- (float) cellSpacing {
	return spacing;
}

- (void) setCellSpacing:(float)sp {
	spacing = sp;
}

- (NSPoint) snapScreenPoint:(NSPoint)point {
	NSPoint gridPoint = [transformer fromScreen:point];
	return [transformer toScreen:[self snapPoint:gridPoint]];
}

- (NSPoint) snapPoint:(NSPoint)p {
	const float snapDistance = spacing/(float)cellSubdivisions;
	p.x = roundToNearest (snapDistance, p.x);
	p.y = roundToNearest (snapDistance, p.y);
	return p;
}

- (void) _setupLinesForContext:(id<RenderContext>)context withSpacing:(float)offset omittingEvery:(int)omitEvery origin:(NSPoint)origin {
	NSRect clip = [context clipBoundingBox];
	float clipx1 = clip.origin.x;
	float clipx2 = clipx1 + clip.size.width;
	float clipy1 = clip.origin.y;
	float clipy2 = clipy1 + clip.size.height;

	// left of the Y axis, moving outwards
	unsigned int count = 1;
	float x = origin.x - offset;
	while (x >= clipx1) {
		if (omitEvery == 0 || (count % omitEvery != 0)) {
			[context moveTo:NSMakePoint(x, clipy1)];
			[context lineTo:NSMakePoint(x, clipy2)];
		}

		x -= offset;
		++count;
	}
	// right of the Y axis, moving outwards
	count = 1;
	x = origin.x + offset;
	while (x <= clipx2) {
		if (omitEvery == 0 || (count % omitEvery != 0)) {
			[context moveTo:NSMakePoint(x, clipy1)];
			[context lineTo:NSMakePoint(x, clipy2)];
		}

		x += offset;
		++count;
	}

	// above the Y axis, moving outwards
	count = 1;
	float y = origin.y - offset;
	while (y >= clipy1) {
		if (omitEvery == 0 || (count % omitEvery != 0)) {
			[context moveTo:NSMakePoint(clipx1, y)];
			[context lineTo:NSMakePoint(clipx2, y)];
		}

		y -= offset;
		++count;
	}
	// below the Y axis, moving outwards
	count = 1;
	y = origin.y + offset;
	while (y <= clipy2) {
		if (omitEvery == 0 || (count % omitEvery != 0)) {
			[context moveTo:NSMakePoint(clipx1, y)];
			[context lineTo:NSMakePoint(clipx2, y)];
		}

		y += offset;
		++count;
	}
}

- (void) _renderSubdivisionsWithContext:(id<RenderContext>)context origin:(NSPoint)origin cellSize:(float)cellSize {
	const float offset = cellSize / cellSubdivisions;

	[self _setupLinesForContext:context withSpacing:offset omittingEvery:cellSubdivisions origin:origin];

	[context strokePathWithColor:MakeSolidRColor (0.9, 0.9, 1.0)];
}

- (void) _renderCellsWithContext:(id<RenderContext>)context origin:(NSPoint)origin cellSize:(float)cellSize {
	[self _setupLinesForContext:context withSpacing:cellSize omittingEvery:0 origin:origin];

	[context strokePathWithColor:MakeSolidRColor (0.8, 0.8, 0.9)];
}

- (void) _renderAxesWithContext:(id<RenderContext>)context origin:(NSPoint)origin {
	NSRect clip = [context clipBoundingBox];

	[context moveTo:NSMakePoint(origin.x, clip.origin.y)];
	[context lineTo:NSMakePoint(origin.x, clip.origin.y + clip.size.height)];
	[context moveTo:NSMakePoint(clip.origin.x, origin.y)];
	[context lineTo:NSMakePoint(clip.origin.x + clip.size.width, origin.y)];

	[context strokePathWithColor:MakeSolidRColor (0.6, 0.6, 0.7)];
}

- (void) renderGridInContext:(id<RenderContext>)cr {
	[self renderGridInContext:cr transformer:transformer];
}

- (void) renderGridInContext:(id<RenderContext>)context transformer:(Transformer*)t {
	const NSPoint origin = [t toScreen:NSZeroPoint];
	const float cellSize = [t scaleToScreen:spacing];
    
	[context saveState];
    
	// common line settings
	[context setLineWidth:1.0];
	[context setAntialiasMode:AntialiasDisabled];
    
	[self _renderSubdivisionsWithContext:context origin:origin cellSize:cellSize];
	[self _renderCellsWithContext:context origin:origin cellSize:cellSize];
	[self _renderAxesWithContext:context origin:origin];
    
	[context restoreState];
}

@end

// vi:ft=objc:ts=4:noet:sts=4:sw=4