2
0

color_picker.cpp 25 KB

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