screen saver(s)

This commit is contained in:
kepler155c@gmail.com
2019-11-02 12:25:00 -06:00
parent e1d05da20f
commit 4ce86a9e5a
10 changed files with 148 additions and 5 deletions

6
screenSaver/.package Normal file
View File

@@ -0,0 +1,6 @@
{
title = 'Screen Savers',
repository = 'kepler155c/opus-apps/{{OPUS_BRANCH}}/screenSaver',
description = [[ Various screen savers ]],
license = 'MIT',
}

View File

@@ -0,0 +1,72 @@
local Config = require('opus.config')
local Util = require('opus.util')
local fs = _G.fs
local kernel = _G.kernel
local multishell = _ENV.multishell
local window = _G.window
if not multishell then
return
end
local config = Config.load('saver', {
enabled = true,
timeout = 60,
random = true,
specific = nil,
})
local BASE = '/packages/screenSaver'
local SAVERS = fs.combine(BASE, 'savers')
local timer = config.enabled and os.startTimer(config.timeout)
local saverUid
local function showScreenSaver()
timer = nil
local files = fs.list(SAVERS)
local saver = config.specific or fs.combine(SAVERS, files[math.random(1, #files)])
local w, h = kernel.terminal.getSize()
local win = window.create(kernel.terminal, 1, 1, w, h, true)
saverUid = multishell.openTab({
path = saver,
focused = true,
title = 'Saver',
window = win,
})
end
kernel.hook({ 'mouse_up', 'mouse_drag', 'key_up' }, function()
if config.enabled then
if timer then
os.cancelTimer(timer)
timer = os.startTimer(config.timeout)
elseif saverUid then
multishell.terminate(saverUid)
saverUid = nil
timer = os.startTimer(config.timeout)
end
end
end)
kernel.hook('timer', function(_, eventData)
if timer and eventData[1] == timer then
showScreenSaver()
end
end)
kernel.hook('config_update', function(_, eventData)
if eventData[1] == 'saver' then
Util.merge(config, eventData[2])
if timer then
os.cancelTimer(timer)
timer = nil
end
if config.enabled then
timer = os.startTimer(config.timeout)
end
end
end)

8
screenSaver/etc/fstab Normal file
View File

@@ -0,0 +1,8 @@
sys/apps/system/saver.lua linkfs packages/screenSaver/system/saver.lua
packages/screenSaver/savers/timespace.lua urlfs https://raw.githubusercontent.com/Allen2277/Computercraft/master/Time%20Space%20Screensaver
packages/screenSaver/savers/visualizer.lua urlfs https://raw.githubusercontent.com/Allen2277/Computercraft/master/ScreenSaver
packages/screenSaver/savers/random.lua urlfs https://pastebin.com/raw/XXW0r5zt
packages/screenSaver/savers/melting.lua urlfs http://pastebin.com/raw/raUv6Pap
packages/screenSaver/savers/bubbles.lua urlfs https://pastebin.com/raw/3CeFxk9X
packages/screenSaver/savers/fire.lua urlfs https://pastebin.com/raw/4CY4AYj3
packages/screenSaver/savers/rain.lua urlfs https://pastebin.com/raw/P86Hm99N

128
screenSaver/savers/life.lua Normal file
View File

@@ -0,0 +1,128 @@
--(c) 2013 Felix Maxwell
--License: CC BY-SA 3.0
--http://www.computercraft.info/forums2/index.php?/topic/12239-100-screensavers-game-of-life-and-matrix/
-- modified for use in Opus
local fps = 4 --Determines how long the program will wait between each tick
local char = "\127" --Live cells will look like this
local colors = _G.colors
local term = _G.term
local function randomColor()
return 2 ^ math.random(0, 14)
end
local function printCharAt( monitor, x, y, ch )
monitor.setCursorPos( x, y )
monitor.write( ch )
end
local function getNumNeighborhood( grid, x, y )
local neighbors = 0
if x > 1 then
if y > 1 then
if grid[x-1][y-1] == char then neighbors = neighbors + 1 end
end
if grid[x-1][y] == char then neighbors = neighbors + 1 end
if y < #grid[x] then
if grid[x-1][y+1] == char then neighbors = neighbors + 1 end
end
end
if y > 1 then
if grid[x][y-1] == char then neighbors = neighbors + 1 end
end
if y < #grid[x] then
if grid[x][y+1] == char then neighbors = neighbors + 1 end
end
if x < #grid then
if y > 1 then
if grid[x+1][y-1] == char then neighbors = neighbors + 1 end
end
if grid[x+1][y] == char then neighbors = neighbors + 1 end
if y < #grid then
if grid[x+1][y+1] == char then neighbors = neighbors + 1 end
end
end
return neighbors
end
local function lifeOrDeath( cur, neighbors )
if neighbors < 2 then
return " "
elseif neighbors > 3 then
return " "
elseif neighbors == 3 then
return char
else
return cur
end
end
local function tick( monitor, grid )
local retGrid = {}
for x=1,#grid do
retGrid[x] = {}
for y=1,#grid[x] do
local num = getNumNeighborhood( grid, x, y )
retGrid[x][y] = lifeOrDeath( grid[x][y], num )
if retGrid[x][y] ~= grid[x][y] then
printCharAt( monitor, x, y, retGrid[x][y] )
end
end
end
return retGrid
end
local function setup( w, h )
local grid = {}
for i=1,w do
grid[i] = {}
for o=1,h do
if math.random(1, 5) == 1 then
grid[i][o] = char
else
grid[i][o] = " "
end
end
end
return grid
end
local function run()
local monitor = term.current()
if monitor.isColor() then
monitor.setTextColor(colors.lime)
monitor.setBackgroundColor(colors.black)
end
local w, h = monitor.getSize()
local grid
local delay = 1/fps
local timerId = os.startTimer(delay)
local reset = 0
while true do
local e, id = os.pullEvent()
if e == 'key' or e == 'char' or e == 'mouse_click' then
break
end
if e == 'timer' and id == timerId then
if reset == 0 then
reset = 300
monitor.setTextColor(randomColor())
grid = setup(w, h)
monitor.clear()
end
reset = reset - 1
grid = tick( monitor, grid )
timerId = os.startTimer(delay)
end
end
end
run()
term.setCursorPos(1, 1)
term.clear()

View File

@@ -0,0 +1,54 @@
local Config = require('opus.config')
local UI = require('opus.ui')
local config = Config.load('saver', {
enabled = true,
timeout = 60,
})
local tab = UI.Tab {
tabTitle = 'Screen Saver',
description = 'Screen saver',
label1 = UI.Text {
x = 2, y = 3,
value = 'Enabled',
},
checkbox = UI.Checkbox {
x = 20, y = 3,
value = config.enabled
},
label2 = UI.Text {
x = 2, y = 4,
value = 'Timeout',
},
timeout = UI.TextEntry {
x = 20, y = 4, width = 6,
limit = 4,
transform = 'number',
value = config.timeout,
accelerators = {
enter = 'update',
},
},
button = UI.Button {
x = 20, y = 6,
text = 'Update',
event = 'update',
},
}
function tab:eventHandler(event)
if event.type =='checkbox_change' then
config.enabled = not not event.checked
elseif event.type == 'update' then
config.timeout = self.timeout.value
Config.update('saver', config)
self:emit({ type = 'success_message', message = 'Settings updated' })
os.queueEvent('config_update', 'saver', config)
end
return UI.Tab.eventHandler(self, event)
end
return tab