From f3fb485c228b7273a331dd5bc8c971faab1df641 Mon Sep 17 00:00:00 2001 From: Radamanthus Batnag Date: Mon, 29 Aug 2011 15:10:03 +0800 Subject: [PATCH] Added init_buttons and deprecated the menu generator --- generate.rb | 129 ----------------------------------------------- init_buttons.lua | 55 ++++++++++++++++++++ loadmenu.lua | 7 +-- menu.json | 34 ------------- menu.lua | 98 +++++++++++++---------------------- 5 files changed, 91 insertions(+), 232 deletions(-) delete mode 100644 generate.rb create mode 100644 init_buttons.lua delete mode 100644 menu.json diff --git a/generate.rb b/generate.rb deleted file mode 100644 index 7b50ee9..0000000 --- a/generate.rb +++ /dev/null @@ -1,129 +0,0 @@ -require 'json' - -MENU_CODE = %{ - local #Button = nil - local function # ( event ) - if event.phase == "release" and #Button.isActive then - # - end - end - #Button = ui.newButton{ - defaultSrc = "btn-#.png", - defaultX = #, - defaultY = #, - overSrc = "btn-#-over.png", - overX = #, - overY = #, - onEvent = #, - id = "#Button", - text = "", - font = "Helvetica", - textColor = { 255, 255, 255, 255 }, - emboss = false - } - #Button.x = # - #Button.y = # - #Button.isActive = true - localGroup:insert(#Button) - -} - -def generate_menu_code_for(menu_item) - button_transition = menu_item['transition'][0] - button_transition_options = menu_item['transition'][1..-1] - if button_transition_options.empty? - change_scene_code = %{director:changeScene("#{menu_item['name']}", "#{button_transition}")} - else - transition_options = button_transition_options.collect do |o| - begin - Float(o) - rescue - %{"#{o}"} - end - end.join(',') - change_scene_code = %{director:changeScene("#{menu_item['name']}", "#{button_transition}", #{transition_options})} - end - menu_code = MENU_CODE.clone - menu_code.gsub!('#', menu_item['name']) - menu_code.gsub!('#', "on#{menu_item['name'].capitalize}") - menu_code.gsub!('#', menu_item['x'].to_s) - menu_code.gsub!('#', menu_item['y'].to_s) - menu_code.gsub!('#', menu_item['width'].to_s) - menu_code.gsub!('#', menu_item['height'].to_s) - menu_code.gsub!('#', change_scene_code) - menu_code -end - -# Load the template -template = DATA.readlines - -# Read the menu.txt file -menu_file = File.open('menu.json', 'r') -menu = JSON.parse(menu_file.readlines.join(' ')) - -# Generate the screen .lua file -menu.each do |screen| - screen_file = File.new("#{screen['name']}.lua", 'w+') - template.each do |l| - screen_file.print l - end - screen_file.close -end - -# Update menu.lua -# Read the entire file into memory, insert the generated code, then overwrite the existing menu.lua -menu_lua_file = File.open('menu.lua', 'r+') -menu_lua_file_lines = menu_lua_file.readlines -menu_lua_file.close -menu_lua_file = File.open('menu.lua', 'w+') -inside_menu_section = false -menu_lua_file_lines.each do |line| - menu_lua_file.print line unless inside_menu_section - if line.include?("-- Menu Buttons - Start") # Look for the start markup - inside_menu_section = true - menu.each do |menu_item| - menu_lua_file.print generate_menu_code_for menu_item - end - end - if line.include?("-- Menu Buttons - End") - menu_lua_file.print line - inside_menu_section = false - end -end -menu_lua_file.close - -__END__ -module(..., package.seeall) - --- Main function - MUST return a display.newGroup() -function new() - local localGroup = display.newGroup() - - -- Background - local background = display.newImageRect("bk-default.png", 480, 320) - background.x = display.contentCenterX - background.y = display.contentCenterY - localGroup:insert(background) - - -- Title - local title = display.newText("Touch to go back", 0, 0, native.systemFontBold, 16) - title:setTextColor( 255,255,255) - title.x = display.contentCenterX - title.y = display.contentCenterY - title.name = "title" - localGroup:insert(title) - - -- Touch to go back - local function touched ( event ) - if ("ended" == event.phase) then - director:changeScene("menu","fade") - end - end - background:addEventListener("touch",touched) - - unloadMe = function() - end - - -- MUST return a display.newGroup() - return localGroup -end diff --git a/init_buttons.lua b/init_buttons.lua new file mode 100644 index 0000000..6751de6 --- /dev/null +++ b/init_buttons.lua @@ -0,0 +1,55 @@ +_G.buttons = { + about = { + defaultSrc = "btn_about.png", + defaultX = 160, + defaultY = 32, + overSrc = "btn_about_over.png", + overX = 160, + overY = 32, + id = "btnAbout", + text = "", + font = "Helvetica", + textColor = { 255, 255, 255, 255 }, + emboss = false + }, + help = { + defaultSrc = "btn_help.png", + defaultX = 160, + defaultY = 32, + overSrc = "btn_help_over.png", + overX = 160, + overY = 32, + id = "btnHelp", + text = "", + font = "Helvetica", + textColor = { 255, 255, 255, 255 }, + emboss = false + }, + play = { + defaultSrc = "btn_play.png", + defaultX = 160, + defaultY = 32, + overSrc = "btn_play_over.png", + overX = 160, + overY = 32, + id = "btnPlay", + text = "", + font = "Helvetica", + textColor = { 255, 255, 255, 255 }, + emboss = false + }, + settings = { + defaultSrc = "btn_settings.png", + defaultX = 160, + defaultY = 32, + overSrc = "btn_settings_over.png", + overX = 160, + overY = 32, + id = "btnSettings", + text = "", + font = "Helvetica", + textColor = { 255, 255, 255, 255 }, + emboss = false + } +} + diff --git a/loadmenu.lua b/loadmenu.lua index 526aa41..1f004e6 100644 --- a/loadmenu.lua +++ b/loadmenu.lua @@ -1,10 +1,7 @@ --- --- adapted from Ghosts Vs Monsters sample project --- (see http://blog.anscamobile.com/2010/12/ghosts-vs-monsters-open-source-game-in-corona-sdk/) --- - module(..., package.seeall) +require "init_buttons" + -- Main function - MUST return a display.newGroup() function new() local localGroup = display.newGroup() diff --git a/menu.json b/menu.json deleted file mode 100644 index 5b82d24..0000000 --- a/menu.json +++ /dev/null @@ -1,34 +0,0 @@ -[ - { - "name":"play", - "x":160, - "y":80, - "width":160, - "height":32, - "transition":["fade", 30, 60, 90] - }, - { - "name":"settings", - "x":160, - "y":130, - "width":160, - "height":32, - "transition":["fade", "green"] - }, - { - "name":"help", - "x":160, - "y":180, - "width":160, - "height":32, - "transition":["overFromTop"] - }, - { - "name":"about", - "x":160, - "y":230, - "width":160, - "height":32, - "transition":["moveFromLeft"] - } -] \ No newline at end of file diff --git a/menu.lua b/menu.lua index 8fd813b..2110bb4 100644 --- a/menu.lua +++ b/menu.lua @@ -1,5 +1,7 @@ module(..., package.seeall) +local radlib = require "radlib" + -- Main function - MUST return a display.newGroup() function new() local ui = require("ui") @@ -15,25 +17,17 @@ function new() -- Menu Buttons - Start local playButton = nil - local function onPlay ( event ) - if event.phase == "release" and playButton.isActive then + local function onPlayPressed ( event ) + if event.phase == "ended" and playButton.isActive then director:changeScene("play", "fade", 30.0,60.0,90.0) end end - playButton = ui.newButton{ - defaultSrc = "btn_play.png", - defaultX = 160, - defaultY = 32, - overSrc = "btn_play_over.png", - overX = 160, - overY = 32, - onEvent = onPlay, - id = "playButton", - text = "", - font = "Helvetica", - textColor = { 255, 255, 255, 255 }, - emboss = false - } + playButton = ui.newButton( + radlib.tableMerge( + _G.buttons['play'], + { onRelease = onPlayPressed } + ) + ) playButton.x = 160 playButton.y = 80 playButton.isActive = true @@ -41,25 +35,17 @@ function new() local settingsButton = nil - local function onSettings ( event ) - if event.phase == "release" and settingsButton.isActive then + local function onSettingsPressed ( event ) + if event.phase == "ended" and settingsButton.isActive then director:changeScene("settings", "fade", "green") end end - settingsButton = ui.newButton{ - defaultSrc = "btn_settings.png", - defaultX = 160, - defaultY = 32, - overSrc = "btn_settings_over.png", - overX = 160, - overY = 32, - onEvent = onSettings, - id = "settingsButton", - text = "", - font = "Helvetica", - textColor = { 255, 255, 255, 255 }, - emboss = false - } + settingsButton = ui.newButton( + radlib.tableMerge( + _G.buttons['settings'], + { onRelease = onSettingsPressed } + ) + ) settingsButton.x = 160 settingsButton.y = 130 settingsButton.isActive = true @@ -67,25 +53,17 @@ function new() local helpButton = nil - local function onHelp ( event ) - if event.phase == "release" and helpButton.isActive then + local function onHelpPressed ( event ) + if event.phase == "ended" and helpButton.isActive then director:changeScene("help", "overFromTop") end end - helpButton = ui.newButton{ - defaultSrc = "btn_help.png", - defaultX = 160, - defaultY = 32, - overSrc = "btn_help_over.png", - overX = 160, - overY = 32, - onEvent = onHelp, - id = "helpButton", - text = "", - font = "Helvetica", - textColor = { 255, 255, 255, 255 }, - emboss = false - } + helpButton = ui.newButton( + radlib.tableMerge( + _G.buttons['help'], + { onRelease = onHelpPressed } + ) + ) helpButton.x = 160 helpButton.y = 180 helpButton.isActive = true @@ -93,25 +71,17 @@ function new() local aboutButton = nil - local function onAbout ( event ) - if event.phase == "release" and aboutButton.isActive then + local function onAboutPressed ( event ) + if event.phase == "ended" and aboutButton.isActive then director:changeScene("about", "moveFromLeft") end end - aboutButton = ui.newButton{ - defaultSrc = "btn_about.png", - defaultX = 160, - defaultY = 32, - overSrc = "btn_about_over.png", - overX = 160, - overY = 32, - onEvent = onAbout, - id = "aboutButton", - text = "", - font = "Helvetica", - textColor = { 255, 255, 255, 255 }, - emboss = false - } + aboutButton = ui.newButton( + radlib.tableMerge( + _G.buttons['about'], + { onRelease = onAboutPressed } + ) + ) aboutButton.x = 160 aboutButton.y = 230 aboutButton.isActive = true