summaryrefslogtreecommitdiff
path: root/app/bin/dease.c
blob: 2b393db5dd2b1c4be6c22c4792cab11267344c8b (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
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
/** \file dease.c
 * Easement Button Hdlrs
 */

/*  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 "common.h"
#include "ccurve.h"
#include "cjoin.h"
#include "cstraigh.h"
#include "custom.h"
#include "fileio.h"
#include "param.h"
#include "track.h"

static wButton_p easementB;

static DIST_T easeX = 0.0;

static DIST_T Rvalues[3];
static DIST_T Lvalues[3];

static DIST_T oldEasementVal;

static wIcon_p enone_bm;
static wIcon_p esharp_bm;
static wIcon_p egtsharp_bm;
static wIcon_p eltsharp_bm;
static wIcon_p enormal_bm;
static wIcon_p eltbroad_bm;
static wIcon_p ebroad_bm;
static wIcon_p egtbroad_bm;
static wIcon_p ecornu_bm;

/****************************************
 *
 * EASEMENTW
 *
 */

static wWin_p easementW;

static void EasementSel( long );
static void SetEasement( DIST_T, void * );
static void EasementOk( void );
static void EasementCancel( void );

static char *easementChoiceLabels[] = { N_("None"), N_("Sharp"), N_("Normal"), N_("Broad"), N_("Cornu"), NULL };
static paramFloatRange_t r0n1_100 = { -1.0, 100.0, 60 };
static paramFloatRange_t r0_100 = { 0.0, 100.0, 60 };
static paramFloatRange_t r0_200 = { 0.0, 200.0, 60 };
static paramFloatRange_t r0_10 = { 0.0, 10.0, 60 };
static long easeM;
static paramData_t easementPLs[] = {
#define I_EASEVAL		(0)
	{	PD_FLOAT, &easementVal, "val", PDO_NOPSHUPD, &r0n1_100, N_("Value") },
	{	PD_FLOAT, &easeR, "r", PDO_DIM|PDO_DLGRESETMARGIN, &r0_200, N_("R"), BO_READONLY },
	{	PD_FLOAT, &easeX, "x", PDO_DIM|PDO_DLGHORZ, &r0_10, N_("X"), BO_READONLY },
	{	PD_FLOAT, &easeL, "l", PDO_DIM|PDO_DLGHORZ, &r0_100, N_("L"), BO_READONLY },
#define I_EASESEL		(4)
	{	PD_RADIO, &easeM, "radio", PDO_DIM|PDO_NORECORD|PDO_NOPREF|PDO_DLGRESETMARGIN, easementChoiceLabels, NULL, BC_HORZ|BC_NONE } };
static paramGroup_t easementPG = { "easement", PGO_RECORD, easementPLs, COUNT( easementPLs ) };


static void SetEasement(
		DIST_T val,
		void * update )
/*
 * Set transition-curve parameters (R and L).
 */
{
	DIST_T z;
	long selVal = -1;
	wIcon_p bm;

	if (val < 0.0) {
		easeX = easeR = easeL = 0.0;
		selVal = 4;
		val = -1;
		bm = ecornu_bm;
	} else {
		if (val == 0.0) {
			easeX = easeR = easeL = 0.0;
			selVal = 0;
			val = 0;
			bm = enone_bm;
		} else if (val <= 1.0) {
			if (val < 0.21) val = 0.21;   //Eliminate values that give negative radii
			z = 1.0/val - 1.0;
			easeR = Rvalues[1] - z * (Rvalues[1] - Rvalues[0]);
			easeL = Lvalues[1] - z * (Lvalues[1] - Lvalues[0]);
			if (easeR != 0.0)
				easeX = easeL*easeL/(24*easeR);
			else
				easeX = 0.0;
			if (val == 1.0) {
				selVal = 2;
				bm = enormal_bm;
			} else if (val == 0.5) {
				selVal = 1;
				bm = esharp_bm;
			} else if (val < 0.5) {
				bm = eltsharp_bm;
				selVal = 1;
			} else {
				selVal = 1;
				bm = egtsharp_bm;
			}
		} else {
			z = val - 1.0;
			easeR = Rvalues[1] + z * (Rvalues[2] - Rvalues[1]);
			easeL = Lvalues[1] + z * (Lvalues[2] - Lvalues[1]);
			if (easeR != 0.0)
				easeX = easeL*easeL/(24*easeR);
			else
				easeX = 0.0;
			if (val == 2.0) {
				selVal = 3;
				bm = ebroad_bm;
			} else if (val < 2.0) {
				selVal = 3;
				bm = eltbroad_bm;
			} else {
				selVal = 3;
				bm = egtbroad_bm;
			}
		}
	}

	easeR = (floor(easeR*100.0))/100.0;
	easementVal = val;
	if (easementW && wWinIsVisible(easementW)) {
		ParamLoadControls( &easementPG );
		if (update) {
			easeM = selVal;
			ParamLoadControl( &easementPG, I_EASESEL );
		}
	}
	/*ParamChange( &easeValPD );*/

	if (easementB)
		wButtonSetLabel( easementB, (char*)bm );
}


static void EasementOk( void )
{
	ParamLoadData( &easementPG );
	SetEasement( easementVal, I2VP(FALSE) );
	wHide( easementW );
}


static void EasementCancel( void )
{
	SetEasement( easementVal = oldEasementVal, I2VP(FALSE) );
	wHide( easementW );
}


static void EasementSel(
		long arg )
/*
 * Handle transition-curve parameter selection.
 */
{
	DIST_T val;
	switch (arg) {
	case 0:
		val = 0;
		break;
	case 1:
		val = 0.5;
		break;
	case 2:
		val = 1.0;
		break;
	case 3:
		val = 2.0;
		break;
	case 4:
		val = -1.0;
		break;
	default:
		AbortProg( "easementSel: bad value %ld", arg);
		val = 0.0;
		break;
	}
	SetEasement( val, I2VP(FALSE) );
}


static void EasementDlgUpdate(
		paramGroup_p pg,
		int inx,
		void * valueP )
{
	switch (inx) {
	case I_EASEVAL:
		SetEasement( *(FLOAT_T*)valueP, I2VP(1) );
		break;
	case I_EASESEL:
		EasementSel( *(long*)valueP );
		break;
	}
}


static void LayoutEasementW(
		paramData_t * pd,
		int inx,
		wWinPix_t colX,
		wWinPix_t * x,
		wWinPix_t * y )
{
	if ( inx == 2 )
		wControlSetPos( easementPLs[0].control, *x, wControlGetPosY(easementPLs[0].control) );
}


static void DoEasement( void * unused )
{
	if (easementW == NULL) {
		easementW = ParamCreateDialog( &easementPG, MakeWindowTitle(_("Easement")), _("Ok"), (paramActionOkProc)EasementOk, (paramActionCancelProc)EasementCancel, TRUE, LayoutEasementW, 0, EasementDlgUpdate );
		SetEasement( easementVal, I2VP(TRUE) );
	}
	oldEasementVal = easementVal;
	wShow( easementW );
	SetEasement( easementVal, I2VP(TRUE) );
}


static void EasementChange( long changes )
/*
 * Handle change of scale. Load new parameters.
 */
{
	if (changes&(CHANGE_SCALE|CHANGE_UNITS)) {
		GetScaleEasementValues( Rvalues, Lvalues );
		SetEasement( easementVal, I2VP(TRUE) );
	}
}


#include "bitmaps/ease-none.xpm"
#include "bitmaps/ease-sharp.xpm"
#include "bitmaps/ease-gt-sharp.xpm"
#include "bitmaps/ease-lt-sharp.xpm"
#include "bitmaps/ease-normal.xpm"
#include "bitmaps/ease-broad.xpm"
#include "bitmaps/ease-gt-broad.xpm"
#include "bitmaps/ease-lt-broad.xpm"
#include "bitmaps/ease-cornu.xpm"


EXPORT addButtonCallBack_t EasementInit( void )
{
	ParamRegister( &easementPG );

	enone_bm = wIconCreatePixMap( ease_none_xpm[iconSize] );
	eltsharp_bm = wIconCreatePixMap( ease_ltsharp_xpm[iconSize] );
	esharp_bm = wIconCreatePixMap( ease_sharp_xpm[iconSize] );
	egtsharp_bm = wIconCreatePixMap( ease_gtsharp_xpm[iconSize] );
	enormal_bm = wIconCreatePixMap( ease_normal_xpm[iconSize] );
	eltbroad_bm = wIconCreatePixMap( ease_ltbroad_xpm[iconSize] );
	ebroad_bm = wIconCreatePixMap( ease_broad_xpm[iconSize] );
	egtbroad_bm = wIconCreatePixMap( ease_gtbroad_xpm[iconSize] );
	ecornu_bm = wIconCreatePixMap( ease_cornu_xpm[iconSize] );
	easementB = AddToolbarButton( "cmdEasement", enone_bm, 0, DoEasementRedir, NULL );

	RegisterChangeNotification( EasementChange );
	return &DoEasement;
}