]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/profile/ButtonNode.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / profile / ButtonNode.java
1 package org.simantics.diagram.profile;\r
2 \r
3 import java.awt.Graphics2D;\r
4 import java.awt.geom.AffineTransform;\r
5 import java.awt.geom.Point2D;\r
6 import java.awt.geom.Rectangle2D;\r
7 \r
8 import org.simantics.datatypes.literal.RGB;\r
9 import org.simantics.datatypes.literal.Vec2d;\r
10 import org.simantics.diagram.elements.DiagramNodeUtil;\r
11 import org.simantics.diagram.internal.Activator;\r
12 import org.simantics.diagram.profile.ButtonResult.A;\r
13 import org.simantics.diagram.profile.ButtonResult.B;\r
14 import org.simantics.scenegraph.g2d.G2DNode;\r
15 import org.simantics.scenegraph.g2d.events.EventTypes;\r
16 import org.simantics.scenegraph.g2d.events.MouseEvent;\r
17 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonPressedEvent;\r
18 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseButtonReleasedEvent;\r
19 import org.simantics.scenegraph.g2d.nodes.SVGNode;\r
20 import org.simantics.scenegraph.utils.NodeUtil;\r
21 import org.simantics.scl.runtime.function.Function1;\r
22 \r
23 public class ButtonNode extends IconButtonNode {\r
24         \r
25         private static final long serialVersionUID = -1963362069190362275L;\r
26         \r
27         private B config;\r
28         private boolean isDown = false;\r
29         private Function1<Boolean, Boolean> pressed;\r
30         \r
31         private static SVGNode ON = new SVGNode();\r
32         private static SVGNode OFF = new SVGNode();\r
33         private static boolean initialized = false;\r
34         \r
35     Rectangle2D EMPTY = new Rectangle2D.Double(0, 0, 0, 0);\r
36         \r
37         private SVGNode current = OFF;\r
38         private Rectangle2D lastBounds = EMPTY; \r
39 \r
40         public void staticInit() {\r
41                 if(!initialized) {\r
42                         ON.setData(createSVG("BUTTON_ON"));\r
43                         OFF.setData(createSVG("BUTTON_OFF"));\r
44                         initialized = true;\r
45                 }\r
46         }\r
47         \r
48         @Override\r
49         public void init() {\r
50                 staticInit();\r
51         addEventHandler(this);\r
52         }\r
53         \r
54     @Override\r
55     public void cleanup() {\r
56         removeEventHandler(this);\r
57         super.cleanup();\r
58     }\r
59         \r
60         private void setValue(double value) {\r
61                 \r
62                 // Value does not affect LATCH buttons\r
63                 if(ButtonMode.LATCH.equals(config.mode)) return;\r
64                 \r
65                 if(value < 0.5) {\r
66                         current = OFF;\r
67                 } else {\r
68                         current = ON;\r
69                 } \r
70                 \r
71         }\r
72         \r
73         private void setConfig(B config) {\r
74 \r
75                 if(this.config == config) return;\r
76         this.config = config;\r
77                 \r
78         }\r
79         \r
80         public void setA(A data) {\r
81 \r
82                 setConfig(data.config);\r
83         setValue(data.value);\r
84                 \r
85         }\r
86         \r
87     @Override\r
88     public void render(Graphics2D g2d) {\r
89 \r
90                 AffineTransform ot = null;\r
91                 if (!transform.isIdentity()) {\r
92                         ot = g2d.getTransform();\r
93                         g2d.transform(transform);\r
94                 }\r
95         \r
96                 current.render(g2d);\r
97                 lastBounds = current.getBounds();\r
98 \r
99                 if (ot != null)\r
100                         g2d.setTransform(ot);\r
101         \r
102     }\r
103 \r
104         @Override\r
105         public Rectangle2D getBoundsInLocal() {\r
106                 return lastBounds;\r
107         }\r
108 \r
109         @Override\r
110         void setData(IconButtonResult state) {\r
111                 ButtonResult br = (ButtonResult)state;\r
112                 setA(br.getA());\r
113                 pressed = br.getPressed();\r
114         }\r
115 \r
116     private boolean hitTest(MouseEvent event) {\r
117         Rectangle2D bounds = getBounds();\r
118         if (bounds == null)\r
119             return false;\r
120         Point2D localPos = NodeUtil.worldToLocal(this, event.controlPosition, new Point2D.Double());\r
121         double x = localPos.getX();\r
122         double y = localPos.getY();\r
123         return bounds.contains(x, y);\r
124     }\r
125         \r
126         @Override\r
127     protected boolean mouseButtonPressed(MouseButtonPressedEvent e) {\r
128                 if(!e.hasAnyButton(MouseEvent.LEFT_BUTTON)) return false;\r
129                 if(!hitTest(e)) return false; \r
130                 if(pressed != null) {\r
131                         pressed.apply(true);\r
132                         isDown = true;\r
133                         if(ButtonMode.LATCH.equals(config.mode)) {\r
134                                 current = ON;\r
135                                 DiagramNodeUtil.getCanvasContext((G2DNode)this).getContentContext().setDirty();\r
136                         }\r
137                 }\r
138         return true;\r
139     }\r
140 \r
141         @Override\r
142     protected boolean mouseButtonReleased(MouseButtonReleasedEvent e) {\r
143                 if(e.hasAnyButton(MouseEvent.LEFT_BUTTON)) return false;\r
144                 if(!hitTest(e) && !isDown) return false; \r
145                 if(pressed != null) {\r
146                         pressed.apply(false);\r
147                         isDown = false;\r
148                         if(ButtonMode.LATCH.equals(config.mode)) {\r
149                                 current = OFF;\r
150                                 DiagramNodeUtil.getCanvasContext((G2DNode)this).getContentContext().setDirty();\r
151                         }\r
152                 }\r
153         return true;\r
154     }\r
155         \r
156     @Override\r
157     public int getEventMask() {\r
158         return EventTypes.MouseButtonPressedMask\r
159                 | EventTypes.MouseButtonReleasedMask\r
160                 ;\r
161     }   \r
162     \r
163     public String createSVG(String iconName) {\r
164         return Activator.ICON_PROVIDER.apply(iconName, new RGB.Integer(0,  0, 0), new Vec2d(1,1));\r
165     }\r
166     \r
167 }