Advertisement
killer64

obit01

Apr 24th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.98 KB | None | 0 0
  1. -- the function that is called so many times
  2. function registerKey(screen, key, text, xPos, yPos, width, height, func, backColor, textColor)
  3.   assert(views[screen], 'A screen with the key \''..screen..'\' does not exist', 2)
  4.   assert(not views[screen][key], 'A key with the key \''..screen..'\' already exists', 2)
  5.   assert(type(text) == 'string', 'Invalid parameter: expected text to be a string, got '..type(text), 2)
  6.   assert(type(xPos) == 'number', 'Invalid parameter: expected xPos to be a number, got '..type(xPos), 2)
  7.   assert(type(yPos) == 'number', 'Invalid parameter: expected yPos to be a number, got '..type(yPos), 2)
  8.   assert(type(width) == 'number' or type(width) == 'nil', 'Invalid parameter: expected width to be a number or nil, got '..type(width), 2)
  9.   assert(type(height) == 'number' or type(height) == 'nil', 'Invalid parameter: expected height to be a number or nil, got '..type(height), 2)
  10.   assert(type(func) == 'function', 'Invalid parameter: expected func to be a function, got '..type(func), 2)
  11.   assert(type(backColor) == 'number' and backColor >= 2^0 and backColor <= 2^15, 'Invalid parameter: expected backColor to be a number color, got '..type(backColor), 2)
  12.   assert(type(textColor) == 'number' and textColor >= 2^0 and textColor <= 2^15, 'Invalid parameter: expected textColor to be a number color, got '..type(textColor), 2)
  13.  
  14.   views[screen][key] = {}
  15.   views[screen][key]._t = text
  16.   views[screen][key]._x = xPos
  17.   views[screen][key]._y = yPos
  18.   views[screen][key]._w = width or #text
  19.   views[screen][key]._h = height or 1
  20.   views[screen][key]._bc = backColor
  21.   views[screen][key]._tc = textColor
  22.   views[screen][key].onClick = key == 'SF' and func or function( ... )
  23. func(...)
  24. shiftPressed = false -- turn off shift, we have done our capital letter
  25.   end
  26.   views[screen][key].wasClicked = function(self, mouse_x, mouse_y)
  27. return mouse_x >= self._x + offset_x and mouse_x <= self._x + offset_x + self._w - 1 and mouse_y >= self._y + offset_y and mouse_y <= self._y + offset_y + self._h - 1
  28.   end
  29.   views[screen][key].onDraw = function(self)
  30. if self._t == '^ ' and shiftPressed then
  31.   term.setBackgroundColor(colors.lightBlue)
  32. else
  33.   term.setBackgroundColor(self._bc)
  34. end
  35. term.setTextColor(self._tc)
  36. for y = 0, self._h - 1 do
  37.   term.setCursorPos(offset_x + self._x, offset_y + self._y + y)
  38.   write( string.rep(' ', self._w) )
  39. end
  40.  
  41. term.setCursorPos(offset_x + self._x + math.floor(self._w / 2 - #self._t / 2), offset_y + self._y + math.floor(self._h / 2))
  42. if shiftPressed and not (self._t == '  space  ' or self._t == 'enter') then
  43.   write(string.upper(self._t) or '?')
  44. else
  45.   write(self._t or '?')
  46. end
  47.   end
  48. end
  49. -- the useless wall of text where the only change between them is a couple numbers and a character, it also wastes time by compiling the same damn function over and over
  50. registerKey('ABC', 'q', 'q', 1, 1, nil, nil, function() os.queueEvent('key', keys.q) os.queueEvent('char', shiftPressed and 'Q' or 'q') end, colors.lightGray, colors.black)
  51. registerKey('ABC', 'w', 'w', 3, 1, nil, nil, function() os.queueEvent('key', keys.w) os.queueEvent('char', shiftPressed and 'W' or 'w') end, colors.lightGray, colors.black)
  52. registerKey('ABC', 'e', 'e', 5, 1, nil, nil, function() os.queueEvent('key', keys.e) os.queueEvent('char', shiftPressed and 'E' or 'e') end, colors.lightGray, colors.black)
  53. registerKey('ABC', 'r', 'r', 7, 1, nil, nil, function() os.queueEvent('key', keys.r) os.queueEvent('char', shiftPressed and 'R' or 'r') end, colors.lightGray, colors.black)
  54. registerKey('ABC', 't', 't', 9, 1, nil, nil, function() os.queueEvent('key', keys.t) os.queueEvent('char', shiftPressed and 'T' or 't') end, colors.lightGray, colors.black)
  55. registerKey('ABC', 'y', 'y', 11, 1, nil, nil, function() os.queueEvent('key', keys.y) os.queueEvent('char', shiftPressed and 'Y' or 'y') end, colors.lightGray, colors.black)
  56. registerKey('ABC', 'u', 'u', 13, 1, nil, nil, function() os.queueEvent('key', keys.u) os.queueEvent('char', shiftPressed and 'U' or 'u') end, colors.lightGray, colors.black)
  57. registerKey('ABC', 'i', 'i', 15, 1, nil, nil, function() os.queueEvent('key', keys.i) os.queueEvent('char', shiftPressed and 'I' or 'i') end, colors.lightGray, colors.black)
  58. registerKey('ABC', 'o', 'o', 17, 1, nil, nil, function() os.queueEvent('key', keys.o) os.queueEvent('char', shiftPressed and 'O' or 'o') end, colors.lightGray, colors.black)
  59. registerKey('ABC', 'p', 'p', 19, 1, nil, nil, function() os.queueEvent('key', keys.p) os.queueEvent('char', shiftPressed and 'P' or 'p') end, colors.lightGray, colors.black)
  60. registerKey('ABC', 'a', 'a', 2, 2, nil, nil, function() os.queueEvent('key', keys.a) os.queueEvent('char', shiftPressed and 'A' or 'a') end, colors.lightGray, colors.black)
  61. registerKey('ABC', 's', 's', 4, 2, nil, nil, function() os.queueEvent('key', keys.s) os.queueEvent('char', shiftPressed and 'S' or 's') end, colors.lightGray, colors.black)
  62. registerKey('ABC', 'd', 'd', 6, 2, nil, nil, function() os.queueEvent('key', keys.d) os.queueEvent('char', shiftPressed and 'D' or 'd') end, colors.lightGray, colors.black)
  63. registerKey('ABC', 'f', 'f', 8, 2, nil, nil, function() os.queueEvent('key', keys.f) os.queueEvent('char', shiftPressed and 'F' or 'f') end, colors.lightGray, colors.black)
  64. registerKey('ABC', 'g', 'g', 10, 2, nil, nil, function() os.queueEvent('key', keys.g) os.queueEvent('char', shiftPressed and 'G' or 'g') end, colors.lightGray, colors.black)
  65. registerKey('ABC', 'h', 'h', 12, 2, nil, nil, function() os.queueEvent('key', keys.h) os.queueEvent('char', shiftPressed and 'H' or 'h') end, colors.lightGray, colors.black)
  66. registerKey('ABC', 'j', 'j', 14, 2, nil, nil, function() os.queueEvent('key', keys.j) os.queueEvent('char', shiftPressed and 'J' or 'j') end, colors.lightGray, colors.black)
  67. registerKey('ABC', 'k', 'k', 16, 2, nil, nil, function() os.queueEvent('key', keys.k) os.queueEvent('char', shiftPressed and 'K' or 'k') end, colors.lightGray, colors.black)
  68. registerKey('ABC', 'l', 'l', 18, 2, nil, nil, function() os.queueEvent('key', keys.l) os.queueEvent('char', shiftPressed and 'L' or 'l') end, colors.lightGray, colors.black)
  69. registerKey('ABC', 'SF', '^ ', 1, 3, nil, nil, function() os.queueEvent('key', keys.leftShift) shiftPressed = not shiftPressed end, colors.lightGray, colors.black)
  70. registerKey('ABC', 'z', 'z', 4, 3, nil, nil, function() os.queueEvent('key', keys.z) os.queueEvent('char', shiftPressed and 'Z' or 'z') end, colors.lightGray, colors.black)
  71. registerKey('ABC', 'x', 'x', 6, 3, nil, nil, function() os.queueEvent('key', keys.x) os.queueEvent('char', shiftPressed and 'X' or 'x') end, colors.lightGray, colors.black)
  72. registerKey('ABC', 'c', 'c', 8, 3, nil, nil, function() os.queueEvent('key', keys.c) os.queueEvent('char', shiftPressed and 'C' or 'c') end, colors.lightGray, colors.black)
  73. registerKey('ABC', 'v', 'v', 10, 3, nil, nil, function() os.queueEvent('key', keys.v) os.queueEvent('char', shiftPressed and 'V' or 'v') end, colors.lightGray, colors.black)
  74. registerKey('ABC', 'b', 'b', 12, 3, nil, nil, function() os.queueEvent('key', keys.b) os.queueEvent('char', shiftPressed and 'B' or 'b') end, colors.lightGray, colors.black)
  75. registerKey('ABC', 'n', 'n', 14, 3, nil, nil, function() os.queueEvent('key', keys.n) os.queueEvent('char', shiftPressed and 'N' or 'n') end, colors.lightGray, colors.black)
  76. registerKey('ABC', 'm', 'm', 16, 3, nil, nil, function() os.queueEvent('key', keys.m) os.queueEvent('char', shiftPressed and 'M' or 'm') end, colors.lightGray, colors.black)
  77. registerKey('ABC', 'BK', '<-', 18, 3, nil, nil, function() os.queueEvent('key', keys.backspace) end, colors.lightGray, colors.black)
  78. registerKey('ABC', 'SP_123', '123', 1, 4, nil, nil, function() setActiveScreen('123') end, colors.lightGray, colors.black)
  79. registerKey('ABC', 'SB', '  space  ', 5, 4, nil, nil, function() os.queueEvent('key', keys.space) os.queueEvent('char', ' ') end, colors.lightGray, colors.black)
  80. registerKey('ABC', 'EN', 'enter', 15, 4, nil, nil, function() os.queueEvent('key', keys.enter) end, colors.lightGray, colors.black)
  81. registerKey('123', '1', '1', 1, 1, nil, nil, function() os.queueEvent('key', keys.one) os.queueEvent('char', '1') end, colors.lightGray, colors.black)
  82. registerKey('123', '2', '2', 3, 1, nil, nil, function() os.queueEvent('key', keys.two) os.queueEvent('char', '2') end, colors.lightGray, colors.black)
  83. registerKey('123', '3', '3', 5, 1, nil, nil, function() os.queueEvent('key', keys.three) os.queueEvent('char', '3') end, colors.lightGray, colors.black)
  84. registerKey('123', '4', '4', 7, 1, nil, nil, function() os.queueEvent('key', keys.four) os.queueEvent('char', '4') end, colors.lightGray, colors.black)
  85. registerKey('123', '5', '5', 9, 1, nil, nil, function() os.queueEvent('key', keys.five) os.queueEvent('char', '5') end, colors.lightGray, colors.black)
  86. registerKey('123', '6', '6', 11, 1, nil, nil, function() os.queueEvent('key', keys.six) os.queueEvent('char', '6') end, colors.lightGray, colors.black)
  87. registerKey('123', '7', '7', 13, 1, nil, nil, function() os.queueEvent('key', keys.seven) os.queueEvent('char', '7') end, colors.lightGray, colors.black)
  88. registerKey('123', '8', '8', 15, 1, nil, nil, function() os.queueEvent('key', keys.eight) os.queueEvent('char', '8') end, colors.lightGray, colors.black)
  89. registerKey('123', '9', '9', 17, 1, nil, nil, function() os.queueEvent('key', keys.nine) os.queueEvent('char', '9') end, colors.lightGray, colors.black)
  90. registerKey('123', '0', '0', 19, 1, nil, nil, function() os.queueEvent('key', keys.zero) os.queueEvent('char', '0') end, colors.lightGray, colors.black)
  91. registerKey('123', 'minus', '-', 2, 2, nil, nil, function() os.queueEvent('key', keys.minus) os.queueEvent('char', '-') end, colors.lightGray, colors.black)
  92. registerKey('123', 'slash', '/', 4, 2, nil, nil, function() os.queueEvent('key', keys.slash) os.queueEvent('char', '/') end, colors.lightGray, colors.black)
  93. registerKey('123', 'colon', ':', 6, 2, nil, nil, function() os.queueEvent('key', keys.colon) os.queueEvent('char', ':') end, colors.lightGray, colors.black)
  94. registerKey('123', 'semicolon', ';', 8, 2, nil, nil, function() os.queueEvent('key', keys.colon) os.queueEvent('char', ';') end, colors.lightGray, colors.black)
  95. registerKey('123', 'lbracket', '(', 10, 2, nil, nil, function() os.queueEvent('key', keys.nine) os.queueEvent('char', '(') end, colors.lightGray, colors.black)
  96. registerKey('123', 'rbrakcet', ')', 12, 2, nil, nil, function() os.queueEvent('key', keys.zero) os.queueEvent('char', ')') end, colors.lightGray, colors.black)
  97. registerKey('123', 'dollars', '$', 14, 2, nil, nil, function() os.queueEvent('key', keys.four) os.queueEvent('char', '$') end, colors.lightGray, colors.black)
  98. registerKey('123', 'amp', '&', 16, 2, nil, nil, function() os.queueEvent('key', keys.seven) os.queueEvent('char', '&') end, colors.lightGray, colors.black)
  99. registerKey('123', 'at', '@', 18, 2, nil, nil, function() os.queueEvent('key', keys.two) os.queueEvent('char', '@') end, colors.lightGray, colors.black)
  100. registerKey('123', 'CYC_#+=', '#=', 1, 3, nil, nil, function() setActiveScreen('#+=') end, colors.lightGray, colors.black)
  101. registerKey('123', 'period', '.', 5, 3, nil, nil, function() os.queueEvent('key', keys.period) os.queueEvent('char', '.') end, colors.lightGray, colors.black)
  102. registerKey('123', 'comma', ',', 7, 3, nil, nil, function() os.queueEvent('key', keys.comma) os.queueEvent('char', ',') end, colors.lightGray, colors.black)
  103. registerKey('123', 'question', '?', 9, 3, nil, nil, function() os.queueEvent('key', keys.slash) os.queueEvent('char', '?') end, colors.lightGray, colors.black)
  104. registerKey('123', 'exclamation', '!', 11, 3, nil, nil, function() os.queueEvent('key', keys.one) os.queueEvent('char', '!') end, colors.lightGray, colors.black)
  105. registerKey('123', 'singleQuote', '\'', 13, 3, nil, nil, function() os.queueEvent('key', 0) os.queueEvent('char', '\'') end, colors.lightGray, colors.black)
  106. registerKey('123', 'quote', '\"', 15, 3, nil, nil, function() os.queueEvent('key', 0) os.queueEvent('char', '\"') end, colors.lightGray, colors.black)
  107. registerKey('123', 'BK', '<-', 18, 3, nil, nil, function() os.queueEvent('key', keys.backspace) end, colors.lightGray, colors.black)
  108. registerKey('123', 'SP_ABC', 'ABC', 1, 4, nil, nil, function() setActiveScreen('ABC') end, colors.lightGray, colors.black)
  109. registerKey('123', 'SB', '  space  ', 5, 4, nil, nil, function() os.queueEvent('key', keys.space) os.queueEvent('char', ' ') end, colors.lightGray, colors.black)
  110. registerKey('123', 'EN', 'enter', 15, 4, nil, nil, function() os.queueEvent('key', keys.enter) end, colors.lightGray, colors.black)
  111. registerKey('#+=', 'lsbrace', '[', 1, 1, nil, nil, function() os.queueEvent('key', keys.leftBracket) os.queueEvent('char', '[') end, colors.lightGray, colors.black)
  112. registerKey('#+=', 'rsbrace', ']', 3, 1, nil, nil, function() os.queueEvent('key', keys.rightBracket) os.queueEvent('char', ']') end, colors.lightGray, colors.black)
  113. registerKey('#+=', 'lcbrace', '{', 5, 1, nil, nil, function() os.queueEvent('key', keys.leftBracket) os.queueEvent('char', '{') end, colors.lightGray, colors.black)
  114. registerKey('#+=', 'rcbrace', '}', 7, 1, nil, nil, function() os.queueEvent('key', keys.rightBracket) os.queueEvent('char', '}') end, colors.lightGray, colors.black)
  115. registerKey('#+=', 'hash', '#', 9, 1, nil, nil, function() os.queueEvent('key', keys.three) os.queueEvent('char', '#') end, colors.lightGray, colors.black)
  116. registerKey('#+=', 'percent', '%', 11, 1, nil, nil, function() os.queueEvent('key', keys.five) os.queueEvent('char', '%') end, colors.lightGray, colors.black)
  117. registerKey('#+=', 'carrot', '^', 13, 1, nil, nil, function() os.queueEvent('key', keys.six) os.queueEvent('char', '^') end, colors.lightGray, colors.black)
  118. registerKey('#+=', 'asterisk', '*', 15, 1, nil, nil, function() os.queueEvent('key', keys.eight) os.queueEvent('char', '*') end, colors.lightGray, colors.black)
  119. registerKey('#+=', 'plus', '+', 17, 1, nil, nil, function() os.queueEvent('key', keys.equals) os.queueEvent('char', '+') end, colors.lightGray, colors.black)
  120. registerKey('#+=', 'equals', '=', 19, 1, nil, nil, function() os.queueEvent('key', keys.equals) os.queueEvent('char', '=') end, colors.lightGray, colors.black)
  121. registerKey('#+=', 'score', '_', 2, 2, nil, nil, function() os.queueEvent('key', keys.minus) os.queueEvent('char', '_') end, colors.lightGray, colors.black)
  122. registerKey('#+=', 'backslash', '\\', 4, 2, nil, nil, function() os.queueEvent('key', keys.backslash) os.queueEvent('char', '\\') end, colors.lightGray, colors.black)
  123. registerKey('#+=', 'bar', '|', 6, 2, nil, nil, function() os.queueEvent('key', keys.backslash) os.queueEvent('char', '|') end, colors.lightGray, colors.black)
  124. registerKey('#+=', 'tild', '~', 8, 2, nil, nil, function() os.queueEvent('key', 0) os.queueEvent('char', '~') end, colors.lightGray, colors.black)
  125. registerKey('#+=', 'lt', '<', 10, 2, nil, nil, function() os.queueEvent('key', keys.comma) os.queueEvent('char', '<') end, colors.lightGray, colors.black)
  126. registerKey('#+=', 'gt', '>', 12, 2, nil, nil, function() os.queueEvent('key', keys.period) os.queueEvent('char', '>') end, colors.lightGray, colors.black)
  127. registerKey('#+=', 'dollars', '$', 14, 2, nil, nil, function() os.queueEvent('key', keys.four) os.queueEvent('char', '$') end, colors.lightGray, colors.black)
  128. registerKey('#+=', 'amp', '&', 16, 2, nil, nil, function() os.queueEvent('key', keys.seven) os.queueEvent('char', '&') end, colors.lightGray, colors.black)
  129. registerKey('#+=', 'at', '@', 18, 2, nil, nil, function() os.queueEvent('key', keys.two) os.queueEvent('char', '@') end, colors.lightGray, colors.black)
  130. registerKey('#+=', 'CYC_123', '12', 1, 3, nil, nil, function() setActiveScreen('123') end, colors.lightGray, colors.black)
  131. registerKey('#+=', 'period', '.', 5, 3, nil, nil, function() os.queueEvent('key', keys.period) os.queueEvent('char', '.') end, colors.lightGray, colors.black)
  132. registerKey('#+=', 'comma', ',', 7, 3, nil, nil, function() os.queueEvent('key', keys.comma) os.queueEvent('char', ',') end, colors.lightGray, colors.black)
  133. registerKey('#+=', 'question', '?', 9, 3, nil, nil, function() os.queueEvent('key', keys.slash) os.queueEvent('char', '?') end, colors.lightGray, colors.black)
  134. registerKey('#+=', 'exclamation', '!', 11, 3, nil, nil, function() os.queueEvent('key', keys.one) os.queueEvent('char', '!') end, colors.lightGray, colors.black)
  135. registerKey('#+=', 'singleQuote', '\'', 13, 3, nil, nil, function() os.queueEvent('key', 0) os.queueEvent('char', '\'') end, colors.lightGray, colors.black)
  136. registerKey('#+=', 'quote', '\"', 15, 3, nil, nil, function() os.queueEvent('key', 0) os.queueEvent('char', '\"') end, colors.lightGray, colors.black)
  137. registerKey('#+=', 'BK', '<-', 18, 3, nil, nil, function() os.queueEvent('key', keys.backspace) end, colors.lightGray, colors.black)
  138. registerKey('#+=', 'SP_ABC', 'ABC', 1, 4, nil, nil, function() setActiveScreen('ABC') end, colors.lightGray, colors.black)
  139. registerKey('#+=', 'SB', '  space  ', 5, 4, nil, nil, function() os.queueEvent('key', keys.space) os.queueEvent('char', ' ') end, colors.lightGray, colors.black)
  140. registerKey('#+=', 'EN', 'enter', 15, 4, nil, nil, function() os.queueEvent('key', keys.enter) end, colors.lightGray, colors.black)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement