~workcraft/workcraft/1.0

« back to all changes in this revision

Viewing changes to workcraft/cpog/Vertex.java

  • Committer: Danil Sokolov
  • Date: 2014-02-04 17:50:42 UTC
  • mfrom: (8.1.21 CPOGM)
  • Revision ID: danilovesky@gmail.com-20140204175042-pfuu19pwbsy02xym
Merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package workcraft.cpog;
 
2
 
 
3
import java.awt.event.KeyEvent;
 
4
import java.awt.event.MouseEvent;
 
5
import java.util.HashMap;
 
6
import java.util.LinkedList;
 
7
import java.util.List;
 
8
import java.util.UUID;
 
9
 
 
10
import javax.media.opengl.GL;
 
11
 
 
12
import org.w3c.dom.Document;
 
13
import org.w3c.dom.Element;
 
14
import org.w3c.dom.NodeList;
 
15
 
 
16
import workcraft.DuplicateIdException;
 
17
import workcraft.Model;
 
18
import workcraft.UnsupportedComponentException;
 
19
import workcraft.common.DefaultConnection;
 
20
import workcraft.common.LabelledConnection;
 
21
import workcraft.editor.BasicEditable;
 
22
import workcraft.editor.EditableConnection;
 
23
import workcraft.logic.DNF;
 
24
import workcraft.logic.InvalidExpressionException;
 
25
import workcraft.util.Colorf;
 
26
import workcraft.util.Mat4x4;
 
27
import workcraft.util.Vec2;
 
28
import workcraft.visual.JOGLPainter;
 
29
import workcraft.visual.Painter;
 
30
import workcraft.visual.ShapeMode;
 
31
import workcraft.visual.TextAlign;
 
32
import workcraft.visual.VertexBuffer;
 
33
import workcraft.visual.GeometryUtil;
 
34
import workcraft.visual.VertexFormat;
 
35
import workcraft.visual.PrimitiveType;
 
36
import workcraft.visual.VertexFormatException;
 
37
 
 
38
public class Vertex extends BasicEditable {
 
39
        public static final UUID _modeluuid = UUID.fromString("25787b48-9c3d-11dc-8314-0800200c9a66");
 
40
        public static final String _displayname = "Vertex";
 
41
        public static final String _hotkey = " v ";
 
42
        public static final int _hotkeyvk = KeyEvent.VK_V;              
 
43
 
 
44
        private static Colorf vertexColor = new Colorf(1.0f, 1.0f, 1.0f, 1.0f);
 
45
        private static Colorf selectedVertexColor = new Colorf(1.0f, 0.9f, 0.9f, 1.0f);
 
46
        private static Colorf enabledVertexColor = new Colorf(0.8f, 0.8f, 1.0f, 1.0f);
 
47
        private static Colorf vertexOutlineColor = new Colorf(0.0f, 0.0f, 0.0f, 1.0f);
 
48
        private static Colorf passiveVertexOutlineColor = new Colorf(0.6f, 0.6f, 0.6f, 1.0f);
 
49
        private static Colorf selectedVertexOutlineColor = new Colorf(0.5f, 0.0f, 0.0f, 1.0f);
 
50
        private static Colorf userVertexOutlineColor = new Colorf(0.0f, 0.6f, 0.0f, 1.0f);
 
51
        private static Colorf vertexFiredColor = new Colorf(0.3f, 0.3f, 0.5f, 1.0f);
 
52
 
 
53
        private Condition condition;
 
54
        
 
55
        boolean fired = false;
 
56
        
 
57
        public boolean canFire = false;
 
58
        public boolean canWork = false;
 
59
 
 
60
        private LinkedList<Vertex> out;
 
61
        private LinkedList<Vertex> in;
 
62
        private LinkedList<ControlVariable> vars;
 
63
        
 
64
        private String rho = null;
 
65
        
 
66
        public String getRho() {
 
67
                return rho;
 
68
        }
 
69
 
 
70
        public void setRho(String rho) {
 
71
                this.rho = rho;
 
72
        }
 
73
 
 
74
        public LinkedList<Vertex> getOut()
 
75
        {
 
76
                return (LinkedList<Vertex>)out.clone();
 
77
        }
 
78
 
 
79
        public LinkedList<Vertex> getIn()
 
80
        {
 
81
                return (LinkedList<Vertex>)in.clone();
 
82
        }
 
83
 
 
84
        public LinkedList<ControlVariable> getVars()
 
85
        {
 
86
                return (LinkedList<ControlVariable>)vars.clone();
 
87
        }
 
88
 
 
89
        public void removeIn(Vertex t)
 
90
        {
 
91
                in.remove(t);
 
92
        }
 
93
 
 
94
        public void removeOut(Vertex t)
 
95
        {
 
96
                out.remove(t);
 
97
        }
 
98
        
 
99
        public void removeVar(ControlVariable t)
 
100
        {
 
101
                vars.remove(t);
 
102
        }
 
103
        
 
104
 
 
105
        public Boolean isActive()
 
106
        {
 
107
                return condition.evaluate();
 
108
        }
 
109
 
 
110
        public boolean addIn(DefaultConnection con) {
 
111
                Vertex t = (Vertex)con.getFirst();
 
112
                if (in.contains(t))
 
113
                        return false;
 
114
                in.add(t);
 
115
                connections.add(con);
 
116
                return true;
 
117
        }
 
118
 
 
119
        public boolean addOut(DefaultConnection con) {
 
120
                Vertex t = (Vertex)con.getSecond();
 
121
                if (out.contains(t))
 
122
                        return false;
 
123
                out.add(t);
 
124
                connections.add(con);
 
125
                return true;
 
126
        }
 
127
        
 
128
        public boolean addVar(DefaultConnection con)
 
129
        {
 
130
                ControlVariable t = (ControlVariable)con.getSecond();
 
131
                if (vars.contains(t))
 
132
                        return false;
 
133
                vars.add(t);
 
134
                connections.add(con);
 
135
                return true;
 
136
        }
 
137
        
 
138
        @Override
 
139
        public boolean hits(Vec2 pointInViewSpace)
 
140
        {
 
141
                Vec2 v = new Vec2(pointInViewSpace);
 
142
                transform.getViewToLocalMatrix().transform(v);
 
143
                return v.length() < 0.05f;
 
144
        }
 
145
 
 
146
        public String getCondition()
 
147
        {
 
148
                return condition.string;
 
149
        }       
 
150
 
 
151
        public void setCondition(String condition)
 
152
        {
 
153
                if (!this.condition.setCondition(condition)) return;
 
154
                
 
155
                String label = getLabel();
 
156
                if (label.lastIndexOf(": ") != -1)
 
157
                {
 
158
                        label = label.substring(0, label.lastIndexOf(": "));
 
159
                }
 
160
                if (!condition.equals("1")) label = label + ": " + condition;
 
161
                setLabel(label);
 
162
        }
 
163
 
 
164
        public Vertex(BasicEditable parent) throws UnsupportedComponentException
 
165
        {
 
166
                super(parent);
 
167
                boundingBox.setExtents(new Vec2(-0.05f, -0.05f), new Vec2(0.05f, 0.05f));
 
168
                condition = new Condition("1", (CPOGModel) ownerDocument);
 
169
                setRho("1");
 
170
                
 
171
                out = new LinkedList<Vertex>();
 
172
                in = new LinkedList<Vertex>();
 
173
                vars = new LinkedList<ControlVariable>();
 
174
        }
 
175
 
 
176
        public void refresh()
 
177
        {
 
178
                if (condition != null) condition.refresh();
 
179
        }
 
180
        
 
181
        public void draw(Painter p)
 
182
        {               
 
183
                p.setTransform(transform.getLocalToViewMatrix());
 
184
                p.setShapeMode(ShapeMode.FILL);
 
185
 
 
186
                if (selected)
 
187
                        p.setFillColor(selectedVertexOutlineColor);
 
188
                else
 
189
                        if (canWork)
 
190
                                p.setFillColor(userVertexOutlineColor);
 
191
                        else
 
192
                                if (isActive())
 
193
                                        p.setFillColor(vertexOutlineColor);
 
194
                                else
 
195
                                        p.setFillColor(passiveVertexOutlineColor);
 
196
 
 
197
                p.drawCircle(0.05f, null);
 
198
 
 
199
                if (selected)
 
200
                        p.setFillColor(selectedVertexColor);
 
201
                else
 
202
                        if (canFire)
 
203
                                p.setFillColor(enabledVertexColor);
 
204
                        else
 
205
                                if (fired)
 
206
                                        p.setFillColor(vertexFiredColor);
 
207
                                else
 
208
                                p.setFillColor(vertexColor);
 
209
                
 
210
                p.drawCircle(0.04f, null);
 
211
                
 
212
                super.draw(p);
 
213
        }
 
214
 
 
215
        @Override
 
216
        public BasicEditable getChildAt(Vec2 point) {
 
217
                // TODO Auto-generated method stub
 
218
                return null;
 
219
        }
 
220
 
 
221
        @Override
 
222
        public void update(Mat4x4 matView) {
 
223
                // TODO Auto-generated method stub
 
224
 
 
225
        }
 
226
 
 
227
        public String getControlledSet()
 
228
        {
 
229
                String res = "";
 
230
                for(ControlVariable v : vars)
 
231
                if (res.length() > 0) res = res + ", " + v.getId(); else res = v.getId();
 
232
                return res;
 
233
        }
 
234
        
 
235
        public List<String> getEditableProperties() {
 
236
                List<String> list = super.getEditableProperties();
 
237
                list.add("str,Condition,getCondition,setCondition");
 
238
                list.add("bool,Active,isActive,-");
 
239
                list.add("str,Restriction function,getRho,setRho");
 
240
                list.add("str,Controlled set,getControlledSet,-");
 
241
                return list;
 
242
        }
 
243
 
 
244
 
 
245
        public void fromXmlDom(Element element) throws DuplicateIdException {
 
246
                NodeList nl = element.getElementsByTagName("vertex");
 
247
                Element ne = (Element) nl.item(0);
 
248
                setCondition(ne.getAttribute("condition"));
 
249
                setRho(ne.getAttribute("rho"));
 
250
                super.fromXmlDom(element);
 
251
        }
 
252
        
 
253
        public Element toXmlDom(Element parent_element) {
 
254
                Element ee = super.toXmlDom(parent_element);
 
255
                Document d = ee.getOwnerDocument();
 
256
                Element ppe = d.createElement("vertex");
 
257
                ppe.setAttribute("condition", getCondition());
 
258
                ppe.setAttribute("rho", getRho());
 
259
                ee.appendChild(ppe);
 
260
                return ee;
 
261
        }
 
262
                        
 
263
        public void simAction(int flag)
 
264
        {
 
265
                if (flag == MouseEvent.BUTTON1 && !fired && isActive())
 
266
                {
 
267
                        canWork = !canWork;
 
268
                }
 
269
        }
 
270
 
 
271
        public void fire()
 
272
        {
 
273
                fired = true;
 
274
                for(ControlVariable v : vars) v.setCurrentValue(v.getFinalValue());
 
275
        }
 
276
 
 
277
}
 
 
b'\\ No newline at end of file'