Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion org.eclipse.gef/src/org/eclipse/gef/SharedCursors.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,13 @@ private static Cursor createCursor(String sourceName) {
if (ImageUtils.isSvgSupported()) {
ImageDescriptor src1 = InternalImages.createDescriptor(sourceName);
ImageDescriptor src2 = InternalCursor.getCursorDescriptor();
ImageDescriptor src = new DecorationOverlayIcon(src1, src2, IDecoration.TOP_LEFT);
ImageDescriptor src = new DecorationOverlayIcon(src1, src2, IDecoration.TOP_LEFT) {
@Override
// Disabled by default due to https://bugs.eclipse.org/bugs/show_bug.cgi?id=97506
protected boolean supportsZoomLevel(int zoomLevel) {
return true;
}
};
return InternalGEFPlugin.createCursor(src, 0, 0);
}
ImageDescriptor src = InternalImages.createDescriptor(sourceName);
Expand Down
96 changes: 10 additions & 86 deletions org.eclipse.gef/src/org/eclipse/gef/internal/InternalCursor.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@

package org.eclipse.gef.internal;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.PaletteData;
import org.eclipse.swt.graphics.Path;
import org.eclipse.swt.widgets.Display;

import org.eclipse.jface.resource.ImageDescriptor;

import org.eclipse.draw2d.ColorConstants;

/**
* This class defines the shape of the default GEF-cursor used for the plug/tree
* images. The cursor can't be part of the SVG itself, as it is a)
Expand All @@ -37,90 +25,26 @@
* always has a stroke-width of 1px.
*/
public class InternalCursor {
/**
* Defines the shape of the cursor at 100% zoom.
*/
//@formatter:off
private static final float[] CURSOR_POINTS = {
0f, 0f,
0f, 17f,
4f, 13f,
7f, 19f,
9f, 18f,
7f, 13f,
7f, 12f,
12f, 12f,
0f, 0f
};
//@formatter:on
/**
* Local cache to store the cursor data for each zoom level.
*/
private static final Map<Integer, ImageData> CURSOR_AT_ZOOM = new HashMap<>();
private static final ImageDescriptor CURSOR_AT_100_ZOOM = InternalImages.createDescriptor("icons/[email protected]"); //$NON-NLS-1$
private static final ImageDescriptor CURSOR_AT_150_ZOOM = InternalImages.createDescriptor("icons/[email protected]"); //$NON-NLS-1$
private static final ImageDescriptor CURSOR_AT_200_ZOOM = InternalImages.createDescriptor("icons/[email protected]"); //$NON-NLS-1$
/**
* The default cursor that is constructed using {link {@link #CURSOR_POINTS}.
* May be replaced with a custom cursor by calling
* {@link #setCursorDescriptor(ImageDescriptor)}.
*/
private static ImageDescriptor CURRENT_CURSOR_DESCRIPTOR = ImageDescriptor
.createFromImageDataProvider(zoom -> CURSOR_AT_ZOOM.computeIfAbsent(zoom, InternalCursor::getCursorAtZoom));

/**
* This method generates the image data for the cursor at the given zoom level.
* The points defined with {@link #CURSOR_POINTS} are scaled by the given zoom
* and painted onto an image.
*
* @param zoom The zoom level. e.g. 100, 125, 200
* @return The cursor image data at the given zoom level.
*/
private static ImageData getCursorAtZoom(int zoom) {
float maxWidth = 0f;
float maxHeight = 0f;

for (int i = 0; i < CURSOR_POINTS.length; i += 2) {
maxWidth = Math.max(maxWidth, CURSOR_POINTS[i]);
maxHeight = Math.max(maxHeight, CURSOR_POINTS[i + 1]);
private static ImageDescriptor CURRENT_CURSOR_DESCRIPTOR = ImageDescriptor.createFromImageDataProvider(zoom -> {
if (zoom < 150) {
return CURSOR_AT_100_ZOOM.getImageData(100);
}

float zoomFactor = zoom / 100.0f;

int width = 1 + (int) Math.ceil(zoomFactor * maxWidth);
int height = 1 + (int) Math.ceil(zoomFactor * maxHeight);

//
Display display = Display.getDefault();
// Construct path
Path path = new Path(display);
for (int i = 0; i < CURSOR_POINTS.length; i += 2) {
float x = zoomFactor * CURSOR_POINTS[i];
float y = zoomFactor * CURSOR_POINTS[i + 1];
if (i == 0) {
path.moveTo(x, y);
} else {
path.lineTo(x, y);
}
if (zoom < 200) {
return CURSOR_AT_150_ZOOM.getImageData(100);
}
// Construct image
ImageData imageData = new ImageData(width, height, 32, new PaletteData(0xFF0000, 0x00FF00, 0x0000FF));
imageData.alphaData = new byte[width * height];
Image image = new Image(display, imageData);
GC gc = new GC(image);
gc.setAlpha(0);
gc.fillRectangle(0, 0, width, height);
gc.setAlpha(255);
gc.setAntialias(SWT.ON);
gc.setLineWidth(1);
gc.setBackground(ColorConstants.white);
gc.fillPath(path);
gc.setBackground(ColorConstants.black);
gc.drawPath(path);
gc.dispose();
path.dispose();
// Image is already scaled to expected zoom level
imageData = image.getImageData(100);
image.dispose();
return imageData;
}
return CURSOR_AT_200_ZOOM.getImageData(100);
});

/**
* Returns the image descriptor for the GEF cursor. Never {@code null}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,18 @@ public static ImageData scaledImageData(ImageDescriptor descriptor, int zoom) {
*/
public static Cursor createCursor(ImageDescriptor source, int hotspotX, int hotspotY) {
try {
ImageDataProvider provider = zoom -> {
if (zoom < 150) {
return source.getImageData(100);
}
if (zoom < 200) {
return source.getImageData(150);
}
return source.getImageData(200);
};
Constructor<Cursor> ctor = Cursor.class.getConstructor(Device.class, ImageDataProvider.class, int.class,
int.class);
return ctor.newInstance(null, (ImageDataProvider) source::getImageData, hotspotX, hotspotY);
return ctor.newInstance(null, provider, hotspotX, hotspotY);
} catch (NoSuchMethodException e) {
// SWT version < 3.131.0 (no ImageDataProvider-based constructor)
return new Cursor(null, source.getImageData(100), hotspotX, hotspotY); // older constructor
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading