local MAX_COL = 2
local MAX_ROW = 2
function vdo_cell_menu_init()
end
Vdo_cell_menu = Vdo_base_object:new_base()
function Vdo_cell_menu:init()
local index = 0
self.menu_btn = {}
--Declare and populate buttons with data
for i = 0,MAX_ROW,1 do
for j = 0,MAX_COL,1 do
self.menu_btn[i ..j] = Vdo_cell_menu_button:new("menu_btn" ..i ..j, self.handle, self.doc_handle)
index = index + 1
end
end
self.animate_in = Vdo_anim_object:new("animate_btns_in", self.handle, self.doc_handle)
self.cursor_x = 0
self.cursor_y = 0
self:set_highlight(self.cursor_x, self.cursor_y)
end
function Vdo_cell_menu:cleanup()
-- Nuke all button subscriptions
Input_tracker:subscribe(false)
end
function Vdo_cell_menu:populate_menu(data, cur_selection)
local index = 0
local hidden_btn = {
label = "",
icon = nil,
is_enabled = false,
is_visible = false,
}
--populate buttons with data
for i = 0,MAX_ROW,1 do
for j = 0,MAX_COL,1 do
local btn_data = hidden_btn
if index <= #data then
btn_data = data[index]
end
self.menu_btn[j ..i]:populate_button(btn_data.label, btn_data.icon, btn_data.is_enabled, btn_data.new_items, btn_data.is_visible, btn_data.can_wrap)
index = index + 1
self.menu_btn[j ..i]:set_highlight(false)
end
end
if cur_selection ~= nil and cur_selection > -1 and cur_selection < 9 then
self:set_id(cur_selection)
else
-- get first non-disabled button
local found = false
for i = 0,MAX_ROW,1 do
for j = 0,MAX_COL,1 do
if self.menu_btn[j ..i].is_enabled ~= false then
if found == false then
found = true
self.cursor_x = j
self.cursor_y = i
--debug_print("always_on", "found " .. self.cursor_x .. self.cursor_y )
end
end
end
end
end
self:set_highlight(self.cursor_x, self.cursor_y)
end
function Vdo_cell_menu:set_highlight(position_x, position_y)
local previous_x = self.cursor_x
local previous_y = self.cursor_y
--Unhighlight previous option...
self.menu_btn[previous_x .. previous_y]:set_highlight(false)
--Highlight new option...
self.menu_btn[position_x .. position_y]:set_highlight(true)
--Store positions to self.
self.cursor_x = position_x
self.cursor_y = position_y
end
function Vdo_cell_menu:show_highlight(show)
local previous_x = self.cursor_x
local previous_y = self.cursor_y
--Unhighlight previous option...
self.menu_btn[previous_x .. previous_y]:set_highlight(show)
end
-- navigation helper function
function row_down_wrap(cur_y)
return abs((cur_y + 1) % (MAX_ROW + 1))
end
function row_up_wrap(cur_y)
return abs((cur_y + MAX_ROW) % (MAX_ROW + 1))
end
function column_right_wrap(cur_x)
return abs((cur_x + 1) % (MAX_COL + 1))
end
function column_left_wrap(cur_x)
return abs((cur_x + MAX_COL) % (MAX_COL + 1))
end
function identity(cur)
return cur
end
-- check if cell button in row / column enabled
function Vdo_cell_menu:search_enabled_cell(cur_x, cur_y, update_x, update_y, try_count)
for i = 1, try_count, 1 do
if self.menu_btn[cur_x ..cur_y].is_enabled ~= false then
return true, cur_x, cur_y
end
cur_x = update_x(cur_x)
cur_y = update_y(cur_y)
end
return false, 0, 0
end
function Vdo_cell_menu:nav_up(event, acceleration)
game_UI_audio_play("UI_Cell_Nav")
local cur_x = self.cursor_x
local cur_y = self.cursor_y
local orig_y = cur_y
-- check start column
local try_count = MAX_ROW
cur_y = row_up_wrap(cur_y)
local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
-- move column left and try again
for i = 1, MAX_COL, 1 do
cur_x = column_left_wrap(cur_x)
cur_y = MAX_COL
try_count = MAX_ROW + 1
found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
end
end
function Vdo_cell_menu:nav_down(event, acceleration)
game_UI_audio_play("UI_Cell_Nav")
local cur_x = self.cursor_x
local cur_y = self.cursor_y
-- check start column
local try_count = MAX_ROW
cur_y = row_down_wrap(cur_y)
local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_down_wrap, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
-- move column left and try again
for i = 1, MAX_COL, 1 do
cur_x = column_right_wrap(cur_x)
cur_y = 0
try_count = MAX_ROW + 1
found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, identity, row_up_wrap, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
end
end
function Vdo_cell_menu:nav_left(event, acceleration)
game_UI_audio_play("UI_Cell_Nav")
local cur_x = self.cursor_x
local cur_y = self.cursor_y
-- check start row
local try_count = MAX_COL
cur_x = column_left_wrap(cur_x)
local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_left_wrap, identity, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
-- move row up and try again
for i = 1, MAX_ROW, 1 do
cur_x = MAX_ROW
cur_y = row_up_wrap(cur_y)
try_count = MAX_ROW + 1
found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_left_wrap, identity, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
end
end
function Vdo_cell_menu:nav_right(event, acceleration)
game_UI_audio_play("UI_Cell_Nav")
local cur_x = self.cursor_x
local cur_y = self.cursor_y
-- check start row
local try_count = MAX_COL
cur_x = column_right_wrap(cur_x)
local found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_right_wrap, identity, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
-- move row down and try again
for i = 1, MAX_ROW, 1 do
cur_x = 0
cur_y = row_down_wrap(cur_y)
try_count = MAX_ROW + 1
found, found_x, found_y = self:search_enabled_cell(cur_x, cur_y, column_right_wrap, identity, try_count)
if found then
self:set_highlight(found_x, found_y)
return
end
end
end
function Vdo_cell_menu:get_id()
local max_col_id = MAX_COL + 1
return (self.cursor_y * max_col_id + self.cursor_x)
end
function Vdo_cell_menu:set_id(id)
local max_col_id = MAX_COL + 1
self.cursor_y = abs(floor(id / max_col_id))
self.cursor_x = abs(id % max_col_id )
end
function Vdo_cell_menu:play_animate_in(is_playing)
self.animate_in:play(0)
end
-- =====================================
-- Mouse Specific Functions
-- =====================================
-- Returns true and sets the highlight to the button if the target_handle matches its image
-- and if the button is not disabled
--
function Vdo_cell_menu:select_button(target_handle)
for i = 0,MAX_ROW,1 do
for j = 0,MAX_COL,1 do
local button = self.menu_btn[j ..i]
if (button ~= nil) and (button.is_enabled ~= false) and (button.btn_img.handle == target_handle) then
self:set_highlight(j, i)
return true
end
end
end
return false
end
function Vdo_cell_menu:add_mouse_inputs(func_prefix, input_tracker, priority)
if (func_prefix == nil) or (input_tracker == nil) then
return
end
-- Set default priority value to 50
priority = priority or 50
local mouse_click_function = func_prefix.."_mouse_click"
local mouse_move_function = func_prefix.."_mouse_move"
-- Add mouse inputs to each of the cell phone button's images
for i = 0,MAX_ROW,1 do
for j = 0,MAX_COL,1 do
if self.menu_btn[j ..i] ~= nil then
input_tracker:add_mouse_input("mouse_click", mouse_click_function, priority, self.menu_btn[j ..i].btn_img.handle)
input_tracker:add_mouse_input("mouse_move", mouse_move_function, priority, self.menu_btn[j ..i].btn_img.handle)
end
end
end
end