summaryrefslogtreecommitdiff
path: root/app/bin/cstraigh.c
blob: 277db96832ce9afa29ecca4c8bce17b175170492 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/** \file cstraigh.c
 * STRAIGHT
 */

/*  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "cstraigh.h"
#include "cselect.h"
#include "cundo.h"
#include "fileio.h"
#include "param.h"
#include "track.h"
#include "layout.h"
#include "common-ui.h"

/*
 * STATE INFO
 */
static struct {
		coOrd pos0, pos1;
		track_p trk;
		EPINX_T ep;
		BOOL_T down;
		} Dl;

static dynArr_t anchors_da;
#define anchors(N) DYNARR_N(trkSeg_t,anchors_da,N)

static void CreateEndAnchor(coOrd p, wBool_t lock) {
	DIST_T d = tempD.scale*0.15;

	DYNARR_APPEND(trkSeg_t,anchors_da,1);
	int i = anchors_da.cnt-1;
	anchors(i).type = lock?SEG_FILCRCL:SEG_CRVLIN;
	anchors(i).color = wDrawColorBlue;
	anchors(i).u.c.center = p;
	anchors(i).u.c.radius = d/2;
	anchors(i).u.c.a0 = 0.0;
	anchors(i).u.c.a1 = 360.0;
	anchors(i).width = 0;
}


static STATUS_T CmdStraight( wAction_t action, coOrd pos )
{
	track_p t;
	DIST_T dist;
	coOrd p;

	switch (action&0xFF) {

	case C_START:
		DYNARR_RESET(trkSeg_t,anchors_da);
		Dl.pos0=pos;
		Dl.pos1=pos;
		Dl.trk = NULL;
		Dl.ep=-1;
		Dl.down = FALSE;
		InfoMessage( _("Place 1st endpoint of straight track, snap to unconnected endpoint") );
		SetAllTrackSelect( FALSE );
		return C_CONTINUE;

	case C_DOWN:
		DYNARR_RESET(trkSeg_t,anchors_da);
		p = pos;
		BOOL_T found = FALSE;
		Dl.trk = NULL;
		if (((MyGetKeyState() & WKEY_ALT) == 0) == magneticSnap) {
			if ((t = OnTrack(&p, FALSE, TRUE)) != NULL) {
			   EPINX_T ep = PickUnconnectedEndPointSilent(p, t);
			   if (ep != -1) {
				   if (GetTrkGauge(t) != GetScaleTrackGauge(GetLayoutCurScale())) {
				   		wBeep();
				   		InfoMessage(_("Track is different gauge"));
				   		return C_CONTINUE;
				   	}
			   		Dl.trk = t;
			   		Dl.ep = ep;
			   		pos = GetTrkEndPos(t, ep);
			   		found = TRUE;
			   }
			}
		}
		Dl.down = TRUE;	
		if (!found) SnapPos( &pos );
		Dl.pos0 = pos;
		InfoMessage( _("Drag to place 2nd end point") );
		DYNARR_SET( trkSeg_t, tempSegs_da, 1 );
		tempSegs(0).color = wDrawColorBlack;
		tempSegs(0).width = 0;
		tempSegs_da.cnt = 0;
		tempSegs(0).type = SEG_STRTRK;
		tempSegs(0).u.l.pos[0] = pos;
		return C_CONTINUE;

	case C_MOVE:
	case wActionMove:
		DYNARR_RESET(trkSeg_t,anchors_da);
		found = FALSE;
		if (!Dl.down) {
			if (((MyGetKeyState() & WKEY_ALT) == 0) == magneticSnap) {
				p = pos;
				if ((t = OnTrack(&p, FALSE, TRUE)) != NULL) {
					if (GetTrkGauge(t) == GetScaleTrackGauge(GetLayoutCurScale())) {
					   EPINX_T ep = PickUnconnectedEndPointSilent(pos, t);
					   if (ep != -1) {
						   if (GetTrkGauge(t) == GetScaleTrackGauge(GetLayoutCurScale())) {
							   CreateEndAnchor(GetTrkEndPos(t,ep),FALSE);
							   found = TRUE;
						   }
					   }
					}
				}
			}
			if (!found && SnapPos( &pos )) {
				CreateEndAnchor(pos,FALSE);
				found = TRUE;
			}
			return C_CONTINUE;
		}
		ANGLE_T angle, angle2;
		if (Dl.trk && !(MyGetKeyState() & WKEY_SHIFT)) {
			angle = NormalizeAngle(GetTrkEndAngle( Dl.trk, Dl.ep));
			angle2 = NormalizeAngle(FindAngle(pos, Dl.pos0)-angle);
			if (angle2 > 90.0 && angle2 < 270.0)
				Translate( &pos, Dl.pos0, angle, FindDistance( Dl.pos0, pos ) );
			else pos = Dl.pos0;	
		} else if (SnapPos( &pos )) {
			 CreateEndAnchor(pos,FALSE);
			 found = TRUE;
		}
		
		InfoMessage( _("Straight Track Length=%s Angle=%0.3f"),
				FormatDistance(FindDistance( Dl.pos0, pos )),
				PutAngle(FindAngle( Dl.pos0, pos )) );
		tempSegs(0).u.l.pos[1] = pos;
		tempSegs_da.cnt = 1;
		return C_CONTINUE;

	case C_UP:
		DYNARR_RESET(trkSeg_t,anchors_da);
		if (!Dl.down) return C_CONTINUE;
		tempSegs_da.cnt = 0;
		if (Dl.trk && !(MyGetKeyState() & WKEY_SHIFT)) {
			angle = NormalizeAngle(GetTrkEndAngle( Dl.trk, Dl.ep));
			angle2 = NormalizeAngle(FindAngle(pos, Dl.pos0)-angle);
			if (angle2 > 90.0 && angle2 < 270.0)
				Translate( &pos, Dl.pos0, angle, FindDistance( Dl.pos0, pos ));
			else pos = Dl.pos0;	
		} else 	SnapPos( &pos );
		if ((dist=FindDistance( Dl.pos0, pos )) <= minLength) {
		   ErrorMessage( MSG_TRK_TOO_SHORT, "Straight ", PutDim(fabs(minLength-dist)) );
		   return C_TERMINATE;
		}
		UndoStart( _("Create Straight Track"), "newStraight" );
		t = NewStraightTrack( Dl.pos0, pos );
		if (Dl.trk && !(MyGetKeyState() & WKEY_SHIFT)) {
			ConnectTracks(Dl.trk, Dl.ep, t, 0);
		}
		UndoEnd();
		DrawNewTrack(t);
		return C_TERMINATE;

	case C_REDRAW:
		if (anchors_da.cnt)
			DrawSegs( &tempD, zero, 0.0, &anchors(0), anchors_da.cnt, trackGauge, wDrawColorBlack );
		if (Dl.down)
			DrawSegs( &tempD, zero, 0.0, &tempSegs(0), tempSegs_da.cnt, trackGauge, wDrawColorBlack );
		return C_CONTINUE;
	case C_CANCEL:
		Dl.down = FALSE;
		return C_CONTINUE;

	default:
		return C_CONTINUE;
	}
}


#include "bitmaps/straight.xpm"

void InitCmdStraight( wMenu_p menu )
{
	AddMenuButton( menu, CmdStraight, "cmdStraight", _("Straight Track"), wIconCreatePixMap(straight_xpm[iconSize]), LEVEL0_50, IC_STICKY|IC_POPUP2|IC_WANT_MOVE, ACCL_STRAIGHT, NULL );
}