color_picker.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. /*************************************************************************/
  2. /* color_picker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/os/input.h"
  32. #include "core/os/keyboard.h"
  33. #include "core/os/os.h"
  34. #ifdef TOOLS_ENABLED
  35. #include "editor_settings.h"
  36. #endif
  37. #include "scene/gui/separator.h"
  38. #include "scene/main/viewport.h"
  39. void ColorPicker::_notification(int p_what) {
  40. switch (p_what) {
  41. case NOTIFICATION_THEME_CHANGED: {
  42. btn_pick->set_icon(get_icon("screen_picker", "ColorPicker"));
  43. bt_add_preset->set_icon(get_icon("add_preset"));
  44. _update_controls();
  45. } break;
  46. case NOTIFICATION_ENTER_TREE: {
  47. btn_pick->set_icon(get_icon("screen_picker", "ColorPicker"));
  48. bt_add_preset->set_icon(get_icon("add_preset"));
  49. _update_color();
  50. #ifdef TOOLS_ENABLED
  51. if (Engine::get_singleton()->is_editor_hint()) {
  52. PoolColorArray saved_presets = EditorSettings::get_singleton()->get_project_metadata("color_picker", "presets", PoolColorArray());
  53. for (int i = 0; i < saved_presets.size(); i++) {
  54. add_preset(saved_presets[i]);
  55. }
  56. }
  57. #endif
  58. } break;
  59. case NOTIFICATION_PARENTED: {
  60. for (int i = 0; i < 4; i++)
  61. set_margin((Margin)i, get_constant("margin"));
  62. } break;
  63. case NOTIFICATION_VISIBILITY_CHANGED: {
  64. Popup *p = Object::cast_to<Popup>(get_parent());
  65. if (p)
  66. p->set_size(Size2(get_combined_minimum_size().width + get_constant("margin") * 2, get_combined_minimum_size().height + get_constant("margin") * 2));
  67. } break;
  68. case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: {
  69. if (screen != NULL) {
  70. if (screen->is_visible()) {
  71. screen->hide();
  72. }
  73. }
  74. } break;
  75. }
  76. }
  77. void ColorPicker::set_focus_on_line_edit() {
  78. c_text->call_deferred("grab_focus");
  79. }
  80. void ColorPicker::_update_controls() {
  81. if (edit_alpha) {
  82. values[3]->show();
  83. scroll[3]->show();
  84. labels[3]->show();
  85. } else {
  86. values[3]->hide();
  87. scroll[3]->hide();
  88. labels[3]->hide();
  89. }
  90. }
  91. void ColorPicker::set_pick_color(const Color &p_color) {
  92. color = p_color;
  93. if (color != last_hsv) {
  94. h = color.get_h();
  95. s = color.get_s();
  96. v = color.get_v();
  97. last_hsv = color;
  98. }
  99. if (!is_inside_tree())
  100. return;
  101. _update_color();
  102. }
  103. void ColorPicker::set_edit_alpha(bool p_show) {
  104. edit_alpha = p_show;
  105. _update_controls();
  106. if (!is_inside_tree())
  107. return;
  108. _update_color();
  109. sample->update();
  110. }
  111. bool ColorPicker::is_editing_alpha() const {
  112. return edit_alpha;
  113. }
  114. void ColorPicker::_value_changed(double) {
  115. if (updating)
  116. return;
  117. for (int i = 0; i < 4; i++) {
  118. color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0);
  119. }
  120. set_pick_color(color);
  121. emit_signal("color_changed", color);
  122. }
  123. void ColorPicker::_html_entered(const String &p_html) {
  124. if (updating)
  125. return;
  126. float last_alpha = color.a;
  127. color = Color::html(p_html);
  128. if (!is_editing_alpha())
  129. color.a = last_alpha;
  130. if (!is_inside_tree())
  131. return;
  132. set_pick_color(color);
  133. emit_signal("color_changed", color);
  134. }
  135. void ColorPicker::_update_color() {
  136. updating = true;
  137. for (int i = 0; i < 4; i++) {
  138. scroll[i]->set_step(0.01);
  139. if (raw_mode_enabled) {
  140. scroll[i]->set_max(100);
  141. if (i == 3)
  142. scroll[i]->set_max(1);
  143. scroll[i]->set_value(color.components[i]);
  144. } else {
  145. const int byte_value = color.components[i] * 255;
  146. scroll[i]->set_max(next_power_of_2(MAX(255, byte_value)) - 1);
  147. scroll[i]->set_value(byte_value);
  148. }
  149. }
  150. _update_text_value();
  151. sample->update();
  152. uv_edit->update();
  153. w_edit->update();
  154. updating = false;
  155. }
  156. void ColorPicker::_update_presets() {
  157. Size2 size = bt_add_preset->get_size();
  158. Size2 preset_size = Size2(size.width * presets.size(), size.height);
  159. preset->set_custom_minimum_size(preset_size);
  160. preset->draw_texture_rect(get_icon("preset_bg", "ColorPicker"), Rect2(Point2(), preset_size), true);
  161. for (int i = 0; i < presets.size(); i++) {
  162. preset->draw_rect(Rect2(Point2(size.width * i, 0), size), presets[i]);
  163. }
  164. }
  165. void ColorPicker::_text_type_toggled() {
  166. if (!Engine::get_singleton()->is_editor_hint())
  167. return;
  168. text_is_constructor = !text_is_constructor;
  169. if (text_is_constructor) {
  170. text_type->set_text("");
  171. text_type->set_icon(get_icon("Script", "EditorIcons"));
  172. } else {
  173. text_type->set_text("#");
  174. text_type->set_icon(NULL);
  175. }
  176. _update_color();
  177. }
  178. Color ColorPicker::get_pick_color() const {
  179. return color;
  180. }
  181. void ColorPicker::add_preset(const Color &p_color) {
  182. if (presets.find(p_color)) {
  183. presets.move_to_back(presets.find(p_color));
  184. } else {
  185. presets.push_back(p_color);
  186. }
  187. preset->update();
  188. if (presets.size() == 10)
  189. bt_add_preset->hide();
  190. #ifdef TOOLS_ENABLED
  191. if (Engine::get_singleton()->is_editor_hint()) {
  192. PoolColorArray arr_to_save = get_presets();
  193. EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
  194. }
  195. #endif
  196. }
  197. void ColorPicker::erase_preset(const Color &p_color) {
  198. if (presets.find(p_color)) {
  199. presets.erase(presets.find(p_color));
  200. preset->update();
  201. #ifdef TOOLS_ENABLED
  202. if (Engine::get_singleton()->is_editor_hint()) {
  203. PoolColorArray arr_to_save = get_presets();
  204. EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
  205. }
  206. #endif
  207. }
  208. }
  209. PoolColorArray ColorPicker::get_presets() const {
  210. PoolColorArray arr;
  211. arr.resize(presets.size());
  212. for (int i = 0; i < presets.size(); i++) {
  213. arr.set(i, presets[i]);
  214. }
  215. return arr;
  216. }
  217. void ColorPicker::set_raw_mode(bool p_enabled) {
  218. if (raw_mode_enabled == p_enabled)
  219. return;
  220. raw_mode_enabled = p_enabled;
  221. if (btn_mode->is_pressed() != p_enabled)
  222. btn_mode->set_pressed(p_enabled);
  223. if (!is_inside_tree())
  224. return;
  225. _update_controls();
  226. _update_color();
  227. }
  228. bool ColorPicker::is_raw_mode() const {
  229. return raw_mode_enabled;
  230. }
  231. void ColorPicker::set_deferred_mode(bool p_enabled) {
  232. deferred_mode_enabled = p_enabled;
  233. }
  234. bool ColorPicker::is_deferred_mode() const {
  235. return deferred_mode_enabled;
  236. }
  237. void ColorPicker::_update_text_value() {
  238. bool visible = true;
  239. if (text_is_constructor) {
  240. String t = "Color(" + String::num(color.r) + "," + String::num(color.g) + "," + String::num(color.b);
  241. if (edit_alpha && color.a < 1)
  242. t += ("," + String::num(color.a) + ")");
  243. else
  244. t += ")";
  245. c_text->set_text(t);
  246. } else {
  247. if (color.r > 1 || color.g > 1 || color.b > 1 || color.r < 0 || color.g < 0 || color.b < 0) {
  248. visible = false;
  249. } else {
  250. c_text->set_text(color.to_html(edit_alpha && color.a < 1));
  251. }
  252. }
  253. c_text->set_visible(visible);
  254. }
  255. void ColorPicker::_sample_draw() {
  256. Rect2 r = Rect2(Point2(), Size2(uv_edit->get_size().width, sample->get_size().height * 0.95));
  257. if (color.a < 1.0) {
  258. sample->draw_texture_rect(get_icon("preset_bg", "ColorPicker"), r, true);
  259. }
  260. sample->draw_rect(r, color);
  261. }
  262. void ColorPicker::_hsv_draw(int p_which, Control *c) {
  263. if (!c)
  264. return;
  265. if (p_which == 0) {
  266. Vector<Point2> points;
  267. points.push_back(Vector2());
  268. points.push_back(Vector2(c->get_size().x, 0));
  269. points.push_back(c->get_size());
  270. points.push_back(Vector2(0, c->get_size().y));
  271. Vector<Color> colors;
  272. colors.push_back(Color(1, 1, 1, 1));
  273. colors.push_back(Color(1, 1, 1, 1));
  274. colors.push_back(Color(0, 0, 0, 1));
  275. colors.push_back(Color(0, 0, 0, 1));
  276. c->draw_polygon(points, colors);
  277. Vector<Color> colors2;
  278. Color col = color;
  279. col.set_hsv(h, 1, 1);
  280. col.a = 0;
  281. colors2.push_back(col);
  282. col.a = 1;
  283. colors2.push_back(col);
  284. col.set_hsv(h, 1, 0);
  285. colors2.push_back(col);
  286. col.a = 0;
  287. colors2.push_back(col);
  288. c->draw_polygon(points, colors2);
  289. int x = CLAMP(c->get_size().x * s, 0, c->get_size().x);
  290. int y = CLAMP(c->get_size().y - c->get_size().y * v, 0, c->get_size().y);
  291. col = color;
  292. col.a = 1;
  293. c->draw_line(Point2(x, 0), Point2(x, c->get_size().y), col.inverted());
  294. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  295. c->draw_line(Point2(x, y), Point2(x, y), Color(1, 1, 1), 2);
  296. } else if (p_which == 1) {
  297. Ref<Texture> hue = get_icon("color_hue", "ColorPicker");
  298. c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
  299. int y = c->get_size().y - c->get_size().y * (1.0 - h);
  300. Color col = Color();
  301. col.set_hsv(h, 1, 1);
  302. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  303. }
  304. }
  305. void ColorPicker::_uv_input(const Ref<InputEvent> &p_event) {
  306. Ref<InputEventMouseButton> bev = p_event;
  307. if (bev.is_valid()) {
  308. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  309. changing_color = true;
  310. float x = CLAMP((float)bev->get_position().x, 0, uv_edit->get_size().width);
  311. float y = CLAMP((float)bev->get_position().y, 0, uv_edit->get_size().height);
  312. s = x / uv_edit->get_size().width;
  313. v = 1.0 - y / uv_edit->get_size().height;
  314. color.set_hsv(h, s, v, color.a);
  315. last_hsv = color;
  316. set_pick_color(color);
  317. _update_color();
  318. if (!deferred_mode_enabled)
  319. emit_signal("color_changed", color);
  320. } else if (deferred_mode_enabled && !bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  321. emit_signal("color_changed", color);
  322. changing_color = false;
  323. } else {
  324. changing_color = false;
  325. }
  326. }
  327. Ref<InputEventMouseMotion> mev = p_event;
  328. if (mev.is_valid()) {
  329. if (!changing_color)
  330. return;
  331. float x = CLAMP((float)mev->get_position().x, 0, uv_edit->get_size().width);
  332. float y = CLAMP((float)mev->get_position().y, 0, uv_edit->get_size().height);
  333. s = x / uv_edit->get_size().width;
  334. v = 1.0 - y / uv_edit->get_size().height;
  335. color.set_hsv(h, s, v, color.a);
  336. last_hsv = color;
  337. set_pick_color(color);
  338. _update_color();
  339. if (!deferred_mode_enabled)
  340. emit_signal("color_changed", color);
  341. }
  342. }
  343. void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
  344. Ref<InputEventMouseButton> bev = p_event;
  345. if (bev.is_valid()) {
  346. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  347. changing_color = true;
  348. float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
  349. h = y / w_edit->get_size().height;
  350. } else {
  351. changing_color = false;
  352. }
  353. color.set_hsv(h, s, v, color.a);
  354. last_hsv = color;
  355. set_pick_color(color);
  356. _update_color();
  357. if (!deferred_mode_enabled)
  358. emit_signal("color_changed", color);
  359. else if (!bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT)
  360. emit_signal("color_changed", color);
  361. }
  362. Ref<InputEventMouseMotion> mev = p_event;
  363. if (mev.is_valid()) {
  364. if (!changing_color)
  365. return;
  366. float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
  367. h = y / w_edit->get_size().height;
  368. color.set_hsv(h, s, v, color.a);
  369. last_hsv = color;
  370. set_pick_color(color);
  371. _update_color();
  372. if (!deferred_mode_enabled)
  373. emit_signal("color_changed", color);
  374. }
  375. }
  376. void ColorPicker::_preset_input(const Ref<InputEvent> &p_event) {
  377. Ref<InputEventMouseButton> bev = p_event;
  378. if (bev.is_valid()) {
  379. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  380. int index = bev->get_position().x / (preset->get_size().x / presets.size());
  381. set_pick_color(presets[index]);
  382. _update_color();
  383. emit_signal("color_changed", color);
  384. } else if (bev->is_pressed() && bev->get_button_index() == BUTTON_RIGHT) {
  385. int index = bev->get_position().x / (preset->get_size().x / presets.size());
  386. Color clicked_preset = presets[index];
  387. erase_preset(clicked_preset);
  388. emit_signal("preset_removed", clicked_preset);
  389. bt_add_preset->show();
  390. }
  391. }
  392. Ref<InputEventMouseMotion> mev = p_event;
  393. if (mev.is_valid()) {
  394. int index = mev->get_position().x * presets.size();
  395. if (preset->get_size().x != 0) {
  396. index /= preset->get_size().x;
  397. }
  398. if (index < 0 || index >= presets.size())
  399. return;
  400. preset->set_tooltip("Color: #" + presets[index].to_html(presets[index].a < 1) + "\n"
  401. "LMB: Set color\n"
  402. "RMB: Remove preset");
  403. }
  404. }
  405. void ColorPicker::_screen_input(const Ref<InputEvent> &p_event) {
  406. Ref<InputEventMouseButton> bev = p_event;
  407. if (bev.is_valid()) {
  408. if (bev->get_button_index() == BUTTON_LEFT && !bev->is_pressed()) {
  409. emit_signal("color_changed", color);
  410. screen->hide();
  411. }
  412. }
  413. Ref<InputEventMouseMotion> mev = p_event;
  414. if (mev.is_valid()) {
  415. Viewport *r = get_tree()->get_root();
  416. if (!r->get_visible_rect().has_point(Point2(mev->get_global_position().x, mev->get_global_position().y)))
  417. return;
  418. Ref<Image> img = r->get_texture()->get_data();
  419. if (img.is_valid() && !img->empty()) {
  420. img->lock();
  421. Vector2 ofs = mev->get_global_position() - r->get_visible_rect().get_position();
  422. Color c = img->get_pixel(ofs.x, r->get_visible_rect().size.height - ofs.y);
  423. img->unlock();
  424. set_pick_color(c);
  425. }
  426. }
  427. }
  428. void ColorPicker::_add_preset_pressed() {
  429. add_preset(color);
  430. emit_signal("preset_added", color);
  431. }
  432. void ColorPicker::_screen_pick_pressed() {
  433. Viewport *r = get_tree()->get_root();
  434. if (!screen) {
  435. screen = memnew(Control);
  436. r->add_child(screen);
  437. screen->set_as_toplevel(true);
  438. screen->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  439. screen->set_default_cursor_shape(CURSOR_POINTING_HAND);
  440. screen->connect("gui_input", this, "_screen_input");
  441. }
  442. screen->raise();
  443. screen->show_modal();
  444. }
  445. void ColorPicker::_focus_enter() {
  446. if (c_text->has_focus()) {
  447. c_text->select_all();
  448. return;
  449. }
  450. for (int i = 0; i < 4; i++) {
  451. if (values[i]->get_line_edit()->has_focus()) {
  452. values[i]->get_line_edit()->select_all();
  453. break;
  454. }
  455. }
  456. }
  457. void ColorPicker::_focus_exit() {
  458. for (int i = 0; i < 4; i++) {
  459. values[i]->get_line_edit()->select(0, 0);
  460. }
  461. c_text->select(0, 0);
  462. }
  463. void ColorPicker::_html_focus_exit() {
  464. _html_entered(c_text->get_text());
  465. _focus_exit();
  466. }
  467. void ColorPicker::_bind_methods() {
  468. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
  469. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
  470. ClassDB::bind_method(D_METHOD("set_raw_mode", "mode"), &ColorPicker::set_raw_mode);
  471. ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
  472. ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
  473. ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
  474. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
  475. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
  476. ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
  477. ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
  478. ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
  479. ClassDB::bind_method(D_METHOD("_value_changed"), &ColorPicker::_value_changed);
  480. ClassDB::bind_method(D_METHOD("_html_entered"), &ColorPicker::_html_entered);
  481. ClassDB::bind_method(D_METHOD("_text_type_toggled"), &ColorPicker::_text_type_toggled);
  482. ClassDB::bind_method(D_METHOD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed);
  483. ClassDB::bind_method(D_METHOD("_screen_pick_pressed"), &ColorPicker::_screen_pick_pressed);
  484. ClassDB::bind_method(D_METHOD("_sample_draw"), &ColorPicker::_sample_draw);
  485. ClassDB::bind_method(D_METHOD("_update_presets"), &ColorPicker::_update_presets);
  486. ClassDB::bind_method(D_METHOD("_hsv_draw"), &ColorPicker::_hsv_draw);
  487. ClassDB::bind_method(D_METHOD("_uv_input"), &ColorPicker::_uv_input);
  488. ClassDB::bind_method(D_METHOD("_w_input"), &ColorPicker::_w_input);
  489. ClassDB::bind_method(D_METHOD("_preset_input"), &ColorPicker::_preset_input);
  490. ClassDB::bind_method(D_METHOD("_screen_input"), &ColorPicker::_screen_input);
  491. ClassDB::bind_method(D_METHOD("_focus_enter"), &ColorPicker::_focus_enter);
  492. ClassDB::bind_method(D_METHOD("_focus_exit"), &ColorPicker::_focus_exit);
  493. ClassDB::bind_method(D_METHOD("_html_focus_exit"), &ColorPicker::_html_focus_exit);
  494. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  495. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  496. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "raw_mode"), "set_raw_mode", "is_raw_mode");
  497. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
  498. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  499. ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
  500. ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
  501. }
  502. ColorPicker::ColorPicker() :
  503. BoxContainer(true) {
  504. updating = true;
  505. edit_alpha = true;
  506. text_is_constructor = false;
  507. raw_mode_enabled = false;
  508. deferred_mode_enabled = false;
  509. changing_color = false;
  510. screen = NULL;
  511. HBoxContainer *hb_smpl = memnew(HBoxContainer);
  512. btn_pick = memnew(ToolButton);
  513. btn_pick->connect("pressed", this, "_screen_pick_pressed");
  514. sample = memnew(TextureRect);
  515. sample->set_h_size_flags(SIZE_EXPAND_FILL);
  516. sample->connect("draw", this, "_sample_draw");
  517. hb_smpl->add_child(sample);
  518. hb_smpl->add_child(btn_pick);
  519. add_child(hb_smpl);
  520. HBoxContainer *hb_edit = memnew(HBoxContainer);
  521. hb_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  522. uv_edit = memnew(Control);
  523. uv_edit->connect("gui_input", this, "_uv_input");
  524. uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  525. uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  526. uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  527. uv_edit->set_custom_minimum_size(Size2(get_constant("sv_width"), get_constant("sv_height")));
  528. uv_edit->connect("draw", this, "_hsv_draw", make_binds(0, uv_edit));
  529. add_child(hb_edit);
  530. w_edit = memnew(Control);
  531. w_edit->set_custom_minimum_size(Size2(get_constant("h_width"), 0));
  532. w_edit->set_h_size_flags(SIZE_FILL);
  533. w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  534. w_edit->connect("gui_input", this, "_w_input");
  535. w_edit->connect("draw", this, "_hsv_draw", make_binds(1, w_edit));
  536. hb_edit->add_child(uv_edit);
  537. hb_edit->add_child(memnew(VSeparator));
  538. hb_edit->add_child(w_edit);
  539. VBoxContainer *vbl = memnew(VBoxContainer);
  540. add_child(vbl);
  541. add_child(memnew(HSeparator));
  542. VBoxContainer *vbr = memnew(VBoxContainer);
  543. add_child(vbr);
  544. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  545. const char *lt[4] = { "R", "G", "B", "A" };
  546. for (int i = 0; i < 4; i++) {
  547. HBoxContainer *hbc = memnew(HBoxContainer);
  548. labels[i] = memnew(Label(lt[i]));
  549. labels[i]->set_custom_minimum_size(Size2(get_constant("label_width"), 0));
  550. labels[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  551. hbc->add_child(labels[i]);
  552. scroll[i] = memnew(HSlider);
  553. scroll[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  554. scroll[i]->set_focus_mode(FOCUS_NONE);
  555. hbc->add_child(scroll[i]);
  556. values[i] = memnew(SpinBox);
  557. scroll[i]->share(values[i]);
  558. hbc->add_child(values[i]);
  559. values[i]->get_line_edit()->connect("focus_entered", this, "_focus_enter");
  560. values[i]->get_line_edit()->connect("focus_exited", this, "_focus_exit");
  561. scroll[i]->set_min(0);
  562. scroll[i]->set_page(0);
  563. scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  564. scroll[i]->connect("value_changed", this, "_value_changed");
  565. vbr->add_child(hbc);
  566. }
  567. HBoxContainer *hhb = memnew(HBoxContainer);
  568. btn_mode = memnew(CheckButton);
  569. btn_mode->set_text(TTR("Raw Mode"));
  570. btn_mode->connect("toggled", this, "set_raw_mode");
  571. hhb->add_child(btn_mode);
  572. vbr->add_child(hhb);
  573. text_type = memnew(Button);
  574. text_type->set_flat(true);
  575. text_type->connect("pressed", this, "_text_type_toggled");
  576. hhb->add_child(text_type);
  577. c_text = memnew(LineEdit);
  578. hhb->add_child(c_text);
  579. c_text->connect("text_entered", this, "_html_entered");
  580. c_text->connect("focus_entered", this, "_focus_enter");
  581. c_text->connect("focus_exited", this, "_html_focus_exit");
  582. text_type->set_text("#");
  583. c_text->set_h_size_flags(SIZE_EXPAND_FILL);
  584. _update_controls();
  585. updating = false;
  586. set_pick_color(Color(1, 1, 1));
  587. HBoxContainer *bbc = memnew(HBoxContainer);
  588. add_child(bbc);
  589. preset = memnew(TextureRect);
  590. bbc->add_child(preset);
  591. preset->connect("gui_input", this, "_preset_input");
  592. preset->connect("draw", this, "_update_presets");
  593. bt_add_preset = memnew(Button);
  594. bt_add_preset->set_tooltip(TTR("Add current color as a preset"));
  595. bt_add_preset->connect("pressed", this, "_add_preset_pressed");
  596. bbc->add_child(bt_add_preset);
  597. }
  598. /////////////////
  599. void ColorPickerButton::_color_changed(const Color &p_color) {
  600. color = p_color;
  601. update();
  602. emit_signal("color_changed", color);
  603. }
  604. void ColorPickerButton::_modal_closed() {
  605. emit_signal("popup_closed");
  606. }
  607. void ColorPickerButton::pressed() {
  608. _update_picker();
  609. popup->set_position(get_global_position() - picker->get_combined_minimum_size());
  610. popup->popup();
  611. picker->set_focus_on_line_edit();
  612. }
  613. void ColorPickerButton::_notification(int p_what) {
  614. if (p_what == NOTIFICATION_DRAW) {
  615. Ref<StyleBox> normal = get_stylebox("normal");
  616. Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
  617. draw_texture_rect(Control::get_icon("bg", "ColorPickerButton"), r, true);
  618. draw_rect(r, color);
  619. }
  620. if (p_what == MainLoop::NOTIFICATION_WM_QUIT_REQUEST && popup) {
  621. popup->hide();
  622. }
  623. }
  624. void ColorPickerButton::set_pick_color(const Color &p_color) {
  625. color = p_color;
  626. if (picker) {
  627. picker->set_pick_color(p_color);
  628. }
  629. update();
  630. }
  631. Color ColorPickerButton::get_pick_color() const {
  632. return color;
  633. }
  634. void ColorPickerButton::set_edit_alpha(bool p_show) {
  635. edit_alpha = p_show;
  636. if (picker) {
  637. picker->set_edit_alpha(p_show);
  638. }
  639. }
  640. bool ColorPickerButton::is_editing_alpha() const {
  641. return edit_alpha;
  642. }
  643. ColorPicker *ColorPickerButton::get_picker() {
  644. _update_picker();
  645. return picker;
  646. }
  647. PopupPanel *ColorPickerButton::get_popup() {
  648. _update_picker();
  649. return popup;
  650. }
  651. void ColorPickerButton::_update_picker() {
  652. if (!picker) {
  653. popup = memnew(PopupPanel);
  654. picker = memnew(ColorPicker);
  655. popup->add_child(picker);
  656. add_child(popup);
  657. picker->connect("color_changed", this, "_color_changed");
  658. popup->connect("modal_closed", this, "_modal_closed");
  659. picker->set_pick_color(color);
  660. picker->set_edit_alpha(edit_alpha);
  661. }
  662. }
  663. void ColorPickerButton::_bind_methods() {
  664. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
  665. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
  666. ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
  667. ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
  668. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
  669. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
  670. ClassDB::bind_method(D_METHOD("_color_changed"), &ColorPickerButton::_color_changed);
  671. ClassDB::bind_method(D_METHOD("_modal_closed"), &ColorPickerButton::_modal_closed);
  672. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  673. ADD_SIGNAL(MethodInfo("popup_closed"));
  674. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  675. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  676. }
  677. ColorPickerButton::ColorPickerButton() {
  678. //Initialization is now done deferred
  679. //this improves performance in the inspector as the color picker
  680. //can be expensive to initialize
  681. picker = NULL;
  682. popup = NULL;
  683. edit_alpha = true;
  684. }