editor_properties_vector.cpp 9.5 KB

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