Skip to content

Commit

Permalink
Allows to lock Java2D displays in the code (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisDrogoul committed May 1, 2024
1 parent e295178 commit ae53f8d
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 80 deletions.
7 changes: 5 additions & 2 deletions gama.core/src/gama/core/outputs/LayeredDisplayData.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*******************************************************************************************************
*
* LayeredDisplayData.java, in gama.core, is part of the source code of the GAMA modeling and simulation platform
* .
* (v.2024-06).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, ESPACE-DEV, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
Expand Down Expand Up @@ -625,6 +625,9 @@ public void initWith(final IScope scope, final IDescription desc) {
final IExpression antialias = facets.getExpr("antialias");
if (antialias != null) { setAntialias(Cast.asBool(scope, antialias.value(scope))); }

final IExpression locked = facets.getExpr("locked");
if (locked != null) { setCameraLocked(Cast.asBool(scope, locked.value(scope))); }

if (camera != null) { camera.refresh(scope); }
if (rotation != null) { rotation.refresh(scope); }
lights.forEach((s, l) -> l.refresh(scope));
Expand Down
5 changes: 5 additions & 0 deletions gama.core/src/gama/core/outputs/LayeredDisplayOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,11 @@
type = IType.BOOL,
optional = true,
doc = @doc ("Allows to enable/disable the light at once. Default is true")),
@facet (
name = "locked",
type = IType.BOOL,
optional = true,
doc = @doc ("Allows to lock/unlock a 2D display when it opens. For 3D displays please use the `camera` statement")),
@facet (
name = "draw_diffuse_light",
type = IType.BOOL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/*******************************************************************************************************
*
* AWTDisplayView.java, in gama.ui.display.java2d, is part of the source code of the GAMA modeling and simulation platform
* .
* AWTDisplayView.java, in gama.ui.display.java2d, is part of the source code of the GAMA modeling and simulation
* platform (v.2024-06).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, ESPACE-DEV, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
Expand All @@ -28,11 +28,13 @@ public class AWTDisplayView extends LayeredDisplayView {
@Override
protected Composite createSurfaceComposite(final Composite parent) {
if (getOutput() == null) return null;
surfaceComposite = SwingControl.create(parent, AWTDisplayView.this, (Java2DDisplaySurface) getDisplaySurface(),
SWT.NO_FOCUS);
surfaceComposite = SwingControl.create(parent, AWTDisplayView.this, getDisplaySurface(), SWT.NO_FOCUS);
return surfaceComposite;
}

@Override
public Java2DDisplaySurface getDisplaySurface() { return (Java2DDisplaySurface) super.getDisplaySurface(); }

@Override
public void ownCreatePartControl(final Composite c) {
super.ownCreatePartControl(c);
Expand All @@ -56,7 +58,6 @@ public void focusCanvas() {
WorkbenchHelper.asyncRun(() -> centralPanel.forceFocus());
}


@Override
public IDisposable getMultiListener() {
SWTLayeredDisplayMultiListener listener = (SWTLayeredDisplayMultiListener) super.getMultiListener();
Expand All @@ -74,4 +75,7 @@ public boolean is2D() {
return true;
}

@Override
public boolean isLocked() { return getDisplaySurface().isLocked(); }

}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class Java2DDisplaySurface extends JPanel implements IDisplaySurface {
Point mousePosition;

/** The is locked. */
private boolean is_locked = false;
private boolean isLocked = false;

/**
* Instantiates a new java 2 D display surface.
Expand All @@ -158,11 +158,11 @@ public Java2DDisplaySurface(final Object... args) {
output.setSurface(this);
setDisplayScope(output.getScope().copyForGraphics("in java2D display"));
output.getData().addListener(this);
// temp_focus = output.getFacet(IKeyword.FOCUS);
setDoubleBuffered(true);
setIgnoreRepaint(true);
setLayout(new BorderLayout());
setBackground(output.getData().getBackgroundColor());
isLocked = output.getData().isCameraLocked();
setName(output.getName());
layerManager = new LayerManager(this, output);

Expand Down Expand Up @@ -227,7 +227,7 @@ public void setMousePosition(final int xm, final int ym) {

@Override
public void draggedTo(final int x, final int y) {
if (!is_locked) {
if (!isLocked) {
final Point origin = getOrigin();
setOrigin(origin.x + x - getMousePosition().x, origin.y + y - getMousePosition().y);
updateDisplay(true);
Expand Down Expand Up @@ -413,12 +413,6 @@ public void focusOn(final IShape geometry) {

updateDisplay(true);
}
//
// @Override
// public void validate() {}
//
// @Override
// public void doLayout() {}

/**
* Zoom.
Expand Down Expand Up @@ -446,19 +440,26 @@ private void zoom(final boolean in) {

@Override
public void zoomIn() {
if (!is_locked) { zoom(true); }
if (!isLocked) { zoom(true); }
}

@Override
public void zoomOut() {
if (!is_locked) { zoom(false); }
if (!isLocked) { zoom(false); }
}

@Override
public void toggleLock() {
is_locked = !is_locked;
isLocked = !isLocked;
}

/**
* Checks if is locked.
*
* @return true, if is locked
*/
public boolean isLocked() { return isLocked; }

/**
* Checks if is image edge in panel.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*******************************************************************************************************
*
* OpenGLDisplayView.java, in gama.ui.display.opengl, is part of the source code of the GAMA modeling and simulation
* platform (v.2.0.0).
* OpenGLDisplayView.java, in gama.ui.display.opengl, is part of the source code of the
* GAMA modeling and simulation platform (v.2024-06).
*
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, TLU, CTU)
*
* Visit https://github.com/gama-platform/gama2 for license information and contacts.
* (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, ESPACE-DEV, CTU)
*
* Visit https://github.com/gama-platform/gama for license information and contacts.
*
********************************************************************************************************/
package gama.ui.display.opengl.view;

Expand Down Expand Up @@ -131,4 +131,7 @@ public boolean is2D() {
return false;
}

@Override
public boolean isLocked() { return getCameraHelper().isCameraLocked(); }

}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/******************************************************************************************************* * * IToolbarDecoratedView.java, in gama.ui.shared, is part of the source code of the GAMA modeling and simulation * platform (v.2024-06). * * (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, ESPACE-DEV, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * ********************************************************************************************************/package gama.ui.shared.views.toolbar;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.util.Collection;import java.util.Collections;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.FileDialog;import org.eclipse.ui.IWorkbenchSite;import gama.core.common.preferences.GamaPreferences;import gama.core.outputs.IOutput;import gama.core.runtime.GAMA;import gama.ui.shared.resources.GamaColors.GamaUIColor;import gama.ui.shared.utils.WorkbenchHelper;/** * * Class IToolbarDecoratedView. * * * * @author drogoul * * @since 7 déc. 2014 * * * */public interface IToolbarDecoratedView { /** The null camera. */ ICameraHelper NULL_CAMERA = new ICameraHelper() {}; /** * The Interface ICameraHelper. */ public interface ICameraHelper { /** * * Gets the camera names. * * * * @return the camera names * */ default Collection<String> getCameraNames() { return Collections.EMPTY_LIST; } /** * * Sets the camera name. * * * * @param p * * the new camera name * */ default void setCameraName(final String p) {} /** * * Gets the camera name. * * * * @return the camera name * */ default String getCameraName() { return GamaPreferences.Displays.OPENGL_DEFAULT_CAM.getValue(); } /** * * Checks if is camera locked. * * * * @return true, if is camera locked * */ default boolean isCameraLocked() { return false; } /** * * Toggle camera. * */ default void toggleCamera() {} /** * Gets the camera definition. * * @return the camera definition */ default String getCameraDefinition() { return ""; } } /** * * Gets the site. * * * * @return the site * */ IWorkbenchSite getSite(); /** * * Creates the tool items. * * * * @param tb * * the tb * */ void createToolItems(GamaToolbar2 tb); /** * * Adds the state listener. ✓ Unicode: U+2713, UTF-8: E2 9C 93 * * * * @param listener * * the listener * */ default void addStateListener(final StateListener listener) {} /** * * The listener interface for receiving state events. The class that is interested in processing a state event * * implements this interface, and the object created with that class is registered with a component using the * * component's <code>addStateListener<code> method. When the state event occurs, that object's appropriate method is * * invoked. * * * * @see StateEvent * */ public interface StateListener { /** * * Update to reflect state. * */ void updateToReflectState(); } /** * * The Interface Expandable. * */ public interface Expandable extends IToolbarDecoratedView { /** * * Expand all. * */ void expandAll(); /** * * Collapse all. * */ void collapseAll(); } /** * * The Interface Pausable. * */ public interface Pausable extends IToolbarDecoratedView { /** * * Pause changed. * */ void pauseChanged(); /** * * Gets the output. * * * * @return the output * */ IOutput getOutput(); } /** * * The Interface Sizable. * */ public interface Sizable extends IToolbarDecoratedView { /** * * Gets the sizable font control. * * * * @return the sizable font control * */ Control getSizableFontControl(); } /** * * The Interface Colorizable. * */ public interface Colorizable extends IToolbarDecoratedView { /** * * Gets the color labels. * * * * @return the color labels * */ String[] getColorLabels(); /** * * Gets the color. * * * * @param index * * the index * * @return the color * */ GamaUIColor getColor(int index); /** * * Sets the color. * * * * @param index * * the index * * @param c * * the c * */ void setColor(int index, GamaUIColor c); } /** * * The Interface CSVExportable. * */ public interface CSVExportable extends IToolbarDecoratedView { /** * * Save as CSV. * */ void saveAsCSV(); } /** * * The Interface LogExportable. * */ public interface LogExportable extends IToolbarDecoratedView { /** * * Save as log. * */ default void saveAsLog() { String text = getContents(); FileDialog fd = new FileDialog(WorkbenchHelper.getShell(), SWT.SAVE); fd.setText("Choose a destination file"); fd.setFilterExtensions(new String[] { "*.log" }); if (GAMA.getExperiment() != null && GAMA.getExperiment().getAgent() != null) { fd.setFilterPath(GAMA.getExperiment().getAgent().getProjectPath()); } else { fd.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); } String f = fd.open(); if (f == null) return; try { Files.writeString(Path.of(f), text, StandardCharsets.UTF_8); } catch (IOException e) {} } /** * * Gets the contents. * * * * @return the contents * */ String getContents(); } /** * * The Interface Zoomable. * */ public interface Zoomable extends IToolbarDecoratedView { /** * * Zoom in. * */ void zoomIn(); /** * * Zoom out. * */ void zoomOut(); /** * * Zoom fit. * */ void zoomFit(); /** * * Locks/unlocks the view. * */ void toggleLock(); /** * * @return the controls that will react to gestures / mouse doucle-cliks * */ Control[] getZoomableControls(); /** * * @return true if the scroll triggers the zooming * */ boolean zoomWhenScrolling(); /** * Gets the camera helper. * * @return the camera helper */ default ICameraHelper getCameraHelper() { return NULL_CAMERA; } /** * * Checks for cameras. * * * * @return true, if successful * */ default boolean hasCameras() { return false; } }}
/******************************************************************************************************* * * IToolbarDecoratedView.java, in gama.ui.shared, is part of the source code of the GAMA modeling and simulation * platform (v.2024-06). * * (c) 2007-2024 UMI 209 UMMISCO IRD/SU & Partners (IRIT, MIAT, ESPACE-DEV, CTU) * * Visit https://github.com/gama-platform/gama for license information and contacts. * ********************************************************************************************************/package gama.ui.shared.views.toolbar;import java.io.IOException;import java.nio.charset.StandardCharsets;import java.nio.file.Files;import java.nio.file.Path;import java.util.Collection;import java.util.Collections;import org.eclipse.core.resources.ResourcesPlugin;import org.eclipse.swt.SWT;import org.eclipse.swt.widgets.Control;import org.eclipse.swt.widgets.FileDialog;import org.eclipse.ui.IWorkbenchSite;import gama.core.common.preferences.GamaPreferences;import gama.core.outputs.IOutput;import gama.core.runtime.GAMA;import gama.ui.shared.resources.GamaColors.GamaUIColor;import gama.ui.shared.utils.WorkbenchHelper;/** * * Class IToolbarDecoratedView. * * * * @author drogoul * * @since 7 déc. 2014 * * * */public interface IToolbarDecoratedView { /** The null camera. */ ICameraHelper NULL_CAMERA = new ICameraHelper() {}; /** * The Interface ICameraHelper. */ public interface ICameraHelper { /** * * Gets the camera names. * * * * @return the camera names * */ default Collection<String> getCameraNames() { return Collections.EMPTY_LIST; } /** * * Sets the camera name. * * * * @param p * * the new camera name * */ default void setCameraName(final String p) {} /** * * Gets the camera name. * * * * @return the camera name * */ default String getCameraName() { return GamaPreferences.Displays.OPENGL_DEFAULT_CAM.getValue(); } /** * * Checks if is camera locked. * * * * @return true, if is camera locked * */ default boolean isCameraLocked() { return false; } /** * * Toggle camera. * */ default void toggleCamera() {} /** * Gets the camera definition. * * @return the camera definition */ default String getCameraDefinition() { return ""; } } /** * * Gets the site. * * * * @return the site * */ IWorkbenchSite getSite(); /** * * Creates the tool items. * * * * @param tb * * the tb * */ void createToolItems(GamaToolbar2 tb); /** * * Adds the state listener. ✓ Unicode: U+2713, UTF-8: E2 9C 93 * * * * @param listener * * the listener * */ default void addStateListener(final StateListener listener) {} /** * * The listener interface for receiving state events. The class that is interested in processing a state event * * implements this interface, and the object created with that class is registered with a component using the * * component's <code>addStateListener<code> method. When the state event occurs, that object's appropriate method is * * invoked. * * * * @see StateEvent * */ public interface StateListener { /** * * Update to reflect state. * */ void updateToReflectState(); } /** * * The Interface Expandable. * */ public interface Expandable extends IToolbarDecoratedView { /** * * Expand all. * */ void expandAll(); /** * * Collapse all. * */ void collapseAll(); } /** * * The Interface Pausable. * */ public interface Pausable extends IToolbarDecoratedView { /** * * Pause changed. * */ void pauseChanged(); /** * * Gets the output. * * * * @return the output * */ IOutput getOutput(); } /** * * The Interface Sizable. * */ public interface Sizable extends IToolbarDecoratedView { /** * * Gets the sizable font control. * * * * @return the sizable font control * */ Control getSizableFontControl(); } /** * * The Interface Colorizable. * */ public interface Colorizable extends IToolbarDecoratedView { /** * * Gets the color labels. * * * * @return the color labels * */ String[] getColorLabels(); /** * * Gets the color. * * * * @param index * * the index * * @return the color * */ GamaUIColor getColor(int index); /** * * Sets the color. * * * * @param index * * the index * * @param c * * the c * */ void setColor(int index, GamaUIColor c); } /** * * The Interface CSVExportable. * */ public interface CSVExportable extends IToolbarDecoratedView { /** * * Save as CSV. * */ void saveAsCSV(); } /** * * The Interface LogExportable. * */ public interface LogExportable extends IToolbarDecoratedView { /** * * Save as log. * */ default void saveAsLog() { String text = getContents(); FileDialog fd = new FileDialog(WorkbenchHelper.getShell(), SWT.SAVE); fd.setText("Choose a destination file"); fd.setFilterExtensions(new String[] { "*.log" }); if (GAMA.getExperiment() != null && GAMA.getExperiment().getAgent() != null) { fd.setFilterPath(GAMA.getExperiment().getAgent().getProjectPath()); } else { fd.setFilterPath(ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString()); } String f = fd.open(); if (f == null) return; try { Files.writeString(Path.of(f), text, StandardCharsets.UTF_8); } catch (IOException e) {} } /** * * Gets the contents. * * * * @return the contents * */ String getContents(); } /** * * The Interface Zoomable. * */ public interface Zoomable extends IToolbarDecoratedView { /** * * Zoom in. * */ void zoomIn(); /** * * Zoom out. * */ void zoomOut(); /** * * Zoom fit. * */ void zoomFit(); /** * * Locks/unlocks the view. * */ void toggleLock(); /** * Checks if is locked. * * @return true, if is locked */ boolean isLocked(); /** * * @return the controls that will react to gestures / mouse doucle-cliks * */ Control[] getZoomableControls(); /** * * @return true if the scroll triggers the zooming * */ boolean zoomWhenScrolling(); /** * Gets the camera helper. * * @return the camera helper */ default ICameraHelper getCameraHelper() { return NULL_CAMERA; } /** * * Checks for cameras. * * * * @return true, if successful * */ default boolean hasCameras() { return false; } }}
Expand Down
Loading

0 comments on commit ae53f8d

Please sign in to comment.