color_picker.cpp 36 KB

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