summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcheapie <no-email-for-you@example.com>2017-04-19 21:37:23 -0500
committercheapie <no-email-for-you@example.com>2017-04-19 21:37:23 -0500
commit2392ba792caf81970e0e2ec77b9dead46886ed32 (patch)
tree9261c303e5c2f62aee94d9955aabbd7eabfcb4a8
downloadTSLib-2392ba792caf81970e0e2ec77b9dead46886ed32.tar
TSLib-2392ba792caf81970e0e2ec77b9dead46886ed32.tar.gz
TSLib-2392ba792caf81970e0e2ec77b9dead46886ed32.tar.bz2
TSLib-2392ba792caf81970e0e2ec77b9dead46886ed32.tar.xz
TSLib-2392ba792caf81970e0e2ec77b9dead46886ed32.zip
Import existing content
-rw-r--r--README80
-rw-r--r--tslib.lua207
2 files changed, 287 insertions, 0 deletions
diff --git a/README b/README
new file mode 100644
index 0000000..05f7173
--- /dev/null
+++ b/README
@@ -0,0 +1,80 @@
+TSLib
+=====
+
+This is a library for Luacontrollers to more easily use the touchscreens from digistuff.
+Since LuaCs can't dofile() or anything, this library is loaded by pasting the contents of tslib.lua
+before the code that will use it.
+
+Available Functions
+===================
+
+tslib:new([channel])
+ Returns a new screen object, optionally setting the digilines channel in the process.
+
+tslib.getColorEscapeSequence(color)
+ Returns the escape sequence to set text to the given color. Identical behavior to minetest.get_color_escape_sequence().
+ Valid colors are any 3, 4, 6 or 8-digit hex color (prefaced with #) or CSS L4 color string.
+
+tslib.colorize(color,message)
+ Returns the message with the text changed to the given color. Behaves similarly to minetest.colorize().
+
+screen:getChannel()
+ Returns the current channel set for the screen object.
+
+screen:setChannel()
+ Changes the channel for the screen object.
+
+screen:clear()
+ Removes all commands from the screen's buffer.
+
+screen:setLock(lockstate)
+ Sets whether the screen can only be interacted with by owners of the area (true) or by anyone (false).
+
+screen:addLabel(X,Y,text[,vertical])
+ Adds a label (or vertical label) at the given X-Y position with the given text.
+
+screen:addImage(X,Y,width,height,texture)
+ Adds an image at the given X-Y position, with the given size and texture. Texture modifications (such as "^[brighten") are supported.
+
+screen:addButton(X,Y,width,height,name,label[,exit])
+ Adds a button at the given X-Y position, with the given size, name and label. An exit button will be created instead of a normal one if "exit" is true.
+ NOTE: "label" is required and will read "nil" if not supplied! Use an empty string ("") to not have one.
+
+screen:addImageButton(X,Y,width,height,name,label,texture[,exit])
+ Adds an image button at the given X-Y position, with the given size, name, label and texture. An exit button will be created instead of a normal one if "exit" is true.
+ NOTE: "label" is required and will read "nil" if not supplied! Use an empty string ("") to not have one.
+
+screen:addField(X,Y,width,height,name,label,default[,password])
+ Adds a single-line text field at the given X-Y position, with the given size, name, label, and default text. Set "password" to true to hide input (password-style field).
+ NOTE: "label" and "default" are required and will read "nil" if not supplied! Use an empty string ("") to not have them.
+
+screen:addTextArea(X,Y,width,height,name,label,default)
+ Adds a multi-line text area at the given X-Y position, with the given size, name, label, and default text.
+ NOTE: "label" and "default" are required and will read "nil" if not supplied! Use an empty string ("") to not have them.
+
+screen:addDropdown(X,Y,width,height,name,choices[,selected])
+ Adds a drop-down menu at the given X-Y position, with the given size, name, and list of choices (which should be a table).
+ An item can optionally be pre-selected by adding a "selected" value. It will default to 1 (the first choice) if not supplied.
+
+screen:draw()
+ Sends the buffer contents to the screen.
+ NOTE: You must call this after adding/changing items on the screen in order for the changes to take effect.
+
+Example Code
+============
+
+local screen = tslib:new()
+screen:setLock(true)
+screen:setChannel("touchscreen")
+screen:addLabel(0,0,tslib.colorize("#00FFFF","TSLib Demo"),false)
+screen:addLabel(3,0,"Hello, world!",true)
+screen:addImage(0,0.5,1,1,"default_dirt.png")
+screen:addButton(0,1.5,1.5,1,"button","Button")
+screen:addButton(0,2.5,2,1,"button2","Exit Button",true)
+screen:addImageButton(0,3.5,1.5,1,"button3","Image Button","default_stone.png")
+screen:addImageButton(0,4.5,2,1,"button4","Image Exit Button","default_tree.png",true)
+screen:addField(0.3,6,2,1,"field","Field","TSLib")
+screen:addField(0.3,7,2,1,"field2","Password Field","",true)
+screen:addTextArea(4,3,2,3,"textarea","Text Area","This is a test of TSLib.")
+screen:addDropdown(4,2,2,1,"dropdown",{"Apple","Orange","Banana"},2)
+screen:draw()
diff --git a/tslib.lua b/tslib.lua
new file mode 100644
index 0000000..14196ad
--- /dev/null
+++ b/tslib.lua
@@ -0,0 +1,207 @@
+--TSLib
+--A product of Advanced Mesecons Devices, a Cheapie Systems company
+--This is free and unencumbered software released into the public domain.
+--See http://unlicense.org/ for more information
+
+--Since LuaCs can't dynamically link libraries,
+--you will have to "statically link" it by pasting this file
+--above the code that will use it.
+
+local tslib = {
+ getColorEscapeSequence = function(color)
+ return(string.char(0x1b).."(c@"..color..")")
+ end,
+ colorize = function(color,message)
+ return(string.char(0x1b).."(c@"..color..")"..message..string.char(0x1b).."(c@#FFFFFF)")
+ end,
+ _mt = {
+ setChannel = function(self,channel)
+ self._channel = channel
+ end,
+ getChannel = function(self)
+ return(self._channel)
+ end,
+ draw = function(self)
+ digiline_send(self._channel,self._commands)
+ end,
+ clear = function(self)
+ self._commands = {{command="clear"}}
+ end,
+ setLock = function(self,lock)
+ if lock then
+ table.insert(self._commands,{command="lock"})
+ else
+ table.insert(self._commands,{command="unlock"})
+ end
+ end,
+ addLabel = function(self,x,y,label,vertical)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ if type(label) ~= "string" then
+ label = tostring(label)
+ end
+ local cmd = {
+ command = "addlabel",
+ X = x,
+ Y = y,
+ label = label,
+ }
+ if vertical then cmd.command = "addvertlabel" end
+ table.insert(self._commands,cmd)
+ end,
+ addImage = function(self,x,y,w,h,tex)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if type(tex) ~= "string" then
+ tex = tostring(tex)
+ end
+ local cmd = {
+ command = "addimage",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ texture_name = tex,
+ }
+ table.insert(self._commands,cmd)
+ end,
+ addButton = function(self,x,y,w,h,name,label,exit)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if type(name) ~= "string" then
+ name = tostring(name)
+ end
+ if type(label) ~= "string" then
+ label = tostring(label)
+ end
+ local cmd = {
+ command = "addbutton",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ name = name,
+ label = label,
+ }
+ if exit then cmd.command = "addbutton_exit" end
+ table.insert(self._commands,cmd)
+ end,
+ addImageButton = function(self,x,y,w,h,name,label,tex,exit)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if type(name) ~= "string" then
+ name = tostring(name)
+ end
+ if type(label) ~= "string" then
+ label = tostring(label)
+ end
+ if type(tex) ~= "string" then
+ tex = tostring(tex)
+ end
+ local cmd = {
+ command = "addimage_button",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ name = name,
+ label = label,
+ image = tex,
+ }
+ if exit then cmd.command = "addimage_button_exit" end
+ table.insert(self._commands,cmd)
+ end,
+ addField = function(self,x,y,w,h,name,label,default,password)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if type(name) ~= "string" then
+ name = tostring(name)
+ end
+ if type(label) ~= "string" then
+ label = tostring(label)
+ end
+ if type(default) ~= "string" then
+ default = tostring(default)
+ end
+ local cmd = {
+ command = "addfield",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ name = name,
+ label = label,
+ default = default,
+ }
+ if password then cmd.command = "addpwdfield" end
+ table.insert(self._commands,cmd)
+ end,
+ addTextArea = function(self,x,y,w,h,name,label,default)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if type(name) ~= "string" then
+ name = tostring(name)
+ end
+ if type(label) ~= "string" then
+ label = tostring(label)
+ end
+ if type(default) ~= "string" then
+ default = tostring(default)
+ end
+ local cmd = {
+ command = "addtextarea",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ name = name,
+ label = label,
+ default = default,
+ }
+ table.insert(self._commands,cmd)
+ end,
+ addDropdown = function(self,x,y,w,h,name,choices,selected)
+ assert((type(x))=="number","Invalid X position")
+ assert((type(y))=="number","Invalid Y position")
+ assert((type(w))=="number","Invalid width")
+ assert((type(h))=="number","Invalid height")
+ if not selected then selected = 1 end
+ assert((type(selected))=="number","Invalid selection index")
+ if type(name) ~= "string" then
+ name = tostring(name)
+ end
+ assert((type(choices) == "table" and #choices >= 1),"Invalid choices list")
+ local cmd = {
+ command = "adddropdown",
+ X = x,
+ Y = y,
+ W = w,
+ H = h,
+ name = name,
+ choices = choices,
+ selected_id = selected,
+ }
+ table.insert(self._commands,cmd)
+ end,
+ },
+ new = function(self,channel)
+ local ret = {}
+ for k,v in pairs(self._mt) do
+ ret[k] = v
+ end
+ ret._channel = channel
+ ret._commands = {{command="clear"}}
+ return ret
+ end,
+}
+