Advertisement
arcanfe

Untitled

Jan 16th, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.39 KB | None | 0 0
  1. grid = {};
  2. iodispatch = {};
  3. vspacing = 4;
  4. hspacing = 4;
  5. cursor = 0;
  6. cellcount = 0;
  7. pageofs = 0;
  8.  
  9. function gridle()
  10.     system_load("scripts/keyconf.lua")();
  11.     games = list_games( {} );
  12.     if (#games == 0) then
  13.         error "No games found";
  14.         shutdown();
  15.     end
  16.  
  17.     keyconfig = keyconf_create(0, { "rMENU_ESCAPE", "rMENU_LEFT", "rMENU_RIGHT", "rMENU_UP", "rMENU_DOWN", "rMENU_SELECT" } );
  18.     keyconfig.iofun = gridle_input;
  19.     if (keyconfig.active == false) then
  20.         gridle_input = function(iotbl)
  21.             if (keyconfig:input(iotbl) == true) then
  22.               gridle_input = keyconfig.iofun;
  23.             end
  24.         end
  25.     end
  26.  
  27.     iodispatch["MENU_UP"]      = function(iotbl) move_cursor( -1 * ncw); end
  28.     iodispatch["MENU_DOWN"]    = function(iotbl) move_cursor( ncw ); end
  29.     iodispatch["MENU_LEFT"]    = function(iotbl) move_cursor( -1 ); end
  30.     iodispatch["MENU_RIGHT"]   = function(iotbl) move_cursor( 1 ); end
  31.     iodispatch["MENU_ESCAPE"]  = function(iotbl) shutdown(); end
  32.     iodispatch["MENU_SELECT"]  = function(iotbl) if (games[cursor + 1]) then
  33.   launch_target( games[cursor + pageofs + 1].title, LAUNCH_EXTERNAL); end end
  34.  
  35.     cursorvid = fill_surface(34, 34, 255, 255, 255);
  36.     build_grid(64, 64);
  37. end
  38.  
  39. function cell_coords(x, y)
  40.     return (0.5 * borderw) + x * (cell_width + hspacing), (0.5 * borderh) + y * (cell_height + vspacing);
  41. end
  42.  
  43. function move_cursor( ofs )
  44.     local pageofs_cur = pageofs;
  45.     cursor = cursor + ofs;
  46.  
  47. -- paging calculations
  48.     if (ofs > 0) then -- right/forward
  49.         if (cursor >= ncc) then -- move right or "forward"
  50.             cursor = cursor - ncc;
  51.             pageofs_cur = pageofs_cur + ncc;
  52.         end
  53.  
  54.         -- wrap around on overflow
  55.         if (pageofs_cur + cursor >= #games) then
  56.             pageofs_cur = 0;
  57.             cursor = 0;
  58.         end
  59.     elseif (ofs < 0) then -- left/backward
  60.         if (cursor < 0) then -- step back a page
  61.             pageofs_cur = pageofs_cur - ncc;
  62.             cursor = ncc - ( -1 * cursor);
  63.  
  64.             if (pageofs_cur < 0) then -- wrap page around
  65.                 pageofs_cur = math.floor(#games / ncc) * ncc;
  66.             end
  67.  
  68.             if (cursor < 0 or cursor >= #games - pageofs_cur) then
  69.                 cursor = #games - pageofs_cur - 1;
  70.             end
  71.         end
  72.     end
  73.  
  74.     local x,y = cell_coords(math.floor(cursor % ncw), math.floor(cursor / ncw));
  75.     move_image(cursorvid, x - hspacing, y - vspacing);
  76.  
  77. -- reload images of the page has changed
  78.     if (pageofs_cur ~= pageofs) then
  79.         erase_grid();
  80.         pageofs = pageofs_cur;
  81.         build_grid(cell_width, cell_height);
  82.     end
  83. end
  84.  
  85. function get_image(romset)
  86.     local rvid = BADID;
  87.  
  88.     if resource("screenshots/" .. romset .. ".png") then
  89.         rvid = load_image("screenshots/" .. romset .. ".png");
  90.     end
  91.  
  92.     if (rvid == BADID) then
  93.         rvid = render_text( [[\ffonts/default.ttf,96 ]] .. romset );
  94.     end
  95.  
  96.     return rvid;
  97. end
  98.  
  99. function erase_grid()
  100.     cellcount = 0;
  101.  
  102.     for row=0, nch-1 do
  103.      for col=0, ncw-1 do
  104.       if (grid[row][col]) then
  105.        delete_image(grid[row][col]);
  106.        grid[row][col] = nil;
  107.       end
  108.      end
  109.     end
  110.  
  111. end
  112.  
  113. function build_grid(width, height)
  114. --  figure out how many full cells we can fit with the current resolution
  115.     ncw = math.floor(VRESW / (width + hspacing));
  116.     nch = math.floor(VRESH / (height + vspacing));
  117.     ncc = ncw * nch;
  118.     cell_width = width;
  119.     cell_height = height;
  120.  
  121. --  figure out how much "empty" space we'll have to pad with
  122.     borderw = VRESW % (width + hspacing);
  123.     borderh = VRESH % (height + vspacing);
  124.  
  125.     for row=0, nch-1 do
  126.         grid[row] = {};
  127.         for col=0, ncw-1 do
  128.             local gameno = (row * ncw + col + pageofs + 1); -- games is 1 indexed
  129.             if (games[gameno] == nil) then break; end
  130.             local vid = get_image(games[gameno]["setname"]);
  131.             resize_image(vid, cell_width, cell_height);
  132.             move_image(vid,cell_coords(col, row));
  133.             order_image(vid, 2);
  134.             show_image(vid);
  135.             grid[row][col] = vid;
  136.             cellcount = cellcount + 1;
  137.         end
  138.     end
  139.  
  140. -- reset the cursor
  141.     delete_image(cursorvid);
  142.     cursorvid = fill_surface(width + 2 * hspacing, height + 2 * vspacing, 255, 255, 255);
  143.     show_image(cursorvid);
  144.     order_image(cursorvid, 0);
  145.     move_cursor(0);
  146. end
  147.  
  148. function gridle_input(iotbl)
  149.  local restbl = keyconfig:match(iotbl);
  150.  if (restbl and iotbl.active) then
  151.   for ind,val in pairs(restbl) do
  152.    if (iodispatch[val]) then
  153.      iodispatch[val](restbl);
  154.    end
  155.   end
  156.  end
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement