--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.g2d.participant;\r
+\r
+import java.awt.BasicStroke;\r
+import java.awt.Color;\r
+import java.awt.Paint;\r
+import java.awt.Shape;\r
+import java.awt.Stroke;\r
+import java.awt.geom.AffineTransform;\r
+import java.awt.geom.Path2D;\r
+import java.util.Map;\r
+\r
+import org.simantics.g2d.canvas.SGDesignation;\r
+import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;\r
+import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;\r
+import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;\r
+import org.simantics.g2d.participant.MouseUtil.MouseInfo;\r
+import org.simantics.scenegraph.g2d.G2DParentNode;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent;\r
+import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
+import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;\r
+import org.simantics.scenegraph.g2d.nodes.InstancingShapeNode;\r
+import org.simantics.utils.datastructures.hints.IHintContext.Key;\r
+import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;\r
+\r
+/**\r
+ * Paints mouse cursors in a multi-mouse setup.\r
+ * \r
+ * @author Toni Kalajainen\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class PointerPainter extends AbstractCanvasParticipant {\r
+\r
+ /**\r
+ * Key for the hint that determines whether cursors are painter or not.\r
+ */\r
+ public static final Key KEY_PAINT_POINTER = new KeyOf(Boolean.class);\r
+\r
+ public static final int CURSOR_PAINT_Z_ORDER = 5000;\r
+\r
+ private final static Stroke CURSOR_STROKE = new BasicStroke(1.0f);\r
+\r
+ @Dependency MouseUtil monitor;\r
+\r
+ InstancingShapeNode node;\r
+\r
+ Shape pointer;\r
+\r
+ public PointerPainter() {\r
+ Path2D p = new Path2D.Double();\r
+ p.moveTo(-10, -10);\r
+ p.lineTo(10, 10);\r
+ p.moveTo(10, -10);\r
+ p.lineTo(-10, 10);\r
+\r
+ this.pointer = p;\r
+ }\r
+\r
+ @SGInit(designation = SGDesignation.CONTROL)\r
+ public void init(G2DParentNode parent) {\r
+ node = parent.addNode("pointers", InstancingShapeNode.class);\r
+ node.setZIndex(CURSOR_PAINT_Z_ORDER);\r
+ node.setShape(pointer);\r
+ node.setStroke(CURSOR_STROKE);\r
+ node.setFill(false);\r
+ node.setScaleStroke(false);\r
+ node.setScaleShape(false);\r
+ }\r
+\r
+ @SGCleanup\r
+ public void cleanup() {\r
+ node.remove();\r
+ node = null;\r
+ }\r
+\r
+ public boolean isCursorEnabled() {\r
+ Boolean pp = getHint(KEY_PAINT_POINTER);\r
+ return Boolean.TRUE.equals(pp);\r
+ }\r
+\r
+ @EventHandler(priority = Integer.MAX_VALUE)\r
+ public boolean handleMouseEvent(MouseEvent e) {\r
+ if (!isCursorEnabled())\r
+ return false;\r
+\r
+ assertDependencies();\r
+\r
+ if (e instanceof MouseMovedEvent || e instanceof MouseButtonPressedEvent\r
+ || e instanceof MouseButtonReleasedEvent) {\r
+ updateNode();\r
+ setDirty();\r
+ }\r
+ return false;\r
+ }\r
+\r
+ private void updateNode() {\r
+ Map<Integer, MouseInfo> miceInfo = monitor.getMiceInfo();\r
+\r
+ AffineTransform[] transforms = new AffineTransform[miceInfo.size()];\r
+ Paint[] paints = new Paint[miceInfo.size()];\r
+\r
+ int i = 0;\r
+ for (MouseInfo mi : miceInfo.values()) {\r
+ int color = 0;\r
+ if ((mi.buttons & 1) > 0) color |= 0xff;\r
+ if ((mi.buttons & 2) > 0) color |= 0xff00;\r
+ if ((mi.buttons & 4) > 0) color |= 0xff0000;\r
+\r
+ transforms[i] = AffineTransform.getTranslateInstance(mi.controlPosition.getX(), mi.controlPosition.getY());\r
+ paints[i] = new Color(color);\r
+\r
+ ++i;\r
+ }\r
+\r
+ node.setInstances(transforms, paints);\r
+ }\r
+\r
+}\r