color_picker.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*************************************************************************/
  2. /* color_picker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "color_picker.h"
  31. #include "os/input.h"
  32. #include "os/keyboard.h"
  33. #include "os/os.h"
  34. #include "scene/gui/separator.h"
  35. #include "scene/main/viewport.h"
  36. void ColorPicker::_notification(int p_what) {
  37. switch (p_what) {
  38. case NOTIFICATION_THEME_CHANGED: {
  39. //sample->set_texture(get_icon("color_sample"));
  40. btn_pick->set_icon(get_icon("screen_picker", "ColorPicker"));
  41. bt_add_preset->set_icon(get_icon("add_preset"));
  42. _update_controls();
  43. } break;
  44. case NOTIFICATION_ENTER_TREE: {
  45. btn_pick->set_icon(get_icon("screen_picker", "ColorPicker"));
  46. bt_add_preset->set_icon(get_icon("add_preset"));
  47. _update_color();
  48. } break;
  49. case NOTIFICATION_PARENTED: {
  50. for (int i = 0; i < 4; i++)
  51. set_margin((Margin)i, get_constant("margin"));
  52. } break;
  53. case NOTIFICATION_VISIBILITY_CHANGED: {
  54. Popup *p = Object::cast_to<Popup>(get_parent());
  55. if (p)
  56. p->set_size(Size2(get_combined_minimum_size().width + get_constant("margin") * 2, get_combined_minimum_size().height + get_constant("margin") * 2));
  57. } break;
  58. case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: {
  59. if (screen != NULL) {
  60. if (screen->is_visible()) {
  61. screen->hide();
  62. }
  63. }
  64. } break;
  65. }
  66. }
  67. void ColorPicker::set_focus_on_line_edit() {
  68. c_text->grab_focus();
  69. c_text->select();
  70. }
  71. void ColorPicker::_update_controls() {
  72. if (edit_alpha) {
  73. values[3]->show();
  74. scroll[3]->show();
  75. labels[3]->show();
  76. } else {
  77. values[3]->hide();
  78. scroll[3]->hide();
  79. labels[3]->hide();
  80. }
  81. }
  82. void ColorPicker::set_pick_color(const Color &p_color) {
  83. color = p_color;
  84. if (color != last_hsv) {
  85. h = color.get_h();
  86. s = color.get_s();
  87. v = color.get_v();
  88. last_hsv = color;
  89. }
  90. if (!is_inside_tree())
  91. return;
  92. _update_color();
  93. }
  94. void ColorPicker::set_edit_alpha(bool p_show) {
  95. edit_alpha = p_show;
  96. _update_controls();
  97. if (!is_inside_tree())
  98. return;
  99. _update_color();
  100. sample->update();
  101. }
  102. bool ColorPicker::is_editing_alpha() const {
  103. return edit_alpha;
  104. }
  105. void ColorPicker::_value_changed(double) {
  106. if (updating)
  107. return;
  108. for (int i = 0; i < 4; i++) {
  109. color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0);
  110. }
  111. set_pick_color(color);
  112. emit_signal("color_changed", color);
  113. }
  114. void ColorPicker::_html_entered(const String &p_html) {
  115. if (updating)
  116. return;
  117. color = Color::html(p_html);
  118. if (!is_inside_tree())
  119. return;
  120. set_pick_color(color);
  121. emit_signal("color_changed", color);
  122. }
  123. void ColorPicker::_update_color() {
  124. updating = true;
  125. for (int i = 0; i < 4; i++) {
  126. scroll[i]->set_max(255);
  127. scroll[i]->set_step(0.01);
  128. if (raw_mode_enabled) {
  129. if (i == 3)
  130. scroll[i]->set_max(1);
  131. scroll[i]->set_value(color.components[i]);
  132. } else {
  133. scroll[i]->set_value(color.components[i] * 255);
  134. }
  135. }
  136. _update_text_value();
  137. sample->update();
  138. uv_edit->update();
  139. w_edit->update();
  140. updating = false;
  141. }
  142. void ColorPicker::_update_presets() {
  143. Size2 size = bt_add_preset->get_size();
  144. Size2 preset_size = Size2(size.width * presets.size(), size.height);
  145. preset->set_custom_minimum_size(preset_size);
  146. preset->draw_texture_rect(get_icon("preset_bg", "ColorPicker"), Rect2(Point2(), preset_size), true);
  147. for (int i = 0; i < presets.size(); i++) {
  148. preset->draw_rect(Rect2(Point2(size.width * i, 0), size), presets[i]);
  149. }
  150. }
  151. void ColorPicker::_text_type_toggled() {
  152. if (!Engine::get_singleton()->is_editor_hint())
  153. return;
  154. text_is_constructor = !text_is_constructor;
  155. if (text_is_constructor) {
  156. text_type->set_text("");
  157. text_type->set_icon(get_icon("Script", "EditorIcons"));
  158. } else {
  159. text_type->set_text("#");
  160. text_type->set_icon(NULL);
  161. }
  162. _update_color();
  163. }
  164. Color ColorPicker::get_pick_color() const {
  165. return color;
  166. }
  167. void ColorPicker::add_preset(const Color &p_color) {
  168. if (presets.find(p_color)) {
  169. presets.move_to_back(presets.find(p_color));
  170. } else {
  171. presets.push_back(p_color);
  172. }
  173. preset->update();
  174. if (presets.size() == 10)
  175. bt_add_preset->hide();
  176. }
  177. void ColorPicker::set_raw_mode(bool p_enabled) {
  178. if (raw_mode_enabled == p_enabled)
  179. return;
  180. raw_mode_enabled = p_enabled;
  181. if (btn_mode->is_pressed() != p_enabled)
  182. btn_mode->set_pressed(p_enabled);
  183. if (!is_inside_tree())
  184. return;
  185. _update_controls();
  186. _update_color();
  187. }
  188. bool ColorPicker::is_raw_mode() const {
  189. return raw_mode_enabled;
  190. }
  191. void ColorPicker::_update_text_value() {
  192. if (text_is_constructor) {
  193. String t = "Color(" + String::num(color.r) + "," + String::num(color.g) + "," + String::num(color.b);
  194. if (edit_alpha && color.a < 1)
  195. t += ("," + String::num(color.a) + ")");
  196. else
  197. t += ")";
  198. c_text->set_text(t);
  199. } else {
  200. c_text->set_text(color.to_html(edit_alpha && color.a < 1));
  201. }
  202. }
  203. void ColorPicker::_sample_draw() {
  204. Rect2 r = Rect2(Point2(), Size2(uv_edit->get_size().width, sample->get_size().height * 0.95));
  205. if (color.a < 1.0) {
  206. sample->draw_texture_rect(get_icon("preset_bg", "ColorPicker"), r, true);
  207. }
  208. sample->draw_rect(r, color);
  209. }
  210. void ColorPicker::_hsv_draw(int p_which, Control *c) {
  211. if (!c)
  212. return;
  213. if (p_which == 0) {
  214. Vector<Point2> points;
  215. points.push_back(Vector2());
  216. points.push_back(Vector2(c->get_size().x, 0));
  217. points.push_back(c->get_size());
  218. points.push_back(Vector2(0, c->get_size().y));
  219. Vector<Color> colors;
  220. colors.push_back(Color(1, 1, 1, 1));
  221. colors.push_back(Color(1, 1, 1, 1));
  222. colors.push_back(Color(0, 0, 0, 1));
  223. colors.push_back(Color(0, 0, 0, 1));
  224. c->draw_polygon(points, colors);
  225. Vector<Color> colors2;
  226. Color col = color;
  227. col.set_hsv(h, 1, 1);
  228. col.a = 0;
  229. colors2.push_back(col);
  230. col.a = 1;
  231. colors2.push_back(col);
  232. col.set_hsv(h, 1, 0);
  233. colors2.push_back(col);
  234. col.a = 0;
  235. colors2.push_back(col);
  236. c->draw_polygon(points, colors2);
  237. int x = CLAMP(c->get_size().x * s, 0, c->get_size().x);
  238. int y = CLAMP(c->get_size().y - c->get_size().y * v, 0, c->get_size().y);
  239. col = color;
  240. col.a = 1;
  241. c->draw_line(Point2(x, 0), Point2(x, c->get_size().y), col.inverted());
  242. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  243. c->draw_line(Point2(x, y), Point2(x, y), Color(1, 1, 1), 2);
  244. } else if (p_which == 1) {
  245. Ref<Texture> hue = get_icon("color_hue", "ColorPicker");
  246. c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
  247. int y = c->get_size().y - c->get_size().y * (1.0 - h);
  248. Color col = Color();
  249. col.set_hsv(h, 1, 1);
  250. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  251. }
  252. }
  253. void ColorPicker::_uv_input(const Ref<InputEvent> &p_event) {
  254. Ref<InputEventMouseButton> bev = p_event;
  255. if (bev.is_valid()) {
  256. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  257. changing_color = true;
  258. float x = CLAMP((float)bev->get_position().x, 0, uv_edit->get_size().width);
  259. float y = CLAMP((float)bev->get_position().y, 0, uv_edit->get_size().height);
  260. s = x / uv_edit->get_size().width;
  261. v = 1.0 - y / uv_edit->get_size().height;
  262. color.set_hsv(h, s, v, color.a);
  263. last_hsv = color;
  264. set_pick_color(color);
  265. _update_color();
  266. emit_signal("color_changed", color);
  267. } else {
  268. changing_color = false;
  269. }
  270. }
  271. Ref<InputEventMouseMotion> mev = p_event;
  272. if (mev.is_valid()) {
  273. if (!changing_color)
  274. return;
  275. float x = CLAMP((float)mev->get_position().x, 0, uv_edit->get_size().width);
  276. float y = CLAMP((float)mev->get_position().y, 0, uv_edit->get_size().height);
  277. s = x / uv_edit->get_size().width;
  278. v = 1.0 - y / uv_edit->get_size().height;
  279. color.set_hsv(h, s, v, color.a);
  280. last_hsv = color;
  281. set_pick_color(color);
  282. _update_color();
  283. emit_signal("color_changed", color);
  284. }
  285. }
  286. void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
  287. Ref<InputEventMouseButton> bev = p_event;
  288. if (bev.is_valid()) {
  289. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  290. changing_color = true;
  291. float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
  292. h = y / w_edit->get_size().height;
  293. } else {
  294. changing_color = false;
  295. }
  296. color.set_hsv(h, s, v, color.a);
  297. last_hsv = color;
  298. set_pick_color(color);
  299. _update_color();
  300. emit_signal("color_changed", color);
  301. }
  302. Ref<InputEventMouseMotion> mev = p_event;
  303. if (mev.is_valid()) {
  304. if (!changing_color)
  305. return;
  306. float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
  307. h = y / w_edit->get_size().height;
  308. color.set_hsv(h, s, v, color.a);
  309. last_hsv = color;
  310. set_pick_color(color);
  311. _update_color();
  312. emit_signal("color_changed", color);
  313. }
  314. }
  315. void ColorPicker::_preset_input(const Ref<InputEvent> &p_event) {
  316. Ref<InputEventMouseButton> bev = p_event;
  317. if (bev.is_valid()) {
  318. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  319. int index = bev->get_position().x / (preset->get_size().x / presets.size());
  320. set_pick_color(presets[index]);
  321. } else if (bev->is_pressed() && bev->get_button_index() == BUTTON_RIGHT) {
  322. int index = bev->get_position().x / (preset->get_size().x / presets.size());
  323. presets.erase(presets[index]);
  324. preset->update();
  325. bt_add_preset->show();
  326. }
  327. _update_color();
  328. emit_signal("color_changed", color);
  329. }
  330. Ref<InputEventMouseMotion> mev = p_event;
  331. if (mev.is_valid()) {
  332. int index = mev->get_position().x * presets.size();
  333. if (preset->get_size().x != 0) {
  334. index /= preset->get_size().x;
  335. }
  336. if (index < 0 || index >= presets.size())
  337. return;
  338. preset->set_tooltip("Color: #" + presets[index].to_html(presets[index].a < 1) + "\n"
  339. "LMB: Set color\n"
  340. "RMB: Remove preset");
  341. }
  342. }
  343. void ColorPicker::_screen_input(const Ref<InputEvent> &p_event) {
  344. Ref<InputEventMouseButton> bev = p_event;
  345. if (bev.is_valid()) {
  346. if (bev->get_button_index() == BUTTON_LEFT && !bev->is_pressed()) {
  347. emit_signal("color_changed", color);
  348. screen->hide();
  349. }
  350. }
  351. Ref<InputEventMouseMotion> mev = p_event;
  352. if (mev.is_valid()) {
  353. Viewport *r = get_tree()->get_root();
  354. if (!r->get_visible_rect().has_point(Point2(mev->get_global_position().x, mev->get_global_position().y)))
  355. return;
  356. Ref<Image> img = r->get_texture()->get_data();
  357. if (img.is_valid() && !img->empty()) {
  358. img->lock();
  359. Vector2 ofs = mev->get_global_position() - r->get_visible_rect().get_position();
  360. Color c = img->get_pixel(ofs.x, r->get_visible_rect().size.height - ofs.y);
  361. img->unlock();
  362. set_pick_color(c);
  363. }
  364. }
  365. }
  366. void ColorPicker::_add_preset_pressed() {
  367. add_preset(color);
  368. }
  369. void ColorPicker::_screen_pick_pressed() {
  370. Viewport *r = get_tree()->get_root();
  371. if (!screen) {
  372. screen = memnew(Control);
  373. r->add_child(screen);
  374. screen->set_as_toplevel(true);
  375. screen->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  376. screen->set_default_cursor_shape(CURSOR_POINTING_HAND);
  377. screen->connect("gui_input", this, "_screen_input");
  378. }
  379. screen->raise();
  380. screen->show_modal();
  381. }
  382. void ColorPicker::_bind_methods() {
  383. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
  384. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
  385. ClassDB::bind_method(D_METHOD("set_raw_mode", "mode"), &ColorPicker::set_raw_mode);
  386. ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
  387. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
  388. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
  389. ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
  390. ClassDB::bind_method(D_METHOD("_value_changed"), &ColorPicker::_value_changed);
  391. ClassDB::bind_method(D_METHOD("_html_entered"), &ColorPicker::_html_entered);
  392. ClassDB::bind_method(D_METHOD("_text_type_toggled"), &ColorPicker::_text_type_toggled);
  393. ClassDB::bind_method(D_METHOD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed);
  394. ClassDB::bind_method(D_METHOD("_screen_pick_pressed"), &ColorPicker::_screen_pick_pressed);
  395. ClassDB::bind_method(D_METHOD("_sample_draw"), &ColorPicker::_sample_draw);
  396. ClassDB::bind_method(D_METHOD("_update_presets"), &ColorPicker::_update_presets);
  397. ClassDB::bind_method(D_METHOD("_hsv_draw"), &ColorPicker::_hsv_draw);
  398. ClassDB::bind_method(D_METHOD("_uv_input"), &ColorPicker::_uv_input);
  399. ClassDB::bind_method(D_METHOD("_w_input"), &ColorPicker::_w_input);
  400. ClassDB::bind_method(D_METHOD("_preset_input"), &ColorPicker::_preset_input);
  401. ClassDB::bind_method(D_METHOD("_screen_input"), &ColorPicker::_screen_input);
  402. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  403. }
  404. ColorPicker::ColorPicker()
  405. : BoxContainer(true) {
  406. updating = true;
  407. edit_alpha = true;
  408. text_is_constructor = false;
  409. raw_mode_enabled = false;
  410. changing_color = false;
  411. screen = NULL;
  412. HBoxContainer *hb_smpl = memnew(HBoxContainer);
  413. btn_pick = memnew(ToolButton);
  414. btn_pick->connect("pressed", this, "_screen_pick_pressed");
  415. sample = memnew(TextureRect);
  416. sample->set_h_size_flags(SIZE_EXPAND_FILL);
  417. sample->connect("draw", this, "_sample_draw");
  418. hb_smpl->add_child(sample);
  419. hb_smpl->add_child(btn_pick);
  420. add_child(hb_smpl);
  421. HBoxContainer *hb_edit = memnew(HBoxContainer);
  422. hb_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  423. uv_edit = memnew(Control);
  424. uv_edit->connect("gui_input", this, "_uv_input");
  425. uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  426. uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  427. uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  428. uv_edit->set_custom_minimum_size(Size2(get_constant("sv_width"), get_constant("sv_height")));
  429. uv_edit->connect("draw", this, "_hsv_draw", make_binds(0, uv_edit));
  430. add_child(hb_edit);
  431. w_edit = memnew(Control);
  432. //w_edit->set_ignore_mouse(false);
  433. w_edit->set_custom_minimum_size(Size2(get_constant("h_width"), 0));
  434. w_edit->set_h_size_flags(SIZE_FILL);
  435. w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  436. w_edit->connect("gui_input", this, "_w_input");
  437. w_edit->connect("draw", this, "_hsv_draw", make_binds(1, w_edit));
  438. hb_edit->add_child(uv_edit);
  439. hb_edit->add_child(memnew(VSeparator));
  440. hb_edit->add_child(w_edit);
  441. VBoxContainer *vbl = memnew(VBoxContainer);
  442. add_child(vbl);
  443. add_child(memnew(HSeparator));
  444. VBoxContainer *vbr = memnew(VBoxContainer);
  445. add_child(vbr);
  446. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  447. const char *lt[4] = { "R", "G", "B", "A" };
  448. for (int i = 0; i < 4; i++) {
  449. HBoxContainer *hbc = memnew(HBoxContainer);
  450. labels[i] = memnew(Label(lt[i]));
  451. labels[i]->set_custom_minimum_size(Size2(get_constant("label_width"), 0));
  452. labels[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  453. hbc->add_child(labels[i]);
  454. scroll[i] = memnew(HSlider);
  455. scroll[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  456. hbc->add_child(scroll[i]);
  457. values[i] = memnew(SpinBox);
  458. scroll[i]->share(values[i]);
  459. hbc->add_child(values[i]);
  460. scroll[i]->set_min(0);
  461. scroll[i]->set_page(0);
  462. scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  463. scroll[i]->connect("value_changed", this, "_value_changed");
  464. vbr->add_child(hbc);
  465. }
  466. HBoxContainer *hhb = memnew(HBoxContainer);
  467. btn_mode = memnew(CheckButton);
  468. btn_mode->set_text(TTR("Raw Mode"));
  469. btn_mode->connect("toggled", this, "set_raw_mode");
  470. hhb->add_child(btn_mode);
  471. vbr->add_child(hhb);
  472. text_type = memnew(Button);
  473. text_type->set_flat(true);
  474. text_type->connect("pressed", this, "_text_type_toggled");
  475. hhb->add_child(text_type);
  476. c_text = memnew(LineEdit);
  477. hhb->add_child(c_text);
  478. c_text->connect("text_entered", this, "_html_entered");
  479. text_type->set_text("#");
  480. c_text->set_h_size_flags(SIZE_EXPAND_FILL);
  481. _update_controls();
  482. //_update_color();
  483. updating = false;
  484. set_pick_color(Color(1, 1, 1));
  485. HBoxContainer *bbc = memnew(HBoxContainer);
  486. add_child(bbc);
  487. preset = memnew(TextureRect);
  488. bbc->add_child(preset);
  489. //preset->set_ignore_mouse(false);
  490. preset->connect("gui_input", this, "_preset_input");
  491. preset->connect("draw", this, "_update_presets");
  492. bt_add_preset = memnew(Button);
  493. bt_add_preset->set_tooltip(TTR("Add current color as a preset"));
  494. bt_add_preset->connect("pressed", this, "_add_preset_pressed");
  495. bbc->add_child(bt_add_preset);
  496. }
  497. /////////////////
  498. void ColorPickerButton::_color_changed(const Color &p_color) {
  499. update();
  500. emit_signal("color_changed", p_color);
  501. }
  502. void ColorPickerButton::pressed() {
  503. popup->set_position(get_global_position() - picker->get_combined_minimum_size());
  504. popup->popup();
  505. picker->set_focus_on_line_edit();
  506. }
  507. void ColorPickerButton::_notification(int p_what) {
  508. if (p_what == NOTIFICATION_DRAW) {
  509. Ref<StyleBox> normal = get_stylebox("normal");
  510. Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
  511. draw_texture_rect(Control::get_icon("bg", "ColorPickerButton"), r, true);
  512. draw_rect(r, picker->get_pick_color());
  513. }
  514. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST) {
  515. popup->hide();
  516. }
  517. }
  518. void ColorPickerButton::set_pick_color(const Color &p_color) {
  519. picker->set_pick_color(p_color);
  520. update();
  521. emit_signal("color_changed", p_color);
  522. }
  523. Color ColorPickerButton::get_pick_color() const {
  524. return picker->get_pick_color();
  525. }
  526. void ColorPickerButton::set_edit_alpha(bool p_show) {
  527. picker->set_edit_alpha(p_show);
  528. }
  529. bool ColorPickerButton::is_editing_alpha() const {
  530. return picker->is_editing_alpha();
  531. }
  532. ColorPicker *ColorPickerButton::get_picker() {
  533. return picker;
  534. }
  535. void ColorPickerButton::_bind_methods() {
  536. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
  537. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
  538. ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
  539. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
  540. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
  541. ClassDB::bind_method(D_METHOD("_color_changed"), &ColorPickerButton::_color_changed);
  542. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  543. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  544. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  545. }
  546. ColorPickerButton::ColorPickerButton() {
  547. popup = memnew(PopupPanel);
  548. picker = memnew(ColorPicker);
  549. popup->add_child(picker);
  550. picker->connect("color_changed", this, "_color_changed");
  551. add_child(popup);
  552. }