|
13 | 13 |
|
14 | 14 | package org.eclipse.gef.internal; |
15 | 15 |
|
16 | | -import java.util.HashMap; |
17 | | -import java.util.Map; |
18 | 16 | import java.util.Objects; |
19 | 17 |
|
20 | | -import org.eclipse.swt.SWT; |
21 | | -import org.eclipse.swt.graphics.GC; |
22 | | -import org.eclipse.swt.graphics.Image; |
23 | | -import org.eclipse.swt.graphics.ImageData; |
24 | | -import org.eclipse.swt.graphics.PaletteData; |
25 | | -import org.eclipse.swt.graphics.Path; |
26 | | -import org.eclipse.swt.widgets.Display; |
27 | | - |
28 | 18 | import org.eclipse.jface.resource.ImageDescriptor; |
29 | 19 |
|
30 | | -import org.eclipse.draw2d.ColorConstants; |
31 | | - |
32 | 20 | /** |
33 | 21 | * This class defines the shape of the default GEF-cursor used for the plug/tree |
34 | 22 | * images. The cursor can't be part of the SVG itself, as it is a) |
|
37 | 25 | * always has a stroke-width of 1px. |
38 | 26 | */ |
39 | 27 | public class InternalCursor { |
40 | | - /** |
41 | | - * Defines the shape of the cursor at 100% zoom. |
42 | | - */ |
43 | | - //@formatter:off |
44 | | - private static final float[] CURSOR_POINTS = { |
45 | | - 0f, 0f, |
46 | | - 0f, 17f, |
47 | | - 4f, 13f, |
48 | | - 7f, 19f, |
49 | | - 9f, 18f, |
50 | | - 7f, 13f, |
51 | | - 7f, 12f, |
52 | | - 12f, 12f, |
53 | | - 0f, 0f |
54 | | - }; |
55 | | - //@formatter:on |
56 | 28 | /** |
57 | 29 | * Local cache to store the cursor data for each zoom level. |
58 | 30 | */ |
59 | | - private static final Map<Integer, ImageData> CURSOR_AT_ZOOM = new HashMap<>(); |
| 31 | + private static final ImageDescriptor CURSOR_AT_100_ZOOM = InternalImages. createDescriptor( "icons/[email protected]"); //$NON-NLS-1$ |
| 32 | + private static final ImageDescriptor CURSOR_AT_150_ZOOM = InternalImages. createDescriptor( "icons/[email protected]"); //$NON-NLS-1$ |
| 33 | + private static final ImageDescriptor CURSOR_AT_200_ZOOM = InternalImages. createDescriptor( "icons/[email protected]"); //$NON-NLS-1$ |
60 | 34 | /** |
61 | 35 | * The default cursor that is constructed using {link {@link #CURSOR_POINTS}. |
62 | 36 | * May be replaced with a custom cursor by calling |
63 | 37 | * {@link #setCursorDescriptor(ImageDescriptor)}. |
64 | 38 | */ |
65 | | - private static ImageDescriptor CURRENT_CURSOR_DESCRIPTOR = ImageDescriptor |
66 | | - .createFromImageDataProvider(zoom -> CURSOR_AT_ZOOM.computeIfAbsent(zoom, InternalCursor::getCursorAtZoom)); |
67 | | - |
68 | | - /** |
69 | | - * This method generates the image data for the cursor at the given zoom level. |
70 | | - * The points defined with {@link #CURSOR_POINTS} are scaled by the given zoom |
71 | | - * and painted onto an image. |
72 | | - * |
73 | | - * @param zoom The zoom level. e.g. 100, 125, 200 |
74 | | - * @return The cursor image data at the given zoom level. |
75 | | - */ |
76 | | - private static ImageData getCursorAtZoom(int zoom) { |
77 | | - float maxWidth = 0f; |
78 | | - float maxHeight = 0f; |
79 | | - |
80 | | - for (int i = 0; i < CURSOR_POINTS.length; i += 2) { |
81 | | - maxWidth = Math.max(maxWidth, CURSOR_POINTS[i]); |
82 | | - maxHeight = Math.max(maxHeight, CURSOR_POINTS[i + 1]); |
| 39 | + private static ImageDescriptor CURRENT_CURSOR_DESCRIPTOR = ImageDescriptor.createFromImageDataProvider(zoom -> { |
| 40 | + if (zoom < 150) { |
| 41 | + return CURSOR_AT_100_ZOOM.getImageData(100); |
83 | 42 | } |
84 | | - |
85 | | - float zoomFactor = zoom / 100.0f; |
86 | | - |
87 | | - int width = 1 + (int) Math.ceil(zoomFactor * maxWidth); |
88 | | - int height = 1 + (int) Math.ceil(zoomFactor * maxHeight); |
89 | | - |
90 | | - // |
91 | | - Display display = Display.getDefault(); |
92 | | - // Construct path |
93 | | - Path path = new Path(display); |
94 | | - for (int i = 0; i < CURSOR_POINTS.length; i += 2) { |
95 | | - float x = zoomFactor * CURSOR_POINTS[i]; |
96 | | - float y = zoomFactor * CURSOR_POINTS[i + 1]; |
97 | | - if (i == 0) { |
98 | | - path.moveTo(x, y); |
99 | | - } else { |
100 | | - path.lineTo(x, y); |
101 | | - } |
| 43 | + if (zoom < 200) { |
| 44 | + return CURSOR_AT_150_ZOOM.getImageData(100); |
102 | 45 | } |
103 | | - // Construct image |
104 | | - ImageData imageData = new ImageData(width, height, 32, new PaletteData(0xFF0000, 0x00FF00, 0x0000FF)); |
105 | | - imageData.alphaData = new byte[width * height]; |
106 | | - Image image = new Image(display, imageData); |
107 | | - GC gc = new GC(image); |
108 | | - gc.setAlpha(0); |
109 | | - gc.fillRectangle(0, 0, width, height); |
110 | | - gc.setAlpha(255); |
111 | | - gc.setAntialias(SWT.ON); |
112 | | - gc.setLineWidth(1); |
113 | | - gc.setBackground(ColorConstants.white); |
114 | | - gc.fillPath(path); |
115 | | - gc.setBackground(ColorConstants.black); |
116 | | - gc.drawPath(path); |
117 | | - gc.dispose(); |
118 | | - path.dispose(); |
119 | | - // Image is already scaled to expected zoom level |
120 | | - imageData = image.getImageData(100); |
121 | | - image.dispose(); |
122 | | - return imageData; |
123 | | - } |
| 46 | + return CURSOR_AT_200_ZOOM.getImageData(100); |
| 47 | + }); |
124 | 48 |
|
125 | 49 | /** |
126 | 50 | * Returns the image descriptor for the GEF cursor. Never {@code null}. |
|
0 commit comments