Ejemplo n.º 1
0
        public static T CreateInstance(Key key)
        {
            XUtils.Assert(sInstances != null, "must initialize XPluralton");

            lock ( sInitLock )
            {
                if (sInstances.ContainsKey(key))
                {
                    XUtils.Assert(false, "instance already exists for this key");
                    return(Instance(key));
                }

                // Create an instance via the private constructor
                Type t        = typeof(T);
                T    instance = (T)Activator.CreateInstance(t, true);
                sInstances.Add(key, instance);
                return(instance);
            }
        }
Ejemplo n.º 2
0
        // called by broadcaster
        public void Post(EventData e)
        {
            if (mEvents.Count == mMaxCapacity)
            {
                if (mFullBehaviour == eEventQueueFullBehaviour.Assert)
                {
                    XUtils.Assert(false, "listener queue full");
                }
                else if (mFullBehaviour == eEventQueueFullBehaviour.IgnoreOldest)
                {
                    mEvents.Dequeue();
                    mEvents.Enqueue(e);
                }

                // deliberate fall through for Ignore
            }
            else
            {
                mEvents.Enqueue(e);
            }
        }
Ejemplo n.º 3
0
        public static T CreateInstance()
        {
            lock ( sInitLock )
            {
                if (sInstance != null)
                {
                    XUtils.Assert(false, "instance already exists");
                    return(sInstance);
                }

                Type t = typeof(T);

                // Ensure there are no public constructors...
                ConstructorInfo[] ctors = t.GetConstructors();
                if (ctors.Length > 0)
                {
                    throw new InvalidOperationException(String.Format("{0} has at least one accesible ctor making it impossible to enforce singleton behaviour", t.Name));
                }

                // Create an instance via the private constructor
                sInstance = (T)Activator.CreateInstance(t, true);
                return(sInstance);
            }
        }
Ejemplo n.º 4
0
 public Vector2 GetMin()
 {
     XUtils.Assert(mIsValid);
     return(mMin);
 }
Ejemplo n.º 5
0
 private Vector2 GetSingleTouchPos()
 {
     XUtils.Assert(mTouches.Count == 1);
     return(mTouches[0].Position);
 }
Ejemplo n.º 6
0
 private static void Validate(Vector2 min, Vector2 max)
 {
     XUtils.Assert(max.X >= min.X && max.Y >= min.Y);
 }
Ejemplo n.º 7
0
 public void     ScaleWorld(float f)
 {
     XUtils.Assert(mIsValid && f >= 0f);
     mMin *= f; mMax *= f;
 }
Ejemplo n.º 8
0
 public float GetArea()
 {
     XUtils.Assert(mIsValid);
     return((mMax.X - mMin.X) * (mMax.Y - mMin.Y));
 }
Ejemplo n.º 9
0
 public Vector2 GetRadius()
 {
     XUtils.Assert(mIsValid);
     return(0.5f * GetSize());
 }
Ejemplo n.º 10
0
 public virtual void Render(XSimpleDraw simple_draw)
 {
     XUtils.Assert(mInitialized);
 }
Ejemplo n.º 11
0
 public void SetInputEnabled(bool value)
 {
     XUtils.Assert(mInitialized);
     mInputEnabled = value;
 }
Ejemplo n.º 12
0
 public Position GetPosition()
 {
     XUtils.Assert(mInitialized);
     return(mPosition);
 }
Ejemplo n.º 13
0
 public Widget GetScreenWidget()
 {
     XUtils.Assert(mScreenWidget != null);
     return(mScreenWidget);
 }
Ejemplo n.º 14
0
 public xMapCell GetMapCell(xCoord coord)
 {
     XUtils.Assert(coord.x >= 0 && coord.y >= 0 && coord.x < mMap.mBounds.x && coord.y < mMap.mBounds.y);
     return(mMap.mData[coord.x, coord.y]);
 }
Ejemplo n.º 15
0
 public Vector2 GetMax()
 {
     XUtils.Assert(mIsValid);
     return(mMax);
 }
Ejemplo n.º 16
0
 public void AddChild(Widget child)
 {
     XUtils.Assert(child != null && mChildren.Find(Widget.CompareWidgets(child)) == null);
     mChildren.Add(child);
 }
Ejemplo n.º 17
0
 public Vector2 GetSize()
 {
     XUtils.Assert(mIsValid);
     return(mMax - mMin);
 }
Ejemplo n.º 18
0
 public void RemoveChild(Widget child)
 {
     XUtils.Assert(child != null && mChildren.Remove(child));
 }
Ejemplo n.º 19
0
 public Vector2 GetCenter()
 {
     XUtils.Assert(mIsValid);
     return(0.5f * (mMin + mMax));
 }
Ejemplo n.º 20
0
 public void Subscribe(XListener <EventData> listener)
 {
     // TODO: maybe add returns number added, so avoid 'contains'
     XUtils.Assert(!mListeners.Contains(listener), "this listener already subscribed");
     mListeners.Add(listener);
 }
Ejemplo n.º 21
0
 public bool Contains(Vector2 point)
 {
     XUtils.Assert(mIsValid);
     return(point.X >= mMin.X && point.X <= mMax.X && point.Y >= mMin.Y && point.Y <= mMax.Y);
 }
Ejemplo n.º 22
0
 public void Unsubscribe(XListener <EventData> listener)
 {
     // TODO: maybe remove returns number removed, so avoid 'contains'
     XUtils.Assert(mListeners.Contains(listener), "this listener is not subscribed");
     mListeners.Remove(listener);
 }
Ejemplo n.º 23
0
 public void Translate(Vector2 translation)
 {
     XUtils.Assert(mIsValid);
     mMin += translation;
     mMax += translation;
 }
Ejemplo n.º 24
0
        public static void UnitTest()
        {
            TestListener    listener    = new TestListener();
            TestBroadcaster broadcaster = new TestBroadcaster();

            XUtils.Assert(listener.mMailbox1.GetNumEvents() == 0);
            //listener_1.mMailbox1.ReadNext(); // correctly hit assert for reading no events

            var event1 = new tEvent1();

            event1.a = 2;

            XUtils.Assert(broadcaster.mEvent1.IsSubscribed(listener.mMailbox1) == false);
            broadcaster.mEvent1.Subscribe(listener.mMailbox1);
            XUtils.Assert(broadcaster.mEvent1.IsSubscribed(listener.mMailbox1) == true);

            broadcaster.mEvent1.Post(event1);
            XUtils.Assert(listener.mMailbox1.GetNumEvents() == 1);
            var enumerator_1 = listener.mMailbox1.CreateEnumerator();

            enumerator_1.MoveNext();
            tEvent1 event_read = enumerator_1.GetCurrent();

            XUtils.Assert(listener.mMailbox1.GetNumEvents() == 0);
            XUtils.Assert(event_read.a == 2);

            // listener.mMailbox1.ReadNext(); // correctly asserts for reading no events after having one
            broadcaster.mEvent1.Post(event1);
            // broadcaster.mEvent1.Post( event1 ); // correctly asserts for event queue full

            var event3 = new tEvent3();

            event3.c = 'a';

            broadcaster.mEvent3.Subscribe(listener.mMailbox3);
            broadcaster.mEvent3.Post(event3);
            var event4 = new tEvent3();

            event4.c = 'b';
            broadcaster.mEvent3.Post(event4);
            broadcaster.mEvent3.Post(event4);
            XUtils.Assert(listener.mMailbox3.GetNumEvents() == 1);
            var enumerator_3 = listener.mMailbox3.CreateEnumerator();

            enumerator_3.MoveNext();
            XUtils.Assert(enumerator_3.GetCurrent().c == 'a');

            var event2 = new tEvent2();
            var event6 = new tEvent2();
            var event7 = new tEvent2();

            event2.b = 1.0f;

            broadcaster.mEvent2.Subscribe(listener.mMailbox2);
            broadcaster.mEvent2.Post(event2);
            event6.b = 2.0f;
            broadcaster.mEvent2.Post(event6);
            event7.b = 3.0f;
            broadcaster.mEvent2.Post(event7);
            XUtils.Assert(listener.mMailbox2.GetNumEvents() == 2);
            var enumerator2 = listener.mMailbox2.CreateEnumerator();

            enumerator2.MoveNext();
            XUtils.Assert(enumerator2.GetCurrent().b == 2.0f);
            enumerator2.MoveNext();
            XUtils.Assert(enumerator2.GetCurrent().b == 3.0f);

            TestListener listener2 = new TestListener();

            broadcaster.mEvent1.Subscribe(listener2.mMailbox1);
            var e1 = listener.mMailbox1.CreateEnumerator();

            e1.MoveNext();
            XUtils.Assert(listener.mMailbox1.GetNumEvents() == 0);
            XUtils.Assert(broadcaster.mEvent1.IsSubscribed(listener.mMailbox1));
            broadcaster.mEvent1.Post(event1);
            XUtils.Assert(listener.mMailbox1.GetNumEvents() == 1);
            XUtils.Assert(listener2.mMailbox1.GetNumEvents() == 1);
        }
Ejemplo n.º 25
0
        public static void UnitTest()
        {
            xAABB2 aabb1 = new xAABB2();

            XUtils.Assert(aabb1.mIsValid == false);

            xAABB2 aabb2 = new xAABB2(Vector2.Zero);
            xAABB2 aabb3 = new xAABB2(Vector2.Zero, 1f);
            xAABB2 aabb4 = new xAABB2(Vector2.Zero, Vector2.Zero);

            XUtils.Assert(aabb2.IsValid() && aabb3.IsValid() && aabb4.IsValid());

            aabb2.Reset();
            XUtils.Assert(!aabb2.IsValid());

            Vector2     oneTwo     = new Vector2(1f, 2f);
            Vector2     negTwoFour = -2f * oneTwo;
            Vector2     TwentyTen  = new Vector2(20f, 10f);
            const float kTol       = 0.001f;

            aabb2.Set(oneTwo);
            XUtils.AssertVal(aabb2.mMin, oneTwo, kTol);
            XUtils.AssertVal(aabb2.mMax, oneTwo, kTol);
            XUtils.Assert(aabb2.IsValid());

            //aabb2.Set( oneTwo, negTwoFour ); inside out, asserts, good
            aabb2.Set(negTwoFour, oneTwo);
            XUtils.Assert(aabb2.Contains(Vector2.Zero));
            XUtils.Assert(!aabb2.Contains(TwentyTen));
            XUtils.Assert(aabb2.Contains(negTwoFour));
            XUtils.Assert(aabb2.Contains(oneTwo));
            XUtils.Assert(!aabb2.Contains(-TwentyTen));

            //aabb2.Set( oneTwo, -3f ); asserts on negative radius, good
            Vector2 fiveFive     = new Vector2(5f, 5f);
            Vector2 sixSeven     = oneTwo + fiveFive;
            Vector2 negFourThree = new Vector2(-4f, -3f);
            Vector2 epsilon      = new Vector2(kTol, kTol);

            aabb2.Set(oneTwo, 5f);
            XUtils.Assert(aabb2.Contains(oneTwo));
            XUtils.Assert(aabb2.Contains(fiveFive));
            XUtils.Assert(aabb2.Contains(negFourThree));
            XUtils.Assert(!aabb2.Contains(TwentyTen));
            XUtils.Assert(!aabb2.Contains(-TwentyTen));
            XUtils.Assert(aabb2.Contains(Vector2.Zero));
            XUtils.Assert(aabb2.Contains(negFourThree + epsilon));
            XUtils.Assert(!aabb2.Contains(negFourThree - epsilon));
            XUtils.Assert(aabb2.Contains(sixSeven - epsilon));
            XUtils.Assert(!aabb2.Contains(sixSeven + epsilon));

            aabb2.Set(Vector2.Zero);
            XUtils.Assert(!aabb2.IsNonDegenerate());

            aabb2.Add(Vector2.UnitX);
            XUtils.Assert(!aabb2.IsNonDegenerate());
            XUtils.Assert(aabb2.Contains(new Vector2(0.5f, 0f)));

            aabb2.Add(oneTwo);
            XUtils.Assert(aabb2.IsNonDegenerate());
            XUtils.Assert(aabb2.Contains(new Vector2(0.5f, 1f)));

            aabb2.Reset();
            aabb2.Add(Vector2.Zero);
            XUtils.Assert(aabb2.IsValid());

            aabb2.Reset();
            aabb2.Add(-fiveFive);
            aabb2.Add(TwentyTen);
            XUtils.AssertVal(aabb2.GetMin(), -fiveFive, kTol);
            XUtils.AssertVal(aabb2.GetMax(), TwentyTen, kTol);
            XUtils.AssertVal(aabb2.GetCenter(), new Vector2(7.5f, 2.5f), kTol);
            XUtils.AssertVal(aabb2.GetRadius(), new Vector2(12.5f, 7.5f), kTol);
            XUtils.AssertVal(aabb2.GetSize(), new Vector2(25f, 15f), kTol);
            XUtils.AssertVal(aabb2.GetArea(), 375f, kTol);

            aabb2.Reset();
            aabb2.Add(Vector2.Zero);
            aabb2.Add(oneTwo);
            aabb2.ScaleWorld(4f);
            XUtils.AssertVal(aabb2.GetArea(), 32f, kTol);

            aabb2.Translate(fiveFive);
            Vector2 center2 = new Vector2(7f, 9f);

            XUtils.AssertVal(aabb2.GetArea(), 32f, kTol);
            XUtils.AssertVal(aabb2.GetCenter(), center2, kTol);
            XUtils.Assert(!aabb2.Contains(oneTwo));
            XUtils.Assert(aabb2.Contains(new Vector2(6f, 8f)));

            //aabb2.ScaleWorld( -1f ); asserts negative scalar, good
            //aabb2.ScaleLocal( -50f ); asserts negative scalar, good
            aabb2.ScaleLocal(0.25f);
            XUtils.AssertVal(aabb2.GetCenter(), center2, kTol);
            XUtils.AssertVal(aabb2.GetArea(), 2f, kTol);
            XUtils.Assert(!aabb2.Contains(new Vector2(6f, 8f)));
            XUtils.Assert(aabb2.Contains(center2));

            aabb2.Reset();
            aabb2.Add(Vector2.Zero);
            aabb2.Add(oneTwo);
            aabb2.Resize(new Vector2(0.1f, -0.3f));
            XUtils.AssertVal(aabb2.GetArea(), 1.68f, kTol);
            XUtils.Assert(!aabb2.Contains(Vector2.Zero));
            XUtils.Assert(aabb2.Contains(new Vector2(-0.05f, 1.4f)));
        }
Ejemplo n.º 26
0
        // state/transition helper functions
        private eContactChange UpdateTouchCount()
        {
            int            num_touches        = mTouches.Count();
            eContactCount  prev_contact_count = mContactCount;
            eContactChange count_change       = eContactChange.NoChange;

            eContactCount new_count = num_touches == 5 ? eContactCount.Five :
                                      num_touches == 4 ? eContactCount.Four :
                                      num_touches > 2 ? eContactCount.Many :
                                      num_touches > 1 ? eContactCount.Two :
                                      num_touches > 0 ? eContactCount.One :
                                      eContactCount.Zero;

            if (prev_contact_count == eContactCount.Zero)
            {
                count_change = new_count == eContactCount.One ? eContactChange.ZeroToOne :
                               new_count == eContactCount.Two ? eContactChange.ZeroToTwo :
                               new_count == eContactCount.Many ? eContactChange.ZeroToMany :
                               new_count == eContactCount.Four ? eContactChange.AnyToFour :
                               new_count == eContactCount.Five ? eContactChange.AnyToFive :
                               eContactChange.NoChange;
            }
            else if (prev_contact_count == eContactCount.One)
            {
                count_change = new_count == eContactCount.Zero ? eContactChange.OneToZero :
                               new_count == eContactCount.Two ? eContactChange.OneToTwo :
                               new_count == eContactCount.Many ? eContactChange.OneToMany :
                               new_count == eContactCount.Four ? eContactChange.AnyToFour :
                               new_count == eContactCount.Five ? eContactChange.AnyToFive :
                               eContactChange.NoChange;
            }
            else if (prev_contact_count == eContactCount.Two)
            {
                count_change = new_count == eContactCount.Zero ? eContactChange.TwoToZero :
                               new_count == eContactCount.One ? eContactChange.TwoToOne :
                               new_count == eContactCount.Many ? eContactChange.TwoToMany :
                               new_count == eContactCount.Four ? eContactChange.AnyToFour :
                               new_count == eContactCount.Five ? eContactChange.AnyToFive :
                               eContactChange.NoChange;
            }
            else if (prev_contact_count == eContactCount.Many)
            {
                count_change = new_count == eContactCount.Zero ? eContactChange.ManyToZero :
                               new_count == eContactCount.One ? eContactChange.ManyToOne :
                               new_count == eContactCount.Two ? eContactChange.ManyToTwo :
                               new_count == eContactCount.Four ? eContactChange.AnyToFour :
                               new_count == eContactCount.Five ? eContactChange.AnyToFive :
                               eContactChange.NoChange;
            }
            else if (prev_contact_count == eContactCount.Four)
            {
                count_change = new_count == eContactCount.Four ? eContactChange.NoChange :
                               new_count == eContactCount.Zero ? eContactChange.FourToZero :
                               eContactChange.FourToAnyPositive;
            }
            else if (prev_contact_count == eContactCount.Five)
            {
                count_change = new_count == eContactCount.Five ? eContactChange.NoChange :
                               new_count == eContactCount.Zero ? eContactChange.FiveToZero :
                               eContactChange.FiveToAnyPositive;
            }
            else if (prev_contact_count == eContactCount.Unknown)
            {
                count_change = (new_count == eContactCount.Zero) ?
                               eContactChange.NoInitialContacts :
                               eContactChange.InitialContacts;
            }
            else
            {
                XUtils.Assert(false);
            }

            mContactCount = new_count;
            return(count_change);
        }