/******************************************************************************* * Copyright (c) 2012 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.maps.eclipse; import org.simantics.g2d.canvas.Hints; import org.simantics.g2d.canvas.ICanvasContext; import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant; import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup; import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; import org.simantics.maps.sg.MapNode; import org.simantics.maps.sg.MapScaleNode; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; import org.simantics.scenegraph.g2d.events.command.CommandEvent; import org.simantics.scenegraph.g2d.events.command.Commands; import org.simantics.utils.datastructures.hints.HintListenerAdapter; import org.simantics.utils.datastructures.hints.IHintListener; import org.simantics.utils.datastructures.hints.IHintObservable; import org.simantics.utils.datastructures.hints.IHintContext.Key; import org.simantics.utils.datastructures.hints.IHintContext.KeyOf; /** * MapPainter is an ICanvasContext participant that uses the scene graph * {@link MapNode} to draw tiled maps in the background of the canvas. * * @author J-P Laine * * @see MapNode */ public class MapPainter extends AbstractCanvasParticipant { /** * Grid enabled status. Default value is True */ public static final Key KEY_MAP_ENABLED = new KeyOf(Boolean.class); public static final double ZOOM_IN_LIMIT = 10000000.0; public static final double ZOOM_OUT_LIMIT = 10.0; IHintListener mapListener = new HintListenerAdapter() { public void hintChanged(IHintObservable sender, Key key, Object oldValue, Object newValue) { ICanvasContext cc = getContext(); if (cc != null) { updateNode(); cc.getContentContext().setDirty(); } } }; protected MapNode node = null; protected MapScaleNode scaleNode = null; private int scale; public MapPainter(int scale) { this.scale = scale; } @Override public void addedToContext(ICanvasContext ctx) { super.addedToContext(ctx); getHintStack().addKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener); } @Override public void removedFromContext(ICanvasContext ctx) { getHintStack().removeKeyHintListener(getThread(), KEY_MAP_ENABLED, mapListener); super.removedFromContext(ctx); } @EventHandler(priority = 0) public boolean handleKeyEvent(CommandEvent e) { if (e.command.equals( Commands.MAP_ENABLE )) { setEnabled(true); updateNode(); setDirty(); return true; } else if (e.command.equals( Commands.MAP_DISABLE )) { setEnabled(false); updateNode(); setDirty(); return true; } else if (e.command.equals( Commands.MAP_TOGGLE )) { setEnabled(!isMapEnabled()); updateNode(); setDirty(); return true; } return false; } @SGInit public void initSG(G2DParentNode parent) { node = parent.addNode("map", MapNode.class); node.setScale(scale); node.setEnabled(true); node.setZIndex(Integer.MIN_VALUE + 999); // Just under the grid scaleNode = parent.addNode("mapScale", MapScaleNode.class); scaleNode.setScale(scale); scaleNode.setEnabled(true); scaleNode.setZIndex(Integer.MAX_VALUE - 999); // Just under the grid } @SGCleanup public void cleanupSG() { node.remove(); } protected void updateNode() { node.setEnabled(isPaintingEnabled()); } boolean isPaintingEnabled() { boolean enabled = isMapEnabled(); Boolean globalDisable = getHint(Hints.KEY_DISABLE_PAINTING); return enabled && !Boolean.TRUE.equals(globalDisable); } public boolean isMapEnabled() { Boolean enabled = getHint(KEY_MAP_ENABLED); return !Boolean.FALSE.equals(enabled); } public void setEnabled(boolean enabled) { setHint(KEY_MAP_ENABLED, enabled); } }