editor_property_name_processor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /**************************************************************************/
  2. /* editor_property_name_processor.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 "editor_property_name_processor.h"
  31. #include "editor_settings.h"
  32. EditorPropertyNameProcessor *EditorPropertyNameProcessor::singleton = nullptr;
  33. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_default_inspector_style() {
  34. const Style style = (Style)EDITOR_GET("interface/inspector/default_property_name_style").operator int();
  35. if (style == STYLE_LOCALIZED && !is_localization_available()) {
  36. return STYLE_CAPITALIZED;
  37. }
  38. return style;
  39. }
  40. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_settings_style() {
  41. const bool translate = EDITOR_GET("interface/editor/localize_settings");
  42. return translate ? STYLE_LOCALIZED : STYLE_CAPITALIZED;
  43. }
  44. EditorPropertyNameProcessor::Style EditorPropertyNameProcessor::get_tooltip_style(Style p_style) {
  45. return p_style == STYLE_LOCALIZED ? STYLE_CAPITALIZED : STYLE_LOCALIZED;
  46. }
  47. bool EditorPropertyNameProcessor::is_localization_available() {
  48. const Vector<String> forbidden = String("en").split(",");
  49. return forbidden.find(EDITOR_GET("interface/editor/editor_language")) == -1;
  50. }
  51. String EditorPropertyNameProcessor::_capitalize_name(const String &p_name) const {
  52. const Map<String, String>::Element *cached = capitalize_string_cache.find(p_name);
  53. if (cached) {
  54. return cached->value();
  55. }
  56. Vector<String> parts = p_name.split("_", false);
  57. for (int i = 0; i < parts.size(); i++) {
  58. // Articles/conjunctions/prepositions which should only be capitalized when not at beginning and end.
  59. if (i > 0 && i + 1 < parts.size() && stop_words.find(parts[i]) != -1) {
  60. continue;
  61. }
  62. const Map<String, String>::Element *remap = capitalize_string_remaps.find(parts[i]);
  63. if (remap) {
  64. parts.write[i] = remap->get();
  65. } else {
  66. parts.write[i] = parts[i].capitalize();
  67. }
  68. }
  69. const String capitalized = String(" ").join(parts);
  70. capitalize_string_cache[p_name] = capitalized;
  71. return capitalized;
  72. }
  73. String EditorPropertyNameProcessor::process_name(const String &p_name, Style p_style) const {
  74. switch (p_style) {
  75. case STYLE_RAW: {
  76. return p_name;
  77. } break;
  78. case STYLE_CAPITALIZED: {
  79. return _capitalize_name(p_name);
  80. } break;
  81. case STYLE_LOCALIZED: {
  82. return TTRGET(_capitalize_name(p_name));
  83. } break;
  84. }
  85. ERR_FAIL_V_MSG(p_name, "Unexpected property name style.");
  86. }
  87. EditorPropertyNameProcessor::EditorPropertyNameProcessor() {
  88. ERR_FAIL_COND(singleton != nullptr);
  89. singleton = this;
  90. // The following initialization is parsed in `editor/translations/extract.py` with a regex.
  91. // The map name and value definition format should be kept synced with the regex.
  92. capitalize_string_remaps["2d"] = "2D";
  93. capitalize_string_remaps["3d"] = "3D";
  94. capitalize_string_remaps["aa"] = "AA";
  95. capitalize_string_remaps["aabb"] = "AABB";
  96. capitalize_string_remaps["adb"] = "ADB";
  97. capitalize_string_remaps["ao"] = "AO";
  98. capitalize_string_remaps["api"] = "API";
  99. capitalize_string_remaps["apk"] = "APK";
  100. capitalize_string_remaps["arm64-v8a"] = "arm64-v8a";
  101. capitalize_string_remaps["armeabi-v7a"] = "armeabi-v7a";
  102. capitalize_string_remaps["arvr"] = "ARVR";
  103. capitalize_string_remaps["bg"] = "BG";
  104. capitalize_string_remaps["bidi"] = "BiDi";
  105. capitalize_string_remaps["bp"] = "BP";
  106. capitalize_string_remaps["bpc"] = "BPC";
  107. capitalize_string_remaps["bptc"] = "BPTC";
  108. capitalize_string_remaps["bvh"] = "BVH";
  109. capitalize_string_remaps["ca"] = "CA";
  110. capitalize_string_remaps["cd"] = "CD";
  111. capitalize_string_remaps["commentfocus"] = "Comment Focus";
  112. capitalize_string_remaps["cpu"] = "CPU";
  113. capitalize_string_remaps["csg"] = "CSG";
  114. capitalize_string_remaps["csgs"] = "CSGs";
  115. capitalize_string_remaps["db"] = "dB";
  116. capitalize_string_remaps["defaultfocus"] = "Default Focus";
  117. capitalize_string_remaps["defaultframe"] = "Default Frame";
  118. capitalize_string_remaps["dof"] = "DoF";
  119. capitalize_string_remaps["dpi"] = "DPI";
  120. capitalize_string_remaps["dtls"] = "DTLS";
  121. capitalize_string_remaps["erp"] = "ERP";
  122. capitalize_string_remaps["etc"] = "ETC";
  123. capitalize_string_remaps["etc2"] = "ETC2";
  124. capitalize_string_remaps["fbx"] = "FBX";
  125. capitalize_string_remaps["fft"] = "FFT";
  126. capitalize_string_remaps["fg"] = "FG";
  127. capitalize_string_remaps["filesystem"] = "FileSystem";
  128. capitalize_string_remaps["fov"] = "FOV";
  129. capitalize_string_remaps["fps"] = "FPS";
  130. capitalize_string_remaps["fs"] = "FS";
  131. capitalize_string_remaps["fsr"] = "FSR";
  132. capitalize_string_remaps["fxaa"] = "FXAA";
  133. capitalize_string_remaps["gdscript"] = "GDScript";
  134. capitalize_string_remaps["ggx"] = "GGX";
  135. capitalize_string_remaps["gi"] = "GI";
  136. capitalize_string_remaps["gl"] = "GL";
  137. capitalize_string_remaps["glb"] = "GLB";
  138. capitalize_string_remaps["gles2"] = "GLES2";
  139. capitalize_string_remaps["gles3"] = "GLES3";
  140. capitalize_string_remaps["gpu"] = "GPU";
  141. capitalize_string_remaps["gui"] = "GUI";
  142. capitalize_string_remaps["guid"] = "GUID";
  143. capitalize_string_remaps["hdr"] = "HDR";
  144. capitalize_string_remaps["hidpi"] = "hiDPI";
  145. capitalize_string_remaps["hipass"] = "High-pass";
  146. capitalize_string_remaps["hseparation"] = "H Separation";
  147. capitalize_string_remaps["hsv"] = "HSV";
  148. capitalize_string_remaps["html"] = "HTML";
  149. capitalize_string_remaps["http"] = "HTTP";
  150. capitalize_string_remaps["id"] = "ID";
  151. capitalize_string_remaps["ids"] = "IDs";
  152. capitalize_string_remaps["igd"] = "IGD";
  153. capitalize_string_remaps["ik"] = "IK";
  154. capitalize_string_remaps["image@2x"] = "Image @2x";
  155. capitalize_string_remaps["image@3x"] = "Image @3x";
  156. capitalize_string_remaps["iod"] = "IOD";
  157. capitalize_string_remaps["ios"] = "iOS";
  158. capitalize_string_remaps["ip"] = "IP";
  159. capitalize_string_remaps["ipad"] = "iPad";
  160. capitalize_string_remaps["iphone"] = "iPhone";
  161. capitalize_string_remaps["ipv6"] = "IPv6";
  162. capitalize_string_remaps["ir"] = "IR";
  163. capitalize_string_remaps["itunes"] = "iTunes";
  164. capitalize_string_remaps["jit"] = "JIT";
  165. capitalize_string_remaps["k1"] = "K1";
  166. capitalize_string_remaps["k2"] = "K2";
  167. capitalize_string_remaps["kb"] = "(KB)"; // Unit.
  168. capitalize_string_remaps["ldr"] = "LDR";
  169. capitalize_string_remaps["lod"] = "LOD";
  170. capitalize_string_remaps["lowpass"] = "Low-pass";
  171. capitalize_string_remaps["macos"] = "macOS";
  172. capitalize_string_remaps["mb"] = "(MB)"; // Unit.
  173. capitalize_string_remaps["mms"] = "MMS";
  174. capitalize_string_remaps["ms"] = "(ms)"; // Unit
  175. capitalize_string_remaps["msaa"] = "MSAA";
  176. capitalize_string_remaps["msdf"] = "MSDF";
  177. // Not used for now as AudioEffectReverb has a `msec` property.
  178. //capitalize_string_remaps["msec"] = "(msec)"; // Unit.
  179. capitalize_string_remaps["navmesh"] = "NavMesh";
  180. capitalize_string_remaps["nfc"] = "NFC";
  181. capitalize_string_remaps["normalmap"] = "Normal Map";
  182. capitalize_string_remaps["ofs"] = "Offset";
  183. capitalize_string_remaps["ok"] = "OK";
  184. capitalize_string_remaps["opengl"] = "OpenGL";
  185. capitalize_string_remaps["opentype"] = "OpenType";
  186. capitalize_string_remaps["openxr"] = "OpenXR";
  187. capitalize_string_remaps["osslsigncode"] = "osslsigncode";
  188. capitalize_string_remaps["pck"] = "PCK";
  189. capitalize_string_remaps["png"] = "PNG";
  190. capitalize_string_remaps["po2"] = "(Power of 2)"; // Unit.
  191. capitalize_string_remaps["pvrtc"] = "PVRTC";
  192. capitalize_string_remaps["pvs"] = "PVS";
  193. capitalize_string_remaps["rcedit"] = "rcedit";
  194. capitalize_string_remaps["rcodesign"] = "rcodesign";
  195. capitalize_string_remaps["rgb"] = "RGB";
  196. capitalize_string_remaps["rid"] = "RID";
  197. capitalize_string_remaps["rmb"] = "RMB";
  198. capitalize_string_remaps["rpc"] = "RPC";
  199. capitalize_string_remaps["s3tc"] = "S3TC";
  200. capitalize_string_remaps["sdf"] = "SDF";
  201. capitalize_string_remaps["sdfgi"] = "SDFGI";
  202. capitalize_string_remaps["sdk"] = "SDK";
  203. capitalize_string_remaps["sec"] = "(sec)"; // Unit.
  204. capitalize_string_remaps["selectedframe"] = "Selected Frame";
  205. capitalize_string_remaps["signtool"] = "signtool";
  206. capitalize_string_remaps["sms"] = "SMS";
  207. capitalize_string_remaps["srgb"] = "sRGB";
  208. capitalize_string_remaps["ssao"] = "SSAO";
  209. capitalize_string_remaps["ssh"] = "SSH";
  210. capitalize_string_remaps["ssil"] = "SSIL";
  211. capitalize_string_remaps["ssl"] = "SSL";
  212. capitalize_string_remaps["stderr"] = "stderr";
  213. capitalize_string_remaps["stdout"] = "stdout";
  214. capitalize_string_remaps["sv"] = "SV";
  215. capitalize_string_remaps["svg"] = "SVG";
  216. capitalize_string_remaps["tcp"] = "TCP";
  217. capitalize_string_remaps["tls"] = "TLS";
  218. capitalize_string_remaps["ui"] = "UI";
  219. capitalize_string_remaps["uri"] = "URI";
  220. capitalize_string_remaps["url"] = "URL";
  221. capitalize_string_remaps["urls"] = "URLs";
  222. capitalize_string_remaps["us"] = String::utf8("(µs)"); // Unit.
  223. capitalize_string_remaps["usb"] = "USB";
  224. capitalize_string_remaps["usec"] = String::utf8("(µsec)"); // Unit.
  225. capitalize_string_remaps["uuid"] = "UUID";
  226. capitalize_string_remaps["uv"] = "UV";
  227. capitalize_string_remaps["uv1"] = "UV1";
  228. capitalize_string_remaps["uv2"] = "UV2";
  229. capitalize_string_remaps["uwp"] = "UWP";
  230. capitalize_string_remaps["vadjust"] = "V Adjust";
  231. capitalize_string_remaps["valign"] = "V Align";
  232. capitalize_string_remaps["vector2"] = "Vector2";
  233. capitalize_string_remaps["vpn"] = "VPN";
  234. capitalize_string_remaps["vram"] = "VRAM";
  235. capitalize_string_remaps["vseparation"] = "V Separation";
  236. capitalize_string_remaps["vsync"] = "V-Sync";
  237. capitalize_string_remaps["wap"] = "WAP";
  238. capitalize_string_remaps["webp"] = "WebP";
  239. capitalize_string_remaps["webrtc"] = "WebRTC";
  240. capitalize_string_remaps["websocket"] = "WebSocket";
  241. capitalize_string_remaps["wine"] = "wine";
  242. capitalize_string_remaps["wifi"] = "Wi-Fi";
  243. capitalize_string_remaps["x86"] = "x86";
  244. capitalize_string_remaps["xr"] = "XR";
  245. capitalize_string_remaps["xray"] = "X-Ray";
  246. capitalize_string_remaps["xy"] = "XY";
  247. capitalize_string_remaps["xz"] = "XZ";
  248. capitalize_string_remaps["yz"] = "YZ";
  249. // Articles, conjunctions, prepositions.
  250. // The following initialization is parsed in `editor/translations/extract.py` with a regex.
  251. // The word definition format should be kept synced with the regex.
  252. stop_words.push_back("a");
  253. stop_words.push_back("an");
  254. stop_words.push_back("and");
  255. stop_words.push_back("as");
  256. stop_words.push_back("at");
  257. stop_words.push_back("by");
  258. stop_words.push_back("for");
  259. stop_words.push_back("in");
  260. stop_words.push_back("not");
  261. stop_words.push_back("of");
  262. stop_words.push_back("on");
  263. stop_words.push_back("or");
  264. stop_words.push_back("over");
  265. stop_words.push_back("per");
  266. stop_words.push_back("the");
  267. stop_words.push_back("then");
  268. stop_words.push_back("to");
  269. }
  270. EditorPropertyNameProcessor::~EditorPropertyNameProcessor() {
  271. singleton = nullptr;
  272. }