Add presets.
Also fix some bugs. Fix a crash and a minor bug
This commit is contained in:
@ -30,19 +30,34 @@
|
||||
|
||||
#include "scene/gui/separator.h"
|
||||
#include "os/os.h"
|
||||
#include "os/input.h"
|
||||
#include "os/keyboard.h"
|
||||
|
||||
void update_material(Ref<CanvasItemMaterial>mat,const Color& p_color) {
|
||||
if (!mat.is_valid())
|
||||
return;
|
||||
Ref<Shader> sdr = mat->get_shader();
|
||||
if (!sdr.is_valid())
|
||||
return;
|
||||
|
||||
mat->set_shader_param("R",p_color.r);
|
||||
mat->set_shader_param("G",p_color.g);
|
||||
mat->set_shader_param("B",p_color.b);
|
||||
mat->set_shader_param("H",p_color.get_h());
|
||||
mat->set_shader_param("S",p_color.get_s());
|
||||
mat->set_shader_param("V",p_color.get_v());
|
||||
mat->set_shader_param("A",p_color.a);
|
||||
}
|
||||
|
||||
void ColorPicker::_notification(int p_what) {
|
||||
|
||||
|
||||
switch(p_what) {
|
||||
case NOTIFICATION_THEME_CHANGED: {
|
||||
uv_material->set_shader(get_shader("uv_editor"));
|
||||
uv_material->set_shader_param("H", h);
|
||||
|
||||
w_material->set_shader(get_shader("w_editor"));
|
||||
|
||||
uv_material->set_shader(get_shader("uv_editor"));
|
||||
w_material->set_shader(get_shader("w_editor"));
|
||||
update_material(uv_material,color);
|
||||
update_material(w_material,color);
|
||||
_update_controls();
|
||||
} break;
|
||||
|
||||
@ -66,13 +81,14 @@ void ColorPicker::_update_controls() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ColorPicker::set_color(const Color& p_color) {
|
||||
|
||||
color=p_color;
|
||||
h=color.get_h();
|
||||
s=color.get_s();
|
||||
v=color.get_v();
|
||||
update_material(uv_material, color);
|
||||
update_material(w_material, color);
|
||||
_update_color();
|
||||
|
||||
}
|
||||
@ -100,6 +116,9 @@ void ColorPicker::_value_changed(double) {
|
||||
}
|
||||
color.components[3] = scroll[3]->get_val()/255.0;
|
||||
|
||||
update_material(uv_material,color);
|
||||
update_material(w_material,color);
|
||||
|
||||
html->set_text(color.to_html(edit_alpha && color.a<1));
|
||||
|
||||
sample->update();
|
||||
@ -137,11 +156,37 @@ void ColorPicker::_update_color() {
|
||||
updating=false;
|
||||
}
|
||||
|
||||
void ColorPicker::_update_presets()
|
||||
{
|
||||
Size2 size=bt_add_preset->get_size();
|
||||
preset->set_custom_minimum_size(Size2(size.width*presets.size(),size.height));
|
||||
Image i(size.x*presets.size(),size.y, false, Image::FORMAT_RGB);
|
||||
for (int y=0;y<size.y;y++)
|
||||
for (int x=0;x<size.x*presets.size();x++)
|
||||
i.put_pixel(x,y,presets[(int)x/size.x]);
|
||||
Ref<ImageTexture> t;
|
||||
t.instance();
|
||||
t->create_from_image(i);
|
||||
preset->set_texture(t);
|
||||
}
|
||||
|
||||
Color ColorPicker::get_color() const {
|
||||
|
||||
return color;
|
||||
}
|
||||
|
||||
void ColorPicker::add_preset(const Color &p_color)
|
||||
{
|
||||
if (presets.find(p_color)) {
|
||||
presets.move_to_back(presets.find(p_color));
|
||||
} else {
|
||||
presets.push_back(p_color);
|
||||
}
|
||||
_update_presets();
|
||||
if (presets.size()==10)
|
||||
bt_add_preset->hide();
|
||||
}
|
||||
|
||||
|
||||
void ColorPicker::set_raw_mode(bool p_enabled) {
|
||||
|
||||
@ -164,8 +209,7 @@ void ColorPicker::_sample_draw() {
|
||||
sample->draw_rect(Rect2(Point2(),Size2(256,20)),color);
|
||||
}
|
||||
|
||||
void ColorPicker::_uv_input(const InputEvent &ev)
|
||||
{
|
||||
void ColorPicker::_uv_input(const InputEvent &ev) {
|
||||
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
||||
const InputEventMouseButton &bev = ev.mouse_button;
|
||||
if (bev.pressed) {
|
||||
@ -174,6 +218,10 @@ void ColorPicker::_uv_input(const InputEvent &ev)
|
||||
float y = CLAMP((float)bev.y,0,256);
|
||||
s=x/256;
|
||||
v=1.0-y/256.0;
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
update_material(w_material, color);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
} else {
|
||||
changing_color = false;
|
||||
}
|
||||
@ -185,34 +233,67 @@ void ColorPicker::_uv_input(const InputEvent &ev)
|
||||
float y = CLAMP((float)bev.y,0,256);
|
||||
s=x/256;
|
||||
v=1.0-y/256.0;
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
update_material(w_material, color);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
}
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
}
|
||||
|
||||
void ColorPicker::_w_input(const InputEvent &ev)
|
||||
{
|
||||
void ColorPicker::_w_input(const InputEvent &ev) {
|
||||
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
||||
const InputEventMouseButton &bev = ev.mouse_button;
|
||||
if (bev.pressed) {
|
||||
changing_color = true;
|
||||
h=((float)bev.y)/256.0;
|
||||
h=1-((float)bev.y)/256.0;
|
||||
|
||||
} else {
|
||||
changing_color = false;
|
||||
}
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
update_material(uv_material, color);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
||||
const InputEventMouse &bev = ev.mouse_motion;
|
||||
if (!changing_color)
|
||||
return;
|
||||
float y = CLAMP((float)bev.y,0,256);
|
||||
h=1.0-y/256.0;
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
update_material(uv_material, color);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
}
|
||||
uv_material->set_shader_param("H", h);
|
||||
color.set_hsv(h,s,v,color.a);
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
}
|
||||
|
||||
void ColorPicker::_preset_input(const InputEvent &ev) {
|
||||
if (ev.type == InputEvent::MOUSE_BUTTON) {
|
||||
const InputEventMouseButton &bev = ev.mouse_button;
|
||||
if (bev.pressed && bev.button_index==BUTTON_LEFT) {
|
||||
int index = bev.x/(preset->get_size().x/presets.size());
|
||||
set_color(presets[index]);
|
||||
} else if (bev.pressed && bev.button_index==BUTTON_RIGHT) {
|
||||
int index = bev.x/(preset->get_size().x/presets.size());
|
||||
presets.erase(presets[index]);
|
||||
_update_presets();
|
||||
bt_add_preset->show();
|
||||
}
|
||||
_update_color();
|
||||
emit_signal("color_changed", color);
|
||||
} else if (ev.type == InputEvent::MOUSE_MOTION) {
|
||||
const InputEventMouseButton &bev = ev.mouse_button;
|
||||
int index = bev.x/(preset->get_size().x/presets.size());
|
||||
if (index<0 || index >= presets.size())
|
||||
return;
|
||||
preset->set_tooltip("Color: #"+presets[index].to_html(presets[index].a<1)+"\n"
|
||||
"LMB: Set color\n"
|
||||
"RMB: Remove preset");
|
||||
}
|
||||
}
|
||||
|
||||
void ColorPicker::_add_preset_pressed() {
|
||||
add_preset(color);
|
||||
}
|
||||
|
||||
void ColorPicker::_bind_methods() {
|
||||
@ -223,18 +304,18 @@ void ColorPicker::_bind_methods() {
|
||||
ObjectTypeDB::bind_method(_MD("is_raw_mode"),&ColorPicker::is_raw_mode);
|
||||
ObjectTypeDB::bind_method(_MD("set_edit_alpha","show"),&ColorPicker::set_edit_alpha);
|
||||
ObjectTypeDB::bind_method(_MD("is_editing_alpha"),&ColorPicker::is_editing_alpha);
|
||||
ObjectTypeDB::bind_method(_MD("add_preset"), &ColorPicker::add_preset);
|
||||
ObjectTypeDB::bind_method(_MD("_value_changed"),&ColorPicker::_value_changed);
|
||||
ObjectTypeDB::bind_method(_MD("_html_entered"),&ColorPicker::_html_entered);
|
||||
ObjectTypeDB::bind_method(_MD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed);
|
||||
ObjectTypeDB::bind_method(_MD("_sample_draw"),&ColorPicker::_sample_draw);
|
||||
ObjectTypeDB::bind_method(_MD("_uv_input"),&ColorPicker::_uv_input);
|
||||
ObjectTypeDB::bind_method(_MD("_w_input"),&ColorPicker::_w_input);
|
||||
ObjectTypeDB::bind_method(_MD("_preset_input"),&ColorPicker::_preset_input);
|
||||
|
||||
ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ColorPicker::ColorPicker() :
|
||||
BoxContainer(true) {
|
||||
|
||||
@ -254,7 +335,7 @@ ColorPicker::ColorPicker() :
|
||||
add_child(hb_smpl);
|
||||
|
||||
HBoxContainer *hb_edit = memnew( HBoxContainer );
|
||||
|
||||
|
||||
uv_edit= memnew ( TextureFrame );
|
||||
Image i(256, 256, false, Image::FORMAT_RGB);
|
||||
for (int y=0;y<256;y++)
|
||||
@ -267,7 +348,7 @@ ColorPicker::ColorPicker() :
|
||||
uv_edit->set_ignore_mouse(false);
|
||||
uv_edit->set_custom_minimum_size(Size2(256,256));
|
||||
uv_edit->connect("input_event", this, "_uv_input");
|
||||
|
||||
|
||||
add_child(hb_edit);
|
||||
w_edit= memnew( TextureFrame );
|
||||
i = Image(15, 256, false, Image::FORMAT_RGB);
|
||||
@ -285,21 +366,22 @@ ColorPicker::ColorPicker() :
|
||||
hb_edit->add_child(uv_edit);
|
||||
hb_edit->add_child(memnew( VSeparator ));
|
||||
hb_edit->add_child(w_edit);
|
||||
|
||||
|
||||
VBoxContainer *vbl = memnew( VBoxContainer );
|
||||
add_child(vbl);
|
||||
|
||||
add_child(memnew( HSeparator ));
|
||||
|
||||
VBoxContainer *vbr = memnew( VBoxContainer );
|
||||
add_child(vbr);
|
||||
vbr->set_h_size_flags(SIZE_EXPAND_FILL);
|
||||
|
||||
const char* lt[4] = {"R","G","B","A"};
|
||||
|
||||
for(int i=0;i<4;i++) {
|
||||
|
||||
HBoxContainer *hbc = memnew( HBoxContainer );
|
||||
|
||||
labels[i]=memnew( Label );
|
||||
labels[i]=memnew( Label(lt[i]) );
|
||||
hbc->add_child(labels[i]);
|
||||
|
||||
scroll[i]=memnew( HSlider );
|
||||
@ -341,12 +423,9 @@ ColorPicker::ColorPicker() :
|
||||
_update_color();
|
||||
updating=false;
|
||||
|
||||
set_color(Color(1,1,1));
|
||||
|
||||
uv_material.instance();
|
||||
Ref<Shader> s_uv = get_shader("uv_editor");
|
||||
uv_material->set_shader(s_uv);
|
||||
uv_material->set_shader_param("H", h);
|
||||
|
||||
w_material.instance();
|
||||
|
||||
@ -356,6 +435,8 @@ ColorPicker::ColorPicker() :
|
||||
uv_edit->set_material(uv_material);
|
||||
w_edit->set_material(w_material);
|
||||
|
||||
set_color(Color(1,1,1));
|
||||
|
||||
i.create(256,20,false,Image::FORMAT_RGB);
|
||||
for (int y=0;y<20;y++)
|
||||
for(int x=0;x<256;x++)
|
||||
@ -367,6 +448,20 @@ ColorPicker::ColorPicker() :
|
||||
t_smpl.instance();
|
||||
t_smpl->create_from_image(i);
|
||||
sample->set_texture(t_smpl);
|
||||
|
||||
HBoxContainer *bbc = memnew( HBoxContainer );
|
||||
add_child(bbc);
|
||||
|
||||
preset = memnew( TextureFrame );
|
||||
bbc->add_child(preset);
|
||||
preset->set_ignore_mouse(false);
|
||||
preset->connect("input_event", this, "_preset_input");
|
||||
|
||||
bt_add_preset = memnew ( Button );
|
||||
bt_add_preset->set_icon(get_icon("add_preset"));
|
||||
bt_add_preset->set_tooltip("Add current color as a preset");
|
||||
bt_add_preset->connect("pressed", this, "_add_preset_pressed", Vector<Variant>());
|
||||
bbc->add_child(bt_add_preset);
|
||||
}
|
||||
|
||||
|
||||
@ -400,7 +495,6 @@ void ColorPickerButton::_notification(int p_what) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ColorPickerButton::set_color(const Color& p_color){
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user