particle_process_material_editor_plugin.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /**************************************************************************/
  2. /* particle_process_material_editor_plugin.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 "particle_process_material_editor_plugin.h"
  31. #include "editor/editor_property_name_processor.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/editor_string_names.h"
  34. #include "editor/gui/editor_spin_slider.h"
  35. #include "editor/themes/editor_theme_manager.h"
  36. #include "scene/gui/box_container.h"
  37. #include "scene/gui/button.h"
  38. #include "scene/gui/label.h"
  39. #include "scene/resources/particle_process_material.h"
  40. void ParticleProcessMaterialMinMaxPropertyEditor::_update_sizing() {
  41. edit_size = range_edit_widget->get_size();
  42. margin = Vector2(range_slider_left_icon->get_width(), (edit_size.y - range_slider_left_icon->get_height()) * 0.5);
  43. usable_area = edit_size - margin * 2;
  44. }
  45. void ParticleProcessMaterialMinMaxPropertyEditor::_range_edit_draw() {
  46. ERR_FAIL_COND(range_slider_left_icon.is_null());
  47. ERR_FAIL_COND(range_slider_right_icon.is_null());
  48. _update_sizing();
  49. bool widget_active = mouse_inside || drag != Drag::NONE;
  50. // FIXME: Need to offset by 1 due to some outline bug.
  51. range_edit_widget->draw_rect(Rect2(margin + Vector2(1, 1), usable_area - Vector2(1, 1)), widget_active ? background_color.lerp(normal_color, 0.3) : background_color, false, 1.0);
  52. Color draw_color;
  53. if (widget_active) {
  54. float icon_offset = _get_left_offset() - range_slider_left_icon->get_width() - 1;
  55. if (drag == Drag::LEFT || drag == Drag::SCALE) {
  56. draw_color = drag_color;
  57. } else if (hover == Hover::LEFT) {
  58. draw_color = hovered_color;
  59. } else {
  60. draw_color = normal_color;
  61. }
  62. range_edit_widget->draw_texture(range_slider_left_icon, Vector2(icon_offset, margin.y), draw_color);
  63. icon_offset = _get_right_offset();
  64. if (drag == Drag::RIGHT || drag == Drag::SCALE) {
  65. draw_color = drag_color;
  66. } else if (hover == Hover::RIGHT) {
  67. draw_color = hovered_color;
  68. } else {
  69. draw_color = normal_color;
  70. }
  71. range_edit_widget->draw_texture(range_slider_right_icon, Vector2(icon_offset, margin.y), draw_color);
  72. }
  73. if (drag == Drag::MIDDLE || drag == Drag::SCALE) {
  74. draw_color = drag_color;
  75. } else if (hover == Hover::MIDDLE) {
  76. draw_color = hovered_color;
  77. } else {
  78. draw_color = normal_color;
  79. }
  80. range_edit_widget->draw_rect(_get_middle_rect(), draw_color);
  81. Rect2 midpoint_rect(Vector2(margin.x + usable_area.x * (_get_min_ratio() + _get_max_ratio()) * 0.5 - 1, margin.y + 2),
  82. Vector2(2, usable_area.y - 4));
  83. range_edit_widget->draw_rect(midpoint_rect, midpoint_color);
  84. }
  85. void ParticleProcessMaterialMinMaxPropertyEditor::_range_edit_gui_input(const Ref<InputEvent> &p_event) {
  86. Ref<InputEventMouseButton> mb = p_event;
  87. Ref<InputEventMouseMotion> mm = p_event;
  88. // Prevent unnecessary computations.
  89. if ((mb.is_null() || mb->get_button_index() != MouseButton::LEFT) && (mm.is_null())) {
  90. return;
  91. }
  92. ERR_FAIL_COND(range_slider_left_icon.is_null());
  93. ERR_FAIL_COND(range_slider_right_icon.is_null());
  94. _update_sizing();
  95. if (mb.is_valid()) {
  96. const Drag prev_drag = drag;
  97. if (mb->is_pressed()) {
  98. if (mb->is_shift_pressed()) {
  99. drag = Drag::SCALE;
  100. drag_from_value = (max_range->get_value() - min_range->get_value()) * 0.5;
  101. drag_midpoint = (max_range->get_value() + min_range->get_value()) * 0.5;
  102. } else if (hover == Hover::LEFT) {
  103. drag = Drag::LEFT;
  104. drag_from_value = min_range->get_value();
  105. } else if (hover == Hover::RIGHT) {
  106. drag = Drag::RIGHT;
  107. drag_from_value = max_range->get_value();
  108. } else {
  109. drag = Drag::MIDDLE;
  110. drag_from_value = min_range->get_value();
  111. }
  112. drag_origin = mb->get_position().x;
  113. } else {
  114. drag = Drag::NONE;
  115. }
  116. if (drag != prev_drag) {
  117. range_edit_widget->queue_redraw();
  118. }
  119. }
  120. float property_length = property_range.y - property_range.x;
  121. if (mm.is_valid()) {
  122. switch (drag) {
  123. case Drag::NONE: {
  124. const Hover prev_hover = hover;
  125. float left_icon_offset = _get_left_offset() - range_slider_left_icon->get_width() - 1;
  126. if (Rect2(Vector2(left_icon_offset, 0), range_slider_left_icon->get_size()).has_point(mm->get_position())) {
  127. hover = Hover::LEFT;
  128. } else if (Rect2(Vector2(_get_right_offset(), 0), range_slider_right_icon->get_size()).has_point(mm->get_position())) {
  129. hover = Hover::RIGHT;
  130. } else if (_get_middle_rect().has_point(mm->get_position())) {
  131. hover = Hover::MIDDLE;
  132. } else {
  133. hover = Hover::NONE;
  134. }
  135. if (hover != prev_hover) {
  136. range_edit_widget->queue_redraw();
  137. }
  138. } break;
  139. case Drag::LEFT:
  140. case Drag::RIGHT: {
  141. float new_value = drag_from_value + (mm->get_position().x - drag_origin) / usable_area.x * property_length;
  142. if (drag == Drag::LEFT) {
  143. new_value = MIN(new_value, max_range->get_value());
  144. _set_clamped_values(new_value, max_range->get_value());
  145. } else {
  146. new_value = MAX(new_value, min_range->get_value());
  147. _set_clamped_values(min_range->get_value(), new_value);
  148. }
  149. } break;
  150. case Drag::MIDDLE: {
  151. float delta = (mm->get_position().x - drag_origin) / usable_area.x * property_length;
  152. float diff = max_range->get_value() - min_range->get_value();
  153. delta = CLAMP(drag_from_value + delta, property_range.x, property_range.y - diff) - drag_from_value;
  154. _set_clamped_values(drag_from_value + delta, drag_from_value + delta + diff);
  155. } break;
  156. case Drag::SCALE: {
  157. float delta = (mm->get_position().x - drag_origin) / usable_area.x * property_length + drag_from_value;
  158. _set_clamped_values(MIN(drag_midpoint, drag_midpoint - delta), MAX(drag_midpoint, drag_midpoint + delta));
  159. } break;
  160. }
  161. }
  162. }
  163. void ParticleProcessMaterialMinMaxPropertyEditor::_set_mouse_inside(bool p_inside) {
  164. mouse_inside = p_inside;
  165. if (!p_inside) {
  166. hover = Hover::NONE;
  167. }
  168. range_edit_widget->queue_redraw();
  169. }
  170. float ParticleProcessMaterialMinMaxPropertyEditor::_get_min_ratio() const {
  171. return (min_range->get_value() - property_range.x) / (property_range.y - property_range.x);
  172. }
  173. float ParticleProcessMaterialMinMaxPropertyEditor::_get_max_ratio() const {
  174. return (max_range->get_value() - property_range.x) / (property_range.y - property_range.x);
  175. }
  176. float ParticleProcessMaterialMinMaxPropertyEditor::_get_left_offset() const {
  177. return margin.x + usable_area.x * _get_min_ratio();
  178. }
  179. float ParticleProcessMaterialMinMaxPropertyEditor::_get_right_offset() const {
  180. return margin.x + usable_area.x * _get_max_ratio();
  181. }
  182. Rect2 ParticleProcessMaterialMinMaxPropertyEditor::_get_middle_rect() const {
  183. if (Math::is_equal_approx(min_range->get_value(), max_range->get_value())) {
  184. return Rect2();
  185. }
  186. return Rect2(
  187. Vector2(_get_left_offset() - 1, margin.y),
  188. Vector2(usable_area.x * (_get_max_ratio() - _get_min_ratio()) + 1, usable_area.y));
  189. }
  190. void ParticleProcessMaterialMinMaxPropertyEditor::_set_clamped_values(float p_min, float p_max) {
  191. // This is required for editing widget in case the properties have or_less or or_greater hint.
  192. min_range->set_value(MAX(p_min, property_range.x));
  193. max_range->set_value(MIN(p_max, property_range.y));
  194. _update_slider_values();
  195. _sync_property();
  196. }
  197. void ParticleProcessMaterialMinMaxPropertyEditor::_sync_property() {
  198. const Vector2 value = Vector2(min_range->get_value(), max_range->get_value());
  199. emit_changed(get_edited_property(), value, "", true);
  200. range_edit_widget->queue_redraw();
  201. }
  202. void ParticleProcessMaterialMinMaxPropertyEditor::_update_mode() {
  203. max_edit->set_read_only(false);
  204. switch (slider_mode) {
  205. case Mode::RANGE: {
  206. min_edit->set_label("min");
  207. max_edit->set_label("max");
  208. max_edit->set_block_signals(true);
  209. max_edit->set_min(max_range->get_min());
  210. max_edit->set_max(max_range->get_max());
  211. max_edit->set_block_signals(false);
  212. min_edit->set_allow_lesser(min_range->is_lesser_allowed());
  213. min_edit->set_allow_greater(min_range->is_greater_allowed());
  214. max_edit->set_allow_lesser(max_range->is_lesser_allowed());
  215. max_edit->set_allow_greater(max_range->is_greater_allowed());
  216. } break;
  217. case Mode::MIDPOINT: {
  218. min_edit->set_label("val");
  219. max_edit->set_label(U"±");
  220. max_edit->set_block_signals(true);
  221. max_edit->set_min(0);
  222. max_edit->set_block_signals(false);
  223. min_edit->set_allow_lesser(min_range->is_lesser_allowed());
  224. min_edit->set_allow_greater(max_range->is_greater_allowed());
  225. max_edit->set_allow_lesser(false);
  226. max_edit->set_allow_greater(min_range->is_lesser_allowed() && max_range->is_greater_allowed());
  227. } break;
  228. }
  229. _update_slider_values();
  230. }
  231. void ParticleProcessMaterialMinMaxPropertyEditor::_toggle_mode(bool p_edit_mode) {
  232. slider_mode = p_edit_mode ? Mode::MIDPOINT : Mode::RANGE;
  233. EditorSettings::get_singleton()->set_project_metadata("editor_metadata", "particle_spin_mode", int(slider_mode));
  234. _update_mode();
  235. }
  236. void ParticleProcessMaterialMinMaxPropertyEditor::_update_slider_values() {
  237. switch (slider_mode) {
  238. case Mode::RANGE: {
  239. min_edit->set_value_no_signal(min_range->get_value());
  240. max_edit->set_value_no_signal(max_range->get_value());
  241. } break;
  242. case Mode::MIDPOINT: {
  243. min_edit->set_value_no_signal((min_range->get_value() + max_range->get_value()) * 0.5);
  244. max_edit->set_value_no_signal((max_range->get_value() - min_range->get_value()) * 0.5);
  245. max_edit->set_block_signals(true);
  246. max_edit->set_max(_get_max_spread());
  247. max_edit->set_read_only(max_edit->get_max() == 0);
  248. max_edit->set_block_signals(false);
  249. } break;
  250. }
  251. }
  252. void ParticleProcessMaterialMinMaxPropertyEditor::_sync_sliders(float, const EditorSpinSlider *p_changed_slider) {
  253. switch (slider_mode) {
  254. case Mode::RANGE: {
  255. if (p_changed_slider == max_edit) {
  256. min_edit->set_value_no_signal(MIN(min_edit->get_value(), max_edit->get_value()));
  257. }
  258. min_range->set_value(min_edit->get_value());
  259. if (p_changed_slider == min_edit) {
  260. max_edit->set_value_no_signal(MAX(min_edit->get_value(), max_edit->get_value()));
  261. }
  262. max_range->set_value(max_edit->get_value());
  263. _sync_property();
  264. } break;
  265. case Mode::MIDPOINT: {
  266. if (p_changed_slider == min_edit) {
  267. max_edit->set_block_signals(true); // If max changes, value may change.
  268. max_edit->set_max(_get_max_spread());
  269. max_edit->set_read_only(max_edit->get_max() == 0);
  270. max_edit->set_block_signals(false);
  271. }
  272. min_range->set_value(min_edit->get_value() - max_edit->get_value());
  273. max_range->set_value(min_edit->get_value() + max_edit->get_value());
  274. _sync_property();
  275. } break;
  276. }
  277. property_range.x = MIN(min_range->get_value(), min_range->get_min());
  278. property_range.y = MAX(max_range->get_value(), max_range->get_max());
  279. }
  280. float ParticleProcessMaterialMinMaxPropertyEditor::_get_max_spread() const {
  281. float max_spread = max_range->get_max() - min_range->get_min();
  282. if (max_edit->is_greater_allowed()) {
  283. return max_spread;
  284. }
  285. if (!min_edit->is_lesser_allowed()) {
  286. max_spread = MIN(max_spread, min_edit->get_value() - min_edit->get_min());
  287. }
  288. if (!min_edit->is_greater_allowed()) {
  289. max_spread = MIN(max_spread, min_edit->get_max() - min_edit->get_value());
  290. }
  291. return max_spread;
  292. }
  293. void ParticleProcessMaterialMinMaxPropertyEditor::_notification(int p_what) {
  294. switch (p_what) {
  295. case NOTIFICATION_THEME_CHANGED: {
  296. toggle_mode_button->set_button_icon(get_editor_theme_icon(SNAME("Anchor")));
  297. range_slider_left_icon = get_editor_theme_icon(SNAME("RangeSliderLeft"));
  298. range_slider_right_icon = get_editor_theme_icon(SNAME("RangeSliderRight"));
  299. min_edit->add_theme_color_override(SNAME("label_color"), get_theme_color(SNAME("property_color_x"), EditorStringName(Editor)));
  300. max_edit->add_theme_color_override(SNAME("label_color"), get_theme_color(SNAME("property_color_y"), EditorStringName(Editor)));
  301. const bool dark_theme = EditorThemeManager::is_dark_theme();
  302. const Color accent_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  303. background_color = dark_theme ? Color(0.3, 0.3, 0.3) : Color(0.7, 0.7, 0.7);
  304. normal_color = dark_theme ? Color(0.5, 0.5, 0.5) : Color(0.8, 0.8, 0.8);
  305. hovered_color = dark_theme ? Color(0.8, 0.8, 0.8) : Color(0.6, 0.6, 0.6);
  306. drag_color = hovered_color.lerp(accent_color, 0.8);
  307. midpoint_color = dark_theme ? Color(1, 1, 1) : Color(0, 0, 0);
  308. range_edit_widget->set_custom_minimum_size(Vector2(0, range_slider_left_icon->get_height() + 8));
  309. } break;
  310. }
  311. }
  312. void ParticleProcessMaterialMinMaxPropertyEditor::setup(float p_min, float p_max, float p_step, bool p_allow_less, bool p_allow_greater, bool p_degrees) {
  313. property_range = Vector2(p_min, p_max);
  314. // Initially all Ranges share properties.
  315. for (Range *range : Vector<Range *>{ min_range, min_edit, max_range, max_edit }) {
  316. range->set_min(p_min);
  317. range->set_max(p_max);
  318. range->set_step(p_step);
  319. range->set_allow_lesser(p_allow_less);
  320. range->set_allow_greater(p_allow_greater);
  321. }
  322. if (p_degrees) {
  323. min_edit->set_suffix(U" \u00B0");
  324. max_edit->set_suffix(U" \u00B0");
  325. }
  326. _update_mode();
  327. }
  328. void ParticleProcessMaterialMinMaxPropertyEditor::update_property() {
  329. const Vector2 value = get_edited_property_value();
  330. min_range->set_value(value.x);
  331. max_range->set_value(value.y);
  332. _update_slider_values();
  333. range_edit_widget->queue_redraw();
  334. }
  335. ParticleProcessMaterialMinMaxPropertyEditor::ParticleProcessMaterialMinMaxPropertyEditor() {
  336. VBoxContainer *content_vb = memnew(VBoxContainer);
  337. content_vb->add_theme_constant_override(SNAME("separation"), 0);
  338. add_child(content_vb);
  339. // Helper Range objects to keep absolute min and max values.
  340. min_range = memnew(Range);
  341. min_range->hide();
  342. add_child(min_range);
  343. max_range = memnew(Range);
  344. max_range->hide();
  345. add_child(max_range);
  346. // Range edit widget.
  347. HBoxContainer *hb = memnew(HBoxContainer);
  348. content_vb->add_child(hb);
  349. range_edit_widget = memnew(Control);
  350. range_edit_widget->set_h_size_flags(SIZE_EXPAND_FILL);
  351. range_edit_widget->set_tooltip_text(TTR("Hold Shift to scale around midpoint instead of moving."));
  352. hb->add_child(range_edit_widget);
  353. range_edit_widget->connect(SceneStringName(draw), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_range_edit_draw));
  354. range_edit_widget->connect(SceneStringName(gui_input), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_range_edit_gui_input));
  355. range_edit_widget->connect(SceneStringName(mouse_entered), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_set_mouse_inside).bind(true));
  356. range_edit_widget->connect(SceneStringName(mouse_exited), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_set_mouse_inside).bind(false));
  357. // Range controls for actual editing. Their min/max may depend on editing mode.
  358. hb = memnew(HBoxContainer);
  359. content_vb->add_child(hb);
  360. min_edit = memnew(EditorSpinSlider);
  361. min_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  362. hb->add_child(min_edit);
  363. min_edit->connect(SceneStringName(value_changed), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_sync_sliders).bind(min_edit));
  364. max_edit = memnew(EditorSpinSlider);
  365. max_edit->set_h_size_flags(SIZE_EXPAND_FILL);
  366. hb->add_child(max_edit);
  367. max_edit->connect(SceneStringName(value_changed), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_sync_sliders).bind(max_edit));
  368. toggle_mode_button = memnew(Button);
  369. toggle_mode_button->set_toggle_mode(true);
  370. toggle_mode_button->set_tooltip_text(TTR("Toggle between minimum/maximum and base value/spread modes."));
  371. hb->add_child(toggle_mode_button);
  372. toggle_mode_button->connect(SceneStringName(toggled), callable_mp(this, &ParticleProcessMaterialMinMaxPropertyEditor::_toggle_mode));
  373. set_bottom_editor(content_vb);
  374. }
  375. bool EditorInspectorParticleProcessMaterialPlugin::can_handle(Object *p_object) {
  376. return Object::cast_to<ParticleProcessMaterial>(p_object);
  377. }
  378. bool EditorInspectorParticleProcessMaterialPlugin::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {
  379. if (!ParticleProcessMaterial::has_min_max_property(p_path)) {
  380. return false;
  381. }
  382. ERR_FAIL_COND_V(p_hint != PROPERTY_HINT_RANGE, false);
  383. Ref<ParticleProcessMaterial> mat = Ref<ParticleProcessMaterial>(p_object);
  384. ERR_FAIL_COND_V(mat.is_null(), false);
  385. PackedStringArray range_hint = p_hint_text.split(",");
  386. float min = range_hint[0].to_float();
  387. float max = range_hint[1].to_float();
  388. float step = range_hint[2].to_float();
  389. bool allow_less = range_hint.find("or_less", 3) > -1;
  390. bool allow_greater = range_hint.find("or_greater", 3) > -1;
  391. bool degrees = range_hint.find("degrees", 3) > -1;
  392. ParticleProcessMaterialMinMaxPropertyEditor *ed = memnew(ParticleProcessMaterialMinMaxPropertyEditor);
  393. ed->setup(min, max, step, allow_less, allow_greater, degrees);
  394. add_property_editor(p_path, ed);
  395. return true;
  396. }