当前位置:网站首页>[Godot] add menu button
[Godot] add menu button
2022-07-03 18:18:00 【Apprentice Zhang】
Godot 3.4.2
Add menus quickly and easily , In the following code , After running, two menus will be created File、Edit
## Menu list
extends HBoxContainer
const MenuKey = {
"MenuName": "menu_name",
'ScanCode': "scancode",
"Control" : 'control',
"Command" : 'Command',
"Shift": "shift",
"Alt": "alt",
}
## Add a menu by adding and modifying the following data
const MenuData = {
"File": [
{
MenuKey.MenuName: "Open Project", # The name of the menu sub item
MenuKey.ScanCode: KEY_O, # Characters required O
MenuKey.Control: true, # need ctrl key
},
{
MenuKey.MenuName: "---"}, # Beginning with --- Is a separator
{
MenuKey.MenuName: "Exit",
MenuKey.ScanCode: KEY_E,
MenuKey.Control: true,
},
],
"Edit": [
{
MenuKey.MenuName: "Undo",
MenuKey.ScanCode: KEY_Z,
MenuKey.Control: true,
},
]
}
func _ready():
# Add menu button
for menu_name in MenuData.keys():
var menu = MenuButton.new()
menu.switch_on_hover = true
menu.text = menu_name
add_child(menu)
# Traverse menu data
var popup := menu.get_popup() as PopupMenu
var idx := -1
for item_data in MenuData[menu_name]:
idx += 1
# Menu name
var item_name := item_data[MenuKey.MenuName] as String
# Add separator
if item_name.begins_with('---'):
popup.add_separator(item_name.right(3))
continue
# Add menu item
popup.add_item(item_name)
# Set menu shortcuts
var shortcut = ShortCut.new()
var input = InputEventKey.new()
shortcut.shortcut = input
if item_data.has(MenuKey.ScanCode):
input.scancode = item_data[MenuKey.ScanCode]
if item_data.has(MenuKey.Control):
input.control = item_data[MenuKey.Control]
if item_data.has(MenuKey.Command):
input.command = item_data[MenuKey.Command]
if item_data.has(MenuKey.Alt):
input.alt = item_data[MenuKey.Alt]
if item_data.has(MenuKey.Shift):
input.shift = item_data[MenuKey.Shift]
popup.set_item_shortcut(idx, shortcut)
popup.connect("index_pressed", self, "_menu_index_pressed", [menu_name])
## Press menu item
func _menu_index_pressed(idx: int, menu_name: String) -> void:
# The data list of the corresponding menu
var menu_data := MenuData[menu_name] as Array
# The data of the corresponding menu item
var item_data := menu_data[idx] as Dictionary
# Menu name
var item_name := item_data[MenuKey.MenuName] as String
print(' Press down %s Menu %s term ' % [menu_name, item_name])
# according to item_name Executive function
# example :
match item_name:
'Open Project':
pass
'Exit':
get_tree().quit()

边栏推荐
- Codeforces Round #803 (Div. 2) C. 3SUM Closure
- On Data Mining
- PHP MySQL Update
- 图像24位深度转8位深度
- Mathematical formula (test)
- Bidding procurement scheme management of Oracle project management system
- Applet with multiple tabs and Swipers + paging of each tab
- Kotlin的协程:上下文
- Image 24 bit depth to 8 bit depth
- SSL / bio pour OpenSSL Get FD
猜你喜欢

Naoqi robot summary 27

How to analyze the rising and falling rules of London gold trend chart
![The number of incremental paths in the grid graph [dfs reverse path + memory dfs]](/img/57/ff494db248171253996dd6c9110715.png)
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
![网格图中递增路径的数目[dfs逆向路径+记忆dfs]](/img/57/ff494db248171253996dd6c9110715.png)
网格图中递增路径的数目[dfs逆向路径+记忆dfs]

What London Silver Trading software supports multiple languages

Redis on local access server

Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028

Five problems of database operation in commodity supermarket system

STM32 realizes 74HC595 control

Redis cache avalanche, penetration, breakdown
随机推荐
English grammar_ Noun classification
(9) Opencv Canny edge detection
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
毕业总结
模块九作业
The vscode code is automatically modified to a compliance code when it is formatted and saved
An academic paper sharing and approval system based on PHP for computer graduation design
[enumeration] annoying frogs always step on my rice fields: (who is the most hateful? (POJ hundred practice 2812)
Closure and closure function
Five problems of database operation in commodity supermarket system
Count the number of pixel values in the image
(8) HS corner detection
How to install PHP on Ubuntu 20.04
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
MySQL grouping query
Prototype inheritance..
SSL / bio pour OpenSSL Get FD
聊聊支付流程的設計與實現邏輯
Computer graduation design PHP makeup sales Beauty shopping mall
Life perception 1