当前位置:网站首页>[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()
边栏推荐
- Analysis report on production and marketing demand and investment forecast of China's PVC industry from 2021 to 2026
- Closure and closure function
- [LINUX]CentOS 7 安装MYSQL时报错“No package mysql-server available“No package zabbix-server-mysql availabl
- Global and Chinese health care OEM and ODM market status survey and investment planning recommendations report 2022-2028
- How to expand the capacity of golang slice slice
- Line by line explanation of yolox source code of anchor free series network (5) -- mosaic data enhancement and mathematical understanding
- [combinatorics] generating function (generating function application scenario | using generating function to solve recursive equation)
- Research on Swift
- Image 24 bits de profondeur à 8 bits de profondeur
- Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
猜你喜欢
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
Bloom filter [proposed by bloom in 1970; redis cache penetration solution]
AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
An academic paper sharing and approval system based on PHP for computer graduation design
聊聊支付流程的设计与实现逻辑
Sensor debugging process
MySQL grouping query
What kind of experience is it when the Institute earns 20000 yuan a month?
Talk about the design and implementation logic of payment process
Research Report on competitive strategy Outlook Analysis and investment strategic planning of China's smart home equipment industry, 2022-2028
随机推荐
Unsafe类的使用
Graduation summary
[combinatorics] generating function (positive integer splitting | unordered | ordered | allowed repetition | not allowed repetition | unordered not repeated splitting | unordered repeated splitting)
Kotlin的协程:上下文
Redis core technology and practice - learning notes (IX): slicing cluster
聊聊支付流程的设计与实现逻辑
Naoqi robot summary 27
On Data Mining
Gear2021 monthly update - December
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
[untitled]
Have you learned the correct expression posture of programmers on Valentine's day?
(9) Opencv Canny edge detection
[combinatorics] generating function (use generating function to solve the number of solutions of indefinite equation example 2 | extended to integer solution)
Talk about the design and implementation logic of payment process
Draw some simple graphics with MFC
English grammar_ Adjective / adverb Level 3 - multiple expression
ES7 - Optimization of promise
图像24位深度转8位深度
Kotlin's collaboration: Context