2021-12-22 16:15:39 +01:00
|
|
|
tool
|
|
|
|
extends Control
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
var rect_editor_node_scene : PackedScene = preload("res://addons/world_generator/ui/RectViewNode.tscn")
|
2021-12-22 16:15:39 +01:00
|
|
|
|
2021-12-23 18:08:13 +01:00
|
|
|
export(NodePath) var zoom_widget_path : NodePath = ""
|
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
var _rect_scale : float = 1
|
2021-12-25 23:34:33 +01:00
|
|
|
|
2021-12-23 16:46:54 +01:00
|
|
|
var edited_resource : WorldGenBaseResource = null
|
2021-12-27 02:36:52 +01:00
|
|
|
var edited_resource_current_size : Vector2 = Vector2()
|
2021-12-22 16:15:39 +01:00
|
|
|
|
2022-01-21 22:58:14 +01:00
|
|
|
var _plugin : EditorPlugin = null
|
|
|
|
var _undo_redo : UndoRedo = null
|
|
|
|
|
2021-12-23 18:08:13 +01:00
|
|
|
func _enter_tree():
|
|
|
|
var zoom_widget : Node = get_node_or_null(zoom_widget_path)
|
|
|
|
|
|
|
|
if !zoom_widget:
|
|
|
|
return
|
|
|
|
|
2021-12-27 18:00:10 +01:00
|
|
|
if !zoom_widget.is_connected("zoom_changed", self, "on_zoom_changed"):
|
|
|
|
zoom_widget.connect("zoom_changed", self, "on_zoom_changed")
|
|
|
|
|
|
|
|
if !is_connected("visibility_changed", self, "on_visibility_changed"):
|
|
|
|
connect("visibility_changed", self, "on_visibility_changed")
|
2021-12-25 23:34:33 +01:00
|
|
|
|
2022-01-21 22:58:14 +01:00
|
|
|
func set_plugin(plugin : EditorPlugin) -> void:
|
|
|
|
_plugin = plugin
|
|
|
|
_undo_redo = _plugin.get_undo_redo()
|
|
|
|
|
2021-12-25 23:34:33 +01:00
|
|
|
func on_visibility_changed() -> void:
|
2021-12-27 17:56:44 +01:00
|
|
|
call_deferred("apply_zoom")
|
2021-12-25 23:34:33 +01:00
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
func apply_zoom() -> void:
|
|
|
|
if !edited_resource:
|
|
|
|
return
|
|
|
|
|
|
|
|
var rect : Rect2 = edited_resource.rect
|
|
|
|
edited_resource_current_size = rect.size
|
|
|
|
rect.position = rect.position * _rect_scale
|
|
|
|
rect.size = rect.size * _rect_scale
|
|
|
|
set_custom_minimum_size(rect.size)
|
|
|
|
|
2021-12-27 17:58:33 +01:00
|
|
|
var p : MarginContainer = get_parent() as MarginContainer
|
|
|
|
|
|
|
|
p.add_constant_override("margin_left", min(rect.size.x / 4.0, 50 * _rect_scale))
|
|
|
|
p.add_constant_override("margin_right", min(rect.size.x / 4.0, 50 * _rect_scale))
|
|
|
|
p.add_constant_override("margin_top", min(rect.size.y / 4.0, 50 * _rect_scale))
|
|
|
|
p.add_constant_override("margin_bottom", min(rect.size.y / 4.0, 50 * _rect_scale))
|
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
for c in get_children():
|
|
|
|
c.set_editor_rect_scale(_rect_scale)
|
2021-12-23 18:08:13 +01:00
|
|
|
|
|
|
|
func on_zoom_changed(zoom : float) -> void:
|
2021-12-27 17:56:44 +01:00
|
|
|
_rect_scale = zoom
|
|
|
|
apply_zoom()
|
2021-12-23 18:08:13 +01:00
|
|
|
|
2021-12-23 16:46:54 +01:00
|
|
|
func _draw():
|
|
|
|
draw_rect(Rect2(Vector2(), get_size()), Color(0.2, 0.2, 0.2, 1))
|
|
|
|
|
2022-08-05 01:21:08 +02:00
|
|
|
var rsh : float = clamp(_rect_scale / 2.0, 1, 5)
|
|
|
|
var c : Color = Color(0.4, 0.4, 0.4, 1)
|
|
|
|
|
|
|
|
# Indicators that show the size of a unit (1 chunk)
|
|
|
|
|
|
|
|
# Top left
|
|
|
|
draw_line(Vector2(_rect_scale, 0), Vector2(_rect_scale, rsh), c)
|
|
|
|
draw_line(Vector2(0, _rect_scale), Vector2(rsh, _rect_scale), c)
|
|
|
|
|
|
|
|
# Top right
|
|
|
|
draw_line(Vector2(get_size().x - _rect_scale, 0), Vector2(get_size().x - _rect_scale, rsh), c)
|
|
|
|
draw_line(Vector2(get_size().x - rsh, _rect_scale), Vector2(get_size().x, _rect_scale), c)
|
|
|
|
|
|
|
|
# Bottom left
|
|
|
|
draw_line(Vector2(_rect_scale, get_size().y - rsh), Vector2(_rect_scale, get_size().y), c)
|
|
|
|
draw_line(Vector2(0, get_size().y - _rect_scale), Vector2(rsh, get_size().y - _rect_scale), c)
|
|
|
|
|
|
|
|
# Bottom right
|
|
|
|
draw_line(Vector2(get_size().x - _rect_scale, get_size().y - rsh), Vector2(get_size().x - _rect_scale, get_size().y), c)
|
|
|
|
draw_line(Vector2(get_size().x - rsh, get_size().y - _rect_scale), Vector2(get_size().x, get_size().y - _rect_scale), c)
|
|
|
|
|
2022-08-05 17:25:37 +02:00
|
|
|
edited_resource.eitor_draw_additional_background(self)
|
|
|
|
|
2021-12-23 16:46:54 +01:00
|
|
|
func refresh() -> void:
|
|
|
|
clear()
|
|
|
|
|
|
|
|
if !edited_resource:
|
|
|
|
return
|
|
|
|
|
|
|
|
var rect : Rect2 = edited_resource.rect
|
2021-12-27 02:36:52 +01:00
|
|
|
edited_resource_current_size = rect.size
|
2021-12-27 17:56:44 +01:00
|
|
|
rect.position = rect.position * _rect_scale
|
|
|
|
rect.size = rect.size * _rect_scale
|
2021-12-23 16:46:54 +01:00
|
|
|
set_custom_minimum_size(rect.size)
|
|
|
|
|
2021-12-27 17:56:44 +01:00
|
|
|
apply_zoom()
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
refresh_rects()
|
|
|
|
|
2021-12-23 16:46:54 +01:00
|
|
|
func clear() -> void:
|
|
|
|
pass
|
|
|
|
|
2021-12-23 17:45:36 +01:00
|
|
|
func refresh_rects() -> void:
|
|
|
|
clear_rects()
|
|
|
|
|
|
|
|
if !edited_resource:
|
|
|
|
return
|
|
|
|
|
|
|
|
var cont : Array = edited_resource.get_content()
|
|
|
|
|
|
|
|
for c in cont:
|
|
|
|
if c:
|
|
|
|
var s : Node = rect_editor_node_scene.instance()
|
|
|
|
|
|
|
|
add_child(s)
|
2022-01-21 23:24:55 +01:00
|
|
|
s.set_plugin(_plugin)
|
2021-12-27 17:56:44 +01:00
|
|
|
s.set_editor_rect_scale(_rect_scale)
|
2021-12-27 02:36:52 +01:00
|
|
|
s.edited_resource_parent_size = edited_resource_current_size
|
2021-12-23 17:45:36 +01:00
|
|
|
s.set_edited_resource(c)
|
|
|
|
|
|
|
|
func clear_rects():
|
|
|
|
for c in get_children():
|
|
|
|
c.queue_free()
|
|
|
|
remove_child(c)
|
|
|
|
|
2021-12-23 16:46:54 +01:00
|
|
|
func set_edited_resource(res : WorldGenBaseResource):
|
2021-12-26 00:25:30 +01:00
|
|
|
if edited_resource:
|
|
|
|
edited_resource.disconnect("changed", self, "on_edited_resource_changed")
|
2021-12-23 16:46:54 +01:00
|
|
|
|
|
|
|
edited_resource = res
|
|
|
|
|
|
|
|
refresh()
|
|
|
|
|
2021-12-26 00:25:30 +01:00
|
|
|
if edited_resource:
|
|
|
|
edited_resource.connect("changed", self, "on_edited_resource_changed")
|
2021-12-22 16:15:39 +01:00
|
|
|
|
2021-12-26 00:25:30 +01:00
|
|
|
func on_edited_resource_changed() -> void:
|
|
|
|
call_deferred("refresh")
|