Skip to content

Instantly share code, notes, and snippets.

@quietsamurai98
Created November 6, 2020 17:01
Show Gist options
  • Save quietsamurai98/d7cd8b5d63480488cee8a2247c8a381a to your computer and use it in GitHub Desktop.
Save quietsamurai98/d7cd8b5d63480488cee8a2247c8a381a to your computer and use it in GitHub Desktop.
Right click drop down menu system
def tick args
args.state.menu ||= [
{
label: "Place black square",
action: lambda {args.outputs.static_solids << [args.state.rmb_click.x-10, args.state.rmb_click.y-10, 20, 20, 0, 0, 0, 255]}
},
{
label: "Place red square",
action: lambda {args.outputs.static_solids << [args.state.rmb_click.x-10, args.state.rmb_click.y-10, 20, 20, 255, 0, 0, 255]}
},
{
label: "Place green square",
action: lambda {args.outputs.static_solids << [args.state.rmb_click.x-10, args.state.rmb_click.y-10, 20, 20, 0, 255, 0, 255]}
},
{
label: "Place blue square",
action: lambda {args.outputs.static_solids << [args.state.rmb_click.x-10, args.state.rmb_click.y-10, 20, 20, 0, 0, 255, 255]}
},
{
label: "Clear squares",
action: lambda {args.outputs.static_solids.clear}
}
]
if args.inputs.mouse.click && args.state.menu_open
args.state.menu_open = false
args.state.menu.each do |item|
mouse_rect = args.inputs.mouse.point
if mouse_rect.intersect_rect? item[:bbox]
item[:action].()
end
end
end
if args.state.rmb_last && !args.inputs.mouse.button_right
args.state.menu_open = true
args.state.rmb_click = args.inputs.mouse.point
end
if args.state.menu_open
draw_menu args.state.menu, args
end
args.state.rmb_last = args.inputs.mouse.button_right
end
def draw_menu menu, args
menu_width, menu_height = 0, 0
font_size = -1
menu.each do |item|
w, h = args.gtk.calcstringbox(item[:label], font_size, "font.ttf")
menu_width = menu_width.greater w
menu_height += h
end
menu_width+=10
menu_height+=10*menu.length
x_off = 0
y_off = 5
unless 1280 > (args.state.rmb_click.x+menu_width)
x_off = 1280-(args.state.rmb_click.x+menu_width)
end
unless 0 < (args.state.rmb_click.y-menu_height)
y_off += (args.state.rmb_click.y-menu_height)-15
end
args.outputs.solids << [args.state.rmb_click.x+x_off, args.state.rmb_click.y-menu_height-10-y_off, menu_width, menu_height+10, 255, 255, 255]
menu.each do |item|
w, h = args.gtk.calcstringbox(item[:label], 0, "font.ttf")
args.outputs.labels << [args.state.rmb_click.x+5+x_off, args.state.rmb_click.y-y_off-5, item[:label], font_size]
y_off += h + 10
args.outputs.borders << [args.state.rmb_click.x+x_off, args.state.rmb_click.y-y_off, menu_width, h+11]
item[:bbox] = [args.state.rmb_click.x+x_off, args.state.rmb_click.y-y_off, menu_width, h+11]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment