Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:NewWidgets.Widgets.WidgetImage"/> class.
        /// Unlike <see cref="T:NewWidgets.Widgets.WidgetBackground"/> it does not allows tiling
        /// </summary>
        /// <param name="style">Style.</param>
        /// <param name="imageStyle">Image style.</param>
        /// <param name="image">Image.</param>
        public WidgetImage(WidgetStyleSheet style = default(WidgetStyleSheet), WidgetBackgroundStyle imageStyle = WidgetBackgroundStyle.ImageFit, string image = "")
            : base(style.IsEmpty ? DefaultStyle : style)
        {
            if (imageStyle != 0)
            {
                ImageStyle = imageStyle;
            }

            if (!string.IsNullOrEmpty(image))
            {
                Image = image;
            }
        }
Ejemplo n.º 2
0
        protected virtual void ApplyStyle(WidgetStyleSheet style)
        {
            m_backgroundInited = false;

            m_backgroundStyle   = style.BackgroundStyle;
            m_backgroundTexture = style.BackgroundTexture;
            m_backgroundScale   = style.BackgroundScale;
            m_backgroundPadding = style.BackgroundPadding;
            m_colorTint         = style.Color;
            m_alpha             = style.Opacity;

            m_clipContents = style.Clip;
            m_clipMargin   = style.ClipMargin;
        }
Ejemplo n.º 3
0
 internal WidgetStyleSheet()
 {
     m_clip              = false;
     m_size              = new Vector2(0, 0);
     m_name              = "Default";
     m_parameters        = new Dictionary <string, string>();
     m_color             = 0xffffff;
     m_backgroundPivot   = new Vector2(0.5f, 0.5f);
     m_opacity           = 1.0f;
     m_clipMargin        = new Margin(2);
     m_backgroundPadding = new Margin(0);
     m_font              = WidgetManager.MainFont;
     m_fontSize          = 1.0f;
     m_backgroundScale   = 1.0f;
     m_backgroundStyle   = WidgetBackgroundStyle.None;
     m_backgroundTexture = "";
 }
Ejemplo n.º 4
0
        public Widget(WidgetStyleSheet style)
            : base(null)
        {
            m_background = new WindowObjectArray <WindowObject>();

            m_backgroundStyle   = style.BackgroundStyle;
            m_backgroundTexture = style.BackgroundTexture;
            m_backgroundScale   = style.BackgroundScale;
            m_backgroundPivot   = style.BackgroundPivot;
            m_backgroundInited  = false;
            m_backgroundDepth   = style.BackgroundDepth;
            m_backgroundPadding = style.BackgroundPadding;
            m_colorTint         = style.Color;
            m_alpha             = style.Opacity;

            m_clipContents = style.Clip;
            m_clipMargin   = style.ClipMargin;

            m_style = style;
        }
Ejemplo n.º 5
0
        internal WidgetStyleSheet(WidgetStyleSheet parent)
        {
            m_clip       = parent.Clip;
            m_size       = parent.Size;
            m_name       = parent.Name + "_" + GetHashCode();
            m_parameters = new Dictionary <string, string>();
            foreach (KeyValuePair <string, string> pair in parent.m_parameters)
            {
                m_parameters.Add(pair.Key, pair.Value);
            }

            m_color             = parent.Color;
            m_opacity           = parent.Opacity;
            m_clipMargin        = parent.ClipMargin;
            m_font              = parent.Font;
            m_fontSize          = parent.FontSize;
            m_backgroundScale   = parent.BackgroundScale;
            m_backgroundDepth   = parent.BackgroundDepth;
            m_backgroundStyle   = parent.BackgroundStyle;
            m_backgroundTexture = parent.BackgroundTexture;
            m_backgroundPivot   = parent.BackgroundPivot;
            m_backgroundPadding = parent.m_backgroundPadding;
            m_padding           = parent.Padding;
        }
Ejemplo n.º 6
0
        protected void InitBackground(WidgetBackgroundStyle style, string texture, float scale, float rotation, Vector2 backgroundPivot, Margin backgroundPadding)
        {
            m_backgroundInited = true;

            foreach (WindowObject obj in m_background.List)
            {
                obj.Remove();
            }

            m_background.Clear();

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

            ISprite textureSprite = WindowController.Instance.CreateSprite(texture, Vector2.Zero);

            if (textureSprite == null)
            {
                WindowController.Instance.LogError("Widget texture not found for sprite {0}", textureSprite);
                return;
            }

            Vector2 backSize   = new Vector2(Size.X - backgroundPadding.Left - backgroundPadding.Right, Size.Y - backgroundPadding.Top - backgroundPadding.Bottom);
            Vector2 backStart  = new Vector2(backgroundPadding.Left, backgroundPadding.Top);
            Vector2 backCenter = backStart + backSize / 2;

            switch (style)
            {
            case WidgetBackgroundStyle.ImageFit:
            case WidgetBackgroundStyle.ImageTopLeft:
            {
                ImageObject background = new ImageObject(this, textureSprite);

                if (style == WidgetBackgroundStyle.ImageTopLeft)
                {
                    background.Position = Vector2.Zero;
                }
                else
                {
                    background.Position = backCenter;
                }

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

                if (background.Scale * background.Sprite.Size.Y > backSize.Y)
                {
                    background.Scale = backSize.Y / background.Sprite.Size.Y;
                }

                m_background.Add(background);
                break;
            }

            case WidgetBackgroundStyle.ImageStretch:
            {
                ImageObject background = new ImageObject(this, textureSprite);

                background.Position = backCenter;

                // Center and stretch
                background.Sprite.PivotShift   = backgroundPivot;
                background.Transform.FlatScale = backSize / background.Sprite.Size;
                background.Rotation            = rotation;

                m_background.Add(background);
                break;
            }

            case WidgetBackgroundStyle.ImageTiled:
            {
                ImageObject background = new ImageObject(this, textureSprite);

                Vector2 size = background.Sprite.Size * scale;

                // Tile unstretched
                int countX = (int)Math.Ceiling(backSize.X / size.X);
                int countY = (int)Math.Ceiling(backSize.Y / size.Y);

                for (int x = 0; x < countX; x++)
                {
                    for (int y = 0; y < countY; y++)
                    {
                        ImageObject image;
                        if (x == 0 && y == 0)
                        {
                            image = background;
                        }
                        else
                        {
                            image = new ImageObject(this, WindowController.Instance.CreateSprite(texture, size * new Vector2(x, y)));
                        }

                        image.Scale = scale;
                        m_background.Add(image);
                    }
                }
                break;
            }

            case WidgetBackgroundStyle.Image:
            {
                ImageObject background = new ImageObject(this, textureSprite);
                background.Position = backStart;

                // Center and no stretch
                background.Sprite.PivotShift = backgroundPivot;
                background.Rotation          = rotation;

                m_background.Add(background);
                break;
            }

            case WidgetBackgroundStyle.NineImage:
            {
                Vector2 shift = (-new Vector2(0.5f, 0.5f) + backgroundPivot) * Size;

                float x = 0;
                float y = 0;

                Vector2[] scales    = new Vector2[9];
                Vector2[] positions = new Vector2[9];
                bool      inited    = false;

                for (int i = 0; i < 9; i++)
                {
                    if (i % 3 == 0)
                    {
                        x = 0;
                    }

                    ISprite sprite = WindowController.Instance.CreateSprite(texture, new Vector2(x, y));
                    sprite.Frame = i;

                    if (!inited)
                    {
                        inited   = true;
                        backSize = sprite.Size * scale;

                        // TODO: right now all sizes are measured as 1/3 of image width or heigth. We need to think of using different sizes to make it flexible

                        float width  = (backSize.X / 3);
                        float height = (backSize.Y / 3);

                        scales    = new Vector2[9];
                        scales[0] = scales[2] = scales[6] = scales[8] = new Vector2(1.0f, 1.0f);
                        scales[1] = scales[7] = new Vector2((Size.X - width * 2) / width, 1.0f);
                        scales[3] = scales[5] = new Vector2(1.0f, (Size.Y - height * 2) / height);
                        scales[4] = new Vector2(scales[1].X, scales[3].Y);

                        positions    = new Vector2[9];
                        positions[0] = new Vector2(0, 0);
                        positions[1] = new Vector2(width, 0);
                        positions[2] = new Vector2(Size.X - width, 0);
                        positions[3] = new Vector2(0, height);
                        positions[4] = new Vector2(width, height);
                        positions[5] = new Vector2(Size.X - width, height);
                        positions[6] = new Vector2(0, Size.Y - height);
                        positions[7] = new Vector2(width, Size.Y - height);
                        positions[8] = new Vector2(Size.X - width, Size.Y - height);
                    }

                    ImageObject background = new ImageObject(this, sprite);
                    background.Position            = shift + positions[i];
                    background.Transform.FlatScale = scales[i] * scale;

                    m_background.Add(background);
                }
                break;
            }

            case WidgetBackgroundStyle.ThreeImage:
            {
                Vector2 shift = (-new Vector2(0.5f, 0.5f) + backgroundPivot) * Size;

                float x = 0;
                float y = 0;

                Vector2[] scales    = null;
                Vector2[] positions = null;

                for (int i = 0; i < 3; i++)
                {
                    ImageObject background = new ImageObject(this, WindowController.Instance.CreateSprite(texture, new Vector2(x, y)));
                    background.Sprite.Frame = i;

                    if (scales == null)
                    {
                        scale = Size.Y / background.Sprite.Size.Y;

                        backSize = background.Sprite.Size * scale;

                        // TODO: right now all sizes are measured as 1/3 of image width or heigth. We need to think of using different sizes to make it flexible

                        float width = (backSize.X / 3);

                        scales    = new Vector2[3];
                        scales[0] = scales[2] = new Vector2(1.0f, 1.0f);
                        scales[1] = new Vector2((Size.X - width * 2) / width, 1.0f);

                        positions    = new Vector2[9];
                        positions[0] = new Vector2(0, 0);
                        positions[1] = new Vector2(width, 0);
                        positions[2] = new Vector2(Size.X - width, 0);
                    }

                    background.Position            = shift + positions[i];
                    background.Transform.FlatScale = scales[i] * scale;

                    m_background.Add(background);
                    background.Parent = this;
                }
                break;
            }
            }
        }
Ejemplo n.º 7
0
        internal WidgetStyleSheet(string name, WidgetStyleSheet parent, XmlNode node)
            : this(parent)
        {
            foreach (XmlNode element in node.ChildNodes)
            {
                try
                {
                    string value = element.InnerText;
                    if (string.IsNullOrEmpty(value) && element.Attributes.GetNamedItem("value") != null)
                    {
                        value = element.Attributes.GetNamedItem("value").Value;
                    }

                    if (value == null)
                    {
                        value = string.Empty;
                    }
                    else
                    {
                        value = value.Trim('\r', '\n', '\t', ' ');
                    }

                    switch (element.Name)
                    {
                    case "back_style":
                        m_backgroundStyle = (WidgetBackgroundStyle)Enum.Parse(typeof(WidgetBackgroundStyle), value);
                        break;

                    case "back_depth":
                        m_backgroundDepth = (WidgetBackgroundDepth)Enum.Parse(typeof(WidgetBackgroundDepth), value);
                        break;

                    case "back_image":
                        m_backgroundTexture = value;
                        break;

                    case "back_scale":
                        m_backgroundScale = FloatParse(value);
                        break;

                    case "back_pivot":
                    {
                        string[] values = value.Split(';');
                        float    x      = FloatParse(values[0]);
                        float    y      = FloatParse(values[1]);
                        m_backgroundPivot = new Vector2(x, y);
                        break;
                    }

                    case "font":
                        m_font = WidgetManager.GetFont(value);
                        break;

                    case "font_size":
                        m_fontSize = FloatParse(value);
                        break;

                    case "clip":
                        m_clip = bool.Parse(value);
                        break;

                    case "color":
                        m_color = ColorParse(value);
                        break;

                    case "opacity":
                        m_opacity = FloatParse(value);
                        break;

                    case "size":
                        m_size = Vector2Parse(value);
                        break;

                    case "clip_margin":
                        m_clipMargin = MarginParse(value);
                        break;

                    case "padding":
                        m_padding = MarginParse(value);
                        break;

                    case "back_padding":
                        m_backgroundPadding = MarginParse(value);
                        break;

                    default:
                        m_parameters[element.Name] = value;
                        break;
                    }
                }
                catch (Exception ex)
                {
                    WindowController.Instance.LogError("Error parsing style {0}, element {1}: {2}", name, element.Name, ex);
                    throw;
                }
            }
        }
Ejemplo n.º 8
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();
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:NewWidgets.Widgets.WidgetImage"/> class.
 /// Unlike <see cref="T:NewWidgets.Widgets.WidgetBackground"/> it does not allows tiling
 /// </summary>
 /// <param name="imageStyle">Image style.</param>
 /// <param name="image">Image.</param>
 public WidgetImage(WidgetBackgroundStyle imageStyle = WidgetBackgroundStyle.ImageFit, string image = "")
     : this(default(WidgetStyleSheet), string.IsNullOrEmpty(image)? 0 : imageStyle, image)
 {
 }
Ejemplo n.º 10
0
 public WidgetImage(WidgetBackgroundStyle style, string image)
     : base(WidgetManager.DefaultWidgetStyle)
 {
     BackgroundTexture = image;
     BackgroundStyle   = style;
 }