Skip to content

Commit

Permalink
Implements some tricks to have correct sizes for the buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed Nov 19, 2024
1 parent c5dd9c1 commit 46ce6f0
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 286 deletions.
5 changes: 1 addition & 4 deletions gama.ui.editor/src/gaml/compiler/ui/editor/GamlEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,6 @@ protected static IPreferenceStore getPreferences() {
/** The menu images. */
static Map<String, Image> menu_images = new HashMap();

/** The max image height. */
static int maxImageHeight = 0;

/** The button padding. How much space between each experiment button */
static int buttonPadding = 4;
Expand All @@ -231,7 +229,7 @@ protected static IPreferenceStore getPreferences() {
GamaIcon.named(ThemeHelper.isDark() ? IGamaIcons.BUTTON_GUI : IGamaIcons.MENU_GUI).image());

images.put("new", GamaIcon.named(IGamaIcons.ADD_EXPERIMENT).image());
for (Image im : images.values()) { maxImageHeight = Math.max(maxImageHeight, im.getBounds().height); }
//for (Image im : images.values()) { maxImageHeight = Math.max(maxImageHeight, im.getBounds().height); }
}

/**
Expand Down Expand Up @@ -603,7 +601,6 @@ private void updateToolbar(final GamlEditorState newState, final boolean forceSt
WorkbenchHelper.runInUI("Editor refresh", 50, m -> {
if (toolbar == null || toolbar.isDisposed()) return;
toolbar.wipe(SWT.LEFT, true);
toolbar.setDefaultHeight(maxImageHeight);

final var c = state.getColor();
var msg = state.getStatus();
Expand Down
104 changes: 59 additions & 45 deletions gama.ui.shared/src/gama/ui/shared/resources/GamaIcon.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ public class GamaIcon {
try {
URL pngFolderURL = toFileURL(Platform.getBundle(IGamaIcons.PLUGIN_ID).getEntry(IGamaIcons.ICONS_PATH));
tmp = Path.of(new URI(pngFolderURL.getProtocol(), pngFolderURL.getPath(), null).normalize());
} catch (Exception e) {}
} catch (Exception e) {
}
PATH_TO_ICONS = tmp;
}

/**
* Preload icons.
*
* @param bundle
* the bundle
* @param bundle the bundle
* @throws IOException
*/
public static void preloadAllIcons() throws IOException {
Expand All @@ -107,26 +107,43 @@ public static void preloadAllIcons() throws IOException {
/**
* Returns the icon named after the path (eg "templates/square.template")
*
* @param path
* the path
* @param path the path
* @return the gama icon
*/
public static GamaIcon named(final String s) {

try {
// DEBUG.OUT("Looking for icon " + s);
if (s != null) return ICON_CACHE.get(s, () -> new GamaIcon(s));
} catch (ExecutionException e) {}
if (s != null)
return ICON_CACHE.get(s, () -> new GamaIcon(s));
} catch (ExecutionException e) {
}
return named(MISSING);
}

/**
* Creates a transparent icon of the desired and width (and 1px wide)
*
*/

public static GamaIcon ofSize(int width, int height) {
final String name = "size" + width + "x" + height;
try {
return ICON_CACHE.get(name, () -> {
DEBUG.OUT(name + " not found. Building it");
GamaImage bi = GamaImage.ofDimensions(width, height, true);
return new GamaIcon(name, bi);
});
} catch (Exception e) {
return null;
}
}

/**
* Creates a color icon, either a round square or a circle
*
* @param gcolor
* the gcolor
* @param square
* the square
* @param gcolor the gcolor
* @param square the square
* @return the image
*/

Expand Down Expand Up @@ -170,12 +187,9 @@ public static GamaIcon ofColor(final GamaUIColor gcolor, final boolean square) {
/**
* Constructor for images loaded from a plugin
*
* @param c
* the code
* @param p
* the path (in the 'icons' folder)
* @param plugin
* the id of the plugin in which the 'icons' folder resides
* @param c the code
* @param p the path (in the 'icons' folder)
* @param plugin the id of the plugin in which the 'icons' folder resides
*/
private GamaIcon(final String c) {
DEBUG.OUT("Creation of icon " + c, false);
Expand All @@ -188,12 +202,11 @@ private GamaIcon(final String c) {
}

/**
* Instantiates a new gama icon directly from an image. We do not produce disabled versions
* Instantiates a new gama icon directly from an image. We do not produce
* disabled versions
*
* @param name
* the name
* @param im
* the im
* @param name the name
* @param im the im
*/
private GamaIcon(final String path, final Image im, final Image disabled) {
code = path;
Expand All @@ -207,10 +220,8 @@ private GamaIcon(final String path, final Image im, final Image disabled) {
* Instantiates a new gama icon.
*
* @author Alexis Drogoul ([email protected])
* @param path
* the path
* @param im
* the im
* @param path the path
* @param im the im
* @date 13 sept. 2023
*/
private GamaIcon(final String path, final GamaImage im) {
Expand All @@ -220,15 +231,15 @@ private GamaIcon(final String path, final GamaImage im) {
/**
* Creates a SWT image from a Java BufferedImage.
*
* @param bufferedImage
* the image.
* @param bufferedImage the image.
* @return returns a SWT image.
*/
public static Image toSWTImage(final GamaImage im) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(im, "png", out);
} catch (IOException e) {}
} catch (IOException e) {
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new Image(Display.getCurrent(), new ImageData(in));
}
Expand All @@ -247,10 +258,8 @@ private static class DisabledFilter extends RGBImageFilter {
/**
* Instantiates a new disabled filter.
*
* @param min
* the min
* @param max
* the max
* @param min the min
* @param max the max
*/
DisabledFilter() {
canFilterIndexColorModel = true;
Expand All @@ -275,13 +284,13 @@ public int filterRGB(final int x, final int y, final int rgb) {
* To buffered image.
*
* @author Alexis Drogoul ([email protected])
* @param img
* the img
* @param img the img
* @return the buffered image
* @date 15 sept. 2023
*/
public static BufferedImage toBufferedImage(final java.awt.Image img) {
if (img instanceof BufferedImage) return (BufferedImage) img;
if (img instanceof BufferedImage)
return (BufferedImage) img;
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
Expand All @@ -292,8 +301,7 @@ public static BufferedImage toBufferedImage(final java.awt.Image img) {
/**
* Creates a SWT image from a Java BufferedImage.
*
* @param bufferedImage
* the image.
* @param bufferedImage the image.
* @return returns a SWT image.
*/
public static Image toDisabledSWTImage(final BufferedImage im) {
Expand All @@ -302,7 +310,8 @@ public static Image toDisabledSWTImage(final BufferedImage im) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(toBufferedImage(gray), "png", out);
} catch (IOException e) {}
} catch (IOException e) {
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new Image(Display.getCurrent(), new ImageData(in));
}
Expand All @@ -321,16 +330,16 @@ public static Image toDisabledSWTImage(final BufferedImage im) {
* To checked SWT image.
*
* @author Alexis Drogoul ([email protected])
* @param im
* the im
* @param im the im
* @return the image
* @date 15 sept. 2023
*/
public static Image toCheckedSWTImage(final GamaImage im) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
try {
ImageIO.write(ImageOperators.darker(null, im, 0.5), "png", out);
} catch (IOException e) {}
} catch (IOException e) {
}
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
return new Image(Display.getCurrent(), new ImageData(in));

Expand Down Expand Up @@ -364,8 +373,11 @@ private Image image(final String key, final Callable<Image> imageCreator) {
if (image == null) {
try {
image = imageCreator.call();
} catch (Exception e) {}
if (image == null) { image = named(MISSING).image(); }
} catch (Exception e) {
}
if (image == null) {
image = named(MISSING).image();
}
if (JFaceResources.getImageRegistry().get(key) == null) {
JFaceResources.getImageRegistry().put(key, image);
}
Expand Down Expand Up @@ -408,7 +420,9 @@ public Image checked() {
*
* @return the code
*/
public String getCode() { return code; }
public String getCode() {
return code;
}

/**
* Compute URL.
Expand Down
Loading

0 comments on commit 46ce6f0

Please sign in to comment.