Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 84 additions & 81 deletions project/addons/terrain_3d/src/asset_dock.gd
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ signal confirmation_closed
signal confirmation_confirmed
signal confirmation_canceled

const ES_DOCK_SLOT: String = "terrain3d/dock/slot"
const ES_DOCK_TILE_SIZE: String = "terrain3d/dock/tile_size"
const ES_DOCK_FLOATING: String = "terrain3d/dock/floating"
const ES_DOCK_PINNED: String = "terrain3d/dock/always_on_top"
const ES_DOCK_WINDOW_POSITION: String = "terrain3d/dock/window_position"
const ES_DOCK_WINDOW_SIZE: String = "terrain3d/dock/window_size"
Expand All @@ -20,8 +18,6 @@ var mesh_list: ListContainer
var current_list: ListContainer
var _updating_list: bool

var placement_opt: OptionButton
var floating_btn: Button
var pinned_btn: Button
var size_slider: HSlider
var box: BoxContainer
Expand All @@ -43,34 +39,53 @@ enum {
}
var state: int = HIDDEN

enum {
POS_LEFT_UL = 0,
POS_LEFT_BL = 1,
POS_LEFT_UR = 2,
POS_LEFT_BR = 3,
POS_RIGHT_UL = 4,
POS_RIGHT_BL = 5,
POS_RIGHT_UR = 6,
POS_RIGHT_BR = 7,
POS_BOTTOM = 8,
POS_MAX = 9,

#DEPRECATED 4.5 - Delete and replace DockSlot with EditorDock.DockSlot
enum DockSlot {
DOCK_SLOT_NONE = -1, # The dock is closed.
DOCK_SLOT_LEFT_UL = 0, # Dock slot, left side, upper-left (empty in default layout).
DOCK_SLOT_LEFT_BL = 1, # Dock slot, left side, bottom-left (empty in default layout).
DOCK_SLOT_LEFT_UR = 2, # Dock slot, left side, upper-right (in default layout includes Scene and Import docks).
DOCK_SLOT_LEFT_BR = 3, # Dock slot, left side, bottom-right (in default layout includes FileSystem and History docks).
DOCK_SLOT_RIGHT_UL = 4, # Dock slot, right side, upper-left (in default layout includes Inspector, Signal, and Group docks).
DOCK_SLOT_RIGHT_BL = 5, # Dock slot, right side, bottom-left (empty in default layout).
DOCK_SLOT_RIGHT_UR = 6, # Dock slot, right side, upper-right (empty in default layout).
DOCK_SLOT_RIGHT_BR = 7, # Dock slot, right side, bottom-right (empty in default layout).
DOCK_SLOT_BOTTOM = 8, # Bottom panel.
DOCK_SLOT_MAX = 9, # Represents the size of the DockSlot enum.
}
var slot: int = POS_RIGHT_BR

var _dock: MarginContainer #DEPRECATED 4.5 - Use EditorDock
var _dock_slot: DockSlot = DockSlot.DOCK_SLOT_BOTTOM
var _initialized: bool = false
var plugin: EditorPlugin
var window: Window
var _godot_last_state: Window.Mode = Window.MODE_FULLSCREEN


func _notification(what: int) -> void:
if what == NOTIFICATION_ENTER_TREE:
await get_tree().process_frame
update_layout()


func initialize(p_plugin: EditorPlugin) -> void:
if p_plugin:
plugin = p_plugin


_dock = ClassDB.instantiate("EditorDock") #DEPRECATED 4.5 - EditorDock.new()
_dock.title = "Terrain3D"
_dock.dock_icon = preload("../icons/terrain3d.svg")
_dock.default_slot = DockSlot.DOCK_SLOT_BOTTOM
_dock.closable = false
_dock.available_layouts = 0x7 #DEPRECATED 4.5 - EditorDock.DOCK_LAYOUT_ALL
_dock.add_child(self)
plugin.add_dock(_dock)
_dock.open()
_dock.make_visible()

_godot_last_state = plugin.godot_editor_window.mode
placement_opt = $Box/Buttons/PlacementOpt
pinned_btn = $Box/Buttons/Pinned
floating_btn = $Box/Buttons/Floating
floating_btn.owner = null # Godot complains about buttons that are reparented
size_slider = $Box/Buttons/SizeSlider
size_slider.owner = null
box = $Box
Expand Down Expand Up @@ -101,8 +116,6 @@ func initialize(p_plugin: EditorPlugin) -> void:
resized.connect(update_layout)
textures_btn.pressed.connect(_on_textures_pressed)
meshes_btn.pressed.connect(_on_meshes_pressed)
placement_opt.item_selected.connect(set_slot)
floating_btn.pressed.connect(make_dock_float)
pinned_btn.toggled.connect(_on_pin_changed)
pinned_btn.visible = ( window != null )
pinned_btn.owner = null
Expand All @@ -112,37 +125,30 @@ func initialize(p_plugin: EditorPlugin) -> void:
meshes_btn.add_theme_font_size_override("font_size", 16 * EditorInterface.get_editor_scale())
textures_btn.add_theme_font_size_override("font_size", 16 * EditorInterface.get_editor_scale())

_initialized = true
update_dock()
update_layout()


func _ready() -> void:
if not _initialized:
return

# Setup styles
set("theme_override_styles/panel", get_theme_stylebox("panel", "Panel"))
# Avoid saving icon resources in tscn when editing w/ a tool script
if EditorInterface.get_edited_scene_root() != self:
pinned_btn.icon = get_theme_icon("Pin", "EditorIcons")
pinned_btn.text = ""
floating_btn.icon = get_theme_icon("MakeFloating", "EditorIcons")
floating_btn.text = ""
search_button.icon = get_theme_icon("Search", "EditorIcons")

search_box.text_changed.connect(_on_search_text_changed)
search_button.pressed.connect(_on_search_button_pressed)

confirm_dialog = ConfirmationDialog.new()
add_child(confirm_dialog, true)
confirm_dialog.hide()
confirm_dialog.confirmed.connect(func(): _confirmed = true; \
emit_signal("confirmation_closed"); \
emit_signal("confirmation_confirmed") )
confirmation_closed.emit(); \
confirmation_confirmed.emit() )
confirm_dialog.canceled.connect(func(): _confirmed = false; \
emit_signal("confirmation_closed"); \
emit_signal("confirmation_canceled") )
confirmation_closed.emit(); \
confirmation_canceled.emit() )

# Setup styles
set("theme_override_styles/panel", get_theme_stylebox("panel", "Panel"))
# Avoid saving icon resources in tscn when editing w/ a tool script
if EditorInterface.get_edited_scene_root() != self:
pinned_btn.icon = get_theme_icon("Pin", "EditorIcons")
pinned_btn.text = ""
search_button.icon = get_theme_icon("Search", "EditorIcons")

_initialized = true
update_dock()
update_layout()


func _gui_input(p_event: InputEvent) -> void:
Expand All @@ -156,26 +162,27 @@ func _gui_input(p_event: InputEvent) -> void:
## Dock placement


func set_slot(p_slot: int) -> void:
func set_slot(p_slot: DockSlot) -> void:
if plugin.debug:
print("Terrain3DAssetDock: set_slot: ", p_slot)
p_slot = clamp(p_slot, 0, POS_MAX-1)
p_slot = clamp(p_slot, 0, DockSlot.DOCK_SLOT_MAX - 1)

if slot != p_slot:
slot = p_slot
placement_opt.selected = slot
if _dock_slot != p_slot:
_dock_slot = p_slot
save_editor_settings()
plugin.select_terrain()
update_dock()


func remove_dock(p_force: bool = false) -> void:
plugin.remove_dock(_dock)

if state == SIDEBAR:
plugin.remove_control_from_docks(self)
#plugin.remove_control_from_docks(self)
state = HIDDEN

elif state == BOTTOM:
plugin.remove_control_from_bottom_panel(self)
#plugin.remove_control_from_bottom_panel(self)
state = HIDDEN

# If windowed and destination is not window or final exit, otherwise leave
Expand All @@ -189,10 +196,7 @@ func remove_dock(p_force: bool = false) -> void:
window.hide()
window.queue_free()
window = null
floating_btn.button_pressed = false
floating_btn.visible = true
pinned_btn.visible = false
placement_opt.visible = true
state = HIDDEN
update_dock() # return window to side/bottom

Expand All @@ -203,40 +207,40 @@ func update_dock() -> void:

update_assets()

# Move dock to new destination
remove_dock()
_dock.make_visible()
# Sidebar
if slot < POS_BOTTOM:
if _dock_slot < DockSlot.DOCK_SLOT_BOTTOM:
state = SIDEBAR
plugin.add_control_to_dock(slot, self)

# Bottom
elif slot == POS_BOTTOM:
elif _dock_slot == DockSlot.DOCK_SLOT_BOTTOM:
state = BOTTOM
plugin.add_control_to_bottom_panel(self, "Terrain3D")
plugin.make_bottom_panel_item_visible(self)



func update_layout() -> void:
if plugin.debug > 1:
print("Terrain3DAssetDock: update_layout")
if not _initialized:
return
if plugin.debug > 1:
print("Terrain3DAssetDock: update_layout")


# Detect if we have a new window from Make floating, grab it so we can free it properly
if not window and get_parent() and get_parent().get_parent() is Window:
window = get_parent().get_parent()
make_dock_float()
if not window and get_parent() and get_parent().get_parent().get_parent() is Window:
window = get_parent().get_parent().get_parent()
return # Will call this function again upon display

elif window and get_parent() and not get_parent().get_parent().get_parent() is Window:
window = null
return # Will call this function again upon display


# Vertical layout: buttons on top
if size.x < 500 or ( not window and slot < POS_BOTTOM ):
if size.x < 500 or ( not window and _dock_slot < DockSlot.DOCK_SLOT_BOTTOM ):
box.vertical = true
buttons.vertical = false
search_box.reparent(box)
box.move_child(search_box, 1)
size_slider.reparent(box)
box.move_child(size_slider, 2)
floating_btn.reparent(buttons)
pinned_btn.reparent(buttons)
else:
# Wide layout: buttons on left
Expand All @@ -245,10 +249,10 @@ func update_layout() -> void:
search_box.reparent(buttons)
buttons.move_child(search_box, 0)
size_slider.reparent(buttons)
buttons.move_child(size_slider, 4)
floating_btn.reparent(box)
buttons.move_child(size_slider, 3)
pinned_btn.reparent(box)

pinned_btn.visible = is_instance_valid(window)
save_editor_settings()


Expand Down Expand Up @@ -398,8 +402,6 @@ func make_dock_float() -> void:
state = WINDOWED
visible = true # Asset dock contents are hidden when popping out of the bottom!
pinned_btn.visible = true
floating_btn.visible = false
placement_opt.visible = false
window.title = "Terrain3D Asset Dock"
window.always_on_top = pinned_btn.button_pressed
window.close_requested.connect(remove_dock.bind(true))
Expand Down Expand Up @@ -479,13 +481,13 @@ func _on_godot_focus_exited() -> void:


func load_editor_settings() -> void:
floating_btn.button_pressed = plugin.get_setting(ES_DOCK_FLOATING, false)
# Remove old editor settings
const ES_DOCK: String = "terrain3d/dock/"
for setting in [ "slot", "floating" ]:
plugin.erase_setting(ES_DOCK + setting)
pinned_btn.button_pressed = plugin.get_setting(ES_DOCK_PINNED, true)
size_slider.value = plugin.get_setting(ES_DOCK_TILE_SIZE, 90)
_on_slider_changed(size_slider.value)
set_slot(plugin.get_setting(ES_DOCK_SLOT, POS_BOTTOM))
if floating_btn.button_pressed:
make_dock_float()
# TODO Don't save tab until thumbnail generation more reliable
#if plugin.get_setting(ES_DOCK_TAB, 0) == 1:
# _on_meshes_pressed()
Expand All @@ -495,9 +497,7 @@ func save_editor_settings() -> void:
if not _initialized:
return
clamp_window_position()
plugin.set_setting(ES_DOCK_SLOT, slot)
plugin.set_setting(ES_DOCK_TILE_SIZE, size_slider.value)
plugin.set_setting(ES_DOCK_FLOATING, floating_btn.button_pressed)
plugin.set_setting(ES_DOCK_PINNED, pinned_btn.button_pressed)
# TODO Don't save tab until thumbnail generation more reliable
# plugin.set_setting(ES_DOCK_TAB, 0 if current_list == texture_list else 1)
Expand Down Expand Up @@ -1055,6 +1055,9 @@ class ListEntry extends MarginContainer:


func set_selected(value: bool) -> void:
if not is_inside_tree():
#push_error("not in tree")
return
is_selected = value
if is_selected:
# Handle scrolling to show the selected item
Expand Down
Loading