Skip to content

Instantly share code, notes, and snippets.

@exerro
Created January 9, 2017 20:13
Show Gist options
  • Save exerro/b75e18183707c101f35931e392c60160 to your computer and use it in GitHub Desktop.
Save exerro/b75e18183707c101f35931e392c60160 to your computer and use it in GitHub Desktop.
local app = sheets.Application()
local button1 = app.screen + sheets.Button( 0, 0, 20, 5, "Hello world!" )
local right = app.screen + sheets.Button( 0, 1, 7, 3, "Right" )
local old = { [button1] = {}, [right] = {} }
right:set_ID "right"
old[button1].width = button1.set_width
old[button1].x = button1.set_x
old[right].width = right.set_width
old[right].x = right.set_x
local function parse_val( self, value )
local refs = {}
local props = {}
if type( value ) == "number" then
return function() return value end, refs, props
end
for a, b in value:gmatch "(%w+)%.(%w+)" do
if a == "self" then
refs[#refs + 1] = self
else
refs[#refs + 1] = app:query( a )[1]
end
props[#props + 1] = b
end
local s = value:gsub( "(%w+)%.(%w+)", function( a, b )
if a == "self" then
return "self." .. b
else
return ("app:query '#%s' [1].%s"):format( a, b )
end
end )
return assert( loadstring( "local app, self = ... return function() return " .. s .. " end", "script" ) )( app, self ), refs, props
end
local function setter( prop )
return function( self, value )
self.values:respawn( prop )
local f, refs, props = parse_val( self, value )
for i = 1, #refs do
local object = refs[i]
local property = props[i]
object.values:subscribe( property, self.values.lifetimes[prop], function()
old[self][prop]( self, f() )
self.values:trigger( prop )
end )
end
self["raw_" .. prop] = value
old[self][prop]( self, f() )
self.values:trigger( prop )
end
end
button1.values:add( "width", "integer", 0, setter "width" )
button1.values:add( "x", "integer", 0, setter "x" )
right.values:add( "width", "integer", 0, setter "width" )
right.values:add( "x", "integer", 0, setter "x" )
right:set_x "51 - self.width + 1"
right:set_width "#self.text + 2"
button1:set_x "5"
button1:set_width "right.x - self.x"
local b = false
function button1:on_click()
if not b then
self:animate_x( 20, 0.4 )
b = true
else
app:stop()
end
end
function right:on_click()
self:animate_width( 15, 0.4 )
end
app:run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment