Beispiel #1
0
        protected void InitImage(WidgetBackgroundStyle style, string texture, float rotation, Vector2 pivot, Margin padding)
        {
            m_imageInited = true;

            if (m_imageObject != null && m_lastTexture != texture) // TODO: check if image was not changed meaning no need to remove it
            {
                m_imageObject.Remove();
                m_imageObject = null;
            }

            m_lastTexture = texture;

            if (string.IsNullOrEmpty(texture))
            {
                if (style != WidgetBackgroundStyle.None)
                {
                    WindowController.Instance.LogMessage("Initing WidgetImage {0} without texture", this);
                }
                return;
            }

            if (m_imageObject == null)
            {
                ISprite textureSprite = WindowController.Instance.CreateSprite(texture, Vector2.Zero);
                if (textureSprite == null)
                {
                    WindowController.Instance.LogError("WidgetImage texture not found for sprite {0}", textureSprite);
                    return;
                }

                m_imageObject = new ImageObject(this, textureSprite);
            }

            Vector2 size   = new Vector2(Size.X - padding.Left - padding.Right, Size.Y - padding.Top - padding.Bottom);
            Vector2 start  = new Vector2(padding.Left, padding.Top);
            Vector2 center = start + size / 2;

            switch (style)
            {
            case WidgetBackgroundStyle.ImageFit:
            case WidgetBackgroundStyle.ImageTopLeft:
            {
                if (style == WidgetBackgroundStyle.ImageTopLeft)
                {
                    m_imageObject.Position = Vector2.Zero;
                }
                else
                {
                    m_imageObject.Position = center;
                }

                // Center and aspect fit. Good only for fixed size windows
                m_imageObject.Sprite.PivotShift = pivot;
                m_imageObject.Scale             = size.X / m_imageObject.Sprite.Size.X;
                m_imageObject.Rotation          = rotation;

                if (m_imageObject.Scale * m_imageObject.Sprite.Size.Y > size.Y)
                {
                    m_imageObject.Scale = size.Y / m_imageObject.Sprite.Size.Y;
                }

                break;
            }

            case WidgetBackgroundStyle.ImageStretch:
            {
                m_imageObject.Position = center;

                // Center and stretch
                m_imageObject.Sprite.PivotShift   = pivot;
                m_imageObject.Transform.FlatScale = size / m_imageObject.Sprite.Size;
                m_imageObject.Rotation            = rotation;
                break;
            }

            case WidgetBackgroundStyle.Image:
            {
                m_imageObject.Position = start;

                // Center and no stretch
                m_imageObject.Sprite.PivotShift = pivot;
                m_imageObject.Rotation          = rotation;
                break;
            }

            default:
                WindowController.Instance.LogError("Invalid background style {0} specified for WidgetImage", style);
                m_imageObject.Remove();
                m_imageObject = null;
                return;
            }

            if (Size.X <= 0 && Size.Y <= 0)
            {
                Size = size;
            }

            UpdateColor();
        }