示例#1
0
        private void OnColorChanged(ColorHSV h)
        {
            var c = h.ToRGB();

            c = ColorStorage.CreateColor(c.R, c.G, c.B, 255);
            OnColorChanged(c, h);
        }
示例#2
0
        private void TextBoxHexTextChangedByUser(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_textFieldHex.Text))
            {
                return;
            }

            var s = "#" + _textFieldHex.Text;

            var color = ColorStorage.FromName(s);

            if (color == null)
            {
                return;
            }

            try
            {
                _textFieldHex.Tag = true;
                Color             = color.Value;
            }
            finally
            {
                _textFieldHex.Tag = false;
            }
        }
示例#3
0
        public static bool HasDefaultValue(this PropertyInfo property, object value)
        {
            var defaultAttribute = property.FindAttribute <DefaultValueAttribute>();

            object defaultAttributeValue = null;

            if (defaultAttribute != null)
            {
                defaultAttributeValue = defaultAttribute.Value;
                // If property is of Color type, than DefaultValueAttribute should contain its name or hex
                if (property.PropertyType == typeof(Color))
                {
                    defaultAttributeValue = ColorStorage.FromName(defaultAttributeValue.ToString()).Value;
                }

                if (property.PropertyType == typeof(string) &&
                    string.IsNullOrEmpty((string)defaultAttributeValue) &&
                    string.IsNullOrEmpty((string)value))
                {
                    // Skip empty/null string
                    return(true);
                }

                if (Equals(value, defaultAttributeValue))
                {
                    // Skip default
                    return(true);
                }
            }

            return(false);
        }
示例#4
0
        private void BuildUI()
        {
            var label1 = new Label();

            label1.Text                = "My Game";
            label1.Font                = AssetManager.Default.Load <SpriteFont>("fonts/arial64.fnt");
            label1.TextColor           = Color.LightBlue;
            label1.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;

            _menuItemStartNewGame      = new MenuItem();
            _menuItemStartNewGame.Text = "Start New Game";
            _menuItemStartNewGame.Id   = "_menuItemStartNewGame";

            _menuItemOptions      = new MenuItem();
            _menuItemOptions.Text = "Options";
            _menuItemOptions.Id   = "_menuItemOptions";

            _menuItemQuit      = new MenuItem();
            _menuItemQuit.Text = "Quit";
            _menuItemQuit.Id   = "_menuItemQuit";

            _mainMenu = new VerticalMenu();
            _mainMenu.HorizontalAlignment      = Myra.Graphics2D.UI.HorizontalAlignment.Center;
            _mainMenu.VerticalAlignment        = Myra.Graphics2D.UI.VerticalAlignment.Center;
            _mainMenu.LabelFont                = AssetManager.Default.Load <SpriteFont>("fonts/comicSans48.fnt");
            _mainMenu.LabelColor               = Color.Indigo;
            _mainMenu.SelectionHoverBackground = new SolidBrush(ColorStorage.FromName("#808000FF").Value);
            _mainMenu.SelectionBackground      = new SolidBrush(ColorStorage.FromName("#FFA500FF").Value);
            _mainMenu.LabelHorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
            _mainMenu.HoverIndexCanBeNull      = false;
            _mainMenu.Background               = new SolidBrush(ColorStorage.FromName("#00000000").Value);
            _mainMenu.Id = "_mainMenu";
            _mainMenu.Items.Add(_menuItemStartNewGame);
            _mainMenu.Items.Add(_menuItemOptions);
            _mainMenu.Items.Add(_menuItemQuit);

            var image1 = new Image();

            image1.Renderable        = AssetManager.Default.Load <TextureRegion>("images/LogoOnly_64px.png");
            image1.Left              = 10;
            image1.Top               = -10;
            image1.VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Bottom;

            var label2 = new Label();

            label2.Text = "Version 0.6";
            label2.Font = AssetManager.Default.Load <SpriteFont>("fonts/calibri32.fnt");
            label2.Left = -10;
            label2.Top  = -10;
            label2.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Right;
            label2.VerticalAlignment   = Myra.Graphics2D.UI.VerticalAlignment.Bottom;


            Background = new SolidBrush(ColorStorage.FromName("#C78100FF").Value);
            Widgets.Add(label1);
            Widgets.Add(_mainMenu);
            Widgets.Add(image1);
            Widgets.Add(label2);
        }
示例#5
0
        public void ShouldReturnNullColorByDefault()
        {
            // Given
            var storage = new ColorStorage();

            // When

            // Then
            storage.Color.ShouldBe(default(Color?));
        }
        private Color FromColorIndex(ColorStorage storage, uint colorRef)
        {
            COLORINDEX[] index = new COLORINDEX[1];
              var hr = storage.Utilities.GetEncodedIndex(colorRef, index);
              ErrorHandler.ThrowOnFailure(hr);

              uint rgb;
              hr = storage.Utilities.GetRGBOfIndex(index[0], out rgb);
              ErrorHandler.ThrowOnFailure(hr);
              return FromWin32(rgb);
        }
示例#7
0
        public static Stylesheet LoadFromSource(string stylesheetXml,
                                                Func <string, IBrush> textureGetter,
                                                Func <string, SpriteFont> fontGetter)
        {
            var xDoc = XDocument.Parse(stylesheetXml);

            var colors     = new Dictionary <string, Color>();
            var colorsNode = xDoc.Root.Element("Colors");

            if (colorsNode != null)
            {
                foreach (var el in colorsNode.Elements())
                {
                    var color = ColorStorage.FromName(el.Attribute("Value").Value);
                    if (color != null)
                    {
                        colors[el.Attribute(BaseContext.IdName).Value] = color.Value;
                    }
                }
            }

            Func <Type, string, object> resourceGetter = (t, s) =>
            {
                if (typeof(IBrush).IsAssignableFrom(t))
                {
                    return(textureGetter(s));
                }
                else if (t == typeof(SpriteFont))
                {
                    return(fontGetter(s));
                }

                throw new Exception(string.Format("Type {0} isn't supported", t.Name));
            };

            var result = new Stylesheet();

            var loadContext = new LoadContext
            {
                Namespaces = new[]
                {
                    typeof(WidgetStyle).Namespace
                },
                ResourceGetter      = resourceGetter,
                NodesToIgnore       = new HashSet <string>(new[] { "Designer", "Colors", "Fonts" }),
                LegacyClassNames    = LegacyClassNames,
                LegacyPropertyNames = LegacyPropertyNames,
                Colors = colors
            };

            loadContext.Load(result, xDoc.Root);

            return(result);
        }
示例#8
0
        public IBrush Load(AssetLoaderContext context, string assetName)
        {
            var color = ColorStorage.FromName(assetName);

            if (color == null)
            {
                throw new Exception(string.Format("Unable to resolve color '{0}'", assetName));
            }

            return(new SolidBrush(color.Value));
        }
示例#9
0
        public void ShouldStoreColor()
        {
            // Given
            var storage = new ColorStorage();

            // When
            storage.SetColor(Color.Error);

            // Then
            storage.Color.ShouldBe(Color.Error);
        }
示例#10
0
        public SolidBrush(string color)
        {
            var c = ColorStorage.FromName(color);

            if (c == null)
            {
                throw new ArgumentException(string.Format("Could not recognize color '{0}'", color));
            }

            Color = c.Value;
        }
示例#11
0
文件: Loader.cs 项目: rds1983/Jord
        public static Color EnsureColor(this JObject obj, string fieldName)
        {
            var s      = obj.EnsureString(fieldName);
            var result = ColorStorage.FromName(s);

            if (result == null)
            {
                RaiseError("Could not find color '{0}'.", s);
            }

            return(result.Value);
        }
示例#12
0
        public void ShouldResetToNullWhenLastReset()
        {
            // Given
            var storage = new ColorStorage();

            // When
            storage.SetColor(Color.Error);
            storage.ResetColor();

            // Then
            storage.Color.ShouldBe(default(Color?));
        }
示例#13
0
        public override void Close()
        {
            base.Close();

            for (var i = 0; i < ColorPickerPanel.UserColors.Length; ++i)
            {
                var colorDisplay = ColorPickerPanel.GetUserColorImage(i);
                var color        = colorDisplay.Color;
                var alpha        = (int)(colorDisplay.Opacity * 255);
                ColorPickerPanel.UserColors[i] = ColorStorage.CreateColor(color.R, color.G, color.B, alpha);
            }
        }
        public void Load(ColorStorage colorStorage)
        {
            ColorableItemInfo[] colors = new ColorableItemInfo[1];
              var hr = colorStorage.Storage.GetItem(classificationName, colors);
              ErrorHandler.ThrowOnFailure(hr);

              this.foregroundChanged = false;
              if ( colors[0].bForegroundValid != 0 ) {
            this.foreground = MapColor(colorStorage, colors[0].crForeground);
              }
              this.backgroundChanged = false;
              if ( colors[0].bBackgroundValid != 0 ) {
            this.background = MapColor(colorStorage, colors[0].crBackground);
              }
        }
示例#15
0
        public void Draw(RenderContext context, Rectangle dest, Color color)
        {
            if (color == Color.White)
            {
                TextureRegion.Draw(context, dest, Color);
            }
            else
            {
                var c = ColorStorage.CreateColor((int)(Color.R * color.R / 255.0f),
                                                 (int)(Color.G * color.G / 255.0f),
                                                 (int)(Color.B * color.B / 255.0f),
                                                 (int)(Color.A * color.A / 255.0f));

                TextureRegion.Draw(context, dest, c);
            }
        }
示例#16
0
        private void HexInputChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_inputHEX.Text) || _inputHEX.Text.Length < 6)
            {
                return;
            }

            var color = ColorStorage.FromName('#' + _inputHEX.Text);

            if (color != null)
            {
                _inputHEX.Tag = true;
                var c = color.Value;
                OnColorChanged(new Color(c.R, c.G, c.B));
                _inputHEX.Tag = false;
            }
        }
示例#17
0
        public Stylesheet Load(AssetLoaderContext context, string assetName)
        {
            var xml = context.Load <string>(assetName);

            var xDoc = XDocument.Parse(xml);
            var attr = xDoc.Root.Attribute("TextureRegionAtlas");

            if (attr == null)
            {
                throw new Exception("Mandatory attribute 'TextureRegionAtlas' doesnt exist");
            }

            var textureRegionAtlas = context.Load <TextureRegionAtlas>(attr.Value);

            // Load fonts
            var fonts     = new Dictionary <string, SpriteFont>();
            var fontsNode = xDoc.Root.Element("Fonts");

            foreach (var el in fontsNode.Elements())
            {
                var font = el.Attribute("File").Value;
                fonts[el.Attribute(BaseContext.IdName).Value] = context.Load <SpriteFont>(font);
            }

            return(Stylesheet.LoadFromSource(xml,
                                             name =>
            {
                TextureRegion region;

                if (!textureRegionAtlas.Regions.TryGetValue(name, out region))
                {
                    var color = ColorStorage.FromName(name);
                    if (color != null)
                    {
                        return new SolidBrush(color.Value);
                    }
                }
                else
                {
                    return region;
                }

                throw new Exception(string.Format("Could not find parse IBrush '{0}'", name));
            },
                                             name => fonts[name]));
        }
示例#18
0
        public void Draw(RenderContext context, Rectangle dest, Color color)
        {
            var white = DefaultAssets.WhiteRegion;

            if (color == Color.White)
            {
                white.Draw(context, dest, Color);
            }
            else
            {
                var c = ColorStorage.CreateColor((int)(Color.R * color.R / 255.0f),
                                                 (int)(Color.G * color.G / 255.0f),
                                                 (int)(Color.B * color.B / 255.0f),
                                                 (int)(Color.A * color.A / 255.0f));

                white.Draw(context, dest, c);
            }
        }
示例#19
0
        private void BuildUI()
        {
            var label1 = new Label();

            label1.Text                = "Soup";
            label1.TextColor           = ColorStorage.CreateColor(254, 57, 48, 255);
            label1.DisabledTextColor   = ColorStorage.CreateColor(64, 64, 64, 255);
            label1.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;

            _menuLoadScenario      = new MenuItem();
            _menuLoadScenario.Text = "Load Scenario";
            _menuLoadScenario.Id   = "_menuLoadScenario";

            _newScenario      = new MenuItem();
            _newScenario.Text = "New Scenario";
            _newScenario.Id   = "_newScenario";

            _quit      = new MenuItem();
            _quit.Text = "Quit";
            _quit.Id   = "_quit";

            var verticalMenu1 = new VerticalMenu();

            verticalMenu1.Background = new SolidBrush("#404040FF");
            verticalMenu1.Items.Add(_menuLoadScenario);
            verticalMenu1.Items.Add(_newScenario);
            verticalMenu1.Items.Add(_quit);

            _gameSpeedSlider          = new HorizontalSlider();
            _gameSpeedSlider.Value    = 50;
            _gameSpeedSlider.MinWidth = 200;
            _gameSpeedSlider.Id       = "_gameSpeedSlider";

            _gameSpeedPanel = new Panel();
            _gameSpeedPanel.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Right;
            _gameSpeedPanel.Border = new SolidBrush("#5BC6FAFF");
            _gameSpeedPanel.Id     = "_gameSpeedPanel";
            _gameSpeedPanel.Widgets.Add(_gameSpeedSlider);


            Widgets.Add(label1);
            Widgets.Add(verticalMenu1);
            Widgets.Add(_gameSpeedPanel);
        }
示例#20
0
        private void RgbInputChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(_inputRGB.Text))
            {
                return;
            }

            string[] st = _inputRGB.Text.Split(',');
            if (st.Length != 3)
            {
                return;
            }
            if (byte.TryParse(st[0], out byte r) && byte.TryParse(st[1], out byte g) && byte.TryParse(st[2], out byte b))
            {
                _inputRGB.Tag = true;
                OnColorChanged(ColorStorage.CreateColor(r, g, b));
                _inputRGB.Tag = false;
            }
        }
        public void Save(ColorStorage colorStorage)
        {
            if ( this.backgroundChanged || this.foregroundChanged ) {
            ColorableItemInfo[] colors = new ColorableItemInfo[1];
            var hr = colorStorage.Storage.GetItem(classificationName, colors);
            ErrorHandler.ThrowOnFailure(hr);

            if ( this.foregroundChanged ) {
              colors[0].crForeground = (uint)ColorTranslator.ToWin32(foreground);
              colors[0].bForegroundValid = 1;
            }
            if ( this.backgroundChanged ) {
              colors[0].crBackground = (uint)ColorTranslator.ToWin32(background);
              colors[0].bBackgroundValid = 1;
            }

            hr = colorStorage.Storage.SetItem(classificationName, colors);
            ErrorHandler.ThrowOnFailure(hr);
              }
        }
示例#22
0
        private void OnColorChanged(Color rgb, ColorHSV hsv)
        {
            if (!(bool)_inputRGB.Tag)
            {
                _inputRGB.Text = string.Format("{0},{1},{2}", rgb.R, rgb.G, rgb.B);
            }
            if (!(bool)_inputHSV.Tag)
            {
                _inputHSV.Text = string.Format("{0},{1},{2}", hsv.H, hsv.S, hsv.V);
            }
            if (!(bool)_inputHEX.Tag)
            {
                _inputHEX.Text = rgb.ToHexString().Substring(1, 6);
            }
            if (!(bool)_inputAlpha.Tag)
            {
                _inputAlpha.Text = DisplayAlpha.ToString();
            }
            if (!(bool)_sliderAlpha.Tag)
            {
                _sliderAlpha.Value = DisplayAlpha;
            }
            if (!(bool)_hsPicker.Tag)
            {
                _hsPicker.Top  = (int)(hsv.S / 200f * WheelHeight * Math.Sin(DegToRad * (-hsv.H + 180)));
                _hsPicker.Left = (int)(hsv.S / 200f * WheelHeight * Math.Cos(DegToRad * (-hsv.H + 180)));
            }
            if (!(bool)_vPicker.Tag)
            {
                _vPicker.Top = (int)(hsv.V / -100f * WheelHeight) + WheelHeight;
            }

            _colorWheel.Color = ColorStorage.CreateColor((int)(hsv.V * 255.0f / 100f), (int)(hsv.V * 255.0f / 100f), (int)(hsv.V * 255.0f / 100f));

            _colorDisplay.Color = rgb;

            colorHSV = hsv;
        }
示例#23
0
        public void Load(object obj, XElement el)
        {
            var type = obj.GetType();

            var baseObject = obj as BaseObject;

            List <PropertyInfo> complexProperties, simpleProperties;

            ParseProperties(type, out complexProperties, out simpleProperties);

            string newName;

            foreach (var attr in el.Attributes())
            {
                var propertyName = attr.Name.ToString();
                if (LegacyPropertyNames != null && LegacyPropertyNames.TryGetValue(propertyName, out newName))
                {
                    propertyName = newName;
                }

                var property = (from p in simpleProperties where p.Name == propertyName select p).FirstOrDefault();

                if (property != null)
                {
                    object value = null;

                    var propertyType = property.PropertyType;
                    if (propertyType.IsEnum)
                    {
                        value = Enum.Parse(propertyType, attr.Value);
                    }
                    else if (propertyType == typeof(Color) || propertyType == typeof(Color?))
                    {
                        Color color;
                        if (Colors != null && Colors.TryGetValue(attr.Value, out color))
                        {
                            value = color;
                        }
                        else
                        {
                            value = ColorStorage.FromName(attr.Value);
                            if (value == null)
                            {
                                throw new Exception(string.Format("Could not find parse color '{0}'", attr.Value));
                            }
                        }
                    }
                    else if ((typeof(IBrush).IsAssignableFrom(propertyType) ||
                              propertyType == typeof(SpriteFont)) &&
                             !string.IsNullOrEmpty(attr.Value) &&
                             ResourceGetter != null)
                    {
                        try
                        {
                            var texture = ResourceGetter(propertyType, attr.Value);
                            if (texture == null)
                            {
                                throw new Exception(string.Format("Could not find resource '{0}'", attr.Value));
                            }
                            value = texture;

                            if (baseObject != null)
                            {
                                baseObject.Resources[property.Name] = attr.Value;
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else if (propertyType == typeof(Thickness))
                    {
                        try
                        {
                            value = Thickness.FromString(attr.Value);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    else
                    {
                        if (propertyType.IsNullablePrimitive())
                        {
                            propertyType = propertyType.GetNullableType();
                        }

                        value = Convert.ChangeType(attr.Value, propertyType, CultureInfo.InvariantCulture);
                    }

                    property.SetValue(obj, value);
                }
                else
                {
                    // Stow away custom user attributes
                    if (propertyName.StartsWith(UserDataAttributePrefix) && baseObject != null)
                    {
                        baseObject.UserData.Add(propertyName, attr.Value);
                    }
                }
            }

            var contentProperty = (from p in complexProperties
                                   where p.FindAttribute <ContentAttribute>()
                                   != null select p).FirstOrDefault();

            foreach (var child in el.Elements())
            {
                var childName = child.Name.ToString();
                if (NodesToIgnore != null && NodesToIgnore.Contains(childName))
                {
                    continue;
                }

                var isProperty = false;
                if (childName.Contains("."))
                {
                    // Property name
                    var parts = childName.Split('.');
                    childName  = parts[1];
                    isProperty = true;
                }

                if (LegacyPropertyNames != null && LegacyPropertyNames.TryGetValue(childName, out newName))
                {
                    childName = newName;
                }

                // Find property
                var property = (from p in complexProperties where p.Name == childName select p).FirstOrDefault();
                if (property != null)
                {
                    do
                    {
                        var value  = property.GetValue(obj);
                        var asList = value as IList;
                        if (asList != null)
                        {
                            // List
                            foreach (var child2 in child.Elements())
                            {
                                var item = ObjectCreator(property.PropertyType.GenericTypeArguments[0], child2);
                                Load(item, child2);
                                asList.Add(item);
                            }

                            break;
                        }

                        var asDict = value as IDictionary;
                        if (asDict != null)
                        {
                            // Dict
                            foreach (var child2 in child.Elements())
                            {
                                var item = ObjectCreator(property.PropertyType.GenericTypeArguments[1], child2);
                                Load(item, child2);

                                var id = string.Empty;
                                if (child2.Attribute(IdName) != null)
                                {
                                    id = child2.Attribute(IdName).Value;
                                }

                                asDict[id] = item;
                            }

                            break;
                        }

                        if (property.SetMethod == null)
                        {
                            // Readonly
                            Load(value, child);
                        }
                        else
                        {
                            var newValue = ObjectCreator(property.PropertyType, child);
                            Load(newValue, child);
                            property.SetValue(obj, newValue);
                        }
                        break;
                    } while (true);
                }
                else
                {
                    // Property not found
                    if (isProperty)
                    {
                        throw new Exception(string.Format("Class {0} doesnt have property {1}", type.Name, childName));
                    }

                    // Should be widget class name then
                    var widgetName = childName;
                    if (LegacyClassNames != null && LegacyClassNames.TryGetValue(widgetName, out newName))
                    {
                        widgetName = newName;
                    }

                    Type itemType = null;
                    foreach (var ns in Namespaces)
                    {
                        itemType = Assembly.GetType(ns + "." + widgetName);
                        if (itemType != null)
                        {
                            break;
                        }
                    }
                    if (itemType != null)
                    {
                        var item = ObjectCreator(itemType, child);
                        Load(item, child);

                        if (contentProperty == null)
                        {
                            throw new Exception(string.Format("Class {0} lacks property marked with ContentAttribute", type.Name));
                        }

                        var containerValue = contentProperty.GetValue(obj);
                        var asList         = containerValue as IList;
                        if (asList != null)
                        {
                            // List
                            asList.Add(item);
                        }
                        else
                        {
                            // Simple
                            contentProperty.SetValue(obj, item);
                        }
                    }
                    else
                    {
                        throw new Exception(string.Format("Could not resolve tag '{0}'", widgetName));
                    }
                }
            }
        }
 private Color MapColor(ColorStorage storage, uint colorRef)
 {
     int type;
       var hr = storage.Utilities.GetColorType(colorRef, out type);
       switch ( (__VSCOLORTYPE)type ) {
     case __VSCOLORTYPE.CT_SYSCOLOR:
     case __VSCOLORTYPE.CT_RAW:
       return FromWin32(colorRef);
     case __VSCOLORTYPE.CT_COLORINDEX:
       return FromColorIndex(storage, colorRef);
     case __VSCOLORTYPE.CT_VSCOLOR:
       return FromVsColor(storage, colorRef);
     default:
       throw new InvalidOperationException("Invalid VS color type");
       }
 }
        private Color FromVsColor(ColorStorage storage, uint colorRef)
        {
            int vsColor;
              var hr = storage.Utilities.GetEncodedVSColor(colorRef, out vsColor);
              ErrorHandler.ThrowOnFailure(hr);

              uint rgb;
              hr = storage.Shell.GetVSSysColorEx(vsColor, out rgb);
              ErrorHandler.ThrowOnFailure(hr);

              return FromWin32(rgb);
        }
示例#26
0
        /// <summary>
        /// Converts HSV color system to RGB
        /// </summary>
        /// <returns></returns>
        public static Color ToRGB(this ColorHSV colorHSV)
        {
            float h = colorHSV.H;

            if (colorHSV.H == 360)
            {
                h = 359;
            }
            float s = colorHSV.S;
            float v = colorHSV.V;

            int   i;
            float f, p, q, t;

            h  = Math.Max(0.0f, Math.Min(360.0f, h));
            s  = Math.Max(0.0f, Math.Min(100.0f, s));
            v  = Math.Max(0.0f, Math.Min(100.0f, v));
            s /= 100;
            v /= 100;
            h /= 60;
            i  = (int)Math.Floor(h);
            f  = h - i;
            p  = v * (1 - s);
            q  = v * (1 - s * f);
            t  = v * (1 - s * (1 - f));

            int r, g, b;

            switch (i)
            {
            case 0:
                r = (int)Math.Round(255 * v);
                g = (int)Math.Round(255 * t);
                b = (int)Math.Round(255 * p);
                break;

            case 1:
                r = (int)Math.Round(255 * q);
                g = (int)Math.Round(255 * v);
                b = (int)Math.Round(255 * p);
                break;

            case 2:
                r = (int)Math.Round(255 * p);
                g = (int)Math.Round(255 * v);
                b = (int)Math.Round(255 * t);
                break;

            case 3:
                r = (int)Math.Round(255 * p);
                g = (int)Math.Round(255 * q);
                b = (int)Math.Round(255 * v);
                break;

            case 4:
                r = (int)Math.Round(255 * t);
                g = (int)Math.Round(255 * p);
                b = (int)Math.Round(255 * v);
                break;

            default:
                r = (int)Math.Round(255 * v);
                g = (int)Math.Round(255 * p);
                b = (int)Math.Round(255 * q);
                break;
            }

            return(ColorStorage.CreateColor((byte)r, (byte)g, (byte)b, (byte)255));
        }
示例#27
0
        internal ChunkInfo LayoutRow(int startIndex, int?width, bool parseCommands)
        {
            var r = new ChunkInfo
            {
                StartIndex = startIndex,
                LineEnd    = true
            };

            if (string.IsNullOrEmpty(_text))
            {
                return(r);
            }

            _stringBuilder.Clear();
            int?  lastBreakPosition = null;
            Point?lastBreakMeasure  = null;

            for (var i = r.StartIndex; i < _text.Length; ++i)
            {
                var c = _text[i];

                if (SupportsCommands && c == '\\')
                {
                    if (i < _text.Length - 2 && _text[i + 1] == 'c' && _text[i + 2] == '[')
                    {
                        // Find end
                        var startPos = i + 3;
                        var j        = _text.IndexOf(']', startPos);

                        if (j != -1)
                        {
                            // Found
                            if (i > r.StartIndex)
                            {
                                // Break right here, as next chunk has another color
                                r.LineEnd = false;
                                return(r);
                            }

                            if (parseCommands)
                            {
                                r.Color = ColorStorage.FromName(_text.Substring(startPos, j - startPos));
                            }

                            r.StartIndex = j + 1;
                            i            = j;
                            continue;
                        }
                    }
                }

                _stringBuilder.Append(c);

                var sz = Point.Zero;

                if (c != '\n')
                {
                    var v = Font.MeasureString(_stringBuilder);
                    sz = new Point((int)v.X, (int)v.Y);
                }
                else
                {
                    sz = new Point(r.X + NewLineWidth, Math.Max(r.Y, CrossEngineStuff.LineSpacing(_font)));

                    // Break right here
                    ++r.CharsCount;
                    r.X = sz.X;
                    r.Y = sz.Y;
                    break;
                }

                if (width != null && sz.X > width.Value)
                {
                    if (lastBreakPosition != null)
                    {
                        r.CharsCount = lastBreakPosition.Value - r.StartIndex;
                    }

                    if (lastBreakMeasure != null)
                    {
                        r.X = lastBreakMeasure.Value.X;
                        r.Y = lastBreakMeasure.Value.Y;
                    }

                    break;
                }

                if (char.IsWhiteSpace(c))
                {
                    lastBreakPosition = i + 1;
                    lastBreakMeasure  = sz;
                }

                ++r.CharsCount;
                r.X = sz.X;
                r.Y = sz.Y;
            }

            return(r);
        }
示例#28
0
文件: Stylesheet.cs 项目: haryck/Myra
        public static Stylesheet LoadFromSource(string stylesheetXml,
                                                TextureRegionAtlas textureRegionAtlas,
                                                Dictionary <string, SpriteFontBase> fonts)
        {
            var xDoc = XDocument.Parse(stylesheetXml);

            var colors     = new Dictionary <string, Color>();
            var colorsNode = xDoc.Root.Element("Colors");

            if (colorsNode != null)
            {
                foreach (var el in colorsNode.Elements())
                {
                    var color = ColorStorage.FromName(el.Attribute("Value").Value);
                    if (color != null)
                    {
                        colors[el.Attribute(BaseContext.IdName).Value] = color.Value;
                    }
                }
            }

            Func <Type, string, object> resourceGetter = (t, name) =>
            {
                if (typeof(IBrush).IsAssignableFrom(t))
                {
                    TextureRegion region;

                    if (!textureRegionAtlas.Regions.TryGetValue(name, out region))
                    {
                        var color = ColorStorage.FromName(name);
                        if (color != null)
                        {
                            return(new SolidBrush(color.Value));
                        }
                    }
                    else
                    {
                        return(region);
                    }

                    throw new Exception(string.Format("Could not find parse IBrush '{0}'", name));
                }
                else if (t == typeof(SpriteFontBase))
                {
                    return(fonts[name]);
                }

                throw new Exception(string.Format("Type {0} isn't supported", t.Name));
            };

            var result = new Stylesheet
            {
                Atlas = textureRegionAtlas,
                Fonts = fonts
            };

            var loadContext = new LoadContext
            {
                Namespaces = new[]
                {
                    typeof(WidgetStyle).Namespace
                },
                ResourceGetter      = resourceGetter,
                NodesToIgnore       = new HashSet <string>(new[] { "Designer", "Colors", "Fonts" }),
                LegacyClassNames    = LegacyClassNames,
                LegacyPropertyNames = LegacyPropertyNames,
                Colors = colors
            };

            loadContext.Load(result, xDoc.Root);

            return(result);
        }
        private void BuildUI()
        {
            _menuItemOpenFile              = new MenuItem();
            _menuItemOpenFile.Text         = "&Open";
            _menuItemOpenFile.ShortcutText = "Ctrl+O";
            _menuItemOpenFile.Id           = "_menuItemOpenFile";

            _menuItemSaveFile              = new MenuItem();
            _menuItemSaveFile.Text         = "&Save";
            _menuItemSaveFile.ShortcutText = "Ctrl+S";
            _menuItemSaveFile.Id           = "_menuItemSaveFile";

            _menuItemChooseFolder              = new MenuItem();
            _menuItemChooseFolder.Text         = "Choose Fol&der";
            _menuItemChooseFolder.ShortcutText = "Ctrl+D";
            _menuItemChooseFolder.Id           = "_menuItemChooseFolder";

            _menuItemChooseColor              = new MenuItem();
            _menuItemChooseColor.Text         = "Choose Co&lor";
            _menuItemChooseColor.ShortcutText = "Ctrl+L";
            _menuItemChooseColor.Id           = "_menuItemChooseColor";

            var menuSeparator1 = new MenuSeparator();

            _menuItemQuit              = new MenuItem();
            _menuItemQuit.Text         = "&Quit";
            _menuItemQuit.ShortcutText = "Ctrl+Q";
            _menuItemQuit.Id           = "_menuItemQuit";

            _menuFile      = new MenuItem();
            _menuFile.Text = "&File";
            _menuFile.Id   = "_menuFile";
            _menuFile.Items.Add(_menuItemOpenFile);
            _menuFile.Items.Add(_menuItemSaveFile);
            _menuFile.Items.Add(_menuItemChooseFolder);
            _menuFile.Items.Add(_menuItemChooseColor);
            _menuFile.Items.Add(menuSeparator1);
            _menuFile.Items.Add(_menuItemQuit);

            _menuItemCopy              = new MenuItem();
            _menuItemCopy.Text         = "&Copy";
            _menuItemCopy.ShortcutText = "Ctrl+Insert, Ctrl+C";
            _menuItemCopy.Id           = "_menuItemCopy";

            _menuItemPaste              = new MenuItem();
            _menuItemPaste.Text         = "&Paste";
            _menuItemPaste.ShortcutText = "Shift+Insert, Ctrl+V";
            _menuItemPaste.Id           = "_menuItemPaste";

            var menuSeparator2 = new MenuSeparator();

            _menuItemUndo              = new MenuItem();
            _menuItemUndo.Text         = "&Undo";
            _menuItemUndo.ShortcutText = "Ctrl+Z";
            _menuItemUndo.Id           = "_menuItemUndo";

            _menuItemRedo              = new MenuItem();
            _menuItemRedo.Text         = "&Redo";
            _menuItemRedo.ShortcutText = "Ctrl+Y";
            _menuItemRedo.Id           = "_menuItemRedo";

            _menuEdit      = new MenuItem();
            _menuEdit.Text = "&Edit";
            _menuEdit.Id   = "_menuEdit";
            _menuEdit.Items.Add(_menuItemCopy);
            _menuEdit.Items.Add(_menuItemPaste);
            _menuEdit.Items.Add(menuSeparator2);
            _menuEdit.Items.Add(_menuItemUndo);
            _menuEdit.Items.Add(_menuItemRedo);

            _menuItemAbout      = new MenuItem();
            _menuItemAbout.Text = "&About";
            _menuItemAbout.Id   = "_menuItemAbout";

            _menuHelp      = new MenuItem();
            _menuHelp.Text = "&Help";
            _menuHelp.Id   = "_menuHelp";
            _menuHelp.Items.Add(_menuItemAbout);

            _mainMenu = new HorizontalMenu();
            _mainMenu.VerticalAlignment = Myra.Graphics2D.UI.VerticalAlignment.Stretch;
            _mainMenu.Id = "_mainMenu";
            _mainMenu.Items.Add(_menuFile);
            _mainMenu.Items.Add(_menuEdit);
            _mainMenu.Items.Add(_menuHelp);

            var label1 = new Label();

            label1.Text = "Button:";

            _buttonSaveFile            = new ImageTextButton();
            _buttonSaveFile.Text       = "Save File";
            _buttonSaveFile.Padding    = new Thickness(8, 0);
            _buttonSaveFile.GridColumn = 1;
            _buttonSaveFile.Id         = "_buttonSaveFile";

            _textSaveFile            = new TextBox();
            _textSaveFile.GridColumn = 2;
            _textSaveFile.Id         = "_textSaveFile";

            var label2 = new Label();

            label2.Text    = "Another Button:";
            label2.GridRow = 1;

            _buttonOpenFile            = new ImageTextButton();
            _buttonOpenFile.Text       = "Open File";
            _buttonOpenFile.Padding    = new Thickness(8, 0);
            _buttonOpenFile.GridColumn = 1;
            _buttonOpenFile.GridRow    = 1;
            _buttonOpenFile.Id         = "_buttonOpenFile";

            _textOpenFile            = new TextBox();
            _textOpenFile.GridColumn = 2;
            _textOpenFile.GridRow    = 1;
            _textOpenFile.Id         = "_textOpenFile";

            var label3 = new Label();

            label3.Text    = "Blue Button:";
            label3.GridRow = 2;

            _buttonChooseFolder            = new ImageTextButton("blue");
            _buttonChooseFolder.Text       = "Choose Folder";
            _buttonChooseFolder.Padding    = new Thickness(8, 0);
            _buttonChooseFolder.GridColumn = 1;
            _buttonChooseFolder.GridRow    = 2;
            _buttonChooseFolder.Id         = "_buttonChooseFolder";

            _textChooseFolder            = new TextBox();
            _textChooseFolder.GridColumn = 2;
            _textChooseFolder.GridRow    = 2;
            _textChooseFolder.Id         = "_textChooseFolder";

            _textButtonLabel         = new Label();
            _textButtonLabel.Text    = "Text Button:";
            _textButtonLabel.GridRow = 3;
            _textButtonLabel.Id      = "_textButtonLabel";

            _buttonChooseColor            = new TextButton();
            _buttonChooseColor.Text       = "Choose Color";
            _buttonChooseColor.Padding    = new Thickness(8, 0);
            _buttonChooseColor.GridColumn = 1;
            _buttonChooseColor.GridRow    = 3;
            _buttonChooseColor.Id         = "_buttonChooseColor";

            var label4 = new Label();

            label4.Text    = "Image Button:";
            label4.GridRow = 4;

            _imageButton            = new ImageButton();
            _imageButton.Padding    = new Thickness(8, 0);
            _imageButton.GridColumn = 1;
            _imageButton.GridRow    = 4;
            _imageButton.Id         = "_imageButton";

            var checkBox1 = new CheckBox();

            checkBox1.Text           = "This is checkbox";
            checkBox1.ImageWidth     = 10;
            checkBox1.ImageHeight    = 10;
            checkBox1.GridRow        = 5;
            checkBox1.GridColumnSpan = 2;

            var label5 = new Label();

            label5.Text    = "Horizontal Slider:";
            label5.GridRow = 6;

            var horizontalSlider1 = new HorizontalSlider();

            horizontalSlider1.GridColumn     = 1;
            horizontalSlider1.GridRow        = 6;
            horizontalSlider1.GridColumnSpan = 2;

            var label6 = new Label();

            label6.Text    = "Combo Box:";
            label6.GridRow = 7;

            var listItem1 = new ListItem();

            listItem1.Text  = "Red";
            listItem1.Color = Color.Red;

            var listItem2 = new ListItem();

            listItem2.Text  = "Green";
            listItem2.Color = Color.Lime;

            var listItem3 = new ListItem();

            listItem3.Text  = "Blue";
            listItem3.Color = ColorStorage.CreateColor(0, 128, 255, 255);

            var comboBox1 = new ComboBox();

            comboBox1.Width          = 200;
            comboBox1.GridColumn     = 1;
            comboBox1.GridRow        = 7;
            comboBox1.GridColumnSpan = 2;
            comboBox1.Items.Add(listItem1);
            comboBox1.Items.Add(listItem2);
            comboBox1.Items.Add(listItem3);

            var label7 = new Label();

            label7.Text    = "Text Field:";
            label7.GridRow = 8;

            var textBox1 = new TextBox();

            textBox1.GridColumn     = 1;
            textBox1.GridRow        = 8;
            textBox1.GridColumnSpan = 2;

            var label8 = new Label();

            label8.Text    = "Spin Button:";
            label8.GridRow = 9;

            var spinButton1 = new SpinButton();

            spinButton1.Value      = 1;
            spinButton1.Width      = 100;
            spinButton1.GridColumn = 1;
            spinButton1.GridRow    = 9;

            var label9 = new Label();

            label9.Text    = "List Box:";
            label9.GridRow = 10;

            var listItem4 = new ListItem();

            listItem4.Text  = "Red";
            listItem4.Color = Color.Red;

            var listItem5 = new ListItem();

            listItem5.Text  = "Green";
            listItem5.Color = Color.Lime;

            var listItem6 = new ListItem();

            listItem6.Text  = "Blue";
            listItem6.Color = Color.Blue;

            var listBox1 = new ListBox();

            listBox1.Width          = 200;
            listBox1.GridColumn     = 1;
            listBox1.GridRow        = 10;
            listBox1.GridColumnSpan = 2;
            listBox1.Items.Add(listItem4);
            listBox1.Items.Add(listItem5);
            listBox1.Items.Add(listItem6);

            var label10 = new Label();

            label10.Text    = "Vertical Menu:";
            label10.GridRow = 11;

            var menuItem1 = new MenuItem();

            menuItem1.Text = "Start New Game";

            var menuItem2 = new MenuItem();

            menuItem2.Text = "Options";

            var menuItem3 = new MenuItem();

            menuItem3.Text = "Quit";

            var verticalMenu1 = new VerticalMenu();

            verticalMenu1.GridColumn = 1;
            verticalMenu1.GridRow    = 11;
            verticalMenu1.Items.Add(menuItem1);
            verticalMenu1.Items.Add(menuItem2);
            verticalMenu1.Items.Add(menuItem3);

            var label11 = new Label();

            label11.Text    = "Tree";
            label11.GridRow = 12;

            _gridRight = new Grid();
            _gridRight.ColumnSpacing        = 8;
            _gridRight.RowSpacing           = 8;
            _gridRight.DefaultRowProportion = new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            };
            _gridRight.ColumnsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            _gridRight.ColumnsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            _gridRight.ColumnsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            _gridRight.Id = "_gridRight";
            _gridRight.Widgets.Add(label1);
            _gridRight.Widgets.Add(_buttonSaveFile);
            _gridRight.Widgets.Add(_textSaveFile);
            _gridRight.Widgets.Add(label2);
            _gridRight.Widgets.Add(_buttonOpenFile);
            _gridRight.Widgets.Add(_textOpenFile);
            _gridRight.Widgets.Add(label3);
            _gridRight.Widgets.Add(_buttonChooseFolder);
            _gridRight.Widgets.Add(_textChooseFolder);
            _gridRight.Widgets.Add(_textButtonLabel);
            _gridRight.Widgets.Add(_buttonChooseColor);
            _gridRight.Widgets.Add(label4);
            _gridRight.Widgets.Add(_imageButton);
            _gridRight.Widgets.Add(checkBox1);
            _gridRight.Widgets.Add(label5);
            _gridRight.Widgets.Add(horizontalSlider1);
            _gridRight.Widgets.Add(label6);
            _gridRight.Widgets.Add(comboBox1);
            _gridRight.Widgets.Add(label7);
            _gridRight.Widgets.Add(textBox1);
            _gridRight.Widgets.Add(label8);
            _gridRight.Widgets.Add(spinButton1);
            _gridRight.Widgets.Add(label9);
            _gridRight.Widgets.Add(listBox1);
            _gridRight.Widgets.Add(label10);
            _gridRight.Widgets.Add(verticalMenu1);
            _gridRight.Widgets.Add(label11);

            var scrollViewer1 = new ScrollViewer();

            scrollViewer1.Content = _gridRight;

            var label12 = new Label();

            label12.Text = "Vertical Slider:";

            var verticalSlider1 = new VerticalSlider();

            verticalSlider1.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
            verticalSlider1.GridRow             = 1;

            var grid1 = new Grid();

            grid1.RowSpacing = 8;
            grid1.ColumnsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            grid1.RowsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            grid1.RowsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            grid1.Widgets.Add(label12);
            grid1.Widgets.Add(verticalSlider1);

            var label13 = new Label();

            label13.Text = "Progress Bars:";

            _horizontalProgressBar         = new HorizontalProgressBar();
            _horizontalProgressBar.GridRow = 1;
            _horizontalProgressBar.Id      = "_horizontalProgressBar";

            _verticalProgressBar = new VerticalProgressBar();
            _verticalProgressBar.HorizontalAlignment = Myra.Graphics2D.UI.HorizontalAlignment.Center;
            _verticalProgressBar.GridRow             = 2;
            _verticalProgressBar.Id = "_verticalProgressBar";

            var grid2 = new Grid();

            grid2.RowSpacing = 8;
            grid2.ColumnsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            grid2.RowsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            grid2.RowsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            grid2.RowsProportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            grid2.Widgets.Add(label13);
            grid2.Widgets.Add(_horizontalProgressBar);
            grid2.Widgets.Add(_verticalProgressBar);

            var verticalSplitPane1 = new VerticalSplitPane();

            verticalSplitPane1.Widgets.Add(grid1);
            verticalSplitPane1.Widgets.Add(grid2);

            var horizontalSplitPane1 = new HorizontalSplitPane();

            horizontalSplitPane1.Widgets.Add(scrollViewer1);
            horizontalSplitPane1.Widgets.Add(verticalSplitPane1);


            Spacing = 8;
            Proportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Auto,
            });
            Proportions.Add(new Proportion
            {
                Type = Myra.Graphics2D.UI.ProportionType.Fill,
            });
            Widgets.Add(_mainMenu);
            Widgets.Add(horizontalSplitPane1);
        }
示例#30
0
 public ClassificationList(ColorStorage colorStorage)
 {
     storage = colorStorage;
       classifications = new Dictionary<String, ClassificationColors>();
 }
示例#31
0
        internal ChunkInfo LayoutRow(int startIndex, int?width, bool parseCommands)
        {
            var r = new ChunkInfo {
                StartIndex = startIndex, LineEnd = true
            };

            if (string.IsNullOrEmpty(_text))
            {
                return(r);
            }

            _stringBuilder.Clear();
            int?  lastBreakPosition = null;
            Point?lastBreakMeasure  = null;

            for (var i = r.StartIndex; i < _text.Length; ++i)
            {
                var c = _text[i];

                if (char.IsHighSurrogate(c))
                {
                    _stringBuilder.Append(c);
                    ++r.CharsCount;
                    continue;
                }

                if (SupportsCommands && c == '\\')
                {
                    if (i < _text.Length - 2)
                    {
                        char commandChar = _text[i + 1];

                        if (_text[i + 2] == '[')
                        {
                            // Find parameter end
                            var parameterStartIndex = i + 3;
                            var parameterEndIndex   = _text.IndexOf(']', parameterStartIndex);

                            if (parameterEndIndex != -1)
                            {
                                // Found
                                if (i > r.StartIndex)
                                {
                                    // Break right here, as next chunk is a command block
                                    if (commandChar == SpriteCommandChar)
                                    {
                                        // sprites have width, will this one fit on our line?
                                        if (width != null)
                                        {
                                            var spriteId = SpriteChunk.GetSpriteId(_text, parameterStartIndex, parameterEndIndex);
                                            var size     = TrollskogIntegration.MeasureSprite(spriteId, _font);
                                            r.LineEnd = (r.X + size.X) > width.Value;
                                        }
                                    }
                                    else
                                    {
                                        r.LineEnd = false;
                                    }

                                    return(r);
                                }

                                if (parseCommands && commandChar == ColorCommandChar)
                                {
                                    r.Color = ColorStorage.FromName(_text.Substring(parameterStartIndex, parameterEndIndex - parameterStartIndex));
                                }
                                else if (commandChar == SpriteCommandChar)
                                {
                                    // Break because this is a sprite chunk
                                    var spriteId = SpriteChunk.GetSpriteId(_text, parameterStartIndex, parameterEndIndex);
                                    var size     = TrollskogIntegration.MeasureSprite(spriteId, _font);

                                    r.LineEnd    = parameterEndIndex == _text.Length - 1;
                                    r.X          = size.X;
                                    r.Y          = size.Y;
                                    r.SpriteId   = spriteId;
                                    r.CharsCount = parameterEndIndex - r.StartIndex + 1;
                                    return(r);
                                }
                            }

                            r.StartIndex = parameterEndIndex + 1;
                            i            = parameterEndIndex;
                            continue;
                        }

                        if (commandChar == NewlineCommandChar)
                        {
                            Point sz2 = new(r.X + NewLineWidth, Math.Max(r.Y, _font.FontSize));

                            // Break right here, consuming 2 chars '\\' and 'n'
                            r.SkipChars   = 2;
                            r.CharsCount += 2;
                            r.LineEnd     = true;
                            r.X           = sz2.X;
                            r.Y           = sz2.Y;
                            break;
                        }
                    }
                }

                _stringBuilder.Append(c);

                Point sz;
                if (c != '\n')
                {
                    var v = Font.MeasureString(_stringBuilder);
                    sz = new Point((int)v.X, _font.FontSize);
                }
                else
                {
                    sz = new Point(r.X + NewLineWidth, Math.Max(r.Y, _font.FontSize));

                    // Break right here
                    ++r.CharsCount;
                    r.X = sz.X;
                    r.Y = sz.Y;
                    break;
                }

                if (width != null && sz.X > width.Value)
                {
                    if (lastBreakPosition != null)
                    {
                        r.CharsCount = lastBreakPosition.Value - r.StartIndex;
                    }

                    if (lastBreakMeasure != null)
                    {
                        r.X = lastBreakMeasure.Value.X;
                        r.Y = lastBreakMeasure.Value.Y;
                    }

                    break;
                }

                if (char.IsWhiteSpace(c))
                {
                    lastBreakPosition = i + 1;
                    lastBreakMeasure  = sz;
                }

                ++r.CharsCount;
                r.X = sz.X;
                r.Y = sz.Y;
            }

            return(r);
        }