color_picker.cpp 73 KB

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