Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- grid = {};
- iodispatch = {};
- vspacing = 4;
- hspacing = 4;
- cursor = 0;
- cellcount = 0;
- pageofs = 0;
- function gridle()
- system_load("scripts/keyconf.lua")();
- games = list_games( {} );
- if (#games == 0) then
- error "No games found";
- shutdown();
- end
- keyconfig = keyconf_create(0, { "rMENU_ESCAPE", "rMENU_LEFT", "rMENU_RIGHT", "rMENU_UP", "rMENU_DOWN", "rMENU_SELECT" } );
- keyconfig.iofun = gridle_input;
- if (keyconfig.active == false) then
- gridle_input = function(iotbl)
- if (keyconfig:input(iotbl) == true) then
- gridle_input = keyconfig.iofun;
- end
- end
- end
- iodispatch["MENU_UP"] = function(iotbl) move_cursor( -1 * ncw); end
- iodispatch["MENU_DOWN"] = function(iotbl) move_cursor( ncw ); end
- iodispatch["MENU_LEFT"] = function(iotbl) move_cursor( -1 ); end
- iodispatch["MENU_RIGHT"] = function(iotbl) move_cursor( 1 ); end
- iodispatch["MENU_ESCAPE"] = function(iotbl) shutdown(); end
- iodispatch["MENU_SELECT"] = function(iotbl) if (games[cursor + 1]) then
- launch_target( games[cursor + pageofs + 1].title, LAUNCH_EXTERNAL); end end
- cursorvid = fill_surface(34, 34, 255, 255, 255);
- build_grid(64, 64);
- end
- function cell_coords(x, y)
- return (0.5 * borderw) + x * (cell_width + hspacing), (0.5 * borderh) + y * (cell_height + vspacing);
- end
- function move_cursor( ofs )
- local pageofs_cur = pageofs;
- cursor = cursor + ofs;
- -- paging calculations
- if (ofs > 0) then -- right/forward
- if (cursor >= ncc) then -- move right or "forward"
- cursor = cursor - ncc;
- pageofs_cur = pageofs_cur + ncc;
- end
- -- wrap around on overflow
- if (pageofs_cur + cursor >= #games) then
- pageofs_cur = 0;
- cursor = 0;
- end
- elseif (ofs < 0) then -- left/backward
- if (cursor < 0) then -- step back a page
- pageofs_cur = pageofs_cur - ncc;
- cursor = ncc - ( -1 * cursor);
- if (pageofs_cur < 0) then -- wrap page around
- pageofs_cur = math.floor(#games / ncc) * ncc;
- end
- if (cursor < 0 or cursor >= #games - pageofs_cur) then
- cursor = #games - pageofs_cur - 1;
- end
- end
- end
- local x,y = cell_coords(math.floor(cursor % ncw), math.floor(cursor / ncw));
- move_image(cursorvid, x - hspacing, y - vspacing);
- -- reload images of the page has changed
- if (pageofs_cur ~= pageofs) then
- erase_grid();
- pageofs = pageofs_cur;
- build_grid(cell_width, cell_height);
- end
- end
- function get_image(romset)
- local rvid = BADID;
- if resource("screenshots/" .. romset .. ".png") then
- rvid = load_image("screenshots/" .. romset .. ".png");
- end
- if (rvid == BADID) then
- rvid = render_text( [[\ffonts/default.ttf,96 ]] .. romset );
- end
- return rvid;
- end
- function erase_grid()
- cellcount = 0;
- for row=0, nch-1 do
- for col=0, ncw-1 do
- if (grid[row][col]) then
- delete_image(grid[row][col]);
- grid[row][col] = nil;
- end
- end
- end
- end
- function build_grid(width, height)
- -- figure out how many full cells we can fit with the current resolution
- ncw = math.floor(VRESW / (width + hspacing));
- nch = math.floor(VRESH / (height + vspacing));
- ncc = ncw * nch;
- cell_width = width;
- cell_height = height;
- -- figure out how much "empty" space we'll have to pad with
- borderw = VRESW % (width + hspacing);
- borderh = VRESH % (height + vspacing);
- for row=0, nch-1 do
- grid[row] = {};
- for col=0, ncw-1 do
- local gameno = (row * ncw + col + pageofs + 1); -- games is 1 indexed
- if (games[gameno] == nil) then break; end
- local vid = get_image(games[gameno]["setname"]);
- resize_image(vid, cell_width, cell_height);
- move_image(vid,cell_coords(col, row));
- order_image(vid, 2);
- show_image(vid);
- grid[row][col] = vid;
- cellcount = cellcount + 1;
- end
- end
- -- reset the cursor
- delete_image(cursorvid);
- cursorvid = fill_surface(width + 2 * hspacing, height + 2 * vspacing, 255, 255, 255);
- show_image(cursorvid);
- order_image(cursorvid, 0);
- move_cursor(0);
- end
- function gridle_input(iotbl)
- local restbl = keyconfig:match(iotbl);
- if (restbl and iotbl.active) then
- for ind,val in pairs(restbl) do
- if (iodispatch[val]) then
- iodispatch[val](restbl);
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement