1 /*
2 	GWEN
3 	Copyright (c) 2010 Facepunch Studios
4 	See license in Gwen.h
5 */
6 
7 #include "Gwen/Controls/HSVColorPicker.h"
8 #include "Gwen/Controls/ColorControls.h"
9 #include "Gwen/Controls/ColorPicker.h"
10 #include "Gwen/Controls/TextBox.h"
11 #include "Gwen/Controls/Label.h"
12 #include "Gwen/Controls/PanelListPanel.h"
13 #include "Gwen/Utility.h"
14 
15 using namespace Gwen;
16 using namespace Gwen::Controls;
17 
GWEN_CONTROL_CONSTRUCTOR(HSVColorPicker)18 GWEN_CONTROL_CONSTRUCTOR(HSVColorPicker)
19 {
20 	SetMouseInputEnabled(true);
21 	SetSize(256, 128);
22 	SetCacheToTexture();
23 
24 	m_LerpBox = new Gwen::Controls::ColorLerpBox(this);
25 	m_LerpBox->onSelectionChanged.Add(this, &HSVColorPicker::ColorBoxChanged);
26 	m_LerpBox->SetPos(5, 5);
27 
28 	m_ColorSlider = new Gwen::Controls::ColorSlider(this);
29 	m_ColorSlider->SetPos(m_LerpBox->Width() + 15, 5);
30 	m_ColorSlider->onSelectionChanged.Add(this, &HSVColorPicker::ColorSliderChanged);
31 
32 	m_After = new Gwen::ControlsInternal::ColorDisplay(this);
33 	m_After->SetSize(48, 24);
34 	m_After->SetPos(m_ColorSlider->X() + m_ColorSlider->Width() + 15, 5);
35 
36 	m_Before = new Gwen::ControlsInternal::ColorDisplay(this);
37 	m_Before->SetSize(48, 24);
38 	m_Before->SetPos(m_After->X(), 28);
39 
40 	int x = m_Before->X();
41 	int y = m_Before->Y() + 30;
42 
43 	{
44 		Label* label = new Label(this);
45 		label->SetText(L"R:");
46 		label->SizeToContents();
47 		label->SetPos(x, y);
48 
49 		TextBoxNumeric* numeric = new TextBoxNumeric(this);
50 		numeric->SetName("RedBox");
51 		numeric->SetPos(x + 15, y - 1);
52 		numeric->SetSize(26, 16);
53 		numeric->SetSelectAllOnFocus(true);
54 		numeric->onTextChanged.Add(this, &HSVColorPicker::NumericTyped);
55 	}
56 
57 	y += 20;
58 
59 	{
60 		Label* label = new Label(this);
61 		label->SetText(L"G:");
62 		label->SizeToContents();
63 		label->SetPos(x, y);
64 
65 		TextBoxNumeric* numeric = new TextBoxNumeric(this);
66 		numeric->SetName("GreenBox");
67 		numeric->SetPos(x + 15, y - 1);
68 		numeric->SetSize(26, 16);
69 		numeric->SetSelectAllOnFocus(true);
70 		numeric->onTextChanged.Add(this, &HSVColorPicker::NumericTyped);
71 	}
72 
73 	y += 20;
74 
75 	{
76 		Label* label = new Label(this);
77 		label->SetText(L"B:");
78 		label->SizeToContents();
79 		label->SetPos(x, y);
80 
81 		TextBoxNumeric* numeric = new TextBoxNumeric(this);
82 		numeric->SetName("BlueBox");
83 		numeric->SetPos(x + 15, y - 1);
84 		numeric->SetSize(26, 16);
85 		numeric->SetSelectAllOnFocus(true);
86 		numeric->onTextChanged.Add(this, &HSVColorPicker::NumericTyped);
87 	}
88 }
89 
NumericTyped(Gwen::Controls::Base * control)90 void HSVColorPicker::NumericTyped(Gwen::Controls::Base* control)
91 {
92 	TextBoxNumeric* box = control->DynamicCastTextBoxNumeric();
93 	if (!box) return;
94 
95 	if (box->GetText() == L"") return;
96 
97 	int textValue = atoi(Gwen::Utility::UnicodeToString(box->GetText()).c_str());
98 	if (textValue < 0) textValue = 0;
99 	if (textValue > 255) textValue = 255;
100 
101 	Gwen::Color newColor = GetColor();
102 
103 	if (box->GetName().find("Red") != Gwen::String::npos)
104 	{
105 		newColor.r = textValue;
106 	}
107 	else if (box->GetName().find("Green") != Gwen::String::npos)
108 	{
109 		newColor.g = textValue;
110 	}
111 	else if (box->GetName().find("Blue") != Gwen::String::npos)
112 	{
113 		newColor.b = textValue;
114 	}
115 	else if (box->GetName().find("Alpha") != Gwen::String::npos)
116 	{
117 		newColor.a = textValue;
118 	}
119 
120 	SetColor(newColor);
121 }
122 
UpdateControls(Gwen::Color color)123 void HSVColorPicker::UpdateControls(Gwen::Color color)
124 {
125 	TextBoxNumeric* redBox = FindChildByName("RedBox", false)->DynamicCastTextBoxNumeric();
126 	if (redBox) redBox->SetText(Gwen::Utility::ToString((int)color.r), false);
127 
128 	TextBoxNumeric* greenBox = FindChildByName("GreenBox", false)->DynamicCastTextBoxNumeric();
129 	if (greenBox) greenBox->SetText(Gwen::Utility::ToString((int)color.g), false);
130 
131 	TextBoxNumeric* blueBox = FindChildByName("BlueBox", false)->DynamicCastTextBoxNumeric();
132 	if (blueBox) blueBox->SetText(Gwen::Utility::ToString((int)color.b), false);
133 
134 	m_After->SetColor(color);
135 }
SetColor(Gwen::Color color,bool onlyHue,bool reset)136 void HSVColorPicker::SetColor(Gwen::Color color, bool onlyHue, bool reset)
137 {
138 	UpdateControls(color);
139 
140 	if (reset)
141 		m_Before->SetColor(color);
142 
143 	m_ColorSlider->SetColor(color);
144 	m_LerpBox->SetColor(color, onlyHue);
145 	m_After->SetColor(color);
146 }
147 
GetColor()148 Gwen::Color HSVColorPicker::GetColor()
149 {
150 	return m_LerpBox->GetSelectedColor();
151 }
152 
ColorBoxChanged(Gwen::Controls::Base *)153 void HSVColorPicker::ColorBoxChanged(Gwen::Controls::Base* /*pControl*/)
154 {
155 	onColorChanged.Call(this);
156 	UpdateControls(GetColor());
157 	Invalidate();
158 }
ColorSliderChanged(Gwen::Controls::Base *)159 void HSVColorPicker::ColorSliderChanged(Gwen::Controls::Base* /*pControl*/)
160 {
161 	if (m_LerpBox)
162 		m_LerpBox->SetColor(m_ColorSlider->GetSelectedColor(), true);
163 	Invalidate();
164 }