using System;
//using System.Drawing;
using Gwen.Input;
namespace Gwen.Control
{
///
/// HSV hue selector.
///
public class ColorSlider : Base
{
private int m_SelectedDist;
private bool m_Depressed;
private Texture m_Texture;
///
/// Invoked when the selected color has been changed.
///
public event GwenEventHandler ColorChanged;
///
/// Initializes a new instance of the class.
///
/// Parent control.
public ColorSlider(Base parent)
: base(parent)
{
SetSize(32, 128);
MouseInputEnabled = true;
m_Depressed = false;
}
///
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
///
public override void Dispose()
{
if (m_Texture != null)
m_Texture.Dispose();
base.Dispose();
}
///
/// Renders the control using specified skin.
///
/// Skin to use.
protected override void Render(Skin.Base skin)
{
//Is there any way to move this into skin? Not for now, no idea how we'll "actually" render these
if (m_Texture == null)
{
byte[] pixelData = new byte[Width * Height * 4];
for (int y = 0; y < Height; y++)
{
Color c = GetColorAtHeight(y);
for (int x = 0; x < Width; x++)
{
pixelData[4 * (x + y * Width)] = c.R;
pixelData[4 * (x + y * Width) + 1] = c.G;
pixelData[4 * (x + y * Width) + 2] = c.B;
pixelData[4 * (x + y * Width) + 3] = c.A;
}
}
m_Texture = new Texture(skin.Renderer);
m_Texture.Width = Width;
m_Texture.Height = Height;
m_Texture.LoadRaw(Width, Height, pixelData);
}
skin.Renderer.DrawColor = Color.White;
skin.Renderer.DrawTexturedRect(m_Texture, new Rectangle(5, 0, Width-10, Height));
int drawHeight = m_SelectedDist - 3;
//Draw our selectors
skin.Renderer.DrawColor = Color.Black;
skin.Renderer.DrawFilledRect(new Rectangle(0, drawHeight + 2, Width, 1));
skin.Renderer.DrawFilledRect(new Rectangle(0, drawHeight, 5, 5));
skin.Renderer.DrawFilledRect(new Rectangle(Width - 5, drawHeight, 5, 5));
skin.Renderer.DrawColor = Color.White;
skin.Renderer.DrawFilledRect(new Rectangle(1, drawHeight + 1, 3, 3));
skin.Renderer.DrawFilledRect(new Rectangle(Width - 4, drawHeight + 1, 3, 3));
base.Render(skin);
}
///
/// Handler invoked on mouse click (left) event.
///
/// X coordinate.
/// Y coordinate.
/// If set to true mouse button is down.
protected override void OnMouseClickedLeft(int x, int y, bool down)
{
m_Depressed = down;
if (down)
InputHandler.MouseFocus = this;
else
InputHandler.MouseFocus = null;
OnMouseMoved(x, y, 0, 0);
}
///
/// Handler invoked on mouse moved event.
///
/// X coordinate.
/// Y coordinate.
/// X change.
/// Y change.
protected override void OnMouseMoved(int x, int y, int dx, int dy)
{
if (m_Depressed)
{
Point cursorPos = CanvasPosToLocal(new Point(x, y));
if (cursorPos.Y < 0)
cursorPos.Y = 0;
if (cursorPos.Y > Height)
cursorPos.Y = Height;
m_SelectedDist = cursorPos.Y;
if (ColorChanged != null)
ColorChanged.Invoke(this);
}
}
private Color GetColorAtHeight(int y)
{
float yPercent = y / (float)Height;
return Util.HSVToColor(yPercent * 360, 1, 1);
}
private void SetColor(Color color)
{
HSV hsv = color.ToHSV();
m_SelectedDist = (int)(hsv.h / 360 * Height);
if (ColorChanged != null)
ColorChanged.Invoke(this);
}
///
/// Selected color.
///
public Color SelectedColor { get { return GetColorAtHeight(m_SelectedDist); } set { SetColor(value); } }
}
}