123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- /*************************************************************************/
- /* color_picker.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* http://www.godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
- /* */
- /* Permission is hereby granted, free of charge, to any person obtaining */
- /* a copy of this software and associated documentation files (the */
- /* "Software"), to deal in the Software without restriction, including */
- /* without limitation the rights to use, copy, modify, merge, publish, */
- /* distribute, sublicense, and/or sell copies of the Software, and to */
- /* permit persons to whom the Software is furnished to do so, subject to */
- /* the following conditions: */
- /* */
- /* The above copyright notice and this permission notice shall be */
- /* included in all copies or substantial portions of the Software. */
- /* */
- /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
- /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
- /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
- /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
- /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
- /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
- /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
- /*************************************************************************/
- #include "color_picker.h"
- void ColorPicker::_notification(int p_what) {
- switch(p_what) {
- case NOTIFICATION_THEME_CHANGED: {
- _update_controls();
- } break;
- /* case NOTIFICATION_DRAW: {
- int w = get_constant("color_width");
- int h = ms.height;
- VisualServer::get_singleton()->canvas_item_add_rect(get_canvas_item(),Rect2(0,0,w,h),color);
- } break;*/
- }
- }
- void ColorPicker::_update_controls() {
- int cw = get_constant("color_width");
- if (edit_alpha) {
- values[3]->show();
- scroll[3]->show();
- labels[3]->show();
- } else {
- values[3]->hide();
- scroll[3]->hide();
- labels[3]->hide();
- }
- }
- void ColorPicker::set_color(const Color& p_color) {
- color=p_color;
- _update_color();
- }
- void ColorPicker::set_edit_alpha(bool p_show) {
- edit_alpha=p_show;
- _update_controls();
- _update_color();
- color_box->update();
- }
- bool ColorPicker::is_editing_alpha() const {
- return edit_alpha;
- }
- void ColorPicker::_value_changed(double) {
- if (updating)
- return;
- switch(mode) {
- case MODE_RGB: {
- for(int i=0;i<4;i++) {
- color.components[i] = scroll[i]->get_val() / 255.0;
- }
- } break;
- case MODE_HSV: {
- color.set_hsv( CLAMP(scroll[0]->get_val()/255,0,0.99), scroll[1]->get_val()/255, scroll[2]->get_val()/255 );
- color.a=scroll[3]->get_val()/255.0;
- } break;
- case MODE_RAW: {
- for(int i=0;i<4;i++) {
- color.components[i] = scroll[i]->get_val();
- }
- } break;
- }
- html->set_text(color.to_html(edit_alpha && color.a<1));
- color_box->update();
- emit_signal("color_changed",color);
- }
- void ColorPicker::_html_entered(const String& p_html) {
- if (updating)
- return;
- color = Color::html(p_html);
- _update_color();
- emit_signal("color_changed",color);
- }
- void ColorPicker::_update_color() {
- updating=true;
- switch(mode) {
- case MODE_RAW: {
- for(int i=0;i<4;i++) {
- scroll[i]->set_step(0.01);
- scroll[i]->set_val(color.components[i]);
- }
- } break;
- case MODE_RGB: {
- for(int i=0;i<4;i++) {
- scroll[i]->set_step(1);
- scroll[i]->set_val(color.components[i]*255);
- }
- } break;
- case MODE_HSV: {
- for(int i=0;i<4;i++) {
- scroll[i]->set_step(1);
- }
- scroll[0]->set_val( color.get_h()*255 );
- scroll[1]->set_val( color.get_s()*255 );
- scroll[2]->set_val( color.get_v()*255 );
- scroll[3]->set_val(color.a*255);
- } break;
- }
- html->set_text(color.to_html(edit_alpha && color.a<1));
- color_box->update();
- updating=false;
- }
- Color ColorPicker::get_color() const {
- return color;
- }
- void ColorPicker::set_mode(Mode p_mode) {
- ERR_FAIL_INDEX(p_mode,3);
- mode=p_mode;
- if (mode_box->get_selected()!=p_mode)
- mode_box->select(p_mode);
- _update_controls();
- _update_color();
- }
- ColorPicker::Mode ColorPicker::get_mode() const {
- return mode;
- }
- void ColorPicker::_color_box_draw() {
- color_box->draw_rect( Rect2( Point2(), color_box->get_size()), color);
- }
- void ColorPicker::_bind_methods() {
- ObjectTypeDB::bind_method(_MD("set_color","color"),&ColorPicker::set_color);
- ObjectTypeDB::bind_method(_MD("get_color"),&ColorPicker::get_color);
- ObjectTypeDB::bind_method(_MD("set_mode","mode"),&ColorPicker::set_mode);
- ObjectTypeDB::bind_method(_MD("get_mode"),&ColorPicker::get_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("_value_changed"),&ColorPicker::_value_changed);
- ObjectTypeDB::bind_method(_MD("_html_entered"),&ColorPicker::_html_entered);
- ObjectTypeDB::bind_method(_MD("_color_box_draw"),&ColorPicker::_color_box_draw);
- ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color")));
- }
- ColorPicker::ColorPicker() {
- //edit_alpha=false;
- updating=true;
- edit_alpha=true;
- VBoxContainer *vbl = memnew( VBoxContainer );
- add_child(vbl);
- mode_box = memnew( OptionButton );
- mode_box->add_item("RGB");
- mode_box->add_item("HSV");
- mode_box->add_item("RAW");
- mode_box->connect("item_selected",this,"set_mode");
- color_box=memnew( Control );
- color_box->set_v_size_flags(SIZE_EXPAND_FILL);
- vbl->add_child(color_box);
- color_box->connect("draw",this,"_color_box_draw");
- vbl->add_child(mode_box);
- VBoxContainer *vbr = memnew( VBoxContainer );
- add_child(vbr);
- vbr->set_h_size_flags(SIZE_EXPAND_FILL);
- for(int i=0;i<4;i++) {
- HBoxContainer *hbc = memnew( HBoxContainer );
- labels[i]=memnew( Label );
- static const char*_lt[4]={"R","G","B","A"};
- labels[i]->set_text(_lt[i]);
- hbc->add_child(labels[i]);
- scroll[i]=memnew( HSlider );
- hbc->add_child(scroll[i]);
- values[i]=memnew( SpinBox );
- scroll[i]->share(values[i]);
- hbc->add_child(values[i]);
- scroll[i]->set_min(0);
- scroll[i]->set_max(255);
- scroll[i]->set_step(1);
- scroll[i]->set_page(0);
- scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
- scroll[i]->connect("value_changed",this,"_value_changed");
- vbr->add_child(hbc);
- }
- HBoxContainer *hhb = memnew( HBoxContainer );
- vbr->add_child(hhb);
- html_num = memnew( Label );
- hhb->add_child(html_num);
- html = memnew( LineEdit );
- hhb->add_child(html);
- html->connect("text_entered",this,"_html_entered");
- html_num->set_text("#");
- html->set_h_size_flags(SIZE_EXPAND_FILL);
- mode=MODE_RGB;
- _update_controls();
- _update_color();
- updating=false;
- }
- /////////////////
- void ColorPickerButton::_color_changed(const Color& p_color) {
- update();
- emit_signal("color_changed",p_color);
- }
- void ColorPickerButton::pressed() {
- Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10);
- popup->set_pos(get_global_pos()-Size2(0,ms.height));
- popup->set_size(ms);
- popup->popup();
- }
- void ColorPickerButton::_notification(int p_what) {
- if (p_what==NOTIFICATION_DRAW) {
- Ref<StyleBox> normal = get_stylebox("normal" );
- draw_rect(Rect2(normal->get_offset(),get_size()-normal->get_minimum_size()),picker->get_color());
- }
- }
- void ColorPickerButton::set_color(const Color& p_color){
- picker->set_color(p_color);
- }
- Color ColorPickerButton::get_color() const{
- return picker->get_color();
- }
- void ColorPickerButton::set_edit_alpha(bool p_show) {
- picker->set_edit_alpha(p_show);
- }
- bool ColorPickerButton::is_editing_alpha() const{
- return picker->is_editing_alpha();
- }
- void ColorPickerButton::_bind_methods(){
- ObjectTypeDB::bind_method(_MD("set_color","color"),&ColorPickerButton::set_color);
- ObjectTypeDB::bind_method(_MD("get_color"),&ColorPickerButton::get_color);
- ObjectTypeDB::bind_method(_MD("set_edit_alpha","show"),&ColorPickerButton::set_edit_alpha);
- ObjectTypeDB::bind_method(_MD("is_editing_alpha"),&ColorPickerButton::is_editing_alpha);
- ObjectTypeDB::bind_method(_MD("_color_changed"),&ColorPickerButton::_color_changed);
- ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color")));
- ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color") );
- ADD_PROPERTY( PropertyInfo(Variant::BOOL,"edit_alpha"),_SCS("set_edit_alpha"),_SCS("is_editing_alpha") );
- }
- ColorPickerButton::ColorPickerButton() {
- popup = memnew( PopupPanel );
- picker = memnew( ColorPicker );
- popup->add_child(picker);
- popup->set_child_rect(picker);
- picker->connect("color_changed",this,"_color_changed");
- add_child(popup);
- }
|