Skip to content

Commit

Permalink
More progress on flexible layout (fixed show/hide panel menus)
Browse files Browse the repository at this point in the history
  • Loading branch information
RodZill4 committed Dec 10, 2023
1 parent febdc73 commit fe49cf8
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 92 deletions.
65 changes: 43 additions & 22 deletions addons/flexible_layout/flexible_layout.gd
Original file line number Diff line number Diff line change
Expand Up @@ -388,24 +388,32 @@ class FlexLayout:
top.set_child(flex_split)
top.child.insert(tab)
return tab

func layout(layout = null):

func is_panel_shown(panel_name : String) -> bool:
var panel : Control = main_control.panels[panel_name]
return (panel and panel.get_meta("flex_node") != null)

func show_panel(panel_name : String):
var panel : Control = main_control.panels[panel_name]
if panel.get_meta("flex_node") != null:
return
if panel_name == "Main":
get_flexmain().add(panel)
else:
get_default_flextab().add(panel)

func init(layout = null):
var default_flextab : FlexTab = null
if layout != null:
top = deserialize(null, layout)
elif main_control == control:
if top == null:
top = FlexTop.new(self)
for panel_name in main_control.panels.keys():
var panel : Control = main_control.panels[panel_name]
if panel.get_meta("flex_node") != null:
continue
if panel_name == "Main":
get_flexmain().add(panel)
else:
if default_flextab == null:
default_flextab = get_default_flextab()
default_flextab.add(panel)
show_panel(panel_name)
layout()

func layout():
if top:
top.layout(control.get_rect())
main_control.layout_changed.emit()
Expand Down Expand Up @@ -478,7 +486,7 @@ class FlexWindow:
flex_layout = FlexLayout.new(main_control, panel)
main_control.get_viewport().add_child(self)
if first_panel:
flex_layout.layout({children=[{type="FlexTab",tabs=[first_panel.name],current=0,h=768,w=288,children=[]}],type="FlexTop",h=768,w=1024})
flex_layout.init({children=[{type="FlexTab",tabs=[first_panel.name],current=0,h=768,w=288,children=[]}],type="FlexTop",h=768,w=1024})
resize()

func _ready():
Expand All @@ -498,10 +506,10 @@ class FlexWindow:
data.layout = flex_layout.serialize()
return data

func layout(data : Dictionary):
func init(data : Dictionary):
position = Vector2i(data.x, data.y)
size = Vector2i(data.w, data.h)
flex_layout.layout(data.layout)
flex_layout.init(data.layout)
resize()

func start_drag():
Expand All @@ -519,8 +527,6 @@ var panels : Dictionary = {}
var flex_layout : FlexLayout
var subwindows : Array[Window]

var no_auto_layout : bool = true

var overlay : Control


Expand All @@ -544,16 +550,32 @@ func add(n : String, c : Control):
c.set_meta("flex_layout", self)
panels[n] = c

func layout(layout = null):
func init(layout = null):
if layout and layout.has("main"):
for w in layout.windows:
var subwindow = FlexWindow.new(self)
subwindows.append(subwindow)
subwindow.layout(w)
flex_layout.layout(layout.main)
flex_layout.init(layout.main)
else:
flex_layout.init(layout)

func layout():
flex_layout.layout()
for w in subwindows:
w.flex_layout.layout()

func show_panel(panel_name : String, v : bool = true):
if v:
flex_layout.show_panel(panel_name)
else:
flex_layout.layout(layout)
no_auto_layout = false
if panel_name == "Main":
return
var panel : Control = panels[panel_name]
var flex_node = panel.get_meta("flex_node")
if flex_node == null:
return
flex_node.remove(panel)

func serialize() -> Dictionary:
var data : Dictionary = {}
Expand All @@ -579,5 +601,4 @@ func end_drag():
w.end_drag()

func _on_resized():
if not no_auto_layout:
layout()
flex_layout.layout()
4 changes: 2 additions & 2 deletions material_maker/main_window.gd
Original file line number Diff line number Diff line change
Expand Up @@ -476,8 +476,8 @@ func create_menu_show_panels(menu : MMMenuManager.MenuBase) -> void:

func _on_ShowPanels_id_pressed(id) -> void:
var panel : String = layout.get_panel_list()[id]
layout.set_panel_visible(panel, !layout.is_panel_visible(panel))

layout.set_panel_visible(panel, not layout.is_panel_visible(panel))
update_menus()

func create_menu_create(menu : MMMenuManager.MenuBase) -> void:
var gens = mm_loader.get_generator_list()
Expand Down
75 changes: 9 additions & 66 deletions material_maker/main_window_layout.gd
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
extends HBoxContainer

const PANEL_POSITIONS = {
TopLeft="Left/Top",
BottomLeft="Left/Bottom",
TopRight="SplitRight/Right/Top",
BottomRight="SplitRight/Right/Bottom"
}

const PANELS = [
{ name="Library", scene=preload("res://material_maker/panels/library/library.tscn"), position="TopLeft" },
{ name="Preview2D", scene=preload("res://material_maker/panels/preview_2d/preview_2d_panel.tscn"), position="TopRight" },
Expand All @@ -23,10 +18,12 @@ const HIDE_PANELS = {
paint=[ "Preview3D", "Histogram", "Hierarchy" ]
}


var panels = {}
var previous_width : float
var current_mode : String = "material"


func _ready() -> void:
previous_width = size.x

Expand All @@ -46,9 +43,10 @@ func load_panels() -> void:
node.set(p, panel.parameters[p])
panels[panel.name] = node
$FlexibleLayout.add(panel.name, node)
var current_config = null
if mm_globals.config.has_section_key("layout", "material"):
var current_config : Dictionary = JSON.parse_string(mm_globals.config.get_value("layout", "material"))
$FlexibleLayout.layout(current_config)
current_config = JSON.parse_string(mm_globals.config.get_value("layout", "material"))
$FlexibleLayout.init(current_config)

func save_config() -> void:
var current_config : Dictionary = $FlexibleLayout.serialize()
Expand All @@ -65,72 +63,17 @@ func get_panel_list() -> Array:
return panels_list

func is_panel_visible(panel_name : String) -> bool:
return panels[panel_name].get_parent() != null
return $FlexibleLayout.flex_layout.is_panel_shown(panel_name)

func set_panel_visible(panel_name : String, v : bool) -> void:
var panel = panels[panel_name]
panel.set_meta("hidden", !v)
if panel.is_inside_tree():
panel.set_meta("parent_tab_container", panel.get_parent())
if v and HIDE_PANELS[current_mode].find(panel_name) == -1:
if ! panel.is_inside_tree():
panel.get_meta("parent_tab_container").add_child(panel)
elif panel.is_inside_tree():
panel.get_parent().remove_child(panel)
update_panels()
$FlexibleLayout.show_panel(panel_name, v)
$FlexibleLayout.layout()

func change_mode(m : String) -> void:
current_mode = m
return
for p in panels:
set_panel_visible(p, !panels[p].get_meta("hidden"))
update_panels()

func update_panels() -> void:
return
"""
var left_width = $Left.size.x
var left_requested = left_width
var right_width = $SplitRight/Right.size.x
var right_requested = right_width
if $Left/Top.get_tab_count() == 0:
if $Left/Bottom.get_tab_count() == 0:
left_requested = 10
$Left.split_offset -= ($Left/Top.size.y-$Left/Bottom.size.y)/2
$Left.clamp_split_offset()
else:
$Left.split_offset -= $Left/Top.size.y-10
$Left.clamp_split_offset()
elif $Left/Bottom.get_tab_count() == 0:
$Left.split_offset += $Left/Bottom.size.y-10
$Left.clamp_split_offset()
if $SplitRight/Right/Top.get_tab_count() == 0:
if $SplitRight/Right/Bottom.get_tab_count() == 0:
right_requested = 10
$SplitRight/Right.split_offset -= ($SplitRight/Right/Top.size.y-$SplitRight/Right/Bottom.size.y)/2
$SplitRight/Right.clamp_split_offset()
else:
$SplitRight/Right.split_offset -= $SplitRight/Right/Top.size.y-10
$SplitRight/Right.clamp_split_offset()
elif $SplitRight/Right/Bottom.get_tab_count() == 0:
$SplitRight/Right.split_offset += $SplitRight/Right/Bottom.size.y-10
$SplitRight/Right.clamp_split_offset()
split_offset += left_requested - left_width + right_requested - right_width
clamp_split_offset()
$SplitRight.split_offset += right_width - right_requested
"""

func _on_Left_dragged(_offset : int) -> void:
$Left.clamp_split_offset()

func _on_Right_dragged(_offset : int) -> void:
$SplitRight/Right.clamp_split_offset()

func _on_tab_changed(_tab):
update_panels()

func _on_Layout_resized():
pass
# warning-ignore:narrowing_conversion
#split_offset -= size.x - previous_width
#previous_width = size.x
7 changes: 5 additions & 2 deletions material_maker/widgets/tabs/tabs.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var current_tab : int = -1 :
node = get_child(current_tab)
node.visible = true
node.position = Vector2(0, $TabBar.size.y)
node.size = size - node.position
node.size = size - node.position - Vector2(6, 6)
$TabBar.current_tab = current_tab
emit_signal("tab_changed", current_tab)

Expand Down Expand Up @@ -115,7 +115,10 @@ func _on_Tabs_tab_changed(tab) -> void:

func _on_Projects_resized() -> void:
$TabBar.size.x = size.x

if current_tab >= 0:
var node = get_child(current_tab)
node.position = Vector2(0, $TabBar.size.y)
node.size = size - node.position - Vector2(6, 6)

func _on_CrashRecoveryTimer_timeout():
for i in range($TabBar.get_tab_count()):
Expand Down

0 comments on commit fe49cf8

Please sign in to comment.