コード例 #1
0
ファイル: ChatBubble.cs プロジェクト: wtfcolt/game
        /// <summary>
        /// Creates a <see cref="ChatBubble"/> instance.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="owner">The owner.</param>
        /// <param name="text">The text.</param>
        /// <returns>The <see cref="ChatBubble"/> instance, or null if any <see cref="Exception"/>s errors occured
        /// while trying to make the <see cref="ChatBubble"/> or any of the supplied parameters were invalid.</returns>
        public static ChatBubble Create(Control parent, Entity owner, string text)
        {
            if (parent == null)
            {
                const string errmsg = "ChatBubble.Create() failed - parent was null.";
                if (log.IsErrorEnabled)
                    log.Error(errmsg);
                Debug.Fail(errmsg);
                return null;
            }

            if (owner == null)
            {
                const string errmsg = "ChatBubble.Create() failed - owner was null.";
                if (log.IsErrorEnabled)
                    log.Error(errmsg);
                Debug.Fail(errmsg);
                return null;
            }

            if (string.IsNullOrEmpty(text))
            {
                const string errmsg = "ChatBubble.Create() failed - text was null or empty.";
                if (log.IsWarnEnabled)
                    log.Warn(errmsg);
                return null;
            }

            lock (_chatBubblesSync)
            {
                ChatBubble c = null;

                try
                {
                    // If the ChatBubble already exists for the given Entity, reuse that one
                    if (_chatBubbles.TryGetValue(owner, out c) && c != null)
                    {
                        c._textControl.ChangeTextAndResize(text);
                        c._deathTime = (TickCount)(TickCount.Now + Lifespan);
                        c.Update(TickCount.Now);
                        return c;
                    }

                    // Create a new ChatBubble
                    if (CreateChatBubbleInstance != null)
                        c = CreateChatBubbleInstance(parent, owner, text);
                    else
                        c = new ChatBubble(parent, owner, text);

                    c.Update(TickCount.Now);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Error attempting to create ChatBubble for entity `{0}` with text `{1}`. Exception: {2}";
                    if (log.IsErrorEnabled)
                        log.ErrorFormat(errmsg, owner, text, ex);
                    Debug.Fail(string.Format(errmsg, owner, text, ex));
                }

                if (c == null)
                    return null;

                _chatBubbles.Add(owner, c);

                return c;
            }
        }
コード例 #2
0
ファイル: ChatBubble.cs プロジェクト: thepirateclub/netgore
        /// <summary>
        /// Creates a <see cref="ChatBubble"/> instance.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <param name="owner">The owner.</param>
        /// <param name="text">The text.</param>
        /// <returns>The <see cref="ChatBubble"/> instance, or null if any <see cref="Exception"/>s errors occured
        /// while trying to make the <see cref="ChatBubble"/> or any of the supplied parameters were invalid.</returns>
        public static ChatBubble Create(Control parent, Entity owner, string text)
        {
            if (parent == null)
            {
                const string errmsg = "ChatBubble.Create() failed - parent was null.";
                if (log.IsErrorEnabled)
                {
                    log.Error(errmsg);
                }
                Debug.Fail(errmsg);
                return(null);
            }

            if (owner == null)
            {
                const string errmsg = "ChatBubble.Create() failed - owner was null.";
                if (log.IsErrorEnabled)
                {
                    log.Error(errmsg);
                }
                Debug.Fail(errmsg);
                return(null);
            }

            if (string.IsNullOrEmpty(text))
            {
                const string errmsg = "ChatBubble.Create() failed - text was null or empty.";
                if (log.IsWarnEnabled)
                {
                    log.Warn(errmsg);
                }
                return(null);
            }

            lock (_chatBubblesSync)
            {
                ChatBubble c = null;

                try
                {
                    // If the ChatBubble already exists for the given Entity, reuse that one
                    if (_chatBubbles.TryGetValue(owner, out c) && c != null)
                    {
                        c._textControl.ChangeTextAndResize(text);
                        c._deathTime = (TickCount)(TickCount.Now + Lifespan);
                        c.Update(TickCount.Now);
                        return(c);
                    }

                    // Create a new ChatBubble
                    if (CreateChatBubbleInstance != null)
                    {
                        c = CreateChatBubbleInstance(parent, owner, text);
                    }
                    else
                    {
                        c = new ChatBubble(parent, owner, text);
                    }

                    c.Update(TickCount.Now);
                }
                catch (Exception ex)
                {
                    const string errmsg = "Error attempting to create ChatBubble for entity `{0}` with text `{1}`. Exception: {2}";
                    if (log.IsErrorEnabled)
                    {
                        log.ErrorFormat(errmsg, owner, text, ex);
                    }
                    Debug.Fail(string.Format(errmsg, owner, text, ex));
                }

                if (c == null)
                {
                    return(null);
                }

                _chatBubbles.Add(owner, c);

                return(c);
            }
        }