editor_properties_vector.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. } else {
  94. ratio_write[i] = 0;
  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(EditorSettings::get_singleton()->get_project_metadata("linked_properties", key, true));
  117. }
  118. } break;
  119. case NOTIFICATION_THEME_CHANGED: {
  120. linked->set_texture_normal(get_editor_theme_icon(SNAME("Unlinked")));
  121. linked->set_texture_pressed(get_editor_theme_icon(SNAME("Instance")));
  122. const Color *colors = _get_property_colors();
  123. for (int i = 0; i < component_count; i++) {
  124. spin_sliders[i]->add_theme_color_override("label_color", colors[i]);
  125. }
  126. } break;
  127. }
  128. }
  129. 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) {
  130. radians_as_degrees = p_radians_as_degrees;
  131. for (EditorSpinSlider *spin : spin_sliders) {
  132. spin->set_min(p_min);
  133. spin->set_max(p_max);
  134. spin->set_step(p_step);
  135. spin->set_hide_slider(p_hide_slider);
  136. spin->set_allow_greater(true);
  137. spin->set_allow_lesser(true);
  138. spin->set_suffix(p_suffix);
  139. }
  140. if (!p_link) {
  141. linked->hide();
  142. }
  143. }
  144. EditorPropertyVectorN::EditorPropertyVectorN(Variant::Type p_type, bool p_force_wide, bool p_horizontal) {
  145. vector_type = p_type;
  146. switch (vector_type) {
  147. case Variant::VECTOR2:
  148. case Variant::VECTOR2I:
  149. component_count = 2;
  150. break;
  151. case Variant::VECTOR3:
  152. case Variant::VECTOR3I:
  153. component_count = 3;
  154. break;
  155. case Variant::VECTOR4:
  156. case Variant::VECTOR4I:
  157. component_count = 4;
  158. break;
  159. default: // Needed to silence a warning.
  160. ERR_PRINT("Not a Vector type.");
  161. break;
  162. }
  163. bool horizontal = p_force_wide || p_horizontal;
  164. HBoxContainer *hb = memnew(HBoxContainer);
  165. hb->set_h_size_flags(SIZE_EXPAND_FILL);
  166. BoxContainer *bc;
  167. if (p_force_wide) {
  168. bc = memnew(HBoxContainer);
  169. hb->add_child(bc);
  170. } else if (horizontal) {
  171. bc = memnew(HBoxContainer);
  172. hb->add_child(bc);
  173. set_bottom_editor(hb);
  174. } else {
  175. bc = memnew(VBoxContainer);
  176. hb->add_child(bc);
  177. }
  178. bc->set_h_size_flags(SIZE_EXPAND_FILL);
  179. spin_sliders.resize(component_count);
  180. EditorSpinSlider **spin = spin_sliders.ptrw();
  181. for (int i = 0; i < component_count; i++) {
  182. spin[i] = memnew(EditorSpinSlider);
  183. bc->add_child(spin[i]);
  184. spin[i]->set_flat(true);
  185. spin[i]->set_label(String(COMPONENT_LABELS[i]));
  186. if (horizontal) {
  187. spin[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  188. }
  189. spin[i]->connect(SNAME("value_changed"), callable_mp(this, &EditorPropertyVectorN::_value_changed).bind(String(COMPONENT_LABELS[i])));
  190. spin[i]->connect(SNAME("grabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(true));
  191. spin[i]->connect(SNAME("ungrabbed"), callable_mp(this, &EditorPropertyVectorN::_grab_changed).bind(false));
  192. add_focusable(spin[i]);
  193. }
  194. ratio.resize(component_count * (component_count - 1));
  195. ratio.fill(1.0);
  196. linked = memnew(TextureButton);
  197. linked->set_toggle_mode(true);
  198. linked->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  199. linked->set_tooltip_text(TTR("Lock/Unlock Component Ratio"));
  200. linked->connect(SNAME("pressed"), callable_mp(this, &EditorPropertyVectorN::_update_ratio));
  201. linked->connect(SNAME("toggled"), callable_mp(this, &EditorPropertyVectorN::_store_link));
  202. hb->add_child(linked);
  203. add_child(hb);
  204. if (!horizontal) {
  205. set_label_reference(spin_sliders[0]); // Show text and buttons around this.
  206. }
  207. }
  208. EditorPropertyVector2::EditorPropertyVector2(bool p_force_wide) :
  209. EditorPropertyVectorN(Variant::VECTOR2, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
  210. EditorPropertyVector2i::EditorPropertyVector2i(bool p_force_wide) :
  211. EditorPropertyVectorN(Variant::VECTOR2I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector2_editing")) {}
  212. EditorPropertyVector3::EditorPropertyVector3(bool p_force_wide) :
  213. EditorPropertyVectorN(Variant::VECTOR3, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  214. EditorPropertyVector3i::EditorPropertyVector3i(bool p_force_wide) :
  215. EditorPropertyVectorN(Variant::VECTOR3I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  216. EditorPropertyVector4::EditorPropertyVector4(bool p_force_wide) :
  217. EditorPropertyVectorN(Variant::VECTOR4, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}
  218. EditorPropertyVector4i::EditorPropertyVector4i(bool p_force_wide) :
  219. EditorPropertyVectorN(Variant::VECTOR4I, p_force_wide, EDITOR_GET("interface/inspector/horizontal_vector_types_editing")) {}