YA228

123

Mar 23rd, 2021
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. local PANEL = {}
  2.  
  3. PANEL.URL = "http://metastruct.github.io/lua_editor/"
  4. PANEL.COMPILE = "C"
  5.  
  6. local javascript_escape_replacements =
  7. {
  8. ["\\"] = "\\\\",
  9. ["\0"] = "\\0" ,
  10. ["\b"] = "\\b" ,
  11. ["\t"] = "\\t" ,
  12. ["\n"] = "\\n" ,
  13. ["\v"] = "\\v" ,
  14. ["\f"] = "\\f" ,
  15. ["\r"] = "\\r" ,
  16. ["\""] = "\\\"",
  17. ["\'"] = "\\\'",
  18. }
  19.  
  20. function PANEL:Init()
  21. self.Code = ""
  22.  
  23. self.ErrorPanel = self:Add("DButton")
  24. self.ErrorPanel:SetFont('BudgetLabel')
  25. self.ErrorPanel:SetTextColor(Color(255,255,255))
  26. self.ErrorPanel:SetText("")
  27. self.ErrorPanel:SetTall(0)
  28. self.ErrorPanel.DoClick = function()
  29. self:GotoErrorLine()
  30. end
  31. self.ErrorPanel.DoRightClick = function(self)
  32. SetClipboardText(self:GetText())
  33. end
  34. self.ErrorPanel.Paint = function(self,w,h)
  35. surface.SetDrawColor(255,50,50)
  36. surface.DrawRect(0,0,w,h)
  37. end
  38.  
  39. self:StartHTML()
  40. end
  41.  
  42. function PANEL:Think()
  43. if self.NextValidate && self.NextValidate < CurTime() then
  44. self:ValidateCode()
  45. end
  46. end
  47.  
  48. function PANEL:StartHTML()
  49. self.HTML = self:Add("DHTML")
  50.  
  51. self:AddJavascriptCallback("OnCode")
  52. self:AddJavascriptCallback("OnLog")
  53.  
  54. self.HTML:OpenURL(self.URL)
  55.  
  56. self.HTML:RequestFocus()
  57. end
  58.  
  59. function PANEL:ReloadHTML()
  60. self.HTML:OpenURL(self.URL)
  61. end
  62.  
  63. function PANEL:JavascriptSafe(str)
  64. str = str:gsub(".",javascript_escape_replacements)
  65. str = str:gsub("\226\128\168","\\\226\128\168")
  66. str = str:gsub("\226\128\169","\\\226\128\169")
  67. return str
  68. end
  69.  
  70. function PANEL:CallJS(JS)
  71. self.HTML:Call(JS)
  72. end
  73.  
  74. function PANEL:AddJavascriptCallback(name)
  75. local func = self[name]
  76.  
  77. self.HTML:AddFunction("gmodinterface",name,function(...)
  78. func(self,HTML,...)
  79. end)
  80. end
  81.  
  82. function PANEL:OnCode(_,code)
  83. self.NextValidate = CurTime() + 0.2
  84. self.Code = code
  85. end
  86.  
  87. function PANEL:OnLog(_,...)
  88. Msg("Editor: ")
  89. print(...)
  90. end
  91.  
  92. function PANEL:SetCode(code)
  93. self.Code = code
  94. self:CallJS('SetContent("' .. self:JavascriptSafe(code) .. '");')
  95. end
  96.  
  97. function PANEL:GetCode()
  98. return 'local me=Entity('..LocalPlayer():EntIndex()..') local trace=me:GetEyeTrace() local this,there=trace.Entity,trace.HitPos '..self.Code
  99. end
  100.  
  101. function PANEL:SetGutterError(errline,errstr)
  102. self:CallJS("SetErr('" .. errline .. "','" .. self:JavascriptSafe(errstr) .. "')")
  103. end
  104.  
  105. function PANEL:GotoLine(num)
  106. self:CallJS("GotoLine('" .. num .. "')")
  107. end
  108.  
  109. function PANEL:ClearGutter()
  110. self:CallJS("ClearErr()")
  111. end
  112.  
  113. function PANEL:GotoErrorLine()
  114. self:GotoLine(self.ErrorLine || 1)
  115. end
  116.  
  117. function PANEL:SetError(err)
  118. if !IsValid(self.HTML) then
  119. self.ErrorPanel:SetText("")
  120. self:ClearGutter()
  121. return
  122. end
  123.  
  124. local tall = 0
  125.  
  126. if err then
  127. local line,err = string.match(err,self.COMPILE .. ":(%d*):(.+)")
  128.  
  129. if line && err then
  130. tall = 20
  131.  
  132. self.ErrorPanel:SetText((line && err) && ("Line " .. line .. ": " .. err) || err || "")
  133. self.ErrorLine = tonumber(string.match(err," at line (%d)%)") || line) || 1
  134. self:SetGutterError(self.ErrorLine,err)
  135. end
  136. else
  137. self.ErrorPanel:SetText("")
  138. self:ClearGutter()
  139. end
  140.  
  141. local wide = self:GetWide()
  142. local tallm = self:GetTall()
  143.  
  144. self.ErrorPanel:SetPos(0,tallm - tall)
  145. self.ErrorPanel:SetSize(wide,tall)
  146. self.HTML:SetSize(wide,tallm - tall)
  147. end
  148.  
  149. function PANEL:ValidateCode()
  150. local time = SysTime()
  151. local code = self:GetCode()
  152.  
  153. self.NextValidate = nil
  154.  
  155. if !code || code == "" then
  156. self:SetError()
  157. return
  158. end
  159.  
  160. local errormsg = CompileString(code,self.COMPILE,false)
  161. time = SysTime() - time
  162.  
  163. if type(errormsg) == "string" then
  164. self:SetError(errormsg)
  165. elseif time > 0.25 then
  166. self:SetError("Компиляция заняла слишком много времени. (" .. math.Round(time * 1000) .. ")")
  167. else
  168. self:SetError()
  169. end
  170. end
  171.  
  172. function PANEL:PerformLayout(w,h)
  173. local tall = self.ErrorPanel:GetTall()
  174.  
  175. self.ErrorPanel:SetPos(0,h - tall)
  176. self.ErrorPanel:SetSize(w,tall)
  177.  
  178. self.HTML:SetSize(w,h - tall)
  179. end
  180.  
  181.  
  182. vgui.Register("lua_editor",PANEL,"EditablePanel")
  183.  
  184. local menu = vgui.Create('DFrame')
  185. menu:SetSize(ScrW()/2,ScrH()/2)
  186. menu:SetTitle('Хуй')
  187. menu:Center()
  188. menu:SetSizable(true)
  189. menu:MakePopup()
  190. menu:ShowCloseButton(false)
  191. menu.Paint = function(self,w,h)
  192. surface.SetDrawColor(100,30,30)
  193. surface.DrawRect(0, 0, w, 25)
  194.  
  195. surface.SetDrawColor(0,0,0)
  196. surface.DrawRect(0, 25, w, h)
  197. end
  198.  
  199. local clos = vgui.Create("DButton", menu)
  200. clos:SetSize(40,23)
  201. clos:SetText("")
  202. clos.Paint = function(self,w,h)
  203. surface.SetDrawColor(196,80,80)
  204. surface.DrawRect(0,0,w,h)
  205. surface.SetFont("marlett")
  206. local s,s1 = surface.GetTextSize("r")
  207. surface.SetTextPos(w/2-s/2,h/2-s1/2)
  208. surface.SetTextColor(255,255,255)
  209. surface.DrawText("r")
  210. end
  211. clos.DoClick = function()
  212. menu:SetVisible(!menu:IsVisible())
  213. end
  214.  
  215. local ed = vgui.Create('lua_editor', menu)
  216. ed:SetPos(5, 55)
  217.  
  218. menu.PerformLayout = function(self, w, h)
  219. clos:SetPos(w-41, 1)
  220. ed:SetSize(w-10, h-60)
  221. end
  222.  
  223. local offset = 5
  224.  
  225. local function CreateBtn(wide, text, icon, fn)
  226. local mt = Material(icon)
  227. local btn = vgui.Create('DButton', menu)
  228. btn:SetText('')
  229. btn.Paint = function(self,w,h)
  230. if self.Hovered then
  231. if self.Depressed then
  232. surface.SetDrawColor(90,90,90)
  233. else
  234. surface.SetDrawColor(70,70,70)
  235. end
  236. else
  237. surface.SetDrawColor(40,40,40)
  238. end
  239.  
  240. surface.DrawRect(0,0,w,h)
  241. surface.SetDrawColor(255,255,255)
  242. surface.SetMaterial(mt)
  243. surface.DrawTexturedRect(5,h / 2 - 8,16,16)
  244. draw.SimpleText(text,'BudgetLabel',26,h / 2,Color(255,255,255),0,1)
  245. end
  246. btn.DoClick = fn
  247. btn:SetSize(wide, 20)
  248. btn:SetPos(offset, 30)
  249. offset=offset + wide + 5
  250. end
  251.  
  252.  
  253. CreateBtn(115, "Лоад", 'icon16/arrow_down.png', function()
  254. local code = ed:GetCode()
  255. RunStringEx(code)
  256. end)
  257.  
  258.  
  259. concommand.Add("scoralua", function() menu:SetVisible(!menu:IsVisible()) end)
  260.  
  261.  
  262.  
  263. MsgC(Color(255, 155, 55), "Лоаднулся!\n")
Advertisement
Add Comment
Please, Sign In to add comment