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
|
/*
* $Header: /home/dmarkle/xtrkcad-fork-cvs/xtrkcad/app/bin/cruler.c,v 1.4 2008-03-06 19:35:06 m_fischer Exp $
*/
/* XTrkCad - Model Railroad CAD
* Copyright (C) 2005 Dave Bullis
*
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "cundo.h"
#include "cselect.h"
#include "fileio.h"
#include "param.h"
#include "track.h"
#include "misc.h"
#define AN_OFF (0)
#define AN_FIRST (1)
#define AN_SECOND (2)
#define AN_ON (3)
static struct {
STATE_T state;
coOrd pos0;
coOrd pos1;
coOrd pos2;
BOOL_T isClose;
int modifyingEnd;
} An = { AN_OFF, { 0,0 }, { 0,0 } };
void DrawAngle(drawCmd_p d, coOrd p0, coOrd p1, coOrd p2, wDrawColor color)
{
char msg[512];
trkSeg_t seg;
if ((An.state != AN_OFF) && !IsClose(FindDistance(p0,p1))) {
seg.type = SEG_STRLIN;
seg.lineWidth = 0;
seg.color = wDrawColorBlack;
seg.u.l.pos[0] = p0;
seg.u.l.pos[1] = p1;
DrawSegs(d,zero,0.0,&seg,1,trackGauge,color);
if (!(IsClose(FindDistance(p0,p2)))) {
seg.type = SEG_STRLIN;
seg.lineWidth = 0;
seg.color = wDrawColorBlack;
seg.u.l.pos[0] = p0;
seg.u.l.pos[1] = p2;
DrawSegs(d,zero,0.0,&seg,1,trackGauge,color);
DIST_T r = min(FindDistance(p0,p2),FindDistance(p0,p1))/2;
ANGLE_T a = DifferenceBetweenAngles(FindAngle(p0,p1),FindAngle(p0,p2));
ANGLE_T a0;
if (a>=0) {
a0 = FindAngle(p0,p1);
} else {
a0 = FindAngle(p0,p2);
}
ANGLE_T a1 = fabs(DifferenceBetweenAngles(FindAngle(p0,p1),FindAngle(p0,p2)));
seg.type = SEG_CRVLIN;
seg.u.c.center = p0;
seg.u.c.radius = r;
seg.u.c.a0 = a0;
seg.u.c.a1 = a1;
DrawSegs(d,zero,0.0,&seg,1,trackGauge,color);
coOrd p;
Translate(&p, p0, a1/2+a0, r );
seg.type = SEG_TEXT;
seg.u.t.angle = 0.0;
sprintf(msg,"RA: %0.3f",a1);
seg.u.t.string = msg;
seg.u.t.pos = p;
seg.u.t.fontSize = 10.0*d->scale;
seg.u.t.fontP = NULL;
seg.u.t.boxed = FALSE;
DrawSegs(d,zero,0.0,&seg,1,trackGauge,color);
}
}
}
static STATUS_T CmdAngle( wAction_t action, coOrd pos )
{
switch (action) {
case C_START:
switch (An.state) {
case AN_OFF:
An.state = AN_ON;
break;
case AN_ON:
An.state = AN_OFF;
case AN_FIRST:
case AN_SECOND:
An.state = AN_OFF;
break;
}
return C_CONTINUE;
case C_DOWN:
switch (An.state) {
case AN_OFF:
case AN_ON:
An.pos0 = An.pos1 = An.pos2 = pos;
An.state = AN_FIRST;
InfoMessage( "Drag out base line" );
break;
case AN_FIRST:
An.pos2 = pos;
An.state = AN_SECOND;
InfoMessage( "Drag Angle" );
break;
}
return C_CONTINUE;
case C_MOVE:
//Lock to 90 degrees with CTRL
if (MyGetKeyState()&WKEY_CTRL) {
ANGLE_T line_angle;
if (An.state == AN_FIRST) { line_angle = 0.0; }
else { line_angle = FindAngle(An.pos0,An.pos1); }
DIST_T l = FindDistance(An.pos0, pos);
if (!IsClose(l)) {
ANGLE_T angle2 = NormalizeAngle(FindAngle(An.pos0, pos)-line_angle);
int oct = (int)((angle2 + 22.5) / 45.0);
l = fabs(l*cos(D2R(angle2-oct*45)));
Translate( &pos, An.pos0, oct*45.0+line_angle, l );
}
}
switch (An.state) {
case AN_FIRST:
An.pos1 = An.pos2 = pos;
break;
case AN_SECOND:
An.pos2 = pos;
break;
default:;
}
if (An.state == AN_FIRST) {
InfoMessage( "Base Angle %0.3f",FindAngle(An.pos0,An.pos1));
}
if (An.state == AN_SECOND)
InfoMessage( "Base Angle %0.3f, Relative Angle %0.3f",FindAngle(An.pos0,
An.pos1),
fabs(DifferenceBetweenAngles(FindAngle(An.pos0,An.pos1),FindAngle(An.pos0,
An.pos2))));
return C_CONTINUE;
case C_UP:
if (An.state == AN_SECOND) { return C_TERMINATE; }
return C_CONTINUE;
case C_REDRAW:
DrawHighlightBoxes(FALSE,FALSE,NULL);
HighlightSelectedTracks(NULL, TRUE, TRUE);
if (An.state != AN_OFF) {
if (!IsClose(FindDistance(An.pos1,An.pos2))) {
DrawAngle( &tempD, An.pos0, An.pos1, An.pos2, wDrawColorBlack );
}
}
return C_CONTINUE;
case C_CANCEL:
return C_TERMINATE;
}
return C_CONTINUE;
}
STATUS_T ModifyProtractor(
wAction_t action,
coOrd pos )
{
switch (action&0xFF) {
case C_DOWN:
An.modifyingEnd = -1;
An.isClose = FALSE;
if ( An.state == AN_OFF ) {
return C_ERROR;
}
if ( IsClose(FindDistance( pos, An.pos0 ))) {
An.modifyingEnd = 0;
} else if ( IsClose(FindDistance( pos, An.pos1 ))) {
An.modifyingEnd = 1;
} else if ( IsClose(FindDistance( pos, An.pos2 ))) {
An.modifyingEnd = 2;
} else {
return C_ERROR;
}
break;
case C_MOVE:
if ( An.modifyingEnd == 0 ) {
An.pos0 = pos;
} else if (An.modifyingEnd == 1) {
An.pos1 = pos;
} else if (An.modifyingEnd == 2) {
An.pos2 = pos;
}
InfoMessage( "Base Angle %0.3f, Relative Angle %0.3f",FindAngle(An.pos0,
An.pos1),
fabs(DifferenceBetweenAngles(FindAngle(An.pos0,An.pos1),FindAngle(An.pos0,
An.pos2))));
return C_CONTINUE;
case C_UP:
return C_CONTINUE;
case C_REDRAW:
DrawAngle( &tempD, An.pos0, An.pos1, An.pos2,
An.isClose?wDrawColorBlue:wDrawColorBlack );
break;
case wActionMove:
if ( IsClose(FindDistance( pos, An.pos0 )) ||
IsClose(FindDistance( pos, An.pos1 )) ||
IsClose(FindDistance( pos, An.pos2 )) ) {
An.isClose = TRUE;
} else {
An.isClose = FALSE;
}
break;
default:
return C_ERROR;
}
return C_CONTINUE;
}
/*****************************************************************************
*
* RULER
*
*/
#define DR_OFF (0)
#define DR_ON (1)
static struct {
STATE_T state;
coOrd pos0;
coOrd pos1;
BOOL_T isClose;
int modifyingEnd;
} Dr = { DR_OFF, { 0,0 }, { 0,0 } };
void RulerRedraw( BOOL_T demo )
{
if (programMode == MODE_TRAIN) { return; }
if (Dr.state == DR_ON) {
DrawRuler( &tempD, Dr.pos0, Dr.pos1, 0.0, TRUE, TRUE,
Dr.isClose?wDrawColorBlue:wDrawColorBlack );
}
if (demo) {
Dr.state = DR_OFF;
An.state = AN_OFF;
}
if (An.state != AN_OFF) {
DrawAngle( &tempD, An.pos0, An.pos1, An.pos2,
An.isClose?wDrawColorBlue:wDrawColorBlack);
}
}
static STATUS_T CmdRuler( wAction_t action, coOrd pos )
{
switch (action) {
case C_START:
Dr.isClose = FALSE;
switch (Dr.state) {
case DR_OFF:
Dr.state = DR_ON;
InfoMessage( "%s", FormatDistance( FindDistance( Dr.pos0, Dr.pos1 ) ) );
break;
case DR_ON:
Dr.state = DR_OFF;
break;
}
return C_CONTINUE;
case C_DOWN:
Dr.pos0 = Dr.pos1 = pos;
Dr.state = DR_ON;
InfoMessage( "0.0" );
return C_CONTINUE;
case C_MOVE:
Dr.pos1 = pos;
InfoMessage( "%s", FormatDistance( FindDistance( Dr.pos0, Dr.pos1 ) ) );
return C_CONTINUE;
case C_UP:
inError = TRUE;
return C_TERMINATE;
case C_REDRAW:
DrawHighlightBoxes(FALSE,FALSE,NULL);
HighlightSelectedTracks(NULL, TRUE, TRUE);
if (Dr.state == DR_ON) {
DrawRuler( &tempD, Dr.pos0, Dr.pos1, 0.0, TRUE, TRUE,
Dr.isClose?wDrawColorBlue:wDrawColorBlack );
}
return C_CONTINUE;
case C_CANCEL:
return C_TERMINATE;
}
return C_CONTINUE;
}
STATUS_T ModifyRuler(
wAction_t action,
coOrd pos )
{
switch (action&0xFF) {
case C_DOWN:
Dr.modifyingEnd = -1;
if ( Dr.state != DR_ON ) {
return C_ERROR;
}
if ( IsClose(FindDistance( pos, Dr.pos0 ))) {
Dr.modifyingEnd = 0;
} else if ( IsClose(FindDistance( pos, Dr.pos1 ))) {
Dr.modifyingEnd = 1;
} else {
return C_ERROR;
}
break;
case C_MOVE:
if ( Dr.modifyingEnd == 0 ) {
Dr.pos0 = pos;
} else if ( Dr.modifyingEnd == 1) {
Dr.pos1 = pos;
} else { return C_ERROR; }
InfoMessage( "%s", FormatDistance( FindDistance( Dr.pos0, Dr.pos1 ) ) );
return C_CONTINUE;
case C_UP:
return C_CONTINUE;
case C_REDRAW:
DrawRuler( &tempD, Dr.pos0, Dr.pos1, 0.0, TRUE, TRUE,
Dr.isClose?wDrawColorBlue:wDrawColorBlack );
break;
case wActionMove:
if ( IsClose(FindDistance( pos, Dr.pos0 )) ||
IsClose(FindDistance( pos, Dr.pos1 ))) {
Dr.isClose = TRUE;
An.isClose = FALSE;
} else {
Dr.isClose = FALSE;
ModifyProtractor(wActionMove,pos);
}
break;
default:
return C_ERROR;
}
return C_CONTINUE;
}
#include "bitmaps/ruler.xpm3"
#include "bitmaps/protractor.xpm3"
void InitCmdRuler( wMenu_p menu )
{
ButtonGroupBegin( _("Measurement"), "cmdMeasureSetCmd", _("Measurement") );
AddMenuButton( menu, CmdRuler, "cmdRuler", _("Ruler"),
wIconCreatePixMap(ruler_xpm3[iconSize]), LEVEL0,
IC_STICKY|IC_POPUP|IC_NORESTART, ACCL_RULER, NULL );
AddMenuButton( menu, CmdAngle, "cmdAngle", _("Protractor"),
wIconCreatePixMap(protractor_xpm3[iconSize]), LEVEL0,
IC_STICKY|IC_POPUP|IC_NORESTART, ACCL_ANGLE, NULL );
ButtonGroupEnd();
}
|