-- Script that encapsulates logic with streaming in and out images for UI elements that navigate through a list/grid/etc.
-- This is intended to be included as a script dependency for other lua scripts. Only one doc can be loaded at a time that
-- depends on this script.
local Image_streamer_data = {
image_list = {},
on = false,
}
function image_streamer_unwrap(arguments, start)
if start < #arguments then
return arguments[start + 1], image_streamer_unwrap(arguments, start + 1)
end
end
-- This callback is called when a load from the image streamer script is complete.
--
function image_streamer_load_cb()
-- Call the document defined callback that was passed in when streaming was started.
Image_streamer_data.in_progress = false
if Image_streamer_data.load_callback ~= nil then
Image_streamer_data.load_callback()
end
end
-- Add an image to the list. Appended to the end of the list. Returns false on error (is streaming already started?).
--
-- image_name: name of the image to add.
--
function image_streamer_add_image(image_name)
if Image_streamer_data.on then
return false
end
-- Make a new entry after any current entries
local index = #Image_streamer_data.image_list + 1
Image_streamer_data.image_list[index] = {}
local info = Image_streamer_data.image_list[index]
info.image_name = image_name
end
-- Removes all images from the list. Returns false on error, for example if you haven't stopped streaming first.
--
function image_streamer_remove_all_images()
if Image_streamer_data.on then
return false
end
-- blow away the list
Image_streamer_data.image_list = {}
return true
end
-- Initialize values for the image streamer, and commence streaming. The image names should be added before calling this.
-- Returns false if there was an error (you've already started streaming?).
--
-- start_index: Index (starting from 1) which will be the index of the first image to be loaded. Image loading will wrap past the
-- last index in a circular fashion.
-- num_loaded: The number of images to be loaded loaded concurrently.
-- load_callback: callback function to call when a streaming load is complete
--
function image_streamer_start_streaming(start_index, num_loaded, load_callback)
-- check for issues
if Image_streamer_data.on or #Image_streamer_data.image_list < num_loaded or #Image_streamer_data.image_list < start_index or num_loaded < 1 or start_index < 1 then
return false
end
-- init settings
Image_streamer_data.on = true
Image_streamer_data.index = start_index
Image_streamer_data.num_loaded = num_loaded
Image_streamer_data.load_callback = load_callback
Image_streamer_data.in_progress = true
-- now stream initial loaded images
local images_to_load = {}
local index = start_index
for image_num = 1,num_loaded do
images_to_load[image_num] = Image_streamer_data.image_list[index].image_name
index = index + 1
if index > #Image_streamer_data.image_list then
index = 1
end
end
game_peg_load_with_cb("image_streamer_load_cb", num_loaded, image_streamer_unwrap(images_to_load, 0))
return true
end
-- Stop streaming of images, and unload all images. Returns false if there was an error (you didn't start streaming?).
--
function image_streamer_end_streaming()
if Image_streamer_data.on == false then
return false
end
Image_streamer_data.on = false
-- now unload all images
local index = Image_streamer_data.index
for image_num = 1,Image_streamer_data.num_loaded do
game_peg_unload(Image_streamer_data.image_list[index].image_name)
index = index + 1
if index > #Image_streamer_data.image_list then
index = 1
end
end
return true
end
-- Returns true if a stream is currently is progress, false otherwise.
--
function image_streamer_is_stream_in_progress()
return Image_streamer_data.in_progress
end
-- Moves the index, and handle all necessary streaming. Will return false if operation not attempted (ie. streaming
-- is already in progress, or not enabled).
--
-- offset: how many positions to move the index. Can be negative. Will wrap when going past either end of the list.
--
function image_streamer_nav_index(offset)
if Image_streamer_data.on == false or Image_streamer_data.in_progress or offset > #Image_streamer_data.image_list then
return false
end
local image_list = {}
local images_to_load = {}
-- Initialize our "state" array.
for image_num = 1,#Image_streamer_data.image_list do
image_list[image_num] = {}
end
-- First, calculate all images that should be loaded now.
local index = Image_streamer_data.index
for image_num = 1,Image_streamer_data.num_loaded do
image_list[index].is_loaded = true
index = index + 1
if index > #Image_streamer_data.image_list then
index = 1
end
end
-- Update the index to the new value. I should really get rid of the use of # and start indexing at 0 so this will be simpler.
Image_streamer_data.index = Image_streamer_data.index + offset
if Image_streamer_data.index > #Image_streamer_data.image_list then
Image_streamer_data.index = Image_streamer_data.index - #Image_streamer_data.image_list
end
if Image_streamer_data.index < 1 then
Image_streamer_data.index = Image_streamer_data.index + #Image_streamer_data.image_list
end
-- Calculate which images should be loaded when we're done with this request.
index = Image_streamer_data.index
for image_num = 1,Image_streamer_data.num_loaded do
image_list[index].will_be_loaded = true
index = index + 1
if index > #Image_streamer_data.image_list then
index = 1
end
end
-- Unload images that have been scrolled off.
for image_num = 1, #Image_streamer_data.image_list do
if image_list[image_num].is_loaded == true and image_list[image_num].will_be_loaded ~= true then
game_peg_unload(Image_streamer_data.image_list[image_num].image_name)
end
end
-- Finally, load images that have been scrolled on.
for image_num = 1, #Image_streamer_data.image_list do
if image_list[image_num].is_loaded ~= true and image_list[image_num].will_be_loaded == true then
local i = #images_to_load + 1
images_to_load[i] = Image_streamer_data.image_list[image_num].image_name
end
end
game_peg_load_with_cb("image_streamer_load_cb", #images_to_load, image_streamer_unwrap(images_to_load, 0))
return true
end
function image_streamer_get_images()
return Image_streamer_data.image_list
end