editor_properties_vector.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /**************************************************************************/
  2. /* editor_properties_vector.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_properties_vector.h"
  31. #include "editor/editor_settings.h"
  32. #include "editor/editor_string_names.h"
  33. #include "editor/gui/editor_spin_slider.h"
  34. #include "editor/themes/editor_scale.h"
  35. #include "scene/gui/box_container.h"
  36. #include "scene/gui/texture_button.h"
  37. const String EditorPropertyVectorN::COMPONENT_LABELS[4] = { "x", "y", "z", "w" };
  38. void EditorPropertyVectorN::_set_read_only(bool p_read_only) {
  39. for (EditorSpinSlider *spin : spin_sliders) {
  40. spin->set_read_only(p_read_only);
  41. }
  42. }
  43. void EditorPropertyVectorN::_value_changed(double val, const String &p_name) {
  44. if (linked->is_pressed()) {
  45. int changed_component = -1;
  46. for (int i = 0; i < component_count; i++) {
  47. if (p_name == COMPONENT_LABELS[i]) {
  48. changed_component = i;
  49. break;
  50. }
  51. }
  52. DEV_ASSERT(changed_component >= 0);
  53. for (int i = 0; i < component_count - 1; i++) {
  54. int slider_idx = (changed_component + 1 + i) % component_count;
  55. int ratio_idx = changed_component * (component_count - 1) + i;
  56. if (ratio[ratio_idx] == 0) {
  57. continue;
  58. }
  59. spin_sliders[slider_idx]->set_value_no_signal(spin_sliders[changed_component]->get_value() * ratio[ratio_idx]);
  60. }
  61. }
  62. Variant v;
  63. Callable::CallError cerror;
  64. Variant::construct(vector_type, v, nullptr, 0, cerror);
  65. for (int i = 0; i < component_count; i++) {
  66. if (radians_as_degrees) {
  67. v.set(i, Math::deg_to_rad(spin_sliders[i]->get_value()));
  68. } else {
  69. v.set(i, spin_sliders[i]->get_value());
  70. }
  71. }
  72. emit_changed(get_edited_property(), v, linked->is_pressed() ? "" : p_name);
  73. }
  74. void EditorPropertyVectorN::update_property() {
  75. Variant val = get_edited_property_value();
  76. for (int i = 0; i < component_count; i++) {
  77. if (radians_as_degrees) {
  78. spin_sliders[i]->set_value_no_signal(Math::rad_to_deg((real_t)val.get(i)));
  79. } else {
  80. spin_sliders[i]->set_value_no_signal(val.get(i));
  81. }
  82. }
  83. if (!is_grabbed) {
  84. _update_ratio();
  85. }
  86. }
  87. void EditorPropertyVectorN::_update_ratio() {
  88. linked->set_modulate(Color(1, 1, 1, linked->is_pressed() ? 1.0 : 0.5));
  89. double *ratio_write = ratio.ptrw();
  90. for (int i = 0; i < ratio.size(); i++) {
  91. int base_slider_idx = i / (component_count - 1);
  92. int secondary_slider_idx = ((base_slider_idx + 1) + i % (component_count - 1)) % component_count;
  93. if (spin_sliders[base_slider_idx]->get_value() != 0) {
  94. ratio_write[i] = spin_sliders[secondary_slider_idx]->get_value() / spin_sliders[base_slider_idx]->get_value();
  95. }
  96. }
  97. }
  98. void EditorPropertyVectorN::_store_link(bool p_linked) {
  99. if (!get_edited_object()) {
  100. return;
  101. }
  102. const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());
  103. EditorSettings::get_singleton()->set_project_metadata("linked_properties", key, p_linked);
  104. }
  105. void EditorPropertyVectorN::_grab_changed(bool p_grab) {
  106. if (p_grab) {
  107. _update_ratio();
  108. }
  109. is_grabbed = p_grab;
  110. }
  111. void EditorPropertyVectorN::_notification(int p_what) {
  112. switch (p_what) {
  113. case NOTIFICATION_READY: {
  114. if (linked->is_visible()) {
  115. const String key = vformat("%s:%s", get_edited_object()->get_class(), get_edited_property());
  116. linked->set_pressed_no_signal(EditorSettings::get_singleton()->get_project_metadata("linked_properties", key, true));
  117. _update_ratio();
  118. }
  119. } break;
  120. case NOTIFICATION_THEME_CHANGED: {
  121. int icon_size = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  122. linked->set_texture_normal(get_editor_theme_icon(SNAME("Unlinked")));
  123. linked->set_texture_pressed(get_editor_theme_icon(SNAME("Instance")));
  124. linked->set_custom_minimum_size(Size2(icon_size + 8 * EDSCALE, 0));
  125. const Color *colors = _get_property_colors();
  126. for (int i = 0; i < component_count; i++) {
  127. spin_sliders[i]->add_theme_color_override("label_color", colors[i]);
  128. }
  129. } break;
  130. }
  131. }
  132. void EditorPropertyVectorN::setup(double p_min, double p_max, double p_step, bool p_hide_slider, bool p_link, const String &p_suffix, bool p_radians_as_degrees) {
  133. radians_as_degrees = p_radians_as_degrees;
  134. for (EditorSpinSlider *spin : spin_sliders) {
  135. spin->set_min(p_min);
  136. spin->set_max(p_max);
  137. spin->set_step(p_step);
  138. spin->set_hide_slider(p_hide_slider);
  139. spin->set_allow_greater(true);
  140. spin->set_allow_lesser(true);
  141. spin->set_suffix(p_suffix);
  142. }
  143. if (!p_link) {
  144. linked->hide();
  145. }
  146. }
  147. EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) {
  148. vector_type = p_type;
  149. switch (vector_type) {
  150. case Variant::VECTOR2:
  151. case Variant::VECTOR2I:
  152. component_count = 2;
  153. break;
  154. case Variant::VECTOR3:
  155. case Variant::VECTOR3I:
  156. component_count = 3;
  157. break;
  158. case Variant::VECTOR4:
  159. case Variant::VECTOR4I:
  160. component_count = 4;
  161. break;
  162. default: // Needed to silence a warning.
  163. ERR_PRINT("Not a Vector type.");
  164. break;
  165. }
  166. bool horizontal = p_force_wide || p_horizontal;
  167. HBoxContainer *hb = memnew(HBoxContainer);
  168. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  169. BoxContainer *bc;
  170. if (p_force_wide) {
  171. bc = memnew(HBoxContainer);
  172. hb->add_child(bc);
  173. } else if (horizontal) {
  174. bc = memnew(HBoxContainer);
  175. hb->add_child(bc);
  176. set_bottom_editor(hb);
  177. } else {
  178. bc = memnew(VBoxContainer);
  179. hb->add_child(bc);
  180. }
  181. bc->set_h_size_flags(SIZE_EXPAND_FILL);
  182. spin_sliders.resize(component_count);
  183. EditorSpinSlider **spin = spin_sliders.ptrw();
  184. for (int i = 0; i < component_count; i++) {
  185. spin[i] = memnew(EditorSpinSlider);
  186. bc->add_child(spin[i]);
  187. spin[i]->set_flat(true);
  188. spin[i]->set_label(String(COMPONENT_LABELS[i]));
  189. if (horizontal) {
  190. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  191. }
  192. spin[i]->connect(SceneStringName(value_changed), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));
  193. spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));
  194. spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));
  195. add_focusable(spin[i]);
  196. }
  197. ratio.resize(component_count * (component_count - 1));
  198. ratio.fill(1.0);
  199. linked = memnew(TextureButton);
  200. linked->set_toggle_mode(true);
  201. linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  202. linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio"));
  203. linked->connect(SceneStringName(pressed), callable_mp(this, &EditorPropertyVectorN::_update_ratio));
  204. linked->connect(SNAME("toggled"), callable_mp(this, &EditorPropertyVectorN::_store_link));
  205. hb->add_child(linked);
  206. add_child(hb);
  207. if (!horizontal) {
  208. set_label_reference(spin_sliders[0]); // Show text and buttons around this.
  209. }
  210. }
  211. EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) :
  212. EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
  213. EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) :
  214. EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
  215. EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) :
  216. EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  217. EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) :
  218. EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  219. EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) :
  220. EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  221. EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) :
  222. EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}