color_picker.cpp 30 KB

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