ãªãã¸ã§ã¯ãæåã§ã©ããã£ã¦APIãä½ããï¼h2touchpanel APIã®è©³ç´°è§£èª¬ï¼
ã¯ããã«
ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã§ComputerCraftã®APIãä½æããã¨ãããããã¢ãã¯ãªä¼ç»ã§ãã
ãã¦ãã©ã®ãããã®äººãèå³ãæã¡ãã¤ãã¦ãããã®ã§ããããâ¦â¦ã
æ¬APIã®ä½ææ¹é
ComputerCraftã«ããã¦APIãä½ãã¨ãã®ååã¯ä»¥ä¸ã®éãã§ãã
- ä¸ç¨®é¡ã®APIãä¸ã¤ã®ãã¡ã¤ã«ã«ã¾ã¨ãã¦æ¸ãã
- h2touchpanel APIãªãã°ããã¡ã¤ã«åãh2touchpanelãã¨ãã¦ä¿å
- APIãã¡ã¤ã«ã®ä½ãæ¹
- APIã¨ãã¦ä½¿ãããããªãï¼é ãºããããï¼é¢æ°ã»å¤æ°ã¯ãã¼ã«ã«å®£è¨ãã¦ãã
- APIã¨ãã¦æä¾ããé¢æ°ã»å¤æ°ã¯ãã®ãã¡ã¤ã«ã®ä¸ã§ã°ãã¼ãã«ã§ãªããã°ãªããªã
- APIã®ä½¿ãæ¹
- ãã®ãã¡ã¤ã«ãAPIã¨ãã¦èªã¿è¾¼ãã¨ãã«ã¯ãos.loadAPI(ãã¡ã¤ã«å)é¢æ°ã使ã
- h2touchpanel.myfunc()ãã®ããã«ããã¼ãã«"h2touchpanel"ã«å ¥ã£ã¦ããé¢æ°ã»å¤æ°ãã.ããããè¨æ³ã§ä½¿ç¨ããã
ãããè¸ã¾ãã¦ãh2touchpanelã®ããã°ã©ãã³ã°æ¹éã¯ä»¥ä¸ã®ã¨ããã
ï¼ãããä¸è¬çãªæ¹éãã©ããã¯ãããã¾ãããããã¾ã§ããã®APIã®æ¹éã§ãï¼
- ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°
- ãªãã¸ã§ã¯ãã表ç¾ãã以ä¸ã®ãã¼ãã«ã¯ååã¨ãã¦local宣è¨
- Coordinate, Rectangle, Button, Panel
- å¯ä¸ãPanel.new()ã®ã©ããã¼ã§ããmakePanel()é¢æ°ã®ã¿ã°ãã¼ãã«åãã¦ããã
- ã¤ã¾ããh2touchpanel APIãæä¾ããé¢æ°ã¯ãmakePanel()ã®ã¿ã
- ãã¨ã¯ãä½æããPanelãªãã¸ã§ã¯ãã«å¯¾ããã¡ãã»ã¼ã¸éä¿¡ï¼ã:ãã³ãã³è¨æ³ï¼ã«ãããªãã¸ã§ã¯ãã«ç¨æããã¦ããã¡ã½ãããå©ç¨ãããã¨ã§ãæ§ã ãªæ©è½ã使ããããã«ããã
ãªãã¸ã§ã¯ãæåAPIã®ä½¿ç¨ä¾
local monitor = peripheral.wrap("top") -- 空ãã¿ã³3x3=9åããæ§æãããã¿ãããã¿ã³ããã«ãä½æ local panel_obj1 = h2touchpanel.makePanel({}, 3, 3, monitor) panel_obj1:draw() -- ããã«ãç»é¢è¡¨ç¤º panel_obj1:pp() -- ããã«ã®æ å ±ãprint() local button_obj1 = panel_obj1:pullButtonPushEvent() -- ãã¿ã³ãæ¼ãããã®ãå¾ ã¤ button_obj1:draw(monitor) -- ãã¿ã³ãç»é¢è¡¨ç¤º
ããã«ãããAPIãå©ç¨ããå´ã®ããã°ã©ã ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã§ãããããé¢æ°åããã°ã©ãã³ã°ã§ããããï¼ããã»ã©ï¼éåæãªããã®APIãå©ç¨ã§ãã¾ãã
ä½æãããªãã¸ã§ã¯ãã®èª¬æ
ãªãããã®APIã§ä½æãããªãã¸ã§ã¯ãã«ã¤ãã¦ã¯ä»¥ä¸ã®ã¨ããã
- Coordinateï¼ã座æ¨ãªãã¸ã§ã¯ãã
- 座æ¨(x,y)ãåæ±ããã¾ãã2座æ¨éã®å¹ ãä¸ãè·é¢ãªã©ãè¨ç®ããã¡ã½ãããæä¾
- Rectangleï¼ãåè§å½¢ãªãã¸ã§ã¯ãã
- å·¦ä¸åº§æ¨ãå³ä¸åº§æ¨ãå¡ãã¤ã¶ãè²ãåæ±ããã¾ããåè§å½¢ãæç»ããdraw()ã¡ã½ãããæä¾ã
- Buttonï¼ããã¿ã³ãªãã¸ã§ã¯ãã
- ä¸è¨Rectangleãªãã¸ã§ã¯ããç¶æ¿ãããRectangleï¼ä¸å¤®ã«ã©ãã«è¡¨è¨ãããããã£ã¨ãã¦ãæåè²ï¼fgColorï¼ãå®è¡é¢æ°ï¼cmdï¼ãªã©ãä¿æã
- ãã¿ã³ãæç»ããdraw()ãããã¿ã³ã«è¨å®ããã¦ããé¢æ°ãå®è¡ããevalCmd()ãæä¾ã
- Panelï¼ãããã«ãªãã¸ã§ã¯ãã
- Buttonãªãã¸ã§ã¯ããè¤æ°ä¿æãPanelãªãã¸ã§ã¯ãçææï¼newï¼ã«ãããã¿ã³è¨å®ãã¼ãã«ãããã¿ã³ã®åæ°colãããã¿ã³ã®è¡æ°rowããã¢ãã¿ã¼æ
å ±ããä¸ãããã¨ã«ãããã¢ãã¿ã¼å
ã«é©åã«ãã¿ã³ãä½æã»é
ç½®ããã
- ãã®ãé©åã«ãã®é¨åããã®ããã°ã©ã ã®ä¸çªé¢åãªã¨ããã§ãããæé·é¢æ°ï¼æ³£ï¼ã
- éè¦ã¡ã½ããã¨ãã¦ãpullButtonPushEvent()ãããã
- "button_push"ã¨ããã¤ãã³ãåï¼æååï¼ãè¿å¤ï¼ãæ¼ããããã¿ã³ãªãã¸ã§ã¯ããè¿å¤ï¼ã¨ãã¦è¿ãã
- Buttonãªãã¸ã§ã¯ããè¤æ°ä¿æãPanelãªãã¸ã§ã¯ãçææï¼newï¼ã«ãããã¿ã³è¨å®ãã¼ãã«ãããã¿ã³ã®åæ°colãããã¿ã³ã®è¡æ°rowããã¢ãã¿ã¼æ
å ±ããä¸ãããã¨ã«ãããã¢ãã¿ã¼å
ã«é©åã«ãã¿ã³ãä½æã»é
ç½®ããã
以éã¯ãã½ã¼ã¹ã³ã¼ãã®å
·ä½çãªè§£èª¬ã§ãããã³ã¼ãéãå¤ãããã®ã§ç´°ããªèª¬æã¯ãã¦ãã¾ããã
ããã«ãã®ã³ã¼ãã®æå³ã®è§£èª¬ã§ãã
ããã°ã©ã ã®èª¬æ
ã½ã¼ã¹ã³ã¼ãå ¨ä½
- 0.1d, 2014/02/17, Panel Optionã®æ´åãã¢ãã¿ã¼ã®scaleãæå®å¯è½
- 0.1c, 2014/02/11, bugfix(ããã«ä½ææã«optãçç¥ããã¨ãã®ä¸å ·åä¿®æ£)
- 0.1b, 2014/02/11, APIä»æ§å¤æ´(cmdã«functionãæå®)
- 0.1a, 2014/02/10, å ¬é
座æ¨ï¼Coordinateï¼ãªãã¸ã§ã¯ãã®ã½ã¼ã¹ã³ã¼ã
-- 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
- æåã«åº§æ¨ãªãã¸ã§ã¯ãçæé¢æ°ï¼newï¼ã®å®ç¾©
- çææã®å¼æ°ã¨ãã¦ãx, y
- ãã®ãªãã¸ã§ã¯ãã¯ããããã£ã¨ãã¦ãx座æ¨ã¨y座æ¨ã®å¤ãä¿æãã¦ãã¾ãã
- ãªãã¸ã§ã¯ãçæã¯æ¬¡ã®ããã«ããlocal coordinate = Coordinate.new(1,1)ã
- ãã®ãªãã¸ã§ã¯ãã®ã¡ã½ããã¨ãã¦ã以ä¸ãç¨æãã¦ãã¾ãã
- coordinate:pp()
- 座æ¨æ å ±ãprint()ãã
- coordinate1:width(coordinate2)
- coordinate1ã¨coordinate2éã®æ¨ªå¹ ãè¨ç®ã(2,2)ã¨(4,4)éã®æ¨ªå¹ ã¯3ï¼X座æ¨ã®å·®+1ï¼ã
- coordinate1:height(coordinate2)
- coordinate1ã¨coordinate2éã®é«ãï¼ä¸ï¼ï¼ãè¨ç®ã(2,2)ã¨(4,4)éã®é«ãã¯3ï¼Y座æ¨ã®å·®+1ï¼ã
- coordinate1:midpoint(coodinate2)
- coordinate1ã¨coordinate2ã®ä¸éå°ç¹åº§æ¨ãªãã¸ã§ã¯ããè¿ãã"floor"ãã©ã°ã渡ãã¨å°æ°ç¹ä»¥ä¸åãæ¨ã¦ã
- coordinate:pp()
åè§å½¢ï¼Rectangleï¼ãªãã¸ã§ã¯ãã®ã½ã¼ã¹ã³ã¼ã
-- 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
- åè§å½¢ãªãã¸ã§ã¯ãçæé¢æ°ï¼newï¼ã®å®ç¾©
- ããããã£
- startãå·¦ä¸ã®ç¹ï¼Coordinateãªãã¸ã§ã¯ãï¼
- goalãå³ä¸ã®ç¹ï¼Coordinateãªãã¸ã§ã¯ãï¼
- bgColorãå¡ãã¤ã¶ãè²
- ããããã£
- ã¡ã½ãã
- rectangle1:draw(mon)
- æå®ãããã¢ãã¿ã¼ãããã¯ã¿ã¼ããã«ç»é¢ã«åè§å½¢ãæç»ããã
- å¼æ°ã¨ãã¦ã¢ãã¿ã¼é¢æ°ãã¼ãã«(peripheral.wrap("top")ãªã©)ããã¿ã¼ããã«ç»é¢é¢æ°ãªãã¸ã§ã¯ãï¼termï¼ãä¸ããã
- rectangle1:draw(mon)
ãã¿ã³ï¼Buttonï¼ãªãã¸ã§ã¯ãã®ã½ã¼ã¹ã³ã¼ã
-- 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 obj.toggle_flag = false 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, 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
- Button = Rectangle + label + cmdãã¿ãããªãã®
- åè§å½¢ãæç»ãã¦ããã¿ã³å(name)ãåè§å½¢ã®ä¸å¤®ã«è¡¨è¨ããã®ãdraw()
- button:isWithin(coordinate)ã¯ãä¸ãã座æ¨coordinateããbuttonã®ç¯å²å ãã©ãããè¿ããbooleanã
- button:evalCmd()ã¯ããã¿ã³ã«è¨å®ãããé¢æ°ãå®è¡ï¼evalï¼ããã
- button:run()ã¯ãevalCmd()ã®å¥åé¢æ°ã
ããã«ï¼Panelï¼ãªãã¸ã§ã¯ãã®ã½ã¼ã¹ã³ã¼ã
-- Panel Object local Panel = {} Panel._makeButtons = function(_bCfg, _col, _row, _mon, opt) -- é·ãããã®ã§çç¥ end Panel.new = function(_bCfg, _col, _row, _mon, opt) if _mon.setTextScale then _mon.setTextScale(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 1) self:draw() end Panel.pullButtonPushEvent = function(self, mon_side) local pushed_btn = false local whichButton = function(btns,x,y) for i,b in ipairs(btns) do if b:isWithin(Coordinate.new(x,y)) then pushed_btn = b break end end end repeat if self.mon.setTextScale then -- when self.mon is advanced_monitor local event, side, x, y = os.pullEvent("monitor_touch") if not mon_side or mon_side == side then whichButton(self.btns, x, y) end else -- when self.mon is term local event, mouse, x, y = os.pullEvent("mouse_click") whichButton(self.btns, x, y) end until pushed_btn return "button_push", pushed_btn end
- _makeButtons()ã¯Panel.new()å
ã§ä½¿ç¨ãããµãé¢æ°ã§ãããã«å
ã«é©åã«ãã¿ã³é
ç½®ã§ãããããã¿ã³ç¾¤ãçæããããã®é¨åã®ãããã°ããã®APIä½ææéã®ç´ååãå ãã¦ããï¼æ³£ï¼ã
- åºæ¬çã«ãåãã¿ã³ã®å¹ ã»ä¸ã¯ãã¢ãã¿ãµã¤ãºãåçå²ããã¦è¨ç®ãä½ã£ãä½ç½é¨åã¯marginã¨ãã¦ã端ã«ãããã¿ã³ã«å ãããã¤ã¾ãã端ã«ãããã¿ã³ã大ãããªãã¨ãããã¶ã¤ã³ã
- ãã¿ã³ã®èæ¯è²ã¯ãè²ãã¿ã¼ã³ãæå®ããã{colors.blue, colors.yellow}ã¨æå®ããããéã»é»ã»éã»é»â¦â¦ã¨ç¹°ãè¿ãã
- Panel.new()ã¡ã½ããè¨è¨ã®ãã¤ã³ãã¯ãå¿
é ã®å¼æ°ãæå°éã«ãããã¨ã§ãã§ããã ãç°¡åã«ããã«ãä½æã§ããããã«ãããã¨ãå¿
é 以å¤ã®å¼æ°ã¯ãªãã·ã§ã³ï¼optï¼ã¨ãã¦ãã¼ãã«ã«ã¾ã¨ãã¦ä¸ããããªãã·ã§ã³ãªã®ã§ãå½ç¶ãªããçç¥å¯è½ã
- opt={leftPos=1, topPos=1, rightPos=10, bottomPos=10, fgColor=colors.white, bgColor={colors.blue, colors.yellow}, scale=0.5}
- ã¿ããããã«è¡¨ç¤ºç¯å²ã®æå®ï¼ãleftPos, topPos, rightPos, bottomPos
- ãã¿ã³ã©ãã«ã®è²ï¼ fgColor
- èæ¯è²ãã¿ã¼ã³ï¼ bgColor
- ã¢ãã¿ã¼ã®setTextScaleå¤ï¼scale
- opt={leftPos=1, topPos=1, rightPos=10, bottomPos=10, fgColor=colors.white, bgColor={colors.blue, colors.yellow}, scale=0.5}
- ç¨æããã¡ã½ããã¯ã以ä¸ã®ã¨ããã
- draw()ï¼ãããã«æç»
- drawPushedButtonEffect()ï¼ãã¿ããããã«ãªã®ã§æ¼ãããã©ããã®ãã£ã¼ãããã¯ããªãã¨ä½¿ãã¥ãããæ¼ããããããã®ãã¿ã³ã ãã1ç§ï¼å¼æ°ã§æå®å¯è½ï¼ã ã強調表示ããã
- pullButtonPushEvent()ï¼ãã¿ããããã«ãã¿ã³ããããããã"button_push"ã¤ãã³ãã¨ãã¦ãæ¼ããããã¿ã³ãªãã¸ã§ã¯ããè¿ãã
- ã¿ããããã«ãã¢ãã¿ã¼ãªãã°ã"monitor_touch"ã¤ãã³ããå©ç¨ãããã¿ã¼ããã«ç»é¢ãªãã°ã"mouse_click"ã¤ãã³ããå©ç¨ããã
ããã°ã©ã ã®æå¾ã«ãæä¾ããé¢æ°ã®ã©ããã¼ãå®ç¾©
-- ######################## -- API Functions function makePanel(bCnfg, col, row, mon, opt) return Panel.new(bCnfg, col, row, mon, opt) end
ãããã«
ãªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ã ã¨ãããã§ãªãã¹ã¿ã¤ã«ã§æ¸ããããã³ã¼ãéã¯ä¸æ°ã«å¢ãã¾ããã
ã§ãããªãã¸ã§ã¯ãæåã®ã¹ã¿ã¤ã«ã身ã«ã¤ãã¦ãããªããããã°ã©ã ã®æ¦è¦ããããããããæ©è½è¿½å ãç°¡åã ã¨æãã¾ãã
ãã ããããã°ã©ãã³ã°åå¿è
ã«ä»åã®ã³ã¼ããè¦ãã¦ããããªãç解ããã¨ããã®ãç¡çãªã話ã
ããã¾ã§ããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°ãç解ãã¦ãã人åãã§ãããâ¦â¦ã
ä»å¾ã®æ¹éã¨ãã¦ã¯ãé·ãè¤éã«ãªããã¡ãªããã°ã©ã ã¯ãªãã¸ã§ã¯ãæåã§ããã°ã©ãã³ã°ããåå¿è åãã®è§£èª¬ãç®çã¨ããããã°ã©ã ãç¹ã«çãã§è¦éãã®è¯ãããã°ã©ã ã«ã¤ãã¦ã¯é¢æ°åã®ããã°ã©ãã³ã°ãå¿ããããã¨æãã¾ãã