Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- This is incomplete program.
- -- #################################
- -- h2touchpanel API
- -- (hevohevo's TouchPanel API)
- -- version 0.2
- -- http://hevohevo.hatenablog.com/
- -- #######################
- -- Notice
- -- This API rewrite peripheral.wrap()
- -- #######################
- -- how to use
- -- 1. install this program
- -- > pastebin get XXXXXX h2touchpanel
- -- 2. insert into your code
- -- > os.loadAPI("h2touchpanel")
- -- 3. You can use some functions. As follows:
- --[[
- os.loadAPI("h2touchpanel")
- -- define function for button-command
- -- button_config: "name"(string) and "cmd"(function) are required.
- local bCfg = {}
- bCfg[1]={name="Up", cmd=turtle.up}
- bCfg[2]={name="Fwd", cmd=turtle.forward}
- bCfg[3]={name="Down", cmd=turtle.down}
- bCfg[4]={name="<=", cmd=turtle.turnLeft}
- bCfg[5]={name="Back", cmd=turtle.back}
- bCfg[6]={name="=>", cmd=turtle.turnRight}
- bCfg[7]={name="backP", cmd=panel.backPage}
- bCfg[8]={name="ref", cmd=turtle.refuel}
- bCfg[9]={name="nextP", cmd=panel.nextPage}
- bCfg[10]={name="place", cmd=turtle.place}
- bCfg[11]={name="dig", cmd=turtle.dig}
- bCfg[12]={name="attack", cmd=turtle.attack}
- bCfg[13]={name="place", cmd=turtle.place}
- bCfg[14]={name="dig", cmd=turtle.dig}
- bCfg[15]={name="attack", cmd=turtle.attack}
- bCfg[16]={name="backP", cmd=panel.backPage}
- bCfg[17]={name="ref", cmd=turtle.refuel}
- bCfg[18]={name="nextP", cmd=panel.nextP}
- -- panelOpt = {topPos=1, leftPos=1, rightPos=nil, bottomPos=nil, scale=0.5,
- -- fgColor=colors.white, bgPattern={colors.blue, colors.lightBlue}}
- local panelOpt={}
- -- makePanel(btn_cfg, col, row, monitor, opt) returns Panel-Object
- local p1 = h2touchpanel.makePanel(bCfg, 3, 2, mon, panelOpt)
- p1:draw() -- same as p1.draw(p1): draw touch-panel-buttons into mon
- while true do
- -- same as p1.pullButtonPushEvent(p1, monitor_side):
- -- return two params, event "button_push" and a pushed button.
- local event, b1 = p1:pullButtonPushEvent()
- p1:drawPushedButtonEffect(b1) -- draw pushed button effect on monitor
- b1:run() -- run function
- end
- --]]
- -- #####################################
- -- Object Oriented Programming
- -- Color name for pp (pretty print) function
- local cName = {
- 1 = 'white',
- 2 = 'orange',
- 4 = 'magenta',
- 8 = 'lightBlue',
- 16 = 'yellow',
- 32 = 'lime',
- 64 = 'pink',
- 128 = 'gray',
- 256 = 'lightGray',
- 512 = 'cyan',
- 1024 = 'purple',
- 2048 = 'blue',
- 4096 = 'brown',
- 8192 = 'green',
- 16384 = 'red',
- 32768 = 'black'
- }
- -- #######################################
- -- overwrite peripheral.wrap in Peripheral API
- -- add new function: peripheral_func_table.getSide()
- if not _G.peripheral.wrap_org then
- _G.peripheral.wrap_org = peripheral.wrap
- _G.peripheral.wrap = function(side)
- local tbl = peripheral.wrap_org(side)
- if tbl then
- tbl.getSide = function() return side end
- end
- return tbl
- end
- end
- -- #######################################
- -- add new functions and overwrite in Turtle API
- -- turtle.select_org(n)
- -- turtle.select(n)
- -- turtle.getSelectedSlot()
- -- turtle.selectNext()
- -- turtle.selectPrev()
- if not _G.turtle.select_org then
- turtle.select_org = turtle.select_org or turtle.select
- turtle.select = function(slot)
- turtle.select_org(slot)
- local func = function (x)
- slot = slot + x
- return (((slot-1) % 16)+1)
- end
- turtle.selectNext = function() return turtle.select_org(func(1)) end
- turtle.selectPrev = function() return turtle.select_org(func(-1)) end
- turtle.getSelectedSlot = function() return func(0) end
- return true
- end
- turtle.select(1)
- end
- -- #######################################
- -- Coordinate Object
- local Coordinate = {}
- Coordinate.new = function(_x,_y)
- local obj = {}
- obj.x = _x
- obj.y = _y
- return setmetatable(obj,{__index = Coordinate})
- end
- Coordinate.pp = function(self) print("(",self.x,',',self.y,")") end
- Coordinate.width = function(startP, goalP) return math.abs(goalP.x - startP.x)+1 end
- Coordinate.height = function(startP, goalP) return math.abs(goalP.y - startP.y)+1 end
- Coordinate.midpoint = function(startP, goalP, floor_flag)
- local x = startP.x + startP:width(goalP)/2
- local y = startP.y + startP:height(goalP)/2
- if floor_flag then
- return Coordinate.new(math.floor(x),math.floor(y))
- else
- return Coordinate.new(x,y)
- end
- end
- -- Rectangle Object
- local Rectangle= {}
- Rectangle.new = function(_startX, _startY, _goalX, _goalY, _color)
- local obj = {}
- obj.start = Coordinate.new(_startX, _startY)
- obj.goal = Coordinate.new(_goalX, _goalY)
- obj.bgcolor = _color
- return setmetatable(obj,{__index = Rectangle})
- end
- Rectangle.draw = function(self,mon)
- mon.setBackgroundColor(self.bgcolor)
- for y=self.start.y, self.goal.y do
- mon.setCursorPos(self.start.x, y)
- for x=self.start.x, self.goal.x do
- mon.write(" ")
- end
- end
- end
- -- Button Object
- local Button = {}
- Button.new = function(_name, _cmd, _startX, _startY, _goalX, _goalY, _fgColor, _bgColor)
- local obj = Rectangle.new(_startX, _startY, _goalX, _goalY, _bgColor)
- obj.name = _name
- obj.fgColor = _fgColor
- obj.cmd = _cmd
- return setmetatable(obj,{__index = Button})
- end
- Button.pp = function(self)
- local str_cmd = self.cmd
- if type(self.cmd) == "function" then
- str_cmd = "a function"
- elseif not self.cmd then
- str_cmd = "do-nothing"
- end
- print(string.format("%s: (%d,%d)-(%d,%d), %d, %s",
- self.name, self.start.x, self.start.y, self.goal.x, self.goal.y, cName[self.bgcolor], str_cmd))
- end
- Button.draw = function(self,mon)
- Rectangle.draw(self, mon)-- draw a rectangle
- -- draw button label into the rectangle
- local bWidth = self.start:width(self.goal)
- local bHeight = self.start:height(self.goal)
- local name = self.name
- local label = string.sub(self.name, 1, bWidth)
- local length = string.len(label)
- local midpoint = self.start:midpoint(self.goal)
- mon.setCursorPos(math.floor(midpoint.x - length/2), math.floor(midpoint.y))
- mon.write(label)
- end
- Button.isWithin = function(self, point)
- local _within = function(n,start,goal)
- return n>=start and n<=goal
- end
- return _within(point.x, self.start.x, self.goal.x) and _within(point.y, self.start.y, self.goal.y)
- end
- Button.evalCmd = function(self)
- if not self.cmd or self.cmd=="" then
- return false
- elseif type(self.cmd) == "string" then
- return assert(loadstring(self.cmd), "invalid cmd: this string is invalid.")()
- elseif type(self.cmd) == "function" then
- return self.cmd()
- else
- return assert(false, "invalid cmd: cmd is function or function-string")
- end
- end
- Button.run = Button.evalCmd
- -- Panel Object
- -- OPTION = {topPos=1, leftPos=1, rightPos=(monitor width), bottomPos=(monitor hight),
- -- scale=0.5, fgColor=colors.white, bgPattern={colors.blue, colors.lightBlue}}
- local Panel = {}
- Panel._makeButtons = function(_bCfg, _col, _row, _mon, _opt)
- local opt = _opt or {}
- local point0 = Coordinate.new((opt.leftPos or 1), (opt.topPos or 1)) -- left-top point
- local point1 = Coordinate.new(_mon.getSize()) -- right-bottom point
- point1.x = opt.rightPos or point1.x
- point1.y = opt.bottomPos or point1.y
- local panelW, panelH = point0:width(point1), point0:height(point1)
- local btnW = math.floor(panelW/_col)
- local btnH = math.floor(panelH/_row)
- local margin_w, margin_h = (panelW % _col), (panelH % _row)
- local margin_left,margin_top = math.floor(margin_w/2), math.floor(margin_h/2)
- local margin_right, margin_bottom = margin_w - margin_left, margin_h - margin_top
- local fgColor = opt.fgColor or colors.white
- local colorRing = function(i) -- make background color pattern
- local array = {colors.blue, colors.lightBlue, colors.cyan}
- if _col%2 == 1 then array = {colors.blue, colors.lightBlue} end
- array = opt.bgPattern or array
- return array[((i-1)%(#array))+1]
- end
- local btns = {}
- local i = 1 -- table index
- local minY = point0.y
- local maxY = minY+btnH-1 + margin_top
- for row=1,_row do
- local minX = point0.x
- local maxX = minX+btnW-1+ margin_left
- for col=1, _col do
- if _bCfg[i] then -- make a button by the button_config
- btns[i] = Button.new(_bCfg[i]["name"], _bCfg[i]["cmd"], minX, minY, maxX, maxY, fgColor, colorRing(i))
- else -- make a blank button
- btns[i] = Button.new("--", nil, minX, minY, maxX, maxY, fgColor, colorRing(i))
- end
- --calculate next
- i = i+1
- minX = maxX +1
- maxX = minX + btnW-1
- if col==_col-1 then maxX = maxX + margin_right end
- end
- minY = maxY +1
- maxY = minY + btnH-1
- if row==_row-1 then maxY = maxY + margin_bottom end
- end
- return btns
- end
- Panel.new = function(_bCfg, _col, _row, _mon, _opt)
- local opt = _opt or {}
- if _mon.setTextScale then _mon.setTextScale(opt.scale or 0.5) end
- local obj= {}
- obj.col=_col
- obj.row=_row
- obj.total = _col*_row
- obj.mon = _mon
- obj.btns= Panel._makeButtons(_bCfg, _col, _row, _mon, opt)
- return setmetatable(obj,{__index = Panel})
- end
- Panel.pp = function(self)
- print("Panel: ",self.col,'x',self.row)
- print(" Btns: ",#self.btns)
- for i,b in ipairs(self.btns) do
- b:pp()
- end
- end
- Panel.draw = function(self)
- self.mon.setBackgroundColor(colors.gray)
- self.mon.clear()
- for i,b in ipairs(self.btns) do
- b:draw(self.mon)
- end
- end
- Panel.drawPushedButtonEffect = function(self, btn, _sec)
- self.mon.setBackgroundColor(colors.gray)
- self.mon.clear()
- btn:draw(self.mon)
- sleep(_sec or 0.5)
- self:draw()
- end
- Panel.whichButton = function(self, x, y)
- local pushedBtn = false
- for i,b in ipairs(self.btns) do
- if b:isWithin(Coordinate.new(x,y)) then
- pushedBtn = b
- break
- end
- end
- return pushedBtn
- end
- Panel._pullEvent= function(self)
- local params = {os.pullEvent()}
- local pushedBtn = false
- -- params, 1:'monitor_touch', 2:side, 3:x, 4:y, (advanced_monitor)
- -- params, 1:'mouse_click', 2:mouse_btn, 3:x, 4:y, (terminal)
- if (self.mon.setTextScale and params[1] == "monitor_touch" and self.mon.getSide() == params[2])
- or (self.mon == _G.term and params[1] == "mouse_click") then
- pushedBtn = whichButton(self.btns, params[3], params[4])
- if pushedBtn then
- params[1] = "button_push"
- params[2] = pushedBtn
- end
- else
- -- do nothing
- end
- return unpack(params)
- end
- Panel.pullEvent = function(self, filter)
- local params = false
- repeat
- params = self._pullEvent()
- until filter == params[1]
- return unpack(params)
- end
- -- PanelGroup Object
- local PanelGroup = {}
- PanelGroup._makePanels = function(self, _bCfg, _col, _row, _mon, _opt)
- local results = {}
- local num = _col*_row
- local page = math.ceil(#_bCfg/num)
- local makeArray = function(_array, minIndex, maxIndex)
- local tmp = {}
- for i=minIndex, maxIndex do
- table.insert(tmp, _array[i])
- end
- return tmp
- end
- for p=1, page do
- table.insert(results, Panel.new(makeArray(_bCfg, 1+num*(page-1), num*page), _co, _row, _mon, _opt))
- end
- return results
- end
- PanelGroup.new = function(self, _bCfg, _col, _row, _mon, _opt)
- local obj = {}
- local panelId = 1
- obj.panels = self._makePanels()
- obj.selectedPanel = function(self)
- return self.panels[((panelId-1)%(#self.panels))+1]
- end
- obj.selectNext = function(self)
- obj.selectedPanelId = obj.selectedPanelId + 1
- return self.panels[((panelId-1)%(#self.panels))+1]
- end
- obj.selectPrev = function(self)
- self.selectedPanelId = self.selectedPanelId - 1
- return self.panels[((panelId-1)%(#self.panels))+1]
- end
- end
- PanelGroup.draw = function(self)
- self:selectedPanel():draw()
- end
- PanelGroup.pullEvent = function(self, filter)
- self:selectedPanel():pullEvent(self, filter)
- end
- -- ########################
- -- API Functions
- function makePanel(bCfg, col, row, mon, opt)
- return PanelGroup:new(bCfg, col, row, mon, opt)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement