color_picker.cpp 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077
  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/input/input.h"
  32. #include "core/io/image.h"
  33. #include "core/math/color.h"
  34. #include "core/os/keyboard.h"
  35. #include "core/os/os.h"
  36. #include "scene/gui/color_mode.h"
  37. #include "servers/display_server.h"
  38. #include "thirdparty/misc/ok_color.h"
  39. #include "thirdparty/misc/ok_color_shader.h"
  40. List<Color> ColorPicker::preset_cache;
  41. List<Color> ColorPicker::recent_preset_cache;
  42. void ColorPicker::_notification(int p_what) {
  43. switch (p_what) {
  44. case NOTIFICATION_ENTER_TREE: {
  45. _update_color();
  46. [[fallthrough]];
  47. }
  48. case NOTIFICATION_THEME_CHANGED: {
  49. btn_pick->set_icon(get_theme_icon(SNAME("screen_picker"), SNAME("ColorPicker")));
  50. _update_drop_down_arrow(btn_preset->is_pressed(), btn_preset);
  51. _update_drop_down_arrow(btn_recent_preset->is_pressed(), btn_recent_preset);
  52. btn_add_preset->set_icon(get_theme_icon(SNAME("add_preset")));
  53. btn_pick->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
  54. btn_shape->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
  55. btn_mode->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
  56. uv_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
  57. w_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("h_width")), 0));
  58. wheel_edit->set_custom_minimum_size(Size2(get_theme_constant(SNAME("sv_width")), get_theme_constant(SNAME("sv_height"))));
  59. wheel_margin->add_theme_constant_override("margin_bottom", 8 * get_theme_default_base_scale());
  60. for (int i = 0; i < SLIDER_COUNT; i++) {
  61. labels[i]->set_custom_minimum_size(Size2(get_theme_constant(SNAME("label_width")), 0));
  62. set_offset((Side)i, get_offset((Side)i) + get_theme_constant(SNAME("margin")));
  63. }
  64. alpha_label->set_custom_minimum_size(Size2(get_theme_constant(SNAME("label_width")), 0));
  65. set_offset((Side)0, get_offset((Side)0) + get_theme_constant(SNAME("margin")));
  66. _reset_theme();
  67. if (Engine::get_singleton()->is_editor_hint()) {
  68. // Adjust for the width of the "Script" icon.
  69. text_type->set_custom_minimum_size(Size2(28 * get_theme_default_base_scale(), 0));
  70. }
  71. _update_presets();
  72. _update_recent_presets();
  73. _update_controls();
  74. } break;
  75. case NOTIFICATION_VISIBILITY_CHANGED: {
  76. Popup *p = Object::cast_to<Popup>(get_parent());
  77. if (p && is_visible_in_tree()) {
  78. p->set_size(Size2(get_combined_minimum_size().width + get_theme_constant(SNAME("margin")) * 2, get_combined_minimum_size().height + get_theme_constant(SNAME("margin")) * 2));
  79. }
  80. } break;
  81. case NOTIFICATION_WM_CLOSE_REQUEST: {
  82. if (picker_window != nullptr && picker_window->is_visible()) {
  83. picker_window->hide();
  84. }
  85. } break;
  86. }
  87. }
  88. Ref<Shader> ColorPicker::wheel_shader;
  89. Ref<Shader> ColorPicker::circle_shader;
  90. Ref<Shader> ColorPicker::circle_ok_color_shader;
  91. void ColorPicker::init_shaders() {
  92. wheel_shader.instantiate();
  93. wheel_shader->set_code(R"(
  94. // ColorPicker wheel shader.
  95. shader_type canvas_item;
  96. void fragment() {
  97. float x = UV.x - 0.5;
  98. float y = UV.y - 0.5;
  99. float a = atan(y, x);
  100. x += 0.001;
  101. y += 0.001;
  102. float b = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  103. x -= 0.002;
  104. float b2 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  105. y -= 0.002;
  106. float b3 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  107. x += 0.002;
  108. float b4 = float(sqrt(x * x + y * y) < 0.5) * float(sqrt(x * x + y * y) > 0.42);
  109. COLOR = vec4(clamp((abs(fract(((a - TAU) / TAU) + vec3(3.0, 2.0, 1.0) / 3.0) * 6.0 - 3.0) - 1.0), 0.0, 1.0), (b + b2 + b3 + b4) / 4.00);
  110. }
  111. )");
  112. circle_shader.instantiate();
  113. circle_shader->set_code(R"(
  114. // ColorPicker circle shader.
  115. shader_type canvas_item;
  116. uniform float v = 1.0;
  117. void fragment() {
  118. float x = UV.x - 0.5;
  119. float y = UV.y - 0.5;
  120. float a = atan(y, x);
  121. x += 0.001;
  122. y += 0.001;
  123. float b = float(sqrt(x * x + y * y) < 0.5);
  124. x -= 0.002;
  125. float b2 = float(sqrt(x * x + y * y) < 0.5);
  126. y -= 0.002;
  127. float b3 = float(sqrt(x * x + y * y) < 0.5);
  128. x += 0.002;
  129. float b4 = float(sqrt(x * x + y * y) < 0.5);
  130. COLOR = vec4(mix(vec3(1.0), clamp(abs(fract(vec3((a - TAU) / TAU) + vec3(1.0, 2.0 / 3.0, 1.0 / 3.0)) * 6.0 - vec3(3.0)) - vec3(1.0), 0.0, 1.0), ((float(sqrt(x * x + y * y)) * 2.0)) / 1.0) * vec3(v), (b + b2 + b3 + b4) / 4.00);
  131. })");
  132. circle_ok_color_shader.instantiate();
  133. circle_ok_color_shader->set_code(OK_COLOR_SHADER + R"(
  134. // ColorPicker ok color hsv circle shader.
  135. uniform float v = 1.0;
  136. void fragment() {
  137. float x = UV.x - 0.5;
  138. float y = UV.y - 0.5;
  139. float h = atan(y, x) / (2.0 * M_PI);
  140. float s = sqrt(x * x + y * y) * 2.0;
  141. vec3 col = okhsl_to_srgb(vec3(h, s, v));
  142. x += 0.001;
  143. y += 0.001;
  144. float b = float(sqrt(x * x + y * y) < 0.5);
  145. x -= 0.002;
  146. float b2 = float(sqrt(x * x + y * y) < 0.5);
  147. y -= 0.002;
  148. float b3 = float(sqrt(x * x + y * y) < 0.5);
  149. x += 0.002;
  150. float b4 = float(sqrt(x * x + y * y) < 0.5);
  151. COLOR = vec4(col, (b + b2 + b3 + b4) / 4.00);
  152. })");
  153. }
  154. void ColorPicker::finish_shaders() {
  155. wheel_shader.unref();
  156. circle_shader.unref();
  157. circle_ok_color_shader.unref();
  158. }
  159. void ColorPicker::set_focus_on_line_edit() {
  160. c_text->call_deferred(SNAME("grab_focus"));
  161. }
  162. void ColorPicker::_update_controls() {
  163. int mode_sliders_count = modes[current_mode]->get_slider_count();
  164. for (int i = current_slider_count; i < mode_sliders_count; i++) {
  165. sliders[i]->show();
  166. labels[i]->show();
  167. values[i]->show();
  168. }
  169. for (int i = mode_sliders_count; i < current_slider_count; i++) {
  170. sliders[i]->hide();
  171. labels[i]->hide();
  172. values[i]->hide();
  173. }
  174. current_slider_count = mode_sliders_count;
  175. for (int i = 0; i < current_slider_count; i++) {
  176. labels[i]->set_text(modes[current_mode]->get_slider_label(i));
  177. }
  178. alpha_label->set_text("A");
  179. slider_theme_modified = modes[current_mode]->apply_theme();
  180. if (edit_alpha) {
  181. alpha_value->show();
  182. alpha_slider->show();
  183. alpha_label->show();
  184. } else {
  185. alpha_value->hide();
  186. alpha_slider->hide();
  187. alpha_label->hide();
  188. }
  189. switch (_get_actual_shape()) {
  190. case SHAPE_HSV_RECTANGLE:
  191. wheel_edit->hide();
  192. w_edit->show();
  193. uv_edit->show();
  194. btn_shape->show();
  195. break;
  196. case SHAPE_HSV_WHEEL:
  197. wheel_edit->show();
  198. w_edit->hide();
  199. uv_edit->hide();
  200. btn_shape->show();
  201. wheel->set_material(wheel_mat);
  202. break;
  203. case SHAPE_VHS_CIRCLE:
  204. wheel_edit->show();
  205. w_edit->show();
  206. uv_edit->hide();
  207. btn_shape->show();
  208. wheel->set_material(circle_mat);
  209. circle_mat->set_shader(circle_shader);
  210. break;
  211. case SHAPE_OKHSL_CIRCLE:
  212. wheel_edit->show();
  213. w_edit->show();
  214. uv_edit->hide();
  215. btn_shape->show();
  216. wheel->set_material(circle_mat);
  217. circle_mat->set_shader(circle_ok_color_shader);
  218. break;
  219. case SHAPE_NONE:
  220. wheel_edit->hide();
  221. w_edit->hide();
  222. uv_edit->hide();
  223. btn_shape->hide();
  224. break;
  225. default: {
  226. }
  227. }
  228. }
  229. void ColorPicker::_set_pick_color(const Color &p_color, bool p_update_sliders) {
  230. if (text_changed) {
  231. add_recent_preset(color);
  232. text_changed = false;
  233. }
  234. color = p_color;
  235. if (color != last_color) {
  236. _copy_color_to_hsv();
  237. last_color = color;
  238. }
  239. if (!is_inside_tree()) {
  240. return;
  241. }
  242. _update_color(p_update_sliders);
  243. }
  244. void ColorPicker::set_pick_color(const Color &p_color) {
  245. _set_pick_color(p_color, true); //because setters can't have more arguments
  246. }
  247. void ColorPicker::set_old_color(const Color &p_color) {
  248. old_color = p_color;
  249. }
  250. void ColorPicker::set_display_old_color(bool p_enabled) {
  251. display_old_color = p_enabled;
  252. }
  253. bool ColorPicker::is_displaying_old_color() const {
  254. return display_old_color;
  255. }
  256. void ColorPicker::set_edit_alpha(bool p_show) {
  257. if (edit_alpha == p_show) {
  258. return;
  259. }
  260. edit_alpha = p_show;
  261. _update_controls();
  262. if (!is_inside_tree()) {
  263. return;
  264. }
  265. _update_color();
  266. sample->queue_redraw();
  267. }
  268. bool ColorPicker::is_editing_alpha() const {
  269. return edit_alpha;
  270. }
  271. void ColorPicker::_value_changed(double) {
  272. if (updating) {
  273. return;
  274. }
  275. color = modes[current_mode]->get_color();
  276. if (current_mode == MODE_HSV || current_mode == MODE_OKHSL) {
  277. h = sliders[0]->get_value() / 360.0;
  278. s = sliders[1]->get_value() / 100.0;
  279. v = sliders[2]->get_value() / 100.0;
  280. last_color = color;
  281. }
  282. _set_pick_color(color, false);
  283. emit_signal(SNAME("color_changed"), color);
  284. }
  285. void ColorPicker::add_mode(ColorMode *p_mode) {
  286. modes.push_back(p_mode);
  287. }
  288. void ColorPicker::create_slider(GridContainer *gc, int idx) {
  289. Label *lbl = memnew(Label());
  290. lbl->set_v_size_flags(SIZE_SHRINK_CENTER);
  291. gc->add_child(lbl);
  292. HSlider *slider = memnew(HSlider);
  293. slider->set_v_size_flags(SIZE_SHRINK_CENTER);
  294. slider->set_focus_mode(FOCUS_NONE);
  295. gc->add_child(slider);
  296. SpinBox *val = memnew(SpinBox);
  297. slider->share(val);
  298. val->set_select_all_on_focus(true);
  299. gc->add_child(val);
  300. LineEdit *vle = val->get_line_edit();
  301. vle->connect("text_changed", callable_mp(this, &ColorPicker::_text_changed));
  302. vle->connect("gui_input", callable_mp(this, &ColorPicker::_line_edit_input));
  303. vle->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
  304. val->connect("gui_input", callable_mp(this, &ColorPicker::_slider_or_spin_input));
  305. slider->set_h_size_flags(SIZE_EXPAND_FILL);
  306. slider->connect("value_changed", callable_mp(this, &ColorPicker::_value_changed));
  307. slider->connect("draw", callable_mp(this, &ColorPicker::_slider_draw).bind(idx));
  308. slider->connect("gui_input", callable_mp(this, &ColorPicker::_slider_or_spin_input));
  309. if (idx < SLIDER_COUNT) {
  310. sliders[idx] = slider;
  311. values[idx] = val;
  312. labels[idx] = lbl;
  313. } else {
  314. alpha_slider = slider;
  315. alpha_value = val;
  316. alpha_label = lbl;
  317. }
  318. }
  319. #ifdef TOOLS_ENABLED
  320. void ColorPicker::set_editor_settings(Object *p_editor_settings) {
  321. if (editor_settings) {
  322. return;
  323. }
  324. editor_settings = p_editor_settings;
  325. if (preset_cache.is_empty()) {
  326. PackedColorArray saved_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "presets", PackedColorArray());
  327. for (int i = 0; i < saved_presets.size(); i++) {
  328. preset_cache.push_back(saved_presets[i]);
  329. }
  330. }
  331. for (int i = 0; i < preset_cache.size(); i++) {
  332. presets.push_back(preset_cache[i]);
  333. }
  334. if (recent_preset_cache.is_empty()) {
  335. PackedColorArray saved_recent_presets = editor_settings->call(SNAME("get_project_metadata"), "color_picker", "recent_presets", PackedColorArray());
  336. for (int i = 0; i < saved_recent_presets.size(); i++) {
  337. recent_preset_cache.push_back(saved_recent_presets[i]);
  338. }
  339. }
  340. for (int i = 0; i < recent_preset_cache.size(); i++) {
  341. recent_presets.push_back(recent_preset_cache[i]);
  342. }
  343. _update_presets();
  344. _update_recent_presets();
  345. }
  346. #endif
  347. HSlider *ColorPicker::get_slider(int p_idx) {
  348. if (p_idx < SLIDER_COUNT) {
  349. return sliders[p_idx];
  350. }
  351. return alpha_slider;
  352. }
  353. Vector<float> ColorPicker::get_active_slider_values() {
  354. Vector<float> cur_values;
  355. for (int i = 0; i < current_slider_count; i++) {
  356. cur_values.push_back(sliders[i]->get_value());
  357. }
  358. cur_values.push_back(alpha_slider->get_value());
  359. return cur_values;
  360. }
  361. void ColorPicker::_copy_color_to_hsv() {
  362. if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) {
  363. h = color.get_ok_hsl_h();
  364. s = color.get_ok_hsl_s();
  365. v = color.get_ok_hsl_l();
  366. } else {
  367. h = color.get_h();
  368. s = color.get_s();
  369. v = color.get_v();
  370. }
  371. }
  372. void ColorPicker::_copy_hsv_to_color() {
  373. if (_get_actual_shape() == SHAPE_OKHSL_CIRCLE) {
  374. color.set_ok_hsl(h, s, v, color.a);
  375. } else {
  376. color.set_hsv(h, s, v, color.a);
  377. }
  378. }
  379. void ColorPicker::_select_from_preset_container(const Color &p_color) {
  380. if (preset_group->get_pressed_button()) {
  381. preset_group->get_pressed_button()->set_pressed(false);
  382. }
  383. for (int i = 1; i < preset_container->get_child_count(); i++) {
  384. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  385. if (current_btn && p_color == current_btn->get_preset_color()) {
  386. current_btn->set_pressed(true);
  387. break;
  388. }
  389. }
  390. }
  391. bool ColorPicker::_select_from_recent_preset_hbc(const Color &p_color) {
  392. for (int i = 0; i < recent_preset_hbc->get_child_count(); i++) {
  393. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
  394. if (current_btn && p_color == current_btn->get_preset_color()) {
  395. current_btn->set_pressed(true);
  396. return true;
  397. }
  398. }
  399. return false;
  400. }
  401. ColorPicker::PickerShapeType ColorPicker::_get_actual_shape() const {
  402. return modes[current_mode]->get_shape_override() != SHAPE_MAX ? modes[current_mode]->get_shape_override() : current_shape;
  403. }
  404. void ColorPicker::_reset_theme() {
  405. Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
  406. style_box_flat->set_content_margin(SIDE_TOP, 16 * get_theme_default_base_scale());
  407. style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
  408. for (int i = 0; i < SLIDER_COUNT; i++) {
  409. sliders[i]->add_theme_icon_override("grabber", get_theme_icon(SNAME("bar_arrow"), SNAME("ColorPicker")));
  410. sliders[i]->add_theme_icon_override("grabber_highlight", get_theme_icon(SNAME("bar_arrow"), SNAME("ColorPicker")));
  411. sliders[i]->add_theme_constant_override("grabber_offset", 8 * get_theme_default_base_scale());
  412. if (!colorize_sliders) {
  413. sliders[i]->add_theme_style_override("slider", style_box_flat);
  414. }
  415. }
  416. alpha_slider->add_theme_icon_override("grabber", get_theme_icon(SNAME("bar_arrow"), SNAME("ColorPicker")));
  417. alpha_slider->add_theme_icon_override("grabber_highlight", get_theme_icon(SNAME("bar_arrow"), SNAME("ColorPicker")));
  418. alpha_slider->add_theme_constant_override("grabber_offset", 8 * get_theme_default_base_scale());
  419. if (!colorize_sliders) {
  420. alpha_slider->add_theme_style_override("slider", style_box_flat);
  421. }
  422. }
  423. void ColorPicker::_html_submitted(const String &p_html) {
  424. if (updating || text_is_constructor || !c_text->is_visible()) {
  425. return;
  426. }
  427. Color previous_color = color;
  428. color = Color::html(p_html);
  429. if (!is_editing_alpha()) {
  430. color.a = previous_color.a;
  431. }
  432. if (color == previous_color) {
  433. return;
  434. }
  435. if (!is_inside_tree()) {
  436. return;
  437. }
  438. set_pick_color(color);
  439. emit_signal(SNAME("color_changed"), color);
  440. }
  441. void ColorPicker::_update_color(bool p_update_sliders) {
  442. updating = true;
  443. if (p_update_sliders) {
  444. float step = modes[current_mode]->get_slider_step();
  445. for (int i = 0; i < current_slider_count; i++) {
  446. sliders[i]->set_max(modes[current_mode]->get_slider_max(i));
  447. sliders[i]->set_step(step);
  448. sliders[i]->set_value(modes[current_mode]->get_slider_value(i));
  449. }
  450. alpha_slider->set_max(modes[current_mode]->get_slider_max(current_slider_count));
  451. alpha_slider->set_step(step);
  452. alpha_slider->set_value(modes[current_mode]->get_slider_value(current_slider_count));
  453. }
  454. _update_text_value();
  455. sample->queue_redraw();
  456. uv_edit->queue_redraw();
  457. w_edit->queue_redraw();
  458. for (int i = 0; i < current_slider_count; i++) {
  459. sliders[i]->queue_redraw();
  460. }
  461. alpha_slider->queue_redraw();
  462. wheel->queue_redraw();
  463. wheel_uv->queue_redraw();
  464. updating = false;
  465. }
  466. void ColorPicker::_update_presets() {
  467. int preset_size = _get_preset_size();
  468. // Only update the preset button size if it has changed.
  469. if (preset_size != prev_preset_size) {
  470. prev_preset_size = preset_size;
  471. btn_add_preset->set_custom_minimum_size(Size2(preset_size, preset_size));
  472. for (int i = 1; i < preset_container->get_child_count(); i++) {
  473. ColorPresetButton *cpb = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  474. cpb->set_custom_minimum_size(Size2(preset_size, preset_size));
  475. }
  476. }
  477. #ifdef TOOLS_ENABLED
  478. if (editor_settings) {
  479. // Only load preset buttons when the only child is the add-preset button.
  480. if (preset_container->get_child_count() == 1) {
  481. for (int i = 0; i < preset_cache.size(); i++) {
  482. _add_preset_button(preset_size, preset_cache[i]);
  483. }
  484. _notification(NOTIFICATION_VISIBILITY_CHANGED);
  485. }
  486. }
  487. #endif
  488. }
  489. void ColorPicker::_update_recent_presets() {
  490. #ifdef TOOLS_ENABLED
  491. if (editor_settings) {
  492. int recent_preset_count = recent_preset_hbc->get_child_count();
  493. for (int i = 0; i < recent_preset_count; i++) {
  494. memdelete(recent_preset_hbc->get_child(0));
  495. }
  496. recent_presets.clear();
  497. for (int i = 0; i < recent_preset_cache.size(); i++) {
  498. recent_presets.push_back(recent_preset_cache[i]);
  499. }
  500. int preset_size = _get_preset_size();
  501. for (int i = 0; i < recent_presets.size(); i++) {
  502. _add_recent_preset_button(preset_size, recent_presets[i]);
  503. }
  504. _notification(NOTIFICATION_VISIBILITY_CHANGED);
  505. }
  506. #endif
  507. }
  508. void ColorPicker::_text_type_toggled() {
  509. text_is_constructor = !text_is_constructor;
  510. if (text_is_constructor) {
  511. text_type->set_text("");
  512. text_type->set_icon(get_theme_icon(SNAME("Script"), SNAME("EditorIcons")));
  513. c_text->set_editable(false);
  514. c_text->set_h_size_flags(SIZE_EXPAND_FILL);
  515. } else {
  516. text_type->set_text("#");
  517. text_type->set_icon(nullptr);
  518. c_text->set_editable(true);
  519. c_text->set_h_size_flags(SIZE_FILL);
  520. }
  521. _update_color();
  522. }
  523. Color ColorPicker::get_pick_color() const {
  524. return color;
  525. }
  526. void ColorPicker::set_picker_shape(PickerShapeType p_shape) {
  527. ERR_FAIL_INDEX(p_shape, SHAPE_MAX);
  528. if (p_shape == current_shape) {
  529. return;
  530. }
  531. if (current_shape != SHAPE_NONE) {
  532. shape_popup->set_item_checked(current_shape, false);
  533. }
  534. if (p_shape != SHAPE_NONE) {
  535. shape_popup->set_item_checked(p_shape, true);
  536. btn_shape->set_icon(shape_popup->get_item_icon(p_shape));
  537. }
  538. current_shape = p_shape;
  539. _copy_color_to_hsv();
  540. _update_controls();
  541. _update_color();
  542. }
  543. ColorPicker::PickerShapeType ColorPicker::get_picker_shape() const {
  544. return current_shape;
  545. }
  546. inline int ColorPicker::_get_preset_size() {
  547. return (int(get_minimum_size().width) - (preset_container->get_theme_constant(SNAME("h_separation")) * (PRESET_COLUMN_COUNT - 1))) / PRESET_COLUMN_COUNT;
  548. }
  549. void ColorPicker::_add_preset_button(int p_size, const Color &p_color) {
  550. ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
  551. btn_preset_new->set_tooltip_text(vformat(RTR("Color: #%s\nLMB: Apply color\nRMB: Remove preset"), p_color.to_html(p_color.a < 1)));
  552. SET_DRAG_FORWARDING_GCDU(btn_preset_new, ColorPicker);
  553. btn_preset_new->set_button_group(preset_group);
  554. preset_container->add_child(btn_preset_new);
  555. btn_preset_new->set_pressed(true);
  556. btn_preset_new->connect("gui_input", callable_mp(this, &ColorPicker::_preset_input).bind(p_color));
  557. }
  558. void ColorPicker::_add_recent_preset_button(int p_size, const Color &p_color) {
  559. ColorPresetButton *btn_preset_new = memnew(ColorPresetButton(p_color, p_size));
  560. btn_preset_new->set_tooltip_text(vformat(RTR("Color: #%s\nLMB: Apply color"), p_color.to_html(p_color.a < 1)));
  561. btn_preset_new->set_button_group(recent_preset_group);
  562. recent_preset_hbc->add_child(btn_preset_new);
  563. recent_preset_hbc->move_child(btn_preset_new, 0);
  564. btn_preset_new->set_pressed(true);
  565. btn_preset_new->connect("toggled", callable_mp(this, &ColorPicker::_recent_preset_pressed).bind(btn_preset_new));
  566. }
  567. void ColorPicker::_show_hide_preset(const bool &p_is_btn_pressed, Button *p_btn_preset, Container *p_preset_container) {
  568. if (p_is_btn_pressed) {
  569. p_preset_container->show();
  570. } else {
  571. p_preset_container->hide();
  572. }
  573. _update_drop_down_arrow(p_is_btn_pressed, p_btn_preset);
  574. }
  575. void ColorPicker::_update_drop_down_arrow(const bool &p_is_btn_pressed, Button *p_btn_preset) {
  576. if (p_is_btn_pressed) {
  577. p_btn_preset->set_icon(get_theme_icon(SNAME("expanded_arrow"), SNAME("ColorPicker")));
  578. } else {
  579. p_btn_preset->set_icon(get_theme_icon(SNAME("folded_arrow"), SNAME("ColorPicker")));
  580. }
  581. }
  582. void ColorPicker::_set_mode_popup_value(ColorModeType p_mode) {
  583. ERR_FAIL_INDEX(p_mode, MODE_MAX + 1);
  584. if (p_mode == MODE_MAX) {
  585. set_colorize_sliders(!colorize_sliders);
  586. } else {
  587. set_color_mode(p_mode);
  588. }
  589. }
  590. Variant ColorPicker::_get_drag_data_fw(const Point2 &p_point, Control *p_from_control) {
  591. ColorPresetButton *dragged_preset_button = Object::cast_to<ColorPresetButton>(p_from_control);
  592. if (!dragged_preset_button) {
  593. return Variant();
  594. }
  595. ColorPresetButton *drag_preview = memnew(ColorPresetButton(dragged_preset_button->get_preset_color(), _get_preset_size()));
  596. set_drag_preview(drag_preview);
  597. Dictionary drag_data;
  598. drag_data["type"] = "color_preset";
  599. drag_data["color_preset"] = dragged_preset_button->get_index();
  600. return drag_data;
  601. }
  602. bool ColorPicker::_can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) const {
  603. Dictionary d = p_data;
  604. if (!d.has("type") || String(d["type"]) != "color_preset") {
  605. return false;
  606. }
  607. return true;
  608. }
  609. void ColorPicker::_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from_control) {
  610. Dictionary d = p_data;
  611. if (!d.has("type")) {
  612. return;
  613. }
  614. if (String(d["type"]) == "color_preset") {
  615. int preset_from_id = d["color_preset"];
  616. int hover_now = p_from_control->get_index();
  617. if (preset_from_id == hover_now || hover_now == -1) {
  618. return;
  619. }
  620. preset_container->move_child(preset_container->get_child(preset_from_id), hover_now);
  621. }
  622. }
  623. void ColorPicker::add_preset(const Color &p_color) {
  624. List<Color>::Element *e = presets.find(p_color);
  625. if (e) {
  626. presets.move_to_back(e);
  627. preset_cache.move_to_back(preset_cache.find(p_color));
  628. preset_container->move_child(preset_group->get_pressed_button(), preset_container->get_child_count() - 1);
  629. } else {
  630. presets.push_back(p_color);
  631. preset_cache.push_back(p_color);
  632. _add_preset_button(_get_preset_size(), p_color);
  633. }
  634. #ifdef TOOLS_ENABLED
  635. if (editor_settings) {
  636. PackedColorArray arr_to_save = get_presets();
  637. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "presets", arr_to_save);
  638. }
  639. #endif
  640. }
  641. void ColorPicker::add_recent_preset(const Color &p_color) {
  642. if (!_select_from_recent_preset_hbc(p_color)) {
  643. if (recent_preset_hbc->get_child_count() >= PRESET_COLUMN_COUNT) {
  644. recent_preset_cache.pop_front();
  645. recent_presets.pop_front();
  646. recent_preset_hbc->get_child(PRESET_COLUMN_COUNT - 1)->queue_free();
  647. }
  648. recent_presets.push_back(p_color);
  649. recent_preset_cache.push_back(p_color);
  650. _add_recent_preset_button(_get_preset_size(), p_color);
  651. }
  652. _select_from_preset_container(p_color);
  653. #ifdef TOOLS_ENABLED
  654. if (editor_settings) {
  655. PackedColorArray arr_to_save = get_recent_presets();
  656. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
  657. }
  658. #endif
  659. }
  660. void ColorPicker::erase_preset(const Color &p_color) {
  661. List<Color>::Element *e = presets.find(p_color);
  662. if (e) {
  663. presets.erase(e);
  664. preset_cache.erase(preset_cache.find(p_color));
  665. // Find preset button to remove.
  666. for (int i = 1; i < preset_container->get_child_count(); i++) {
  667. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(preset_container->get_child(i));
  668. if (current_btn && p_color == current_btn->get_preset_color()) {
  669. current_btn->queue_free();
  670. break;
  671. }
  672. }
  673. #ifdef TOOLS_ENABLED
  674. if (editor_settings) {
  675. PackedColorArray arr_to_save = get_presets();
  676. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "presets", arr_to_save);
  677. }
  678. #endif
  679. }
  680. }
  681. void ColorPicker::erase_recent_preset(const Color &p_color) {
  682. List<Color>::Element *e = recent_presets.find(p_color);
  683. if (e) {
  684. recent_presets.erase(e);
  685. recent_preset_cache.erase(recent_preset_cache.find(p_color));
  686. // Find recent preset button to remove.
  687. for (int i = 1; i < recent_preset_hbc->get_child_count(); i++) {
  688. ColorPresetButton *current_btn = Object::cast_to<ColorPresetButton>(recent_preset_hbc->get_child(i));
  689. if (current_btn && p_color == current_btn->get_preset_color()) {
  690. current_btn->queue_free();
  691. break;
  692. }
  693. }
  694. #ifdef TOOLS_ENABLED
  695. if (editor_settings) {
  696. PackedColorArray arr_to_save = get_recent_presets();
  697. editor_settings->call(SNAME("set_project_metadata"), "color_picker", "recent_presets", arr_to_save);
  698. }
  699. #endif
  700. }
  701. }
  702. PackedColorArray ColorPicker::get_presets() const {
  703. PackedColorArray arr;
  704. arr.resize(presets.size());
  705. for (int i = 0; i < presets.size(); i++) {
  706. arr.set(i, presets[i]);
  707. }
  708. return arr;
  709. }
  710. PackedColorArray ColorPicker::get_recent_presets() const {
  711. PackedColorArray arr;
  712. arr.resize(recent_presets.size());
  713. for (int i = 0; i < recent_presets.size(); i++) {
  714. arr.set(i, recent_presets[i]);
  715. }
  716. return arr;
  717. }
  718. void ColorPicker::set_color_mode(ColorModeType p_mode) {
  719. ERR_FAIL_INDEX(p_mode, MODE_MAX);
  720. if (current_mode == p_mode) {
  721. return;
  722. }
  723. if (slider_theme_modified) {
  724. _reset_theme();
  725. }
  726. mode_popup->set_item_checked(current_mode, false);
  727. mode_popup->set_item_checked(p_mode, true);
  728. if (p_mode < MODE_BUTTON_COUNT) {
  729. mode_btns[p_mode]->set_pressed(true);
  730. } else if (current_mode < MODE_BUTTON_COUNT) {
  731. mode_btns[current_mode]->set_pressed(false);
  732. }
  733. current_mode = p_mode;
  734. if (!is_inside_tree()) {
  735. return;
  736. }
  737. _update_controls();
  738. _update_color();
  739. }
  740. ColorPicker::ColorModeType ColorPicker::get_color_mode() const {
  741. return current_mode;
  742. }
  743. void ColorPicker::set_colorize_sliders(bool p_colorize_sliders) {
  744. if (colorize_sliders == p_colorize_sliders) {
  745. return;
  746. }
  747. colorize_sliders = p_colorize_sliders;
  748. mode_popup->set_item_checked(MODE_MAX + 1, colorize_sliders);
  749. if (colorize_sliders) {
  750. Ref<StyleBoxEmpty> style_box_empty(memnew(StyleBoxEmpty));
  751. if (!slider_theme_modified) {
  752. for (int i = 0; i < SLIDER_COUNT; i++) {
  753. sliders[i]->add_theme_style_override("slider", style_box_empty);
  754. }
  755. }
  756. alpha_slider->add_theme_style_override("slider", style_box_empty);
  757. } else {
  758. Ref<StyleBoxFlat> style_box_flat(memnew(StyleBoxFlat));
  759. style_box_flat->set_content_margin(SIDE_TOP, 16 * get_theme_default_base_scale());
  760. style_box_flat->set_bg_color(Color(0.2, 0.23, 0.31).lerp(Color(0, 0, 0, 1), 0.3).clamp());
  761. if (!slider_theme_modified) {
  762. for (int i = 0; i < SLIDER_COUNT; i++) {
  763. sliders[i]->add_theme_style_override("slider", style_box_flat);
  764. }
  765. }
  766. alpha_slider->add_theme_style_override("slider", style_box_flat);
  767. }
  768. }
  769. bool ColorPicker::is_colorizing_sliders() const {
  770. return colorize_sliders;
  771. }
  772. void ColorPicker::set_deferred_mode(bool p_enabled) {
  773. deferred_mode_enabled = p_enabled;
  774. }
  775. bool ColorPicker::is_deferred_mode() const {
  776. return deferred_mode_enabled;
  777. }
  778. void ColorPicker::_update_text_value() {
  779. bool text_visible = true;
  780. if (text_is_constructor) {
  781. String t = "Color(" + String::num(color.r) + ", " + String::num(color.g) + ", " + String::num(color.b);
  782. if (edit_alpha && color.a < 1) {
  783. t += ", " + String::num(color.a) + ")";
  784. } else {
  785. t += ")";
  786. }
  787. c_text->set_text(t);
  788. }
  789. if (color.r > 1 || color.g > 1 || color.b > 1 || color.r < 0 || color.g < 0 || color.b < 0) {
  790. text_visible = false;
  791. } else if (!text_is_constructor) {
  792. c_text->set_text(color.to_html(edit_alpha && color.a < 1));
  793. }
  794. text_type->set_visible(text_visible);
  795. c_text->set_visible(text_visible);
  796. }
  797. void ColorPicker::_sample_input(const Ref<InputEvent> &p_event) {
  798. const Ref<InputEventMouseButton> mb = p_event;
  799. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
  800. const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  801. if (rect_old.has_point(mb->get_position())) {
  802. // Revert to the old color when left-clicking the old color sample.
  803. set_pick_color(old_color);
  804. emit_signal(SNAME("color_changed"), color);
  805. }
  806. }
  807. }
  808. void ColorPicker::_sample_draw() {
  809. // Covers the right half of the sample if the old color is being displayed,
  810. // or the whole sample if it's not being displayed.
  811. Rect2 rect_new;
  812. if (display_old_color) {
  813. rect_new = Rect2(Point2(sample->get_size().width * 0.5, 0), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  814. // Draw both old and new colors for easier comparison (only if spawned from a ColorPickerButton).
  815. const Rect2 rect_old = Rect2(Point2(), Size2(sample->get_size().width * 0.5, sample->get_size().height * 0.95));
  816. if (old_color.a < 1.0) {
  817. sample->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), rect_old, true);
  818. }
  819. sample->draw_rect(rect_old, old_color);
  820. if (old_color.r > 1 || old_color.g > 1 || old_color.b > 1) {
  821. // Draw an indicator to denote that the old color is "overbright" and can't be displayed accurately in the preview.
  822. sample->draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2());
  823. }
  824. } else {
  825. rect_new = Rect2(Point2(), Size2(sample->get_size().width, sample->get_size().height * 0.95));
  826. }
  827. if (color.a < 1.0) {
  828. sample->draw_texture_rect(get_theme_icon(SNAME("sample_bg"), SNAME("ColorPicker")), rect_new, true);
  829. }
  830. sample->draw_rect(rect_new, color);
  831. if (color.r > 1 || color.g > 1 || color.b > 1) {
  832. // Draw an indicator to denote that the new color is "overbright" and can't be displayed accurately in the preview.
  833. sample->draw_texture(get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), Point2(uv_edit->get_size().width * 0.5, 0));
  834. }
  835. }
  836. void ColorPicker::_hsv_draw(int p_which, Control *c) {
  837. if (!c) {
  838. return;
  839. }
  840. PickerShapeType actual_shape = _get_actual_shape();
  841. if (p_which == 0) {
  842. Vector<Point2> points;
  843. Vector<Color> colors;
  844. Vector<Color> colors2;
  845. Color col = color;
  846. Vector2 center = c->get_size() / 2.0;
  847. switch (actual_shape) {
  848. case SHAPE_HSV_WHEEL: {
  849. points.resize(4);
  850. colors.resize(4);
  851. colors2.resize(4);
  852. real_t ring_radius_x = Math_SQRT12 * c->get_size().width * 0.42;
  853. real_t ring_radius_y = Math_SQRT12 * c->get_size().height * 0.42;
  854. points.set(0, center - Vector2(ring_radius_x, ring_radius_y));
  855. points.set(1, center + Vector2(ring_radius_x, -ring_radius_y));
  856. points.set(2, center + Vector2(ring_radius_x, ring_radius_y));
  857. points.set(3, center + Vector2(-ring_radius_x, ring_radius_y));
  858. colors.set(0, Color(1, 1, 1, 1));
  859. colors.set(1, Color(1, 1, 1, 1));
  860. colors.set(2, Color(0, 0, 0, 1));
  861. colors.set(3, Color(0, 0, 0, 1));
  862. c->draw_polygon(points, colors);
  863. col.set_hsv(h, 1, 1);
  864. col.a = 0;
  865. colors2.set(0, col);
  866. col.a = 1;
  867. colors2.set(1, col);
  868. col.set_hsv(h, 1, 0);
  869. colors2.set(2, col);
  870. col.a = 0;
  871. colors2.set(3, col);
  872. c->draw_polygon(points, colors2);
  873. break;
  874. }
  875. case SHAPE_HSV_RECTANGLE: {
  876. points.resize(4);
  877. colors.resize(4);
  878. colors2.resize(4);
  879. points.set(0, Vector2());
  880. points.set(1, Vector2(c->get_size().x, 0));
  881. points.set(2, c->get_size());
  882. points.set(3, Vector2(0, c->get_size().y));
  883. colors.set(0, Color(1, 1, 1, 1));
  884. colors.set(1, Color(1, 1, 1, 1));
  885. colors.set(2, Color(0, 0, 0, 1));
  886. colors.set(3, Color(0, 0, 0, 1));
  887. c->draw_polygon(points, colors);
  888. col = color;
  889. col.set_hsv(h, 1, 1);
  890. col.a = 0;
  891. colors2.set(0, col);
  892. col.a = 1;
  893. colors2.set(1, col);
  894. col.set_hsv(h, 1, 0);
  895. colors2.set(2, col);
  896. col.a = 0;
  897. colors2.set(3, col);
  898. c->draw_polygon(points, colors2);
  899. break;
  900. }
  901. default: {
  902. }
  903. }
  904. Ref<Texture2D> cursor = get_theme_icon(SNAME("picker_cursor"), SNAME("ColorPicker"));
  905. int x;
  906. int y;
  907. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  908. x = center.x + (center.x * Math::cos(h * Math_TAU) * s) - (cursor->get_width() / 2);
  909. y = center.y + (center.y * Math::sin(h * Math_TAU) * s) - (cursor->get_height() / 2);
  910. } else {
  911. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  912. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  913. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  914. x = CLAMP(real_size.x * s, 0, real_size.x) + corner_x - (cursor->get_width() / 2);
  915. y = CLAMP(real_size.y - real_size.y * v, 0, real_size.y) + corner_y - (cursor->get_height() / 2);
  916. }
  917. c->draw_texture(cursor, Point2(x, y));
  918. col.set_hsv(h, 1, 1);
  919. if (actual_shape == SHAPE_HSV_WHEEL) {
  920. points.resize(4);
  921. double h1 = h - (0.5 / 360);
  922. double h2 = h + (0.5 / 360);
  923. points.set(0, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU)), center.y + (center.y * Math::sin(h1 * Math_TAU))));
  924. points.set(1, Point2(center.x + (center.x * Math::cos(h1 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h1 * Math_TAU) * 0.84)));
  925. points.set(2, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU)), center.y + (center.y * Math::sin(h2 * Math_TAU))));
  926. points.set(3, Point2(center.x + (center.x * Math::cos(h2 * Math_TAU) * 0.84), center.y + (center.y * Math::sin(h2 * Math_TAU) * 0.84)));
  927. c->draw_multiline(points, col.inverted());
  928. }
  929. } else if (p_which == 1) {
  930. if (actual_shape == SHAPE_HSV_RECTANGLE) {
  931. Ref<Texture2D> hue = get_theme_icon(SNAME("color_hue"), SNAME("ColorPicker"));
  932. c->draw_set_transform(Point2(), -Math_PI / 2, Size2(c->get_size().x, -c->get_size().y));
  933. c->draw_texture_rect(hue, Rect2(Point2(), Size2(1, 1)));
  934. c->draw_set_transform(Point2(), 0, Size2(1, 1));
  935. int y = c->get_size().y - c->get_size().y * (1.0 - h);
  936. Color col;
  937. col.set_hsv(h, 1, 1);
  938. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  939. } else if (actual_shape == SHAPE_OKHSL_CIRCLE) {
  940. Vector<Point2> points;
  941. Vector<Color> colors;
  942. Color col;
  943. col.set_ok_hsl(h, s, 1);
  944. Color col2;
  945. col2.set_ok_hsl(h, s, 0.5);
  946. Color col3;
  947. col3.set_ok_hsl(h, s, 0);
  948. points.resize(6);
  949. colors.resize(6);
  950. points.set(0, Vector2(c->get_size().x, 0));
  951. points.set(1, Vector2(c->get_size().x, c->get_size().y * 0.5));
  952. points.set(2, c->get_size());
  953. points.set(3, Vector2(0, c->get_size().y));
  954. points.set(4, Vector2(0, c->get_size().y * 0.5));
  955. points.set(5, Vector2());
  956. colors.set(0, col);
  957. colors.set(1, col2);
  958. colors.set(2, col3);
  959. colors.set(3, col3);
  960. colors.set(4, col2);
  961. colors.set(5, col);
  962. c->draw_polygon(points, colors);
  963. int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1);
  964. col.set_ok_hsl(h, 1, v);
  965. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  966. } else if (actual_shape == SHAPE_VHS_CIRCLE) {
  967. Vector<Point2> points;
  968. Vector<Color> colors;
  969. Color col;
  970. col.set_hsv(h, s, 1);
  971. points.resize(4);
  972. colors.resize(4);
  973. points.set(0, Vector2());
  974. points.set(1, Vector2(c->get_size().x, 0));
  975. points.set(2, c->get_size());
  976. points.set(3, Vector2(0, c->get_size().y));
  977. colors.set(0, col);
  978. colors.set(1, col);
  979. colors.set(2, Color(0, 0, 0));
  980. colors.set(3, Color(0, 0, 0));
  981. c->draw_polygon(points, colors);
  982. int y = c->get_size().y - c->get_size().y * CLAMP(v, 0, 1);
  983. col.set_hsv(h, 1, v);
  984. c->draw_line(Point2(0, y), Point2(c->get_size().x, y), col.inverted());
  985. }
  986. } else if (p_which == 2) {
  987. c->draw_rect(Rect2(Point2(), c->get_size()), Color(1, 1, 1));
  988. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  989. circle_mat->set_shader_parameter("v", v);
  990. }
  991. }
  992. }
  993. void ColorPicker::_slider_draw(int p_which) {
  994. if (colorize_sliders) {
  995. modes[current_mode]->slider_draw(p_which);
  996. }
  997. }
  998. void ColorPicker::_uv_input(const Ref<InputEvent> &p_event, Control *c) {
  999. Ref<InputEventMouseButton> bev = p_event;
  1000. PickerShapeType actual_shape = _get_actual_shape();
  1001. if (bev.is_valid()) {
  1002. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1003. Vector2 center = c->get_size() / 2.0;
  1004. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1005. real_t dist = center.distance_to(bev->get_position());
  1006. if (dist <= center.x) {
  1007. real_t rad = center.angle_to_point(bev->get_position());
  1008. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1009. s = CLAMP(dist / center.x, 0, 1);
  1010. } else {
  1011. return;
  1012. }
  1013. } else {
  1014. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  1015. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  1016. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  1017. if (bev->get_position().x < corner_x || bev->get_position().x > c->get_size().x - corner_x ||
  1018. bev->get_position().y < corner_y || bev->get_position().y > c->get_size().y - corner_y) {
  1019. {
  1020. real_t dist = center.distance_to(bev->get_position());
  1021. if (dist >= center.x * 0.84 && dist <= center.x) {
  1022. real_t rad = center.angle_to_point(bev->get_position());
  1023. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1024. spinning = true;
  1025. } else {
  1026. return;
  1027. }
  1028. }
  1029. }
  1030. if (!spinning) {
  1031. real_t x = CLAMP(bev->get_position().x - corner_x, 0, real_size.x);
  1032. real_t y = CLAMP(bev->get_position().y - corner_y, 0, real_size.y);
  1033. s = x / real_size.x;
  1034. v = 1.0 - y / real_size.y;
  1035. }
  1036. }
  1037. changing_color = true;
  1038. _copy_hsv_to_color();
  1039. last_color = color;
  1040. set_pick_color(color);
  1041. _update_color();
  1042. if (!deferred_mode_enabled) {
  1043. emit_signal(SNAME("color_changed"), color);
  1044. }
  1045. } else if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1046. if (deferred_mode_enabled) {
  1047. emit_signal(SNAME("color_changed"), color);
  1048. }
  1049. add_recent_preset(color);
  1050. changing_color = false;
  1051. spinning = false;
  1052. } else {
  1053. changing_color = false;
  1054. spinning = false;
  1055. }
  1056. }
  1057. Ref<InputEventMouseMotion> mev = p_event;
  1058. if (mev.is_valid()) {
  1059. if (!changing_color) {
  1060. return;
  1061. }
  1062. Vector2 center = c->get_size() / 2.0;
  1063. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1064. real_t dist = center.distance_to(mev->get_position());
  1065. real_t rad = center.angle_to_point(mev->get_position());
  1066. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1067. s = CLAMP(dist / center.x, 0, 1);
  1068. } else {
  1069. if (spinning) {
  1070. real_t rad = center.angle_to_point(mev->get_position());
  1071. h = ((rad >= 0) ? rad : (Math_TAU + rad)) / Math_TAU;
  1072. } else {
  1073. real_t corner_x = (c == wheel_uv) ? center.x - Math_SQRT12 * c->get_size().width * 0.42 : 0;
  1074. real_t corner_y = (c == wheel_uv) ? center.y - Math_SQRT12 * c->get_size().height * 0.42 : 0;
  1075. Size2 real_size(c->get_size().x - corner_x * 2, c->get_size().y - corner_y * 2);
  1076. real_t x = CLAMP(mev->get_position().x - corner_x, 0, real_size.x);
  1077. real_t y = CLAMP(mev->get_position().y - corner_y, 0, real_size.y);
  1078. s = x / real_size.x;
  1079. v = 1.0 - y / real_size.y;
  1080. }
  1081. }
  1082. _copy_hsv_to_color();
  1083. last_color = color;
  1084. set_pick_color(color);
  1085. _update_color();
  1086. if (!deferred_mode_enabled) {
  1087. emit_signal(SNAME("color_changed"), color);
  1088. }
  1089. }
  1090. }
  1091. void ColorPicker::_w_input(const Ref<InputEvent> &p_event) {
  1092. Ref<InputEventMouseButton> bev = p_event;
  1093. PickerShapeType actual_shape = _get_actual_shape();
  1094. if (bev.is_valid()) {
  1095. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1096. changing_color = true;
  1097. float y = CLAMP((float)bev->get_position().y, 0, w_edit->get_size().height);
  1098. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1099. v = 1.0 - (y / w_edit->get_size().height);
  1100. } else {
  1101. h = y / w_edit->get_size().height;
  1102. }
  1103. } else {
  1104. changing_color = false;
  1105. }
  1106. _copy_hsv_to_color();
  1107. last_color = color;
  1108. set_pick_color(color);
  1109. _update_color();
  1110. if (!bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1111. add_recent_preset(color);
  1112. emit_signal(SNAME("color_changed"), color);
  1113. } else if (!deferred_mode_enabled) {
  1114. emit_signal(SNAME("color_changed"), color);
  1115. }
  1116. }
  1117. Ref<InputEventMouseMotion> mev = p_event;
  1118. if (mev.is_valid()) {
  1119. if (!changing_color) {
  1120. return;
  1121. }
  1122. float y = CLAMP((float)mev->get_position().y, 0, w_edit->get_size().height);
  1123. if (actual_shape == SHAPE_VHS_CIRCLE || actual_shape == SHAPE_OKHSL_CIRCLE) {
  1124. v = 1.0 - (y / w_edit->get_size().height);
  1125. } else {
  1126. h = y / w_edit->get_size().height;
  1127. }
  1128. _copy_hsv_to_color();
  1129. last_color = color;
  1130. set_pick_color(color);
  1131. _update_color();
  1132. if (!deferred_mode_enabled) {
  1133. emit_signal(SNAME("color_changed"), color);
  1134. }
  1135. }
  1136. }
  1137. void ColorPicker::_slider_or_spin_input(const Ref<InputEvent> &p_event) {
  1138. if (line_edit_mouse_release) {
  1139. line_edit_mouse_release = false;
  1140. return;
  1141. }
  1142. Ref<InputEventMouseButton> bev = p_event;
  1143. if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1144. add_recent_preset(color);
  1145. }
  1146. }
  1147. void ColorPicker::_line_edit_input(const Ref<InputEvent> &p_event) {
  1148. Ref<InputEventMouseButton> bev = p_event;
  1149. if (bev.is_valid() && !bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1150. line_edit_mouse_release = true;
  1151. }
  1152. }
  1153. void ColorPicker::_preset_input(const Ref<InputEvent> &p_event, const Color &p_color) {
  1154. Ref<InputEventMouseButton> bev = p_event;
  1155. if (bev.is_valid()) {
  1156. if (bev->is_pressed() && bev->get_button_index() == MouseButton::LEFT) {
  1157. set_pick_color(p_color);
  1158. add_recent_preset(color);
  1159. emit_signal(SNAME("color_changed"), p_color);
  1160. } else if (bev->is_pressed() && bev->get_button_index() == MouseButton::RIGHT && can_add_swatches) {
  1161. erase_preset(p_color);
  1162. emit_signal(SNAME("preset_removed"), p_color);
  1163. }
  1164. }
  1165. }
  1166. void ColorPicker::_recent_preset_pressed(const bool p_pressed, ColorPresetButton *p_preset) {
  1167. if (!p_pressed) {
  1168. return;
  1169. }
  1170. set_pick_color(p_preset->get_preset_color());
  1171. recent_presets.move_to_back(recent_presets.find(p_preset->get_preset_color()));
  1172. List<Color>::Element *e = recent_preset_cache.find(p_preset->get_preset_color());
  1173. if (e) {
  1174. recent_preset_cache.move_to_back(e);
  1175. }
  1176. recent_preset_hbc->move_child(p_preset, 0);
  1177. emit_signal(SNAME("color_changed"), p_preset->get_preset_color());
  1178. }
  1179. void ColorPicker::_picker_texture_input(const Ref<InputEvent> &p_event) {
  1180. if (!is_inside_tree()) {
  1181. return;
  1182. }
  1183. Ref<InputEventMouseButton> bev = p_event;
  1184. if (bev.is_valid() && bev->get_button_index() == MouseButton::LEFT && !bev->is_pressed()) {
  1185. set_pick_color(picker_color);
  1186. emit_signal(SNAME("color_changed"), color);
  1187. picker_window->hide();
  1188. }
  1189. Ref<InputEventMouseMotion> mev = p_event;
  1190. if (mev.is_valid()) {
  1191. Ref<Image> img = picker_texture_rect->get_texture()->get_image();
  1192. if (img.is_valid() && !img->is_empty()) {
  1193. Vector2 ofs = mev->get_position();
  1194. picker_color = img->get_pixel(ofs.x, ofs.y);
  1195. picker_preview_style_box->set_bg_color(picker_color);
  1196. picker_preview_label->set_self_modulate(picker_color.get_luminance() < 0.5 ? Color(1.0f, 1.0f, 1.0f) : Color(0.0f, 0.0f, 0.0f));
  1197. }
  1198. }
  1199. }
  1200. void ColorPicker::_text_changed(const String &) {
  1201. text_changed = true;
  1202. }
  1203. void ColorPicker::_add_preset_pressed() {
  1204. add_preset(color);
  1205. emit_signal(SNAME("preset_added"), color);
  1206. }
  1207. void ColorPicker::_pick_button_pressed() {
  1208. if (!is_inside_tree()) {
  1209. return;
  1210. }
  1211. if (!picker_window) {
  1212. picker_window = memnew(Popup);
  1213. picker_window->hide();
  1214. picker_window->set_transient(true);
  1215. add_child(picker_window);
  1216. picker_texture_rect = memnew(TextureRect);
  1217. picker_texture_rect->set_anchors_preset(Control::PRESET_FULL_RECT);
  1218. picker_window->add_child(picker_texture_rect);
  1219. picker_texture_rect->set_default_cursor_shape(CURSOR_POINTING_HAND);
  1220. picker_texture_rect->connect(SNAME("gui_input"), callable_mp(this, &ColorPicker::_picker_texture_input));
  1221. picker_preview = memnew(Panel);
  1222. picker_preview->set_anchors_preset(Control::PRESET_CENTER_TOP);
  1223. picker_preview->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1224. picker_window->add_child(picker_preview);
  1225. picker_preview_label = memnew(Label);
  1226. picker_preview->set_anchors_preset(Control::PRESET_CENTER_TOP);
  1227. picker_preview_label->set_text("Color Picking active");
  1228. picker_preview->add_child(picker_preview_label);
  1229. picker_preview_style_box = (Ref<StyleBoxFlat>)memnew(StyleBoxFlat);
  1230. picker_preview_style_box->set_bg_color(Color(1.0, 1.0, 1.0));
  1231. picker_preview->add_theme_style_override("panel", picker_preview_style_box);
  1232. }
  1233. Rect2i screen_rect;
  1234. if (picker_window->is_embedded()) {
  1235. screen_rect = picker_window->get_embedder()->get_visible_rect();
  1236. picker_window->set_position(Point2i());
  1237. picker_texture_rect->set_texture(ImageTexture::create_from_image(picker_window->get_embedder()->get_texture()->get_image()));
  1238. } else {
  1239. screen_rect = picker_window->get_parent_rect();
  1240. picker_window->set_position(screen_rect.position);
  1241. Ref<Image> target_image = Image::create_empty(screen_rect.size.x, screen_rect.size.y, false, Image::FORMAT_RGB8);
  1242. DisplayServer *ds = DisplayServer::get_singleton();
  1243. // Add the Texture of each Window to the Image.
  1244. Vector<DisplayServer::WindowID> wl = ds->get_window_list();
  1245. // FIXME: sort windows by visibility.
  1246. for (int index = 0; index < wl.size(); index++) {
  1247. DisplayServer::WindowID wid = wl[index];
  1248. if (wid == DisplayServer::INVALID_WINDOW_ID) {
  1249. continue;
  1250. }
  1251. ObjectID woid = DisplayServer::get_singleton()->window_get_attached_instance_id(wid);
  1252. if (woid == ObjectID()) {
  1253. continue;
  1254. }
  1255. Window *w = Object::cast_to<Window>(ObjectDB::get_instance(woid));
  1256. Ref<Image> img = w->get_texture()->get_image();
  1257. if (!img.is_valid() || img->is_empty()) {
  1258. continue;
  1259. }
  1260. img->convert(Image::FORMAT_RGB8);
  1261. target_image->blit_rect(img, Rect2i(Point2i(0, 0), img->get_size()), w->get_position());
  1262. }
  1263. picker_texture_rect->set_texture(ImageTexture::create_from_image(target_image));
  1264. }
  1265. picker_window->set_size(screen_rect.size);
  1266. picker_preview->set_size(screen_rect.size / 10.0); // 10% of size in each axis.
  1267. picker_window->popup();
  1268. }
  1269. void ColorPicker::_html_focus_exit() {
  1270. if (c_text->is_menu_visible()) {
  1271. return;
  1272. }
  1273. _html_submitted(c_text->get_text());
  1274. }
  1275. void ColorPicker::set_can_add_swatches(bool p_enabled) {
  1276. if (can_add_swatches == p_enabled) {
  1277. return;
  1278. }
  1279. can_add_swatches = p_enabled;
  1280. if (!p_enabled) {
  1281. btn_add_preset->set_disabled(true);
  1282. btn_add_preset->set_focus_mode(FOCUS_NONE);
  1283. } else {
  1284. btn_add_preset->set_disabled(false);
  1285. btn_add_preset->set_focus_mode(FOCUS_ALL);
  1286. }
  1287. }
  1288. bool ColorPicker::are_swatches_enabled() const {
  1289. return can_add_swatches;
  1290. }
  1291. void ColorPicker::set_presets_visible(bool p_visible) {
  1292. if (presets_visible == p_visible) {
  1293. return;
  1294. }
  1295. presets_visible = p_visible;
  1296. btn_preset->set_visible(p_visible);
  1297. btn_recent_preset->set_visible(p_visible);
  1298. }
  1299. bool ColorPicker::are_presets_visible() const {
  1300. return presets_visible;
  1301. }
  1302. void ColorPicker::set_modes_visible(bool p_visible) {
  1303. if (color_modes_visible == p_visible) {
  1304. return;
  1305. }
  1306. color_modes_visible = p_visible;
  1307. mode_hbc->set_visible(p_visible);
  1308. }
  1309. bool ColorPicker::are_modes_visible() const {
  1310. return color_modes_visible;
  1311. }
  1312. void ColorPicker::set_sampler_visible(bool p_visible) {
  1313. if (sampler_visible == p_visible) {
  1314. return;
  1315. }
  1316. sampler_visible = p_visible;
  1317. sample_hbc->set_visible(p_visible);
  1318. }
  1319. bool ColorPicker::is_sampler_visible() const {
  1320. return sampler_visible;
  1321. }
  1322. void ColorPicker::set_sliders_visible(bool p_visible) {
  1323. if (sliders_visible == p_visible) {
  1324. return;
  1325. }
  1326. sliders_visible = p_visible;
  1327. slider_gc->set_visible(p_visible);
  1328. }
  1329. bool ColorPicker::are_sliders_visible() const {
  1330. return sliders_visible;
  1331. }
  1332. void ColorPicker::set_hex_visible(bool p_visible) {
  1333. if (hex_visible == p_visible) {
  1334. return;
  1335. }
  1336. hex_visible = p_visible;
  1337. hex_hbc->set_visible(p_visible);
  1338. }
  1339. bool ColorPicker::is_hex_visible() const {
  1340. return hex_visible;
  1341. }
  1342. void ColorPicker::_bind_methods() {
  1343. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPicker::set_pick_color);
  1344. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPicker::get_pick_color);
  1345. ClassDB::bind_method(D_METHOD("set_deferred_mode", "mode"), &ColorPicker::set_deferred_mode);
  1346. ClassDB::bind_method(D_METHOD("is_deferred_mode"), &ColorPicker::is_deferred_mode);
  1347. ClassDB::bind_method(D_METHOD("set_color_mode", "color_mode"), &ColorPicker::set_color_mode);
  1348. ClassDB::bind_method(D_METHOD("get_color_mode"), &ColorPicker::get_color_mode);
  1349. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPicker::set_edit_alpha);
  1350. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPicker::is_editing_alpha);
  1351. ClassDB::bind_method(D_METHOD("set_can_add_swatches", "enabled"), &ColorPicker::set_can_add_swatches);
  1352. ClassDB::bind_method(D_METHOD("are_swatches_enabled"), &ColorPicker::are_swatches_enabled);
  1353. ClassDB::bind_method(D_METHOD("set_presets_visible", "visible"), &ColorPicker::set_presets_visible);
  1354. ClassDB::bind_method(D_METHOD("are_presets_visible"), &ColorPicker::are_presets_visible);
  1355. ClassDB::bind_method(D_METHOD("set_modes_visible", "visible"), &ColorPicker::set_modes_visible);
  1356. ClassDB::bind_method(D_METHOD("are_modes_visible"), &ColorPicker::are_modes_visible);
  1357. ClassDB::bind_method(D_METHOD("set_sampler_visible", "visible"), &ColorPicker::set_sampler_visible);
  1358. ClassDB::bind_method(D_METHOD("is_sampler_visible"), &ColorPicker::is_sampler_visible);
  1359. ClassDB::bind_method(D_METHOD("set_sliders_visible", "visible"), &ColorPicker::set_sliders_visible);
  1360. ClassDB::bind_method(D_METHOD("are_sliders_visible"), &ColorPicker::are_sliders_visible);
  1361. ClassDB::bind_method(D_METHOD("set_hex_visible", "visible"), &ColorPicker::set_hex_visible);
  1362. ClassDB::bind_method(D_METHOD("is_hex_visible"), &ColorPicker::is_hex_visible);
  1363. ClassDB::bind_method(D_METHOD("add_preset", "color"), &ColorPicker::add_preset);
  1364. ClassDB::bind_method(D_METHOD("erase_preset", "color"), &ColorPicker::erase_preset);
  1365. ClassDB::bind_method(D_METHOD("get_presets"), &ColorPicker::get_presets);
  1366. ClassDB::bind_method(D_METHOD("add_recent_preset", "color"), &ColorPicker::add_recent_preset);
  1367. ClassDB::bind_method(D_METHOD("erase_recent_preset", "color"), &ColorPicker::erase_recent_preset);
  1368. ClassDB::bind_method(D_METHOD("get_recent_presets"), &ColorPicker::get_recent_presets);
  1369. ClassDB::bind_method(D_METHOD("set_picker_shape", "shape"), &ColorPicker::set_picker_shape);
  1370. ClassDB::bind_method(D_METHOD("get_picker_shape"), &ColorPicker::get_picker_shape);
  1371. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  1372. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  1373. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_mode", PROPERTY_HINT_ENUM, "RGB,HSV,RAW,OKHSL"), "set_color_mode", "get_color_mode");
  1374. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "deferred_mode"), "set_deferred_mode", "is_deferred_mode");
  1375. ADD_PROPERTY(PropertyInfo(Variant::INT, "picker_shape", PROPERTY_HINT_ENUM, "HSV Rectangle,HSV Rectangle Wheel,VHS Circle,OKHSL Circle,None"), "set_picker_shape", "get_picker_shape");
  1376. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_add_swatches"), "set_can_add_swatches", "are_swatches_enabled");
  1377. ADD_GROUP("Customization", "");
  1378. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sampler_visible"), "set_sampler_visible", "is_sampler_visible");
  1379. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "color_modes_visible"), "set_modes_visible", "are_modes_visible");
  1380. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sliders_visible"), "set_sliders_visible", "are_sliders_visible");
  1381. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hex_visible"), "set_hex_visible", "is_hex_visible");
  1382. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "presets_visible"), "set_presets_visible", "are_presets_visible");
  1383. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  1384. ADD_SIGNAL(MethodInfo("preset_added", PropertyInfo(Variant::COLOR, "color")));
  1385. ADD_SIGNAL(MethodInfo("preset_removed", PropertyInfo(Variant::COLOR, "color")));
  1386. BIND_ENUM_CONSTANT(MODE_RGB);
  1387. BIND_ENUM_CONSTANT(MODE_HSV);
  1388. BIND_ENUM_CONSTANT(MODE_RAW);
  1389. BIND_ENUM_CONSTANT(MODE_OKHSL);
  1390. BIND_ENUM_CONSTANT(SHAPE_HSV_RECTANGLE);
  1391. BIND_ENUM_CONSTANT(SHAPE_HSV_WHEEL);
  1392. BIND_ENUM_CONSTANT(SHAPE_VHS_CIRCLE);
  1393. BIND_ENUM_CONSTANT(SHAPE_OKHSL_CIRCLE);
  1394. BIND_ENUM_CONSTANT(SHAPE_NONE);
  1395. }
  1396. ColorPicker::ColorPicker() {
  1397. HBoxContainer *hb_edit = memnew(HBoxContainer);
  1398. add_child(hb_edit, false, INTERNAL_MODE_FRONT);
  1399. hb_edit->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1400. uv_edit = memnew(Control);
  1401. hb_edit->add_child(uv_edit);
  1402. uv_edit->connect("gui_input", callable_mp(this, &ColorPicker::_uv_input).bind(uv_edit));
  1403. uv_edit->set_mouse_filter(MOUSE_FILTER_PASS);
  1404. uv_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1405. uv_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1406. uv_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw).bind(0, uv_edit));
  1407. sample_hbc = memnew(HBoxContainer);
  1408. add_child(sample_hbc, false, INTERNAL_MODE_FRONT);
  1409. btn_pick = memnew(Button);
  1410. sample_hbc->add_child(btn_pick);
  1411. btn_pick->set_tooltip_text(RTR("Pick a color from the application window."));
  1412. btn_pick->connect(SNAME("pressed"), callable_mp(this, &ColorPicker::_pick_button_pressed));
  1413. sample = memnew(TextureRect);
  1414. sample_hbc->add_child(sample);
  1415. sample->set_h_size_flags(SIZE_EXPAND_FILL);
  1416. sample->connect("gui_input", callable_mp(this, &ColorPicker::_sample_input));
  1417. sample->connect("draw", callable_mp(this, &ColorPicker::_sample_draw));
  1418. btn_shape = memnew(MenuButton);
  1419. btn_shape->set_flat(false);
  1420. sample_hbc->add_child(btn_shape);
  1421. btn_shape->set_toggle_mode(true);
  1422. btn_shape->set_tooltip_text(RTR("Select a picker shape."));
  1423. current_shape = SHAPE_HSV_RECTANGLE;
  1424. shape_popup = btn_shape->get_popup();
  1425. shape_popup->add_icon_radio_check_item(get_theme_icon(SNAME("shape_rect"), SNAME("ColorPicker")), "HSV Rectangle", SHAPE_HSV_RECTANGLE);
  1426. shape_popup->add_icon_radio_check_item(get_theme_icon(SNAME("shape_rect_wheel"), SNAME("ColorPicker")), "HSV Wheel", SHAPE_HSV_WHEEL);
  1427. shape_popup->add_icon_radio_check_item(get_theme_icon(SNAME("shape_circle"), SNAME("ColorPicker")), "VHS Circle", SHAPE_VHS_CIRCLE);
  1428. shape_popup->add_icon_radio_check_item(get_theme_icon(SNAME("shape_circle"), SNAME("ColorPicker")), "OKHSL Circle", SHAPE_OKHSL_CIRCLE);
  1429. shape_popup->set_item_checked(current_shape, true);
  1430. shape_popup->connect("id_pressed", callable_mp(this, &ColorPicker::set_picker_shape));
  1431. btn_shape->set_icon(shape_popup->get_item_icon(current_shape));
  1432. add_mode(new ColorModeRGB(this));
  1433. add_mode(new ColorModeHSV(this));
  1434. add_mode(new ColorModeRAW(this));
  1435. add_mode(new ColorModeOKHSL(this));
  1436. mode_hbc = memnew(HBoxContainer);
  1437. add_child(mode_hbc, false, INTERNAL_MODE_FRONT);
  1438. mode_group.instantiate();
  1439. for (int i = 0; i < MODE_BUTTON_COUNT; i++) {
  1440. mode_btns[i] = memnew(Button);
  1441. mode_hbc->add_child(mode_btns[i]);
  1442. mode_btns[i]->set_focus_mode(FOCUS_NONE);
  1443. mode_btns[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  1444. mode_btns[i]->add_theme_style_override("pressed", get_theme_stylebox("tab_selected", "TabContainer"));
  1445. mode_btns[i]->add_theme_style_override("normal", get_theme_stylebox("tab_unselected", "TabContainer"));
  1446. mode_btns[i]->add_theme_style_override("hover", get_theme_stylebox("tab_selected", "TabContainer"));
  1447. mode_btns[i]->set_toggle_mode(true);
  1448. mode_btns[i]->set_text(modes[i]->get_name());
  1449. mode_btns[i]->set_button_group(mode_group);
  1450. mode_btns[i]->connect("pressed", callable_mp(this, &ColorPicker::set_color_mode).bind((ColorModeType)i));
  1451. }
  1452. mode_btns[0]->set_pressed(true);
  1453. btn_mode = memnew(MenuButton);
  1454. btn_mode->set_text("...");
  1455. btn_mode->set_flat(false);
  1456. mode_hbc->add_child(btn_mode);
  1457. btn_mode->set_toggle_mode(true);
  1458. btn_mode->set_tooltip_text(RTR("Select a picker mode."));
  1459. current_mode = MODE_RGB;
  1460. mode_popup = btn_mode->get_popup();
  1461. for (int i = 0; i < modes.size(); i++) {
  1462. mode_popup->add_radio_check_item(modes[i]->get_name(), i);
  1463. }
  1464. mode_popup->add_separator();
  1465. mode_popup->add_check_item("Colorized Sliders", MODE_MAX);
  1466. mode_popup->set_item_checked(current_mode, true);
  1467. mode_popup->set_item_checked(MODE_MAX + 1, true);
  1468. mode_popup->connect("id_pressed", callable_mp(this, &ColorPicker::_set_mode_popup_value));
  1469. VBoxContainer *vbl = memnew(VBoxContainer);
  1470. add_child(vbl, false, INTERNAL_MODE_FRONT);
  1471. VBoxContainer *vbr = memnew(VBoxContainer);
  1472. add_child(vbr, false, INTERNAL_MODE_FRONT);
  1473. vbr->set_h_size_flags(SIZE_EXPAND_FILL);
  1474. slider_gc = memnew(GridContainer);
  1475. vbr->add_child(slider_gc);
  1476. slider_gc->set_h_size_flags(SIZE_EXPAND_FILL);
  1477. slider_gc->set_columns(3);
  1478. for (int i = 0; i < SLIDER_COUNT + 1; i++) {
  1479. create_slider(slider_gc, i);
  1480. }
  1481. alpha_label->set_text("A");
  1482. hex_hbc = memnew(HBoxContainer);
  1483. hex_hbc->set_alignment(ALIGNMENT_BEGIN);
  1484. vbr->add_child(hex_hbc);
  1485. hex_hbc->add_child(memnew(Label("Hex")));
  1486. text_type = memnew(Button);
  1487. hex_hbc->add_child(text_type);
  1488. text_type->set_text("#");
  1489. text_type->set_tooltip_text(RTR("Switch between hexadecimal and code values."));
  1490. if (Engine::get_singleton()->is_editor_hint()) {
  1491. text_type->connect("pressed", callable_mp(this, &ColorPicker::_text_type_toggled));
  1492. } else {
  1493. text_type->set_flat(true);
  1494. text_type->set_mouse_filter(MOUSE_FILTER_IGNORE);
  1495. }
  1496. c_text = memnew(LineEdit);
  1497. hex_hbc->add_child(c_text);
  1498. c_text->set_select_all_on_focus(true);
  1499. c_text->connect("text_submitted", callable_mp(this, &ColorPicker::_html_submitted));
  1500. c_text->connect("text_changed", callable_mp(this, &ColorPicker::_text_changed));
  1501. c_text->connect("focus_exited", callable_mp(this, &ColorPicker::_html_focus_exit));
  1502. wheel_edit = memnew(AspectRatioContainer);
  1503. wheel_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  1504. wheel_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1505. hb_edit->add_child(wheel_edit);
  1506. wheel_mat.instantiate();
  1507. wheel_mat->set_shader(wheel_shader);
  1508. circle_mat.instantiate();
  1509. circle_mat->set_shader(circle_shader);
  1510. wheel_margin = memnew(MarginContainer);
  1511. wheel_margin->add_theme_constant_override("margin_bottom", 8);
  1512. wheel_edit->add_child(wheel_margin);
  1513. wheel = memnew(Control);
  1514. wheel_margin->add_child(wheel);
  1515. wheel->set_mouse_filter(MOUSE_FILTER_PASS);
  1516. wheel->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw).bind(2, wheel));
  1517. wheel_uv = memnew(Control);
  1518. wheel_margin->add_child(wheel_uv);
  1519. wheel_uv->connect("gui_input", callable_mp(this, &ColorPicker::_uv_input).bind(wheel_uv));
  1520. wheel_uv->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw).bind(0, wheel_uv));
  1521. w_edit = memnew(Control);
  1522. hb_edit->add_child(w_edit);
  1523. w_edit->set_h_size_flags(SIZE_FILL);
  1524. w_edit->set_v_size_flags(SIZE_EXPAND_FILL);
  1525. w_edit->connect("gui_input", callable_mp(this, &ColorPicker::_w_input));
  1526. w_edit->connect("draw", callable_mp(this, &ColorPicker::_hsv_draw).bind(1, w_edit));
  1527. _update_controls();
  1528. updating = false;
  1529. preset_container = memnew(GridContainer);
  1530. preset_container->set_h_size_flags(SIZE_EXPAND_FILL);
  1531. preset_container->set_columns(PRESET_COLUMN_COUNT);
  1532. preset_container->hide();
  1533. preset_group.instantiate();
  1534. btn_preset = memnew(Button);
  1535. btn_preset->set_text("Swatches");
  1536. btn_preset->set_flat(true);
  1537. btn_preset->set_toggle_mode(true);
  1538. btn_preset->set_focus_mode(FOCUS_NONE);
  1539. btn_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1540. btn_preset->connect("toggled", callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_preset, preset_container));
  1541. add_child(btn_preset, false, INTERNAL_MODE_FRONT);
  1542. add_child(preset_container, false, INTERNAL_MODE_FRONT);
  1543. recent_preset_hbc = memnew(HBoxContainer);
  1544. recent_preset_hbc->set_v_size_flags(SIZE_SHRINK_BEGIN);
  1545. recent_preset_hbc->hide();
  1546. recent_preset_group.instantiate();
  1547. btn_recent_preset = memnew(Button);
  1548. btn_recent_preset->set_text("Recent Colors");
  1549. btn_recent_preset->set_flat(true);
  1550. btn_recent_preset->set_toggle_mode(true);
  1551. btn_recent_preset->set_focus_mode(FOCUS_NONE);
  1552. btn_recent_preset->set_text_alignment(HORIZONTAL_ALIGNMENT_LEFT);
  1553. btn_recent_preset->connect("toggled", callable_mp(this, &ColorPicker::_show_hide_preset).bind(btn_recent_preset, recent_preset_hbc));
  1554. add_child(btn_recent_preset, false, INTERNAL_MODE_FRONT);
  1555. add_child(recent_preset_hbc, false, INTERNAL_MODE_FRONT);
  1556. set_pick_color(Color(1, 1, 1));
  1557. btn_add_preset = memnew(Button);
  1558. btn_add_preset->set_icon_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  1559. btn_add_preset->set_tooltip_text(RTR("Add current color as a preset."));
  1560. btn_add_preset->connect("pressed", callable_mp(this, &ColorPicker::_add_preset_pressed));
  1561. preset_container->add_child(btn_add_preset);
  1562. }
  1563. ColorPicker::~ColorPicker() {
  1564. for (int i = 0; i < modes.size(); i++) {
  1565. delete modes[i];
  1566. }
  1567. }
  1568. /////////////////
  1569. void ColorPickerButton::_about_to_popup() {
  1570. set_pressed(true);
  1571. if (picker) {
  1572. picker->set_old_color(color);
  1573. }
  1574. }
  1575. void ColorPickerButton::_color_changed(const Color &p_color) {
  1576. color = p_color;
  1577. queue_redraw();
  1578. emit_signal(SNAME("color_changed"), color);
  1579. }
  1580. void ColorPickerButton::_modal_closed() {
  1581. emit_signal(SNAME("popup_closed"));
  1582. set_pressed(false);
  1583. }
  1584. void ColorPickerButton::pressed() {
  1585. _update_picker();
  1586. Size2 size = get_size() * get_viewport()->get_canvas_transform().get_scale();
  1587. popup->reset_size();
  1588. picker->_update_presets();
  1589. picker->_update_recent_presets();
  1590. Rect2i usable_rect = popup->get_usable_parent_rect();
  1591. //let's try different positions to see which one we can use
  1592. Rect2i cp_rect(Point2i(), popup->get_size());
  1593. for (int i = 0; i < 4; i++) {
  1594. if (i > 1) {
  1595. cp_rect.position.y = get_screen_position().y - cp_rect.size.y;
  1596. } else {
  1597. cp_rect.position.y = get_screen_position().y + size.height;
  1598. }
  1599. if (i & 1) {
  1600. cp_rect.position.x = get_screen_position().x;
  1601. } else {
  1602. cp_rect.position.x = get_screen_position().x - MAX(0, (cp_rect.size.x - size.x));
  1603. }
  1604. if (usable_rect.encloses(cp_rect)) {
  1605. break;
  1606. }
  1607. }
  1608. popup->set_position(cp_rect.position);
  1609. popup->popup();
  1610. picker->set_focus_on_line_edit();
  1611. }
  1612. void ColorPickerButton::_notification(int p_what) {
  1613. switch (p_what) {
  1614. case NOTIFICATION_DRAW: {
  1615. const Ref<StyleBox> normal = get_theme_stylebox(SNAME("normal"));
  1616. const Rect2 r = Rect2(normal->get_offset(), get_size() - normal->get_minimum_size());
  1617. draw_texture_rect(Control::get_theme_icon(SNAME("bg"), SNAME("ColorPickerButton")), r, true);
  1618. draw_rect(r, color);
  1619. if (color.r > 1 || color.g > 1 || color.b > 1) {
  1620. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  1621. draw_texture(Control::get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPicker")), normal->get_offset());
  1622. }
  1623. } break;
  1624. case NOTIFICATION_WM_CLOSE_REQUEST: {
  1625. if (popup) {
  1626. popup->hide();
  1627. }
  1628. } break;
  1629. case NOTIFICATION_VISIBILITY_CHANGED: {
  1630. if (popup && !is_visible_in_tree()) {
  1631. popup->hide();
  1632. }
  1633. } break;
  1634. }
  1635. }
  1636. void ColorPickerButton::set_pick_color(const Color &p_color) {
  1637. if (color == p_color) {
  1638. return;
  1639. }
  1640. color = p_color;
  1641. if (picker) {
  1642. picker->set_pick_color(p_color);
  1643. }
  1644. queue_redraw();
  1645. }
  1646. Color ColorPickerButton::get_pick_color() const {
  1647. return color;
  1648. }
  1649. void ColorPickerButton::set_edit_alpha(bool p_show) {
  1650. if (edit_alpha == p_show) {
  1651. return;
  1652. }
  1653. edit_alpha = p_show;
  1654. if (picker) {
  1655. picker->set_edit_alpha(p_show);
  1656. }
  1657. }
  1658. bool ColorPickerButton::is_editing_alpha() const {
  1659. return edit_alpha;
  1660. }
  1661. ColorPicker *ColorPickerButton::get_picker() {
  1662. _update_picker();
  1663. return picker;
  1664. }
  1665. PopupPanel *ColorPickerButton::get_popup() {
  1666. _update_picker();
  1667. return popup;
  1668. }
  1669. void ColorPickerButton::_update_picker() {
  1670. if (!picker) {
  1671. popup = memnew(PopupPanel);
  1672. popup->set_wrap_controls(true);
  1673. picker = memnew(ColorPicker);
  1674. picker->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
  1675. popup->add_child(picker);
  1676. add_child(popup, false, INTERNAL_MODE_FRONT);
  1677. picker->connect("color_changed", callable_mp(this, &ColorPickerButton::_color_changed));
  1678. popup->connect("about_to_popup", callable_mp(this, &ColorPickerButton::_about_to_popup));
  1679. popup->connect("popup_hide", callable_mp(this, &ColorPickerButton::_modal_closed));
  1680. picker->connect("minimum_size_changed", callable_mp((Window *)popup, &Window::reset_size));
  1681. picker->set_pick_color(color);
  1682. picker->set_edit_alpha(edit_alpha);
  1683. picker->set_display_old_color(true);
  1684. emit_signal(SNAME("picker_created"));
  1685. }
  1686. }
  1687. void ColorPickerButton::_bind_methods() {
  1688. ClassDB::bind_method(D_METHOD("set_pick_color", "color"), &ColorPickerButton::set_pick_color);
  1689. ClassDB::bind_method(D_METHOD("get_pick_color"), &ColorPickerButton::get_pick_color);
  1690. ClassDB::bind_method(D_METHOD("get_picker"), &ColorPickerButton::get_picker);
  1691. ClassDB::bind_method(D_METHOD("get_popup"), &ColorPickerButton::get_popup);
  1692. ClassDB::bind_method(D_METHOD("set_edit_alpha", "show"), &ColorPickerButton::set_edit_alpha);
  1693. ClassDB::bind_method(D_METHOD("is_editing_alpha"), &ColorPickerButton::is_editing_alpha);
  1694. ClassDB::bind_method(D_METHOD("_about_to_popup"), &ColorPickerButton::_about_to_popup);
  1695. ADD_SIGNAL(MethodInfo("color_changed", PropertyInfo(Variant::COLOR, "color")));
  1696. ADD_SIGNAL(MethodInfo("popup_closed"));
  1697. ADD_SIGNAL(MethodInfo("picker_created"));
  1698. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "color"), "set_pick_color", "get_pick_color");
  1699. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "edit_alpha"), "set_edit_alpha", "is_editing_alpha");
  1700. }
  1701. ColorPickerButton::ColorPickerButton(const String &p_text) :
  1702. Button(p_text) {
  1703. set_toggle_mode(true);
  1704. }
  1705. /////////////////
  1706. void ColorPresetButton::_notification(int p_what) {
  1707. switch (p_what) {
  1708. case NOTIFICATION_DRAW: {
  1709. const Rect2 r = Rect2(Point2(0, 0), get_size());
  1710. Ref<StyleBox> sb_raw = get_theme_stylebox(SNAME("preset_fg"), SNAME("ColorPresetButton"))->duplicate();
  1711. Ref<StyleBoxFlat> sb_flat = sb_raw;
  1712. Ref<StyleBoxTexture> sb_texture = sb_raw;
  1713. if (sb_flat.is_valid()) {
  1714. sb_flat->set_border_width(SIDE_BOTTOM, 2);
  1715. if (get_draw_mode() == DRAW_PRESSED || get_draw_mode() == DRAW_HOVER_PRESSED) {
  1716. sb_flat->set_border_color(Color(1, 1, 1, 1));
  1717. } else {
  1718. sb_flat->set_border_color(Color(0, 0, 0, 1));
  1719. }
  1720. if (preset_color.a < 1) {
  1721. // Draw a background pattern when the color is transparent.
  1722. sb_flat->set_bg_color(Color(1, 1, 1));
  1723. sb_flat->draw(get_canvas_item(), r);
  1724. Rect2 bg_texture_rect = r.grow_side(SIDE_LEFT, -sb_flat->get_margin(SIDE_LEFT));
  1725. bg_texture_rect = bg_texture_rect.grow_side(SIDE_RIGHT, -sb_flat->get_margin(SIDE_RIGHT));
  1726. bg_texture_rect = bg_texture_rect.grow_side(SIDE_TOP, -sb_flat->get_margin(SIDE_TOP));
  1727. bg_texture_rect = bg_texture_rect.grow_side(SIDE_BOTTOM, -sb_flat->get_margin(SIDE_BOTTOM));
  1728. draw_texture_rect(get_theme_icon(SNAME("preset_bg"), SNAME("ColorPresetButton")), bg_texture_rect, true);
  1729. sb_flat->set_bg_color(preset_color);
  1730. }
  1731. sb_flat->set_bg_color(preset_color);
  1732. sb_flat->draw(get_canvas_item(), r);
  1733. } else if (sb_texture.is_valid()) {
  1734. if (preset_color.a < 1) {
  1735. // Draw a background pattern when the color is transparent.
  1736. 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);
  1737. draw_texture_rect(get_theme_icon(SNAME("preset_bg"), SNAME("ColorPresetButton")), r, use_tile_texture);
  1738. }
  1739. sb_texture->set_modulate(preset_color);
  1740. sb_texture->draw(get_canvas_item(), r);
  1741. } else {
  1742. WARN_PRINT("Unsupported StyleBox used for ColorPresetButton. Use StyleBoxFlat or StyleBoxTexture instead.");
  1743. }
  1744. if (preset_color.r > 1 || preset_color.g > 1 || preset_color.b > 1) {
  1745. // Draw an indicator to denote that the color is "overbright" and can't be displayed accurately in the preview
  1746. draw_texture(Control::get_theme_icon(SNAME("overbright_indicator"), SNAME("ColorPresetButton")), Vector2(0, 0));
  1747. }
  1748. } break;
  1749. }
  1750. }
  1751. void ColorPresetButton::set_preset_color(const Color &p_color) {
  1752. preset_color = p_color;
  1753. }
  1754. Color ColorPresetButton::get_preset_color() const {
  1755. return preset_color;
  1756. }
  1757. ColorPresetButton::ColorPresetButton(Color p_color, int p_size) {
  1758. preset_color = p_color;
  1759. set_toggle_mode(true);
  1760. set_custom_minimum_size(Size2(p_size, p_size));
  1761. }
  1762. ColorPresetButton::~ColorPresetButton() {
  1763. }