public CompositionTemplate() { references = new ReferenceDataComponent(); var nameText = new BasicText(); nameText.position = new Vector2(5, 5); Register(references); var tracker = new PointCollectionTracker <Reference>(references, x => { return((TemplateManager.LOADED_TEMPLATES[x.name].GetBoundingBox().Min + TemplateManager.LOADED_TEMPLATES[x.name].GetBoundingBox().Max) / 2 + x.position); }); selector = new Selector <Reference>(new CameraSelectionProvider <Reference>(tracker), x => { referenceOutlines[x].boxColor = Color.Orange; nameText.text = GetSelectedText(selector.selected); }, x => { referenceOutlines[x].boxColor = Color.White; nameText.text = GetSelectedText(selector.selected); }); Register(selector, nameText); RegisterListener(new InputListener(Trigger.E, x => { if (selector.selected.Count != 1) { return; } editMode = true; editingItem = TemplateManager.LOADED_TEMPLATES[selector.selected.Single().name]; editingItem.Focus(); var listener = new InputListener(Trigger.Escape, y => { editingItem.UnregisterListener(y); this.Focus(); RecalculateBoundingBoxes(); editingItem = null; editMode = false; }); editingItem.RegisterListener(listener); })); }
// TODO: somehow we have orphan vertices public MeshTemplate() { vertexData = new VertexDataComponent() { saveColor = false }; lineData = new LineDataComponent() { vertexData = vertexData }; polyData = new VertexListHashDataComponent() { vertexData = vertexData }; polyData.AddObserver(this); faceMesh = new FaceMesh() { vertexData = vertexData }; lineMesh = new LineMesh() { vertexData = vertexData }; pointMesh = new PointMesh() { vertexData = vertexData }; var tracker = new PointCollectionTracker <VertexData>(vertexData, x => x.position); Register(vertexData, lineData, polyData, faceMesh, lineMesh, pointMesh); // setup ui // TODO: these all need to be in reversible actions var selector = new Selector <VertexData>(new CameraSelectionProvider <VertexData>(tracker), x => { x.color = Color.Orange; RecalculateEverything(); }, x => { x.color = Color.Black; RecalculateEverything(); } ); Register(selector); CameraMouseTracker dragMouseTracker = new CameraMouseTracker() { stepSize = 0.25f }; // TODO: maybe make event handlers so you can do += stuff... dragMouseTracker.OnStepDiff = x => { foreach (var s in selector.selected) { s.position += x; } RecalculateEverything(); }; StateSwitcher switcher = new StateSwitcher(this); switcher.AddKeyFocus(Trigger.G, dragMouseTracker, () => { // translate vertices Vector3 selectedSum = Vector3.Zero; foreach (var s in selector.selected) { selectedSum += s.position; } dragMouseTracker.worldOrigin = selectedSum / selector.selected.Count; dragMouseTracker.mouseOrigin = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); dragMouseTracker.oldOffset = null; }, true); switcher.AddKeyFocus(Trigger.E, dragMouseTracker, () => { // extrude Vector3 selectedSum = Vector3.Zero; foreach (var s in selector.selected) { selectedSum += s.position; } dragMouseTracker.worldOrigin = selectedSum / selector.selected.Count; dragMouseTracker.mouseOrigin = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); dragMouseTracker.oldOffset = null; var selectedPolys = GetSelectedPolys(selector); var clonedPolys = ClonePolys(selectedPolys); foreach (var p in clonedPolys) { polyData.Add(p); } SetSelected(selector, clonedPolys); var perim1 = GetPerimeterPieces(selectedPolys); var perim2 = GetPerimeterPieces(clonedPolys); for (int i = 0; i < perim1.Count; i++) { polyData.Add(new VertexData[] { perim1[i][1], perim2[i][1], perim2[i][0], perim1[i][0] }); } foreach (var p in selectedPolys) { polyData.Remove(p); } }, true); switcher.AddKeyFocus(Trigger.ShiftA, dragMouseTracker, () => { // add a new unit plane and drag var v1 = new VertexData(new Vector3(0, 0, 0), Color.Black); var v2 = new VertexData(new Vector3(1, 0, 0), Color.Black); var v3 = new VertexData(new Vector3(1, 0, 1), Color.Black); var v4 = new VertexData(new Vector3(0, 0, 1), Color.Black); vertexData.AddRange(new[] { v1, v2, v3, v4 }); var newPoly = new VertexData[] { v1, v2, v3, v4 }; polyData.Add(newPoly); SetSelected(selector, new List <VertexData[]>() { newPoly }); dragMouseTracker.worldOrigin = new Vector3(0.5f, 0, 0.5f); dragMouseTracker.mouseOrigin = new Vector2(Mouse.GetState().X, Mouse.GetState().Y); dragMouseTracker.oldOffset = null; var selectedPolys = GetSelectedPolys(selector); }, true); Register(switcher); RegisterListener(new InputListener(Trigger.Delete, x => { var selectedPolys = GetSelectedPolys(selector); foreach (var p in selectedPolys) { polyData.Remove(p); } })); RegisterListener(new InputListener(Trigger.F, x => { var selectedLines = GetSelectedLines(selector); VertexData[] newPoly = new VertexData[selector.selected.Count]; newPoly[0] = selectedLines[0][1]; newPoly[1] = selectedLines[0][0]; List <VertexData> remaining = selector.selected.ToList(); remaining.Remove(newPoly[0]); remaining.Remove(newPoly[1]); for (int i = 2; i < newPoly.Length; i++) { VertexData best = null; double bestValue = -100; foreach (var r in remaining) { Vector3 v1 = newPoly[i - 1].position - newPoly[i - 2].position; Vector3 v2 = r.position - newPoly[i - 1].position; v1.Normalize(); v2.Normalize(); double thisValue = Vector3.Dot(v1, v2); if (thisValue > bestValue) { bestValue = thisValue; best = r; } } remaining.Remove(best); newPoly[i] = best; } polyData.Add(newPoly); })); Register(new PlaneGrid()); }