using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Graphics; namespace BehaviourDemoPort { enum EditMode { Select, Move, } class SceneEditor { EditMode editMode; public SceneEditor() { Keys[] allKeys = (Keys[])Enum.GetValues(typeof(Keys)); foreach (var key in allKeys) KeyboardInput.AddKey(key); KeyboardInput.KeyRelease += new KeyHandler(KeyboardInput_KeyRelease); MouseInput.MouseMove += new MouseMoveHandler(MouseInput_MouseMove); MouseInput.MouseDown += new MouseClickHandler(MouseInput_MouseDown); MouseInput.MouseUp += new MouseClickHandler(MouseInput_MouseUp); } void KeyboardInput_KeyRelease(Keys key, KeyboardState keyState) { if (key == Keys.Q) { this.editMode = EditMode.Select; } else if (key == Keys.W) { this.editMode = EditMode.Move; } else if (key == Keys.Delete) { foreach (var actor in Actor.Selection) { actor.Delete(); } Actor.Selection.Clear(); } else if (key == Keys.C && Actor.Selection.Count >= 2) { for (int i = 1; i < Actor.Selection.Count; i++) { Node nodeA = Actor.Selection[i - 1] as Node; Node nodeB = Actor.Selection[i] as Node; if (nodeA != null & nodeB != null) nodeA.BidiConnect(nodeB); } } else if (key == Keys.X && Actor.Selection.Count >= 2) { for (int i = 1; i < Actor.Selection.Count; i++) { Node nodeA = Actor.Selection[i - 1] as Node; Node nodeB = Actor.Selection[i] as Node; if (nodeA != null & nodeB != null) nodeA.BidiDisconnect(nodeB); } } } void MouseInput_MouseMove(Vector2 position, Vector2 movement) { if (this.editMode == EditMode.Move && MouseInput.IsLeftButtonDown) { foreach (var actor in Actor.Selection) { actor.Position += movement; } } } void MouseInput_MouseDown(Vector2 position) { if (KeyboardInput.IsShiftDown) { Node node = new Node(); node.Position = position; Node lastNode = Actor.LastSelected as Node; if (lastNode != null) { node.BidiConnect(lastNode); } Actor.Selection.Clear(); node.Select(); return; } Actor actor = GetActorAt(position); if (actor != null) { if (!actor.IsSelected && !KeyboardInput.IsControlDown) Actor.Selection.Clear(); if (KeyboardInput.IsControlDown) actor.ToggleSelect(); else actor.Select(); } else if (!KeyboardInput.IsControlDown) Actor.Selection.Clear(); } void MouseInput_MouseUp(Vector2 position) { if (Actor.Selection.Count > 0 && !KeyboardInput.IsControlDown) { Actor.Selection.Clear(); Actor actor = GetActorAt(position); if (actor != null) actor.Select(); } } Actor GetActorAt(Vector2 position) { for (int i = Actor.Actors.Count -1; i >= 0; i--) { Actor actor = Actor.Actors[i]; if (Vector2.Distance(actor.Position, position) < actor.Radius) { return actor; } } return null; } public void Draw(SpriteBatch SpriteBatch) { string text = String.Format("mode: {0}", this.editMode); SpriteBatch.DrawString(Style.FontLarge, text, new Vector2(10, 10), Color.White); } } }