editor_properties_vector.cpp 9.8 KB

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