1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g2d.participant;
14 import java.awt.BasicStroke;
15 import java.awt.Color;
16 import java.awt.Paint;
17 import java.awt.Shape;
18 import java.awt.Stroke;
19 import java.awt.geom.AffineTransform;
20 import java.awt.geom.Path2D;
23 import org.simantics.g2d.canvas.SGDesignation;
24 import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant;
25 import org.simantics.g2d.canvas.impl.DependencyReflection.Dependency;
26 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup;
27 import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit;
28 import org.simantics.g2d.participant.MouseUtil.MouseInfo;
29 import org.simantics.scenegraph.g2d.G2DParentNode;
30 import org.simantics.scenegraph.g2d.events.MouseEvent;
31 import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler;
32 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;
33 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;
34 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
35 import org.simantics.scenegraph.g2d.nodes.InstancingShapeNode;
36 import org.simantics.utils.datastructures.hints.IHintContext.Key;
37 import org.simantics.utils.datastructures.hints.IHintContext.KeyOf;
40 * Paints mouse cursors in a multi-mouse setup.
42 * @author Toni Kalajainen
43 * @author Tuukka Lehtonen
45 public class PointerPainter extends AbstractCanvasParticipant {
48 * Key for the hint that determines whether cursors are painter or not.
50 public static final Key KEY_PAINT_POINTER = new KeyOf(Boolean.class);
52 public static final int CURSOR_PAINT_Z_ORDER = 5000;
54 private final static Stroke CURSOR_STROKE = new BasicStroke(1.0f);
56 @Dependency MouseUtil monitor;
58 InstancingShapeNode node;
62 public PointerPainter() {
63 Path2D p = new Path2D.Double();
72 @SGInit(designation = SGDesignation.CONTROL)
73 public void init(G2DParentNode parent) {
74 node = parent.addNode("pointers", InstancingShapeNode.class);
75 node.setZIndex(CURSOR_PAINT_Z_ORDER);
76 node.setShape(pointer);
77 node.setStroke(CURSOR_STROKE);
79 node.setScaleStroke(false);
80 node.setScaleShape(false);
84 public void cleanup() {
89 public boolean isCursorEnabled() {
90 Boolean pp = getHint(KEY_PAINT_POINTER);
91 return Boolean.TRUE.equals(pp);
94 @EventHandler(priority = Integer.MAX_VALUE)
95 public boolean handleMouseEvent(MouseEvent e) {
96 if (!isCursorEnabled())
101 if (e instanceof MouseMovedEvent || e instanceof MouseButtonPressedEvent
102 || e instanceof MouseButtonReleasedEvent) {
109 private void updateNode() {
110 Map<Integer, MouseInfo> miceInfo = monitor.getMiceInfo();
112 AffineTransform[] transforms = new AffineTransform[miceInfo.size()];
113 Paint[] paints = new Paint[miceInfo.size()];
116 for (MouseInfo mi : miceInfo.values()) {
118 if ((mi.buttons & 1) > 0) color |= 0xff;
119 if ((mi.buttons & 2) > 0) color |= 0xff00;
120 if ((mi.buttons & 4) > 0) color |= 0xff0000;
122 transforms[i] = AffineTransform.getTranslateInstance(mi.controlPosition.getX(), mi.controlPosition.getY());
123 paints[i] = new Color(color);
128 node.setInstances(transforms, paints);