Guest User

All Lua Console V8

a guest
Aug 2nd, 2012
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.85 KB | None | 0 0
  1. --[[ This program is free software. It comes without any warranty, to
  2. the extent permitted by applicable law. You can redistribute it
  3. and/or modify it under the terms of the Do What The Fuck You Want
  4. To Public License, Version 2, as published by Sam Hocevar. See
  5. http://sam.zoy.org/wtfpl/COPYING for more details. --]]
  6.  
  7. -- The ALL lua-console
  8. -- by devast8a
  9.  
  10. console = {
  11. version = "8.0",
  12.  
  13. -- Current state of console
  14. -- 0 = Closed
  15. -- 1 = Opened
  16. -- 2 = Old console opened
  17. -- [READONLY]
  18. active = 0,
  19.  
  20. -- Debug state of console, 1 if active, 0 if not
  21. -- In no way effected by tpt.setdebug
  22. -- [READONLY]
  23. debugmode = 0,
  24.  
  25. -- Current command line text for console
  26. currentCommand = "",
  27.  
  28. buffer = {}, -- Handle writing output to a larger display than screen can show
  29. colors = {}, -- store console colors & resolve text colors to RGB
  30. command = {}, -- current command
  31. history = {}, -- command history
  32. keys = {}, -- handle key input
  33. old = {}, -- support for old console
  34. quick = {}, -- Quick commands (commands that being with !)
  35. screen = {}, -- handle the drawing of the console to the screen
  36.  
  37. bitwise = {}, -- Implementation for bitwise operations
  38. }
  39.  
  40. ------------------------------------------------
  41. -- GLOBAL API ----------------------------------
  42. ------------------------------------------------
  43. function print(message)
  44. console.writeln(message)
  45. --console.screen.buffer = console.screen.buffer .. "\n" .. message
  46. end
  47.  
  48. function error(message)
  49. console.writeln(message, "error", "errorbg")
  50. end
  51.  
  52. -- Call cmd & automatically call error (w/ error message) upon error
  53. function ecall(cmd, ...)
  54. _, result = pcall(cmd, unpack(arg))
  55.  
  56. if _ == false then
  57. error(result)
  58. end
  59.  
  60. return _, result
  61. end
  62.  
  63. -- Execute a commnad
  64. function runcommand(command)
  65. -- Handle quick commands
  66. if string.sub(command, 1, 1) == "!" then
  67. local l = string.find(command, " ")
  68.  
  69. if l == nil then
  70. command = string.sub(command, 2)
  71. args = ""
  72. else
  73. args = string.trim(string.sub(command, l))
  74. command = string.sub(command, 2, l - 1)
  75. end
  76.  
  77. cmd = console.quick[command]
  78. message = "No command by the name of '" .. command .. "' exists"
  79.  
  80. -- Short hand for return
  81. elseif string.sub(command, 1, 1) == "@" then
  82. command = "return " .. string.sub(command, 2)
  83. cmd, message = loadstring(command)
  84.  
  85. -- Compile the string to a function
  86. else
  87. cmd, message = loadstring(command)
  88. end
  89.  
  90. -- Handle errors
  91. if cmd == nil then
  92. error(message)
  93. else
  94. if args == nil then
  95. _, result = pcall(cmd)
  96. else
  97. _, result = pcall(cmd, args)
  98. end
  99.  
  100. if _ == true then
  101. if result ~= nil then
  102. print(result)
  103. end
  104. else
  105. error(result)
  106. end
  107. end
  108. end
  109.  
  110. ------------------------------------------------
  111. -- console.bitwise ----------------------------
  112. ------------------------------------------------
  113. console.bitwise.hasflag = function(value, flag)
  114. return value % (2*flag) >= flag
  115. end
  116.  
  117. ------------------------------------------------
  118. -- string --------------------------------------
  119. ------------------------------------------------
  120. string.trim = function (str)
  121. return (string.gsub(str, "^%s*(.-)%s*$", "%1"))
  122. end
  123.  
  124. string.split = function (str, split)
  125. local output = {}
  126.  
  127. lastpos = 1
  128. while true do
  129. pos = string.find(str, split, lastpos)
  130. if pos == nil then
  131. table.insert(output, string.sub(str, lastpos))
  132. return output
  133. end
  134.  
  135. table.insert(output, string.sub(str, lastpos, pos-1))
  136. lastpos = pos + 1
  137. end
  138.  
  139. return { str }
  140. end
  141.  
  142. ------------------------------------------------
  143. -- console -------------------------------------
  144. ------------------------------------------------
  145. console.set = function(value)
  146. if console.active == 2 then
  147. if value == 0 then
  148. console.active = 0
  149. else
  150. console.active = 2
  151. end
  152.  
  153. return console.old.set_console(value)
  154. end
  155.  
  156. if value == 0 then
  157. console.active = 0
  158. tpt.hud(1)
  159. else
  160. console.old.set_pause(1)
  161. tpt.hud(0)
  162. console.active = 1
  163. end
  164. end
  165.  
  166. console.toggle = function()
  167. console.set(1-console.active)
  168. end
  169.  
  170. ------------------------------------------------
  171. -- console.old ---------------------------------
  172. ------------------------------------------------
  173. console.old = {
  174. active = 0,
  175. hud_value = 1,
  176. }
  177.  
  178. console.old.open = function()
  179. -- Old console quirks
  180. tpt.hud(1)
  181.  
  182. -- Open the old console
  183. console.active = 2
  184. console.old.set_console(1)
  185. end
  186.  
  187. ------------------------------------------------
  188. -- console.old.interop -------------------------
  189. ------------------------------------------------
  190. console.old.interop = {}
  191.  
  192. -- tpt.set_pause
  193. console.old.interop.set_pause = function(value)
  194. if console.active == 0 then
  195. console.old.set_pause(value)
  196. end
  197. end
  198. console.old.set_pause = tpt.set_pause
  199. tpt.set_pause = console.old.interop.set_pause
  200.  
  201. -- tpt.log
  202. console.old.interop.log = function(message)
  203. if console.active == 2 then
  204. return console.old.log(message)
  205. end
  206.  
  207. return print(message)
  208. end
  209. console.old.log = tpt.log
  210. tpt.log = console.old.interop.log
  211.  
  212. -- tpt.hud
  213. console.old.interop.hud = function(value)
  214. if value == 0 then
  215. console.old.hud_value = 0
  216. else
  217. console.old.hud_value = 1
  218. end
  219.  
  220. console.old.hud(value)
  221. end
  222. console.old.hud = tpt.hud
  223. tpt.hud = console.old.interop.hud
  224.  
  225. -- tpt.set_console
  226. console.old.set_console = tpt.set_console
  227. tpt.set_console = console.set
  228.  
  229. ------------------------------------------------
  230. -- console.command -----------------------------
  231. ------------------------------------------------
  232. console.command = {
  233. current = {},
  234. cursor = 0,
  235. }
  236.  
  237. console.command.writechar = function(self, char, foreground, background)
  238. self.cursor = self.cursor + 1
  239. table.insert(self.current, self.cursor, {char, foreground, background})
  240. end
  241.  
  242. console.command.clear = function(self)
  243. self.cursor = 0
  244. self.current = {}
  245. end
  246.  
  247. console.command.set = function(self, value)
  248. self.cursor = #value
  249. self.current = value
  250. end
  251.  
  252. console.command.left = function(self)
  253. if self.cursor > 0 then
  254. self.cursor = self.cursor - 1
  255. end
  256. end
  257.  
  258. console.command.right = function(self)
  259. if self.cursor < #self.current then
  260. self.cursor = self.cursor + 1
  261. end
  262. end
  263.  
  264. console.command.deleteback = function(self)
  265. if self.cursor > 0 then
  266. table.remove(self.current, self.cursor)
  267. self.cursor = self.cursor - 1
  268. end
  269. end
  270.  
  271. console.command.deleteforward = function(self)
  272. if self.cursor < #self.current then
  273. table.remove(self.current, self.cursor + 1)
  274. end
  275. end
  276.  
  277. console.command.get = function(self)
  278. local t = { }
  279.  
  280. for i = 1,#self.current do
  281. t[i] = self.current[i][1]
  282. end
  283.  
  284. return table.concat(t)
  285. end
  286.  
  287. console.command.run = function(self)
  288. console.history:store(self.current)
  289. local temp = console.command:get()
  290. self:clear()
  291. runcommand(temp)
  292. end
  293.  
  294. ------------------------------------------------
  295. -- console.buffer ------------------------------
  296. ------------------------------------------------
  297. console.buffer = {
  298. x = 1,
  299. y = 1,
  300. newline = 0,
  301. width = 85,
  302. height = 500,
  303. lines = {},
  304.  
  305. more = {
  306. enabled = false,
  307. }
  308. }
  309.  
  310. console.buffer.iter = function(self, selection, func)
  311. y = 0
  312. for i = selection.y+(1+self.y-selection.height), selection.y+self.y do
  313. if i < 1 then
  314. line = self.lines[self.height + i]
  315. else
  316. if i > self.height then
  317. line = self.lines[i-self.height]
  318. else
  319. line = self.lines[i]
  320. end
  321. end
  322.  
  323. if line ~= nil then
  324. x = 0
  325. for ch = selection.x + 1, selection.x+selection.width do
  326. char = line[ch]
  327.  
  328. if char == 0 then
  329. break
  330. end
  331.  
  332. if char ~= nil then
  333. func(x, y, char)
  334. end
  335.  
  336. x = x + 1
  337. end
  338. end
  339.  
  340. y = y + 1
  341. end
  342. end
  343.  
  344. console.buffer._write = function(char)
  345. self = console.buffer
  346.  
  347. if self.newline == true then
  348. self.y = self.y + 1
  349. self.lines[self.y] = {}
  350. self.newline = false
  351. end
  352.  
  353. if char == "\n" then
  354. self.newline = true
  355. self.x = 1
  356. elseif char == "\r" then
  357. self.x = 1
  358. else
  359. line = self.lines[self.y]
  360.  
  361. if line == nil then line = {} self.lines[self.y] = line end
  362.  
  363. line[self.x] = {char, self.cf, self.cb}
  364.  
  365. self.x = self.x + 1
  366. if self.x > self.width then
  367. self.y = self.y + 1
  368. self.x = 1
  369. self.lines[self.y] = {}
  370. end
  371. end
  372.  
  373. if self.y > self.height then
  374. self.y = 1
  375. self.lines[1] = {}
  376. end
  377. end
  378.  
  379. console.buffer.write = function(text, colorF, colorB)
  380. self = console.buffer
  381.  
  382. self.cf = colorF
  383. self.cb = colorB
  384.  
  385. string.gsub(tostring(text), ".", console.buffer._write)
  386. end
  387. console.write = console.buffer.write
  388.  
  389. console.buffer.clear = function()
  390. console.buffer.lines = {}
  391. end
  392. console.clear = console.buffer.clear
  393.  
  394. console.buffer.writeln = function(text, colorF, colorB)
  395. console.buffer.write(tostring(text) .. "\n", colorF, colorB)
  396. end
  397. console.writeln = console.buffer.writeln
  398.  
  399. ------------------------------------------------
  400. -- console.screen ------------------------------
  401. ------------------------------------------------
  402. console.screen = {
  403. x = 0,
  404. y = 0,
  405. height = 40,
  406. width = 85,
  407. fontx = 7,
  408. fonty = 9,
  409. offsetx = 8,
  410. offsety = 10,
  411. cursors = {},
  412.  
  413. commandwidth = 70,
  414. commandstart = 1,
  415. }
  416.  
  417. console.screen.right = function(self)
  418. self.x = self.x + 1
  419. if self.x > console.buffer.width - self.width then
  420. self.x = 0
  421. end
  422. end
  423.  
  424. console.screen.left = function(self)
  425. self.x = self.x - 1
  426. if self.x < 0 then
  427. self.x = 0
  428. end
  429. end
  430.  
  431. console.screen.down = function(self)
  432. self.y = self.y + 1
  433. if self.y > console.buffer.height - 1 then
  434. self.y = 0
  435. end
  436. end
  437.  
  438. console.screen.up = function(self)
  439. self.y = self.y - 1
  440. if self.y < 0 then
  441. self.y = console.buffer.height - 1
  442. end
  443. end
  444.  
  445. function g_drawchar(x, y, w, h, char, fg, bg)
  446. self = console.screen
  447.  
  448. if bg ~= 0 then
  449. tpt.fillrect(x - 2, y - 2, w, h, bg[1], bg[2], bg[3], bg[4])
  450. end
  451.  
  452. tpt.drawtext(x, y, char, fg[1], fg[2], fg[3], fg[4])
  453. end
  454.  
  455. -- Draw each character to the console
  456. console.screen._draw = function(x, y, char)
  457. self = console.screen
  458.  
  459. rx = self.offset
  460.  
  461. g_drawchar(
  462. self.offsetx + x * self.fontx,
  463. self.offsety + y * self.fonty,
  464. self.fontx + 2,
  465. self.fonty + 2,
  466. char[1],
  467. console.colors.get(char[2], console.colors.foreground),
  468. console.colors.get(char[3], console.colors.background)
  469. )
  470. end
  471.  
  472. -- Set cursor type
  473. console.screen.setcursor = function(self, cursor)
  474. if cursor == nil then
  475. cursor = self
  476. self = console.screen
  477. end
  478.  
  479. c = self.cursors[cursor]
  480.  
  481. if c == nil then
  482. error("A cursor with the name '" .. cursor .. "' does not exist")
  483. return
  484. end
  485.  
  486. self.cursor = self.cursors[cursor]
  487. end
  488.  
  489. -- Draw the entire console
  490. console.screen.draw = function(self)
  491. cbk = console.colors.backdrop
  492. cbr = console.colors.border
  493. cln = console.colors.line
  494.  
  495. tpt.fillrect(5, 5, 600, 380, cbk[1], cbk[2], cbk[3], cbk[4])
  496. tpt.drawline(5, 372, 605, 372, cln[1], cln[2], cln[3], cln[4])
  497. tpt.drawrect(5, 5, 600, 380, cbr[1], cbr[2], cbr[3], cbr[4])
  498.  
  499. res, error_message = pcall(console.buffer.iter, console.buffer, console.screen, console.screen._draw)
  500. tpt.drawtext(0, 0, error_message)
  501.  
  502. -- Scrolling for the cursor
  503. len = #console.command.current+1
  504. c = console.command.cursor
  505.  
  506. if self.commandstart > len then
  507. self.commandstart = len
  508. end
  509.  
  510. if len > self.commandwidth then
  511. bound = 3
  512.  
  513.  
  514. if c < (self.commandstart + bound - 1) then
  515. self.commandstart = c - bound + 1
  516.  
  517. if self.commandstart < 1 then
  518. self.commandstart = 1
  519. end
  520.  
  521. rend = self.commandstart + self.commandwidth - 1
  522. elseif c > (self.commandstart + self.commandwidth - bound - 2) then
  523. rend = c + bound + 1
  524.  
  525. if rend > len then
  526. rend = len
  527. end
  528.  
  529. self.commandstart = rend - self.commandwidth + 1
  530. end
  531.  
  532. rend = self.commandstart + self.commandwidth - 2
  533. else
  534. self.commandstart = 1
  535. rend = len
  536. end
  537. -- End scrolling cursor
  538.  
  539. c = c - (self.commandstart - 1)
  540.  
  541. if self.cursor.stage == 0 then
  542. self.cursor:run(self, c)
  543. end
  544.  
  545. if console.command.current == nil then
  546. tpt.drawtext(8, 375, "current command is nil", 255, 0, 0)
  547. else
  548. g_drawchar(
  549. 8,
  550. 375,
  551. self.fontx + 2,
  552. self.fonty + 2,
  553. '>',
  554. console.colors.command,
  555. console.colors.commandbg
  556. )
  557.  
  558. x = 1
  559. for i = self.commandstart,rend do
  560. char = console.command.current[i]
  561.  
  562. if char ~= nil then
  563. g_drawchar(
  564. 12 + self.fontx * x,
  565. 375,
  566. self.fontx + 2,
  567. self.fonty + 2,
  568. char[1],
  569. console.colors.get(char[2], console.colors.command),
  570. console.colors.get(char[3], console.colors.commandbg)
  571. )
  572. end
  573. x = x + 1
  574. end
  575. end
  576.  
  577. if self.cursor.stage == 1 then
  578. self.cursor:run(self, c)
  579. end
  580. end
  581.  
  582. ------------------------------------------------
  583. -- console.screen.cursors ----------------------
  584. ------------------------------------------------
  585. console.screen.cursors = {
  586. }
  587.  
  588. console.screen.cursors.line = {
  589. stage = 0,
  590.  
  591. run = function(self, screen, cursorpos)
  592. if os.clock() % 0.8 <= 0.4 then
  593. c = console.colors.cursor
  594. x = 19 + screen.fontx * cursorpos
  595. tpt.drawline(
  596. x,
  597. 375,
  598. x,
  599. 382,
  600. c[1],
  601. c[2],
  602. c[3],
  603. c[4]
  604. )
  605. end
  606. end,
  607. }
  608.  
  609. console.screen.cursors.block = {
  610. stage = 0,
  611.  
  612. run = function(self, screen, cursorpos)
  613. if os.clock() % 0.8 <= 0.4 then
  614. c = console.colors.cursor
  615.  
  616. tpt.fillrect(
  617. 17 + screen.fontx * cursorpos,
  618. 373,
  619. screen.fontx + 1,
  620. screen.fonty + 1,
  621. c[1],
  622. c[2],
  623. c[3],
  624. c[4])
  625. end
  626. end,
  627. }
  628.  
  629. console.screen.cursor = console.screen.cursors.block
  630.  
  631. ------------------------------------------------
  632. -- console.colors ------------------------------
  633. ------------------------------------------------
  634. console.colors = {
  635. black = { 0, 0, 0, 255},
  636. blue = { 0, 0, 255, 255},
  637. cyan = { 0, 255, 255, 255},
  638. darkblue = { 0, 0, 178, 255},
  639. darkcyan = { 0, 178, 178, 255},
  640. darkgreen = { 0, 178, 0, 255},
  641. darkpurple = {178, 0, 178, 255},
  642. darkred = {178, 0, 0, 255},
  643. darkyellow = {178, 178, 0, 255},
  644. default = {178, 32, 255, 255},
  645. gray = {178, 178, 178, 255},
  646. green = { 0, 255, 0, 255},
  647. purple = {255, 0, 255, 255},
  648. red = {255, 0, 0, 255},
  649. white = {255, 255, 255, 255},
  650. yellow = {255, 255, 0, 255},
  651. cursorg = { 0, 255, 32, 120},
  652. }
  653.  
  654. -- bound colors
  655. console.colors.command = console.colors.default -- foreground for current command
  656. console.colors.commandbg = 0 -- background for current command
  657. console.colors.cursor = console.colors.cursorg -- color for the cursor
  658. console.colors.foreground = console.colors.default -- default foreground color
  659. console.colors.background = 0 -- background of nil won't draw anything
  660. console.colors.error = console.colors.red -- default foreground for errors
  661. console.colors.errorbg = 0 -- default background for errors
  662. console.colors.backdrop = {0,0,0,178} -- backdrop, the color behind the text
  663. console.colors.border = console.colors.white -- border to the console
  664. console.colors.line = console.colors.white -- line which seperates console output from input
  665. console.colors.hud_motd = console.colors.default -- message of the day display
  666. console.colors.hud_debug = console.colors.default -- debug display
  667. console.colors.hud_close = console.colors.default -- close message when old console is opened
  668.  
  669. console.colors.get = function(color, default)
  670. if color == nil then
  671. return default
  672. end
  673.  
  674. if type(color) == "table" then
  675. if #color == 3 then
  676. table.insert(color, 255)
  677. elseif #color ~= 4 then
  678. error("Color must be a table of 3 or 4 numbers")
  679. return default
  680. end
  681.  
  682. return color
  683. end
  684.  
  685. c = console.colors[color]
  686.  
  687. if c == nil then
  688. error("Unknown color '" .. color .. "'")
  689. return default
  690. end
  691.  
  692. return c
  693. end
  694.  
  695. ------------------------------------------------
  696. -- console.keys --------------------------------
  697. ------------------------------------------------
  698. console.keys = {
  699. binds = {},
  700. numbers = {},
  701. symbols = {},
  702. modifiers = {},
  703.  
  704. -- key information
  705. lastkey = 0,
  706. lastmod = 0,
  707. lastks = 0, -- Last key pressed (as string)
  708. lastpks = 0, -- Last key pressed (processed)
  709.  
  710. down = 0,
  711. downstart = 0,
  712. repeatDelay = 0.5,
  713. repeatTimer = 0.02,
  714.  
  715. pressedvkey = {},
  716. }
  717.  
  718. console.keys.vkey = {
  719. shift = 0x001,
  720. shift_left = 0x002,
  721. shift_right = 0x004,
  722. ctrl = 0x008,
  723. ctrl_left = 0x010,
  724. ctrl_right = 0x020,
  725. alt = 0x040,
  726. alt_left = 0x080,
  727. alt_right = 0x100,
  728. caps_lock = 0x200,
  729. shift_lock = 0x400,
  730. num_lock = 0x800,
  731. }
  732.  
  733. console.keys.isvkeydown = function(self, search)
  734. if type(search) == "table" then
  735. matched = 0
  736. for i=1,#search do
  737. if self.pressedvkey[search[i]] == true then
  738. matched = matched + 1
  739. end
  740. end
  741. return matched == #search
  742. end
  743.  
  744. return self.pressedvkey[search] == true
  745. end
  746.  
  747. console.keys.default = function(self)
  748. self:clear()
  749.  
  750. -- Shifted Numbers Map
  751. self.numbers = {")", "!", "@", "#", "$", "%", "^", "&", "*", "("}
  752.  
  753. -- Shifted Symbols Map
  754. self.symbols["-"] = "_"
  755. self.symbols["="] = "+"
  756. self.symbols["["] = "{"
  757. self.symbols["]"] = "}"
  758. self.symbols[";"] = ":"
  759. self.symbols["'"] = "\""
  760. self.symbols[","] = "<"
  761. self.symbols["."] = ">"
  762. self.symbols["/"] = "?"
  763. self.symbols["\\"] = "|"
  764.  
  765. -- Modifier key map
  766. -- No bind is -1
  767. self.modifiers = {
  768. { 1, self.vkey.shift, self.vkey.shift_left},
  769. { 2, self.vkey.shift, self.vkey.shift_right},
  770. { 64, self.vkey.ctrl , self.vkey.ctrl_left},
  771. {128, self.vkey.ctrl , self.vkey.ctrl_right},
  772. {256, self.vkey.alt , self.vkey.alt_left},
  773. {512, self.vkey.alt , self.vkey.alt_left},
  774. {8192, self.vkey.caps_lock}
  775. }
  776.  
  777. -- Set binds
  778. -- -1 Called on ANY key mod (only when no other key mod bind exists)
  779. -- -2 Always called when button is pressed
  780. self:bind(8, -1, function() console.command:deleteback() end) -- Backspace
  781. self:bind(8, self.vkey.ctrl, function() console.command:clear() end) -- Ctrl+Backspace
  782. self:bind(13, -1, function() console.command:run() end) -- Enter
  783. self:bind(27, -1, function() console.set(0) end) -- Esc
  784. self:bind(96, -1, function() console.set(0) end) -- ` - Console Key
  785. self:bind(127, -1, function() console.command:deleteforward() end) -- Delete
  786. self:bind(273, -1, function() console.history:next() end) -- Up
  787. self:bind(274, -1, function() console.history:prev() end) -- Down
  788. self:bind(275, -1, function() console.command:right() end) -- Right
  789. self:bind(276, -1, function() console.command:left() end) -- Left
  790. self:bind(278, -1, function() console.screen:left() end) -- Home
  791. self:bind(279, -1, function() console.screen:right() end) -- End
  792. self:bind(280, -1, function() console.screen:up() end) -- Page Up
  793. self:bind(281, -1, function() console.screen:down() end) -- Page Down
  794. end
  795.  
  796. -- default keys should work for US style keyboards
  797. console.keys.us = console.keys.default
  798.  
  799. console.keys.clear = function(self)
  800. self.binds = {}
  801. self.numbers = {}
  802. self.symbols = {}
  803. self.modifiers = {}
  804. end
  805.  
  806. -- Repeat the key if held down for longer than 0.5 seconds
  807. console.keys.heldkey = function(self)
  808. if self.down == 1 then
  809. if os.clock() - self.downstart > self.repeatDelay then
  810. self.down = 2
  811. end
  812. end
  813. if self.down == 2 and (os.clock() - self.downstart) > self.repeatTimer then
  814. downstart = os.clock()
  815. console.keys:process(self.lastks, self.lastkey, self.lastmod, 3)
  816.  
  817. -- HACK: down is set within console.keys.process, set it back to 2
  818. self.down = 2
  819. end
  820. end
  821.  
  822. console.keys.bind = function(self, key, modifier, func)
  823. if self.binds[key] == nil then
  824. self.binds[key] = {}
  825. end
  826.  
  827. self.binds[key][modifier] = func
  828. end
  829.  
  830. console.keys.callbind = function(self, bind, vkey)
  831. for i,v in pairs(bind) do
  832. if self:isvkeydown(i) then
  833. v()
  834. return
  835. end
  836. end
  837.  
  838. f = bind[-1]
  839.  
  840. if f ~= nil then
  841. f()
  842. end
  843. end
  844.  
  845. console.keys.process = function(self, keyString, key, modifier, event)
  846. self.lastkey = key
  847. self.lastks = keyString
  848. self.lastpks = keyString
  849.  
  850. self.down = 2 - event
  851. self.downstart = os.clock()
  852.  
  853. -- Handle keys closing old console
  854. if console.active == 2 and (key == 96 or key == 27) then
  855. console.active = 0
  856. end
  857.  
  858. -- Update key modifier
  859. if self.lastmod ~= modifier then
  860. self.pressedvkey = {}
  861.  
  862. if modifier ~= 0 then
  863. for i=1,#self.modifiers do
  864. m = self.modifiers[i]
  865. f = m[1]
  866.  
  867. if modifier % (2*f) >= f then
  868. for ii=2,#m do
  869. if self.pressedvkey[m[ii]] ~= true then
  870. self.pressedvkey[m[ii]] = true
  871. end
  872. end
  873. end
  874. end
  875. end
  876. end
  877. self.lastmod = modifier
  878.  
  879. -- Override keys
  880. if console.active == 0 and event == 1 then
  881. -- ` - Console Key
  882. if key == 96 then
  883. if console.active == 0 and (modifier == 1 or modifier == 2) then
  884. console.old.open()
  885. return false
  886. end
  887.  
  888. console.toggle()
  889. return false
  890. end
  891.  
  892. -- d - Debug Mode
  893. if key == 100 and event == 1 then
  894. console.debugmode = 1 - console.debugmode
  895. end
  896.  
  897. -- h - Hud Toggle
  898. if key == 104 and event == 1 then
  899. tpt.hud(1 - console.old.hud_value)
  900. return false
  901. end
  902. end
  903.  
  904. if console.active == 1 then
  905. if event == 2 then
  906. return false
  907. end
  908.  
  909. -- Execute bind for key
  910. bind = self.binds[key]
  911.  
  912. if bind ~= nil then
  913. self:callbind(bind, self.pressedvkey)
  914. return false
  915. end
  916.  
  917. -- Ignore any special keys
  918. if key >= 256 then
  919. return false
  920. end
  921.  
  922. -- Mappings for shifted keys
  923. if modifier == 1 or modifier == 2 then
  924. if key >= 48 and key <= 57 then
  925. keyString = self.numbers[key - 47]
  926. elseif key >= 97 and key <= 122 then
  927. keyString = string.char(key - 97 + 65)
  928. else
  929. local temp = self.symbols[keyString]
  930.  
  931. if temp ~= nil then
  932. keyString = temp
  933. end
  934. end
  935. end
  936.  
  937. console.command:writechar(keyString)
  938. return false
  939. end
  940. end
  941.  
  942. ------------------------------------------------
  943. -- console.history -----------------------------
  944. ------------------------------------------------
  945. console.history = {
  946. contents = {},
  947. maxsize = 30,
  948. size = 0,
  949. }
  950.  
  951. console.history.store = function(self, value)
  952. self.size = self.size + 1
  953. table.insert(self.contents, 1, value)
  954. self.selected = 0
  955. end
  956.  
  957. console.history.next = function(self)
  958. if self.size == 0 then
  959. return
  960. end
  961. self.selected = self.selected + 1
  962.  
  963. if self.selected > self.size then
  964. self.selected = 1
  965. end
  966.  
  967. console.command:set(self.contents[self.selected])
  968. end
  969.  
  970. console.history.prev = function(self)
  971. if self.size == 0 then
  972. return
  973. end
  974. self.selected = self.selected - 1
  975.  
  976. if self.selected <= 0 then
  977. self.selected = self.size
  978. end
  979.  
  980. console.command:set(self.contents[self.selected])
  981. end
  982.  
  983. console.history.select = function(self, n)
  984. if self.size == 0 then
  985. return
  986. end
  987.  
  988. n = n%self.size
  989. print(n)
  990. self.selected = (self.selected + n)
  991.  
  992. print(self.selected)
  993. console.command:set(self.contents[self.selected])
  994. end
  995.  
  996. ------------------------------------------------
  997. -- console.quick -------------------------------
  998. ------------------------------------------------
  999. local function console_quick_help()
  1000. print("!cls - Clear screen")
  1001. print("!color [name] <color> - Set color class to specified color")
  1002. print("!cursor [name] - set current cursor to cursor specified")
  1003. print("!e <text> - Echo text")
  1004. print("!r <file> - Run a file")
  1005. print("!help - Display this text")
  1006. end
  1007.  
  1008. console.quick[""] = console_quick_help
  1009. console.quick["help"] = console_quick_help
  1010.  
  1011. console.quick["e"] = console.buffer.writeln
  1012. console.quick["cls"] = console.clear
  1013. console.quick["cursor"] = console.screen.setcursor
  1014.  
  1015. console.quick["color"] = function(arguments)
  1016. args = string.split(arguments, " ")
  1017.  
  1018. if #args == 1 then
  1019. name = "foreground"
  1020. color = args[1]
  1021. elseif #args == 2 then
  1022. name = args[1]
  1023. color = args[2]
  1024. else
  1025. error("!color [name] <color>")
  1026. return
  1027. end
  1028.  
  1029. c = string.split(color,",")
  1030.  
  1031. if #c == 1 then
  1032. if color == "nil" then
  1033. console.colors[name] = 0
  1034. return
  1035. end
  1036.  
  1037. c = console.colors.get(color)
  1038.  
  1039. if c == nil then
  1040. return
  1041. end
  1042. elseif #c == 3 or #c == 4 then
  1043. alpha = 255
  1044.  
  1045. if #c == 4 then
  1046. alpha = tonumber(c[4])
  1047. end
  1048.  
  1049. c = {
  1050. tonumber(c[1]),
  1051. tonumber(c[2]),
  1052. tonumber(c[3]),
  1053. alpha
  1054. }
  1055. end
  1056.  
  1057. console.colors[name] = c
  1058. end
  1059.  
  1060. -- Function for running a file named filename or filename.lua
  1061. console.quick["r"] = function(filename)
  1062. if string.sub(filename, -4) ~= ".lua" then
  1063. f, msg = io.open(filename, "r")
  1064.  
  1065. if not f then
  1066. f, msg = io.open(filename .. ".lua", "r")
  1067. if f then
  1068. f:close()
  1069. return dofile(filename .. ".lua")
  1070. end
  1071. else
  1072. f:close()
  1073. end
  1074. end
  1075. return dofile(filename)
  1076. end
  1077.  
  1078. ------------------------------------------------
  1079. -- LETS DO THIS! -------------------------------
  1080. ------------------------------------------------
  1081. local function keyHandler(keyString, key, modifier, event)
  1082. return console.keys:process(keyString, key, modifier, event)
  1083. end
  1084.  
  1085. motd = 1
  1086. motdstart = 0
  1087.  
  1088. local function mouseHandler(mousex, mousey, button, event)
  1089. if motd == 1 then
  1090. motdstart = os.clock()
  1091. motd = 0
  1092. end
  1093.  
  1094. if console.active == 1 then
  1095. return false
  1096. end
  1097.  
  1098. return true
  1099. end
  1100.  
  1101. local function draw()
  1102. if console.active == 1 then
  1103. console.keys:heldkey()
  1104. console.screen:draw()
  1105. end
  1106.  
  1107. if motd == 1 then
  1108. alpha = 1
  1109. else
  1110. alpha = 1 - (os.clock() - motdstart)
  1111. end
  1112.  
  1113. if alpha ~= 0 then
  1114. c = console.colors.hud_motd
  1115. tpt.drawtext(15, 335, "The all lua-console V" .. console.version .. " by devast8a is installed!", c[1], c[2], c[3], c[4] * alpha)
  1116. end
  1117.  
  1118. if console.debugmode == 1 then
  1119. x = 16
  1120. y = 27
  1121.  
  1122. c = console.colors.hud_debug
  1123. if console.old.hud_value == 1 then
  1124. tpt.drawtext(16, 27, "lua-console (V" .. console.version .. ") (" .. console.keys.lastkey .. ")", c[1], c[2], c[3], c[4])
  1125. end
  1126. if console.active == 1 then
  1127. tpt.drawtext(575, 8, "[" .. console.keys.lastkey .. "]", c[1], c[2], c[3], c[4])
  1128. end
  1129. end
  1130.  
  1131. if console.active == 2 then
  1132. c = console.colors.hud_close
  1133. tpt.drawtext(5, 228, "Old console [Press escape to close] -- lua-console", c[1], c[2], c[3], c[4])
  1134. end
  1135. end
  1136.  
  1137. tpt.register_step(draw)
  1138. tpt.register_keypress(keyHandler)
  1139. tpt.register_mouseclick(mouseHandler)
  1140.  
  1141. console.old.log("lua-console loaded successfully")
  1142.  
  1143. tpt.log("The all lua-console V" .. console.version)
  1144. tpt.log(" by devast8a")
  1145. tpt.log("")
  1146. tpt.log("Page Up/Down - move console screen up/down")
  1147. -- Home/End - move console left/right (Only useful when buffer is larger than screen)
  1148. tpt.log("Up/Down - cycle through command history")
  1149. tpt.log("")
  1150. tpt.log("Commands starting with ! don't work, instead close this console")
  1151. tpt.log(" and press shift + console key to use the old console.")
  1152.  
  1153. ------------------------------------------------
  1154. -- Last initialization steps -------------------
  1155. ------------------------------------------------
  1156. console.keys:us() -- keyboard mapping
Advertisement
Add Comment
Please, Sign In to add comment