-- Offset of list and header from other store layouts
local REWARDS_OFFSET = 80
local REWARDS_LIST_WIDTH = 0
-- Hint bar and store header vdo's.
local Hint_bar
local Hint_bar_rotate
local Store_logo
local Store_popup
local Color_grid
local Color_grid_grp
local Not_popup_grp
local Reward_image
local Hint_bar_mouse_input_tracker
local Cell_rewards_loading_img = nil
local Cell_rewards_unload_img = nil
local Cell_rewards_category_id = 0
local Cell_rewards_category_name = nil
local Cell_rewards_doc_handle = -1
local Cell_rewards_building_menu = {}
local Cell_rewards_img_update_thread = -1
---------------------------------------------------------------------------
-- Initialize Rewards Store
---------------------------------------------------------------------------
function cell_rewards_init()
Cell_rewards_doc_handle = vint_document_find("cell_rewards")
-- Set up some callbacks for the common store script
Store_common_populate_list_cb = cell_rewards_populate_list
Store_common_exit_cb = cell_rewards_exit
-- Subscribe to the button presses we need
Input_tracker:remove_input("map")
Input_tracker:add_input("map", "cell_rewards_input_map", 50)
Input_tracker:subscribe(false)
--Hide locked grp
local elements_locked_grp_h = vint_object_find("elements_locked_grp", 0, Cell_rewards_doc_handle)
vint_set_property(elements_locked_grp_h, "visible", false)
--Set up title
local cell_title_h = Vdo_cell_title:new("cell_title", 0, Cell_rewards_doc_handle)
cell_title_h:set_text("STORE_UPGRADES_TITLE")
cell_title_h:set_icon("ui_cell_icon_rewards")
cell_title_h:play_dots_anim()
--Setup Button Hints
Hint_bar = Vdo_hint_bar:new("hint_bar", 0, Store_common_doc_handle)
local hint_data = {
{CTRL_MENU_BUTTON_B, "MENU_BACK"},
}
Hint_bar:set_hints(hint_data)
Hint_bar:set_visible(true)
Store_header:set_color(COLOR_STORE_REWARDS_PRIMARY, COLOR_STORE_REWARDS_SECONDARY, COLOR_STORE_REWARDS_TERTIARY)
Store_header:set_visible(true)
Active_list = Vdo_mega_list:new("list_1", 0, Store_common_doc_handle, "store_common.lua", "Active_list")
Store_logo = Vdo_base_object:new("store_logo_img", 0, Store_common_doc_handle)
-- Everything that is not in the popup so we can fade it when the popup is active
Not_popup_grp = Vdo_base_object:new("not_popup_grp", 0, Store_common_doc_handle)
-- Setup purchase popup
Store_popup = Vdo_store_popup:new("store_popup", 0, Store_common_doc_handle)
Store_popup:set_visible(false)
Store_popup:set_color(COLOR_STORE_REWARDS_PRIMARY, COLOR_STORE_REWARDS_SECONDARY, COLOR_STORE_REWARDS_TERTIARY)
Store_popup:set_size(STORE_COMMON_LIST_SIZE, (LIST_BUTTON_HEIGHT * 5) + (LIST_BUTTON_SPACE * 5))
Color_grid_grp = Vdo_base_object:new("color_grid_grp", 0, Store_common_doc_handle)
Color_grid_grp:set_visible(false)
-- Needs to be done after the list is drawn/populated
Active_list:set_highlight_color(COLOR_STORE_REWARDS_PRIMARY, COLOR_STORE_REWARDS_SECONDARY)
local rewards_grp = Vdo_base_object:new("rewards_grp", 0, Cell_rewards_doc_handle)
rewards_grp:set_visible(true)
-- Pass the table data to the megalist vdo.
vint_dataresponder_request("cell_rewards_category_dr", "cell_rewards_add_category", 0)
cell_rewards_populate_list(Menu_data, 1)
-- Add this on_enter callback so the category menu gets refreshed when returning from a submenu
Menu_data.on_enter = cell_rewards_category_menu_update
-- For the rewards store, we don't want the bg to render behind the player, so change this property.
-- Perhaps these can be organized so it's easier to do this.
local grp1_h = vint_object_find("lets_pretend_bg_grp", 0, Store_common_doc_handle)
vint_set_property(grp1_h, "background", false)
vint_set_property(grp1_h, "visible", false)
-- Animate phone in
local from_phone_anim_h = vint_object_find("rewards_anim_in", 0, Store_common_doc_handle)
lua_play_anim(from_phone_anim_h, 0, Store_common_doc_handle)
-- Iniitialize reward image...
Reward_image = Vdo_bitmap_viewer:new("reward_image", 0, Cell_rewards_doc_handle)
-- Hide description
Reward_image:set_description(" ", nil)
Reward_image:set_visible(false)
-- Make sure our cellphone frame is in view, sometimes we could come straight into this menu...
cell_show_frame()
-- Mouse input
if game_get_platform() == "PC" then
Hint_bar:set_highlight(0)
Hint_bar_mouse_input_tracker = Vdo_input_tracker:new()
Hint_bar:add_mouse_inputs("cell_rewards", Hint_bar_mouse_input_tracker)
Hint_bar_mouse_input_tracker:subscribe(true)
end
--Transition the screen in...
if Rewards_from_menu then
cell_transition_screen(CELL_STATE_LANDSCAPE, CELL_SCREEN_REWARDS, CELL_SCREEN_MAIN, cell_rewards_unlock_controls)
else
--fade out header from the foreground...
cell_foreground_fade_out_header()
cell_transition_screen(CELL_STATE_LANDSCAPE, CELL_SCREEN_REWARDS, CELL_SCREEN_NONE, cell_rewards_unlock_controls)
--Reset our rewards from menu flag, this is a quick fix so back button functions as normal,
-- even if we went into the screen via (press back to upgrade). JMH 7/3/201)
Rewards_from_menu = true
end
--glitch
glitch_cell()
end
-- Cleanup
function cell_rewards_cleanup()
-- Nuke all button subscriptions
Input_tracker:subscribe(false)
cell_rewards_enable_mouse(false)
local current_image = Reward_image:get_image()
if current_image ~= nil then
game_peg_unload(current_image)
end
if Cell_rewards_unload_img ~= nil then
game_peg_unload(Cell_rewards_unload_img)
end
if Cell_rewards_loading_img ~= nil then
game_peg_unload(Cell_rewards_loading_img)
end
end
-------------------------------------------------------------------------------
-- Controls Unlock
--
function cell_rewards_unlock_controls()
Input_tracker:subscribe(true)
cell_rewards_enable_mouse(true)
end
-------------------------------------------------------------------------------
-- Controls Lock
--
function cell_rewards_lock_controls()
Input_tracker:subscribe(false)
cell_rewards_enable_mouse(false)
end
-- Populate the active megalist menu, and hide the previous menu
--
-- list_data: this table contains the data to populate the megalist
-- current_index: the current index in the menu that's selected
--
function cell_rewards_populate_list(list_data, current_index)
Active_list:set_scale_text_if_too_big(true)
Active_list:draw_items(list_data, current_index, STORE_COMMON_LIST_SIZE, 9, LIST_FONT_SCALE)--12, .8)
Active_list:set_visible(true)
if game_get_platform() == "PC" then
Mouse_input_tracker:remove_all()
Active_list:set_store("cell_rewards")
Active_list:add_mouse_inputs("store_common", Mouse_input_tracker)
Mouse_input_tracker:subscribe(true)
end
end
-- Called in a thread to delay the image updating.
-- This is done for 2 reasons: to slow down rapid scrolling updates done too quickly,
-- and because the streaming system didn't always co-operate with the images being released
-- from the callback context that was executed when another stream load completed.
--
function cell_rewards_image_update()
delay(0.2)
game_peg_unload(Cell_rewards_unload_img)
game_peg_load_with_cb("cell_rewards_show_image", 1, Cell_rewards_loading_img)
Cell_rewards_img_update_thread = -1
end
-- Callback for when an image is done loading.
-- SEH: It might make sense to roll this into vdo_bitmap_viewer...
--
function cell_rewards_show_image()
local index = Active_list:get_selection()
local new_image = Menu_data[index].image
local img_scale_x = .68
local img_scale_y = .68
if Cell_rewards_loading_img == new_image then
Reward_image:set_image(new_image, Menu_data[index].is_locked, Menu_data[index].is_purchased, Menu_data[index].level, Menu_data[index].level_required, img_scale_x, img_scale_y, Menu_data[index].gameplay_gate)
Reward_image:set_description(nil, Menu_data[index].description)
Reward_image:set_visible(true)
Cell_rewards_loading_img = nil
else
-- a new image was picked while we were loading - load this now, and unload the one we just loaded
if new_image ~= nil then
Cell_rewards_unload_img = Cell_rewards_loading_img
Cell_rewards_loading_img = new_image
Cell_rewards_img_update_thread = thread_new("cell_rewards_image_update")
else
game_peg_unload(Cell_rewards_loading_img)
Cell_rewards_loading_img = nil
end
end
end
-- Update reward image based on which reward is highlighted
--
-- index of the reward in the table
--
function cell_rewards_item_nav(menu_data)
Reward_image:set_visible(false)
if Cell_rewards_loading_img == nil then
local new_image = menu_data.image
local current_image = Reward_image:get_image()
if current_image ~= nil then
game_peg_unload(current_image)
end
if new_image ~= nil then
-- must set Cell_rewards_loading_img before calling game_peg_load_with_cb(), which could fire the callback right away
Cell_rewards_loading_img = new_image
game_peg_load_with_cb("cell_rewards_show_image", 1, new_image)
end
end
-- if this unlockable was marked as "new", then clear the flag since the player highlighted it
if menu_data.is_new then
vint_dataresponder_post("cell_rewards_reward_dr", "Clear_new", menu_data.id)
--Remove our mouse inputs first...
if Mouse_input_tracker ~= 0 then
Mouse_input_tracker:remove_all()
end
--Get index from the list and then remove the flag...
local index = Active_list:get_index_from_id(menu_data.id)
if index ~= -1 then
Active_list:remove_new_flag(index)
end
--Update cursor,
Active_list:move_cursor(0)
--Re add mouse inputs...
if Mouse_input_tracker ~= 0 then
Mouse_input_tracker:remove_all()
Active_list:add_mouse_inputs("store_common", Mouse_input_tracker)
Mouse_input_tracker:subscribe(true)
end
end
Store_header:set_price(menu_data.price, menu_data.is_orb)
Store_header:set_level(menu_data.level_required, menu_data.is_orb)
if menu_data.is_orb == true then
Store_header:set_cash(menu_data.orbs_collected, menu_data.is_orb)
else
Store_header:set_cash(Store_common_player_cash, menu_data.is_orb)
end
--Reward_image:set_level_required(Menu_data[index].level_required)
--------------------
--Locked by gameplay
--------------------
local elements_locked_grp_h = vint_object_find("elements_locked_grp", 0, Cell_rewards_doc_handle)
if menu_data.gameplay_gate ~= nil and menu_data.gameplay_gate ~= "" then
local elements_locked_txt_h = vint_object_find("elements_locked_txt", 0, Cell_rewards_doc_handle)
local elements_locked_bg_h = vint_object_find("elements_locked_bg", 0, Cell_rewards_doc_handle)
local elements_locked_trim_h = vint_object_find("elements_locked_trim", 0, Cell_rewards_doc_handle)
local elements_locked_trim2_h = vint_object_find("elements_locked_trim2", 0, Cell_rewards_doc_handle)
local elements_locked_string = ""
if menu_data.gameplay_gate == "quest" then
elements_locked_string = "UPGRADES_LOCKED_QUEST"
elseif menu_data.gameplay_gate == "challenge" then
elements_locked_string = "UPGRADES_LOCKED_CHALLENGE"
end
--show and format locked text
vint_set_property(elements_locked_txt_h, "text_tag", elements_locked_string)
local width, height = element_get_actual_size(elements_locked_txt_h)
element_set_actual_size(elements_locked_bg_h, width + 24, height + 14)
element_set_actual_size(elements_locked_trim_h, width + 24, 2)
element_set_actual_size(elements_locked_trim2_h, width + 24, 2)
vint_set_property(elements_locked_trim_h, "anchor", 0, -(height/2) - 7)
vint_set_property(elements_locked_trim2_h, "anchor", 0, (height/2) + 7)
vint_set_property(elements_locked_grp_h, "visible", true)
else
vint_set_property(elements_locked_grp_h, "visible", false)
end
end
-- Update category image based on which category is highlighted
--
-- index of the category in the table
--
function cell_rewards_category_nav(menu_data)
Cell_rewards_category_id = menu_data.id
Cell_rewards_category_name = menu_data.label
Reward_image:set_visible(false)
-- SEH - for now, we're not going to show any category images since they aren't ready. Uncomment following code when we are.
-- if Cell_rewards_loading_img == nil then
-- local new_image = Menu_data[index].image
-- local current_image = Reward_image:get_image()
-- if current_image ~= nil then
-- game_peg_unload(current_image)
-- end
-- if new_image ~= nil then
-- -- must set Cell_rewards_loading_img before calling game_peg_load_with_cb(), which could fire the callback right away
-- Cell_rewards_loading_img = new_image
-- game_peg_load_with_cb("cell_rewards_show_image", 1, new_image)
-- end
-- end
end
function cell_rewards_purchase_select(menu_data)
if menu_data.is_locked == true or menu_data.is_purchased == true then
return
end
cell_rewards_enable_mouse(false)
-- Are we short on cash/orbs?
if menu_data.is_orb == true then
if menu_data.price > menu_data.orbs_collected then
Store_popup:populate_list(Ok_choice, 1, STORE_COMMON_LIST_SIZE, 1)
Store_popup:set_title("MENU_TITLE_NOTICE")
Store_popup:set_text("HUD_SHOP_INSUFFICIENT_ORBS")
Store_popup:nav_enable(true, "cell_rewards_fail_msg", "cell_rewards_popup_nav")
Not_popup_grp:set_alpha(.5)
Active_list:set_visible(false)
return
end
else
if menu_data.price > Store_common_player_cash then
Store_popup:populate_list(Ok_choice, 1, STORE_COMMON_LIST_SIZE, 1)
Store_popup:set_title("MENU_TITLE_NOTICE")
Store_popup:set_text("HUD_SHOP_INSUFFICIENT_FUNDS")
Store_popup:nav_enable(true, "cell_rewards_fail_msg", "cell_rewards_popup_nav")
Not_popup_grp:set_alpha(.5)
Active_list:set_visible(false)
return
end
end
-- Make sure they want to buy this
Store_popup:populate_list(Yes_no_choices, 1, STORE_COMMON_LIST_SIZE, 2)
Store_popup:set_title("STORE_TITLE_PURCHASING")
Store_popup:set_text("STORE_TEXT_CONFIRM_PURCHASE_BLANK")
Store_popup:nav_enable(true, "cell_rewards_purchase_final", "cell_rewards_popup_nav")
Not_popup_grp:set_alpha(.5)
Active_list:set_visible(false)
end
function cell_rewards_popup_nav(event, value)
if event == "nav_up" then
Store_popup.list:move_cursor(-1)
elseif event == "nav_down" then
Store_popup.list:move_cursor(1)
elseif event == "mouse_move" then
-- value contains the target_handle in this case
local new_index = Store_popup.list:get_button_index(value)
if new_index ~= 0 then
Store_popup.list:set_selection(new_index)
Store_popup.list:move_cursor(0, true)
end
else
--do nothing
end
end
function cell_rewards_fail_msg(event)
Store_popup:nav_enable(false, nil, nil)
Not_popup_grp:set_alpha(1)
Active_list:set_visible(true)
cell_rewards_enable_mouse(true)
end
-- Finalize or cancel a purchase based on the result of the confirmation question.
--
-- response: how did the user respond?
-- action: indicates if the dialog box was closed or some other action.
--
function cell_rewards_purchase_final(event)
Store_popup:nav_enable(false, nil, nil)
Not_popup_grp:set_alpha(1)
Active_list:set_visible(true)
cell_rewards_enable_mouse(true)
-- The user hit the B button and are cancelling the purchase
-- This is redundant to selecting "No" in the list
if event == "back" then
--Play menu back audio
ui_audio_post_event("UI_Hub_Menu_Back")
return
end
--Play menu forward audio
ui_audio_post_event("UI_Hub_Menu_Forward")
-- Did we select yes? This assumes yes is the first item
if Store_popup:get_selected_data() ~= 1 then
return
end
-- give reward and take money here
local current_index = Active_list:get_selection()
vint_dataresponder_post("cell_rewards_reward_dr", "Purchase", Menu_data[current_index].id)
-- do this so holding down the button doesn't cause repeated actions
Input_tracker:subscribe(false)
-- Save off index before we refresh the list
Menu_data.start_index = current_index
end
function cell_rewards_add_category(name, image_name, is_new, disabled)
local menu_idx = #Menu_data + 1
local new_item = {
type = TYPE_BUTTON,
id = menu_idx - 1,
label = name,
label_crc = nil,
image = image_name,
on_nav = cell_rewards_category_nav,
on_sub_menu_fill = cell_rewards_populate_rewards,
is_new = is_new,
disabled = disabled,
}
Menu_data[menu_idx] = new_item
end
-- This is called after the sub menu is generated for a selected category, and we now need to update
-- the image and description for the first award available. Must be called in on_enter(), because
-- the script asssumes Menu_data has already been set to the new sub menu we entered.
--
function cell_rewards_on_enter_draw_image()
cell_rewards_item_nav(Menu_data[1])
end
function cell_rewards_populate_rewards(menu_data)
Cell_rewards_building_menu = menu_data.sub_menu
vint_dataresponder_request("cell_rewards_reward_dr", "cell_rewards_add_reward", 0, Cell_rewards_category_id)
Cell_rewards_building_menu.on_enter = cell_rewards_on_enter_draw_image
end
--gameplay_gate will be either...
--""
--"quest"
--"challenge"
function cell_rewards_add_reward(name_crc, desc_crc, image_name, is_locked, is_purchased, price, id, level_required, trumps, is_new, is_orb, orbs_collected, gameplay_gate)
local menu_idx = #Cell_rewards_building_menu + 1
--Levels always displayed to the user as +1... (JMH 6/9/2011)
level_required = level_required + 1
local new_item = {
type = TYPE_BUTTON,
id = id,
label = nil,
label_crc = name_crc,
image = image_name,
description = desc_crc,
is_locked = is_locked,
is_purchased = is_purchased,
price = price,
-- Display of level and max_level disabled for now
level = nil,
max_level = nil,
level_required = level_required,
on_nav = cell_rewards_item_nav,
on_select = cell_rewards_purchase_select,
on_back = cell_rewards_revert_header,
trumps = trumps, -- CRC for the name of the prerequisite
is_new = is_new,
is_orb = is_orb,
orbs_collected = orbs_collected,
gameplay_gate = gameplay_gate,
}
Cell_rewards_building_menu[menu_idx] = new_item
end
function cell_rewards_revert_header(menu_data)
-- kill update thread
if Cell_rewards_img_update_thread ~= -1 then
thread_kill(Cell_rewards_img_update_thread)
Cell_rewards_img_update_thread = -1
end
Store_header:set_price(nil)
Store_header:set_level(nil)
Store_header:set_cash(Store_common_player_cash)
-- Hide description
Reward_image:set_description(" ", nil)
Reward_image:set_visible(false)
local current_image = Reward_image:get_image()
if current_image ~= nil then
game_peg_unload(current_image)
end
if Cell_rewards_unload_img ~= nil then
game_peg_unload(Cell_rewards_unload_img)
end
if Cell_rewards_loading_img ~= nil then
game_peg_unload(Cell_rewards_loading_img)
Cell_rewards_loading_img = nil
end
end
function cell_rewards_input_map()
Rewards_from_menu = false
cell_rewards_exit()
end
function cell_rewards_exit(event)
-- exit the interface
cell_rewards_lock_controls()
-- Animate reward screen out
local anim_out_h = vint_object_find("rewards_anim_out", 0, Store_common_doc_handle)
lua_play_anim(anim_out_h)
if Rewards_from_menu then
--Transition to cell main
cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_REWARDS, cell_rewards_exit_to_main)
--Play menu back audio
ui_audio_post_event("UI_Hub_Menu_Back")
else
--Transition to game
cell_transition_screen(CELL_STATE_OUT, CELL_SCREEN_NONE, CELL_SCREEN_REWARDS, cell_rewards_exit_to_game)
--Play menu back audio
ui_audio_post_event("UI_Hub_Menu_Back")
end
end
--HVS_TBT 6/26/2014: added missing *_unload() function
function cell_rewards_unload()
--Play menu back audio
ui_audio_post_event("UI_Hub_Menu_Back")
--Transition to cell main
cell_transition_screen(CELL_STATE_PORTRAIT, CELL_SCREEN_MAIN, CELL_SCREEN_REWARDS, nil)
end
function cell_rewards_exit_to_main()
pop_screen()
end
function cell_rewards_exit_to_game(event)
pop_screen() --Rewards
pop_screen() --Main
pop_screen() --Cell Frame
end
-- After the completion screen from a purchase, refresh the list of rewards.
--
function cell_rewards_gained_focus()
local index = Active_list:get_selection()
Menu_data = {}
Cell_rewards_building_menu = Menu_data
vint_dataresponder_request("cell_rewards_reward_dr", "cell_rewards_add_reward", 0, Cell_rewards_category_id)
-- May need to update image and description if list was shifted
cell_rewards_item_nav(Cell_rewards_building_menu[index])
cell_rewards_populate_list(Menu_data, index)
Input_tracker:subscribe(true)
end
-- =====================================
-- Mouse Specific Functions
-- =====================================
function cell_rewards_enable_mouse(enable)
if game_get_platform() == "PC" and enable ~= nil then
if Mouse_input_tracker ~= 0 then
Mouse_input_tracker:subscribe(enable)
end
if Hint_bar_mouse_input_tracker ~= nil then
Hint_bar_mouse_input_tracker:subscribe(enable)
end
end
end
-- Mouse inputs
function cell_rewards_mouse_click(event, target_handle, mouse_x, mouse_y)
local hint_index = Hint_bar:get_hint_index(target_handle)
if hint_index == 1 then
store_common_button_b()
end
local new_index = Active_list:get_button_index(target_handle)
if new_index ~= 0 then
Active_list:set_selection(new_index)
cell_rewards_button_a()
end
end
function cell_rewards_mouse_move(event, target_handle)
Hint_bar:set_highlight(0)
local hint_index = Hint_bar:get_hint_index(target_handle)
if hint_index ~= 0 then
Hint_bar:set_highlight(hint_index)
end
local old_index = Active_list:get_selection()
local new_index = Active_list:get_button_index(target_handle)
if new_index ~= 0 and new_index ~= old_index then
Active_list:set_selection(new_index)
Active_list:move_cursor(0, true)
-- If the item has a callback for navigation, do it
local data_item = Active_list:return_selected_data()
if data_item.on_nav ~= nil then
data_item.on_nav(data_item)
end
end
end
-- When returning to the category menu from submenu, this gets called to repopulate the data,
-- since we need to determine which categories still have new rewards.
--
function cell_rewards_category_menu_update(menu_data)
local index = Active_list:get_selection()
Menu_data = {}
Cell_rewards_building_menu = Menu_data
vint_dataresponder_request("cell_rewards_category_dr", "cell_rewards_add_category", 0)
Menu_data = Cell_rewards_building_menu
Menu_data.on_enter = cell_rewards_category_menu_update
cell_rewards_populate_list(Menu_data, index)
--Hide locked grp
local elements_locked_grp_h = vint_object_find("elements_locked_grp", 0, Cell_rewards_doc_handle)
vint_set_property(elements_locked_grp_h, "visible", false)
--show respect on category view
Store_header:respect_meter_show()
end