Exemple #1
0
 /// <summary>
 /// Apply a selectors style to an instance.
 /// </summary>
 /// <param name="instance"></param>
 public void Apply(Instance instance)
 {
     foreach (StyleAttribute attr in this.StyleAttributes)
     {
         instance.TrySetProperty(attr.Property, attr.Value);
     }
 }
 public void AddInstance( Instance instance)
 {
     if (m_objects.ContainsKey(instance.Name))
     {
         throw new Exceptions.CompileException("There is already another instance with the name: " + instance.Name);
     }
     m_objects.Add( instance.Name, instance);
 }
Exemple #3
0
        // The include tag is left there, but INSIDE the include tag is all the included content.
        private void LoadAllChildren(Instance parentInstance, XmlNode parentNode, GameManager gm, string parentClassName)
        {
            // Algo...
            // Traverse entire tree...
            //   If <tagname> is one of "class", "include", "library", or "application" ignore it.
            //   If <tagname> is in symbol table, then create instance of it. Add all children to parent.
            //   If <tagname> is EVENT, METHOD, or ATTR
            //   If <tagname> is not in symbol table, then error.
            int uniqueIndex = 0;
            foreach (XmlNode childNode in parentNode.ChildNodes)
            {
                LoadNode(parentInstance, childNode, gm, parentClassName, uniqueIndex);
                uniqueIndex++;
            }

            return;
        }
Exemple #4
0
        /// <summary>
        /// Keep looking up the stack to find the room that an instance is in.
        /// </summary>
        /// <param name="child"></param>
        /// <returns></returns>
        public static Room FindRoomContaining(Instance child)
        {
            // Special case for self
            if (child is Room)
            {
                return child as Room;
            }

            while (child.Parent != null)
            {
                if (child.Parent is Room)
                {
                    return child.Parent as Room;
                }
                else
                {
                    // Recurse
                    FindRoomContaining(child.Parent);
                }
            }

            // Room not found. Probably unlikely.
            return null;
        }
Exemple #5
0
 /// <summary>
 /// Set the current room.
 /// </summary>
 /// <param name="room"></param>
 /// <param name="symTable"></param>
 public void GotoRoom(Room room, GameManager gm)
 {
     Room = room;
     GameMgr = gm;
 }
Exemple #6
0
        /// <summary>
        /// First implementation is a very simple cascade:
        /// (1) Apply universal styles, if applicable.
        /// (2) If found, apply styles
        /// </summary>
        /// <param name="instance"></param>
        public void ApplyStyleCascadeToInstance( Instance instance )
        {
            // Create selector by building up cascade
            Selector selectorToApply = new Selector();

            // Start with all universal selectors
            // TODO: Why do this on every call? Can't we precalculate the universal ones?
            foreach( Selector selector in m_UniversalStyle.Children.Convert<Selector>())
            {
                selectorToApply.Merge( selector as Selector);
            }

            // Replace with type styles, if applicable
            if (m_StylesForType.Children.ContainsKey(instance.GetSimpleClassName()))
            {
                selectorToApply.Merge(m_StylesForType.Children[instance.GetSimpleClassName()] as Selector);
            }

            // CssClass attribute styles override all previous
            if (m_StylesForCssClass.Children.ContainsKey(instance.CssClass))
            {
                selectorToApply.Merge(m_StylesForCssClass.Children[instance.CssClass] as Selector);
            }

            // Id attributes override all previous
            if (m_StylesForId.Children.ContainsKey(instance.Name))
            {
                selectorToApply.Merge(m_StylesForId.Children[instance.Name] as Selector);
            }

            // We now have a list of styles to apply to this instance, but not all of them
            //  may be real properties of the instance. Try each one.
            selectorToApply.Apply(instance);
        }
Exemple #7
0
 public void Raise_RightMouseUp(Instance self, Instance other, string extra)
 {
     if (RightMouseUp != null) { RightMouseUp(self, other, extra); }
 }
Exemple #8
0
 public void Raise_MouseOver(Instance self, Instance other, string extra)
 {
     if (MouseOver != null) { MouseOver(self, other, extra); }
 }
Exemple #9
0
 public void Raise_LostFocus(Instance self, Instance other, string extra)
 {
     if (LostFocus != null) { LostFocus(self, other, extra); }
 }
Exemple #10
0
 public void Raise_LeftMouseUp(Instance self, Instance other, string extra)
 {
     if (LeftMouseUp != null) { LeftMouseUp(self, other, extra); }
 }
Exemple #11
0
 public void Raise_Draw(Instance self, Instance other, string extra)
 {
     if (Draw != null) { Draw(self, other, extra); }
 }
Exemple #12
0
        private void RecurseChildren(Instance instance, ChildRecursor d )
        {
            foreach( Instance child in instance.Children.Values)
            {
                RecurseChildren( child, d);

                d.Invoke( child);
            }
        }
Exemple #13
0
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your drawing code here

            XnaGame xgcGame = this.Game as XnaGame;

            CurrentViewInstance = xgcGame.Room;

            FocusManager evm = xgcGame.GameMgr.FocusManager;

            // Fire events for every instance...
            /* Order of events
            Begin step events
            Alarm events
            Keyboard, Key press, and Key release events
            Mouse events
            Normal step events
            (now all instances are set to their new positions)
            Collision events
            End step events
            Drawing events
             */
            Keys[] keys = Keyboard.GetState().GetPressedKeys();
            #if !XBOX360
            MouseState mouseState = Mouse.GetState();
            #endif
            GamePadState padState = GamePad.GetState(PlayerIndex.One);

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (instance.IsJustCreated)
                {
                    instance.IsJustCreated = false;
                    instance.Raise_Create(instance, null, null);
                }
            });

            // Fire DESTROY event. Should this be last???
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (instance.IsJustDestroyed)
                {
                    instance.IsJustDestroyed = false;
                    instance.Raise_Destroy(instance, null, null);

                    // TODO: remove from instances...
                    CurrentViewInstance.Children.Remove(instance.Name);

                    // Don't do any more events -- we're destroyed.
                    //continue;
                }
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (this.IsJustStarted)
                {
                    instance.Raise_GameStart(instance, null, null); // TODO: OTHER is game object?
                }
            });

            this.IsJustStarted = false;

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (this.IsJustEnded)
                {
                    instance.Raise_GameEnd(instance, null, null); // TODO: OTHER is game object?
                }
            });

            this.IsJustEnded = false;

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
                {
                    if (CurrentViewInstance.IsJustCreated)
                    {
                        instance.Raise_RoomStart(instance, CurrentViewInstance, null);
                    }
                });

            CurrentViewInstance.IsJustCreated = false;

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (CurrentViewInstance.IsJustDestroyed)
                {
                    instance.Raise_RoomEnd(instance, CurrentViewInstance, null);
                }
            });

            CurrentViewInstance.IsJustDestroyed = false;

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (instance.Lives <= 0)
                {
                    instance.Raise_NoMoreLives(instance, null, null);
                }
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (instance.Health <= 0)
                {
                    instance.Raise_NoMoreHealth(instance, null, null);
                }
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                instance.Raise_EndOfAnimation(instance, null, null);
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (instance.IsEndOfPath)
                {
                    instance.Raise_EndOfPath(instance, null, null);
                }
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if (this.IsJustCloseButton)
                {
                    instance.Raise_CloseButton(instance, null, null);
                }
            });

            this.IsJustCloseButton = false;

            /* TODO
            foreach (Instance instance in CurrentViewInstance.Children.Values)
            {
                // Support more than 1...
                if (instance.HasUserDefinedEvent())
                {
                    instance.UserEvent( instance, (this, "UserEvent", null);
                }
            }
             * */

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                instance.Raise_BeginStep(instance, null, null);
            });

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {

                if (instance.IsJustAlarmed)
                {
                    instance.Raise_Alarm(instance, null, null);
                }
            });

            #if TBD
            // Key events go to focused control.
            // TODO: TAB/SHIFT-TAB can change focused control.
            if (keys.Length > 0)
            {
                View view = evm.GetFocusedView();
                if( view.KeyPress != null)
                {
                    view.KeyPress( view, null, keys.ToString() );
                }
            }
            #endif

            #if !XBOX360
            // Mouse events go to?
            //   HitTest?
            //   Bubble?
            //

            /*
             * Per .NET specs...
             *  Mouse events occur in the following order:

                   1.      MouseEnter
                   2.      MouseMove
                   3.      MouseHover / MouseDown / MouseWheel
                   4.      MouseUp
                   5.      MouseLeave
             */

            if (m_mouseInView != null)
            {
                if ((mouseState.LeftButton == ButtonState.Pressed) && (m_MouseStateLast.LeftButton == ButtonState.Released))
                {
                    m_mouseInView.Raise_LeftMouseDown(m_mouseInView, null, null);

                    // Focus change?
                    if (m_mouseInView != m_focusedView)
                    {
                        if (m_mouseInView.AcceptsFocus)
                        {
                            if ((m_focusedView != null))
                            {
                                m_focusedView.Raise_LostFocus(m_focusedView, m_mouseInView, null);
                            }

                            m_focusedView = m_mouseInView;

                            m_focusedView.Raise_GotFocus(m_focusedView, null, null);
                        }
                    }
                }
                else if ((mouseState.LeftButton == ButtonState.Released) && (m_MouseStateLast.LeftButton == ButtonState.Pressed))
                {
                    m_mouseInView.Raise_LeftMouseUp(m_mouseInView, null, null);
                }
            }

            // Right mouse button handling. Does not affect focus, currently.
            if (m_mouseInView != null)
            {
                if ((mouseState.RightButton == ButtonState.Pressed) && (m_MouseStateLast.RightButton == ButtonState.Released))
                {
                    m_mouseInView.Raise_RightMouseDown(m_mouseInView, null, null);
                }
                else if ((mouseState.RightButton == ButtonState.Released) && (m_MouseStateLast.RightButton == ButtonState.Pressed))
                {
                    m_mouseInView.Raise_RightMouseUp(m_mouseInView, null, null);
                }
            }

            int mx = mouseState.X;
            int my = mouseState.Y;

            // Find mouse in....
            // LAST: OUT, CURRENT: IN -> MOUSE IN + MOUSE OVER
            // LAST: IN, CURRENT: OUT -> MOUSE OUT
            // LAST: IN, CURRENT: IN, LASTPOS!=CURPOS -> MOUSE OVER (NOT IMPLEMENTED THIS WAY!)
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                View view = instance as View;
                if (view != null)
                {
                    if (view.MouseIsIn)
                    {
                        if (!view.HitTest(mx, my))
                        {
                            view.Raise_MouseOut(view, null, null);
                            view.MouseIsIn = false;
                            m_mouseInView = null;
                        }
                        else
                        {
                            // TODO: Should we only do this if mouse moved?
                            view.Raise_MouseOver(view, null, null);

                        }
                    }
                    else if (!view.MouseIsIn)
                    {
                        if (view.HitTest(mx, my))
                        {
                            view.MouseIsIn = true;
                            view.Raise_MouseIn(view, null, null);
                            m_mouseInView = view;
                        }
                    }
                }
            });

            #endif

            #if TBD
            // TODO: Add more events here
            if (padState.IsButtonDown(Buttons.A))
            {
                View view = evm.GetFocusedView();
                if (view != null)
                {
                    view.Raise_PadButtonA(view, null, null);
                }
            }
            #endif

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                m_gameTimer.Step(gameTime);
                instance.Raise_Step(instance, m_gameTimer, null);
            });

            // (now all instances are set to their new positions)

            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                // TODO: Need to check collisions against all objects.
                // Can be made more efficient by only checking collisions
                //  if there is a handler for that collision.
                if (instance.IsCollision())
                {
                    instance.Raise_Collision(instance, null, null);
                }
            });

            #if ROOM_SUPPORT
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if ( (CurrentRoomInstance).IsInstanceBoundaryHit(instance))
                {
                    instance.CallEventHandler(this, "Boundary", CurrentRoomInstance);
                }
            });
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if ( (CurrentRoomInstance).IsInstanceOutside(instance))
                {
                    instance.CallEventHandler(this, "Outside", CurrentRoomInstance);
                }
            });
            #endif

            #if HANDLE_VIEWS
            TODO
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if ( CurrentView.Bou())
                {
                    instance.CallEventHandler(this, "BoundaryView", null);
                }
            });
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                if ( CurrentView.OutsideView())
                {
                    instance.CallEventHandler(this, "OutsideView", null);
                }
            });
            #endif
            RecurseChildren(CurrentViewInstance, delegate(Instance instance)
            {
                instance.Raise_EndStep(instance, null, null);
            });

            m_MouseStateLast = CopyMouseState(mouseState);
            m_GamePadStateLast = CopyGamePadState(padState);

            // TODO: Add your update code here
            base.Update(gameTime);
        }
Exemple #14
0
        private void LoadNode(Instance parentInstance, XmlNode childNode, GameManager gm, string parentClassName, int uniqueIndex)
        {
            // skip comments
            if (childNode.NodeType == XmlNodeType.Comment)
            {
                return;
            }

            switch (childNode.Name)
            {
                case "Event":
                    {
                        // There should be a method on the script class with
                        // the name:
                        // instance.GetType().ToString() + "_" + name;
                        string name = GetInstanceId(childNode, parentClassName, uniqueIndex);
                        parentInstance.AddEventHandler(name);
                        break;

                    }
                case "Handler":
                    {
                        string name = GetInstanceId(childNode, parentClassName, uniqueIndex);
                        parentInstance.AddEventHandler(name);
                        break;
                    }
                case "Attribute":
                    {
                        // Add all attributes for instance.
                        //string name = GetRequiredAttribute(node, "Name");
                        //string t = GetRequiredAttribute(node, "Type");
                        //parentInstance.SetProperty(attr.Name, attr.Value);
                        break;
                    }
                case "Class":
                case "Method":
                case "Using":
                case "Resource":
                    // Skip
                    break;
                case "Include":
                    {
                        // Skip over these structural nodes.
                        if (childNode.HasChildNodes)
                        {
                            // Recurse
                            LoadAllChildren(parentInstance, childNode, gm, parentClassName);
                        }
                        break;
                    }
                case "Library":
                    // Skip over these structural nodes.
                    if (childNode.HasChildNodes)
                    {
                        // Recurse
                        LoadAllChildren(parentInstance, childNode, gm, parentClassName);
                    }
                    break;
                default:
                    {
                        string instanceId = GetInstanceId(childNode, parentClassName, uniqueIndex);

                        // Recurse
                        string nestClassName;
                        if (parentClassName == null)
                        {
                            // Yuck! Special case for constructing root object.
                            nestClassName = "xgc3.Generated." + childNode.Name + "_" + instanceId;
                        }
                        else
                        {
                            nestClassName = parentClassName + "+" + childNode.Name + "_" + instanceId;
                        }
                        Instance instance = gm.SymbolTable.Construct(instanceId, nestClassName, gm);

                        if (instance != null)
                        {
                            // Keep track of it by ID for quick lookup.
                            gm.RunningObjectTable.AddInstance(instance);

                            instance.Parent = parentInstance;

                            // Add instance to parent (root has no parent).
                            if (parentInstance != null)
                            {
                                if ( instance.ChildrenType != null)
                                {
                                    if ( !instance.GetType().IsSubclassOf( instance.ChildrenType))
                                    {
                                        throw new Exceptions.RuntimeEnvException("Unexpected child type. Expected: " + instance.ChildrenType.ToString());
                                    }
                                }

                                parentInstance.Children.Add(instanceId, instance);
                            }

                            // Set all properties on node.
                            foreach (XmlAttribute attr in childNode.Attributes)
                            {
                                instance.SetProperty(attr.Name, attr.Value);
                            }

                            if (childNode.HasChildNodes)
                            {
                                LoadAllChildren(instance, childNode, gm, nestClassName);
                            }
                        }
                        else
                        {
                            Console.WriteLine("Could not load instance of: " + nestClassName);
                        }

                        // If new styles found, add them to the stylsheet manager
                        if (childNode.Name == "StyleSheet")
                        {
                            StyleSheet sh = instance as StyleSheet;
                            m_styleSheetManager.AddStyleSheet(sh);
                        }

                        // Apply style sheet to just loaded instance.
                        m_styleSheetManager.ApplyStyleCascadeToInstance(instance);

                        //instance.Raise_Load(instance, null, null);
                    }

                    break;

                }
        }
 public void Raise_BarEvent(Instance self, Instance other, string extra)
 {
     if (BarEvent != null) { BarEvent(self, other, extra); }
 }
 public void Raise_Click(Instance self, Instance other, string extra)
 {
     if (Click != null) { Click(self, other, extra); }
 }