Somepotato

AceEditor for Gmod

Nov 13th, 2012
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.90 KB | None | 0 0
  1. --AceEditor by Somepotato, if used do tell, I'm curiouz liek that.
  2. --v 1.0
  3. local fontList={}
  4. local PANEL={}
  5.  
  6. local function escape(str)
  7.     return select(1,str:gsub("[^%w _~%.%-]",function(c) local b=string.format("%X",tostring(string.byte(c))) return "%"..(#b==1 and "0"..b or b) end))
  8. end
  9. local function unescape(str)
  10.     return str:gsub("%%(%x%x)",function(c) return string.char(tonumber("0x"..c)) end)
  11. end
  12. function PANEL:OnCallback(_,func,args)
  13.     if(func=="OnLoad") then
  14.         self.Loaded=true
  15.         if(self.OnLoadSetCode) then self:SetCode(self.OnLoadSetCode) self.OnLoadSetCode=nil end
  16.         if(self.OnLoad) then
  17.             self:OnLoad()
  18.         end
  19.     elseif(func == "ReceiveCode") then
  20.         self.code=unescape(args[1])
  21.         if(self.OnCodeChanged) then
  22.             self:OnCodeChanged(self.code)
  23.         end
  24.     end
  25. end
  26. function PANEL:Init()
  27.     self:NewObjectCallback("glua","ReceiveCode")
  28.     self:NewObjectCallback("glua","OnLoad")
  29.     self:SetHTML([[<body onload='glua.OnLoad()'></body> <div id="editor" style="position:fixed;top:0;left:0;right:0;bottom:0">LOADING...</div><script src="http://d1n0x3qji82z53.cloudfront.net/src-min-noconflict/ace.js" type="text/javascript" charset="utf-8"></script><script>var editor = ace.edit("editor");editor.setTheme("ace/theme/monokai");editor.getSession().setMode("ace/mode/lua");
  30.         function SetCode(code){
  31.             editor.setValue(unescape(code),1)
  32.         }
  33.         function GetCode(){
  34.             glua.ReceiveCode(escape(editor.getValue()));
  35.         }
  36.         editor.on("change",GetCode)
  37.         function populateFontList(arr){
  38.             var ret="";
  39.             for(var k in arr){
  40.                 ret+=arr[k]+"\n"
  41.             }
  42.             editor.setValue(ret);
  43.         }
  44.     </script>
  45.     ]])
  46.     self.Loaded = false
  47.     self:SetCode("")
  48. end
  49.  
  50. function PANEL:SetCode(code)
  51.     if not self.Loaded then
  52.         self.OnLoadSetCode = code
  53.         return
  54.     end
  55.     self.code=code
  56.     self:RunJavascript("SetCode(\""..escape(code).."\")")
  57. end
  58. function PANEL:GetCode()
  59.     return self.code or ""
  60. end
  61.  
  62. vgui.Register("AceEditor",PANEL,"Awesomium")
Advertisement
Add Comment
Please, Sign In to add comment