]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.g2d/src/org/simantics/g2d/participant/PointerPainter.java
Fixed invalid comparisons which were identified by Eclipse IDE 2018-09
[simantics/platform.git] / bundles / org.simantics.g2d / src / org / simantics / g2d / participant / PointerPainter.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.g2d.participant;
13
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;
21 import java.util.Map;
22
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;
38
39 /**
40  * Paints mouse cursors in a multi-mouse setup.
41  * 
42  * @author Toni Kalajainen
43  * @author Tuukka Lehtonen
44  */
45 public class PointerPainter extends AbstractCanvasParticipant {
46
47     /**
48      * Key for the hint that determines whether cursors are painter or not.
49      */
50     public static final Key     KEY_PAINT_POINTER     = new KeyOf(Boolean.class);
51
52     public static final int     CURSOR_PAINT_Z_ORDER = 5000;
53
54     private final static Stroke CURSOR_STROKE         = new BasicStroke(1.0f);
55
56     @Dependency MouseUtil       monitor;
57
58     InstancingShapeNode         node;
59
60     Shape                       pointer;
61
62     public PointerPainter() {
63         Path2D p = new Path2D.Double();
64         p.moveTo(-10, -10);
65         p.lineTo(10, 10);
66         p.moveTo(10, -10);
67         p.lineTo(-10, 10);
68
69         this.pointer = p;
70     }
71
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);
78         node.setFill(false);
79         node.setScaleStroke(false);
80         node.setScaleShape(false);
81     }
82
83     @SGCleanup
84     public void cleanup() {
85         node.remove();
86         node = null;
87     }
88
89     public boolean isCursorEnabled() {
90         Boolean pp = getHint(KEY_PAINT_POINTER);
91         return Boolean.TRUE.equals(pp);
92     }
93
94     @EventHandler(priority = Integer.MAX_VALUE)
95     public boolean handleMouseEvent(MouseEvent e) {
96         if (!isCursorEnabled())
97             return false;
98
99         assertDependencies();
100
101         if (e instanceof MouseMovedEvent || e instanceof MouseButtonPressedEvent
102                 || e instanceof MouseButtonReleasedEvent) {
103             updateNode();
104             setDirty();
105         }
106         return false;
107     }
108
109     private void updateNode() {
110         Map<Integer, MouseInfo> miceInfo = monitor.getMiceInfo();
111
112         AffineTransform[] transforms = new AffineTransform[miceInfo.size()];
113         Paint[] paints = new Paint[miceInfo.size()];
114
115         int i = 0;
116         for (MouseInfo mi : miceInfo.values()) {
117             int color = 0;
118             if ((mi.buttons & 1) > 0) color |= 0xff;
119             if ((mi.buttons & 2) > 0) color |= 0xff00;
120             if ((mi.buttons & 4) > 0) color |= 0xff0000;
121
122             transforms[i] = AffineTransform.getTranslateInstance(mi.controlPosition.getX(), mi.controlPosition.getY());
123             paints[i] = new Color(color);
124
125             ++i;
126         }
127
128         node.setInstances(transforms, paints);
129     }
130
131 }