color_picker.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*************************************************************************/
  2. /* color_picker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "color_picker.h"
  30. void ColorPicker::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_THEME_CHANGED: {
  33. _update_controls();
  34. } break;
  35. /* case NOTIFICATION_DRAW: {
  36. int w = get_constant("color_width");
  37. int h = ms.height;
  38. VisualServer::get_singleton()->canvas_item_add_rect(get_canvas_item(),Rect2(0,0,w,h),color);
  39. } break;*/
  40. }
  41. }
  42. void ColorPicker::_update_controls() {
  43. int cw = get_constant("color_width");
  44. if (edit_alpha) {
  45. values[3]->show();
  46. scroll[3]->show();
  47. labels[3]->show();
  48. } else {
  49. values[3]->hide();
  50. scroll[3]->hide();
  51. labels[3]->hide();
  52. }
  53. }
  54. void ColorPicker::set_color(const Color& p_color) {
  55. color=p_color;
  56. _update_color();
  57. }
  58. void ColorPicker::set_edit_alpha(bool p_show) {
  59. edit_alpha=p_show;
  60. _update_controls();
  61. _update_color();
  62. color_box->update();
  63. }
  64. bool ColorPicker::is_editing_alpha() const {
  65. return edit_alpha;
  66. }
  67. void ColorPicker::_value_changed(double) {
  68. if (updating)
  69. return;
  70. switch(mode) {
  71. case MODE_RGB: {
  72. for(int i=0;i<4;i++) {
  73. color.components[i] = scroll[i]->get_val() / 255.0;
  74. }
  75. } break;
  76. case MODE_HSV: {
  77. color.set_hsv( CLAMP(scroll[0]->get_val()/255,0,0.99), scroll[1]->get_val()/255, scroll[2]->get_val()/255 );
  78. color.a=scroll[3]->get_val()/255.0;
  79. } break;
  80. case MODE_RAW: {
  81. for(int i=0;i<4;i++) {
  82. color.components[i] = scroll[i]->get_val();
  83. }
  84. } break;
  85. }
  86. html->set_text(color.to_html(edit_alpha && color.a<1));
  87. color_box->update();
  88. emit_signal("color_changed",color);
  89. }
  90. void ColorPicker::_html_entered(const String& p_html) {
  91. if (updating)
  92. return;
  93. color = Color::html(p_html);
  94. _update_color();
  95. emit_signal("color_changed",color);
  96. }
  97. void ColorPicker::_update_color() {
  98. updating=true;
  99. switch(mode) {
  100. case MODE_RAW: {
  101. for(int i=0;i<4;i++) {
  102. scroll[i]->set_step(0.01);
  103. scroll[i]->set_val(color.components[i]);
  104. }
  105. } break;
  106. case MODE_RGB: {
  107. for(int i=0;i<4;i++) {
  108. scroll[i]->set_step(1);
  109. scroll[i]->set_val(color.components[i]*255);
  110. }
  111. } break;
  112. case MODE_HSV: {
  113. for(int i=0;i<4;i++) {
  114. scroll[i]->set_step(1);
  115. }
  116. scroll[0]->set_val( color.get_h()*255 );
  117. scroll[1]->set_val( color.get_s()*255 );
  118. scroll[2]->set_val( color.get_v()*255 );
  119. scroll[3]->set_val(color.a*255);
  120. } break;
  121. }
  122. html->set_text(color.to_html(edit_alpha && color.a<1));
  123. color_box->update();
  124. updating=false;
  125. }
  126. Color ColorPicker::get_color() const {
  127. return color;
  128. }
  129. void ColorPicker::set_mode(Mode p_mode) {
  130. ERR_FAIL_INDEX(p_mode,3);
  131. mode=p_mode;
  132. if (mode_box->get_selected()!=p_mode)
  133. mode_box->select(p_mode);
  134. _update_controls();
  135. _update_color();
  136. }
  137. ColorPicker::Mode ColorPicker::get_mode() const {
  138. return mode;
  139. }
  140. void ColorPicker::_color_box_draw() {
  141. color_box->draw_rect( Rect2( Point2(), color_box->get_size()), color);
  142. }
  143. void ColorPicker::_bind_methods() {
  144. ObjectTypeDB::bind_method(_MD("set_color","color"),&ColorPicker::set_color);
  145. ObjectTypeDB::bind_method(_MD("get_color"),&ColorPicker::get_color);
  146. ObjectTypeDB::bind_method(_MD("set_mode","mode"),&ColorPicker::set_mode);
  147. ObjectTypeDB::bind_method(_MD("get_mode"),&ColorPicker::get_mode);
  148. ObjectTypeDB::bind_method(_MD("set_edit_alpha","show"),&ColorPicker::set_edit_alpha);
  149. ObjectTypeDB::bind_method(_MD("is_editing_alpha"),&ColorPicker::is_editing_alpha);
  150. ObjectTypeDB::bind_method(_MD("_value_changed"),&ColorPicker::_value_changed);
  151. ObjectTypeDB::bind_method(_MD("_html_entered"),&ColorPicker::_html_entered);
  152. ObjectTypeDB::bind_method(_MD("_color_box_draw"),&ColorPicker::_color_box_draw);
  153. ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color")));
  154. }
  155. ColorPicker::ColorPicker() {
  156. //edit_alpha=false;
  157. updating=true;
  158. edit_alpha=true;
  159. VBoxContainer *vbl = memnew( VBoxContainer );
  160. add_child(vbl);
  161. mode_box = memnew( OptionButton );
  162. mode_box->add_item("RGB");
  163. mode_box->add_item("HSV");
  164. mode_box->add_item("RAW");
  165. mode_box->connect("item_selected",this,"set_mode");
  166. color_box=memnew( Control );
  167. color_box->set_v_size_flags(SIZE_EXPAND_FILL);
  168. vbl->add_child(color_box);
  169. color_box->connect("draw",this,"_color_box_draw");
  170. vbl->add_child(mode_box);
  171. VBoxContainer *vbr = memnew( VBoxContainer );
  172. add_child(vbr);
  173. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  174. for(int i=0;i<4;i++) {
  175. HBoxContainer *hbc = memnew( HBoxContainer );
  176. labels[i]=memnew( Label );
  177. static const char*_lt[4]={"R","G","B","A"};
  178. labels[i]->set_text(_lt[i]);
  179. hbc->add_child(labels[i]);
  180. scroll[i]=memnew( HSlider );
  181. hbc->add_child(scroll[i]);
  182. values[i]=memnew( SpinBox );
  183. scroll[i]->share(values[i]);
  184. hbc->add_child(values[i]);
  185. scroll[i]->set_min(0);
  186. scroll[i]->set_max(255);
  187. scroll[i]->set_step(1);
  188. scroll[i]->set_page(0);
  189. scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  190. scroll[i]->connect("value_changed",this,"_value_changed");
  191. vbr->add_child(hbc);
  192. }
  193. HBoxContainer *hhb = memnew( HBoxContainer );
  194. vbr->add_child(hhb);
  195. html_num = memnew( Label );
  196. hhb->add_child(html_num);
  197. html = memnew( LineEdit );
  198. hhb->add_child(html);
  199. html->connect("text_entered",this,"_html_entered");
  200. html_num->set_text("#");
  201. html->set_h_size_flags(SIZE_EXPAND_FILL);
  202. mode=MODE_RGB;
  203. _update_controls();
  204. _update_color();
  205. updating=false;
  206. }
  207. /////////////////
  208. void ColorPickerButton::_color_changed(const Color& p_color) {
  209. update();
  210. emit_signal("color_changed",p_color);
  211. }
  212. void ColorPickerButton::pressed() {
  213. Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10);
  214. popup->set_pos(get_global_pos()-Size2(0,ms.height));
  215. popup->set_size(ms);
  216. popup->popup();
  217. }
  218. void ColorPickerButton::_notification(int p_what) {
  219. if (p_what==NOTIFICATION_DRAW) {
  220. Ref<StyleBox> normal = get_stylebox("normal" );
  221. draw_rect(Rect2(normal->get_offset(),get_size()-normal->get_minimum_size()),picker->get_color());
  222. }
  223. }
  224. void ColorPickerButton::set_color(const Color& p_color){
  225. picker->set_color(p_color);
  226. }
  227. Color ColorPickerButton::get_color() const{
  228. return picker->get_color();
  229. }
  230. void ColorPickerButton::set_edit_alpha(bool p_show) {
  231. picker->set_edit_alpha(p_show);
  232. }
  233. bool ColorPickerButton::is_editing_alpha() const{
  234. return picker->is_editing_alpha();
  235. }
  236. void ColorPickerButton::_bind_methods(){
  237. ObjectTypeDB::bind_method(_MD("set_color","color"),&ColorPickerButton::set_color);
  238. ObjectTypeDB::bind_method(_MD("get_color"),&ColorPickerButton::get_color);
  239. ObjectTypeDB::bind_method(_MD("set_edit_alpha","show"),&ColorPickerButton::set_edit_alpha);
  240. ObjectTypeDB::bind_method(_MD("is_editing_alpha"),&ColorPickerButton::is_editing_alpha);
  241. ObjectTypeDB::bind_method(_MD("_color_changed"),&ColorPickerButton::_color_changed);
  242. ADD_SIGNAL( MethodInfo("color_changed",PropertyInfo(Variant::COLOR,"color")));
  243. ADD_PROPERTY( PropertyInfo(Variant::COLOR,"color"),_SCS("set_color"),_SCS("get_color") );
  244. ADD_PROPERTY( PropertyInfo(Variant::BOOL,"edit_alpha"),_SCS("set_edit_alpha"),_SCS("is_editing_alpha") );
  245. }
  246. ColorPickerButton::ColorPickerButton() {
  247. popup = memnew( PopupPanel );
  248. picker = memnew( ColorPicker );
  249. popup->add_child(picker);
  250. popup->set_child_rect(picker);
  251. picker->connect("color_changed",this,"_color_changed");
  252. add_child(popup);
  253. }