1 /*
2 	GWEN
3 	Copyright (c) 2010 Facepunch Studios
4 	See license in Gwen.h
5 */
6 
7 #include "Gwen/Controls/ColorPicker.h"
8 #include "Gwen/Controls/HorizontalSlider.h"
9 #include "Gwen/Controls/GroupBox.h"
10 #include "Gwen/Controls/TextBox.h"
11 #include "Gwen/Utility.h"
12 
13 using namespace Gwen;
14 using namespace Gwen::Controls;
15 using namespace Gwen::ControlsInternal;
16 
GWEN_CONTROL_CONSTRUCTOR(ColorPicker)17 GWEN_CONTROL_CONSTRUCTOR(ColorPicker)
18 {
19 	SetMouseInputEnabled(true);
20 	SetKeyboardInputEnabled(true);
21 	SetSize(256, 150);
22 	CreateControls();
23 	SetColor(Gwen::Color(50, 60, 70, 255));
24 }
25 
CreateColorControl(Gwen::String name,int y)26 void ColorPicker::CreateColorControl(Gwen::String name, int y)
27 {
28 	int colorSize = 12;
29 
30 	GroupBox* colorGroup = new GroupBox(this);
31 	colorGroup->SetPos(10, y);
32 	colorGroup->SetText(name);
33 	colorGroup->SetSize(160, 35);
34 	colorGroup->SetName(name + "groupbox");
35 
36 	ColorDisplay* disp = new ColorDisplay(colorGroup);
37 	disp->SetName(name);
38 	disp->SetBounds(0, 10, colorSize, colorSize);
39 
40 	TextBoxNumeric* numeric = new TextBoxNumeric(colorGroup);
41 	numeric->SetName(name + "Box");
42 	numeric->SetPos(105, 7);
43 	numeric->SetSize(26, 16);
44 	numeric->SetSelectAllOnFocus(true);
45 	numeric->onTextChanged.Add(this, &ColorPicker::NumericTyped);
46 
47 	HorizontalSlider* slider = new HorizontalSlider(colorGroup);
48 	slider->SetPos(colorSize + 5, 10);
49 	slider->SetRange(0, 255);
50 	slider->SetSize(80, colorSize);
51 	slider->SetName(name + "Slider");
52 	slider->onValueChanged.Add(this, &ColorPicker::SlidersMoved);
53 }
54 
NumericTyped(Gwen::Controls::Base * control)55 void ColorPicker::NumericTyped(Gwen::Controls::Base* control)
56 {
57 	if (!control)
58 		return;
59 
60 	TextBoxNumeric* box = control->DynamicCastTextBoxNumeric();
61 	if (!box)
62 		return;
63 
64 	if (box->GetText() == L"")
65 		return;
66 
67 	int textValue = atoi(Utility::UnicodeToString(box->GetText()).c_str());
68 	if (textValue < 0) textValue = 0;
69 	if (textValue > 255) textValue = 255;
70 
71 	if (box->GetName().find("Red") != Gwen::String::npos)
72 		SetRed(textValue);
73 
74 	if (box->GetName().find("Green") != Gwen::String::npos)
75 		SetGreen(textValue);
76 
77 	if (box->GetName().find("Blue") != Gwen::String::npos)
78 		SetBlue(textValue);
79 
80 	if (box->GetName().find("Alpha") != Gwen::String::npos)
81 		SetAlpha(textValue);
82 
83 	UpdateControls();
84 }
85 
SetColor(Gwen::Color color)86 void ColorPicker::SetColor(Gwen::Color color)
87 {
88 	m_Color = color;
89 	UpdateControls();
90 }
91 
CreateControls()92 void ColorPicker::CreateControls()
93 {
94 	int startY = 5;
95 	int height = 35;
96 
97 	CreateColorControl("Red", startY);
98 	CreateColorControl("Green", startY + height);
99 	CreateColorControl("Blue", startY + height * 2);
100 	CreateColorControl("Alpha", startY + height * 3);
101 
102 	GroupBox* finalGroup = new GroupBox(this);
103 	finalGroup->SetPos(180, 40);
104 	finalGroup->SetSize(60, 60);
105 	finalGroup->SetText("Result");
106 	finalGroup->SetName("ResultGroupBox");
107 
108 	ColorDisplay* disp = new ColorDisplay(finalGroup);
109 	disp->SetName("Result");
110 	disp->SetBounds(0, 10, 32, 32);
111 	disp->SetDrawCheckers(true);
112 
113 	//UpdateControls();
114 }
115 
UpdateColorControls(Gwen::String name,Gwen::Color col,int sliderVal)116 void ColorPicker::UpdateColorControls(Gwen::String name, Gwen::Color col, int sliderVal)
117 {
118 	Base* el = FindChildByName(name, true);
119 
120 	ColorDisplay* disp = el ? el->DynamicCastColorDisplay() : 0;
121 	disp->SetColor(col);
122 
123 	HorizontalSlider* slider = FindChildByName(name + "Slider", true)->DynamicCastHorizontalSlider();
124 	slider->SetValue(sliderVal);
125 
126 	TextBoxNumeric* box = FindChildByName(name + "Box", true)->DynamicCastTextBoxNumeric();
127 	box->SetText(Gwen::Utility::ToString(sliderVal));
128 }
129 
UpdateControls()130 void ColorPicker::UpdateControls()
131 {
132 	//This is a little weird, but whatever for now
133 	UpdateColorControls("Red", Color(GetColor().r, 0, 0, 255), GetColor().r);
134 	UpdateColorControls("Green", Color(0, GetColor().g, 0, 255), GetColor().g);
135 	UpdateColorControls("Blue", Color(0, 0, GetColor().b, 255), GetColor().b);
136 	UpdateColorControls("Alpha", Color(255, 255, 255, GetColor().a), GetColor().a);
137 
138 	ColorDisplay* disp = FindChildByName("Result", true)->DynamicCastColorDisplay();
139 	disp->SetColor(Color(GetColor().r, GetColor().g, GetColor().b, GetColor().a));
140 
141 	onColorChanged.Call(this);
142 }
SlidersMoved(Gwen::Controls::Base * control)143 void ColorPicker::SlidersMoved(Gwen::Controls::Base* control)
144 {
145 	HorizontalSlider* slider = control->DynamicCastHorizontalSlider();
146 	if (slider)
147 		SetColorByName(GetColorFromName(slider->GetName()), slider->GetValue());
148 
149 	UpdateControls();
150 	//SetColor( Gwen::Color( redSlider->GetValue(), greenSlider->GetValue(), blueSlider->GetValue(), alphaSlider->GetValue() ) );
151 }
152 
Layout(Skin::Base * skin)153 void ColorPicker::Layout(Skin::Base* skin)
154 {
155 	BaseClass::Layout(skin);
156 
157 	SizeToChildren(false, true);
158 	SetSize(Width(), Height() + 5);
159 
160 	GroupBox* groupBox = FindChildByName("ResultGroupBox", true)->DynamicCastGroupBox();
161 	if (groupBox)
162 		groupBox->SetPos(groupBox->X(), Height() * 0.5f - groupBox->Height() * 0.5f);
163 
164 	UpdateControls();
165 }
166 
Render(Skin::Base * skin)167 void ColorPicker::Render(Skin::Base* skin)
168 {
169 	skin->DrawBackground(this);
170 }
171 
GetColorByName(Gwen::String colorName)172 int ColorPicker::GetColorByName(Gwen::String colorName)
173 {
174 	if (colorName == "Red")
175 		return GetColor().r;
176 	else if (colorName == "Green")
177 		return GetColor().g;
178 	else if (colorName == "Blue")
179 		return GetColor().b;
180 	else if (colorName == "Alpha")
181 		return GetColor().a;
182 	else
183 		return 0;
184 }
185 
GetColorFromName(Gwen::String name)186 Gwen::String ColorPicker::GetColorFromName(Gwen::String name)
187 {
188 	if (name.find("Red") != Gwen::String::npos)
189 		return "Red";
190 	if (name.find("Green") != Gwen::String::npos)
191 		return "Green";
192 	if (name.find("Blue") != Gwen::String::npos)
193 		return "Blue";
194 	if (name.find("Alpha") != Gwen::String::npos)
195 		return "Alpha";
196 	else
197 		return "";
198 }
199 
SetColorByName(Gwen::String colorName,int colorValue)200 void ColorPicker::SetColorByName(Gwen::String colorName, int colorValue)
201 {
202 	if (colorName == "Red")
203 		SetRed(colorValue);
204 	else if (colorName == "Green")
205 		SetGreen(colorValue);
206 	else if (colorName == "Blue")
207 		SetBlue(colorValue);
208 	else if (colorName == "Alpha")
209 		SetAlpha(colorValue);
210 }
211 
SetAlphaVisible(bool visible)212 void ColorPicker::SetAlphaVisible(bool visible)
213 {
214 	GroupBox* groupBox = FindChildByName("Alphagroupbox", true)->DynamicCastGroupBox();
215 	groupBox->SetHidden(!visible);
216 	Invalidate();
217 }
218