private WUndoCommand CreateUndoActionForGizmo(bool isDone) { WUndoCommand undoAction = null; WActorNode[] actors = new WActorNode[m_selectionList.Count]; for (int i = 0; i < m_selectionList.Count; i++) { actors[i] = m_selectionList[i]; } switch (m_transformGizmo.Mode) { case FTransformMode.Translation: undoAction = new WTranslateActorAction(actors, this, m_transformGizmo.DeltaTranslation, m_transformGizmo.TransformSpace, isDone); break; case FTransformMode.Rotation: undoAction = new WRotateActorAction(actors, this, m_transformGizmo.DeltaRotation, m_transformGizmo.TransformSpace, isDone); break; case FTransformMode.Scale: undoAction = new WScaleActorAction(actors, this, m_transformGizmo.DeltaScale, isDone); Console.WriteLine(m_transformGizmo.DeltaScale); break; default: break; } return(undoAction); }
private WActorNode Raycast(FRay ray) { if (m_world.Map == null) { return(null); } WActorNode closestResult = null; float closestDistance = float.MaxValue; foreach (var scene in m_world.Map.SceneList) { List <WActorNode> allActors = scene.GetChildrenOfType <WActorNode>(); foreach (WActorNode actorNode in allActors) { float intersectDistance; bool hitActor = actorNode.Raycast(ray, out intersectDistance); if (hitActor) { if (intersectDistance >= 0 && intersectDistance < closestDistance) { closestDistance = intersectDistance; closestResult = actorNode; } } } } return(closestResult); }
private void CheckForObjectSelectionChange(WSceneView view) { // If we have a gizmo and we're transforming it, don't check for selection change. if (m_transformGizmo != null && m_transformGizmo.IsTransforming) { return; } if (WInput.GetMouseButtonDown(0) && !WInput.GetMouseButton(1)) { FRay mouseRay = view.ProjectScreenToWorld(WInput.MousePosition); WActorNode addedActor = Raycast(mouseRay); // Check the behaviour of this click to determine appropriate selection modification behaviour. // Click w/o Modifiers = Clear Selection, add result to selection // Click /w Ctrl = Toggle Selection State // Click /w Shift = Add to Selection bool ctrlPressed = WInput.GetKey(Key.LeftCtrl) || WInput.GetKey(Key.RightCtrl); bool shiftPressed = WInput.GetKey(Key.LeftShift) || WInput.GetKey(Key.RightShift); if (!ctrlPressed & !shiftPressed) { ModifySelection(SelectionType.Add, addedActor, true); //m_selectionList.Clear(); //if (addedActor != null) m_selectionList.Add(addedActor); } else if (addedActor != null && (ctrlPressed && !shiftPressed)) { if (m_selectionList.Contains(addedActor)) { ModifySelection(SelectionType.Remove, addedActor, false); } //m_selectionList.Remove(addedActor); else { ModifySelection(SelectionType.Add, addedActor, false); } //m_selectionList.Add(addedActor); } else if (addedActor != null && shiftPressed) { if (!m_selectionList.Contains(addedActor)) { ModifySelection(SelectionType.Add, addedActor, false); } //m_selectionList.Add(addedActor); } if (m_transformGizmo != null && m_selectionList.Count > 0) { m_transformGizmo.SetPosition(m_selectionList[0].Transform.Position); m_transformGizmo.SetLocalRotation(m_selectionList[0].Transform.Rotation); } } }
private void PasteSelection() { throw new NotImplementedException(); BindingList <WActorNode> serializedObjects = AttemptToDeserializeObjectsFromClipboard(); if (serializedObjects == null) { return; } WActorNode[] actorRange = new WActorNode[serializedObjects.Count]; serializedObjects.CopyTo(actorRange, 0); ModifySelection(SelectionType.Add, actorRange, true); //m_selectionList.Clear(); foreach (var item in serializedObjects) { //m_world.FocusedScene.RegisterObject(item); //m_selectionList.Add(item); } }
private void ModifySelection(SelectionType action, WActorNode[] actors, bool clearSelection) { // Cache the current selection list. WActorNode[] currentSelection = new WActorNode[m_selectionList.Count]; m_selectionList.CopyTo(currentSelection, 0); List <WActorNode> newSelection = new List <WActorNode>(currentSelection); // Now build us a new array depending on the action. if (clearSelection) { newSelection.Clear(); } if (action == SelectionType.Add) { for (int i = 0; i < actors.Length; i++) { if (actors[i] != null) { newSelection.Add(actors[i]); } } } else if (action == SelectionType.Remove) { for (int i = 0; i < actors.Length; i++) { if (actors[i] != null) { newSelection.Remove(actors[i]); } } } WSelectionChangedAction selectionAction = new WSelectionChangedAction(this, currentSelection, newSelection.ToArray(), m_selectionList); m_world.UndoStack.Push(selectionAction); }
public void ExportToStream(EndianBinaryWriter writer, WScene scene) { // Build a dictionary which lists unique FourCC's and a list of all relevant actors. var actorCategories = new Dictionary <string, List <WActorNode> >(); foreach (var child in scene) { WActorNode actor = child as WActorNode; if (actor != null) { string fixedFourCC = ChunkHeader.LayerToFourCC(actor.FourCC, actor.Layer); if (!actorCategories.ContainsKey(fixedFourCC)) { actorCategories[fixedFourCC] = new List <WActorNode>(); } actorCategories[fixedFourCC].Add(actor); } } // Create a chunk header for each one. var chunkHeaders = new List <ChunkHeader>(); foreach (var kvp in actorCategories) { ChunkHeader header = new ChunkHeader(); header.FourCC = kvp.Key; header.ElementCount = kvp.Value.Count; chunkHeaders.Add(header); } long chunkStart = writer.BaseStream.Position; // Write the Header writer.Write(chunkHeaders.Count); for (int i = 0; i < chunkHeaders.Count; i++) { writer.Write((int)0); // Dummy Placeholder values for the Chunk Header. writer.Write((int)0); writer.Write((int)0); } // For each chunk, write the data for that chunk. Before writing the data, get the current offset and update the header. List <WActorNode>[] dictionaryData = new List <WActorNode> [actorCategories.Count]; actorCategories.Values.CopyTo(dictionaryData, 0); for (int i = 0; i < chunkHeaders.Count; i++) { ChunkHeader header = chunkHeaders[i]; chunkHeaders[i] = new ChunkHeader(header.FourCC, header.ElementCount, (int)(writer.BaseStream.Position - chunkStart)); List <WActorNode> actors = dictionaryData[i]; foreach (var actor in actors) { MapActorDescriptor template = m_sActorDescriptors.Find(x => x.FourCC == actor.FourCC); if (template == null) { Console.WriteLine("Unsupported FourCC (\"{0}\") for exporting!", actor.FourCC); continue; } WriteActorToChunk(actor, template, writer); } } // Now that we've written every actor to file we can go back and re-write the headers now that we know their offsets. writer.BaseStream.Position = chunkStart + 0x4; // 0x4 is the offset to the Chunk Headers foreach (var header in chunkHeaders) { writer.WriteFixedString(header.FourCC, 4); // FourCC writer.Write(header.ElementCount); // Number of Entries writer.Write(header.ChunkOffset); // Offset from start of file. } // Seek to the end of the file, and then pad us to 32-byte alignment. writer.BaseStream.Seek(0, SeekOrigin.End); int delta = WMath.Pad32Delta(writer.BaseStream.Position); for (int i = 0; i < delta; i++) { writer.Write((byte)0xFF); } }
private void WriteActorToChunk(WActorNode actor, MapActorDescriptor template, EndianBinaryWriter writer) { // Just convert their rotation to Euler Angles now instead of doing it in parts later. Vector3 eulerRot = actor.Transform.Rotation.ToEulerAngles(); foreach (var field in template.Fields) { IPropertyValue propValue = actor.Properties.Find(x => x.Name == field.FieldName); if (field.FieldName == "Position") { propValue = new TVector3PropertyValue(actor.Transform.Position, "Position"); } else if (field.FieldName == "X Rotation") { short xRotShort = WMath.RotationFloatToShort(eulerRot.X); propValue = new TShortPropertyValue(xRotShort, "X Rotation"); } else if (field.FieldName == "Y Rotation") { short yRotShort = WMath.RotationFloatToShort(eulerRot.Y); propValue = new TShortPropertyValue(yRotShort, "Y Rotation"); } else if (field.FieldName == "Z Rotation") { short zRotShort = WMath.RotationFloatToShort(eulerRot.Z); propValue = new TShortPropertyValue(zRotShort, "Z Rotation"); } else if (field.FieldName == "X Scale") { float xScale = actor.Transform.LocalScale.X; propValue = new TBytePropertyValue((byte)(xScale * 10), "X Scale"); } else if (field.FieldName == "Y Scale") { float yScale = actor.Transform.LocalScale.Y; propValue = new TBytePropertyValue((byte)(yScale * 10), "Y Scale"); } else if (field.FieldName == "Z Scale") { float zScale = actor.Transform.LocalScale.Z; propValue = new TBytePropertyValue((byte)(zScale * 10), "Z Scale"); } switch (field.FieldType) { case PropertyValueType.Byte: writer.Write((byte)propValue.GetValue()); break; case PropertyValueType.Bool: writer.Write((bool)propValue.GetValue()); break; case PropertyValueType.Short: writer.Write((short)propValue.GetValue()); break; case PropertyValueType.Int: writer.Write((int)propValue.GetValue()); break; case PropertyValueType.Float: writer.Write((float)propValue.GetValue()); break; case PropertyValueType.String: writer.Write(System.Text.Encoding.ASCII.GetBytes((string)propValue.GetValue())); break; case PropertyValueType.FixedLengthString: string fixedLenStr = (string)propValue.GetValue(); for (int i = 0; i < field.Length; i++) { writer.Write(i < fixedLenStr.Length ? (byte)fixedLenStr[i] : (byte)0); } //writer.WriteFixedString((string)propValue.GetValue(), (int)field.Length); break; break; case PropertyValueType.Vector2: Vector2 vec2Val = (Vector2)propValue.GetValue(); writer.Write(vec2Val.X); writer.Write(vec2Val.Y); break; case PropertyValueType.Vector3: Vector3 vec3Val = (Vector3)propValue.GetValue(); writer.Write(vec3Val.X); writer.Write(vec3Val.Y); writer.Write(vec3Val.Z); break; case PropertyValueType.XRotation: case PropertyValueType.YRotation: case PropertyValueType.ZRotation: writer.Write((short)propValue.GetValue()); break; case PropertyValueType.Color24: WLinearColor color24 = (WLinearColor)propValue.GetValue(); writer.Write((byte)color24.R); writer.Write((byte)color24.G); writer.Write((byte)color24.B); break; case PropertyValueType.Color32: WLinearColor color32 = (WLinearColor)propValue.GetValue(); writer.Write((byte)color32.R); writer.Write((byte)color32.G); writer.Write((byte)color32.B); writer.Write((byte)color32.A); break; default: Console.WriteLine("Unsupported PropertyValueType: {0}", field.FieldType); break; } } }
public void CreateEntity(string fourCC) { if (m_world.Map == null || m_world.Map.FocusedScene == null) { return; } var actorDescriptors = new List <MapActorDescriptor>(); foreach (var file in Directory.GetFiles("resources/templates/")) { MapActorDescriptor descriptor = JsonConvert.DeserializeObject <MapActorDescriptor>(File.ReadAllText(file)); actorDescriptors.Add(descriptor); } MapActorDescriptor entityDescriptor = actorDescriptors.Find(x => string.Compare(x.FourCC, fourCC, true) == 0); if (entityDescriptor == null) { Console.WriteLine("Attempted to spawn unsupported FourCC: {0}", fourCC); return; } List <IPropertyValue> actorProperties = new List <IPropertyValue>(); foreach (var field in entityDescriptor.Fields) { switch (field.FieldName) { case "Position": case "X Rotation": case "Y Rotation": case "Z Rotation": case "X Scale": case "Y Scale": case "Z Scale": continue; } IPropertyValue propValue = null; switch (field.FieldType) { case PropertyValueType.Byte: propValue = new TBytePropertyValue(0, field.FieldName); break; case PropertyValueType.Bool: propValue = new TBoolPropertyValue(false, field.FieldName); break; case PropertyValueType.Short: propValue = new TShortPropertyValue(0, field.FieldName); break; case PropertyValueType.Int: propValue = new TIntPropertyValue(0, field.FieldName); break; case PropertyValueType.Float: propValue = new TFloatPropertyValue(0f, field.FieldName); break; case PropertyValueType.FixedLengthString: case PropertyValueType.String: propValue = new TStringPropertyValue("", field.FieldName); break; case PropertyValueType.Vector2: propValue = new TVector2PropertyValue(new OpenTK.Vector2(0f, 0f), field.FieldName); break; case PropertyValueType.Vector3: propValue = new TVector3PropertyValue(new OpenTK.Vector3(0f, 0f, 0f), field.FieldName); break; case PropertyValueType.XRotation: case PropertyValueType.YRotation: case PropertyValueType.ZRotation: propValue = new TShortPropertyValue(0, field.FieldName); break; case PropertyValueType.Color24: propValue = new TLinearColorPropertyValue(new WLinearColor(1f, 1f, 1f), field.FieldName); break; case PropertyValueType.Color32: propValue = new TLinearColorPropertyValue(new WLinearColor(1f, 1f, 1f, 1f), field.FieldName); break; default: Console.WriteLine("Unsupported PropertyValueType: {0}", field.FieldType); break; } propValue.SetUndoStack(m_world.UndoStack); actorProperties.Add(propValue); } var newActor = new WActorNode(fourCC, m_world); newActor.Transform.Position = new OpenTK.Vector3(0, 200, 0); newActor.SetParent(m_world.Map.FocusedScene); newActor.Properties.AddRange(actorProperties); newActor.PostFinishedLoad(); ModifySelection(SelectionType.Add, newActor, true); }
private void ModifySelection(SelectionType action, WActorNode actor, bool clearSelection) { ModifySelection(action, new[] { actor }, clearSelection); }
private WActorNode LoadActorFromChunk(string fourCC, MapActorDescriptor template) { var newActor = new WActorNode(fourCC, m_world); List <IPropertyValue> actorProperties = new List <IPropertyValue>(); foreach (var field in template.Fields) { IPropertyValue propValue = null; switch (field.FieldType) { case PropertyValueType.Byte: propValue = new TBytePropertyValue(m_reader.ReadByte(), field.FieldName); break; case PropertyValueType.Bool: propValue = new TBoolPropertyValue(m_reader.ReadBoolean(), field.FieldName); break; case PropertyValueType.Short: propValue = new TShortPropertyValue(m_reader.ReadInt16(), field.FieldName); break; case PropertyValueType.Int: propValue = new TIntPropertyValue(m_reader.ReadInt32(), field.FieldName); break; case PropertyValueType.Float: propValue = new TFloatPropertyValue(m_reader.ReadSingle(), field.FieldName); break; case PropertyValueType.FixedLengthString: case PropertyValueType.String: string stringVal = (field.Length == 0) ? m_reader.ReadStringUntil('\0') : m_reader.ReadString(field.Length); stringVal = stringVal.Trim(new[] { '\0' }); propValue = new TStringPropertyValue(stringVal, field.FieldName); break; case PropertyValueType.Vector2: propValue = new TVector2PropertyValue(new OpenTK.Vector2(m_reader.ReadSingle(), m_reader.ReadSingle()), field.FieldName); break; case PropertyValueType.Vector3: propValue = new TVector3PropertyValue(new OpenTK.Vector3(m_reader.ReadSingle(), m_reader.ReadSingle(), m_reader.ReadSingle()), field.FieldName); break; case PropertyValueType.XRotation: case PropertyValueType.YRotation: case PropertyValueType.ZRotation: propValue = new TShortPropertyValue(m_reader.ReadInt16(), field.FieldName); break; case PropertyValueType.Color24: propValue = new TLinearColorPropertyValue(new WLinearColor(m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f), field.FieldName); break; case PropertyValueType.Color32: propValue = new TLinearColorPropertyValue(new WLinearColor(m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f, m_reader.ReadByte() / 255f), field.FieldName); break; default: Console.WriteLine("Unsupported PropertyValueType: {0}", field.FieldType); break; } propValue.SetUndoStack(m_world.UndoStack); actorProperties.Add(propValue); } // Now that we have loaded all properties out of it, we need to post-process them. IPropertyValue positionProperty = actorProperties.Find(x => x.Name == "Position"); IPropertyValue xRotProperty = actorProperties.Find(x => x.Name == "X Rotation"); IPropertyValue yRotProperty = actorProperties.Find(x => x.Name == "Y Rotation"); IPropertyValue zRotProperty = actorProperties.Find(x => x.Name == "Z Rotation"); IPropertyValue xScaleProperty = actorProperties.Find(x => x.Name == "X Scale"); IPropertyValue yScaleProperty = actorProperties.Find(x => x.Name == "Y Scale"); IPropertyValue zScaleProperty = actorProperties.Find(x => x.Name == "Z Scale"); // Remove these properties from the actor so they don't get added to the UI. actorProperties.Remove(positionProperty); actorProperties.Remove(xRotProperty); actorProperties.Remove(yRotProperty); actorProperties.Remove(zRotProperty); actorProperties.Remove(xScaleProperty); actorProperties.Remove(yScaleProperty); actorProperties.Remove(zScaleProperty); if (positionProperty != null) { newActor.Transform.Position = (Vector3)positionProperty.GetValue(); } float xRot = 0, yRot = 0, zRot = 0; if (xRotProperty != null) { xRot = WMath.RotationShortToFloat((short)xRotProperty.GetValue()); } if (yRotProperty != null) { yRot = WMath.RotationShortToFloat((short)yRotProperty.GetValue()); } if (zRotProperty != null) { zRot = WMath.RotationShortToFloat((short)zRotProperty.GetValue()); } // Build rotation with ZYX order. Quaternion xRotQ = Quaternion.FromAxisAngle(new Vector3(1, 0, 0), WMath.DegreesToRadians(xRot)); Quaternion yRotQ = Quaternion.FromAxisAngle(new Vector3(0, 1, 0), WMath.DegreesToRadians(yRot)); Quaternion zRotQ = Quaternion.FromAxisAngle(new Vector3(0, 0, 1), WMath.DegreesToRadians(zRot)); newActor.Transform.Rotation = zRotQ * yRotQ * xRotQ; float xScale = 1, yScale = 1, zScale = 1; if (xScaleProperty != null) { xScale = ((byte)xScaleProperty.GetValue()) / 10f; } if (yScaleProperty != null) { yScale = ((byte)yScaleProperty.GetValue()) / 10f; } if (zScaleProperty != null) { zScale = ((byte)zScaleProperty.GetValue()) / 10f; } newActor.Transform.LocalScale = new Vector3(xScale, yScale, zScale); newActor.Properties.AddRange(actorProperties); newActor.PostFinishedLoad(); return(newActor); }