Unlocker001_TFM

Calculator Mice

Feb 24th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. -- Calculator Mice BY Unlocker001
  2. -- Use this variable to change language (currently available languages: AR, EN)
  3. local defaultLanguage = "AR"
  4. local text = {}
  5.  
  6. text.AR = {}
  7. text.AR.help =
  8. [[
  9. : الاوامر
  10. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  11. <b>!help</b> لإظهار لائحة المساعدة
  12. <b>!calc</b> إظهار/إخفاء الالة الحاسبة
  13. <b>!move</b> تسمح لك بتحريك الالة الحاسبة بالضغط على الفأرة
  14.  
  15. <b>!Num</b> ادخال الرقم مباشرة
  16. !مثال: 23423
  17.  
  18. <b>!NumOpNum</b> حساب العمليات التي تريد حسابها
  19. !امثلة: 4+5
  20. !21*3
  21. !21x3
  22. !2-4323
  23.  
  24. <b>!pi</b> (3.1415...) لكتابة الرقم بي
  25.  
  26. ]]
  27. text.AR.welcome = ui.addPopup(0, 0, "<font color='#5eb6d6' face='DejaVu Sans Mono' size='30'><p align='center'>الالة الحاسبة</p></font><br><font color='#ffd711' face='DejaVu Sans Mono' size='18'><p align='center'>مرحباً بكم في سكربت الالة الحاسبة ارجوا ان يعجبكم</p><br><p align='center'>اكتب !calc لإظهار/إخفاء الالة الحاسبة و !help لرؤية قائمة الاوامر</p></font><br><font color='#EB1D51' face='DejaVu Sans Mono' size='18'><p align='center'>السكربت معد من طرف</p><br><p align='center'>Unlocker001</p></font>", nil, 97, 69.5, 622, true)
  28.  
  29. text.AR.move = ui.addPopup(0, 0, "<font color='#5eb6d6' face='DejaVu Sans Mono' size='30'><p align='center'>التحريك</p></font><br><font color='#ffd711' face='DejaVu Sans Mono' size='18'><p align='center'>لتحريك الالة الحاسبة عليك كتابة</p><br><p align='center'>!move</p></font><br><font color='#EB1D51' face='DejaVu Sans Mono' size='18'><p align='center'>والضغط على المكان التي تريد<br> ان تكون به الالة الحاسبة و اكتب<br>!move<br>لايقاف التحريك</p><br><p ", nil, 479, 27, 305, true)
  30.  
  31.  
  32. text.EN = {}
  33. text.EN.help =
  34. [[
  35. Commands:
  36. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
  37. <b>!help</b> Shows this help
  38. <b>!calc</b> Shows or hides the calculator
  39. <b>!move</b> Allows you to move the calculator by clicking
  40.  
  41. <b>!Num</b> Directly enters the given number
  42. Example: !23423
  43.  
  44. <b>!NumOpNum</b> Does the desired operation
  45. Examples: !4+5
  46. !21*3
  47. !21x3
  48. !2-4323
  49.  
  50. <b>!pi</b> Enters the PI constant (3.1415...)
  51.  
  52. ]]
  53. text.EN.welcome = ui.addPopup(0, 0, "<font color='#5eb6d6' face='DejaVu Sans Mono' size='30'><p align='center'>Calculator</p></font><br><font color='#ffd711' face='DejaVu Sans Mono' size='18'><p align='center'>Welcome to Calculator Script, I hope you like </p><br><p align='center'>Type !calc to show or hide the calculator and !help to see a list of commands.</p></font><br><font color='#EB1D51' face='DejaVu Sans Mono' size='18'><p align='center'>by</p><br><p align='center'>Unlocker001</p></font>", nil, 137, 60, 554, true)
  54. text.EN.move = ui.addPopup(0, 0, "<font color='#5eb6d6' face='DejaVu Sans Mono' size='30'><p align='center'>Moving</p></font><br><font color='#ffd711' face='DejaVu Sans Mono' size='18'><p align='center'>To move the calculator type :</p><br><p align='center'>!move</p></font><br><font color='#EB1D51' face='DejaVu Sans Mono' size='18'><p align='center'>and click anywhere on the screen to move the calculator.<br>Type '!move'<br>again when you're done.</p><br><p ", nil, 486.5, 28, 305, true)
  55.  
  56.  
  57. local mice = {}
  58. local buttonText = {
  59. "7","8","9","/",
  60. "4","5","6","x",
  61. "1","2","3","-",
  62. "0",".","?","+",
  63. "C","&lt;","="
  64. }
  65. local buttonCallback = {
  66. "number7", "number8", "number9", "divide",
  67. "number4", "number5", "number6", "times",
  68. "number1", "number2", "number3", "minus",
  69. "number0", "point", "random", "plus",
  70. "clear", "delete", "equal"
  71. }
  72. local operator = {
  73. divide = "/",
  74. times = "x",
  75. minus = "-",
  76. plus = "+"
  77. }
  78.  
  79. function main()
  80. for name in pairs(tfm.get.room.playerList) do
  81. eventNewPlayer (name)
  82. end
  83.  
  84. system.disableChatCommandDisplay ("help")
  85. system.disableChatCommandDisplay ("calc")
  86. system.disableChatCommandDisplay ("move")
  87. system.disableChatCommandDisplay ("pi")
  88. end
  89.  
  90. function eventNewPlayer (name)
  91. mice[name] = {}
  92. mice[name].calcShown = false
  93. mice[name].data = {0,nil,nil}
  94. mice[name].moveCalc = false
  95. mice[name].calc={
  96. x = 20,
  97. y = 40
  98. }
  99.  
  100. showMessage (getTrans().welcome, name)
  101. system.bindMouse (name, true)
  102. end
  103.  
  104. function showCalc (name)
  105. mice[name].calcShown = true
  106.  
  107. local x0 = mice[name].calc.x
  108. local y0 = mice[name].calc.y
  109.  
  110. ui.addTextArea (10, "", name, x0, y0, 184, 300, nil, nil, 0.6)
  111. ui.addTextArea (11, "", name, x0 + 20, y0 + 20, 144, 40, nil, nil, 0.2)
  112. refreshCalc (name)
  113.  
  114. for j=0,4 do
  115. for i=1,4 do
  116. local n = 4*(j) + i
  117. local text = string.format ("<a href='event:%s'><p align='center'><font size='14'>%s</font></p></a>", buttonCallback[n], buttonText[n])
  118. if j==4 and i==3 then
  119. ui.addTextArea (20 + n, text, name, x0+20 + (i-1)*40, y0+90 + j*40, 64, 24, nil, nil, 0.4)
  120. break
  121. else
  122. ui.addTextArea (20 + n, text, name, x0+20 + (i-1)*40, y0+90 + j*40, 24, 24, nil, nil, 0.4)
  123. end
  124. end
  125. end
  126. end
  127.  
  128. function refreshCalc (name)
  129. local data = mice[name].data
  130. local op = data[2] and operator[data[2]] or ""
  131. local text = string.format ("<font size='14'><TD>%s<br>%s %s</TD></font>", data[1] or "MATH ERROR", op, data[3] or "")
  132. ui.updateTextArea (11, text, name)
  133. end
  134.  
  135. function hideCalc (name)
  136. mice[name].calcShown = false
  137. for i=10,11 do
  138. ui.removeTextArea (i, name)
  139. end
  140. for i=21,39 do
  141. ui.removeTextArea (i, name)
  142. end
  143. end
  144.  
  145. function eventTextAreaCallback (id, name, callback)
  146. local data = mice[name].data
  147.  
  148. if callback:sub(1,6) == "number" and tonumber(callback:sub(7,-1)) then
  149. local digit = tonumber(callback:sub(7,-1))
  150. if not data[2] and not data[3] then
  151. mice[name].data[1] = tonumber((data[1] or 0) .. digit)
  152. else
  153. mice[name].data[3] = tonumber((data[3] or 0) .. digit)
  154. end
  155.  
  156. elseif callback == "divide" or callback == "times" or callback == "minus" or callback == "plus" then
  157. if not data[3] then
  158. mice[name].data[2] = callback
  159. else
  160. calculate (name)
  161. mice[name].data[2] = callback
  162. end
  163.  
  164. elseif callback == "equal" then
  165. calculate (name)
  166.  
  167. elseif callback == "clear" then
  168. mice[name].data[1] = 0
  169. mice[name].data[2] = nil
  170. mice[name].data[3] = nil
  171.  
  172. elseif callback == "delete" and data[1] then
  173. if data[3] then
  174. mice[name].data[3] = tonumber (string.sub(data[3], 1, -2)) or 0
  175. elseif not data[2] then
  176. mice[name].data[1] = tonumber (string.sub(data[1], 1, -2)) or 0
  177. end
  178.  
  179. end
  180.  
  181. refreshCalc(name)
  182. end
  183.  
  184. function calculate (name)
  185. data = mice[name].data
  186.  
  187. local result
  188. if not data[2] or not data[3] then
  189. result = data[1]
  190. else
  191. if data[2] == "divide" then
  192. if data[3] == 0 then
  193. result = nil
  194. else
  195. result = data[1] / data[3]
  196. end
  197. elseif data[2] == "times" then
  198. result = data[1] * data[3]
  199. elseif data[2] == "minus" then
  200. result = data[1] - data[3]
  201. elseif data[2] == "plus" then
  202. result = data[1] + data[3]
  203. end
  204. end
  205.  
  206. mice[name].data[1] = result
  207. mice[name].data[2] = nil
  208. mice[name].data[3] = nil
  209. end
  210.  
  211. function eventChatCommand (name, mes)
  212. if mes == "calc" then
  213. toggleCalc(name)
  214.  
  215. -- debug
  216. --elseif mes == "data" then
  217. -- print ((mice[name].data[1] or "Nil") .. " " .. (mice[name].data[2] or "Nil") .. " " .. (mice[name].data[3] or "Nil"))
  218.  
  219. elseif mes == "move" then
  220. if mice[name].moveCalc then
  221. mice[name].moveCalc = false
  222. else
  223. mice[name].moveCalc = true
  224. showMessage (getTrans().move, name)
  225. end
  226.  
  227. elseif mes == "help" then
  228. showMessage (getTrans().help, name, 400)
  229.  
  230. elseif mice[name].calcShown and tonumber(mes) then
  231. enterNumber (name, tonumber(mes))
  232. refreshCalc(name)
  233.  
  234. elseif mice[name].calcShown and mes == "pi" then
  235. enterNumber (name, math.pi)
  236. refreshCalc(name)
  237.  
  238. elseif mice[name].calcShown then
  239. local n1, op, n2
  240. n1, op, n2 = mes:match ("(%d+)([%-%+/%*x])(%d+)")
  241. if n1 and op and n2 then
  242. mice[name].data[1] = n1
  243. mice[name].data[2] = operatorName(op)
  244. mice[name].data[3] = n2
  245. calculate(name)
  246. refreshCalc(name)
  247. end
  248. end
  249. end
  250.  
  251. function operatorName (operator)
  252. if operator == "/" then
  253. return "divide"
  254. elseif operator == "*" or operator == "x" then
  255. return "times"
  256. elseif operator == "-" then
  257. return "minus"
  258. elseif operator == "+" then
  259. return "plus"
  260. end
  261. end
  262.  
  263. function enterNumber (name, number)
  264. local data = mice[name].data
  265. if not data[2] and not data[3] then
  266. mice[name].data[1] = number
  267. else
  268. mice[name].data[3] = number
  269. end
  270. end
  271.  
  272. function toggleCalc(name)
  273. if mice[name].calcShown then
  274. hideCalc(name)
  275. else
  276. showCalc(name)
  277. end
  278. end
  279.  
  280. function eventMouse (name, mx, my)
  281. if mice[name].moveCalc then
  282. mice[name].calc.x = mx
  283. mice[name].calc.y = my
  284. showCalc (name)
  285. end
  286. end
  287.  
  288. function showMessage (message, name, width)
  289. if not width then width = 200 end
  290. ui.addPopup (0, 0, message, name, 780 - width, 40, width)
  291. end
  292.  
  293. function getTrans ()
  294. local com = tfm.get.room.community
  295. if text[com] then
  296. return text[com]
  297. else
  298. return text[defaultLanguage]
  299. end
  300. end
  301.  
  302. main()
Add Comment
Please, Sign In to add comment