diff options
-rw-r--r-- | scene/gui/color_picker.cpp | 154 | ||||
-rw-r--r-- | scene/gui/color_picker.h | 9 | ||||
-rw-r--r-- | scene/resources/default_theme/default_theme.cpp | 3 | ||||
-rw-r--r-- | scene/resources/default_theme/icon_add.png | bin | 0 -> 351 bytes | |||
-rw-r--r-- | scene/resources/default_theme/theme_data.h | 5 |
5 files changed, 139 insertions, 32 deletions
diff --git a/scene/gui/color_picker.cpp b/scene/gui/color_picker.cpp index 2acb7b4bfd..0afca6b977 100644 --- a/scene/gui/color_picker.cpp +++ b/scene/gui/color_picker.cpp @@ -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){ diff --git a/scene/gui/color_picker.h b/scene/gui/color_picker.h index bfd73c9e2a..8a21d4b1b5 100644 --- a/scene/gui/color_picker.h +++ b/scene/gui/color_picker.h @@ -50,6 +50,9 @@ private: TextureFrame *uv_edit; TextureFrame *w_edit; TextureFrame *sample; + TextureFrame *preset; + Button *bt_add_preset; + List<Color> presets; ToolButton *btn_pick; CheckButton *btn_mode; Ref<CanvasItemMaterial> uv_material; @@ -72,10 +75,13 @@ private: void _value_changed(double); void _update_controls(); void _update_color(); + void _update_presets(); void _sample_draw(); void _uv_input(const InputEvent& p_input); void _w_input(const InputEvent& p_input); + void _preset_input(const InputEvent& p_input); + void _add_preset_pressed(); protected: void _notification(int); @@ -87,7 +93,8 @@ public: void set_color(const Color& p_color); Color get_color() const; - + + void add_preset(const Color& p_color); void set_raw_mode(bool p_enabled); bool is_raw_mode() const; diff --git a/scene/resources/default_theme/default_theme.cpp b/scene/resources/default_theme/default_theme.cpp index 2d6039ca4d..f713b9e979 100644 --- a/scene/resources/default_theme/default_theme.cpp +++ b/scene/resources/default_theme/default_theme.cpp @@ -773,8 +773,9 @@ void make_default_theme() { t->set_constant("color_width","ColorPicker", 100); t->set_constant("label_width","ColorPicker", 20); t->set_constant("hseparator","ColorPicker", 4); - + t->set_icon("screen_picker","ColorPicker", make_icon( icon_color_pick_png ) ); + t->set_icon("add_preset","ColorPicker", make_icon( icon_add_png ) ); t->set_shader("uv_editor", "ColorPicker", make_shader("", uv_editor_shader_code, "")); t->set_shader("w_editor", "ColorPicker", make_shader("", w_editor_shader_code, "")); diff --git a/scene/resources/default_theme/icon_add.png b/scene/resources/default_theme/icon_add.png Binary files differnew file mode 100644 index 0000000000..26283ca67c --- /dev/null +++ b/scene/resources/default_theme/icon_add.png diff --git a/scene/resources/default_theme/theme_data.h b/scene/resources/default_theme/theme_data.h index 004b3e4ce8..4c440ab99a 100644 --- a/scene/resources/default_theme/theme_data.h +++ b/scene/resources/default_theme/theme_data.h @@ -179,6 +179,11 @@ static const unsigned char hsplitter_png[]={ }; +static const unsigned char icon_add_png[]={ +0x89,0x50,0x4e,0x47,0xd,0xa,0x1a,0xa,0x0,0x0,0x0,0xd,0x49,0x48,0x44,0x52,0x0,0x0,0x0,0x10,0x0,0x0,0x0,0x10,0x8,0x6,0x0,0x0,0x0,0x1f,0xf3,0xff,0x61,0x0,0x0,0x0,0x9,0x70,0x48,0x59,0x73,0x0,0x0,0xb,0x12,0x0,0x0,0xb,0x12,0x1,0xd2,0xdd,0x7e,0xfc,0x0,0x0,0x0,0x19,0x74,0x45,0x58,0x74,0x43,0x6f,0x6d,0x6d,0x65,0x6e,0x74,0x0,0x43,0x72,0x65,0x61,0x74,0x65,0x64,0x20,0x77,0x69,0x74,0x68,0x20,0x47,0x49,0x4d,0x50,0x57,0x81,0xe,0x17,0x0,0x0,0x0,0xec,0x49,0x44,0x41,0x54,0x38,0x8d,0xdd,0x92,0x41,0x4a,0xc4,0x30,0x18,0x85,0xbf,0x67,0x93,0xc,0xd9,0x28,0xb9,0x41,0xcf,0x21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,0x42,0x60,0x82 +}; + + static const unsigned char icon_close_png[]={ 0x89,0x50,0x4e,0x47,0xd,0xa,0x1a,0xa,0x0,0x0,0x0,0xd,0x49,0x48,0x44,0x52,0x0,0x0,0x0,0x10,0x0,0x0,0x0,0x10,0x8,0x6,0x0,0x0,0x0,0x1f,0xf3,0xff,0x61,0x0,0x0,0x0,0x1,0x73,0x52,0x47,0x42,0x0,0xae,0xce,0x1c,0xe9,0x0,0x0,0x0,0x9,0x70,0x48,0x59,0x73,0x0,0x0,0xb,0x13,0x0,0x0,0xb,0x13,0x1,0x0,0x9a,0x9c,0x18,0x0,0x0,0x0,0x7,0x74,0x49,0x4d,0x45,0x7,0xd9,0xb,0x17,0x6,0x11,0xd,0x6a,0x1e,0x9a,0x6f,0x0,0x0,0x0,0x5e,0x49,0x44,0x41,0x54,0x38,0xcb,0xd5,0x53,0xcb,0xe,0xc0,0x30,0x8,0x12,0xb2,0xff,0xff,0x65,0x77,0x5a,0xb3,0x36,0xf8,0x48,0xcc,0xe,0xf3,0xd4,0x3,0xa2,0x40,0x85,0x9b,0xdb,0xa4,0x68,0xc3,0x1a,0x13,0x5c,0xcf,0x3,0x86,0xa5,0xc5,0xcd,0xa1,0xc0,0xa,0x43,0xd5,0xf4,0x6,0x56,0x3,0x36,0x9,0x11,0x49,0xb6,0x1d,0x54,0xa,0x6a,0x83,0x48,0x1a,0xbb,0xc0,0xc8,0x97,0x6f,0x62,0xac,0x4c,0x4c,0x9,0x4e,0xc3,0xaa,0x74,0xd8,0x89,0x2a,0x23,0x61,0xf7,0x23,0x85,0x11,0xff,0xff,0x98,0x6e,0x7,0x20,0x33,0x1a,0x5b,0xf5,0xcc,0xfe,0x0,0x0,0x0,0x0,0x49,0x45,0x4e,0x44,0xae,0x42,0x60,0x82 }; |