font_config_plugin.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /**************************************************************************/
  2. /* font_config_plugin.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 "font_config_plugin.h"
  31. #include "core/string/translation_server.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/import/dynamic_font_import_settings.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/margin_container.h"
  36. /*************************************************************************/
  37. /* EditorPropertyFontMetaObject */
  38. /*************************************************************************/
  39. bool EditorPropertyFontMetaObject::_set(const StringName &p_name, const Variant &p_value) {
  40. String name = p_name;
  41. if (name.begins_with("keys")) {
  42. String key = name.get_slicec('/', 1);
  43. dict[key] = p_value;
  44. return true;
  45. }
  46. return false;
  47. }
  48. bool EditorPropertyFontMetaObject::_get(const StringName &p_name, Variant &r_ret) const {
  49. String name = p_name;
  50. if (name.begins_with("keys")) {
  51. String key = name.get_slicec('/', 1);
  52. r_ret = dict[key];
  53. return true;
  54. }
  55. return false;
  56. }
  57. void EditorPropertyFontMetaObject::set_dict(const Dictionary &p_dict) {
  58. dict = p_dict;
  59. }
  60. Dictionary EditorPropertyFontMetaObject::get_dict() {
  61. return dict;
  62. }
  63. /*************************************************************************/
  64. /* EditorPropertyFontOTObject */
  65. /*************************************************************************/
  66. bool EditorPropertyFontOTObject::_set(const StringName &p_name, const Variant &p_value) {
  67. String name = p_name;
  68. if (name.begins_with("keys")) {
  69. int key = name.get_slicec('/', 1).to_int();
  70. dict[key] = p_value;
  71. return true;
  72. }
  73. return false;
  74. }
  75. bool EditorPropertyFontOTObject::_get(const StringName &p_name, Variant &r_ret) const {
  76. String name = p_name;
  77. if (name.begins_with("keys")) {
  78. int key = name.get_slicec('/', 1).to_int();
  79. r_ret = dict[key];
  80. return true;
  81. }
  82. return false;
  83. }
  84. void EditorPropertyFontOTObject::set_dict(const Dictionary &p_dict) {
  85. dict = p_dict;
  86. }
  87. Dictionary EditorPropertyFontOTObject::get_dict() {
  88. return dict;
  89. }
  90. void EditorPropertyFontOTObject::set_defaults(const Dictionary &p_dict) {
  91. defaults_dict = p_dict;
  92. }
  93. Dictionary EditorPropertyFontOTObject::get_defaults() {
  94. return defaults_dict;
  95. }
  96. bool EditorPropertyFontOTObject::_property_can_revert(const StringName &p_name) const {
  97. String name = p_name;
  98. if (name.begins_with("keys")) {
  99. int key = name.get_slicec('/', 1).to_int();
  100. return defaults_dict.has(key) && dict.has(key);
  101. }
  102. return false;
  103. }
  104. bool EditorPropertyFontOTObject::_property_get_revert(const StringName &p_name, Variant &r_property) const {
  105. String name = p_name;
  106. if (name.begins_with("keys")) {
  107. int key = name.get_slicec('/', 1).to_int();
  108. if (defaults_dict.has(key)) {
  109. Vector3i range = defaults_dict[key];
  110. r_property = range.z;
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. /*************************************************************************/
  117. /* EditorPropertyFontMetaOverride */
  118. /*************************************************************************/
  119. void EditorPropertyFontMetaOverride::_notification(int p_what) {
  120. switch (p_what) {
  121. case NOTIFICATION_ENTER_TREE:
  122. case NOTIFICATION_THEME_CHANGED: {
  123. if (button_add) {
  124. button_add->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  125. }
  126. } break;
  127. }
  128. }
  129. void EditorPropertyFontMetaOverride::_property_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
  130. if (p_property.begins_with("keys")) {
  131. Dictionary dict = object->get_dict();
  132. String key = p_property.get_slicec('/', 1);
  133. dict[key] = (bool)p_value;
  134. emit_changed(get_edited_property(), dict, "", true);
  135. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  136. object->set_dict(dict);
  137. }
  138. }
  139. void EditorPropertyFontMetaOverride::_remove(Object *p_button, const String &p_key) {
  140. Dictionary dict = object->get_dict();
  141. dict.erase(p_key);
  142. emit_changed(get_edited_property(), dict, "", false);
  143. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  144. object->set_dict(dict);
  145. update_property();
  146. }
  147. void EditorPropertyFontMetaOverride::_add_menu() {
  148. if (script_editor) {
  149. Size2 size = get_size();
  150. menu->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  151. menu->reset_size();
  152. menu->popup();
  153. } else {
  154. locale_select->popup_locale_dialog();
  155. }
  156. }
  157. void EditorPropertyFontMetaOverride::_add_script(int p_option) {
  158. Dictionary dict = object->get_dict();
  159. dict[script_codes[p_option]] = true;
  160. emit_changed(get_edited_property(), dict, "", false);
  161. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  162. object->set_dict(dict);
  163. update_property();
  164. }
  165. void EditorPropertyFontMetaOverride::_add_lang(const String &p_locale) {
  166. Dictionary dict = object->get_dict();
  167. dict[p_locale] = true;
  168. emit_changed(get_edited_property(), dict, "", false);
  169. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  170. object->set_dict(dict);
  171. update_property();
  172. }
  173. void EditorPropertyFontMetaOverride::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  174. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  175. }
  176. void EditorPropertyFontMetaOverride::update_property() {
  177. Variant updated_val = get_edited_property_value();
  178. Dictionary dict = updated_val;
  179. edit->set_text(vformat(TTR("Overrides (%d)"), dict.size()));
  180. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  181. if (edit->is_pressed() != unfolded) {
  182. edit->set_pressed(unfolded);
  183. }
  184. if (unfolded) {
  185. updating = true;
  186. if (!container) {
  187. container = memnew(MarginContainer);
  188. container->set_theme_type_variation("MarginContainer4px");
  189. add_child(container);
  190. set_bottom_editor(container);
  191. VBoxContainer *vbox = memnew(VBoxContainer);
  192. vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  193. container->add_child(vbox);
  194. property_vbox = memnew(VBoxContainer);
  195. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  196. vbox->add_child(property_vbox);
  197. paginator = memnew(EditorPaginator);
  198. paginator->connect("page_changed", callable_mp(this, &EditorPropertyFontMetaOverride::_page_changed));
  199. vbox->add_child(paginator);
  200. } else {
  201. // Queue children for deletion, deleting immediately might cause errors.
  202. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  203. property_vbox->get_child(i)->queue_free();
  204. }
  205. button_add = nullptr;
  206. }
  207. int size = dict.size();
  208. int max_page = MAX(0, size - 1) / page_length;
  209. page_index = MIN(page_index, max_page);
  210. paginator->update(page_index, max_page);
  211. paginator->set_visible(max_page > 0);
  212. int offset = page_index * page_length;
  213. int amount = MIN(size - offset, page_length);
  214. dict = dict.duplicate();
  215. object->set_dict(dict);
  216. for (int i = 0; i < amount; i++) {
  217. String name = dict.get_key_at_index(i);
  218. EditorProperty *prop = memnew(EditorPropertyCheck);
  219. prop->set_object_and_property(object.ptr(), "keys/" + name);
  220. if (script_editor) {
  221. prop->set_label(TranslationServer::get_singleton()->get_script_name(name));
  222. } else {
  223. prop->set_label(TranslationServer::get_singleton()->get_locale_name(name));
  224. }
  225. prop->set_tooltip_text(name);
  226. prop->set_selectable(false);
  227. prop->connect("property_changed", callable_mp(this, &EditorPropertyFontMetaOverride::_property_changed));
  228. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyFontMetaOverride::_object_id_selected));
  229. HBoxContainer *hbox = memnew(HBoxContainer);
  230. property_vbox->add_child(hbox);
  231. hbox->add_child(prop);
  232. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  233. Button *remove = memnew(Button);
  234. remove->set_accessibility_name(TTRC("Remove"));
  235. remove->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  236. hbox->add_child(remove);
  237. remove->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyFontMetaOverride::_remove).bind(remove, name));
  238. prop->update_property();
  239. }
  240. if (script_editor) {
  241. // TRANSLATORS: Script refers to a writing system.
  242. button_add = EditorInspector::create_inspector_action_button(TTR("Add Script", "Locale"));
  243. } else {
  244. button_add = EditorInspector::create_inspector_action_button(TTR("Add Locale"));
  245. }
  246. button_add->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyFontMetaOverride::_add_menu));
  247. property_vbox->add_child(button_add);
  248. updating = false;
  249. } else {
  250. if (container) {
  251. set_bottom_editor(nullptr);
  252. memdelete(container);
  253. button_add = nullptr;
  254. container = nullptr;
  255. }
  256. }
  257. }
  258. void EditorPropertyFontMetaOverride::_edit_pressed() {
  259. Variant prop_val = get_edited_property_value();
  260. if (prop_val.get_type() == Variant::NIL) {
  261. Callable::CallError ce;
  262. Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
  263. get_edited_object()->set(get_edited_property(), prop_val);
  264. }
  265. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  266. update_property();
  267. }
  268. void EditorPropertyFontMetaOverride::_page_changed(int p_page) {
  269. if (updating) {
  270. return;
  271. }
  272. page_index = p_page;
  273. update_property();
  274. }
  275. EditorPropertyFontMetaOverride::EditorPropertyFontMetaOverride(bool p_script) {
  276. script_editor = p_script;
  277. object.instantiate();
  278. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  279. edit = memnew(Button);
  280. edit->set_accessibility_name(TTRC("Edit"));
  281. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  282. edit->set_clip_text(true);
  283. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyFontMetaOverride::_edit_pressed));
  284. edit->set_toggle_mode(true);
  285. add_child(edit);
  286. add_focusable(edit);
  287. menu = memnew(PopupMenu);
  288. if (script_editor) {
  289. script_codes = TranslationServer::get_singleton()->get_all_scripts();
  290. for (int i = 0; i < script_codes.size(); i++) {
  291. menu->add_item(TranslationServer::get_singleton()->get_script_name(script_codes[i]) + " (" + script_codes[i] + ")", i);
  292. }
  293. }
  294. add_child(menu);
  295. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyFontMetaOverride::_add_script));
  296. locale_select = memnew(EditorLocaleDialog);
  297. locale_select->connect("locale_selected", callable_mp(this, &EditorPropertyFontMetaOverride::_add_lang));
  298. add_child(locale_select);
  299. }
  300. /*************************************************************************/
  301. /* EditorPropertyOTVariation */
  302. /*************************************************************************/
  303. void EditorPropertyOTVariation::_property_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
  304. if (p_property.begins_with("keys")) {
  305. Dictionary dict = object->get_dict();
  306. int key = p_property.get_slicec('/', 1).to_int();
  307. dict[key] = (int)p_value;
  308. emit_changed(get_edited_property(), dict, "", true);
  309. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  310. object->set_dict(dict);
  311. }
  312. }
  313. void EditorPropertyOTVariation::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  314. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  315. }
  316. void EditorPropertyOTVariation::update_property() {
  317. Variant updated_val = get_edited_property_value();
  318. Dictionary dict = updated_val;
  319. Ref<Font> fd;
  320. if (Object::cast_to<Font>(get_edited_object()) != nullptr) {
  321. fd = get_edited_object();
  322. } else if (Object::cast_to<DynamicFontImportSettingsData>(get_edited_object()) != nullptr) {
  323. Ref<DynamicFontImportSettingsData> imp = Object::cast_to<DynamicFontImportSettingsData>(get_edited_object());
  324. fd = imp->get_font();
  325. }
  326. Dictionary supported = (fd.is_valid()) ? fd->get_supported_variation_list() : Dictionary();
  327. for (int i = 0; i < supported.size(); i++) {
  328. int name_tag = supported.get_key_at_index(i);
  329. Vector3i range = supported.get_value_at_index(i);
  330. if ((dict.has(name_tag) && dict[name_tag].get_type() == Variant::NIL) || !dict.has(name_tag)) {
  331. dict[name_tag] = range.z;
  332. }
  333. }
  334. edit->set_text(vformat(TTR("Variation Coordinates (%d)"), supported.size()));
  335. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  336. if (edit->is_pressed() != unfolded) {
  337. edit->set_pressed(unfolded);
  338. }
  339. if (unfolded) {
  340. updating = true;
  341. if (!container) {
  342. container = memnew(MarginContainer);
  343. container->set_theme_type_variation("MarginContainer4px");
  344. add_child(container);
  345. set_bottom_editor(container);
  346. VBoxContainer *vbox = memnew(VBoxContainer);
  347. vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  348. container->add_child(vbox);
  349. property_vbox = memnew(VBoxContainer);
  350. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  351. vbox->add_child(property_vbox);
  352. paginator = memnew(EditorPaginator);
  353. paginator->connect("page_changed", callable_mp(this, &EditorPropertyOTVariation::_page_changed));
  354. vbox->add_child(paginator);
  355. } else {
  356. // Queue children for deletion, deleting immediately might cause errors.
  357. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  358. property_vbox->get_child(i)->queue_free();
  359. }
  360. }
  361. int size = supported.size();
  362. int max_page = MAX(0, size - 1) / page_length;
  363. page_index = MIN(page_index, max_page);
  364. paginator->update(page_index, max_page);
  365. paginator->set_visible(max_page > 0);
  366. int offset = page_index * page_length;
  367. int amount = MIN(size - offset, page_length);
  368. dict = dict.duplicate();
  369. object->set_dict(dict);
  370. object->set_defaults(supported);
  371. for (int i = 0; i < amount; i++) {
  372. int name_tag = supported.get_key_at_index(i);
  373. Vector3i range = supported.get_value_at_index(i);
  374. EditorPropertyInteger *prop = memnew(EditorPropertyInteger);
  375. prop->setup(range.x, range.y, false, true, false, false);
  376. prop->set_object_and_property(object.ptr(), "keys/" + itos(name_tag));
  377. String name = TS->tag_to_name(name_tag);
  378. String name_cap;
  379. {
  380. String aux = name.replace("_", " ").strip_edges();
  381. for (int j = 0; j < aux.get_slice_count(" "); j++) {
  382. String slice = aux.get_slicec(' ', j);
  383. if (slice.length() > 0) {
  384. slice[0] = String::char_uppercase(slice[0]);
  385. if (i > 0) {
  386. name_cap += " ";
  387. }
  388. name_cap += slice;
  389. }
  390. }
  391. }
  392. prop->set_label(name_cap);
  393. prop->set_tooltip_text(name);
  394. prop->set_selectable(false);
  395. prop->connect("property_changed", callable_mp(this, &EditorPropertyOTVariation::_property_changed));
  396. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyOTVariation::_object_id_selected));
  397. property_vbox->add_child(prop);
  398. prop->update_property();
  399. }
  400. updating = false;
  401. } else {
  402. if (container) {
  403. set_bottom_editor(nullptr);
  404. memdelete(container);
  405. container = nullptr;
  406. }
  407. }
  408. }
  409. void EditorPropertyOTVariation::_edit_pressed() {
  410. Variant prop_val = get_edited_property_value();
  411. if (prop_val.get_type() == Variant::NIL) {
  412. Callable::CallError ce;
  413. Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
  414. get_edited_object()->set(get_edited_property(), prop_val);
  415. }
  416. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  417. update_property();
  418. }
  419. void EditorPropertyOTVariation::_page_changed(int p_page) {
  420. if (updating) {
  421. return;
  422. }
  423. page_index = p_page;
  424. update_property();
  425. }
  426. EditorPropertyOTVariation::EditorPropertyOTVariation() {
  427. object.instantiate();
  428. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  429. edit = memnew(Button);
  430. edit->set_accessibility_name(TTRC("Edit"));
  431. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  432. edit->set_clip_text(true);
  433. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyOTVariation::_edit_pressed));
  434. edit->set_toggle_mode(true);
  435. add_child(edit);
  436. add_focusable(edit);
  437. }
  438. /*************************************************************************/
  439. /* EditorPropertyOTFeatures */
  440. /*************************************************************************/
  441. void EditorPropertyOTFeatures::_notification(int p_what) {
  442. switch (p_what) {
  443. case NOTIFICATION_ENTER_TREE:
  444. case NOTIFICATION_THEME_CHANGED: {
  445. if (button_add) {
  446. button_add->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  447. }
  448. } break;
  449. }
  450. }
  451. void EditorPropertyOTFeatures::_property_changed(const String &p_property, const Variant &p_value, const String &p_name, bool p_changing) {
  452. if (p_property.begins_with("keys")) {
  453. Dictionary dict = object->get_dict();
  454. int key = p_property.get_slicec('/', 1).to_int();
  455. dict[key] = (int)p_value;
  456. emit_changed(get_edited_property(), dict, "", true);
  457. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  458. object->set_dict(dict);
  459. }
  460. }
  461. void EditorPropertyOTFeatures::_remove(Object *p_button, int p_key) {
  462. Dictionary dict = object->get_dict();
  463. dict.erase(p_key);
  464. emit_changed(get_edited_property(), dict, "", false);
  465. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  466. object->set_dict(dict);
  467. update_property();
  468. }
  469. void EditorPropertyOTFeatures::_add_menu() {
  470. Size2 size = get_size();
  471. menu->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  472. menu->reset_size();
  473. menu->popup();
  474. }
  475. void EditorPropertyOTFeatures::_add_feature(int p_option) {
  476. Dictionary dict = object->get_dict();
  477. dict[p_option] = 1;
  478. emit_changed(get_edited_property(), dict, "", false);
  479. dict = dict.duplicate(); // Duplicate, so undo/redo works better.
  480. object->set_dict(dict);
  481. update_property();
  482. }
  483. void EditorPropertyOTFeatures::_object_id_selected(const StringName &p_property, ObjectID p_id) {
  484. emit_signal(SNAME("object_id_selected"), p_property, p_id);
  485. }
  486. void EditorPropertyOTFeatures::update_property() {
  487. Variant updated_val = get_edited_property_value();
  488. Dictionary dict = updated_val;
  489. Ref<Font> fd;
  490. if (Object::cast_to<FontVariation>(get_edited_object()) != nullptr) {
  491. fd = get_edited_object();
  492. } else if (Object::cast_to<DynamicFontImportSettingsData>(get_edited_object()) != nullptr) {
  493. Ref<DynamicFontImportSettingsData> imp = Object::cast_to<DynamicFontImportSettingsData>(get_edited_object());
  494. fd = imp->get_font();
  495. }
  496. Dictionary supported;
  497. if (fd.is_valid()) {
  498. supported = fd->get_supported_feature_list();
  499. }
  500. if (supported.is_empty()) {
  501. edit->set_text(vformat(TTR("No supported features")));
  502. if (container) {
  503. set_bottom_editor(nullptr);
  504. memdelete(container);
  505. button_add = nullptr;
  506. container = nullptr;
  507. }
  508. return;
  509. }
  510. edit->set_text(vformat(TTR("Features (%d of %d set)"), dict.size(), supported.size()));
  511. bool unfolded = get_edited_object()->editor_is_section_unfolded(get_edited_property());
  512. if (edit->is_pressed() != unfolded) {
  513. edit->set_pressed(unfolded);
  514. }
  515. if (unfolded) {
  516. updating = true;
  517. if (!container) {
  518. container = memnew(MarginContainer);
  519. container->set_theme_type_variation("MarginContainer4px");
  520. add_child(container);
  521. set_bottom_editor(container);
  522. VBoxContainer *vbox = memnew(VBoxContainer);
  523. vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  524. container->add_child(vbox);
  525. property_vbox = memnew(VBoxContainer);
  526. property_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  527. vbox->add_child(property_vbox);
  528. paginator = memnew(EditorPaginator);
  529. paginator->connect("page_changed", callable_mp(this, &EditorPropertyOTFeatures::_page_changed));
  530. vbox->add_child(paginator);
  531. } else {
  532. // Queue children for deletion, deleting immediately might cause errors.
  533. for (int i = property_vbox->get_child_count() - 1; i >= 0; i--) {
  534. property_vbox->get_child(i)->queue_free();
  535. }
  536. button_add = nullptr;
  537. }
  538. // Update add menu items.
  539. menu->clear(false);
  540. bool have_sub[FGRP_MAX];
  541. for (int i = 0; i < FGRP_MAX; i++) {
  542. menu_sub[i]->clear();
  543. have_sub[i] = false;
  544. }
  545. bool show_hidden = EDITOR_GET("interface/inspector/show_low_level_opentype_features");
  546. for (int i = 0; i < supported.size(); i++) {
  547. int name_tag = supported.get_key_at_index(i);
  548. Dictionary info = supported.get_value_at_index(i);
  549. bool hidden = info["hidden"].operator bool();
  550. String name = TS->tag_to_name(name_tag);
  551. FeatureGroups grp = FGRP_MAX;
  552. if (hidden && !show_hidden) {
  553. continue;
  554. }
  555. if (name.begins_with("stylistic_set_")) {
  556. grp = FGRP_STYLISTIC_SET;
  557. } else if (name.begins_with("character_variant_")) {
  558. grp = FGRP_CHARACTER_VARIANT;
  559. } else if (name.ends_with("_capitals")) {
  560. grp = FGRP_CAPITLS;
  561. } else if (name.ends_with("_ligatures")) {
  562. grp = FGRP_LIGATURES;
  563. } else if (name.ends_with("_alternates")) {
  564. grp = FGRP_ALTERNATES;
  565. } else if (name.ends_with("_kanji_forms") || name.begins_with("jis") || name == "simplified_forms" || name == "traditional_name_forms" || name == "traditional_forms") {
  566. grp = FGRP_EAL;
  567. } else if (name.ends_with("_widths")) {
  568. grp = FGRP_EAW;
  569. } else if (name == "tabular_figures" || name == "proportional_figures") {
  570. grp = FGRP_NUMAL;
  571. } else if (name.begins_with("custom_")) {
  572. grp = FGRP_CUSTOM;
  573. }
  574. String disp_name = name.capitalize();
  575. if (info.has("label")) {
  576. disp_name = vformat("%s (%s)", disp_name, info["label"].operator String());
  577. }
  578. if (grp == FGRP_MAX) {
  579. menu->add_item(disp_name, name_tag);
  580. } else {
  581. menu_sub[grp]->add_item(disp_name, name_tag);
  582. have_sub[grp] = true;
  583. }
  584. }
  585. for (int i = 0; i < FGRP_MAX; i++) {
  586. if (have_sub[i]) {
  587. menu->add_submenu_node_item(TTRGET(group_names[i]), menu_sub[i]);
  588. }
  589. }
  590. int size = dict.size();
  591. int max_page = MAX(0, size - 1) / page_length;
  592. page_index = MIN(page_index, max_page);
  593. paginator->update(page_index, max_page);
  594. paginator->set_visible(max_page > 0);
  595. int offset = page_index * page_length;
  596. int amount = MIN(size - offset, page_length);
  597. dict = dict.duplicate();
  598. object->set_dict(dict);
  599. for (int i = 0; i < amount; i++) {
  600. int name_tag = dict.get_key_at_index(i);
  601. if (supported.has(name_tag)) {
  602. Dictionary info = supported[name_tag];
  603. Variant::Type vtype = Variant::Type(info["type"].operator int());
  604. bool hidden = info["hidden"].operator bool();
  605. if (hidden && !show_hidden) {
  606. continue;
  607. }
  608. EditorProperty *prop = nullptr;
  609. switch (vtype) {
  610. case Variant::NIL: {
  611. prop = memnew(EditorPropertyNil);
  612. } break;
  613. case Variant::BOOL: {
  614. prop = memnew(EditorPropertyCheck);
  615. } break;
  616. case Variant::INT: {
  617. EditorPropertyInteger *editor = memnew(EditorPropertyInteger);
  618. editor->setup(0, 255, 1, false, false, false);
  619. prop = editor;
  620. } break;
  621. default: {
  622. ERR_CONTINUE_MSG(true, vformat("Unsupported OT feature data type %s", Variant::get_type_name(vtype)));
  623. }
  624. }
  625. prop->set_object_and_property(object.ptr(), "keys/" + itos(name_tag));
  626. String name = TS->tag_to_name(name_tag);
  627. String disp_name = name.capitalize();
  628. if (info.has("label")) {
  629. disp_name = vformat("%s (%s)", disp_name, info["label"].operator String());
  630. }
  631. prop->set_label(disp_name);
  632. prop->set_tooltip_text(name);
  633. prop->set_selectable(false);
  634. prop->connect("property_changed", callable_mp(this, &EditorPropertyOTFeatures::_property_changed));
  635. prop->connect("object_id_selected", callable_mp(this, &EditorPropertyOTFeatures::_object_id_selected));
  636. HBoxContainer *hbox = memnew(HBoxContainer);
  637. property_vbox->add_child(hbox);
  638. hbox->add_child(prop);
  639. prop->set_h_size_flags(SIZE_EXPAND_FILL);
  640. Button *remove = memnew(Button);
  641. remove->set_accessibility_name(TTRC("Remove"));
  642. remove->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
  643. hbox->add_child(remove);
  644. remove->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyOTFeatures::_remove).bind(remove, name_tag));
  645. prop->update_property();
  646. }
  647. }
  648. button_add = EditorInspector::create_inspector_action_button(TTR("Add Feature"));
  649. button_add->set_button_icon(get_editor_theme_icon(SNAME("Add")));
  650. button_add->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyOTFeatures::_add_menu));
  651. property_vbox->add_child(button_add);
  652. updating = false;
  653. } else {
  654. if (container) {
  655. set_bottom_editor(nullptr);
  656. memdelete(container);
  657. button_add = nullptr;
  658. container = nullptr;
  659. }
  660. }
  661. }
  662. void EditorPropertyOTFeatures::_edit_pressed() {
  663. Variant prop_val = get_edited_property_value();
  664. if (prop_val.get_type() == Variant::NIL) {
  665. Callable::CallError ce;
  666. Variant::construct(Variant::DICTIONARY, prop_val, nullptr, 0, ce);
  667. get_edited_object()->set(get_edited_property(), prop_val);
  668. }
  669. get_edited_object()->editor_set_section_unfold(get_edited_property(), edit->is_pressed());
  670. update_property();
  671. }
  672. void EditorPropertyOTFeatures::_page_changed(int p_page) {
  673. if (updating) {
  674. return;
  675. }
  676. page_index = p_page;
  677. update_property();
  678. }
  679. EditorPropertyOTFeatures::EditorPropertyOTFeatures() {
  680. object.instantiate();
  681. page_length = int(EDITOR_GET("interface/inspector/max_array_dictionary_items_per_page"));
  682. edit = memnew(Button);
  683. edit->set_accessibility_name(TTRC("Edit"));
  684. edit->set_h_size_flags(SIZE_EXPAND_FILL);
  685. edit->set_clip_text(true);
  686. edit->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyOTFeatures::_edit_pressed));
  687. edit->set_toggle_mode(true);
  688. add_child(edit);
  689. add_focusable(edit);
  690. menu = memnew(PopupMenu);
  691. add_child(menu);
  692. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyOTFeatures::_add_feature));
  693. for (int i = 0; i < FGRP_MAX; i++) {
  694. menu_sub[i] = memnew(PopupMenu);
  695. menu->add_child(menu_sub[i]);
  696. menu_sub[i]->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyOTFeatures::_add_feature));
  697. }
  698. group_names[FGRP_STYLISTIC_SET] = TTRC("Stylistic Sets");
  699. group_names[FGRP_CHARACTER_VARIANT] = TTRC("Character Variants");
  700. group_names[FGRP_CAPITLS] = TTRC("Capitals");
  701. group_names[FGRP_LIGATURES] = TTRC("Ligatures");
  702. group_names[FGRP_ALTERNATES] = TTRC("Alternates");
  703. group_names[FGRP_EAL] = TTRC("East Asian Language");
  704. group_names[FGRP_EAW] = TTRC("East Asian Widths");
  705. group_names[FGRP_NUMAL] = TTRC("Numeral Alignment");
  706. group_names[FGRP_CUSTOM] = TTRC("Custom");
  707. }
  708. /*************************************************************************/
  709. /* EditorInspectorPluginFontVariation */
  710. /*************************************************************************/
  711. bool EditorInspectorPluginFontVariation::can_handle(Object *p_object) {
  712. return (Object::cast_to<FontVariation>(p_object) != nullptr) || (Object::cast_to<DynamicFontImportSettingsData>(p_object) != nullptr);
  713. }
  714. bool EditorInspectorPluginFontVariation::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  715. if (p_path == "variation_opentype") {
  716. add_property_editor(p_path, memnew(EditorPropertyOTVariation));
  717. return true;
  718. } else if (p_path == "opentype_features") {
  719. add_property_editor(p_path, memnew(EditorPropertyOTFeatures));
  720. return true;
  721. } else if (p_path == "language_support") {
  722. add_property_editor(p_path, memnew(EditorPropertyFontMetaOverride(false)));
  723. return true;
  724. } else if (p_path == "script_support") {
  725. add_property_editor(p_path, memnew(EditorPropertyFontMetaOverride(true)));
  726. return true;
  727. }
  728. return false;
  729. }
  730. /*************************************************************************/
  731. /* FontPreview */
  732. /*************************************************************************/
  733. void FontPreview::_notification(int p_what) {
  734. switch (p_what) {
  735. case NOTIFICATION_DRAW: {
  736. // Draw font name (style).
  737. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  738. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  739. Color text_color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
  740. // Draw font preview.
  741. bool prev_ok = true;
  742. if (prev_font.is_valid()) {
  743. if (prev_font->get_font_name().is_empty()) {
  744. prev_ok = false;
  745. } else {
  746. String name;
  747. if (prev_font->get_font_style_name().is_empty()) {
  748. name = prev_font->get_font_name();
  749. } else {
  750. name = vformat("%s (%s)", prev_font->get_font_name(), prev_font->get_font_style_name());
  751. }
  752. if (prev_font->is_class("FontVariation")) {
  753. // TRANSLATORS: This refers to variable font config, appended to the font name.
  754. name += " - " + TTR("Variation");
  755. }
  756. font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + 2 * EDSCALE), name, HORIZONTAL_ALIGNMENT_CENTER, get_size().x, font_size, text_color);
  757. String sample;
  758. static const String sample_base = U"12漢字ԱբΑαАбΑαאבابܐܒހށआআਆઆଆஆఆಆആආกิກິༀကႠა한글ሀᎣᐁᚁᚠᜀᜠᝀᝠកᠠᤁᥐAb😀";
  759. for (int i = 0; i < sample_base.length(); i++) {
  760. if (prev_font->has_char(sample_base[i])) {
  761. sample += sample_base[i];
  762. }
  763. }
  764. if (sample.is_empty()) {
  765. sample = prev_font->get_supported_chars().substr(0, 6);
  766. }
  767. if (sample.is_empty()) {
  768. prev_ok = false;
  769. } else {
  770. prev_font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + prev_font->get_height(25 * EDSCALE)), sample, HORIZONTAL_ALIGNMENT_CENTER, get_size().x, 25 * EDSCALE, text_color);
  771. }
  772. }
  773. }
  774. if (!prev_ok) {
  775. text_color.a *= 0.5;
  776. font->draw_string(get_canvas_item(), Point2(0, font->get_height(font_size) + 2 * EDSCALE), TTR("Unable to preview font"), HORIZONTAL_ALIGNMENT_CENTER, get_size().x, font_size, text_color);
  777. }
  778. } break;
  779. case NOTIFICATION_EXIT_TREE: {
  780. if (prev_font.is_valid()) {
  781. prev_font->disconnect_changed(callable_mp(this, &FontPreview::_preview_changed));
  782. }
  783. } break;
  784. }
  785. }
  786. void FontPreview::_bind_methods() {}
  787. Size2 FontPreview::get_minimum_size() const {
  788. return Vector2(64, 64) * EDSCALE;
  789. }
  790. void FontPreview::set_data(const Ref<Font> &p_f) {
  791. if (prev_font.is_valid()) {
  792. prev_font->disconnect_changed(callable_mp(this, &FontPreview::_preview_changed));
  793. }
  794. prev_font = p_f;
  795. if (prev_font.is_valid()) {
  796. prev_font->connect_changed(callable_mp(this, &FontPreview::_preview_changed));
  797. }
  798. queue_redraw();
  799. }
  800. void FontPreview::_preview_changed() {
  801. queue_redraw();
  802. }
  803. /*************************************************************************/
  804. /* EditorInspectorPluginFontPreview */
  805. /*************************************************************************/
  806. bool EditorInspectorPluginFontPreview::can_handle(Object *p_object) {
  807. return Object::cast_to<Font>(p_object) != nullptr;
  808. }
  809. void EditorInspectorPluginFontPreview::parse_begin(Object *p_object) {
  810. Font *fd = Object::cast_to<Font>(p_object);
  811. ERR_FAIL_NULL(fd);
  812. FontPreview *editor = memnew(FontPreview);
  813. editor->set_data(fd);
  814. add_custom_control(editor);
  815. }
  816. bool EditorInspectorPluginFontPreview::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  817. return false;
  818. }
  819. /*************************************************************************/
  820. /* EditorPropertyFontNamesArray */
  821. /*************************************************************************/
  822. void EditorPropertyFontNamesArray::_add_element() {
  823. Size2 size = get_size();
  824. menu->set_position(get_screen_position() + Size2(0, size.height * get_global_transform().get_scale().y));
  825. menu->reset_size();
  826. menu->popup();
  827. }
  828. void EditorPropertyFontNamesArray::_add_font(int p_option) {
  829. if (updating) {
  830. return;
  831. }
  832. Variant array = object->get_array();
  833. int previous_size = array.call("size");
  834. array.call("resize", previous_size + 1);
  835. array.set(previous_size, menu->get_item_text(p_option));
  836. emit_changed(get_edited_property(), array, "", false);
  837. object->set_array(array);
  838. update_property();
  839. }
  840. EditorPropertyFontNamesArray::EditorPropertyFontNamesArray() {
  841. menu = memnew(PopupMenu);
  842. menu->add_item("Sans-Serif", 0);
  843. menu->add_item("Serif", 1);
  844. menu->add_item("Monospace", 2);
  845. menu->add_item("Fantasy", 3);
  846. menu->add_item("Cursive", 4);
  847. menu->add_separator();
  848. if (OS::get_singleton()) {
  849. Vector<String> fonts = OS::get_singleton()->get_system_fonts();
  850. fonts.sort();
  851. for (int i = 0; i < fonts.size(); i++) {
  852. menu->add_item(fonts[i], i + 6);
  853. }
  854. }
  855. add_child(menu);
  856. menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditorPropertyFontNamesArray::_add_font));
  857. }
  858. /*************************************************************************/
  859. /* EditorInspectorPluginSystemFont */
  860. /*************************************************************************/
  861. bool EditorInspectorPluginSystemFont::can_handle(Object *p_object) {
  862. return Object::cast_to<SystemFont>(p_object) != nullptr;
  863. }
  864. bool EditorInspectorPluginSystemFont::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  865. if (p_path == "font_names") {
  866. EditorPropertyFontNamesArray *editor = memnew(EditorPropertyFontNamesArray);
  867. editor->setup(p_type, p_hint_text);
  868. add_property_editor(p_path, editor);
  869. return true;
  870. }
  871. return false;
  872. }
  873. /*************************************************************************/
  874. /* FontEditorPlugin */
  875. /*************************************************************************/
  876. FontEditorPlugin::FontEditorPlugin() {
  877. Ref<EditorInspectorPluginFontVariation> fc_plugin;
  878. fc_plugin.instantiate();
  879. EditorInspector::add_inspector_plugin(fc_plugin);
  880. Ref<EditorInspectorPluginSystemFont> fs_plugin;
  881. fs_plugin.instantiate();
  882. EditorInspector::add_inspector_plugin(fs_plugin);
  883. Ref<EditorInspectorPluginFontPreview> fp_plugin;
  884. fp_plugin.instantiate();
  885. EditorInspector::add_inspector_plugin(fp_plugin);
  886. }