Initial support for PopupMenu.

This commit is contained in:
Nolan Darilek 2018-07-19 20:12:01 +00:00
parent d544935ebb
commit b05ad0307e

View File

@ -2,6 +2,8 @@ extends Object
var node var node
var position_in_children = 0
func item_or_items(count): func item_or_items(count):
if count == 1: if count == 1:
return "item" return "item"
@ -17,8 +19,6 @@ func focus_button():
else: else:
print("button") print("button")
var list_pos = 0
func focus_item_list(): func focus_item_list():
var count = node.get_item_count() var count = node.get_item_count()
var selected = node.get_selected_items() var selected = node.get_selected_items()
@ -36,29 +36,26 @@ func handle_item_list_nothing_selected():
print("Nothing selected") print("Nothing selected")
func input_item_list(event): func input_item_list(event):
var old_list_pos = list_pos var old_pos = position_in_children
if event.echo or not event.pressed: if event.echo or not event.pressed:
return return
if event.scancode == KEY_UP: if event.scancode == KEY_UP:
node.get_tree().set_input_as_handled() node.get_tree().set_input_as_handled()
if list_pos == 0: if position_in_children == 0:
return return
list_pos -= 1 position_in_children -= 1
elif event.scancode == KEY_DOWN: elif event.scancode == KEY_DOWN:
node.get_tree().set_input_as_handled() node.get_tree().set_input_as_handled()
if list_pos >= node.get_item_count()-1: if position_in_children >= node.get_item_count()-1:
return return
list_pos += 1 position_in_children += 1
elif event.scancode == KEY_HOME: elif event.scancode == KEY_HOME:
list_pos = 0 position_in_children = 0
elif event.scancode == KEY_END: elif event.scancode == KEY_END:
list_pos = node.get_item_count()-1 position_in_children = node.get_item_count()-1
if old_list_pos != list_pos: if old_pos != position_in_children:
var text = node.get_item_text(list_pos) var text = node.get_item_text(position_in_children)
print("%s: %s of %s" % [text, list_pos+1, node.get_item_count()]) print("%s: %s of %s" % [text, position_in_children+1, node.get_item_count()])
func unfocus_item_list():
list_pos = 0
func focus_line_edit(): func focus_line_edit():
var text = "blank" var text = "blank"
@ -91,25 +88,55 @@ func check_caret_moved():
print(text[pos]) print(text[pos])
old_pos = pos old_pos = pos
func render_selected_tree_item(): func focus_menu_button():
print(node.text, ": menu")
func render_popup_menu_item(id):
var item = node.get_item_text(id)
var submenu = node.get_item_submenu(position_in_children)
var tooltip = node.get_item_tooltip(position_in_children)
if submenu:
item = submenu
if item and tooltip:
item += ": "
item += tooltip
elif tooltip:
item = tooltip
var shortcut = node.get_item_shortcut(position_in_children)
if shortcut:
item += ": "+shortcut.get_as_text()
return item
func focus_popup_menu():
print("menu")
func focus_popup_menu_item(id):
print(render_popup_menu_item(id))
func render_tree_item():
var item = node.get_selected() var item = node.get_selected()
var result = item.get_text(0) var result = item.get_text(0)
return result return result
func focus_tree(): func focus_tree():
print(render_selected_tree_item(), ": tree item") print(render_tree_item(), ": tree item")
func select_tree(): func select_tree():
if node.has_focus(): if node.has_focus():
print(render_selected_tree_item()) print(render_tree_item())
func focused(): func focused():
if node is Button: # print(node)
if node is MenuButton:
focus_menu_button()
elif node is Button:
focus_button() focus_button()
elif node is ItemList: elif node is ItemList:
focus_item_list() focus_item_list()
elif node is LineEdit: elif node is LineEdit:
focus_line_edit() focus_line_edit()
elif node is PopupMenu:
focus_popup_menu()
elif node is Tree: elif node is Tree:
focus_tree() focus_tree()
else: else:
@ -118,13 +145,12 @@ func focused():
print(node.hint_tooltip) print(node.hint_tooltip)
func unfocused(): func unfocused():
if node is ItemList: position_in_children = 0
unfocus_item_list()
func gui_input(event): func gui_input(event):
if node is ItemList: if node is ItemList:
return input_item_list(event) return input_item_list(event)
if node is LineEdit: elif node is LineEdit:
return check_caret_moved() return check_caret_moved()
func _init(node): func _init(node):
@ -147,6 +173,8 @@ func _init(node):
elif node is LineEdit: elif node is LineEdit:
node.connect("text_deleted", self, "text_deleted") node.connect("text_deleted", self, "text_deleted")
node.connect("text_inserted", self, "text_inserted") node.connect("text_inserted", self, "text_inserted")
elif node is PopupMenu:
node.connect("id_focused", self, "focus_popup_menu_item")
elif node is Tree: elif node is Tree:
node.connect("item_selected", self, "select_tree") node.connect("item_selected", self, "select_tree")
node.connect("tree_exiting", self, "free") node.connect("tree_exiting", self, "free")