Last active
November 28, 2021 01:32
-
-
Save me2beats/7f978da1b4b7edcfa4010640e16ec369 to your computer and use it in GitHub Desktop.
workaround for getting godot editor main screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
another way is using EditorPlugin main_screen_changed signal