summaryrefslogtreecommitdiffstats
path: root/logo/src/xlogo/kernel/perspective/LightDialog.java
blob: 36382135c545a9900af6292b479e40eccb516c90 (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
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
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
 * Copyright (C) 2013 Marko Zivkovic
 * 
 * Contact Information: marko88zivkovic at gmail dot com
 * 
 * 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.
 * 
 * 
 * This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
 * during his Bachelor thesis at the computer science department of ETH Zurich,
 * in the year 2013 and/or during future work.
 * 
 * It is a reengineered version of XLogo written by Loic Le Coq, published
 * under the GPL License at http://xlogo.tuxfamily.org/
 * 
 * Contents of this file were initially written by Loic Le Coq,
 * modifications, extensions, refactorings might have been applied by Marko Zivkovic 
 */

package xlogo.kernel.perspective;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Color;

import xlogo.Logo;

import javax.vecmath.Color3f;
import javax.vecmath.Tuple3f;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.border.TitledBorder;

import java.awt.event.*;

import xlogo.gui.preferences.PanelColor;
import xlogo.storage.WSManager;

public class LightDialog extends JDialog implements ActionListener
{
	private static final long	serialVersionUID	= 1L;
	private String[]			type				= { Logo.messages.getString("3d.light.none"),
			Logo.messages.getString("3d.light.ambient"), Logo.messages.getString("3d.light.directional"),
			Logo.messages.getString("3d.light.point"), Logo.messages.getString("3d.light.spot") };
	
	private JComboBox			comboType;
	
	PanelColor					panelColor;
	
	private PanelPosition		panelPosition;
	
	private PanelPosition		panelDirection;
	
	private PanelAngle			panelAngle;
	
	private JLabel				labelType;
	
	private JButton				ok;
	
	private JButton				refresh;
	
	private Viewer3D			viewer3d;
	
	private MyLight				light;
	
	LightDialog(Viewer3D viewer3d, MyLight light, String title)
	{
		super(viewer3d, title, true);
		this.viewer3d = viewer3d;
		this.light = light;
		initGui();
	}
	
	private void initGui()
	{
		getContentPane().setLayout(new GridBagLayout());
		setSize(500, 200);
		labelType = new JLabel(Logo.messages.getString("3d.light.type"));
		comboType = new JComboBox(type);
		comboType.setSelectedIndex(light.getType());
		
		Color3f col = light.getColor();
		if (null != col)
			panelColor = new PanelColor(col.get());
		else
			panelColor = new PanelColor(Color.white);
		panelColor.setBackground(comboType.getBackground());
		
		panelPosition = new PanelPosition(Logo.messages.getString("3d.light.position"), light.getPosition());
		panelDirection = new PanelPosition(Logo.messages.getString("3d.light.direction"), light.getDirection());
		panelAngle = new PanelAngle(light.getAngle());
		ok = new JButton(Logo.messages.getString("pref.ok"));
		refresh = new JButton(Logo.messages.getString("3d.light.apply"));
		
		Font font = WSManager.getWorkspaceConfig().getFont();
		
		labelType.setFont(font);
		comboType.setFont(font);
		ok.setFont(font);
		refresh.setFont(font);
		
		comboType.addActionListener(this);
		comboType.setActionCommand("combo");
		ok.addActionListener(this);
		ok.setActionCommand("ok");
		refresh.addActionListener(this);
		refresh.setActionCommand("refresh");
		
		getContentPane().add(
				labelType,
				new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				comboType,
				new GridBagConstraints(1, 0, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				panelColor,
				new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				panelPosition,
				new GridBagConstraints(0, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				panelDirection,
				new GridBagConstraints(1, 2, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				panelAngle,
				new GridBagConstraints(0, 3, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				refresh,
				new GridBagConstraints(0, 4, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
						new Insets(0, 0, 0, 0), 0, 0));
		getContentPane().add(
				ok,
				new GridBagConstraints(1, 4, 1, 1, 1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.NONE,
						new Insets(0, 0, 0, 0), 0, 0));
		selectComponents();
		setVisible(true);
		
	}
	
	public void actionPerformed(ActionEvent e)
	{
		String cmd = e.getActionCommand();
		// The selected item in the combo Box has changed
		if (cmd.equals("combo"))
			selectComponents();
		// Button Ok pressed
		else if (cmd.equals("ok"))
		{
			updateLight();
			dispose();
		}
		// Button Apply pressed
		else if (cmd.equals("refresh"))
		{
			updateLight();
		}
	}
	
	private void updateLight()
	{
		int t = comboType.getSelectedIndex();
		Color3f c = new Color3f(panelColor.getValue());
		Point3f p = panelPosition.getPosition();
		Vector3f d = panelDirection.getDirection();
		float a = panelAngle.getAngleValue();
		light.setType(t);
		light.setColor(c);
		light.setPosition(p);
		light.setDirection(d);
		light.setAngle(a);
		light.detach();
		light.removeAllChildren();
		light.createLight();
		// System.out.println(c+" "+" "+p+" "+d);
		viewer3d.addNode(light);
		
	}
	
	private void selectComponents()
	{
		int id = comboType.getSelectedIndex();
		// None
		if (id == MyLight.LIGHT_OFF)
		{
			panelColor.setEnabled(false);
			panelPosition.setEnabled(false);
			panelDirection.setEnabled(false);
			panelAngle.setEnabled(false);
		}
		// Ambient
		else if (id == MyLight.LIGHT_AMBIENT)
		{
			panelColor.setEnabled(true);
			panelPosition.setEnabled(false);
			panelDirection.setEnabled(false);
			panelAngle.setEnabled(false);
		}
		// Directional
		else if (id == MyLight.LIGHT_DIRECTIONAL)
		{
			panelColor.setEnabled(true);
			panelPosition.setEnabled(false);
			panelDirection.setEnabled(true);
			panelAngle.setEnabled(false);
		}
		// PointLight
		else if (id == MyLight.LIGHT_POINT)
		{
			panelColor.setEnabled(true);
			panelPosition.setEnabled(true);
			panelDirection.setEnabled(false);
			panelAngle.setEnabled(false);
		}
		// Spot
		else if (id == MyLight.LIGHT_SPOT)
		{
			panelColor.setEnabled(true);
			panelPosition.setEnabled(true);
			panelDirection.setEnabled(true);
			panelAngle.setEnabled(true);
		}
	}
	
	class PanelAngle extends JPanel
	{
		private static final long	serialVersionUID	= 1L;
		private JLabel				label;
		private JTextField			angle;
		private float				angleValue;
		
		PanelAngle(float angleValue)
		{
			this.angleValue = angleValue;
			
			initGui();
		}
		
		private void initGui()
		{
			label = new JLabel(Logo.messages.getString("3d.light.angle"));
			label.setFont(WSManager.getWorkspaceConfig().getFont());
			angle = new JTextField(String.valueOf(angleValue));
			angle.setSize(30, WSManager.getWorkspaceConfig().getFont().getSize() + 10);
			add(label);
			add(angle);
		}
		
		public void setEnabled(boolean b)
		{
			super.setEnabled(b);
			label.setEnabled(b);
			angle.setEnabled(b);
		}
		
		/**
		 * @return
		 * @uml.property name="angleValue"
		 */
		float getAngleValue()
		{
			try
			{
				float f = Float.parseFloat(angle.getText());
				return f;
			}
			catch (NumberFormatException e)
			{}
			return MyLight.DEFAULT_ANGLE;
		}
	}
	
	class PanelPosition extends JPanel
	{
		private static final long	serialVersionUID	= 1L;
		private String				title;
		private JTextField			Xpos;
		private JTextField			Ypos;
		private JTextField			Zpos;
		private JLabel				sep1;
		private JLabel				sep2;
		Tuple3f						tuple;
		
		PanelPosition(String title, Tuple3f tuple)
		{
			this.title = title;
			this.tuple = tuple;
			initGui();
		}
		
		private void initGui()
		{
			TitledBorder tb = BorderFactory.createTitledBorder(title);
			tb.setTitleFont(WSManager.getWorkspaceConfig().getFont());
			setBorder(tb);
			sep1 = new JLabel("x");
			sep2 = new JLabel("x");
			Xpos = new JTextField(4);
			Ypos = new JTextField(4);
			Zpos = new JTextField(4);
			if (null != tuple)
			{
				Xpos.setText(String.valueOf((int) (tuple.x * 1000)));
				Ypos.setText(String.valueOf((int) (tuple.y * 1000)));
				Zpos.setText(String.valueOf((int) (tuple.z * 1000)));
			}
			add(Xpos);
			add(sep1);
			add(Ypos);
			add(sep2);
			add(Zpos);
		}
		
		Point3f getPosition()
		{
			try
			{
				float x = Float.parseFloat(Xpos.getText());
				float y = Float.parseFloat(Ypos.getText());
				float z = Float.parseFloat(Zpos.getText());
				return new Point3f(x / 1000, y / 1000, z / 1000);
			}
			catch (NumberFormatException e)
			{}
			return (new Point3f(0, 0, 0));
		}
		
		Vector3f getDirection()
		{
			try
			{
				float x = Float.parseFloat(Xpos.getText());
				float y = Float.parseFloat(Ypos.getText());
				float z = Float.parseFloat(Zpos.getText());
				return new Vector3f(x / 1000, y / 1000, z / 1000);
			}
			catch (NumberFormatException e)
			{}
			return (new Vector3f(0, 0, 0));
			
		}
		
		public void setEnabled(boolean b)
		{
			super.setEnabled(b);
			sep1.setEnabled(b);
			sep2.setEnabled(b);
			Xpos.setEnabled(b);
			Ypos.setEnabled(b);
			Zpos.setEnabled(b);
		}
		
	}
	
}