function init_pause_menu()
--if you declared your window somewhere else comment this line out and
--add pauseable=false to your window
window{pauseable=false,autoclose=true,capture_escapes=true}
--
game_paused=false
pause_menu={
title="Game Is Paused",
title_reminder="Game Is Paused",
items_and_actions={
{"Continue","BACK"},
{"Options",{{"<\16\16\16\16\16\16\16\16>","VOLUME"},{"Sound On","TOGGLE_AUDIO"},{"Return","BACK"}}},
{"Exit Game",{{"Go Back","BACK"},{"Really Exit","EXIT"}}},--comment this line if you don't want to exit accidentally
{"EXIT TO PICOTRON","DEV_EXIT"},--delete this for your exports
},
sounds={--comment this for no sfx or set this to your own sfx if you want UI sounds
blip=1,
select_item=2,
toggle_visibility=3,
},
current_item=0,
current_sub_menu=nil,
}
end
function update_pause_menu()
if game_paused then
if btnp(2) or btnp(2,1) or keyp("w") then
pause_menu.current_item-=1
if pause_menu.sounds then
sfx(pause_menu.sounds.blip)
end
elseif btnp(3) or btnp(3,1) or keyp("s") then
pause_menu.current_item+=1
if pause_menu.sounds then
sfx(pause_menu.sounds.blip)
end
elseif btnp(5) or btnp(5,1) or key("esc") then
do_pause_menu_action("BACK")
if pause_menu.sounds then
sfx(pause_menu.sounds.blip)
end
elseif btnp(1) or btnp(1,1) or btnp(0) or btnp(0,1) or keyp("a") or keyp("d") then
local action=pause_menu.items_and_actions[(pause_menu.current_item%#pause_menu.items_and_actions)+1][2]
if pause_menu.current_sub_menu then
action=pause_menu.current_sub_menu[(pause_menu.current_item%#pause_menu.current_sub_menu)+1][2]
end
if action=="VOLUME" then
if btnp(1) or btnp(1,1) or keyp("d") then
do_pause_menu_action("VOLUME_UP")
elseif btnp(0) or btnp(0,1) or keyp("a") then
do_pause_menu_action("VOLUME_DOWN")
end
end
elseif btnp(4) or btnp(4,1) or keyp("space") then
if pause_menu.current_sub_menu then
do_pause_menu_action(pause_menu.current_sub_menu[(pause_menu.current_item%#pause_menu.current_sub_menu)+1][2],pause_menu.current_sub_menu[(pause_menu.current_item%#pause_menu.current_sub_menu)+1][1])
else
do_pause_menu_action(pause_menu.items_and_actions[(pause_menu.current_item%#pause_menu.items_and_actions)+1][2],pause_menu.items_and_actions[(pause_menu.current_item%#pause_menu.items_and_actions)+1][1])
end
if pause_menu.sounds then
sfx(pause_menu.sounds.select_item)
end
end
end
if keyp("escape") or btnp(6) or btnp(6,1) then
if game_paused then
do_pause_menu_action("BACK")
else
game_paused=true
end
if pause_menu.sounds then
sfx(pause_menu.sounds.toggle_visibility)
end
end
end
function draw_pause_menu()
--remember to call camera() before this function to nullify your own offset
camera()
if game_paused then
local half_screen_height=get_display():height()/2
local half_screen_width=get_display():width()/2
local y_item_padding=15
local y_size=#pause_menu.items_and_actions*y_item_padding
local y_start=half_screen_height-y_size/2
local width=200
local x_start=half_screen_width-width/2
rectfill(x_start,half_screen_height-y_size/2,x_start+width,half_screen_height+y_size/2,1)
rect(x_start+1,half_screen_height-y_size/2+1,x_start+width-1,half_screen_height+y_size/2-1,7)
local length=print("\^w\^t\^o0ff"..pause_menu.title,0,-10000)
print("\^w\^t\^o0ff"..pause_menu.title,half_screen_width-(length/2),y_start-16,7)
local counter=0
if not pause_menu.current_sub_menu then
for i in all(pause_menu.items_and_actions) do
local length=print(i[1],0,-1000)
local pretext=""
if (pause_menu.current_item%#pause_menu.items_and_actions)+1==counter+1 then
pretext="\^odff"
end
print(pretext..i[1],half_screen_width-length/2,y_start+y_item_padding/4+1+counter*y_item_padding,7)
counter+=1
end
elseif pause_menu.current_sub_menu then
for i in all(pause_menu.current_sub_menu) do
local length=print(i[1],0,-1000)
local pretext=""
if (pause_menu.current_item%#pause_menu.current_sub_menu)+1==counter+1 then
pretext="\^odff"
end
print(pretext..i[1],half_screen_width-length/2,y_start+y_item_padding/4+1+counter*y_item_padding,7)
counter+=1
end
end
print("\131\148 or W S to navigate \142 or SPACEBAR to choose \151 or ESC to exit",half_screen_width-165,get_display():height()-8,7)
end
end
function do_pause_menu_action(what,menuitem_string)
if type(what)=="table" then
pause_menu.current_sub_menu=what
pause_menu.title=menuitem_string
pause_menu.current_item=0
elseif what=="BACK" then
if pause_menu.current_sub_menu then
pause_menu.current_sub_menu=nil
pause_menu.current_item=0
for i=1,#pause_menu.items_and_actions do
if pause_menu.items_and_actions[i][1]==pause_menu.title then
pause_menu.current_item=i-1
end
end
else
game_paused=false
end
pause_menu.title=pause_menu.title_reminder
elseif what=="VOLUME_UP" or what=="VOLUME_DOWN" then
--determine current volume
local cur_volume=peek(0x5538,2)
if what=="VOLUME_UP" and cur_volume<64 then
cur_volume=cur_volume+8
elseif what=="VOLUME_DOWN" and cur_volume>0 then
cur_volume=cur_volume-8
end
if pause_menu.sounds then
sfx(pause_menu.sounds.select_item)
end
poke(0x5538 ,cur_volume)
poke(0x5539 ,cur_volume)
--visually represent the change and change the menu items label
local new_label="<"
for i=1,8 do
if cur_volume/8>=i then
new_label=new_label.."\16"
else
new_label=new_label.."-"
end
end
new_label=new_label..">"
if pause_menu.current_sub_menu then
pause_menu.current_sub_menu[(pause_menu.current_item%#pause_menu.current_sub_menu)+1][1]=new_label
else
pause_menu.items_and_actions[(pause_menu.current_item%#pause_menu.items_and_actions)+1][1]=new_label
end
elseif what=="TOGGLE_AUDIO" then
--poke the global volume setting for both sfx and music (0x40==1.0-->0x00==0.0)
current_volume=peek(0x5538,2)
local new_label="Sound Off"
if current_volume==0 then
poke(0x5538 ,0x40)
poke(0x5539 ,0x40)
new_label="Sound On"
else
poke(0x5538 ,0x00)
poke(0x5539 ,0x00)
end
--visually represent the change and change the menu items label
if pause_menu.current_sub_menu then
pause_menu.current_sub_menu[(pause_menu.current_item%#pause_menu.current_sub_menu)+1][1]=new_label
else
pause_menu.items_and_actions[(pause_menu.current_item%#pause_menu.items_and_actions)+1][1]=new_label
end
elseif what=="DEV_EXIT" then
exit()
elseif what=="EXIT" then
send_message(2, {event="shutdown"})
end
end