]> gerrit.simantics Code Review - simantics/3d.git/blob - dev/org.simantics.proconf.g3d/src/org/simantics/proconf/g3d/gizmo/MultiSelectionGizmo.java
Release
[simantics/3d.git] / dev / org.simantics.proconf.g3d / src / org / simantics / proconf / g3d / gizmo / MultiSelectionGizmo.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.proconf.g3d.gizmo;\r
12 \r
13 import java.nio.FloatBuffer;\r
14 \r
15 import javax.vecmath.Color4f;\r
16 \r
17 import org.simantics.proconf.g3d.base.VecmathJmeTools;\r
18 \r
19 import com.jme.renderer.ColorRGBA;\r
20 import com.jme.scene.TriMesh;\r
21 import com.jme.util.geom.BufferUtils;\r
22 \r
23 \r
24 \r
25 public abstract class MultiSelectionGizmo extends AbstractGizmo {\r
26     \r
27     public static final int UNSELECTED = 0;\r
28     public static final int SELECTED = 1;\r
29 \r
30         private TriMesh geoms[] = new TriMesh[4];\r
31         private ColorRGBA colors[][] = new ColorRGBA[4][2];\r
32    \r
33 \r
34     private boolean selected[];\r
35     \r
36     \r
37     private int selectedIndex = -1;\r
38     \r
39     \r
40     public MultiSelectionGizmo() {\r
41         super();\r
42 \r
43         colors = new ColorRGBA[getCount()][2];\r
44         geoms = new TriMesh[getCount()];\r
45         selected = new boolean[getCount()];\r
46         for (int i = 0; i < selected.length; i++) {\r
47             selected[i] = false;\r
48         }\r
49     }\r
50     \r
51     protected abstract int getCount();\r
52     protected abstract int getIndexForName(String name);\r
53     \r
54     public int getSelected() {\r
55         return selectedIndex;\r
56     }\r
57     \r
58     private void updateColor(int index, int selected) {\r
59 \r
60         FloatBuffer buff = geoms[index].getColorBuffer(0);\r
61         for (int i = 0; i < geoms[index].getBatch(0).getVertexCount(); i++) {\r
62                 BufferUtils.setInBuffer(colors[index][selected], buff, i);\r
63         }\r
64     }\r
65     \r
66     /* (non-Javadoc)\r
67      * @see fi.vtt.proconf.shapeeditor.common.Gizmo#changeSelected(java.lang.String)\r
68      */\r
69     public void setSelected(String name) {\r
70         \r
71         if (name == null) {\r
72             for (int j = 0; j < getCount(); j++) {\r
73                 if (selected[j]) {\r
74                     selected[j] = false;\r
75                     updateColor(j,UNSELECTED);\r
76                     setChanged(true);\r
77                 }\r
78             }\r
79             selectedIndex = -1;\r
80             return;\r
81         }\r
82         int index = getIndexForName(name);\r
83         if (index == -1)\r
84             return;\r
85         selectedIndex = index;\r
86         if (!selected[index]) {\r
87             selected[index] = true;\r
88             updateColor(index,SELECTED);\r
89             setChanged(true);\r
90         }\r
91         for (int j = 0; j < getCount(); j++) {\r
92             if (j != index) {\r
93                 if (selected[j]) {\r
94                     selected[j] = false;\r
95                     updateColor(j,UNSELECTED);\r
96                     setChanged(true);\r
97                 }\r
98             }\r
99         } \r
100     }\r
101     \r
102     \r
103     protected void setGeometry(int index, TriMesh geom) {\r
104         geoms[index] = geom;\r
105     }\r
106     \r
107     protected void setColor(int index, int selected, Color4f color) {\r
108         colors[index][selected] = VecmathJmeTools.get(color);\r
109     }\r
110 }\r