SHOW:
|
|
- or go back to the newest paste.
| 1 | ------------------------------------------------------- | |
| 2 | -- Программа КАЛЬКУЛЯТОР -- | |
| 3 | -- для компьютеров из мода OpenComputers -- | |
| 4 | -- проект http://computercraft.ru -- | |
| 5 | -- Version 0.2 -- | |
| 6 | -- 2018, © Asior -- | |
| 7 | ------------------------------------------------------- | |
| 8 | --pastebin run PaVaRGcd | |
| 9 | local event = require("event")
| |
| 10 | local gpu = require('component').gpu
| |
| 11 | local computer = require('computer')
| |
| 12 | local un = require('unicode')
| |
| 13 | local pull_e = require('event').pull
| |
| 14 | gpu.setResolution(25, 19) | |
| 15 | local W, H = gpu.getResolution() | |
| 16 | local b_color, f_color = gpu.getBackground(), gpu.getForeground() | |
| 17 | num_vr, num1, mat = '','','' | |
| 18 | event.shouldInterrupt = function () return false end | |
| 19 | ||
| 20 | ||
| 21 | local tButtons = {
| |
| 22 | {
| |
| 23 | visible = false, | |
| 24 | X = 23, | |
| 25 | Y = 1, | |
| 26 | W = 3, | |
| 27 | H = 1, | |
| 28 | color = 0xff0000, | |
| 29 | textColor = 0xffffff, | |
| 30 | text = 'X', | |
| 31 | action = function() | |
| 32 | gpu.setBackground(b_color) | |
| 33 | gpu.setForeground(f_color) | |
| 34 | gpu.fill(1, 1, W, H, ' ') | |
| 35 | gpu.setResolution(80, 25) | |
| 36 | os.exit() | |
| 37 | end | |
| 38 | }, | |
| 39 | {
| |
| 40 | visible = false, | |
| 41 | X = 2, | |
| 42 | Y = 5, | |
| 43 | W = 5, | |
| 44 | H = 2, | |
| 45 | color = 0x004444, | |
| 46 | textColor = 0xffff00, | |
| 47 | text = 'CE', | |
| 48 | action = function() | |
| 49 | gpu.setForeground(0xffffff) | |
| 50 | gpu.setBackground(0x000000) | |
| 51 | num_vr = '' | |
| 52 | num1 = '' | |
| 53 | gpu.set(2, 2, (' '):rep(23))
| |
| 54 | gpu.set(2, 3, (' '):rep(23))
| |
| 55 | end | |
| 56 | }, | |
| 57 | {
| |
| 58 | visible = false, | |
| 59 | X = 8, | |
| 60 | Y = 5, | |
| 61 | W = 5, | |
| 62 | H = 2, | |
| 63 | color = 0x004400, | |
| 64 | textColor = 0xffff00, | |
| 65 | text = '/', | |
| 66 | action = function() | |
| 67 | gpu.setForeground(0xffffff) | |
| 68 | gpu.setBackground(0x000000) | |
| 69 | num1 = tonumber(num_vr) | |
| 70 | num_vr = '' | |
| 71 | mat = '/' | |
| 72 | if num1 then | |
| 73 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 74 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 75 | end | |
| 76 | end | |
| 77 | }, | |
| 78 | {
| |
| 79 | visible = false, | |
| 80 | X = 14, | |
| 81 | Y = 5, | |
| 82 | W = 5, | |
| 83 | H = 2, | |
| 84 | color = 0x004400, | |
| 85 | textColor = 0xffff00, | |
| 86 | text = '*', | |
| 87 | action = function() | |
| 88 | gpu.setForeground(0xffffff) | |
| 89 | gpu.setBackground(0x000000) | |
| 90 | num1 = tonumber(num_vr) | |
| 91 | num_vr = '' | |
| 92 | mat = '*' | |
| 93 | if num1 then | |
| 94 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 95 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 96 | end | |
| 97 | end | |
| 98 | }, | |
| 99 | {
| |
| 100 | visible = false, | |
| 101 | X = 20, | |
| 102 | Y = 5, | |
| 103 | W = 5, | |
| 104 | H = 2, | |
| 105 | color = 0x004400, | |
| 106 | textColor = 0xffff00, | |
| 107 | text = '-', | |
| 108 | action = function() | |
| 109 | gpu.setForeground(0xffffff) | |
| 110 | gpu.setBackground(0x000000) | |
| 111 | num1 = tonumber(num_vr) | |
| 112 | num_vr = '' | |
| 113 | mat = '-' | |
| 114 | if num1 then | |
| 115 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 116 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 117 | end | |
| 118 | end | |
| 119 | }, | |
| 120 | {
| |
| 121 | visible = false, | |
| 122 | X = 20, | |
| 123 | Y = 8, | |
| 124 | W = 5, | |
| 125 | H = 5, | |
| 126 | color = 0x004400, | |
| 127 | textColor = 0xffff00, | |
| 128 | text = '+', | |
| 129 | action = function() | |
| 130 | gpu.setForeground(0xffffff) | |
| 131 | gpu.setBackground(0x000000) | |
| 132 | num1 = tonumber(num_vr) | |
| 133 | num_vr = '' | |
| 134 | mat = '+' | |
| 135 | if num1 then | |
| 136 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 137 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 138 | end | |
| 139 | end | |
| 140 | }, | |
| 141 | {
| |
| 142 | visible = false, | |
| 143 | X = 20, | |
| 144 | Y = 14, | |
| 145 | W = 5, | |
| 146 | H = 5, | |
| 147 | color = 0x004400, | |
| 148 | textColor = 0xffff00, | |
| 149 | text = '=', | |
| 150 | action = function() | |
| 151 | gpu.setForeground(0xffffff) | |
| 152 | gpu.setBackground(0x000000) | |
| 153 | num1 = arf(num1, tonumber(num_vr), mat) | |
| 154 | mat = '' | |
| 155 | num_vr = '' | |
| 156 | gpu.set(2, 2, (' '):rep(22))
| |
| 157 | gpu.set(2, 3, (' '):rep(23-string.len(num1))..num1)
| |
| 158 | end | |
| 159 | }, | |
| 160 | --------------------------------- | |
| 161 | {
| |
| 162 | visible = false, | |
| 163 | X = 2, | |
| 164 | Y = 8, | |
| 165 | W = 5, | |
| 166 | H = 2, | |
| 167 | color = 0x008800, | |
| 168 | textColor = 0xffff00, | |
| 169 | text = '7', | |
| 170 | action = function() | |
| 171 | gpu.setForeground(0xffffff) | |
| 172 | gpu.setBackground(0x000000) | |
| 173 | num_vr = num_vr..7 | |
| 174 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 175 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 176 | end | |
| 177 | }, | |
| 178 | {
| |
| 179 | visible = false, | |
| 180 | X = 8, | |
| 181 | Y = 8, | |
| 182 | W = 5, | |
| 183 | H = 2, | |
| 184 | color = 0x008800, | |
| 185 | textColor = 0xffff00, | |
| 186 | text = '8', | |
| 187 | action = function() | |
| 188 | gpu.setForeground(0xffffff) | |
| 189 | gpu.setBackground(0x000000) | |
| 190 | num_vr = num_vr..8 | |
| 191 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 192 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 193 | end | |
| 194 | }, | |
| 195 | {
| |
| 196 | visible = false, | |
| 197 | X = 14, | |
| 198 | Y = 8, | |
| 199 | W = 5, | |
| 200 | H = 2, | |
| 201 | color = 0x008800, | |
| 202 | textColor = 0xffff00, | |
| 203 | text = '9', | |
| 204 | action = function() | |
| 205 | gpu.setForeground(0xffffff) | |
| 206 | gpu.setBackground(0x000000) | |
| 207 | num_vr = num_vr..9 | |
| 208 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 209 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 210 | end | |
| 211 | }, | |
| 212 | --------------------------------- | |
| 213 | {
| |
| 214 | visible = false, | |
| 215 | X = 2, | |
| 216 | Y = 11, | |
| 217 | W = 5, | |
| 218 | H = 2, | |
| 219 | color = 0x008800, | |
| 220 | textColor = 0xffff00, | |
| 221 | text = '4', | |
| 222 | action = function() | |
| 223 | gpu.setForeground(0xffffff) | |
| 224 | gpu.setBackground(0x000000) | |
| 225 | num_vr = num_vr..4 | |
| 226 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 227 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 228 | end | |
| 229 | }, | |
| 230 | {
| |
| 231 | visible = false, | |
| 232 | X = 8, | |
| 233 | Y = 11, | |
| 234 | W = 5, | |
| 235 | H = 2, | |
| 236 | color = 0x008800, | |
| 237 | textColor = 0xffff00, | |
| 238 | text = '5', | |
| 239 | action = function() | |
| 240 | gpu.setForeground(0xffffff) | |
| 241 | gpu.setBackground(0x000000) | |
| 242 | num_vr = num_vr..5 | |
| 243 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 244 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 245 | end | |
| 246 | }, | |
| 247 | {
| |
| 248 | visible = false, | |
| 249 | X = 14, | |
| 250 | Y = 11, | |
| 251 | W = 5, | |
| 252 | H = 2, | |
| 253 | color = 0x008800, | |
| 254 | textColor = 0xffff00, | |
| 255 | text = '6', | |
| 256 | action = function() | |
| 257 | gpu.setForeground(0xffffff) | |
| 258 | gpu.setBackground(0x000000) | |
| 259 | num_vr = num_vr..6 | |
| 260 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 261 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 262 | end | |
| 263 | }, | |
| 264 | --------------------------------- | |
| 265 | {
| |
| 266 | visible = false, | |
| 267 | X = 2, | |
| 268 | Y = 14, | |
| 269 | W = 5, | |
| 270 | H = 2, | |
| 271 | color = 0x008800, | |
| 272 | textColor = 0xffff00, | |
| 273 | text = '1', | |
| 274 | action = function() | |
| 275 | gpu.setForeground(0xffffff) | |
| 276 | gpu.setBackground(0x000000) | |
| 277 | num_vr = num_vr..1 | |
| 278 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 279 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 280 | end | |
| 281 | }, | |
| 282 | {
| |
| 283 | visible = false, | |
| 284 | X = 8, | |
| 285 | Y = 14, | |
| 286 | W = 5, | |
| 287 | H = 2, | |
| 288 | color = 0x008800, | |
| 289 | textColor = 0xffff00, | |
| 290 | text = '2', | |
| 291 | action = function() | |
| 292 | gpu.setForeground(0xffffff) | |
| 293 | gpu.setBackground(0x000000) | |
| 294 | num_vr = num_vr..2 | |
| 295 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 296 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 297 | end | |
| 298 | }, | |
| 299 | {
| |
| 300 | visible = false, | |
| 301 | X = 14, | |
| 302 | Y = 14, | |
| 303 | W = 5, | |
| 304 | H = 2, | |
| 305 | color = 0x008800, | |
| 306 | textColor = 0xffff00, | |
| 307 | text = '3', | |
| 308 | action = function() | |
| 309 | gpu.setForeground(0xffffff) | |
| 310 | gpu.setBackground(0x000000) | |
| 311 | num_vr = num_vr..3 | |
| 312 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 313 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 314 | end | |
| 315 | }, | |
| 316 | --------------------------------- | |
| 317 | {
| |
| 318 | visible = false, | |
| 319 | X = 2, | |
| 320 | Y = 17, | |
| 321 | W = 11, | |
| 322 | H = 2, | |
| 323 | color = 0x008800, | |
| 324 | textColor = 0xffff00, | |
| 325 | text = '0', | |
| 326 | action = function() | |
| 327 | gpu.setForeground(0xffffff) | |
| 328 | gpu.setBackground(0x000000) | |
| 329 | num_vr = num_vr..0 | |
| 330 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 331 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 332 | end | |
| 333 | }, | |
| 334 | {
| |
| 335 | visible = false, | |
| 336 | X = 14, | |
| 337 | Y = 17, | |
| 338 | W = 5, | |
| 339 | H = 2, | |
| 340 | color = 0x008800, | |
| 341 | textColor = 0xffff00, | |
| 342 | text = ',', | |
| 343 | action = function() | |
| 344 | gpu.setForeground(0xffffff) | |
| 345 | gpu.setBackground(0x000000) | |
| 346 | num_vr = num_vr..'.' | |
| 347 | gpu.set(2, 2, (' '):rep(20-string.len(num1))..num1..' '..mat..' ')
| |
| 348 | gpu.set(2, 3, (' '):rep(23-string.len(num_vr))..num_vr)
| |
| 349 | end | |
| 350 | }, | |
| 351 | } | |
| 352 | ||
| 353 | function arf(n1,n2,m) | |
| 354 | if m ~= '' and n1 and n2 then | |
| 355 | if m == '/' then | |
| 356 | if n1 == 0 then | |
| 357 | return -0 | |
| 358 | else | |
| 359 | return n1/n2 | |
| 360 | end | |
| 361 | elseif m == '*' then | |
| 362 | return n1*n2 | |
| 363 | elseif m == '-' then | |
| 364 | return n1-n2 | |
| 365 | elseif m == '+' then | |
| 366 | return n1+n2 | |
| 367 | end | |
| 368 | else | |
| 369 | return 0 | |
| 370 | end | |
| 371 | end | |
| 372 | ||
| 373 | local function drawButton(n) -- функция рисования кнопки | |
| 374 | gpu.setBackground(tButtons[n].color) -- задаем цвет кнопки | |
| 375 | gpu.setForeground(tButtons[n].textColor) -- задаем цвет текста | |
| 376 | gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- заливаем область | |
| 377 | gpu.set(tButtons[n].X+(tButtons[n].W/2)-(un.len(tButtons[n].text)/2), tButtons[n].Y+(tButtons[n].H/2), tButtons[n].text) -- пишем текст по центру | |
| 378 | end | |
| 379 | ||
| 380 | local function toggleVisible(n) -- переключение видимости кнопки | |
| 381 | if tButtons[n].visible then -- если кнопка видима | |
| 382 | tButtons[n].visible = false -- отключаем | |
| 383 | gpu.setBackground(b_color) -- берем цвет фона, полученный при старте программы | |
| 384 | gpu.fill(tButtons[n].X, tButtons[n].Y, tButtons[n].W, tButtons[n].H, ' ') -- стираем кнопку | |
| 385 | else -- если кнопка не активна | |
| 386 | tButtons[n].visible = true -- активируем | |
| 387 | drawButton(n) -- запускаем отрисовку | |
| 388 | end | |
| 389 | end | |
| 390 | ||
| 391 | local function blink(n) -- мигание кнопки | |
| 392 | tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем местами цвета фона и текста | |
| 393 | drawButton(n) -- отрисовываем кнопку | |
| 394 | os.sleep(0.09) -- делаем задержку | |
| 395 | tButtons[n].color, tButtons[n].textColor = tButtons[n].textColor, tButtons[n].color -- меняем цвета обратно | |
| 396 | drawButton(n) -- перерисовываем кнопку | |
| 397 | end | |
| 398 | ||
| 399 | gpu.fill(1, 1, W, H, ' ') -- очищаем экран | |
| 400 | ||
| 401 | gpu.set(1, 1, '╔═════════════════════') | |
| 402 | for i=2,18 do | |
| 403 | gpu.set(1, i, '║ ║') | |
| 404 | end | |
| 405 | gpu.set(1, 4, '╠═══════════════════════╣') | |
| 406 | gpu.set(1, 19, '╚═══════════════════════╝') | |
| 407 | ||
| 408 | for i = 1, #tButtons do | |
| 409 | toggleVisible(i) -- активируем каждую кнопку | |
| 410 | end | |
| 411 | ||
| 412 | while true do | |
| 413 | local tEvent = {pull_e('touch')} -- ждем клика
| |
| 414 | for i = 1, #tButtons do -- перебираем все кнопки | |
| 415 | if tButtons[i].visible then -- если кнопка активна | |
| 416 | if tEvent[3] >= tButtons[i].X and tEvent[3] <= tButtons[i].X+tButtons[i].W-1 and tEvent[4] >= tButtons[i].Y and tEvent[4] <= tButtons[i].Y+ tButtons[i].H-1 then -- если клик произведен в пределах кнопки | |
| 417 | blink(i) -- мигнуть кнопкой | |
| 418 | tButtons[i].action() -- выполнить назначенный код | |
| 419 | break | |
| 420 | end | |
| 421 | end | |
| 422 | end | |
| 423 | end |