Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 50f1dc53fa836f00cac0f0ccdb4ecb1e9f2cadd8 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2006, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

package org.eclipse.swt.examples.graphics;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.MouseMoveListener;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Device;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.graphics.Transform;
import org.eclipse.swt.widgets.Composite;

/**
 * This tab presents cubic and quadratic curves that can be drawn.
 * The user may reposition the cubic and quadratic handles.
 */
public class CurvesTab extends GraphicsTab {
	/** These rectangles represent the handles on the curves. */
	private Rectangle quadHndl, quadEndHndl, cubHndl1, cubHndl2, cubEndHndl;

	/** These values represent the positions of the curves. */
	private float quadXPos, quadYPos, cubXPos, cubYPos;

	/** These values represent the x and y displacement of each handle. */
	private float quadDiffX, quadDiffY, quadEndDiffX, quadEndDiffY;
	private float cubDiffX1, cubDiffY1, cubDiffX2, cubDiffY2, cubEndDiffX, cubEndDiffY;

	/** These are flags that indicate whether or not a handle has been moved. */
	private boolean quadPtMoved, quadEndPtMoved, cubPt1Moved, cubPt2Moved, cubEndPtMoved;

	private MouseMoveListener mouseMoveListener;
	private MouseListener mouseListener;
	private Cursor cursor;

	/** true if hovering over a handle, false otherwise */
	private boolean hovering = false;

	/** true if left mouse button is held down, false otherwise */
	private boolean mouseDown = false;


public CurvesTab(GraphicsExample example) {
	super(example);
	quadHndl = new Rectangle(200, 150, 5, 5);
	quadEndHndl = new Rectangle(400, 0, 5, 5);
	quadDiffX = quadDiffY = quadEndDiffX = quadEndDiffY = 0;
	cubHndl1 = new Rectangle(133, -60, 5, 5);
	cubHndl2 = new Rectangle(266, 60, 5, 5);
	cubDiffX1 = cubDiffY1 = cubDiffX2 = cubDiffY2 = 0;
	cubEndHndl = new Rectangle(400, 0, 5, 5);
	cubEndDiffX = cubEndDiffY = 0;
}

@Override
public String getCategory() {
	return GraphicsExample.getResourceString("Curves"); //$NON-NLS-1$
}

@Override
public String getText() {
	return GraphicsExample.getResourceString("Curves"); //$NON-NLS-1$
}

@Override
public String getDescription() {
	return GraphicsExample.getResourceString("CurvesDescription"); //$NON-NLS-1$
}

@Override
public boolean getDoubleBuffered() {
	return true;
}

@Override
public void dispose() {
	if (mouseListener != null)
		example.canvas.removeMouseListener(mouseListener);

	if (mouseMoveListener != null)
		example.canvas.removeMouseMoveListener(mouseMoveListener);

	cursor = null;
}

/**
 * This helper method determines whether or not the cursor is positioned
 * over a handle.
 *
 * @param e
 *            A MouseEvent
 * @return true if cursor is positioned over a handle; false otherwise
 */
private boolean isHovering(MouseEvent e) {
	Rectangle quad = new Rectangle(quadHndl.x + (int)quadXPos - 1, quadHndl.y + (int)quadYPos - 1, quadHndl.width+2, quadHndl.height+2);
	Rectangle quadEnd = new Rectangle(quadEndHndl.x + (int)quadXPos - 1, quadEndHndl.y + (int)quadYPos - 1, quadEndHndl.width+2, quadEndHndl.height+2);
	Rectangle cub1 = new Rectangle(cubHndl1.x + (int)cubXPos - 1, cubHndl1.y + (int)cubYPos - 1, cubHndl1.width+2, cubHndl1.height+2);
	Rectangle cub2 = new Rectangle(cubHndl2.x + (int)cubXPos - 1, cubHndl2.y + (int)cubYPos - 1, cubHndl2.width+2, cubHndl2.height+2);
	Rectangle cubEnd = new Rectangle(cubEndHndl.x + (int)cubXPos - 1, cubEndHndl.y + (int)cubYPos - 1, cubEndHndl.width+2, cubEndHndl.height+2);

	return ( quad.contains(e.x, e.y) || quadEnd.contains(e.x, e.y)
		 || cub1.contains(e.x, e.y) || cub2.contains(e.x, e.y)
		 || cubEnd.contains(e.x, e.y));
}
/**
 * Creates the widgets used to control the drawing.
 */
@Override
public void createControlPanel(Composite parent) {
	if (cursor == null) {
		cursor = parent.getDisplay().getSystemCursor(SWT.CURSOR_HAND);
	}

	mouseMoveListener = e -> {
		if (hovering && mouseDown) {
			example.canvas.setCursor(cursor);
		} else if (isHovering(e)) {
			example.canvas.setCursor(cursor);
			hovering = true;
		} else {
			example.canvas.setCursor(null);
			hovering = false;
		}

		if (quadPtMoved) {
			quadDiffX = quadDiffX + e.x - (int)quadXPos - quadHndl.x;
			quadDiffY = quadDiffY + e.y - (int)quadYPos - quadHndl.y;
			quadHndl.x = e.x - (int)quadXPos;
			quadHndl.y = e.y - (int)quadYPos;
		} else if (quadEndPtMoved) {
			quadEndDiffX = quadEndDiffX + e.x - (int)quadXPos - quadEndHndl.x;
			quadEndDiffY = quadEndDiffY + e.y - (int)quadYPos - quadEndHndl.y;
			quadEndHndl.x = e.x - (int)quadXPos;
			quadEndHndl.y = e.y - (int)quadYPos;
		} else if (cubPt1Moved) {
			cubDiffX1 = cubDiffX1 + e.x - (int)cubXPos - cubHndl1.x;
			cubDiffY1 = cubDiffY1 + e.y - (int)cubYPos - cubHndl1.y;
			cubHndl1.x = e.x - (int)cubXPos;
			cubHndl1.y = e.y - (int)cubYPos;
		} else if (cubPt2Moved) {
			cubDiffX2 = cubDiffX2 + e.x - (int)cubXPos - cubHndl2.x;
			cubDiffY2 = cubDiffY2 + e.y - (int)cubYPos - cubHndl2.y;
			cubHndl2.x = e.x - (int)cubXPos;
			cubHndl2.y = e.y - (int)cubYPos;
		} else if (cubEndPtMoved) {
			cubEndDiffX = cubEndDiffX + e.x - (int)cubXPos - cubEndHndl.x;
			cubEndDiffY = cubEndDiffY + e.y - (int)cubYPos - cubEndHndl.y;
			cubEndHndl.x = e.x - (int)cubXPos;
			cubEndHndl.y = e.y - (int)cubYPos;
		}
		example.redraw();
	};

	mouseListener = new MouseListener() {

		@Override
		public void mouseDoubleClick(MouseEvent e) {}

		/**
		 * Sent when a mouse button is pressed.
		 *
		 * @param e an event containing information about the mouse button press
		 */
		@Override
		public void mouseDown(MouseEvent e) {
			Rectangle quad = new Rectangle(quadHndl.x + (int)quadXPos - 1, quadHndl.y + (int)quadYPos - 1, quadHndl.width+2, quadHndl.height+2);
			Rectangle quadEnd = new Rectangle(quadEndHndl.x + (int)quadXPos - 1, quadEndHndl.y + (int)quadYPos - 1, quadEndHndl.width+2, quadEndHndl.height+2);
			Rectangle cub1 = new Rectangle(cubHndl1.x + (int)cubXPos - 1, cubHndl1.y + (int)cubYPos - 1, cubHndl1.width+2, cubHndl1.height+2);
			Rectangle cub2 = new Rectangle(cubHndl2.x + (int)cubXPos - 1, cubHndl2.y + (int)cubYPos - 1, cubHndl2.width+2, cubHndl2.height+2);
			Rectangle cubEnd = new Rectangle(cubEndHndl.x + (int)cubXPos - 1, cubEndHndl.y + (int)cubYPos - 1, cubEndHndl.width+2, cubEndHndl.height+2);

			if (quad.contains(e.x, e.y)) {
				quadPtMoved = true;
				mouseDown = true;
			} else if (quadEnd.contains(e.x, e.y)) {
				quadEndPtMoved = true;
				mouseDown = true;
			} else if (cub1.contains(e.x, e.y)) {
				cubPt1Moved = true;
				mouseDown = true;
			} else if (cub2.contains(e.x, e.y)) {
				cubPt2Moved = true;
				mouseDown = true;
			} else if (cubEnd.contains(e.x, e.y)) {
				cubEndPtMoved = true;
				mouseDown = true;
			}
		}

		/**
		 * Sent when a mouse button is released.
		 *
		 * @param e an event containing information about the mouse button release
		 */
		@Override
		public void mouseUp(MouseEvent e) {
			mouseDown = false;
			if (isHovering(e)) {
				example.canvas.setCursor(cursor);
			} else {
				example.canvas.setCursor(null);
			}

			if (quadPtMoved)
				quadPtMoved = false;
			if (quadEndPtMoved)
				quadEndPtMoved = false;
			if (cubPt1Moved)
				cubPt1Moved = false;
			if (cubPt2Moved)
				cubPt2Moved = false;
			if (cubEndPtMoved)
				cubEndPtMoved = false;

			example.redraw();
		}
	};
	example.canvas.addMouseMoveListener(mouseMoveListener);
	example.canvas.addMouseListener(mouseListener);
}

@Override
public void paint(GC gc, int width, int height) {
	if (!example.checkAdvancedGraphics()) return;
	Device device = gc.getDevice();

	Font font = new Font(device, getPlatformFont(), 16, SWT.ITALIC);
	gc.setFont(font);
	gc.setLineWidth(5);

	Transform transform;

	// ----- cubic curve -----
	cubXPos = width/5;
	cubYPos = height/3;

	transform = new Transform(device);
	transform.translate(cubXPos, cubYPos);
	gc.setTransform(transform);
	transform.dispose();

	gc.setForeground(device.getSystemColor(SWT.COLOR_RED));
	gc.drawString(GraphicsExample.getResourceString("Cubic"), 25, -70, true);

	Path path = new Path(device);
	path.cubicTo(133 + cubDiffX1, -60 + cubDiffY1, 266 + cubDiffX2, 60 + cubDiffY2, 400 + cubEndDiffX, 0 + cubEndDiffY);
	gc.drawPath(path);
	path.dispose();

	gc.setTransform(null);
	gc.setForeground(device.getSystemColor(SWT.COLOR_DARK_BLUE));
	gc.drawRectangle(cubHndl1.x + (int)cubXPos, cubHndl1.y + (int)cubYPos, cubHndl1.width, cubHndl1.height);
	gc.drawRectangle(cubHndl2.x + (int)cubXPos, cubHndl2.y + (int)cubYPos, cubHndl2.width, cubHndl2.height);
	gc.drawRectangle(cubEndHndl.x + (int)cubXPos, cubEndHndl.y + (int)cubYPos, cubEndHndl.width, cubEndHndl.height);

	// ----- quadratic curve -----
	quadXPos = width/5;
	quadYPos = 2*height/3;

	transform = new Transform(device);
	transform.translate(quadXPos, quadYPos);
	gc.setTransform(transform);
	transform.dispose();

	gc.setForeground(device.getSystemColor(SWT.COLOR_GREEN));
	gc.drawString(GraphicsExample.getResourceString("Quadratic"), 0, -50, true);

	path = new Path(device);
	path.quadTo(200 + quadDiffX, 150 + quadDiffY, 400 + quadEndDiffX, 0 + quadEndDiffY);
	gc.drawPath(path);
	path.dispose();

	gc.setTransform(null);
	gc.setForeground(device.getSystemColor(SWT.COLOR_GRAY));
	gc.drawRectangle(quadHndl.x + (int)quadXPos, quadHndl.y + (int)quadYPos, quadHndl.width, quadHndl.height);
	gc.drawRectangle(quadEndHndl.x + (int)quadXPos, quadEndHndl.y + (int)quadYPos, quadEndHndl.width, quadEndHndl.height);

	font.dispose();
}

/**
 * Returns the name of a valid font for the resident platform.
 */
static String getPlatformFont() {
	if(SWT.getPlatform() == "win32") {
		return "Arial";
	} else if (SWT.getPlatform() == "gtk") {
		return "Baekmuk Batang";
	} else {
		return "Verdana";
	}
}

}

Back to the top