Advertisement
builderman_build

Untitled

Oct 15th, 2016
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.68 KB | None | 0 0
  1. local coreGui = game:GetService("CoreGui")
  2. local robloxGui = coreGui:WaitForChild("RobloxGui")
  3. local userInput = game:GetService("UserInputService")
  4. local guiService = game:GetService("GuiService")
  5. local rs = game:GetService("RunService")
  6. local history = {}
  7.  
  8. spawn(function ()
  9. local controlFrame = robloxGui:WaitForChild("ControlFrame",5)
  10. if controlFrame then
  11. local toggleDevConsole = Instance.new("BindableFunction")
  12. toggleDevConsole.Name = "ToggleDevConsole"
  13. toggleDevConsole.Parent = controlFrame
  14. end
  15. end)
  16.  
  17. local cmd = Instance.new("TextBox",robloxGui)
  18. cmd.Name = "Cmd"
  19. cmd.Size = UDim2.new(1,0,0,29)
  20. cmd.Position = UDim2.new(0,0,1,0)
  21. cmd.TextXAlignment = "Left"
  22. cmd.FontSize = Enum.FontSize.Size28
  23. cmd.Font = "SourceSans"
  24. cmd.Text = ""
  25.  
  26. -------------------------------------------------------------------------------------------------------------------------------------
  27. -- Input and Tweening
  28. -------------------------------------------------------------------------------------------------------------------------------------
  29.  
  30. local inset = 0
  31. local goalInset = 0
  32. local isActive = false
  33.  
  34. local function moveTowards(value,goal,rate)
  35. if value < goal then
  36. return math.min(goal,value + rate)
  37. elseif value > goal then
  38. return math.max(goal,value - rate)
  39. else
  40. return goal
  41. end
  42. end
  43.  
  44. local function renderUpdate()
  45. if inset ~= goalInset then
  46. local yOffset = 0
  47. if robloxGui:FindFirstChild("TopBarContainer") then
  48. yOffset = robloxGui.TopBarContainer.Size.Y.Offset
  49. end
  50. inset = moveTowards(inset,goalInset,6)
  51. guiService:SetGlobalGuiInset(0,yOffset,0,inset)
  52. end
  53. end
  54.  
  55. local function setActive(active)
  56. isActive = active
  57. if isActive then
  58. goalInset = 30
  59. cmd:CaptureFocus()
  60. else
  61. goalInset = 0
  62. cmd:ReleaseFocus()
  63. cmd.Text = ""
  64. end
  65. end
  66.  
  67. local function onInputBegan(input)
  68. if input.KeyCode == Enum.KeyCode.Backquote then
  69. setActive(not isActive)
  70. end
  71. end
  72.  
  73. spawn(function ()
  74. local topBar = robloxGui:WaitForChild("TopBarContainer",5)
  75. if topBar then
  76. inset = inset - 1 -- bump the inset
  77. end
  78. end)
  79.  
  80. rs:BindToRenderStep(http:GenerateGUID(),0,renderUpdate)
  81. userInput.InputBegan:connect(onInputBegan)
  82.  
  83. -------------------------------------------------------------------------------------------------------------------------------------
  84. -- Syntax Highlighting
  85. -------------------------------------------------------------------------------------------------------------------------------------
  86. local primitivesRaw = "and break do else elseif end false for function if in local nil not or repeat return then true until while"
  87. local operators = "+ - * / %% ^ # = ~ < > %( %) { } %[ %] ; : , %."
  88. local env = getfenv()
  89. local studio = settings().Studio
  90.  
  91. local primitives = {}
  92. for primitive in primitivesRaw:gmatch("[^ ]+") do
  93. primitives[primitive] = true
  94. end
  95.  
  96. local deprecatedBuiltInKeywords =
  97. {
  98. Game = true;
  99. PluginManager = true;
  100. settings = true;
  101. Workspace = true;
  102. }
  103.  
  104. local function isPrimitiveKeyword(keyword,s)
  105. return primitives[keyword]
  106. end
  107.  
  108. local function isBuiltInKeyword(keyword)
  109. return (env[keyword] ~= nil and rawget(env,keyword) == nil and not deprecatedBuiltInKeywords[keyword])
  110. end
  111.  
  112. local function indexGmatch(str,pattern)
  113. local indexAt = 1
  114. return function ()
  115. local startIndex,endIndex = str:find(pattern,indexAt)
  116. if startIndex and endIndex and startIndex <= endIndex then
  117. indexAt = endIndex + 1
  118. return startIndex,endIndex,str:sub(startIndex,endIndex)
  119. end
  120. end
  121. end
  122.  
  123. -- Main --
  124.  
  125. local blankC3 = Color3.new()
  126.  
  127. local function syntaxHighlight(str,fontSize)
  128. -- Update colors
  129.  
  130. local backgroundColor = studio["Background Color"]
  131. local builtInFunctionColor = studio["Built-in Function Color"]
  132. local commentColor = studio["Comment Color"]
  133. local keywordColor = studio["Keyword Color"]
  134. local numberColor = studio["Number Color"]
  135. local operatorColor = studio["Operator Color"]
  136. local stringColor = studio["String Color"]
  137. local textColor = studio["Text Color"]
  138.  
  139. cmd.BackgroundColor3 = backgroundColor
  140. cmd.BorderColor3 = blankC3:lerp(Color3.new(1-backgroundColor.r,1-backgroundColor.g,1-backgroundColor.b),0.4)
  141. cmd.TextColor3 = cmd.BorderColor3
  142.  
  143.  
  144. -- Build the individual characters
  145.  
  146. local fontHeight = tonumber(fontSize.Name:match("%d+$"))
  147. local charMap = {}
  148. local activeWidth = 0
  149.  
  150. for _,v in pairs(cmd:GetChildren()) do
  151. if tonumber(v.Name) then
  152. v:Destroy()
  153. end
  154. end
  155.  
  156. for char in str:gmatch(".") do
  157. local label = Instance.new("TextLabel",cmd)
  158. label.BackgroundTransparency = 1
  159. label.FontSize = fontSize
  160. label.Font = "SourceSans"
  161. label.Text = ((char == "\n" or char == "\t") and " " or char)
  162. label.TextColor3 = textColor
  163. label.ZIndex = 2
  164. local bounds = label.TextBounds
  165. label.Size = UDim2.new(0,bounds.X,0,bounds.Y)
  166. label.Position = UDim2.new(0,activeWidth-1,0,1)
  167. activeWidth = activeWidth + bounds.X
  168. table.insert(charMap,{
  169. Character = char;
  170. Label = label;
  171. })
  172. label.Name = #charMap
  173. end
  174.  
  175. local function markCharMap(tag,color,startIndex,endIndex,setBold)
  176. for i = startIndex,endIndex do
  177. local char = charMap[i]
  178. char[tag] = true
  179. char.Label.TextColor3 = color
  180. if setBold ~= nil then
  181. if setBold then
  182. char.Label.Font = "SourceSansBold"
  183. else
  184. char.Label.Font = "SourceSans"
  185. end
  186. end
  187. end
  188. end
  189.  
  190. -- Process Numbers
  191.  
  192. for startIndex,endIndex in indexGmatch(str,"%d+") do
  193. local canMark = true
  194. if startIndex-1 > 0 then
  195. local charToLeft = str:sub(startIndex-1,startIndex-1)
  196. if charToLeft:match("%w") then
  197. canMark = false
  198. end
  199. end
  200. if canMark then
  201. while endIndex < #str do
  202. local n = endIndex + 1
  203. if str:sub(n,n):match("[%s%)%}%]]") then
  204. break
  205. else
  206. endIndex = n
  207. end
  208. end
  209. markCharMap("Number",numberColor,startIndex,endIndex)
  210. end
  211. end
  212.  
  213. -- Process built-in functions and primitives.
  214.  
  215. for startIndex,endIndex,keyword in indexGmatch(str,"%w+") do
  216. if #keyword > 1 then
  217. if isPrimitiveKeyword(keyword) then
  218. markCharMap("Primitive",keywordColor,startIndex,endIndex,true)
  219. elseif isBuiltInKeyword(keyword) then
  220. local canMark = true
  221. if startIndex-1 > 0 then
  222. local char = str:sub(startIndex-1,startIndex-1)
  223. if char == "." or char == ":" then
  224. canMark = false
  225. end
  226. end
  227. if canMark then
  228. markCharMap("BuiltIn",builtInFunctionColor,startIndex,endIndex,true)
  229. end
  230. end
  231. end
  232. end
  233.  
  234. -- Process basic strings
  235.  
  236. local i = 1
  237. local readingStr = false
  238. local activeChar = ""
  239.  
  240. while i <= #str do
  241. local char = str:sub(i,i)
  242. if char == '"' or char == "'" then
  243. markCharMap("String",stringColor,i,i,false)
  244. if not readingStr then
  245. readingStr = true
  246. activeChar = char
  247. elseif activeChar == char then
  248. readingStr = false
  249. end
  250. end
  251. if readingStr then
  252. markCharMap("String",stringColor,i,i,false)
  253. if char == "\\" then
  254. -- Mark the next character as a string and skip it.
  255. -- Don't even ask questions.
  256. i = i + 1
  257. if i <= #str then
  258. markCharMap("String",stringColor,i,i,false)
  259. end
  260. elseif char == "\n" then
  261. -- Code execution will result in an error, but stop reading anyway.
  262. readingStr = false
  263. activeChar = ""
  264. end
  265. end
  266. i = i + 1
  267. end
  268.  
  269. -- Process Blocks
  270.  
  271. local i = 1
  272. local startBlock = ""
  273. local inBlock = false
  274.  
  275. while i <= #str do
  276. local char = str:sub(i,i)
  277. if char == "[" then
  278. local startedAt = i
  279. local invalid = false
  280. startBlock = "["
  281. while true do
  282. i = i + 1
  283. if i > #str then
  284. break
  285. end
  286. local nextChar = str:sub(i,i)
  287. startBlock = startBlock .. nextChar
  288. if nextChar == "[" then
  289. break
  290. elseif nextChar ~= "=" then
  291. invalid = true
  292. break
  293. end
  294. end
  295. if not invalid and #startBlock > 1 then
  296. local endBlock = startBlock:gsub("%[","%]")
  297. local endedAt = #str
  298. local ebStart,ebEnd = str:find(endBlock,startedAt)
  299. if ebStart and ebEnd and ebStart < ebEnd then
  300. endedAt = ebEnd
  301. i = ebEnd
  302. end
  303. markCharMap("Block",stringColor,startedAt,endedAt,false)
  304. end
  305. end
  306. i = i + 1
  307. end
  308.  
  309. -- Process Comments
  310.  
  311. local inBlock = false
  312.  
  313. for startIndex,endIndex,comment in indexGmatch(str,"%-%-") do
  314. if not charMap[startIndex].Block then
  315. while endIndex < #str do
  316. endIndex = endIndex + 1
  317. local char = charMap[endIndex].Character
  318. if charMap[endIndex].Block then
  319. inBlock = true
  320. elseif inBlock or char == "\n" then
  321. break
  322. end
  323. end
  324. markCharMap("Comment",commentColor,startIndex,endIndex,false)
  325. end
  326. end
  327.  
  328. -- Process Operators
  329.  
  330. for operator in operators:gmatch("[^ ]+") do
  331. for startIndex,endIndex in indexGmatch(str,operator) do
  332. local m = charMap[startIndex]
  333. if not m.Comment and not m.Block and not m.String and not m.Number then
  334. markCharMap("Operator",operatorColor,startIndex,endIndex)
  335. end
  336. end
  337. end
  338.  
  339. lastCharTyped = tick()
  340. end
  341.  
  342. local function onChanged(property)
  343. if property == "Text" or pcall(function () return studio[property] end) then
  344. syntaxHighlight(cmd.Text,cmd.FontSize)
  345. end
  346. end
  347.  
  348. onChanged("Text")
  349. cmd.Changed:connect(onChanged)
  350. studio.Changed:connect(onChanged)
  351.  
  352. --------------------------------------------------------------------------------------------------------------------------------------
  353. -- Command Execution
  354. --------------------------------------------------------------------------------------------------------------------------------------
  355. local historyIndex = -1
  356.  
  357. local function onMouseWheelBackward()
  358. if isActive then
  359. if historyIndex == -1 then
  360. historyIndex = 1
  361. else
  362. historyIndex = math.min(#history,historyIndex+1)
  363. end
  364. if history[historyIndex] then
  365. cmd.Text = history[historyIndex]
  366. end
  367. end
  368. end
  369.  
  370. local function onMouseWheelForward()
  371. if isActive then
  372. if historyIndex == -1 then
  373. historyIndex = #history
  374. else
  375. historyIndex = math.max(1,historyIndex-1)
  376. end
  377. if history[historyIndex] then
  378. cmd.Text = history[historyIndex]
  379. end
  380. end
  381. end
  382.  
  383. local function onFocusLost(enterPressed)
  384. if enterPressed then
  385. historyIndex = -1
  386. local text = cmd.Text
  387. if #text > 0 then
  388. table.insert(history,text)
  389. warn(">",text)
  390. spawn(function ()
  391. local func,errorMsg = loadstring(text)
  392. if func then
  393. func()
  394. else
  395. error("Error in script: "..errorMsg)
  396. end
  397. end)
  398. end
  399. end
  400. if historyIndex == -1 then
  401. setActive(false)
  402. end
  403. end
  404.  
  405. cmd.FocusLost:connect(onFocusLost)
  406. cmd.MouseWheelBackward:connect(onMouseWheelBackward)
  407. cmd.MouseWheelForward:connect(onMouseWheelForward)
  408. --------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement