-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rework the way instance library items are exposed in the editor.
This is mainly to workaround issues with the old approach that was based on "emulated properties". Now it's a Blender-style custom list where the selected item appears below. An array was not possible because items need stable IDs.
- Loading branch information
Showing
14 changed files
with
684 additions
and
225 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
#include "control_sizer.h" | ||
#include "../../util/errors.h" | ||
#include "../../util/godot/classes/input_event_mouse_button.h" | ||
#include "../../util/godot/classes/input_event_mouse_motion.h" | ||
#include "../../util/godot/core/input_enums.h" | ||
#include "../../util/godot/editor_scale.h" | ||
#include "../../util/math/funcs.h" | ||
|
||
namespace zylann { | ||
|
||
ZN_ControlSizer::ZN_ControlSizer() { | ||
set_default_cursor_shape(Control::CURSOR_VSIZE); | ||
const real_t editor_scale = EDSCALE; | ||
set_custom_minimum_size(Vector2(0, editor_scale * 5)); | ||
} | ||
|
||
void ZN_ControlSizer::set_target_control(Control *control) { | ||
_target_control.set(control); | ||
} | ||
|
||
#ifdef ZN_GODOT | ||
void ZN_ControlSizer::gui_input(const Ref<InputEvent> &p_event) { | ||
#elif defined(ZN_GODOT_EXTENSION) | ||
void ZN_ControlSizer::_gui_input(const Ref<InputEvent> &p_event) { | ||
#endif | ||
|
||
Ref<InputEventMouseButton> mb = p_event; | ||
if (mb.is_valid()) { | ||
Control *target_control = _target_control.get(); | ||
ZN_ASSERT_RETURN(target_control != nullptr); | ||
|
||
if (mb->is_pressed()) { | ||
if (mb->get_button_index() == ::godot::MOUSE_BUTTON_LEFT) { | ||
_dragging = true; | ||
} | ||
} else { | ||
_dragging = false; | ||
} | ||
} | ||
|
||
Ref<InputEventMouseMotion> mm = p_event; | ||
if (mm.is_valid()) { | ||
if (_dragging) { | ||
Control *target_control = _target_control.get(); | ||
ZN_ASSERT_RETURN(target_control != nullptr); | ||
|
||
const Vector2 ms = target_control->get_custom_minimum_size(); | ||
// Assuming the UI is not scaled | ||
const Vector2 rel = mm->get_relative(); | ||
// Assuming vertical for now | ||
// TODO Clamp min_size to `target.get_minimum_size()`? | ||
target_control->set_custom_minimum_size(Vector2(ms.x, math::clamp(ms.y + rel.y, _min_size, _max_size))); | ||
} | ||
} | ||
} | ||
|
||
void ZN_ControlSizer::_notification(int p_what) { | ||
switch (p_what) { | ||
case NOTIFICATION_MOUSE_ENTER: { | ||
_mouse_inside = true; | ||
queue_redraw(); | ||
} break; | ||
|
||
case NOTIFICATION_MOUSE_EXIT: { | ||
_mouse_inside = false; | ||
queue_redraw(); | ||
} break; | ||
|
||
case NOTIFICATION_ENTER_TREE: | ||
cache_theme(); | ||
break; | ||
|
||
case NOTIFICATION_THEME_CHANGED: | ||
cache_theme(); | ||
break; | ||
|
||
case NOTIFICATION_DRAW: { | ||
if (_dragging || _mouse_inside) { | ||
draw_texture(_hover_icon, (get_size() - _hover_icon->get_size()) / 2); | ||
} | ||
} break; | ||
} | ||
} | ||
|
||
void ZN_ControlSizer::cache_theme() { | ||
// TODO I'd like to cache this, but `BIND_THEME_ITEM_CUSTOM` is not exposed to GDExtension... | ||
// TODO Have a framework-level StringName cache singleton | ||
_hover_icon = get_theme_icon("v_grabber", "SplitContainer"); | ||
} | ||
|
||
void ZN_ControlSizer::_bind_methods() {} | ||
|
||
} // namespace zylann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef ZN_GODOT_CONTROL_SIZER_H | ||
#define ZN_GODOT_CONTROL_SIZER_H | ||
|
||
#include "../../util/godot/classes/control.h" | ||
#include "../../util/godot/object_weak_ref.h" | ||
|
||
namespace zylann { | ||
|
||
// Implements similar logic as the middle resizing handle of SplitContainer, but works on a target control instead | ||
class ZN_ControlSizer : public Control { | ||
GDCLASS(ZN_ControlSizer, Control) | ||
public: | ||
ZN_ControlSizer(); | ||
|
||
void set_target_control(Control *control); | ||
|
||
#ifdef ZN_GODOT | ||
void gui_input(const Ref<InputEvent> &p_event) override; | ||
#elif defined(ZN_GODOT_EXTENSION) | ||
void _gui_input(const Ref<InputEvent> &p_event) override; | ||
#endif | ||
|
||
private: | ||
static void _bind_methods(); | ||
|
||
void _notification(int p_what); | ||
|
||
void cache_theme(); | ||
|
||
zylann::godot::ObjectWeakRef<Control> _target_control; | ||
bool _dragging = false; | ||
bool _mouse_inside = false; | ||
float _min_size = 10.0; | ||
float _max_size = 1000.0; | ||
Ref<Texture2D> _hover_icon; | ||
}; | ||
|
||
} // namespace zylann | ||
|
||
#endif // ZN_GODOT_CONTROL_SIZER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.