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); } }
// 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); } }
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); } }
public Vector2 GetMin() { XUtils.Assert(mIsValid); return(mMin); }
private Vector2 GetSingleTouchPos() { XUtils.Assert(mTouches.Count == 1); return(mTouches[0].Position); }
private static void Validate(Vector2 min, Vector2 max) { XUtils.Assert(max.X >= min.X && max.Y >= min.Y); }
public void ScaleWorld(float f) { XUtils.Assert(mIsValid && f >= 0f); mMin *= f; mMax *= f; }
public float GetArea() { XUtils.Assert(mIsValid); return((mMax.X - mMin.X) * (mMax.Y - mMin.Y)); }
public Vector2 GetRadius() { XUtils.Assert(mIsValid); return(0.5f * GetSize()); }
public virtual void Render(XSimpleDraw simple_draw) { XUtils.Assert(mInitialized); }
public void SetInputEnabled(bool value) { XUtils.Assert(mInitialized); mInputEnabled = value; }
public Position GetPosition() { XUtils.Assert(mInitialized); return(mPosition); }
public Widget GetScreenWidget() { XUtils.Assert(mScreenWidget != null); return(mScreenWidget); }
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]); }
public Vector2 GetMax() { XUtils.Assert(mIsValid); return(mMax); }
public void AddChild(Widget child) { XUtils.Assert(child != null && mChildren.Find(Widget.CompareWidgets(child)) == null); mChildren.Add(child); }
public Vector2 GetSize() { XUtils.Assert(mIsValid); return(mMax - mMin); }
public void RemoveChild(Widget child) { XUtils.Assert(child != null && mChildren.Remove(child)); }
public Vector2 GetCenter() { XUtils.Assert(mIsValid); return(0.5f * (mMin + mMax)); }
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); }
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); }
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); }
public void Translate(Vector2 translation) { XUtils.Assert(mIsValid); mMin += translation; mMax += translation; }
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); }
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))); }
// 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); }