label_settings.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /**************************************************************************/
  2. /* label_settings.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 "label_settings.h"
  31. void LabelSettings::_font_changed() {
  32. emit_changed();
  33. }
  34. void LabelSettings::_bind_methods() {
  35. ClassDB::bind_method(D_METHOD("set_line_spacing", "spacing"), &LabelSettings::set_line_spacing);
  36. ClassDB::bind_method(D_METHOD("get_line_spacing"), &LabelSettings::get_line_spacing);
  37. ClassDB::bind_method(D_METHOD("set_paragraph_spacing", "spacing"), &LabelSettings::set_paragraph_spacing);
  38. ClassDB::bind_method(D_METHOD("get_paragraph_spacing"), &LabelSettings::get_paragraph_spacing);
  39. ClassDB::bind_method(D_METHOD("set_font", "font"), &LabelSettings::set_font);
  40. ClassDB::bind_method(D_METHOD("get_font"), &LabelSettings::get_font);
  41. ClassDB::bind_method(D_METHOD("set_font_size", "size"), &LabelSettings::set_font_size);
  42. ClassDB::bind_method(D_METHOD("get_font_size"), &LabelSettings::get_font_size);
  43. ClassDB::bind_method(D_METHOD("set_font_color", "color"), &LabelSettings::set_font_color);
  44. ClassDB::bind_method(D_METHOD("get_font_color"), &LabelSettings::get_font_color);
  45. ClassDB::bind_method(D_METHOD("set_outline_size", "size"), &LabelSettings::set_outline_size);
  46. ClassDB::bind_method(D_METHOD("get_outline_size"), &LabelSettings::get_outline_size);
  47. ClassDB::bind_method(D_METHOD("set_outline_color", "color"), &LabelSettings::set_outline_color);
  48. ClassDB::bind_method(D_METHOD("get_outline_color"), &LabelSettings::get_outline_color);
  49. ClassDB::bind_method(D_METHOD("set_shadow_size", "size"), &LabelSettings::set_shadow_size);
  50. ClassDB::bind_method(D_METHOD("get_shadow_size"), &LabelSettings::get_shadow_size);
  51. ClassDB::bind_method(D_METHOD("set_shadow_color", "color"), &LabelSettings::set_shadow_color);
  52. ClassDB::bind_method(D_METHOD("get_shadow_color"), &LabelSettings::get_shadow_color);
  53. ClassDB::bind_method(D_METHOD("set_shadow_offset", "offset"), &LabelSettings::set_shadow_offset);
  54. ClassDB::bind_method(D_METHOD("get_shadow_offset"), &LabelSettings::get_shadow_offset);
  55. // Stacked outlines
  56. ClassDB::bind_method(D_METHOD("get_stacked_outline_count"), &LabelSettings::get_stacked_outline_count);
  57. ClassDB::bind_method(D_METHOD("set_stacked_outline_count", "count"), &LabelSettings::set_stacked_outline_count);
  58. ClassDB::bind_method(D_METHOD("add_stacked_outline", "index"), &LabelSettings::add_stacked_outline, DEFVAL(-1));
  59. ClassDB::bind_method(D_METHOD("move_stacked_outline", "from_index", "to_position"), &LabelSettings::move_stacked_outline);
  60. ClassDB::bind_method(D_METHOD("remove_stacked_outline", "index"), &LabelSettings::remove_stacked_outline);
  61. ClassDB::bind_method(D_METHOD("set_stacked_outline_size", "index", "size"), &LabelSettings::set_stacked_outline_size);
  62. ClassDB::bind_method(D_METHOD("get_stacked_outline_size", "index"), &LabelSettings::get_stacked_outline_size);
  63. ClassDB::bind_method(D_METHOD("set_stacked_outline_color", "index", "color"), &LabelSettings::set_stacked_outline_color);
  64. ClassDB::bind_method(D_METHOD("get_stacked_outline_color", "index"), &LabelSettings::get_stacked_outline_color);
  65. // Stacked shadows
  66. ClassDB::bind_method(D_METHOD("get_stacked_shadow_count"), &LabelSettings::get_stacked_shadow_count);
  67. ClassDB::bind_method(D_METHOD("set_stacked_shadow_count", "count"), &LabelSettings::set_stacked_shadow_count);
  68. ClassDB::bind_method(D_METHOD("add_stacked_shadow", "index"), &LabelSettings::add_stacked_shadow, DEFVAL(-1));
  69. ClassDB::bind_method(D_METHOD("move_stacked_shadow", "from_index", "to_position"), &LabelSettings::move_stacked_shadow);
  70. ClassDB::bind_method(D_METHOD("remove_stacked_shadow", "index"), &LabelSettings::remove_stacked_shadow);
  71. ClassDB::bind_method(D_METHOD("set_stacked_shadow_offset", "index", "offset"), &LabelSettings::set_stacked_shadow_offset);
  72. ClassDB::bind_method(D_METHOD("get_stacked_shadow_offset", "index"), &LabelSettings::get_stacked_shadow_offset);
  73. ClassDB::bind_method(D_METHOD("set_stacked_shadow_color", "index", "color"), &LabelSettings::set_stacked_shadow_color);
  74. ClassDB::bind_method(D_METHOD("get_stacked_shadow_color", "index"), &LabelSettings::get_stacked_shadow_color);
  75. ClassDB::bind_method(D_METHOD("set_stacked_shadow_outline_size", "index", "size"), &LabelSettings::set_stacked_shadow_outline_size);
  76. ClassDB::bind_method(D_METHOD("get_stacked_shadow_outline_size", "index"), &LabelSettings::get_stacked_shadow_outline_size);
  77. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "line_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_line_spacing", "get_line_spacing");
  78. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "paragraph_spacing", PROPERTY_HINT_NONE, "suffix:px"), "set_paragraph_spacing", "get_paragraph_spacing");
  79. ADD_GROUP("Font", "font_");
  80. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_font", "get_font");
  81. ADD_PROPERTY(PropertyInfo(Variant::INT, "font_size", PROPERTY_HINT_RANGE, "1,1024,1,or_greater,suffix:px"), "set_font_size", "get_font_size");
  82. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "font_color"), "set_font_color", "get_font_color");
  83. ADD_GROUP("Outline", "outline_");
  84. ADD_PROPERTY(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_RANGE, "0,127,1,or_greater,suffix:px"), "set_outline_size", "get_outline_size");
  85. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "outline_color"), "set_outline_color", "get_outline_color");
  86. ADD_GROUP("Shadow", "shadow_");
  87. ADD_PROPERTY(PropertyInfo(Variant::INT, "shadow_size", PROPERTY_HINT_RANGE, "0,127,1,or_greater,suffix:px"), "set_shadow_size", "get_shadow_size");
  88. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "shadow_color"), "set_shadow_color", "get_shadow_color");
  89. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "shadow_offset", PROPERTY_HINT_NONE, "suffix:px"), "set_shadow_offset", "get_shadow_offset");
  90. ADD_GROUP("Stacked Effects", "");
  91. ADD_ARRAY_COUNT("Stacked Outlines", "stacked_outline_count", "set_stacked_outline_count", "get_stacked_outline_count", "stacked_outline_");
  92. ADD_ARRAY_COUNT("Stacked Shadows", "stacked_shadow_count", "set_stacked_shadow_count", "get_stacked_shadow_count", "stacked_shadow_");
  93. constexpr StackedOutlineData stacked_outline_defaults;
  94. stacked_outline_base_property_helper.set_prefix("stacked_outline_");
  95. stacked_outline_base_property_helper.set_array_length_getter(&LabelSettings::get_stacked_outline_count);
  96. stacked_outline_base_property_helper.register_property(PropertyInfo(Variant::INT, "size", PROPERTY_HINT_NONE, "0,127,1,or_greater,suffix:px"), stacked_outline_defaults.size, &LabelSettings::set_stacked_outline_size, &LabelSettings::get_stacked_outline_size);
  97. stacked_outline_base_property_helper.register_property(PropertyInfo(Variant::COLOR, "color"), stacked_outline_defaults.color, &LabelSettings::set_stacked_outline_color, &LabelSettings::get_stacked_outline_color);
  98. PropertyListHelper::register_base_helper(&stacked_outline_base_property_helper);
  99. constexpr StackedShadowData stacked_shadow_defaults;
  100. stacked_shadow_base_property_helper.set_prefix("stacked_shadow_");
  101. stacked_shadow_base_property_helper.set_array_length_getter(&LabelSettings::get_stacked_shadow_count);
  102. stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::VECTOR2, "offset", PROPERTY_HINT_NONE, "suffix:px"), stacked_shadow_defaults.offset, &LabelSettings::set_stacked_shadow_offset, &LabelSettings::get_stacked_shadow_offset);
  103. stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::COLOR, "color"), stacked_shadow_defaults.color, &LabelSettings::set_stacked_shadow_color, &LabelSettings::get_stacked_shadow_color);
  104. stacked_shadow_base_property_helper.register_property(PropertyInfo(Variant::INT, "outline_size", PROPERTY_HINT_NONE, "0,127,1,or_greater,suffix:px"), stacked_shadow_defaults.outline_size, &LabelSettings::set_stacked_shadow_outline_size, &LabelSettings::get_stacked_shadow_outline_size);
  105. PropertyListHelper::register_base_helper(&stacked_shadow_base_property_helper);
  106. }
  107. void LabelSettings::set_line_spacing(real_t p_spacing) {
  108. if (line_spacing != p_spacing) {
  109. line_spacing = p_spacing;
  110. emit_changed();
  111. }
  112. }
  113. real_t LabelSettings::get_line_spacing() const {
  114. return line_spacing;
  115. }
  116. void LabelSettings::set_paragraph_spacing(real_t p_spacing) {
  117. if (paragraph_spacing != p_spacing) {
  118. paragraph_spacing = p_spacing;
  119. emit_changed();
  120. }
  121. }
  122. real_t LabelSettings::get_paragraph_spacing() const {
  123. return paragraph_spacing;
  124. }
  125. void LabelSettings::set_font(const Ref<Font> &p_font) {
  126. if (font != p_font) {
  127. if (font.is_valid()) {
  128. font->disconnect_changed(callable_mp(this, &LabelSettings::_font_changed));
  129. }
  130. font = p_font;
  131. if (font.is_valid()) {
  132. font->connect_changed(callable_mp(this, &LabelSettings::_font_changed), CONNECT_REFERENCE_COUNTED);
  133. }
  134. emit_changed();
  135. }
  136. }
  137. Ref<Font> LabelSettings::get_font() const {
  138. return font;
  139. }
  140. void LabelSettings::set_font_size(int p_size) {
  141. if (font_size != p_size) {
  142. font_size = p_size;
  143. emit_changed();
  144. }
  145. }
  146. int LabelSettings::get_font_size() const {
  147. return font_size;
  148. }
  149. void LabelSettings::set_font_color(const Color &p_color) {
  150. if (font_color != p_color) {
  151. font_color = p_color;
  152. emit_changed();
  153. }
  154. }
  155. Color LabelSettings::get_font_color() const {
  156. return font_color;
  157. }
  158. void LabelSettings::set_outline_size(int p_size) {
  159. if (outline_size != p_size) {
  160. outline_size = p_size;
  161. emit_changed();
  162. }
  163. }
  164. int LabelSettings::get_outline_size() const {
  165. return outline_size;
  166. }
  167. void LabelSettings::set_outline_color(const Color &p_color) {
  168. if (outline_color != p_color) {
  169. outline_color = p_color;
  170. emit_changed();
  171. }
  172. }
  173. Color LabelSettings::get_outline_color() const {
  174. return outline_color;
  175. }
  176. void LabelSettings::set_shadow_size(int p_size) {
  177. if (shadow_size != p_size) {
  178. shadow_size = p_size;
  179. emit_changed();
  180. }
  181. }
  182. int LabelSettings::get_shadow_size() const {
  183. return shadow_size;
  184. }
  185. void LabelSettings::set_shadow_color(const Color &p_color) {
  186. if (shadow_color != p_color) {
  187. shadow_color = p_color;
  188. emit_changed();
  189. }
  190. }
  191. Color LabelSettings::get_shadow_color() const {
  192. return shadow_color;
  193. }
  194. void LabelSettings::set_shadow_offset(const Vector2 &p_offset) {
  195. if (shadow_offset != p_offset) {
  196. shadow_offset = p_offset;
  197. emit_changed();
  198. }
  199. }
  200. Vector2 LabelSettings::get_shadow_offset() const {
  201. return shadow_offset;
  202. }
  203. Vector<LabelSettings::StackedOutlineData> LabelSettings::get_stacked_outline_data() const {
  204. return stacked_outline_data;
  205. }
  206. int LabelSettings::get_stacked_outline_count() const {
  207. return stacked_outline_data.size();
  208. }
  209. void LabelSettings::set_stacked_outline_count(int p_count) {
  210. ERR_FAIL_COND(p_count < 0);
  211. if (stacked_outline_data.size() != p_count) {
  212. stacked_outline_data.resize(p_count);
  213. notify_property_list_changed();
  214. emit_changed();
  215. }
  216. }
  217. void LabelSettings::add_stacked_outline(int p_index) {
  218. if (p_index < 0) {
  219. p_index = stacked_outline_data.size();
  220. }
  221. ERR_FAIL_INDEX(p_index, stacked_outline_data.size() + 1);
  222. stacked_outline_data.insert(p_index, StackedOutlineData());
  223. notify_property_list_changed();
  224. emit_changed();
  225. }
  226. void LabelSettings::move_stacked_outline(int p_from_index, int p_to_position) {
  227. ERR_FAIL_INDEX(p_from_index, stacked_outline_data.size());
  228. ERR_FAIL_INDEX(p_to_position, stacked_outline_data.size() + 1);
  229. stacked_outline_data.insert(p_to_position, stacked_outline_data[p_from_index]);
  230. stacked_outline_data.remove_at(p_to_position < p_from_index ? p_from_index + 1 : p_from_index);
  231. notify_property_list_changed();
  232. emit_changed();
  233. }
  234. void LabelSettings::remove_stacked_outline(int p_index) {
  235. ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
  236. stacked_outline_data.remove_at(p_index);
  237. notify_property_list_changed();
  238. emit_changed();
  239. }
  240. void LabelSettings::set_stacked_outline_size(int p_index, int p_size) {
  241. ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
  242. if (stacked_outline_data[p_index].size != p_size) {
  243. stacked_outline_data.write[p_index].size = p_size;
  244. emit_changed();
  245. }
  246. }
  247. int LabelSettings::get_stacked_outline_size(int p_index) const {
  248. ERR_FAIL_INDEX_V(p_index, stacked_outline_data.size(), 0);
  249. return stacked_outline_data[p_index].size;
  250. }
  251. void LabelSettings::set_stacked_outline_color(int p_index, const Color &p_color) {
  252. ERR_FAIL_INDEX(p_index, stacked_outline_data.size());
  253. if (stacked_outline_data[p_index].color != p_color) {
  254. stacked_outline_data.write[p_index].color = p_color;
  255. emit_changed();
  256. }
  257. }
  258. Color LabelSettings::get_stacked_outline_color(int p_index) const {
  259. ERR_FAIL_INDEX_V(p_index, stacked_outline_data.size(), Color());
  260. return stacked_outline_data[p_index].color;
  261. }
  262. Vector<LabelSettings::StackedShadowData> LabelSettings::get_stacked_shadow_data() const {
  263. return stacked_shadow_data;
  264. }
  265. int LabelSettings::get_stacked_shadow_count() const {
  266. return stacked_shadow_data.size();
  267. }
  268. void LabelSettings::set_stacked_shadow_count(int p_count) {
  269. ERR_FAIL_COND(p_count < 0);
  270. if (stacked_shadow_data.size() != p_count) {
  271. stacked_shadow_data.resize(p_count);
  272. notify_property_list_changed();
  273. emit_changed();
  274. }
  275. }
  276. void LabelSettings::add_stacked_shadow(int p_index) {
  277. if (p_index < 0) {
  278. p_index = stacked_shadow_data.size();
  279. }
  280. ERR_FAIL_INDEX(p_index, stacked_shadow_data.size() + 1);
  281. stacked_shadow_data.insert(p_index, StackedShadowData());
  282. notify_property_list_changed();
  283. emit_changed();
  284. }
  285. void LabelSettings::move_stacked_shadow(int p_from_index, int p_to_position) {
  286. ERR_FAIL_INDEX(p_from_index, stacked_shadow_data.size());
  287. ERR_FAIL_INDEX(p_to_position, stacked_shadow_data.size() + 1);
  288. stacked_shadow_data.insert(p_to_position, stacked_shadow_data[p_from_index]);
  289. stacked_shadow_data.remove_at(p_to_position < p_from_index ? p_from_index + 1 : p_from_index);
  290. notify_property_list_changed();
  291. emit_changed();
  292. }
  293. void LabelSettings::remove_stacked_shadow(int p_index) {
  294. ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
  295. stacked_shadow_data.remove_at(p_index);
  296. notify_property_list_changed();
  297. emit_changed();
  298. }
  299. void LabelSettings::set_stacked_shadow_offset(int p_index, const Vector2 &p_offset) {
  300. ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
  301. if (stacked_shadow_data[p_index].offset != p_offset) {
  302. stacked_shadow_data.write[p_index].offset = p_offset;
  303. emit_changed();
  304. }
  305. }
  306. Vector2 LabelSettings::get_stacked_shadow_offset(int p_index) const {
  307. ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), Vector2());
  308. return stacked_shadow_data[p_index].offset;
  309. }
  310. void LabelSettings::set_stacked_shadow_color(int p_index, const Color &p_color) {
  311. ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
  312. if (stacked_shadow_data[p_index].color != p_color) {
  313. stacked_shadow_data.write[p_index].color = p_color;
  314. emit_changed();
  315. }
  316. }
  317. Color LabelSettings::get_stacked_shadow_color(int p_index) const {
  318. ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), Color());
  319. return stacked_shadow_data[p_index].color;
  320. }
  321. void LabelSettings::set_stacked_shadow_outline_size(int p_index, int p_size) {
  322. ERR_FAIL_INDEX(p_index, stacked_shadow_data.size());
  323. if (stacked_shadow_data[p_index].outline_size != p_size) {
  324. stacked_shadow_data.write[p_index].outline_size = p_size;
  325. emit_changed();
  326. }
  327. }
  328. int LabelSettings::get_stacked_shadow_outline_size(int p_index) const {
  329. ERR_FAIL_INDEX_V(p_index, stacked_shadow_data.size(), 0);
  330. return stacked_shadow_data[p_index].outline_size;
  331. }