-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ruler1.pas
451 lines (401 loc) · 15.3 KB
/
Ruler1.pas
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//Copyright (c) 2012 by Donald R. Ziesig
//
//Donald.at.Ziesig.org
//
//This file is part of MagicTracks.
//
//MagicTracks 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 3 of the License, or
//(at your option) any later version.
//
//MagicTracks 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 MagicTracks. If not, see <http://www.gnu.org/licenses/>.
unit Ruler1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, StdCtrls, ExtCtrls, DrawingCommon1, Drawing1;
type
{ TRuler }
TRuler = class
private
fPB : TPaintBox;
fScrollBar : TScrollBar;
fBoxType : TDrawingBox;
function SBPosition : Double; { returns 0 .. 1.0 }
public
constructor Create( PB : TPaintBox;
ScrollBar : TScrollBar;
BoxType : TDrawingBox );
destructor Destroy; override;
procedure Draw( Drawing : TDrawing );
procedure OriginToLowerLeft( Drawing : TDrawing );
procedure Center( ScrollBar : TScrollBar; Pos : Double; Drawing : TDrawing );
end;
implementation
uses
Graphics, UnitConversion1, Preferences1, Common1;
type
TInchesDraw = ( idInchesPlusFractions,
idInchesOnly,
idSixInches,
idFeet,
idTenFeet );
const
InchesDraw : array [0..MaxZoom] of TInchesDraw =
( idInchesPlusFractions, idInchesPlusFractions, idInchesPlusFractions, idInchesPlusFractions,
idInchesPlusFractions, idInchesPlusFractions, idInchesOnly, idInchesOnly,
idInchesOnly, idInchesOnly, idSixInches, idSixInches,
idSixInches, idSixInches, idSixInches, idSixInches,
idSixInches, idFeet, idTenFeet, idTenFeet );
{ TRuler }
procedure TRuler.Center(ScrollBar : TScrollBar; Pos: Double; Drawing: TDrawing);
begin
{ TODO 1 -oDon Ziesig -cUser Interface : Implement Center Ruler on Coordinate }
end;
constructor TRuler.Create( PB: TPaintBox;
ScrollBar : TScrollBar;
BoxType : TDrawingBox );
begin
fPB := PB;
fScrollBar := ScrollBar;
fBoxType := BoxType;
end;
destructor TRuler.Destroy;
begin
end;
procedure TRuler.Draw( Drawing : TDrawing );
var
RulerRect : TRect;
PosSB : Double;
Horiz : Boolean;
DisplayWidth : Integer;
DisplayHeight : Integer;
procedure Horizontal;
var
WidthMicrons : Double;
XMin, XMax : Double;
LeftPixels : Integer;
I : Integer;
X, OldX : Integer;
begin
WidthMicrons := PixelsToMicrons( DisplayWidth, Drawing.Preferences );
case fBoxType of
XY, XZ:
if Drawing.Preferences.EnglishUnits then
begin
XMin := Drawing.XMin - 10 * FtToMicrons;
XMax := Drawing.XMax + 10 * FtToMicrons;
end;
YZ:
if Drawing.Preferences.EnglishUnits then
begin
XMin := Drawing.YMin - 10 * FtToMicrons;
XMax := Drawing.YMax + 10 * FtToMicrons;
end;
end;
LeftPixels := MicronsToPixels( PosSB * ( XMax - WidthMicrons - XMin) + XMin, Drawing.Preferences );
case InchesDraw[Drawing.Preferences.ZoomIndex] of
idInchesPlusFractions:
begin
OldX := Round(PixelsToValue(LeftPixels,Drawing.Preferences)*48);
for I := 0 to DisplayWidth do
begin
X := Round(PixelsToValue(I+LeftPixels,Drawing.Preferences)*48);
if (X <> OldX) then
begin
OldX := X;
fPB.Canvas.MoveTo(I,0);
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.2));
if (X mod 48) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut(I+3,Trunc(DisplayHeight * 0.45),IntToStr(X div 48)+'''');
end
else if (X mod 24) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut( I+3,Trunc(DisplayHeight * 0.45),
IntToStr(X div 48)+'''' + '6"');
end
else if (X mod 4) = 0 then
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.5));
end;
end;
end;
idInchesOnly:
begin
OldX := Round(PixelsToValue(LeftPixels,Drawing.Preferences)*12);
for I := 0 to DisplayWidth do
begin
X := Round(PixelsToValue(I+LeftPixels,Drawing.Preferences)*12);
if (X <> OldX) then
begin
OldX := X;
fPB.Canvas.MoveTo(I,0);
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.3));
if (X mod 6) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.5));
end;
if (X mod 12) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut(I+3,Trunc(DisplayHeight * 0.45),IntToStr(X div 12)+'''');
end;
end;
end;
end;
idSixInches:
begin
OldX := Round(PixelsToValue(LeftPixels,Drawing.Preferences)*6);
for I := 0 to DisplayWidth do
begin
X := Round(PixelsToValue(I+LeftPixels,Drawing.Preferences)*6);
if (X <> OldX) then
begin
OldX := X;
fPB.Canvas.MoveTo(I,0);
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.3));
if (X mod 3) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.5));
end;
if (X mod 6) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut(I+3,Trunc(DisplayHeight * 0.45),IntToStr(X div 6)+'''');
end;
end;
end;
end;
idFeet:
begin
OldX := Round(PixelsToValue(LeftPixels,Drawing.Preferences)*2);
for I := 0 to DisplayWidth do
begin
X := Round(PixelsToValue(I+LeftPixels,Drawing.Preferences)*2);
if (X <> OldX) then
begin
OldX := X;
fPB.Canvas.MoveTo(I,0);
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.3));
if (X mod 2) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut(I+3,Trunc(DisplayHeight * 0.45),IntToStr(X div 2)+'''');
end;
end;
end;
end;
idTenFeet:
begin
OldX := Round(PixelsToValue(LeftPixels,Drawing.Preferences));
for I := 0 to DisplayWidth do
begin
X := Round(PixelsToValue(I+LeftPixels,Drawing.Preferences));
if (X <> OldX) then
begin
OldX := X;
fPB.Canvas.MoveTo(I,0);
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.3));
if (X mod 10) = 0 then
begin
fPB.Canvas.LineTo(I,Trunc(DisplayHeight * 0.67));
fPB.Canvas.TextOut(I+3,Trunc(DisplayHeight * 0.45),IntToStr(X)+'''');
end;
end;
end;
end;
end;
end;
{ NOTE: The vertical rulers invert the coordinates. The paint box and scroll bar
have their zero coordinate at the top. The drawing uses standard +Y is
UP! }
procedure Vertical;
var
HeightMicrons : Double;
YMin, YMax : Double;
TopPixels : Integer;
Y, OldY : Integer;
I : Integer;
PosSBPrime : Double;
begin
PosSBPrime := 1.0 - PosSB;
HeightMicrons := PixelsToMicrons( DisplayHeight, Drawing.Preferences );
case fBoxType of
XY:
if Drawing.Preferences.EnglishUnits then
begin
YMin := Drawing.YMin - 10 * FtToMicrons;
YMax := Drawing.YMax + 10 * FtToMicrons;
end;
XZ, YZ:
if Drawing.Preferences.EnglishUnits then
begin
YMin := Drawing.ZMin - 10 * FtToMicrons;
YMax := Drawing.ZMax + 10 * FtToMicrons;
end;
end;
TopPixels := MicronsToPixels( PosSBPrime * ( YMax - HeightMicrons - YMin) + YMin, Drawing.Preferences );
case InchesDraw[Drawing.Preferences.ZoomIndex] of
idInchesPlusFractions:
begin
OldY := Round(PixelsToValue(TopPixels,Drawing.Preferences)*48);
for I := 0 to DisplayHeight do
begin
Y := Round(PixelsToValue(I+TopPixels,Drawing.Preferences)*48);
if (Y <> OldY) then
begin
OldY := Y;
fPB.Canvas.MoveTo(DisplayWidth,DisplayHeight - I);
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.7),DisplayHeight - I);
if (Y mod 48) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.33),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y div 96)+'''');
end
else if (Y mod 24) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.5),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y div 96)+'''' + ' 6"');
end
else if (Y mod 4) = 0 then
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.5),DisplayHeight - I);
end;
end;
end;
idInchesOnly:
begin
OldY := Round(PixelsToValue(TopPixels,Drawing.Preferences)*12);
for I := 0 to DisplayHeight do
begin
Y := Round(PixelsToValue(I+TopPixels,Drawing.Preferences)*12);
if (Y <> OldY) then
begin
OldY := Y;
fPB.Canvas.MoveTo(DisplayWidth,DisplayHeight - I);
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.7),DisplayHeight - I);
if (Y mod 6) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.5),DisplayHeight - I);
end;
if (Y mod 12) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.33),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y div 12)+'''');
end;
end;
end;
end;
idSixInches:
begin
OldY := Round(PixelsToValue(TopPixels,Drawing.Preferences)*6);
for I := 0 to DisplayHeight do
begin
Y := Round(PixelsToValue(I+TopPixels,Drawing.Preferences)*6);
if (Y <> OldY) then
begin
OldY := Y;
fPB.Canvas.MoveTo(DisplayWidth,DisplayHeight - I);
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.7),DisplayHeight - I);
if (Y mod 3) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.5),DisplayHeight - I);
end;
if (Y mod 6) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.33),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y div 6)+'''');
end;
end;
end;
end;
idFeet:
begin
OldY := Round(PixelsToValue(TopPixels,Drawing.Preferences)*2);
for I := 0 to DisplayHeight do
begin
Y := Round(PixelsToValue(I+TopPixels,Drawing.Preferences)*2);
if (Y <> OldY) then
begin
OldY := Y;
fPB.Canvas.MoveTo(DisplayWidth,DisplayHeight - I);
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.7),DisplayHeight - I);
if (Y mod 2) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.33),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y div 2)+'''');
end;
end;
end;
end;
idTenFeet:
begin
OldY := Round(PixelsToValue(TopPixels,Drawing.Preferences));
for I := 0 to DisplayHeight do
begin
Y := Round(PixelsToValue(I+TopPixels,Drawing.Preferences));
if (Y <> OldY) then
begin
OldY := Y;
fPB.Canvas.MoveTo(DisplayWidth,DisplayHeight - I);
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.7),DisplayHeight - I);
if (Y mod 10) = 0 then
begin
fPB.Canvas.LineTo(Trunc(DisplayWidth * 0.33),DisplayHeight - I);
AngleTextOut( fPB.Canvas, 270, Trunc(DisplayWidth * 0.45) +1,
DisplayHeight - I+3,IntToStr(Y)+'''');
end;
end;
end;
end;
end;
end;
begin
if Drawing = nil then exit;
RulerRect := RectToOrigin(fPB.BoundsRect );
PosSB := SBPosition;
{ Setup and draw the ruler background }
fPB.Canvas.Brush.Color := clYellow;
fPB.Canvas.Brush.Style := bsSolid;
fPb.Canvas.Pen.Width := 1;
fPB.Canvas.Pen.Style := psSolid;
fPB.Canvas.Pen.Color := clBlack;
fPB.Canvas.FillRect( RulerRect );
{ Determine the orientation of the ruler }
DisplayWidth := RulerRect.Right;
DisplayHeight := RulerRect.Bottom;
Horiz := DisplayHeight < DisplayWidth;
{ Outline the ruler in black }
fPB.Canvas.Rectangle( RulerRect );
{ Draw the appropriate ruler }
if Horiz then
Horizontal
else
Vertical;
end;
procedure TRuler.OriginToLowerLeft(Drawing: TDrawing);
begin
// LeftPixels := MicronsToPixels( PosSB * ( XMax - WidthMicrons - XMin) + XMin, Drawing.Preferences );
// LeftMicrons = PosSB * (XMax - WidthMicrons - XMin) + XMin;
// 0 = PosSb * (XMax - WidthMicrons - XMin ) + XMin - LeftMicrons
// PosSb * (XMax - WidthMicrons - XMin ) = - XMin + LeftMicrons
// PosSb = (LeftMicrons - XMin) / (XMax - WidthMicrons - XMin )
end;
function TRuler.SBPosition: Double;
begin
Result := fScrollBar.Position / fScrollBar.Max;
end;
end.