Skip to content

Instantly share code, notes, and snippets.

@brettchalupa
Last active December 23, 2022 20:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettchalupa/bd6b581f636d64000400b7cc85fcc295 to your computer and use it in GitHub Desktop.
Save brettchalupa/bd6b581f636d64000400b7cc85fcc295 to your computer and use it in GitHub Desktop.
DragonRuby Game Toolkit Menu Demo
# LICENSE: https://unlicense.org/ a.k.a. do whatever you want with it, no credit or notice needed
def tick(args)
args.state.scene ||= :main_menu
args.state.main_menu.options ||= [
{
text: "Start",
on_select: -> (args) { args.state.scene = :gameplay }
},
{
text: "Settings",
on_select: -> (args) { args.state.scene = :settings }
},
{
text: "Quit",
on_select: -> (args) { args.gtk.request_quit }
},
]
args.state.main_menu.current_option_i ||= 0
args.state.hold_delay ||= 0
labels = []
args.state.main_menu.options.each.with_index do |option, i|
size = 6
label = {
x: args.grid.w / 2, y: 360 + (args.state.main_menu.options.length - i * 52),
text: option[:text], size_enum: size, alignment_enum: 1
}
label_size = args.gtk.calcstringbox(label.text, size)
labels << label
if args.state.main_menu.current_option_i == i
args.outputs.solids << {
x: label.x - (label_size[0] / 1.4) - 24 + (Math.sin(args.state.tick_count / 8) * 4),
y: label.y - 22,
w: 16,
h: 16,
}
end
end
labels << {
x: args.grid.w / 2, y: args.grid.top - 100,
text: "Menu Demo", size_enum: 12, alignment_enum: 1
}
labels << {
x: args.grid.w / 2, y: args.grid.top - 180,
text: "Select option with Z", size_enum: 6, alignment_enum: 1
}
labels << {
x: args.grid.w / 2, y: args.grid.top - 220,
text: "Scene: #{args.state.scene}", size_enum: 4, alignment_enum: 1
}
args.outputs.labels << labels
move = nil
if args.inputs.down
move = :down
elsif args.inputs.up
move = :up
else
args.state.hold_delay = 0
end
if move
args.state.hold_delay -= 1
if args.state.hold_delay <= 0
index = args.state.main_menu.current_option_i
if move == :up
index -= 1
else
index += 1
end
if index < 0
index = args.state.main_menu.options.length - 1
elsif index > args.state.main_menu.options.length - 1
index = 0
end
args.state.main_menu.current_option_i = index
args.state.hold_delay = 10
end
end
if args.inputs.keyboard.key_down.z
args.state.main_menu.options[args.state.main_menu.current_option_i][:on_select].call(args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment