コード例 #1
0
        private GUIComponent CreateStringField(ISerializableEntity entity, SerializableProperty property, string value, int yPos, GUIComponent parent)
        {
            int boxHeight = 18;
            var editable  = property.GetAttribute <Editable>();

            boxHeight = (int)(Math.Ceiling(editable.MaxLength / 40.0f) * boxHeight);

            var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);

            label.ToolTip = property.GetAttribute <Editable>().ToolTip;
            GUITextBox propertyBox = new GUITextBox(new Rectangle(0, yPos, 250, boxHeight), Alignment.Right, "", parent);

            propertyBox.ToolTip = editable.ToolTip;
            propertyBox.Font    = GUI.SmallFont;

            propertyBox.Text           = value;
            propertyBox.OnEnterPressed = (textBox, text) =>
            {
                if (property.TrySetValue(text))
                {
                    TrySendNetworkUpdate(entity, property);
                    textBox.Text = (string)property.GetValue();
                    textBox.Deselect();
                }
                return(true);
            };

            return(propertyBox);
        }
コード例 #2
0
        private GUIComponent CreateEnumField(ISerializableEntity entity, SerializableProperty property, object value, int yPos, GUIComponent parent)
        {
            var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);

            label.ToolTip = property.GetAttribute <Editable>().ToolTip;
            GUIDropDown enumDropDown = new GUIDropDown(new Rectangle(180, yPos, 0, 18), "", "", Alignment.TopLeft, parent);

            enumDropDown.ToolTip = property.GetAttribute <Editable>().ToolTip;

            foreach (object enumValue in Enum.GetValues(value.GetType()))
            {
                var enumTextBlock = new GUITextBlock(new Rectangle(0, 0, 200, 25), enumValue.ToString(), "", enumDropDown);
                enumTextBlock.UserData = enumValue;
            }

            enumDropDown.OnSelected += (selected, val) =>
            {
                if (property.TrySetValue(val))
                {
                    TrySendNetworkUpdate(entity, property);
                }
                return(true);
            };

            enumDropDown.SelectItem(value);

            return(enumDropDown);
        }
コード例 #3
0
        private GUIComponent CreateFloatField(ISerializableEntity entity, SerializableProperty property, float value, int yPos, GUIComponent parent)
        {
            var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);

            label.ToolTip = property.GetAttribute <Editable>().ToolTip;
            GUINumberInput numberInput = new GUINumberInput(new Rectangle(180, yPos, 0, 18), "", GUINumberInput.NumberType.Float, Alignment.Left, parent);

            numberInput.ToolTip = property.GetAttribute <Editable>().ToolTip;
            numberInput.Font    = GUI.SmallFont;

            var editableAttribute = property.GetAttribute <Editable>();

            numberInput.MinValueFloat = editableAttribute.MinValueFloat;
            numberInput.MaxValueFloat = editableAttribute.MaxValueFloat;

            numberInput.FloatValue = value;

            numberInput.OnValueChanged += (numInput) =>
            {
                if (property.TrySetValue(numInput.FloatValue))
                {
                    TrySendNetworkUpdate(entity, property);
                }
            };

            return(numberInput);
        }
コード例 #4
0
        private GUIComponent CreateRectangleField(ISerializableEntity entity, SerializableProperty property, Rectangle value, int yPos, GUIComponent parent)
        {
            var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);

            label.ToolTip = property.GetAttribute <Editable>().ToolTip;
            for (int i = 0; i < 4; i++)
            {
                new GUITextBlock(new Rectangle(140 + i * 70, yPos, 100, 18), rectComponentLabels[i], "", Alignment.TopLeft, Alignment.CenterLeft, parent, false, GUI.SmallFont);
                GUINumberInput numberInput = new GUINumberInput(new Rectangle(160 + i * 70, yPos, 45, 18), "", GUINumberInput.NumberType.Int, Alignment.Left, parent);
                numberInput.Font = GUI.SmallFont;

                if (i == 0)
                {
                    numberInput.IntValue = value.X;
                }
                else if (i == 1)
                {
                    numberInput.IntValue = value.Y;
                }
                else if (i == 2)
                {
                    numberInput.IntValue = value.Width;
                }
                else
                {
                    numberInput.IntValue = value.Height;
                }

                int comp = i;
                numberInput.OnValueChanged += (numInput) =>
                {
                    Rectangle newVal = (Rectangle)property.GetValue();
                    if (comp == 0)
                    {
                        newVal.X = numInput.IntValue;
                    }
                    else if (comp == 1)
                    {
                        newVal.Y = numInput.IntValue;
                    }
                    else if (comp == 2)
                    {
                        newVal.Width = numInput.IntValue;
                    }
                    else
                    {
                        newVal.Height = numInput.IntValue;
                    }

                    if (property.TrySetValue(newVal))
                    {
                        TrySendNetworkUpdate(entity, property);
                    }
                };
            }

            return(label);
        }
コード例 #5
0
ファイル: StatusEffect.cs プロジェクト: Cavcode/Barotrauma
        private void ApplyToProperty(ISerializableEntity target, SerializableProperty property, object value, float deltaTime)
        {
            if (disableDeltaTime || setValue)
            {
                deltaTime = 1.0f;
            }

            Type type = value.GetType();

            if (type == typeof(float) ||
                (type == typeof(int) && property.GetValue(target) is float))
            {
                float floatValue = Convert.ToSingle(value) * deltaTime;

                if (!setValue)
                {
                    floatValue += (float)property.GetValue(target);
                }
                property.TrySetValue(target, floatValue);
            }
            else if (type == typeof(int) && value is int)
            {
                int intValue = (int)((int)value * deltaTime);
                if (!setValue)
                {
                    intValue += (int)property.GetValue(target);
                }
                property.TrySetValue(target, intValue);
            }
            else if (type == typeof(bool) && value is bool)
            {
                property.TrySetValue(target, (bool)value);
            }
            else if (type == typeof(string))
            {
                property.TrySetValue(target, (string)value);
            }
            else
            {
                DebugConsole.ThrowError("Couldn't apply value " + value.ToString() + " (" + type + ") to property \"" + property.Name + "\" (" + property.GetValue(target).GetType() + ")! "
                                        + "Make sure the type of the value set in the config files matches the type of the property.");
            }
        }
コード例 #6
0
        private GUIComponent CreateBoolField(ISerializableEntity entity, SerializableProperty property, bool value, int yPos, GUIComponent parent)
        {
            GUITickBox propertyTickBox = new GUITickBox(new Rectangle(10, yPos, 18, 18), property.Name, Alignment.Left, parent);

            propertyTickBox.Font     = GUI.SmallFont;
            propertyTickBox.Selected = value;
            propertyTickBox.ToolTip  = property.GetAttribute <Editable>().ToolTip;

            propertyTickBox.OnSelected = (tickBox) =>
            {
                if (property.TrySetValue(tickBox.Selected))
                {
                    TrySendNetworkUpdate(entity, property);
                }
                return(true);
            };

            return(propertyTickBox);
        }
コード例 #7
0
        public static Dictionary <string, SerializableProperty> DeserializeProperties(object obj, XElement element)
        {
            var properties = TypeDescriptor.GetProperties(obj.GetType()).Cast <PropertyDescriptor>();

            Dictionary <string, SerializableProperty> dictionary = new Dictionary <string, SerializableProperty>();

            foreach (var property in properties)
            {
                SerializableProperty objProperty = new SerializableProperty(property, obj);
                dictionary.Add(property.Name.ToLowerInvariant(), objProperty);

                //set the value of the property to the default value if there is one
                foreach (var ini in property.Attributes.OfType <Serialize>())
                {
                    objProperty.TrySetValue(ini.defaultValue);
                    break;
                }
            }

            if (element != null)
            {
                //go through all the attributes in the xml element
                //and set the value of the matching property if it is initializable
                foreach (XAttribute attribute in element.Attributes())
                {
                    SerializableProperty property = null;
                    if (!dictionary.TryGetValue(attribute.Name.ToString().ToLowerInvariant(), out property))
                    {
                        continue;
                    }
                    if (!property.Attributes.OfType <Serialize>().Any())
                    {
                        continue;
                    }
                    property.TrySetValue(attribute.Value);
                }
            }

            return(dictionary);
        }
コード例 #8
0
        private GUIComponent CreateColorField(ISerializableEntity entity, SerializableProperty property, Color value, int yPos, GUIComponent parent)
        {
            var label = new GUITextBlock(new Rectangle(0, yPos, 0, 18), property.Name, "", Alignment.TopLeft, Alignment.Left, parent, false, GUI.SmallFont);

            label.ToolTip = property.GetAttribute <Editable>().ToolTip;

            var colorBoxBack = new GUIFrame(new Rectangle(110 - 1, yPos - 1, 25 + 2, 18 + 2), Color.Black, Alignment.TopLeft, null, parent);
            var colorBox     = new GUIFrame(new Rectangle(110, yPos, 25, 18), value, Alignment.TopLeft, null, parent);

            for (int i = 0; i < 4; i++)
            {
                new GUITextBlock(new Rectangle(140 + i * 70, yPos, 100, 18), colorComponentLabels[i], "", Alignment.TopLeft, Alignment.CenterLeft, parent, false, GUI.SmallFont);
                GUINumberInput numberInput = new GUINumberInput(new Rectangle(160 + i * 70, yPos, 45, 18), "", GUINumberInput.NumberType.Int, Alignment.Left, parent);
                numberInput.MinValueInt = 0;
                numberInput.MaxValueInt = 255;

                if (i == 0)
                {
                    numberInput.IntValue = value.R;
                }
                else if (i == 1)
                {
                    numberInput.IntValue = value.G;
                }
                else if (i == 2)
                {
                    numberInput.IntValue = value.B;
                }
                else
                {
                    numberInput.IntValue = value.A;
                }

                numberInput.Font = GUI.SmallFont;

                int comp = i;
                numberInput.OnValueChanged += (numInput) =>
                {
                    Color newVal = (Color)property.GetValue();
                    if (comp == 0)
                    {
                        newVal.R = (byte)(numInput.IntValue);
                    }
                    else if (comp == 1)
                    {
                        newVal.G = (byte)(numInput.IntValue);
                    }
                    else if (comp == 2)
                    {
                        newVal.B = (byte)(numInput.IntValue);
                    }
                    else
                    {
                        newVal.A = (byte)(numInput.IntValue);
                    }

                    if (property.TrySetValue(newVal))
                    {
                        TrySendNetworkUpdate(entity, property);
                        colorBox.Color = newVal;
                    }
                };
            }

            return(label);
        }