private void childrenChanged() { this.layoutDirty = true; if (this.binding != null) this.Remove(this.binding); this.binding = null; }
protected override void updateLayout() { if (!this.ResizeHorizontal && !this.ResizeVertical) return; if (this.binding == null) { this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }).ToArray()); this.Add(this.binding); } Vector2 size = new Vector2(this.PaddingLeft, this.PaddingTop); foreach (UIComponent child in this.Children) { Vector2 childPos = child.Position; Vector2 min = childPos - (child.AnchorPoint.Value * child.Size.Value); childPos.X += Math.Max(0, this.PaddingLeft - min.X); childPos.Y += Math.Max(0, this.PaddingTop - min.Y); child.Position.Value = childPos; if (child.Visible) { Vector2 max = childPos + (child.InverseAnchorPoint.Value * child.Size.Value); size.X = Math.Max(size.X, max.X); size.Y = Math.Max(size.Y, max.Y); } } Vector2 originalSize = this.Size; size.X = this.ResizeHorizontal ? size.X + this.PaddingRight : originalSize.X; size.Y = this.ResizeVertical ? size.Y + this.PaddingBottom : originalSize.Y; this.Size.Value = size; }
private void childrenChanged() { if (this.binding != null) { this.Remove(this.binding); this.binding = null; } this.layoutDirty = true; }
public override void Awake() { this.EnabledInEditMode = false; this.EnabledWhenPaused = false; this.Bind.Action = () => { this.FindProperties(); if (this.targetInProperty != null) { if (this._inChanged == null) { _inChanged = new NotifyBinding(InChanged, this.Enabled, targetInProperty); } this.Add(_inChanged); if (this.Enabled) { InChanged(); } } if (this.targetOutProperty != null && this.TwoWay) { if (this._outChanged == null) { _outChanged = new NotifyBinding(OutChanged, this.Enabled, targetOutProperty); } this.Add(_outChanged); } }; this.UnBind.Action = () => { if (this._inChanged != null) { this.Remove(_inChanged); } if (this._outChanged != null) { this.Remove(_outChanged); } }; this.Set.Action = () => { this.FindProperties(); if (this.targetInProperty != null) { InChanged(); } }; base.Awake(); if (this.BindImmediately) { this.Bind.Execute(); } }
protected override void updateLayout() { if (this.binding == null) { if (this.Children.Length == 1) { UIComponent child = this.Children.First(); this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Size, child.ScaledSize); this.Add(this.binding); } } if (this.Children.Length == 1) { UIComponent child = this.Children.First(); Vector2 newPosition = child.Position; if (this.ResizeHorizontal) { float size = child.ScaledSize.Value.X; if (size > this.MaxHorizontalSize && this.MaxHorizontalSize != 0.0f) { size = this.MaxHorizontalSize; } this.Size.Value = new Vector2(size, this.Size.Value.Y); } newPosition.X = Math.Max(newPosition.X, this.Size.Value.X - child.ScaledSize.Value.X); newPosition.X = Math.Min(newPosition.X, 0); if (this.ResizeVertical) { float size = child.ScaledSize.Value.Y; if (size > this.MaxVerticalSize && this.MaxVerticalSize != 0.0f) { size = this.MaxVerticalSize; } this.Size.Value = new Vector2(this.Size.Value.X, size); } newPosition.Y = Math.Max(newPosition.Y, this.Size.Value.Y - child.ScaledSize.Value.Y); newPosition.Y = Math.Min(newPosition.Y, 0); child.Position.Value = newPosition; } }
protected override void updateLayout() { if (this.binding == null) { if (this.Children.Length == 1) { UIComponent child = this.Children.First(); this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Size, child.ScaledSize); this.Add(this.binding); } } if (this.Children.Length == 1) { UIComponent child = this.Children.First(); Vector2 newPosition = child.Position; if (this.ResizeHorizontal) { float size = child.ScaledSize.Value.X; if (size > this.MaxHorizontalSize && this.MaxHorizontalSize != 0.0f) size = this.MaxHorizontalSize; this.Size.Value = new Vector2(size, this.Size.Value.Y); } newPosition.X = Math.Max(newPosition.X, this.Size.Value.X - child.ScaledSize.Value.X); newPosition.X = Math.Min(newPosition.X, 0); if (this.ResizeVertical) { float size = child.ScaledSize.Value.Y; if (size > this.MaxVerticalSize && this.MaxVerticalSize != 0.0f) size = this.MaxVerticalSize; this.Size.Value = new Vector2(this.Size.Value.X, size); } newPosition.Y = Math.Max(newPosition.Y, this.Size.Value.Y - child.ScaledSize.Value.Y); newPosition.Y = Math.Min(newPosition.Y, 0); child.Position.Value = newPosition; } }
protected override void updateLayout() { if (!this.ResizeHorizontal && !this.ResizeVertical) { return; } if (this.binding == null) { this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }).ToArray()); this.Add(this.binding); } Vector2 size = new Vector2(this.PaddingLeft, this.PaddingTop); for (int i = 0; i < this.Children.Count; i++) { UIComponent child = this.Children[i]; Vector2 childPos = child.Position; Vector2 min = childPos - (child.AnchorPoint.Value * child.Size.Value); childPos.X += Math.Max(0, this.PaddingLeft - min.X); childPos.Y += Math.Max(0, this.PaddingTop - min.Y); child.Position.Value = childPos; if (child.Visible) { Vector2 max = childPos + (child.InverseAnchorPoint.Value * child.Size.Value); size.X = Math.Max(size.X, max.X); size.Y = Math.Max(size.Y, max.Y); } } Vector2 originalSize = this.Size; size.X = this.ResizeHorizontal ? size.X + this.PaddingRight : originalSize.X; size.Y = this.ResizeVertical ? size.Y + this.PaddingBottom : originalSize.Y; this.Size.Value = size; }
private static void attachEditorComponents(Entity result, Main main) { Transform transform = result.Get<Transform>(); Property<bool> selected = new Property<bool> { Value = false, Editable = false, Serialize = false }; result.Add("EditorSelected", selected); Property<Entity.Handle> parentMap = result.GetOrMakeProperty<Entity.Handle>("Parent"); Command<Entity> toggleEntityConnected = new Command<Entity> { Action = delegate(Entity entity) { parentMap.Value = entity; } }; result.Add("ToggleEntityConnected", toggleEntityConnected); LineDrawer connectionLines = new LineDrawer { Serialize = false }; connectionLines.Add(new Binding<bool>(connectionLines.Enabled, selected)); Color connectionLineColor = new Color(1.0f, 1.0f, 1.0f, 0.5f); Action recalculateLine = delegate() { connectionLines.Lines.Clear(); Entity parent = parentMap.Value.Target; if (parent != null) { connectionLines.Lines.Add(new LineDrawer.Line { A = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(transform.Position, connectionLineColor), B = new Microsoft.Xna.Framework.Graphics.VertexPositionColor(parent.Get<Transform>().Position, connectionLineColor) }); } }; Model model = new Model(); model.Filename.Value = "Models\\cone"; model.Editable = false; model.Serialize = false; result.Add("DirectionModel", model); Property<Direction> dir = result.GetProperty<Direction>("Direction"); Transform mapTransform = result.Get<Transform>("MapTransform"); model.Add(new Binding<Matrix>(model.Transform, delegate() { Matrix m = Matrix.Identity; m.Translation = transform.Position; if (dir == Direction.None) m.Forward = m.Right = m.Up = Vector3.Zero; else { Vector3 normal = Vector3.TransformNormal(dir.Value.GetVector(), mapTransform.Matrix); m.Forward = -normal; if (normal.Equals(Vector3.Up)) m.Right = Vector3.Left; else if (normal.Equals(Vector3.Down)) m.Right = Vector3.Right; else m.Right = Vector3.Normalize(Vector3.Cross(normal, Vector3.Down)); m.Up = Vector3.Cross(normal, m.Left); } return m; }, transform.Matrix, mapTransform.Matrix)); NotifyBinding recalculateBinding = null; Action rebuildBinding = delegate() { if (recalculateBinding != null) { connectionLines.Remove(recalculateBinding); recalculateBinding = null; } if (parentMap.Value.Target != null) { recalculateBinding = new NotifyBinding(recalculateLine, parentMap.Value.Target.Get<Transform>().Matrix); connectionLines.Add(recalculateBinding); } recalculateLine(); }; connectionLines.Add(new NotifyBinding(rebuildBinding, parentMap)); connectionLines.Add(new NotifyBinding(recalculateLine, selected)); connectionLines.Add(new NotifyBinding(recalculateLine, () => selected, transform.Position)); result.Add(connectionLines); }
protected override void updateLayout() { if (this.binding == null) { this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }).ToArray()); this.Add(this.binding); } float spacing = this.Spacing; Vector2 maxSize = Vector2.Zero; if (this.ResizePerpendicular) { foreach (UIComponent child in this.Children) { if (child.Visible) { Vector2 size = child.ScaledSize; maxSize.X = Math.Max(maxSize.X, size.X); maxSize.Y = Math.Max(maxSize.Y, size.Y); } } } else maxSize = this.Size; if (this.Orientation.Value == ListOrientation.Horizontal) { Vector2 anchorPoint; switch (this.Alignment.Value) { case ListAlignment.Min: anchorPoint = Vector2.Zero; break; case ListAlignment.Middle: anchorPoint = new Vector2(0, 0.5f); break; case ListAlignment.Max: anchorPoint = new Vector2(0, 1); break; default: anchorPoint = Vector2.Zero; break; } Vector2 pos = new Vector2(0, maxSize.Y * anchorPoint.Y); foreach (UIComponent child in this.Reversed ? this.Children.Reverse() : this.Children) { child.AnchorPoint.Value = anchorPoint; child.Position.Value = pos; if (child.Visible) pos.X += child.ScaledSize.Value.X + spacing; } this.Size.Value = new Vector2(Math.Max(0, pos.X - spacing), maxSize.Y); } else { Vector2 anchorPoint; switch (this.Alignment.Value) { case ListAlignment.Min: anchorPoint = Vector2.Zero; break; case ListAlignment.Middle: anchorPoint = new Vector2(0.5f, 0); break; case ListAlignment.Max: anchorPoint = new Vector2(1, 0); break; default: anchorPoint = Vector2.Zero; break; } Vector2 pos = new Vector2(maxSize.X * anchorPoint.X, 0); foreach (UIComponent child in this.Reversed ? this.Children.Reverse() : this.Children) { child.AnchorPoint.Value = anchorPoint; child.Position.Value = pos; if (child.Visible) pos.Y += child.ScaledSize.Value.Y + spacing; } this.Size.Value = new Vector2(maxSize.X, Math.Max(0, pos.Y - spacing)); } }
protected override void updateLayout() { if (this.binding == null) { this.binding = new NotifyBinding(delegate() { this.layoutDirty = true; }, this.Children.SelectMany(x => new IProperty[] { x.ScaledSize, x.Visible }).ToArray()); this.Add(this.binding); } float spacing = this.Spacing; Vector2 maxSize = Vector2.Zero; if (this.ResizePerpendicular) { for (int i = 0; i < this.Children.Count; i++) { UIComponent child = this.Children[i]; if (child.Visible) { Vector2 size = child.ScaledSize; maxSize.X = Math.Max(maxSize.X, size.X); maxSize.Y = Math.Max(maxSize.Y, size.Y); } } } else { maxSize = this.Size; } if (this.Orientation.Value == ListOrientation.Horizontal) { Vector2 anchorPoint; switch (this.Alignment.Value) { case ListAlignment.Min: anchorPoint = Vector2.Zero; break; case ListAlignment.Middle: anchorPoint = new Vector2(0, 0.5f); break; case ListAlignment.Max: anchorPoint = new Vector2(0, 1); break; default: anchorPoint = Vector2.Zero; break; } Vector2 pos = new Vector2(0, maxSize.Y * anchorPoint.Y); foreach (UIComponent child in this.Reversed ? this.Children.Reverse() : this.Children) { child.AnchorPoint.Value = anchorPoint; child.Position.Value = pos; if (child.Visible) { pos.X += child.ScaledSize.Value.X + spacing; } } this.Size.Value = new Vector2(Math.Max(0, pos.X - spacing), maxSize.Y); } else { Vector2 anchorPoint; switch (this.Alignment.Value) { case ListAlignment.Min: anchorPoint = Vector2.Zero; break; case ListAlignment.Middle: anchorPoint = new Vector2(0.5f, 0); break; case ListAlignment.Max: anchorPoint = new Vector2(1, 0); break; default: anchorPoint = Vector2.Zero; break; } Vector2 pos = new Vector2(maxSize.X * anchorPoint.X, 0); foreach (UIComponent child in this.Reversed ? this.Children.Reverse() : this.Children) { child.AnchorPoint.Value = anchorPoint; child.Position.Value = pos; if (child.Visible) { pos.Y += child.ScaledSize.Value.Y + spacing; } } this.Size.Value = new Vector2(maxSize.X, Math.Max(0, pos.Y - spacing)); } }
public void FlushComponents() { if (this.updating) return; lock (this.ComponentFlushLock) { foreach (IComponent c in this.componentsToAdd) { this.components.Add(c); Type t = c.GetType(); if (typeof(IDrawableComponent).IsAssignableFrom(t)) { this.drawables.Add((IDrawableComponent)c); if (this.drawableBinding != null) { this.drawableBinding.Delete(); this.drawableBinding = null; } } if (typeof(IUpdateableComponent).IsAssignableFrom(t)) this.updateables.Add((IUpdateableComponent)c); if (typeof(IDrawablePreFrameComponent).IsAssignableFrom(t)) this.preframeDrawables.Add((IDrawablePreFrameComponent)c); if (typeof(INonPostProcessedDrawableComponent).IsAssignableFrom(t)) this.nonPostProcessedDrawables.Add((INonPostProcessedDrawableComponent)c); if (typeof(IDrawableAlphaComponent).IsAssignableFrom(t)) { this.alphaDrawables.Add((IDrawableAlphaComponent)c); if (this.alphaDrawableBinding != null) { this.alphaDrawableBinding.Delete(); this.alphaDrawableBinding = null; } } } this.componentsToAdd.Clear(); foreach (IComponent c in this.componentsToRemove) { Type t = c.GetType(); if (typeof(IUpdateableComponent).IsAssignableFrom(t)) this.updateables.Remove((IUpdateableComponent)c); if (typeof(IDrawableComponent).IsAssignableFrom(t)) this.drawables.Remove((IDrawableComponent)c); if (typeof(IDrawablePreFrameComponent).IsAssignableFrom(t)) this.preframeDrawables.Remove((IDrawablePreFrameComponent)c); if (typeof(INonPostProcessedDrawableComponent).IsAssignableFrom(t)) this.nonPostProcessedDrawables.Remove((INonPostProcessedDrawableComponent)c); if (typeof(IDrawableAlphaComponent).IsAssignableFrom(t)) this.alphaDrawables.Remove((IDrawableAlphaComponent)c); this.components.Remove(c); } this.componentsToRemove.Clear(); } }
public static void Bind(Entity entity, Main main, Func <BEPUphysics.Entities.Entity, BEPUphysics.Entities.Entity, Vector3, Vector3, Vector3, ISpaceObject> createJoint, bool allowRotation, bool creating = false, bool directional = true) { Transform mapTransform = entity.GetOrCreate <Transform>("MapTransform"); mapTransform.Selectable.Value = false; Transform transform = entity.GetOrCreate <Transform>("Transform"); Factory.Get <DynamicVoxelFactory>().InternalBind(entity, main, creating, mapTransform); DynamicVoxel map = entity.Get <DynamicVoxel>(); Components.Joint jointData = entity.GetOrCreate <Components.Joint>("Joint"); Action refreshMapTransform = delegate() { Entity parent = jointData.Parent.Value.Target; if (parent != null && parent.Active) { Voxel staticMap = parent.Get <Voxel>(); jointData.Coord.Value = staticMap.GetCoordinate(transform.Matrix.Value.Translation); mapTransform.Position.Value = staticMap.GetAbsolutePosition(staticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + staticMap.Offset + map.Offset.Value); if (allowRotation) { if (main.EditorEnabled) { mapTransform.Quaternion.Value = transform.Quaternion; } } else { Matrix parentOrientation = staticMap.Transform; parentOrientation.Translation = Vector3.Zero; mapTransform.Quaternion.Value = Quaternion.CreateFromRotationMatrix(parentOrientation); } } else { mapTransform.Matrix.Value = transform.Matrix; } }; if (main.EditorEnabled) { entity.Add(new NotifyBinding(refreshMapTransform, transform.Matrix, transform.Quaternion, map.Offset, jointData.Parent)); } ISpaceObject joint = null; CommandBinding jointDeleteBinding = null, parentPhysicsUpdateBinding = null; NotifyBinding parentStaticMoveBinding = null; Action updateJoint = null; Action rebuildJoint = null; rebuildJoint = delegate() { if (jointDeleteBinding != null) { entity.Remove(jointDeleteBinding); } jointDeleteBinding = null; if (parentPhysicsUpdateBinding != null) { entity.Remove(parentPhysicsUpdateBinding); } parentPhysicsUpdateBinding = null; updateJoint(); }; updateJoint = delegate() { if (joint != null) { if (joint.Space != null) { main.Space.Remove(joint); } joint = null; } if (parentStaticMoveBinding != null) { entity.Remove(parentStaticMoveBinding); parentStaticMoveBinding = null; } Entity parent = jointData.Parent.Value.Target; if (main.EditorEnabled) { refreshMapTransform(); } else if (parent != null && parent.Active) { Voxel parentStaticMap = parent.Get <Voxel>(); //map.PhysicsEntity.Position = mapTransform.Position; if (!allowRotation) { map.PhysicsEntity.Orientation = mapTransform.Quaternion; } if (jointData.Direction != Direction.None) { Vector3 relativeLineAnchor = parentStaticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + parentStaticMap.Offset + map.Offset; Vector3 lineAnchor = parentStaticMap.GetAbsolutePosition(relativeLineAnchor); DynamicVoxel parentDynamicMap = parent.Get <DynamicVoxel>(); joint = createJoint(map.PhysicsEntity, parentDynamicMap == null ? null : parentDynamicMap.PhysicsEntity, lineAnchor, parentStaticMap.GetAbsoluteVector(jointData.Direction.Value.GetVector()), parentStaticMap.GetAbsolutePosition(jointData.Coord)); main.Space.Add(joint); map.PhysicsEntity.ActivityInformation.Activate(); if (parentDynamicMap != null && parentPhysicsUpdateBinding == null) { parentPhysicsUpdateBinding = new CommandBinding(parentDynamicMap.PhysicsUpdated, updateJoint); entity.Add(parentPhysicsUpdateBinding); } if (parentDynamicMap == null && joint is PrismaticJoint) { parentStaticMoveBinding = new NotifyBinding(delegate() { PrismaticJoint prismaticJoint = (PrismaticJoint)joint; Vector3 a = parentStaticMap.GetAbsolutePosition(relativeLineAnchor); prismaticJoint.PointOnLineJoint.LineAnchor = a; prismaticJoint.Limit.OffsetA = a; prismaticJoint.Motor.OffsetA = a; }, parentStaticMap.Transform); entity.Add(parentStaticMoveBinding); } if (jointDeleteBinding == null) { jointDeleteBinding = new CommandBinding(parent.Delete, delegate() { jointData.Parent.Value = null; }); entity.Add(jointDeleteBinding); } } } }; entity.Add(new CommandBinding(map.PhysicsUpdated, updateJoint)); entity.Add(new NotifyBinding(rebuildJoint, jointData.Parent)); entity.Add(new CommandBinding(entity.Delete, delegate() { if (joint != null && joint.Space != null) { main.Space.Remove(joint); joint = null; } })); entity.Add(new CommandBinding(map.OnSuspended, delegate() { if (joint != null && joint.Space != null) { main.Space.Remove(joint); } })); entity.Add(new CommandBinding(map.OnResumed, delegate() { if (joint != null && joint.Space == null) { main.Space.Add(joint); } })); entity.Add(new PostInitialization(rebuildJoint)); if (main.EditorEnabled) { JointFactory.attachEditorComponents(entity, main, directional); } }
protected override void Update(GameTime gameTime) { if (gameTime.ElapsedGameTime.TotalSeconds > 0.1f) gameTime = new GameTime(gameTime.TotalGameTime, new TimeSpan((long)(0.1f * (float)TimeSpan.TicksPerSecond)), true); this.GameTime = gameTime; this.ElapsedTime.Value = (float)gameTime.ElapsedGameTime.TotalSeconds * this.TimeMultiplier; if (!this.Paused) this.TotalTime.Value += this.ElapsedTime; if (!this.EditorEnabled && this.mapLoaded) { try { IEnumerable<string> mapGlobalScripts = Directory.GetFiles(Path.Combine(this.Content.RootDirectory, "GlobalScripts"), "*", SearchOption.AllDirectories).Select(x => Path.Combine("..\\GlobalScripts", Path.GetFileNameWithoutExtension(x))); foreach (string scriptName in mapGlobalScripts) this.executeScript(scriptName); } catch (IOException) { } } this.mapLoaded = false; this.LastKeyboardState.Value = this.KeyboardState; this.KeyboardState.Value = Microsoft.Xna.Framework.Input.Keyboard.GetState(); this.LastMouseState.Value = this.MouseState; this.MouseState.Value = Microsoft.Xna.Framework.Input.Mouse.GetState(); this.LastGamePadState.Value = this.GamePadState; this.GamePadState.Value = Microsoft.Xna.Framework.Input.GamePad.GetState(PlayerIndex.One); if (this.GamePadState.Value.IsConnected != this.GamePadConnected) this.GamePadConnected.Value = this.GamePadState.Value.IsConnected; #if PERFORMANCE_MONITOR Stopwatch timer = new Stopwatch(); timer.Start(); #endif for (int i = 0; i < this.updateables.Count; i++) { IUpdateableComponent c = this.updateables[i]; if (this.componentEnabled(c)) c.Update(this.ElapsedTime); } this.FlushComponents(); if (this.alphaDrawableBinding == null) { this.alphaDrawableBinding = new NotifyBinding(delegate() { this.alphaDrawablesModified = true; }, this.alphaDrawables.Select(x => x.DrawOrder).ToArray()); this.alphaDrawablesModified = true; } if (this.alphaDrawablesModified) { this.alphaDrawables.InsertionSort(delegate(IDrawableAlphaComponent a, IDrawableAlphaComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); } if (this.postAlphaDrawableBinding == null) { this.postAlphaDrawableBinding = new NotifyBinding(delegate() { this.postAlphaDrawablesModified = true; }, this.postAlphaDrawables.Select(x => x.DrawOrder).ToArray()); this.postAlphaDrawablesModified = true; } if (this.postAlphaDrawablesModified) { this.postAlphaDrawables.InsertionSort(delegate(IDrawablePostAlphaComponent a, IDrawablePostAlphaComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); } if (this.nonPostProcessedDrawableBinding == null) { this.nonPostProcessedDrawableBinding = new NotifyBinding(delegate() { this.nonPostProcessedDrawablesModified = true; }, this.nonPostProcessedDrawables.Select(x => x.DrawOrder).ToArray()); this.nonPostProcessedDrawablesModified = true; } if (this.nonPostProcessedDrawablesModified) { this.nonPostProcessedDrawables.InsertionSort(delegate(INonPostProcessedDrawableComponent a, INonPostProcessedDrawableComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); } if (this.resize != null && this.resize.Value.X > 0 && this.resize.Value.Y > 0) { this.ResizeViewport(this.resize.Value.X, this.resize.Value.Y, false); this.resize = null; } #if PERFORMANCE_MONITOR timer.Stop(); this.updateSum = Math.Max(this.updateSum, timer.Elapsed.TotalSeconds); timer.Restart(); #endif if (!this.Paused && !this.EditorEnabled) this.Space.Update(this.ElapsedTime); #if PERFORMANCE_MONITOR timer.Stop(); this.physicsSum = Math.Max(this.physicsSum, timer.Elapsed.TotalSeconds); AkSoundEngine.RenderAudio(); this.frameSum++; this.performanceInterval += (float)this.GameTime.ElapsedGameTime.TotalSeconds; if (this.performanceInterval > Main.performanceUpdateTime) { if (this.performanceMonitor.Visible) { this.frameRate.Value = this.frameSum / this.performanceInterval; this.physicsTime.Value = this.physicsSum; this.updateTime.Value = this.updateSum; this.preframeTime.Value = this.preframeSum; this.rawRenderTime.Value = this.rawRenderSum; this.shadowRenderTime.Value = this.shadowRenderSum; this.postProcessTime.Value = this.postProcessSum; this.unPostProcessedTime.Value = this.unPostProcessedSum; this.drawCalls.Value = this.drawCallCounter; this.triangles.Value = this.triangleCounter; this.drawCallCounter = 0; this.triangleCounter = 0; } this.physicsSum = 0; this.updateSum = 0; this.preframeSum = 0; this.rawRenderSum = 0; this.shadowRenderSum = 0; this.postProcessSum = 0; this.unPostProcessedSum = 0; this.frameSum = 0; this.performanceInterval = 0; } #endif TotalGameTime.Value += this.ElapsedTime.Value; SteamWorker.SetStat("stat_time_played", (int)TotalGameTime.Value); #if STEAMWORKS SteamWorker.Update(); #endif GeeUI.GeeUI.Update(this.ElapsedTime); this.update(); }
public void FlushComponents() { for (int i = 0; i < this.componentsToAdd.Count; i++) { IComponent c = this.componentsToAdd[i]; Type t = c.GetType(); if (typeof(IDrawableComponent).IsAssignableFrom(t)) this.drawables.Add((IDrawableComponent)c); if (typeof(IUpdateableComponent).IsAssignableFrom(t)) this.updateables.Add((IUpdateableComponent)c); if (typeof(IDrawablePreFrameComponent).IsAssignableFrom(t)) this.preframeDrawables.Add((IDrawablePreFrameComponent)c); if (typeof(INonPostProcessedDrawableComponent).IsAssignableFrom(t)) this.nonPostProcessedDrawables.Add((INonPostProcessedDrawableComponent)c); if (typeof(IDrawableAlphaComponent).IsAssignableFrom(t)) { this.alphaDrawables.Add((IDrawableAlphaComponent)c); if (this.alphaDrawableBinding != null) { this.alphaDrawableBinding.Delete(); this.alphaDrawableBinding = null; } } if (typeof(IDrawablePostAlphaComponent).IsAssignableFrom(t)) { this.postAlphaDrawables.Add((IDrawablePostAlphaComponent)c); if (this.postAlphaDrawableBinding != null) { this.postAlphaDrawableBinding.Delete(); this.postAlphaDrawableBinding = null; } } } this.componentsToAdd.Clear(); for (int i = 0; i < this.componentsToRemove.Count; i++) { IComponent c = this.componentsToRemove[i]; Type t = c.GetType(); if (typeof(IUpdateableComponent).IsAssignableFrom(t)) this.updateables.Remove((IUpdateableComponent)c); if (typeof(IDrawableComponent).IsAssignableFrom(t)) this.drawables.Remove((IDrawableComponent)c); if (typeof(IDrawablePreFrameComponent).IsAssignableFrom(t)) this.preframeDrawables.Remove((IDrawablePreFrameComponent)c); if (typeof(INonPostProcessedDrawableComponent).IsAssignableFrom(t)) this.nonPostProcessedDrawables.Remove((INonPostProcessedDrawableComponent)c); if (typeof(IDrawableAlphaComponent).IsAssignableFrom(t)) this.alphaDrawables.Remove((IDrawableAlphaComponent)c); c.delete(); } this.componentsToRemove.Clear(); }
public override void Awake() { base.Awake(); this.FontFile.Get = delegate() { return this.fontFile; }; this.FontFile.Set = delegate(string value) { this.fontFile = value; if (this.main != null) this.loadFont(); }; this.internalText.Set = delegate(string value) { this.internalText.InternalValue = value; this.updateText(); }; this.WrapWidth.Set = delegate(float value) { this.WrapWidth.InternalValue = value; this.updateText(); }; this.Text.Set = delegate(string value) { this.Text.InternalValue = value; List<IProperty> dependencies = new List<IProperty>(); bool dependsOnLanguage = false; StringBuilder builder; if (value != null && value.Length > 0 && value[0] == '\\') { string key = value.Substring(1); string translated = this.main.Strings.Get(key); if (translated == null) translated = key; builder = new StringBuilder(translated); dependsOnLanguage = true; } else builder = new StringBuilder(value); for (int i = 0; i < builder.Length; i++) { if (builder[i] == '{' && builder[i + 1] == '{') { // Grab the key string key = null; StringBuilder keyBuilder = new StringBuilder(); for (int j = i + 2; j < builder.Length; j++) { if (builder[j] == '}' && builder[j + 1] == '}') { key = keyBuilder.ToString(); break; } else keyBuilder.Append(builder[j]); } if (key != null) { IProperty property; if (TextElement.BindableProperties.TryGetValue(key, out property)) { string oldKey = string.Format("{{{{{0}}}}}", key); string argumentIndexKey = string.Format("{{{0}}}", dependencies.Count); builder.Replace(oldKey, argumentIndexKey, i, key.Length + 4); dependencies.Add(property); i += argumentIndexKey.Length - 1; } } } } if (this.internalTextBinding != null) this.Remove(this.internalTextBinding); if (this.languageBinding != null) this.Remove(this.languageBinding); if (dependencies.Count > 0) { dependsOnLanguage = true; string format = builder.ToString(); IProperty[] dependenciesArray = dependencies.ToArray(); this.internalTextBinding = new Binding<string>(this.internalText, delegate() { string[] strings = new string[dependenciesArray.Length]; for (int i = 0; i < dependenciesArray.Length; i++) { string dependency = dependenciesArray[i].ToString(); if (dependency[0] == '\\') { string key = dependency.Substring(1); dependency = this.main.Strings.Get(key); if (dependency == null) dependency = key; } strings[i] = dependency; } return string.Format(CultureInfo.CurrentCulture, format, strings); }, dependenciesArray); this.Add(this.internalTextBinding); } else { this.internalTextBinding = null; this.internalText.Value = builder.ToString(); } if (dependsOnLanguage) { this.languageBinding = new NotifyBinding(this.Text.Reset, this.main.Strings.Language); this.Add(this.languageBinding); } else this.languageBinding = null; }; }
public override void Awake() { base.Awake(); this.Add(new SetBinding <string>(this.FontFile, delegate(string value) { if (this.main != null) { this.loadFont(); } })); this.Add(new SetBinding <string>(this.internalText, delegate(string value) { this.updateText(); })); this.Add(new SetBinding <float>(this.WrapWidth, delegate(float value) { this.updateText(); })); this.Add(new SetBinding <bool>(this.FilterUnicode, delegate(bool value) { this.updateText(); })); this.Add(new SetBinding <string>(this.Text, delegate(string value) { if (this.internalTextBinding != null) { this.Remove(this.internalTextBinding); } if (this.languageBinding != null) { this.Remove(this.languageBinding); } if (this.Interpolation) { List <IProperty> dependencies = new List <IProperty>(); bool dependsOnLanguage = false; StringBuilder builder; if (value != null && value.Length > 0 && value[0] == '\\') { string key = value.Substring(1); string translated = this.main.Strings.Get(key); if (translated == null) { translated = key; } builder = new StringBuilder(translated); dependsOnLanguage = true; } else { builder = new StringBuilder(value); } for (int i = 0; i < builder.Length; i++) { if (builder[i] == '{' && builder[i + 1] == '{') { // Grab the key string key = null; StringBuilder keyBuilder = new StringBuilder(); for (int j = i + 2; j < builder.Length; j++) { if (builder[j] == '}' && builder[j + 1] == '}') { key = keyBuilder.ToString(); break; } else { keyBuilder.Append(builder[j]); } } if (key != null) { IProperty property; if (TextElement.BindableProperties.TryGetValue(key, out property)) { string oldKey = string.Format("{{{{{0}}}}}", key); string argumentIndexKey = string.Format("{{{0}}}", dependencies.Count); builder.Replace(oldKey, argumentIndexKey, i, key.Length + 4); dependencies.Add(property); i += argumentIndexKey.Length - 1; } } } } if (dependencies.Count > 0) { dependsOnLanguage = true; string format = builder.ToString(); IProperty[] dependenciesArray = dependencies.ToArray(); this.internalTextBinding = new Binding <string>(this.internalText, delegate() { string[] strings = new string[dependenciesArray.Length]; for (int i = 0; i < dependenciesArray.Length; i++) { string dependency = dependenciesArray[i].ToString(); if (dependency[0] == '\\') { string key = dependency.Substring(1); dependency = this.main.Strings.Get(key); if (dependency == null) { dependency = key; } } strings[i] = dependency; } return(string.Format(CultureInfo.CurrentCulture, format, strings)); }, dependenciesArray); this.Add(this.internalTextBinding); } else { this.internalTextBinding = null; this.internalText.Value = builder.ToString(); } if (dependsOnLanguage) { this.languageBinding = new NotifyBinding(this.Text.Reset, this.main.Strings.Language); this.Add(this.languageBinding); } else { this.languageBinding = null; } } else { this.internalTextBinding = null; this.languageBinding = null; this.internalText.Value = value; } })); this.Add(new NotifyBinding(this.Text.Reset, this.Interpolation)); }
protected override void Update(GameTime gameTime) { if (!this.EditorEnabled && this.mapLoaded) { IEnumerable<string> mapGlobalScripts = Directory.GetFiles(Path.Combine(this.Content.RootDirectory, "Maps", "GlobalScripts"), "*", SearchOption.AllDirectories).Select(x => Path.Combine("Maps", "GlobalScripts", Path.GetFileNameWithoutExtension(x))); foreach (string scriptName in mapGlobalScripts) this.executeScript(scriptName); } this.mapLoaded = false; if (gameTime.ElapsedGameTime.TotalSeconds > 0.1f) gameTime = new GameTime(gameTime.TotalGameTime, new TimeSpan((long)(0.1f * (float)TimeSpan.TicksPerSecond)), true); this.GameTime = gameTime; this.ElapsedTime.Value = (float)gameTime.ElapsedGameTime.TotalSeconds * this.TimeMultiplier; if (!this.Paused) this.TotalTime.Value += this.ElapsedTime; this.LastKeyboardState.Value = this.KeyboardState; this.KeyboardState.Value = Microsoft.Xna.Framework.Input.Keyboard.GetState(); this.LastMouseState.Value = this.MouseState; this.MouseState.Value = Microsoft.Xna.Framework.Input.Mouse.GetState(); #if PERFORMANCE_MONITOR Stopwatch timer = new Stopwatch(); timer.Start(); #endif if (!this.Paused && !this.EditorEnabled) this.Space.Update(this.ElapsedTime); #if PERFORMANCE_MONITOR timer.Stop(); this.physicsSum = Math.Max(this.physicsSum, timer.Elapsed.TotalSeconds); #endif #if PERFORMANCE_MONITOR timer.Restart(); #endif this.updating = true; foreach (IUpdateableComponent c in this.updateables) { if (this.componentEnabled((Component)c)) c.Update(this.ElapsedTime); } this.updating = false; this.FlushComponents(); if (this.drawableBinding == null) { this.drawableBinding = new NotifyBinding(delegate() { this.drawablesModified = true; }, this.drawables.Select(x => x.DrawOrder).ToArray()); this.drawablesModified = true; } if (this.drawablesModified) { this.drawables.InsertionSort(delegate(IDrawableComponent a, IDrawableComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); this.drawablesModified = false; } if (this.alphaDrawableBinding == null) { this.alphaDrawableBinding = new NotifyBinding(delegate() { this.alphaDrawablesModified = true; }, this.alphaDrawables.Select(x => x.DrawOrder).ToArray()); this.alphaDrawablesModified = true; } if (this.alphaDrawablesModified) { this.alphaDrawables.InsertionSort(delegate(IDrawableAlphaComponent a, IDrawableAlphaComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); } if (this.nonPostProcessedDrawableBinding == null) { this.nonPostProcessedDrawableBinding = new NotifyBinding(delegate() { this.nonPostProcessedDrawablesModified = true; }, this.nonPostProcessedDrawables.Select(x => x.DrawOrder).ToArray()); this.nonPostProcessedDrawablesModified = true; } if (this.nonPostProcessedDrawablesModified) { this.nonPostProcessedDrawables.InsertionSort(delegate(INonPostProcessedDrawableComponent a, INonPostProcessedDrawableComponent b) { return a.DrawOrder.Value.CompareTo(b.DrawOrder.Value); }); } this.AudioEngine.Update(); if (this.resize != null && this.resize.Value.X > 0 && this.resize.Value.Y > 0) { this.ResizeViewport(this.resize.Value.X, this.resize.Value.Y, false); this.resize = null; } #if PERFORMANCE_MONITOR timer.Stop(); this.updateSum = Math.Max(this.updateSum, timer.Elapsed.TotalSeconds); this.frameSum++; this.performanceInterval += this.ElapsedTime; if (this.performanceInterval > Main.performanceUpdateTime) { this.frameRate.Value = this.frameSum / this.performanceInterval; this.physicsTime.Value = this.physicsSum; this.updateTime.Value = this.updateSum; this.preframeTime.Value = this.preframeSum; this.rawRenderTime.Value = this.rawRenderSum; this.shadowRenderTime.Value = this.shadowRenderSum; this.postProcessTime.Value = this.postProcessSum; this.physicsSum = 0; this.updateSum = 0; this.preframeSum = 0; this.rawRenderSum = 0; this.shadowRenderSum = 0; this.postProcessSum = 0; this.frameSum = 0; this.performanceInterval = 0; } #endif }
public static void BindMember(MemberInfo member, AutoConVar convar, object instance = null) { List <Type> allowedTypes = new Type[] { typeof(bool), typeof(int), typeof(float), typeof(double), typeof(string) }.ToList(); List <Type> generics = allowedTypes.Select(Type => typeof(Property <>).MakeGenericType(Type)).ToList(); allowedTypes.AddRange(generics); bool instantiated = instance != null; Type curType = null; object value = "null"; bool isProperty = true; string name = convar.ConVarName; string desc = convar.ConVarDesc; switch (member.MemberType) { case MemberTypes.Field: curType = ((FieldInfo)member).FieldType; value = ((FieldInfo)member).GetValue(instance); break; case MemberTypes.Property: curType = ((PropertyInfo)member).PropertyType; value = ((PropertyInfo)member).GetValue(instance, null); break; } if (curType == null) { return; } if (!allowedTypes.Contains(curType)) { throw new Exception("Cannot auto-bind convar to type " + curType.ToString()); } object propertyValue = value; var binding = new NotifyBinding(() => { if (propertyValue == null || propertyValue.GetType().GetProperty("Value") == null || propertyValue.GetType().GetProperty("Value").GetValue(propertyValue, null) == null) { return; } GetConVar(name).Value.Value = propertyValue.GetType().GetProperty("Value").GetValue(propertyValue, null).ToString(); }); if (generics.Contains(curType)) { value = propertyValue.GetType().GetProperty("Value").GetValue(propertyValue, null); if (value == null) { value = "null"; } else { value = value.ToString(); } curType = curType.GetGenericArguments()[0]; propertyValue.GetType() .InvokeMember("AddBinding", BindingFlags.InvokeMethod, null, propertyValue, new object[] { binding }); } else { isProperty = false; } if (instantiated) { RemoveConVar(name); } if (!isProperty) { AddConVar(new ConVar(name, desc, (string)value) { TypeConstraint = curType, Value = new Property <string>() { Value = (string)value }, OnChanged = new Property <Action <string> >() { Value = (s) => { if (instantiated && instance == null) { Log("Removing convar " + name + ": linked instance is null"); RemoveConVar(name); return; } switch (member.MemberType) { case MemberTypes.Field: ((FieldInfo)member).SetValue(instance, GetConVar(name).GetCastedValue()); break; case MemberTypes.Property: ((PropertyInfo)member).SetValue(instance, GetConVar(name).GetCastedValue(), null); break; } } } }); } else { AddConVar(new ConVar(name, desc, (string)value) { TypeConstraint = curType, Value = new Property <string>() { Value = (string)value }, OnChanged = new Property <Action <string> >() { Value = (s) => { if (instantiated && instance == null) { Log("Removing convar " + name + ": linked instance is null"); RemoveConVar(name); return; } object val = GetConVar(name).GetCastedValue(); propertyValue.GetType().GetProperty("Value").SetValue(propertyValue, val, null); } } }); } }
public static void Bind(Entity entity, Main main, Func<BEPUphysics.Entities.Entity, BEPUphysics.Entities.Entity, Vector3, Vector3, Vector3, ISpaceObject> createJoint, bool allowRotation, bool creating = false, bool directional = true) { Transform mapTransform = entity.GetOrCreate<Transform>("MapTransform"); mapTransform.Selectable.Value = false; Transform transform = entity.GetOrCreate<Transform>("Transform"); Factory.Get<DynamicVoxelFactory>().InternalBind(entity, main, creating, mapTransform); DynamicVoxel map = entity.Get<DynamicVoxel>(); Components.Joint jointData = entity.GetOrCreate<Components.Joint>("Joint"); Action refreshMapTransform = delegate() { Entity parent = jointData.Parent.Value.Target; if (parent != null && parent.Active) { Voxel staticMap = parent.Get<Voxel>(); jointData.Coord.Value = staticMap.GetCoordinate(transform.Matrix.Value.Translation); mapTransform.Position.Value = staticMap.GetAbsolutePosition(staticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + staticMap.Offset + map.Offset.Value); if (allowRotation) { if (main.EditorEnabled) mapTransform.Quaternion.Value = transform.Quaternion; } else { Matrix parentOrientation = staticMap.Transform; parentOrientation.Translation = Vector3.Zero; mapTransform.Quaternion.Value = Quaternion.CreateFromRotationMatrix(parentOrientation); } } else mapTransform.Matrix.Value = transform.Matrix; }; if (main.EditorEnabled) entity.Add(new NotifyBinding(refreshMapTransform, transform.Matrix, transform.Quaternion, map.Offset, jointData.Parent)); ISpaceObject joint = null; CommandBinding jointDeleteBinding = null, parentPhysicsUpdateBinding = null; NotifyBinding parentStaticMoveBinding = null; Action updateJoint = null; Action rebuildJoint = null; rebuildJoint = delegate() { if (jointDeleteBinding != null) entity.Remove(jointDeleteBinding); jointDeleteBinding = null; if (parentPhysicsUpdateBinding != null) entity.Remove(parentPhysicsUpdateBinding); parentPhysicsUpdateBinding = null; updateJoint(); }; updateJoint = delegate() { if (joint != null) { if (joint.Space != null) main.Space.Remove(joint); joint = null; } if (parentStaticMoveBinding != null) { entity.Remove(parentStaticMoveBinding); parentStaticMoveBinding = null; } Entity parent = jointData.Parent.Value.Target; if (main.EditorEnabled) refreshMapTransform(); else if (parent != null && parent.Active) { Voxel parentStaticMap = parent.Get<Voxel>(); //map.PhysicsEntity.Position = mapTransform.Position; if (!allowRotation) map.PhysicsEntity.Orientation = mapTransform.Quaternion; if (jointData.Direction != Direction.None) { Vector3 relativeLineAnchor = parentStaticMap.GetRelativePosition(jointData.Coord) - new Vector3(0.5f) + parentStaticMap.Offset + map.Offset; Vector3 lineAnchor = parentStaticMap.GetAbsolutePosition(relativeLineAnchor); DynamicVoxel parentDynamicMap = parent.Get<DynamicVoxel>(); joint = createJoint(map.PhysicsEntity, parentDynamicMap == null ? null : parentDynamicMap.PhysicsEntity, lineAnchor, parentStaticMap.GetAbsoluteVector(jointData.Direction.Value.GetVector()), parentStaticMap.GetAbsolutePosition(jointData.Coord)); main.Space.Add(joint); map.PhysicsEntity.ActivityInformation.Activate(); if (parentDynamicMap != null && parentPhysicsUpdateBinding == null) { parentPhysicsUpdateBinding = new CommandBinding(parentDynamicMap.PhysicsUpdated, updateJoint); entity.Add(parentPhysicsUpdateBinding); } if (parentDynamicMap == null && joint is PrismaticJoint) { parentStaticMoveBinding = new NotifyBinding(delegate() { PrismaticJoint prismaticJoint = (PrismaticJoint)joint; Vector3 a = parentStaticMap.GetAbsolutePosition(relativeLineAnchor); prismaticJoint.PointOnLineJoint.LineAnchor = a; prismaticJoint.Limit.OffsetA = a; prismaticJoint.Motor.OffsetA = a; }, parentStaticMap.Transform); entity.Add(parentStaticMoveBinding); } if (jointDeleteBinding == null) { jointDeleteBinding = new CommandBinding(parent.Delete, delegate() { jointData.Parent.Value = null; }); entity.Add(jointDeleteBinding); } } } }; entity.Add(new CommandBinding(map.PhysicsUpdated, updateJoint)); entity.Add(new NotifyBinding(rebuildJoint, jointData.Parent)); entity.Add(new CommandBinding(entity.Delete, delegate() { if (joint != null && joint.Space != null) { main.Space.Remove(joint); joint = null; } })); entity.Add(new CommandBinding(map.OnSuspended, delegate() { if (joint != null && joint.Space != null) main.Space.Remove(joint); })); entity.Add(new CommandBinding(map.OnResumed, delegate() { if (joint != null && joint.Space == null) main.Space.Add(joint); })); entity.Add(new PostInitialization(rebuildJoint)); if (main.EditorEnabled) JointFactory.attachEditorComponents(entity, main, directional); }