color_picker.cpp 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /*************************************************************************/
  2. /* color_picker.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #endif
  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_controls();
  50. _update_color();
  51. #ifdef TOOLS_ENABLED
  52. if (Engine::get_singleton()->is_editor_hint()) {
  53. PoolColorArray saved_presets = EditorSettings::get_singleton()->get_project_metadata("color_picker", "presets", PoolColorArray());
  54. for (int i = 0; i < saved_presets.size(); i++) {
  55. add_preset(saved_presets[i]);
  56. }
  57. }
  58. #endif
  59. } break;
  60. case NOTIFICATION_PARENTED: {
  61. for (int i = 0; i < 4; i++)
  62. set_margin((Margin)i, get_margin((Margin)i) + get_constant("margin"));
  63. } break;
  64. case NOTIFICATION_VISIBILITY_CHANGED: {
  65. Popup *p = Object::cast_to<Popup>(get_parent());
  66. if (p)
  67. p->set_size(Size2(get_combined_minimum_size().width + get_constant("margin") * 2, get_combined_minimum_size().height + get_constant("margin") * 2));
  68. } break;
  69. case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: {
  70. if (screen != NULL && screen->is_visible())
  71. screen->hide();
  72. } break;
  73. }
  74. }
  75. void ColorPicker::set_focus_on_line_edit() {
  76. c_text->call_deferred("grab_focus");
  77. }
  78. void ColorPicker::_update_controls() {
  79. const char *rgb[3] = { "R", "G", "B" };
  80. const char *hsv[3] = { "H", "S", "V" };
  81. if (hsv_mode_enabled) {
  82. for (int i = 0; i < 3; i++)
  83. labels[i]->set_text(hsv[i]);
  84. } else {
  85. for (int i = 0; i < 3; i++)
  86. labels[i]->set_text(rgb[i]);
  87. }
  88. if (hsv_mode_enabled) {
  89. set_raw_mode(false);
  90. btn_raw->set_disabled(true);
  91. } else if (raw_mode_enabled) {
  92. set_hsv_mode(false);
  93. btn_hsv->set_disabled(true);
  94. } else {
  95. btn_raw->set_disabled(false);
  96. btn_hsv->set_disabled(false);
  97. }
  98. if (edit_alpha) {
  99. values[3]->show();
  100. scroll[3]->show();
  101. labels[3]->show();
  102. } else {
  103. values[3]->hide();
  104. scroll[3]->hide();
  105. labels[3]->hide();
  106. }
  107. }
  108. void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
  109. color = p_color;
  110. if (color != last_hsv) {
  111. h = color.get_h();
  112. s = color.get_s();
  113. v = color.get_v();
  114. last_hsv = color;
  115. }
  116. if (!is_inside_tree())
  117. return;
  118. _update_color(p_update_sliders);
  119. }
  120. void ColorPicker::set_pick_color(const Color &p_color) {
  121. _set_pick_color(p_color, true); //because setters can't have more arguments
  122. }
  123. void ColorPicker::set_edit_alpha(bool p_show) {
  124. edit_alpha = p_show;
  125. _update_controls();
  126. if (!is_inside_tree())
  127. return;
  128. _update_color();
  129. sample->update();
  130. }
  131. bool ColorPicker::is_editing_alpha() const {
  132. return edit_alpha;
  133. }
  134. void ColorPicker::_value_changed(double) {
  135. if (updating)
  136. return;
  137. if (hsv_mode_enabled) {
  138. h = scroll[0]->get_value() / 360.0;
  139. s = scroll[1]->get_value() / 100.0;
  140. v = scroll[2]->get_value() / 100.0;
  141. color.set_hsv(h, s, v, scroll[3]->get_value() / 255.0);
  142. last_hsv = color;
  143. } else {
  144. for (int i = 0; i < 4; i++) {
  145. color.components[i] = scroll[i]->get_value() / (raw_mode_enabled ? 1.0 : 255.0);
  146. }
  147. }
  148. _set_pick_color(color, false);
  149. emit_signal("color_changed", color);
  150. }
  151. void ColorPicker::_html_entered(const String &p_html) {
  152. if (updating || text_is_constructor || !c_text->is_visible())
  153. return;
  154. float last_alpha = color.a;
  155. color = Color::html(p_html);
  156. if (!is_editing_alpha())
  157. color.a = last_alpha;
  158. if (!is_inside_tree())
  159. return;
  160. set_pick_color(color);
  161. emit_signal("color_changed", color);
  162. }
  163. void ColorPicker::_update_color(bool p_update_sliders) {
  164. updating = true;
  165. if (p_update_sliders) {
  166. if (hsv_mode_enabled) {
  167. for (int i = 0; i < 4; i++) {
  168. scroll[i]->set_step(1.0);
  169. }
  170. scroll[0]->set_max(359);
  171. scroll[0]->set_value(h * 360.0);
  172. scroll[1]->set_max(100);
  173. scroll[1]->set_value(s * 100.0);
  174. scroll[2]->set_max(100);
  175. scroll[2]->set_value(v * 100.0);
  176. scroll[3]->set_max(255);
  177. scroll[3]->set_value(color.components[3] * 255.0);
  178. } else {
  179. for (int i = 0; i < 4; i++) {
  180. if (raw_mode_enabled) {
  181. scroll[i]->set_step(0.01);
  182. scroll[i]->set_max(100);
  183. if (i == 3)
  184. scroll[i]->set_max(1);
  185. scroll[i]->set_value(color.components[i]);
  186. } else {
  187. scroll[i]->set_step(1);
  188. const float byte_value = color.components[i] * 255.0;
  189. scroll[i]->set_max(next_power_of_2(MAX(255, byte_value)) - 1);
  190. scroll[i]->set_value(byte_value);
  191. }
  192. }
  193. }
  194. }
  195. _update_text_value();
  196. sample->update();
  197. uv_edit->update();
  198. w_edit->update();
  199. updating = false;
  200. }
  201. void ColorPicker::_update_presets() {
  202. presets_per_row = 10;
  203. Size2 size = bt_add_preset->get_size();
  204. Size2 preset_size = Size2(MIN(size.width * presets.size(), presets_per_row * size.width), size.height * (Math::ceil((float)presets.size() / presets_per_row)));
  205. preset->set_custom_minimum_size(preset_size);
  206. preset_container->set_custom_minimum_size(preset_size);
  207. preset->draw_rect(Rect2(Point2(), preset_size), Color(1, 1, 1, 0));
  208. for (int i = 0; i < presets.size(); i++) {
  209. int x = (i % presets_per_row) * size.width;
  210. int y = (Math::floor((float)i / presets_per_row)) * size.height;
  211. preset->draw_rect(Rect2(Point2(x, y), size), presets[i]);
  212. }
  213. _notification(NOTIFICATION_VISIBILITY_CHANGED);
  214. }
  215. void ColorPicker::_text_type_toggled() {
  216. text_is_constructor = !text_is_constructor;
  217. if (text_is_constructor) {
  218. text_type->set_text("");
  219. text_type->set_icon(get_icon("Script", "EditorIcons"));
  220. c_text->set_editable(false);
  221. } else {
  222. text_type->set_text("#");
  223. text_type->set_icon(NULL);
  224. c_text->set_editable(true);
  225. }
  226. _update_color();
  227. }
  228. Color ColorPicker::get_pick_color() const {
  229. return color;
  230. }
  231. void ColorPicker::add_preset(const Color &p_color) {
  232. if (presets.find(p_color)) {
  233. presets.move_to_back(presets.find(p_color));
  234. } else {
  235. presets.push_back(p_color);
  236. }
  237. preset->update();
  238. #ifdef TOOLS_ENABLED
  239. if (Engine::get_singleton()->is_editor_hint()) {
  240. PoolColorArray arr_to_save = get_presets();
  241. EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
  242. }
  243. #endif
  244. }
  245. void ColorPicker::erase_preset(const Color &p_color) {
  246. if (presets.find(p_color)) {
  247. presets.erase(presets.find(p_color));
  248. preset->update();
  249. #ifdef TOOLS_ENABLED
  250. if (Engine::get_singleton()->is_editor_hint()) {
  251. PoolColorArray arr_to_save = get_presets();
  252. EditorSettings::get_singleton()->set_project_metadata("color_picker", "presets", arr_to_save);
  253. }
  254. #endif
  255. }
  256. }
  257. PoolColorArray ColorPicker::get_presets() const {
  258. PoolColorArray arr;
  259. arr.resize(presets.size());
  260. for (int i = 0; i < presets.size(); i++) {
  261. arr.set(i, presets[i]);
  262. }
  263. return arr;
  264. }
  265. void ColorPicker::set_hsv_mode(bool p_enabled) {
  266. if (hsv_mode_enabled == p_enabled || raw_mode_enabled)
  267. return;
  268. hsv_mode_enabled = p_enabled;
  269. if (btn_hsv->is_pressed() != p_enabled)
  270. btn_hsv->set_pressed(p_enabled);
  271. if (!is_inside_tree())
  272. return;
  273. _update_controls();
  274. _update_color();
  275. }
  276. bool ColorPicker::is_hsv_mode() const {
  277. return hsv_mode_enabled;
  278. }
  279. void ColorPicker::set_raw_mode(bool p_enabled) {
  280. if (raw_mode_enabled == p_enabled || hsv_mode_enabled)
  281. return;
  282. raw_mode_enabled = p_enabled;
  283. if (btn_raw->is_pressed() != p_enabled)
  284. btn_raw->set_pressed(p_enabled);
  285. if (!is_inside_tree())
  286. return;
  287. _update_controls();
  288. _update_color();
  289. }
  290. bool ColorPicker::is_raw_mode() const {
  291. return raw_mode_enabled;
  292. }
  293. void ColorPicker::set_deferred_mode(bool p_enabled) {
  294. deferred_mode_enabled = p_enabled;
  295. }
  296. bool ColorPicker::is_deferred_mode() const {
  297. return deferred_mode_enabled;
  298. }
  299. void ColorPicker::_update_text_value() {
  300. bool visible = true;
  301. if (text_is_constructor) {
  302. String t = "Color(" + String::num(color.r) + ", " + String::num(color.g) + ", " + String::num(color.b);
  303. if (edit_alpha && color.a < 1)
  304. t += ", " + String::num(color.a) + ")";
  305. else
  306. t += ")";
  307. c_text->set_text(t);
  308. }
  309. if (color.r > 1 || color.g > 1 || color.b > 1 || color.r < 0 || color.g < 0 || color.b < 0) {
  310. visible = false;
  311. } else if (!text_is_constructor) {
  312. c_text->set_text(color.to_html(edit_alpha && color.a < 1));
  313. }
  314. text_type->set_visible(visible);
  315. c_text->set_visible(visible);
  316. }
  317. void ColorPicker::_sample_draw() {
  318. const Rect2 r = Rect2(Point2(), Size2(uv_edit->get_size().width, sample->get_size().height * 0.95));
  319. if (color.a < 1.0) {
  320. sample->draw_texture_rect(get_icon("preset_bg", "ColorPicker"), r, true);
  321. }
  322. sample->draw_rect(r, color);
  323. if (color.r > 1 || color.g > 1 || color.b > 1) {
  324. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  325. sample->draw_texture(get_icon("overbright_indicator", "ColorPicker"), Point2());
  326. }
  327. }
  328. void ColorPicker::_hsv_draw(int p_which, Control *c) {
  329. if (!c)
  330. return;
  331. if (p_which == 0) {
  332. Vector<Point2> points;
  333. points.push_back(Vector2());
  334. points.push_back(Vector2(c->get_size().x, 0));
  335. points.push_back(c->get_size());
  336. points.push_back(Vector2(0, c->get_size().y));
  337. Vector<Color> colors;
  338. colors.push_back(Color(1, 1, 1, 1));
  339. colors.push_back(Color(1, 1, 1, 1));
  340. colors.push_back(Color(0, 0, 0, 1));
  341. colors.push_back(Color(0, 0, 0, 1));
  342. c->draw_polygon(points, colors);
  343. Vector<Color> colors2;
  344. Color col = color;
  345. col.set_hsv(h, 1, 1);
  346. col.a = 0;
  347. colors2.push_back(col);
  348. col.a = 1;
  349. colors2.push_back(col);
  350. col.set_hsv(h, 1, 0);
  351. colors2.push_back(col);
  352. col.a = 0;
  353. colors2.push_back(col);
  354. c->draw_polygon(points, colors2);
  355. int x = CLAMP(c->get_size().x * s, 0, c->get_size().x);
  356. int y = CLAMP(c->get_size().y - c->get_size().y * v, 0, c->get_size().y);
  357. col = color;
  358. col.a = 1;
  359. c->draw_line(Point2(x, 0), Point2(x, c->get_size().y), col.inverted());
  360. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  361. c->draw_line(Point2(x, y), Point2(x, y), Color(1, 1, 1), 2);
  362. } else if (p_which == 1) {
  363. Ref<Texture> hue = get_icon("color_hue", "ColorPicker");
  364. c->draw_texture_rect(hue, Rect2(Point2(), c->get_size()));
  365. int y = c->get_size().y - c->get_size().y * (1.0 - h);
  366. Color col = Color();
  367. col.set_hsv(h, 1, 1);
  368. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  369. }
  370. }
  371. void ColorPicker::_uv_input(const Ref<InputEvent> &p_event) {
  372. Ref<InputEventMouseButton> bev = p_event;
  373. if (bev.is_valid()) {
  374. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  375. changing_color = true;
  376. float x = CLAMP((float)bev->get_position().x, 0, uv_edit->get_size().width);
  377. float y = CLAMP((float)bev->get_position().y, 0, uv_edit->get_size().height);
  378. s = x / uv_edit->get_size().width;
  379. v = 1.0 - y / uv_edit->get_size().height;
  380. color.set_hsv(h, s, v, color.a);
  381. last_hsv = color;
  382. set_pick_color(color);
  383. _update_color();
  384. if (!deferred_mode_enabled)
  385. emit_signal("color_changed", color);
  386. } else if (deferred_mode_enabled && !bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  387. emit_signal("color_changed", color);
  388. changing_color = false;
  389. } else {
  390. changing_color = false;
  391. }
  392. }
  393. Ref<InputEventMouseMotion> mev = p_event;
  394. if (mev.is_valid()) {
  395. if (!changing_color)
  396. return;
  397. float x = CLAMP((float)mev->get_position().x, 0, uv_edit->get_size().width);
  398. float y = CLAMP((float)mev->get_position().y, 0, uv_edit->get_size().height);
  399. s = x / uv_edit->get_size().width;
  400. v = 1.0 - y / uv_edit->get_size().height;
  401. color.set_hsv(h, s, v, color.a);
  402. last_hsv = color;
  403. set_pick_color(color);
  404. _update_color();
  405. if (!deferred_mode_enabled)
  406. emit_signal("color_changed", color);
  407. }
  408. }
  409. void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
  410. Ref<InputEventMouseButton> bev = p_event;
  411. if (bev.is_valid()) {
  412. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  413. changing_color = true;
  414. float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
  415. h = y / w_edit->get_size().height;
  416. } else {
  417. changing_color = false;
  418. }
  419. color.set_hsv(h, s, v, color.a);
  420. last_hsv = color;
  421. set_pick_color(color);
  422. _update_color();
  423. if (!deferred_mode_enabled)
  424. emit_signal("color_changed", color);
  425. else if (!bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT)
  426. emit_signal("color_changed", color);
  427. }
  428. Ref<InputEventMouseMotion> mev = p_event;
  429. if (mev.is_valid()) {
  430. if (!changing_color)
  431. return;
  432. float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
  433. h = y / w_edit->get_size().height;
  434. color.set_hsv(h, s, v, color.a);
  435. last_hsv = color;
  436. set_pick_color(color);
  437. _update_color();
  438. if (!deferred_mode_enabled)
  439. emit_signal("color_changed", color);
  440. }
  441. }
  442. void ColorPicker::_preset_input(const Ref<InputEvent> &p_event) {
  443. Ref<InputEventMouseButton> bev = p_event;
  444. if (bev.is_valid()) {
  445. int index = 0;
  446. if (bev->is_pressed() && bev->get_button_index() == BUTTON_LEFT) {
  447. for (int i = 0; i < presets.size(); i++) {
  448. int x = (i % presets_per_row) * bt_add_preset->get_size().x;
  449. int y = (Math::floor((float)i / presets_per_row)) * bt_add_preset->get_size().y;
  450. if (bev->get_position().x > x && bev->get_position().x < x + preset->get_size().x && bev->get_position().y > y && bev->get_position().y < y + preset->get_size().y) {
  451. index = i;
  452. }
  453. }
  454. set_pick_color(presets[index]);
  455. _update_color();
  456. emit_signal("color_changed", color);
  457. } else if (bev->is_pressed() && bev->get_button_index() == BUTTON_RIGHT && presets_enabled) {
  458. index = bev->get_position().x / (preset->get_size().x / presets.size());
  459. Color clicked_preset = presets[index];
  460. erase_preset(clicked_preset);
  461. emit_signal("preset_removed", clicked_preset);
  462. bt_add_preset->show();
  463. }
  464. }
  465. Ref<InputEventMouseMotion> mev = p_event;
  466. if (mev.is_valid()) {
  467. int index = mev->get_position().x * presets.size();
  468. if (preset->get_size().x != 0) {
  469. index /= preset->get_size().x;
  470. }
  471. if (index < 0 || index >= presets.size())
  472. return;
  473. preset->set_tooltip(vformat(RTR("Color: #%s\nLMB: Set color\nRMB: Remove preset"), presets[index].to_html(presets[index].a < 1)));
  474. }
  475. }
  476. void ColorPicker::_screen_input(const Ref<InputEvent> &p_event) {
  477. if (!is_inside_tree())
  478. return;
  479. Ref<InputEventMouseButton> bev = p_event;
  480. if (bev.is_valid() && bev->get_button_index() == BUTTON_LEFT && !bev->is_pressed()) {
  481. emit_signal("color_changed", color);
  482. screen->hide();
  483. }
  484. Ref<InputEventMouseMotion> mev = p_event;
  485. if (mev.is_valid()) {
  486. Viewport *r = get_tree()->get_root();
  487. if (!r->get_visible_rect().has_point(Point2(mev->get_global_position().x, mev->get_global_position().y)))
  488. return;
  489. Ref<Image> img = r->get_texture()->get_data();
  490. if (img.is_valid() && !img->empty()) {
  491. img->lock();
  492. Vector2 ofs = mev->get_global_position() - r->get_visible_rect().get_position();
  493. Color c = img->get_pixel(ofs.x, r->get_visible_rect().size.height - ofs.y);
  494. img->unlock();
  495. set_pick_color(c);
  496. }
  497. }
  498. }
  499. void ColorPicker::_add_preset_pressed() {
  500. add_preset(color);
  501. emit_signal("preset_added", color);
  502. }
  503. void ColorPicker::_screen_pick_pressed() {
  504. if (!is_inside_tree())
  505. return;
  506. Viewport *r = get_tree()->get_root();
  507. if (!screen) {
  508. screen = memnew(Control);
  509. r->add_child(screen);
  510. screen->set_as_toplevel(true);
  511. screen->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  512. screen->set_default_cursor_shape(CURSOR_POINTING_HAND);
  513. screen->connect("gui_input", this, "_screen_input");
  514. // It immediately toggles off in the first press otherwise.
  515. screen->call_deferred("connect", "hide", btn_pick, "set_pressed", varray(false));
  516. }
  517. screen->raise();
  518. screen->show_modal();
  519. }
  520. void ColorPicker::_focus_enter() {
  521. bool has_ctext_focus = c_text->has_focus();
  522. if (has_ctext_focus) {
  523. c_text->select_all();
  524. } else {
  525. c_text->select(0, 0);
  526. }
  527. for (int i = 0; i < 4; i++) {
  528. if (values[i]->get_line_edit()->has_focus() && !has_ctext_focus) {
  529. values[i]->get_line_edit()->select_all();
  530. } else {
  531. values[i]->get_line_edit()->select(0, 0);
  532. }
  533. }
  534. }
  535. void ColorPicker::_focus_exit() {
  536. for (int i = 0; i < 4; i++) {
  537. if (!values[i]->get_line_edit()->get_menu()->is_visible())
  538. values[i]->get_line_edit()->select(0, 0);
  539. }
  540. c_text->select(0, 0);
  541. }
  542. void ColorPicker::_html_focus_exit() {
  543. if (c_text->get_menu()->is_visible())
  544. return;
  545. _html_entered(c_text->get_text());
  546. _focus_exit();
  547. }
  548. void ColorPicker::set_presets_enabled(bool p_enabled) {
  549. presets_enabled = p_enabled;
  550. if (!p_enabled) {
  551. bt_add_preset->set_disabled(true);
  552. bt_add_preset->set_focus_mode(FOCUS_NONE);
  553. } else {
  554. bt_add_preset->set_disabled(false);
  555. bt_add_preset->set_focus_mode(FOCUS_ALL);
  556. }
  557. }
  558. bool ColorPicker::are_presets_enabled() const {
  559. return presets_enabled;
  560. }
  561. void ColorPicker::set_presets_visible(bool p_visible) {
  562. presets_visible = p_visible;
  563. preset_separator->set_visible(p_visible);
  564. preset_container->set_visible(p_visible);
  565. preset_container2->set_visible(p_visible);
  566. }
  567. bool ColorPicker::are_presets_visible() const {
  568. return presets_visible;
  569. }
  570. void ColorPicker::_bind_methods() {
  571. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
  572. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
  573. ClassDB::bind_method(D_METHOD("set_hsv_mode", "mode"), &ColorPicker::set_hsv_mode);
  574. ClassDB::bind_method(D_METHOD("is_hsv_mode"), &ColorPicker::is_hsv_mode);
  575. ClassDB::bind_method(D_METHOD("set_raw_mode", "mode"), &ColorPicker::set_raw_mode);
  576. ClassDB::bind_method(D_METHOD("is_raw_mode"), &ColorPicker::is_raw_mode);
  577. ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
  578. ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
  579. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
  580. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
  581. ClassDB::bind_method(D_METHOD("set_presets_enabled", "enabled"), &ColorPicker::set_presets_enabled);
  582. ClassDB::bind_method(D_METHOD("are_presets_enabled"), &ColorPicker::are_presets_enabled);
  583. ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible);
  584. ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible);
  585. ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
  586. ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
  587. ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
  588. ClassDB::bind_method(D_METHOD("_value_changed"), &ColorPicker::_value_changed);
  589. ClassDB::bind_method(D_METHOD("_html_entered"), &ColorPicker::_html_entered);
  590. ClassDB::bind_method(D_METHOD("_text_type_toggled"), &ColorPicker::_text_type_toggled);
  591. ClassDB::bind_method(D_METHOD("_add_preset_pressed"), &ColorPicker::_add_preset_pressed);
  592. ClassDB::bind_method(D_METHOD("_screen_pick_pressed"), &ColorPicker::_screen_pick_pressed);
  593. ClassDB::bind_method(D_METHOD("_sample_draw"), &ColorPicker::_sample_draw);
  594. ClassDB::bind_method(D_METHOD("_update_presets"), &ColorPicker::_update_presets);
  595. ClassDB::bind_method(D_METHOD("_hsv_draw"), &ColorPicker::_hsv_draw);
  596. ClassDB::bind_method(D_METHOD("_uv_input"), &ColorPicker::_uv_input);
  597. ClassDB::bind_method(D_METHOD("_w_input"), &ColorPicker::_w_input);
  598. ClassDB::bind_method(D_METHOD("_preset_input"), &ColorPicker::_preset_input);
  599. ClassDB::bind_method(D_METHOD("_screen_input"), &ColorPicker::_screen_input);
  600. ClassDB::bind_method(D_METHOD("_focus_enter"), &ColorPicker::_focus_enter);
  601. ClassDB::bind_method(D_METHOD("_focus_exit"), &ColorPicker::_focus_exit);
  602. ClassDB::bind_method(D_METHOD("_html_focus_exit"), &ColorPicker::_html_focus_exit);
  603. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  604. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  605. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hsv_mode"), "set_hsv_mode", "is_hsv_mode");
  606. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "raw_mode"), "set_raw_mode", "is_raw_mode");
  607. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
  608. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_enabled"), "set_presets_enabled", "are_presets_enabled");
  609. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
  610. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  611. ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
  612. ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
  613. }
  614. ColorPicker::ColorPicker() :
  615. BoxContainer(true) {
  616. updating = true;
  617. edit_alpha = true;
  618. text_is_constructor = false;
  619. hsv_mode_enabled = false;
  620. raw_mode_enabled = false;
  621. deferred_mode_enabled = false;
  622. changing_color = false;
  623. presets_enabled = true;
  624. presets_visible = true;
  625. screen = NULL;
  626. HBoxContainer *hb_edit = memnew(HBoxContainer);
  627. add_child(hb_edit);
  628. hb_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  629. uv_edit = memnew(Control);
  630. hb_edit->add_child(uv_edit);
  631. uv_edit->connect("gui_input", this, "_uv_input");
  632. uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  633. uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  634. uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  635. uv_edit->set_custom_minimum_size(Size2(get_constant("sv_width"), get_constant("sv_height")));
  636. uv_edit->connect("draw", this, "_hsv_draw", make_binds(0, uv_edit));
  637. w_edit = memnew(Control);
  638. hb_edit->add_child(w_edit);
  639. w_edit->set_custom_minimum_size(Size2(get_constant("h_width"), 0));
  640. w_edit->set_h_size_flags(SIZE_FILL);
  641. w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  642. w_edit->connect("gui_input", this, "_w_input");
  643. w_edit->connect("draw", this, "_hsv_draw", make_binds(1, w_edit));
  644. HBoxContainer *hb_smpl = memnew(HBoxContainer);
  645. add_child(hb_smpl);
  646. sample = memnew(TextureRect);
  647. hb_smpl->add_child(sample);
  648. sample->set_h_size_flags(SIZE_EXPAND_FILL);
  649. sample->connect("draw", this, "_sample_draw");
  650. btn_pick = memnew(ToolButton);
  651. hb_smpl->add_child(btn_pick);
  652. btn_pick->set_toggle_mode(true);
  653. btn_pick->set_tooltip(RTR("Pick a color from the editor window."));
  654. btn_pick->connect("pressed", this, "_screen_pick_pressed");
  655. VBoxContainer *vbl = memnew(VBoxContainer);
  656. add_child(vbl);
  657. add_child(memnew(HSeparator));
  658. VBoxContainer *vbr = memnew(VBoxContainer);
  659. add_child(vbr);
  660. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  661. for (int i = 0; i < 4; i++) {
  662. HBoxContainer *hbc = memnew(HBoxContainer);
  663. labels[i] = memnew(Label());
  664. labels[i]->set_custom_minimum_size(Size2(get_constant("label_width"), 0));
  665. labels[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  666. hbc->add_child(labels[i]);
  667. scroll[i] = memnew(HSlider);
  668. scroll[i]->set_v_size_flags(SIZE_SHRINK_CENTER);
  669. scroll[i]->set_focus_mode(FOCUS_NONE);
  670. hbc->add_child(scroll[i]);
  671. values[i] = memnew(SpinBox);
  672. scroll[i]->share(values[i]);
  673. hbc->add_child(values[i]);
  674. values[i]->get_line_edit()->connect("focus_entered", this, "_focus_enter");
  675. values[i]->get_line_edit()->connect("focus_exited", this, "_focus_exit");
  676. scroll[i]->set_min(0);
  677. scroll[i]->set_page(0);
  678. scroll[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  679. scroll[i]->connect("value_changed", this, "_value_changed");
  680. vbr->add_child(hbc);
  681. }
  682. labels[3]->set_text("A");
  683. HBoxContainer *hhb = memnew(HBoxContainer);
  684. vbr->add_child(hhb);
  685. btn_hsv = memnew(CheckButton);
  686. hhb->add_child(btn_hsv);
  687. btn_hsv->set_text(RTR("HSV"));
  688. btn_hsv->connect("toggled", this, "set_hsv_mode");
  689. btn_raw = memnew(CheckButton);
  690. hhb->add_child(btn_raw);
  691. btn_raw->set_text(RTR("Raw"));
  692. btn_raw->connect("toggled", this, "set_raw_mode");
  693. text_type = memnew(Button);
  694. hhb->add_child(text_type);
  695. text_type->set_text("#");
  696. text_type->set_tooltip(TTR("Switch between hexadecimal and code values."));
  697. if (Engine::get_singleton()->is_editor_hint()) {
  698. #ifdef TOOLS_ENABLED
  699. text_type->set_custom_minimum_size(Size2(28 * EDSCALE, 0)); // Adjust for the width of the "Script" icon.
  700. #endif
  701. text_type->connect("pressed", this, "_text_type_toggled");
  702. } else {
  703. text_type->set_flat(true);
  704. text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
  705. }
  706. c_text = memnew(LineEdit);
  707. hhb->add_child(c_text);
  708. c_text->set_h_size_flags(SIZE_EXPAND_FILL);
  709. c_text->connect("text_entered", this, "_html_entered");
  710. c_text->connect("focus_entered", this, "_focus_enter");
  711. c_text->connect("focus_exited", this, "_html_focus_exit");
  712. _update_controls();
  713. updating = false;
  714. set_pick_color(Color(1, 1, 1));
  715. preset_separator = memnew(HSeparator);
  716. add_child(preset_separator);
  717. preset_container = memnew(HBoxContainer);
  718. preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
  719. add_child(preset_container);
  720. preset = memnew(TextureRect);
  721. preset_container->add_child(preset);
  722. preset->connect("gui_input", this, "_preset_input");
  723. preset->connect("draw", this, "_update_presets");
  724. preset_container2 = memnew(HBoxContainer);
  725. preset_container2->set_h_size_flags(SIZE_EXPAND_FILL);
  726. add_child(preset_container2);
  727. bt_add_preset = memnew(Button);
  728. preset_container2->add_child(bt_add_preset);
  729. bt_add_preset->set_tooltip(RTR("Add current color as a preset."));
  730. bt_add_preset->connect("pressed", this, "_add_preset_pressed");
  731. }
  732. /////////////////
  733. void ColorPickerButton::_color_changed(const Color &p_color) {
  734. color = p_color;
  735. update();
  736. emit_signal("color_changed", color);
  737. }
  738. void ColorPickerButton::_modal_closed() {
  739. emit_signal("popup_closed");
  740. }
  741. void ColorPickerButton::pressed() {
  742. _update_picker();
  743. popup->set_position(get_global_position() - picker->get_combined_minimum_size() * get_global_transform().get_scale());
  744. popup->set_scale(get_global_transform().get_scale());
  745. popup->popup();
  746. picker->set_focus_on_line_edit();
  747. }
  748. void ColorPickerButton::_notification(int p_what) {
  749. switch (p_what) {
  750. case NOTIFICATION_DRAW: {
  751. const Ref<StyleBox> normal = get_stylebox("normal");
  752. const Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
  753. draw_texture_rect(Control::get_icon("bg", "ColorPickerButton"), r, true);
  754. draw_rect(r, color);
  755. if (color.r > 1 || color.g > 1 || color.b > 1) {
  756. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  757. draw_texture(Control::get_icon("overbright_indicator", "ColorPicker"), normal->get_offset());
  758. }
  759. } break;
  760. case MainLoop::NOTIFICATION_WM_QUIT_REQUEST: {
  761. if (popup)
  762. popup->hide();
  763. } break;
  764. }
  765. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  766. if (popup && !is_visible_in_tree()) {
  767. popup->hide();
  768. }
  769. }
  770. }
  771. void ColorPickerButton::set_pick_color(const Color &p_color) {
  772. color = p_color;
  773. if (picker) {
  774. picker->set_pick_color(p_color);
  775. }
  776. update();
  777. }
  778. Color ColorPickerButton::get_pick_color() const {
  779. return color;
  780. }
  781. void ColorPickerButton::set_edit_alpha(bool p_show) {
  782. edit_alpha = p_show;
  783. if (picker) {
  784. picker->set_edit_alpha(p_show);
  785. }
  786. }
  787. bool ColorPickerButton::is_editing_alpha() const {
  788. return edit_alpha;
  789. }
  790. ColorPicker *ColorPickerButton::get_picker() {
  791. _update_picker();
  792. return picker;
  793. }
  794. PopupPanel *ColorPickerButton::get_popup() {
  795. _update_picker();
  796. return popup;
  797. }
  798. void ColorPickerButton::_update_picker() {
  799. if (!picker) {
  800. popup = memnew(PopupPanel);
  801. picker = memnew(ColorPicker);
  802. popup->add_child(picker);
  803. add_child(popup);
  804. picker->connect("color_changed", this, "_color_changed");
  805. popup->connect("modal_closed", this, "_modal_closed");
  806. popup->connect("about_to_show", this, "set_pressed", varray(true));
  807. popup->connect("popup_hide", this, "set_pressed", varray(false));
  808. picker->set_pick_color(color);
  809. picker->set_edit_alpha(edit_alpha);
  810. emit_signal("picker_created");
  811. }
  812. }
  813. void ColorPickerButton::_bind_methods() {
  814. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
  815. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
  816. ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
  817. ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
  818. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
  819. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
  820. ClassDB::bind_method(D_METHOD("_color_changed"), &ColorPickerButton::_color_changed);
  821. ClassDB::bind_method(D_METHOD("_modal_closed"), &ColorPickerButton::_modal_closed);
  822. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  823. ADD_SIGNAL(MethodInfo("popup_closed"));
  824. ADD_SIGNAL(MethodInfo("picker_created"));
  825. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  826. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  827. }
  828. ColorPickerButton::ColorPickerButton() {
  829. // Initialization is now done deferred,
  830. // this improves performance in the inspector as the color picker
  831. // can be expensive to initialize.
  832. picker = NULL;
  833. popup = NULL;
  834. edit_alpha = true;
  835. set_toggle_mode(true);
  836. }