2021-12-23 17:45:36 +01:00
|
|
|
tool
|
|
|
|
extends MarginContainer
|
|
|
|
|
2021-12-24 22:20:01 +01:00
|
|
|
enum DragType {
|
|
|
|
DRAG_NONE = 0,
|
|
|
|
DRAG_MOVE = 1,
|
|
|
|
DRAG_RESIZE_TOP = 1 << 1,
|
|
|
|
DRAG_RESIZE_RIGHT = 1 << 2,
|
|
|
|
DRAG_RESIZE_BOTTOM = 1 << 3,
|
|
|
|
DRAG_RESIZE_LEFT = 1 << 4
|
|
|
|
};
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
var edited_resource : WorldGenBaseResource = null
|
2021-12-27 02:36:52 +01:00
|
|
|
var edited_resource_parent_size : Vector2 = Vector2()
|
2021-12-23 17:45:36 +01:00
|
|
|
|
2021-12-26 00:03:17 +01:00
|
|
|
var _edited_resource_rect_border_color : Color = Color(1, 1, 1, 1)
|
|
|
|
var _edited_resource_rect_color : Color = Color(0.8, 0.8, 0.8, 0.9)
|
|
|
|
var _editor_rect_border_size : int = 2
|
|
|
|
var _edited_resource_font_color : Color = Color(0, 0, 0, 1)
|
|
|
|
var _editor_additional_text : String = ""
|
|
|
|
|
2021-12-24 22:20:01 +01:00
|
|
|
var drag_type : int
|
|
|
|
var drag_offset : Vector2
|
|
|
|
var drag_offset_far : Vector2
|
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
var _rect_scale : float = 1
|
|
|
|
|
2022-01-21 23:24:55 +01:00
|
|
|
var _edited_resource_event_ignore : bool = false
|
|
|
|
|
|
|
|
var _plugin : EditorPlugin = null
|
|
|
|
var _undo_redo : UndoRedo = null
|
|
|
|
|
|
|
|
func set_plugin(plugin : EditorPlugin) -> void:
|
|
|
|
_plugin = plugin
|
|
|
|
_undo_redo = _plugin.get_undo_redo()
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
func _draw():
|
2021-12-26 00:03:17 +01:00
|
|
|
draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_color)
|
|
|
|
draw_rect(Rect2(Vector2(), get_size()), _edited_resource_rect_border_color, false, _editor_rect_border_size)
|
|
|
|
|
|
|
|
var font : Font = get_font("font")
|
|
|
|
|
|
|
|
var res_name : String = "NULL"
|
|
|
|
|
|
|
|
if edited_resource:
|
|
|
|
res_name = edited_resource.resource_name
|
2021-12-26 18:50:09 +01:00
|
|
|
|
|
|
|
var res_cls : String = ""
|
|
|
|
|
|
|
|
if edited_resource:
|
|
|
|
res_cls = edited_resource.get_editor_class()
|
2021-12-26 00:03:17 +01:00
|
|
|
|
|
|
|
draw_string(font, Vector2(_editor_rect_border_size, font.get_height()), res_name, _edited_resource_font_color)
|
2022-08-05 13:35:27 +02:00
|
|
|
|
2021-12-26 18:50:09 +01:00
|
|
|
if res_cls != "":
|
2022-08-05 13:35:27 +02:00
|
|
|
draw_string(font, Vector2(_editor_rect_border_size, font.get_height() * 2), res_cls, _edited_resource_font_color, get_rect().size.x)
|
|
|
|
|
|
|
|
if _editor_additional_text != "":
|
|
|
|
draw_string(font, Vector2(_editor_rect_border_size, font.get_height() * 3), _editor_additional_text, _edited_resource_font_color, get_rect().size.x)
|
|
|
|
|
2022-08-05 13:42:22 +02:00
|
|
|
if edited_resource:
|
|
|
|
edited_resource.eitor_draw_additional(self)
|
2021-12-23 17:45:36 +01:00
|
|
|
|
|
|
|
func refresh() -> void:
|
|
|
|
if !edited_resource:
|
|
|
|
return
|
2021-12-27 02:36:52 +01:00
|
|
|
|
|
|
|
#anchor is bottom left here
|
2021-12-23 17:45:36 +01:00
|
|
|
var rect : Rect2 = edited_resource.get_rect()
|
2021-12-27 17:56:44 +01:00
|
|
|
rect.position *= _rect_scale
|
|
|
|
rect.size *= _rect_scale
|
2021-12-23 17:45:36 +01:00
|
|
|
|
2021-12-27 02:36:52 +01:00
|
|
|
#anchor needs to be on top left here
|
|
|
|
var rp : Vector2 = rect.position
|
2021-12-27 17:56:44 +01:00
|
|
|
rp.y = edited_resource_parent_size.y * _rect_scale - rect.size.y - rect.position.y
|
2021-12-27 02:36:52 +01:00
|
|
|
rect_position = rp
|
2021-12-23 17:45:36 +01:00
|
|
|
rect_size = rect.size
|
|
|
|
|
|
|
|
update()
|
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
func set_editor_rect_scale(rect_scale) -> void:
|
|
|
|
_rect_scale = rect_scale
|
|
|
|
|
|
|
|
refresh()
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
func set_edited_resource(res : WorldGenBaseResource):
|
|
|
|
edited_resource = res
|
|
|
|
|
2021-12-26 00:03:17 +01:00
|
|
|
if edited_resource:
|
|
|
|
_edited_resource_rect_border_color = edited_resource.get_editor_rect_border_color()
|
|
|
|
_edited_resource_rect_color = edited_resource.get_editor_rect_color()
|
|
|
|
_editor_rect_border_size = edited_resource.get_editor_rect_border_size()
|
|
|
|
_edited_resource_font_color = edited_resource.get_editor_font_color()
|
|
|
|
_editor_additional_text = edited_resource.get_editor_additional_text()
|
2022-01-21 23:24:55 +01:00
|
|
|
|
|
|
|
edited_resource.connect("changed", self, "on_edited_resource_changed")
|
|
|
|
|
|
|
|
refresh()
|
2021-12-26 00:03:17 +01:00
|
|
|
|
2022-01-21 23:24:55 +01:00
|
|
|
func on_edited_resource_changed() -> void:
|
|
|
|
if _edited_resource_event_ignore:
|
|
|
|
return
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
refresh()
|
2021-12-26 00:03:17 +01:00
|
|
|
|
2021-12-24 22:20:01 +01:00
|
|
|
#based on / ported from engine/scene/gui/dialogs.h and .cpp
|
|
|
|
func _notification(p_what : int) -> void:
|
|
|
|
if (p_what == NOTIFICATION_MOUSE_EXIT):
|
|
|
|
# Reset the mouse cursor when leaving the resizable window border.
|
|
|
|
if (edited_resource && !edited_resource.locked && !drag_type):
|
|
|
|
if (get_default_cursor_shape() != CURSOR_ARROW):
|
|
|
|
set_default_cursor_shape(CURSOR_ARROW)
|
|
|
|
|
|
|
|
#based on / ported from engine/scene/gui/dialogs.h and .cpp
|
|
|
|
func _gui_input(p_event : InputEvent) -> void:
|
|
|
|
if (p_event is InputEventMouseButton) && (p_event.get_button_index() == BUTTON_LEFT):
|
|
|
|
var mb : InputEventMouseButton = p_event as InputEventMouseButton
|
|
|
|
|
|
|
|
if (mb.is_pressed()):
|
|
|
|
# Begin a possible dragging operation.
|
|
|
|
drag_type = _drag_hit_test(Vector2(mb.get_position().x, mb.get_position().y))
|
|
|
|
|
|
|
|
if (drag_type != DragType.DRAG_NONE):
|
|
|
|
drag_offset = get_global_mouse_position() - get_position()
|
|
|
|
|
|
|
|
drag_offset_far = get_position() + get_size() - get_global_mouse_position()
|
|
|
|
|
|
|
|
elif (drag_type != DragType.DRAG_NONE && !mb.is_pressed()):
|
|
|
|
# End a dragging operation.
|
|
|
|
drag_type = DragType.DRAG_NONE
|
2022-01-21 23:24:55 +01:00
|
|
|
|
|
|
|
var rect : Rect2 = get_rect()
|
|
|
|
#rect needs to be converted back
|
|
|
|
rect.position.y = edited_resource_parent_size.y * _rect_scale - rect.size.y - rect.position.y
|
|
|
|
rect.position /= _rect_scale
|
|
|
|
rect.size /= _rect_scale
|
|
|
|
|
|
|
|
#edited_resource.set_rect(rect)
|
|
|
|
_edited_resource_event_ignore = true
|
|
|
|
_undo_redo.create_action("WE: Drag End")
|
|
|
|
_undo_redo.add_do_method(edited_resource, "set_rect", rect)
|
|
|
|
_undo_redo.add_undo_method(edited_resource, "set_rect", edited_resource.get_rect())
|
|
|
|
_undo_redo.commit_action()
|
|
|
|
_edited_resource_event_ignore = false
|
2021-12-24 22:20:01 +01:00
|
|
|
|
|
|
|
if p_event is InputEventMouseMotion:
|
|
|
|
var mm : InputEventMouseMotion = p_event as InputEventMouseMotion
|
|
|
|
|
|
|
|
if (drag_type == DragType.DRAG_NONE):
|
|
|
|
# Update the cursor while moving along the borders.
|
|
|
|
var cursor = CURSOR_ARROW
|
|
|
|
if (!edited_resource.locked):
|
2021-12-24 22:32:45 +01:00
|
|
|
var preview_drag_type : int = _drag_hit_test(Vector2(mm.get_position().x, mm.get_position().y))
|
2021-12-24 22:20:01 +01:00
|
|
|
|
|
|
|
var top_left : int = DragType.DRAG_RESIZE_TOP + DragType.DRAG_RESIZE_LEFT
|
|
|
|
var bottom_right : int = DragType.DRAG_RESIZE_BOTTOM + DragType.DRAG_RESIZE_RIGHT
|
|
|
|
var top_right : int = DragType.DRAG_RESIZE_TOP + DragType.DRAG_RESIZE_RIGHT
|
|
|
|
var bottom_left : int = DragType.DRAG_RESIZE_BOTTOM + DragType.DRAG_RESIZE_LEFT
|
|
|
|
|
|
|
|
match (preview_drag_type):
|
|
|
|
DragType.DRAG_RESIZE_TOP:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_VSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
DragType.DRAG_RESIZE_BOTTOM:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_VSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
DragType.DRAG_RESIZE_LEFT:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_HSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
DragType.DRAG_RESIZE_RIGHT:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_HSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
top_left:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_FDIAGSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
bottom_right:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_FDIAGSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
top_right:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_BDIAGSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
bottom_left:
|
2021-12-24 22:25:56 +01:00
|
|
|
cursor = CURSOR_BDIAGSIZE
|
2021-12-24 22:20:01 +01:00
|
|
|
|
|
|
|
if (get_cursor_shape() != cursor):
|
|
|
|
set_default_cursor_shape(cursor);
|
|
|
|
|
|
|
|
else:
|
|
|
|
# Update while in a dragging operation.
|
|
|
|
var global_pos : Vector2 = get_global_mouse_position()
|
|
|
|
|
|
|
|
var rect : Rect2 = get_rect()
|
|
|
|
var min_size : Vector2 = get_combined_minimum_size()
|
|
|
|
|
|
|
|
if (drag_type == DragType.DRAG_MOVE):
|
|
|
|
rect.position = global_pos - drag_offset
|
|
|
|
else:
|
|
|
|
if (drag_type & DragType.DRAG_RESIZE_TOP):
|
|
|
|
var bottom : int = rect.position.y + rect.size.y
|
|
|
|
var max_y : int = bottom - min_size.y
|
|
|
|
rect.position.y = min(global_pos.y - drag_offset.y, max_y)
|
|
|
|
rect.size.y = bottom - rect.position.y
|
|
|
|
elif (drag_type & DragType.DRAG_RESIZE_BOTTOM):
|
|
|
|
rect.size.y = global_pos.y - rect.position.y + drag_offset_far.y
|
|
|
|
|
|
|
|
if (drag_type & DragType.DRAG_RESIZE_LEFT):
|
|
|
|
var right : int = rect.position.x + rect.size.x
|
|
|
|
var max_x : int = right - min_size.x
|
|
|
|
rect.position.x = min(global_pos.x - drag_offset.x, max_x)
|
|
|
|
rect.size.x = right - rect.position.x
|
|
|
|
elif (drag_type & DragType.DRAG_RESIZE_RIGHT):
|
|
|
|
rect.size.x = global_pos.x - rect.position.x + drag_offset_far.x
|
|
|
|
|
|
|
|
set_size(rect.size)
|
|
|
|
set_position(rect.position)
|
|
|
|
|
|
|
|
#based on / ported from engine/scene/gui/dialogs.h and .cpp
|
|
|
|
func _drag_hit_test(pos : Vector2) -> int:
|
|
|
|
var drag_type : int = DragType.DRAG_NONE
|
|
|
|
|
|
|
|
if (!edited_resource.locked):
|
2021-12-25 23:41:50 +01:00
|
|
|
var scaleborder_size : int = 5 #get_constant("scaleborder_size", "WindowDialog")
|
2021-12-24 22:20:01 +01:00
|
|
|
|
|
|
|
var rect : Rect2 = get_rect()
|
|
|
|
|
2021-12-24 22:25:56 +01:00
|
|
|
if (pos.y < (scaleborder_size)):
|
2021-12-24 22:20:01 +01:00
|
|
|
drag_type = DragType.DRAG_RESIZE_TOP
|
|
|
|
elif (pos.y >= (rect.size.y - scaleborder_size)):
|
|
|
|
drag_type = DragType.DRAG_RESIZE_BOTTOM
|
|
|
|
|
|
|
|
if (pos.x < scaleborder_size):
|
|
|
|
drag_type |= DragType.DRAG_RESIZE_LEFT
|
|
|
|
elif (pos.x >= (rect.size.x - scaleborder_size)):
|
|
|
|
drag_type |= DragType.DRAG_RESIZE_RIGHT
|
2021-12-24 22:32:45 +01:00
|
|
|
|
|
|
|
if (drag_type == DragType.DRAG_NONE):
|
|
|
|
drag_type = DragType.DRAG_MOVE
|
2021-12-24 22:20:01 +01:00
|
|
|
|
|
|
|
return drag_type
|