Originally Posted by
PoeHudContrib
I didn't implement it exactly as threetowers had it in his snippet, but if you could take a look and let me know how you would have done it differently I would appreciate the learning experience.
Code:
public class SettingColor : SettingsBlock
{
public SettingColor(string name, int Red = 255, int Green = 255, int Blue = 255) : base(name)
{
this.Red.Default = Red;
this.Green.Default = Green;
this.Blue.Default = Blue;
}
public Color FromRGB { get { return Color.FromArgb(this.Red, this.Green, this.Blue); } }
public SettingIntRange Red = new SettingIntRange("Red", 0, 255);
public SettingIntRange Green = new SettingIntRange("Green", 0, 255);
public SettingIntRange Blue = new SettingIntRange("Blue", 0, 255);
}
This is what usually happens when a project loses its architect.
Code:
private void createChildMenus(BooleanButton parent, SettingsBlock module)
{
foreach (ISetting setting in module.Members.Where(setting => !String.IsNullOrWhiteSpace(setting.Key)))
{
if( setting is Setting<bool>)
parent.AddChild(new BooleanButton(Settings, setting.Key, setting as Setting<bool>));
else if (setting is Setting<Color>)
parent.AddChild(new ColorPicker(Settings, setting.Key, setting as Setting<Color>));
(continued)
Code:
namespace PoeHUD.Hud.Menu
{
public class ColorPicker : MenuItem
{
private int barBeingDragged = -1;
private Color value;
private readonly string text;
private readonly Setting<Color> setting;
public override int Height { get { return base.Height * 3 + 5; } }
public ColorPicker(Menu.MenuSettings menuSettings, string text, Setting<Color> setting) : base(menuSettings)
{
this.text = text;
this.value = setting.Value;
this.setting = setting;
}
private void CalcValue(int x)
{
int lBound = base.Bounds.X + 5;
int rBound = base.Bounds.X + base.Bounds.W - 5;
float num3 = x <= lBound ? 0f : (x >= rBound ? 1f : (float) (x - lBound)/(rBound - lBound));
switch (barBeingDragged)
{
case 0:
this.value = Color.FromArgb((int)Math.Round(num3 * 255), value.G, value.B);
break;
case 1:
this.value = Color.FromArgb(value.R, (int)Math.Round(num3 * 255), value.B);
break;
case 2:
this.value = Color.FromArgb(value.R, value.G, (int)Math.Round(num3 * 255));
break;
}
setting.Value = value;
}
protected override bool TestBounds(Vec2 pos)
{
return this.barBeingDragged >= 0 || base.TestBounds(pos);
}
(write the rest here, using IntPicker as an example for Render and HandleEvent methods, Yet you'll draw 3 bars, detect which one is being dragged, add a color preview box to see the resulting color immediatelly)