using System; using Gwen.ControlInternal; using Gwen.Input; namespace Gwen.Control.Property { /// /// Color property. /// public class Color : Text { protected readonly ColorButton m_Button; /// /// Initializes a new instance of the class. /// /// Parent control. public Color(Control.Base parent) : base(parent) { m_Button = new ColorButton(m_TextBox); m_Button.Dock = Pos.Right; m_Button.Width = 20; m_Button.Margin = new Margin(1, 1, 1, 2); m_Button.Clicked += OnButtonPressed; } /// /// Color-select button press handler. /// /// Event source. protected virtual void OnButtonPressed(Control.Base control) { Menu menu = new Menu(GetCanvas()); menu.SetSize(256, 180); menu.DeleteOnClose = true; menu.IconMarginDisabled = true; HSVColorPicker picker = new HSVColorPicker(menu); picker.Dock = Pos.Fill; picker.SetSize(256, 128); String[] split = m_TextBox.Text.Split(' '); picker.SetColor(GetColorFromText(), false, true); picker.ColorChanged += OnColorChanged; menu.Open(Pos.Right | Pos.Top); } /// /// Color changed handler. /// /// Event source. protected virtual void OnColorChanged(Control.Base control) { HSVColorPicker picker = control as HSVColorPicker; SetTextFromColor(picker.SelectedColor); DoChanged(); } /// /// Property value. /// public override string Value { get { return m_TextBox.Text; } set { base.Value = value; } } /// /// Sets the property value. /// /// Value to set. /// Determines whether to fire "value changed" event. public override void SetValue(string value, bool fireEvents = false) { m_TextBox.SetText(value, fireEvents); } /// /// Indicates whether the property value is being edited. /// public override bool IsEditing { get { return m_TextBox == InputHandler.KeyboardFocus; } } private void SetTextFromColor(Gwen.Color color) { m_TextBox.Text = String.Format("{0} {1} {2}", color.R, color.G, color.B); } private Gwen.Color GetColorFromText() { String[] split = m_TextBox.Text.Split(' '); byte red = 0; byte green = 0; byte blue = 0; byte alpha = 255; if (split.Length > 0 && split[0].Length > 0) { Byte.TryParse(split[0], out red); } if (split.Length > 1 && split[1].Length > 0) { Byte.TryParse(split[1], out green); } if (split.Length > 2 && split[2].Length > 0) { Byte.TryParse(split[2], out blue); } return Gwen.Color.FromArgb(alpha, red, green, blue); } protected override void DoChanged() { base.DoChanged(); m_Button.Color = GetColorFromText(); } } }