Skip to content

Instantly share code, notes, and snippets.

@me2beats
Last active November 28, 2021 01:32
Show Gist options
  • Save me2beats/7f978da1b4b7edcfa4010640e16ec369 to your computer and use it in GitHub Desktop.
Save me2beats/7f978da1b4b7edcfa4010640e16ec369 to your computer and use it in GitHub Desktop.
workaround for getting godot editor main screen
static func get_main_screen(plugin:EditorPlugin)->int:
var idx = -1
var base:Panel = plugin.get_editor_interface().get_base_control()
var button:ToolButton = find_node_by_class_path(
base, ['VBoxContainer', 'HBoxContainer', 'HBoxContainer', 'ToolButton'], false
)
if not button:
return idx
for b in button.get_parent().get_children():
b = b as ToolButton
if not b: continue
if b.pressed:
return b.get_index()
return idx
static func find_node_by_class_path(node:Node, class_path:Array, inverted:= true)->Node:
var res:Node
var stack = []
var depths = []
var first = class_path[0]
var children = node.get_children()
if not inverted:
children.invert()
for c in children:
if c.get_class() == first:
stack.push_back(c)
depths.push_back(0)
if not stack: return res
var max_ = class_path.size()-1
while stack:
var d = depths.pop_back()
var n = stack.pop_back()
if d>max_:
continue
if n.get_class() == class_path[d]:
if d == max_:
res = n
return res
var children_ = n.get_children()
if not inverted:
children_.invert()
for c in children_:
stack.push_back(c)
depths.push_back(d+1)
return res
@me2beats
Copy link
Author

me2beats commented Jan 6, 2021

another way is using EditorPlugin main_screen_changed signal

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment