Advertisement
Alakazard12

rGUI

Apr 14th, 2013
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.62 KB | None | 0 0
  1.  
  2. -- rGUI Alpha 0.x
  3. -- robhol
  4.  
  5. term.clear();
  6.  
  7. if not redirect then error("redirect required"); end
  8. if not robapi   or not robapi.version or robapi.version() < 1.11 then error("robapi 1.11+ required"); end
  9. if not term.isColor() then error("advanced computer required"); end
  10.  
  11. local swidth, sheight = term.getSize();
  12. buffer = redirect.createRedirectBuffer( swidth, sheight + 1, nil, nil, true );
  13.  
  14. RGUI_DEBUG = true;
  15.  
  16. -- load other components
  17. local loc = shell.getRunningProgram();
  18. loc = loc:sub(1, loc:len() - fs.getName(loc):len());
  19.  
  20. dofile(loc .. "util");
  21. dofile(loc .. "prototypes");
  22.  
  23. if RGUI_DEBUG then sleep(0.5); end
  24.  
  25. -- set up vital engine functions and stuff
  26. local lastId = 0;
  27. local id_increment = 0.001;
  28. local exiting = false;
  29.  
  30. function createObject(otype, x, y, fc, bc)
  31.    
  32.     local obj =
  33.     {
  34.         name=false,
  35.         type=otype,
  36.         x=round(x),
  37.         y=round(y),
  38.         forecolor=(fc or colors.white),
  39.         backcolor=bc
  40.     };
  41.  
  42.     inherit(obj, prototypes.object)
  43.  
  44.     --make sure objects are rendered/processed in the order they were created, unless changed
  45.     lastId = lastId + id_increment;
  46.     obj.order = lastId;
  47.  
  48.     return obj;
  49. end
  50.  
  51. function createEvent(event, ...)
  52.     return {name=event, handled=false, args={...}};
  53. end
  54.  
  55. -- internal loading - root object and prototypes
  56. root = createObject("root", 0, 0 );
  57.  
  58. root:addHandlerCC("mouse_click", function ( self )
  59.     prototypes.window._dragData = false;
  60. end);
  61.  
  62. -- set up public API
  63. rgui =
  64. {
  65.    
  66.     createApp = function ( name )
  67.         local obj = createObject("app", 0, 0, nil, nil);
  68.         inherit(obj, prototypes.obj_creator); -- allow object creation on this object
  69.         inherit(obj, prototypes.app);
  70.  
  71.         obj.name = name;
  72.  
  73.         obj.width  = swidth;
  74.         obj.height = sheight;
  75.  
  76.         root:addChild(obj);
  77.         root.focusedChild = obj;
  78.  
  79.         return obj;
  80.     end,
  81.  
  82.     forceUpdate = function ()
  83.         os.queueEvent("_forceDraw");
  84.     end,
  85.    
  86.     getRoot = function ()
  87.         return root;
  88.     end,
  89.  
  90.     getScreenSize = function ()
  91.         return swidth, sheight;
  92.     end,
  93.    
  94.     execute = function ( file, args )
  95.        
  96.        
  97.     end,
  98.    
  99.     exit = function()
  100.         exiting = true;
  101.     end,
  102.  
  103. }
  104.  
  105. --alias
  106. rgui.exitToDOS = rgui.exit;
  107.  
  108. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  109. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  110. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  111.  
  112. -- debug app loading, replace with some kind of proper system later.
  113.  
  114. local rgui_args = {...};
  115. dofile(loc ..  (rgui_args[1] or ("test/" .. "controls")) );
  116. --dofile(loc ..  (rgui_args[1] or "demo/rsctl") );
  117.  
  118. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  119. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  120. -- TEST STUFF TEST STUFF TEST STUFF TEST STUFF
  121.  
  122.  
  123. --main event loop
  124. rgui.forceUpdate();
  125. local timer = false;
  126. while not exiting do
  127.    
  128.     timer = timer and timer or os.startTimer(0.0501);
  129.    
  130.     --process events
  131.     local event = createEvent( os.pullEventRaw() );
  132.    
  133.     if event.name == "timer" and event.args[1] == timer then
  134.         --hijack event and replace with our animation event
  135.         timer = false;
  136.         event = createEvent( "animation" );
  137.     elseif event.name == "terminate" then
  138.         exiting = true;
  139.     end
  140.    
  141.     root:handle(event);
  142.    
  143.     --(re)draw screen
  144.     buffer.setBackgroundColor(colors.black);
  145.     buffer.clear();
  146.    
  147.     root:draw();
  148.  
  149.     buffer.blit(1,1,1,0,swidth,sheight + 1);
  150. end
  151.  
  152. --exiting; get rid of stuff
  153. rgui = nil;
  154.  
  155. term.clear();
  156. term.setCursorPos(1, 1);
  157.  
  158. print("Goodbye :)");
  159.  
  160. sleep(0.2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement