Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local AAF_SURFACE_PATH="/lib/surface" -- configure to path where your surfaceAPI is
- local default_width,default_height=term.getSize()
- if not surface then
- os.loadAPI(AAF_SURFACE_PATH)
- if not surface then
- print("No Surface API found, has to be placed in "..AAF_SURFACE_PATH..".")
- do return end
- end
- end
- if not term.isColor() then
- print("AwesomeAppFramework can only be used on advanced computers.")
- do return end
- end
- -- COMPONENT CONFIGURATIONS
- local BUTTON_COLOR_DOWN = colors.cyan
- local BUTTON_COLOR_NORMAL = colors.gray
- local WINDOW_BACKGROUND = colors.lightGray
- local ALERT_NORMAL = colors.white
- local ALERT_TEXT = colors.black
- local ALERT_ACCENT = colors.cyan
- local ALERT_ACCENT_TEXT = colors.black
- local EDITTEXT_COLOR_NORMAL = colors.white
- local EDITTEXT_TEXT_NORMAL = colors.black
- -- APPLICATION DEFINITIONS
- Application = {
- appWindow, alive
- }
- Application.__index=Application
- -- COMPONENT DEFINITIONS
- Screen = {
- components = {}, holdComp=-1, focusedComp=-1, context=0, backColor=WINDOW_BACKGROUND,
- altDown=false, appParent, graphics, appBar
- }
- Screen.__index = Screen
- AppBar = {}
- AppBar.__index=AppBar
- Component = {
- x,y,width,height,color,kind="component",
- draw = function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end,
- onKeyDown = function(self) end,
- onKeyUp = function(self) end,
- screenOwner,
- }
- Component.__index=Component
- Button = {
- x,y,width,height,color,kind="button",
- draw = function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end
- }
- Button.__index=Button
- EditText = {
- x,y,width,height,color=EDITTEXT_COLOR_NORMAL,colorText=EDITTEXT_TEXT_NORMAL,kind="edittext",
- draw = function(self) end,
- onKeyDown = function(self) end,
- onKeyUp = function(self) end
- }
- EditText.__index=EditText
- Alert = {
- title,message,
- onMouseClick = function (self,x,y) end,
- onResult = function() end
- }
- Alert.__index=Alert
- ConfirmDialog = {}
- ConfirmDialog.__index=ConfirmDialog
- Label = {
- x,y,width,height,color,kind="label",
- draw = function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end
- }
- Label.__index=Label
- ProgressBar = {
- x,y,width,height,color,kind="progressbar",
- draw = function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end
- }
- ProgressBar.__index=ProgressBar
- ComboBox = {
- x,y,width,height,color,kind="combobox",
- draw = function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end
- }
- ComboBox.__index=ComboBox
- ImageView = {}
- ImageView.__index=ImageView
- ListView={}
- ListView.__index=ListView
- CheckBox = {}
- CheckBox.__index=CheckBox
- ScrollView = {}
- ScrollView.__index=ScrollView
- Container = {
- x,y,width,height,bgColor,kind="container",
- draw=function(self) end,
- onMouseClick = function(self) end,
- onMouseDown = function(self) end,
- onMouseUp = function(self) end
- }
- Container.__index=Container
- -- MAIN CODE
- function AppBar.onMouseDown(self,btn,x,y)
- if self.hasCloseButton and x==self.w then
- self.screenOwner:close()
- end
- end
- function AppBar.draw(self)
- local surf=self:getSurface()
- surf:fillRect(1,1,self.w,1," ",colors.cyan)
- if self.hasCloseButton then
- surf:fillRect(self.w,1,self.w,1,"X",colors.red)
- end
- if self.title then
- surf:drawText(self.w/2-#self.title/2, 1, self.title, colors.cyan, colors.black)
- end
- surf:render(term,1,self.y)
- end
- function AppBar.new(scr)
- local self={
- items={},
- hasCloseButton=true,
- showName=true,
- draw=AppBar.draw,
- getSurface=function(self) return self.graphics end,
- onMouseDown=AppBar.onMouseDown,
- }
- local w,h=scr:getSize()
- self.w=w
- self.y=h+1
- self.graphics=surface.create(w,1," ",colors.black, colors.white)
- return self
- end
- function Screen.showAlert(self, title, message, onres)
- local c_alert=Alert.new(title,message)
- if not (onres==nil) then
- c_alert.onResult=onres
- end
- self.c_alert=c_alert
- self.cont.context=1
- self.context=1
- end
- function Screen.showConfirm(self, title, message, onPos, onNeg, pos, neg)
- local c_alert=ConfirmDialog.new(title,message,pos,neg)
- if onPos then
- c_alert.onPositiveResult=onPos
- end
- if onNeg then
- c_alert.onNegativeResult=onNeg
- end
- self.c_alert=c_alert
- self.cont.context=1
- self.context=1
- end
- function Screen.close(self)
- self:onClose()
- if (self.disposeOnClose) then
- self.appParent:dispose()
- end
- end
- function Screen.onClose(self)
- end
- function Screen.new(name,sw,sh,sx,sy,woAppBar)
- local self = setmetatable({
- cont=Container.new()
- }, Screen)
- self.cont.screenOwner=self
- self.resetFocus=Screen.resetFocus
- self.disposeOnClose=true
- if not woAppBar then
- if (sw==nil)or(sh==nil) then
- local tw,th=term.getSize()
- self.cont:resize(tw,th-1)
- else
- self.cont:resize(sw,sh-1)
- end
- else
- if (sw==nil)or(sh==nil) then
- local tw,th=term.getSize()
- self.cont:resize(tw,th)
- else
- self.cont:resize(sw,sh)
- end
- end
- self.cont.color=WINDOW_BACKGROUND
- self.cont.top=true
- self.c_alert=nil
- local xw,yh=term.getSize()
- if not((sx==nil)or(sy==nil)) then
- self:move(sx,sy)
- else
- self:move(1,1)
- end
- if not woAppBar then
- self.appBar=AppBar.new(self)
- self.appBar.screenOwner=self
- local w0,h0=self:getSize()
- self.appBarHeight=h0+1
- end
- if name then
- self.name=name
- if not woAppBar then
- self.appBar.title=name
- end
- else
- self.name="Application"
- if not woAppBar then
- self.appBar.title="Application"
- end
- end
- return self
- end
- function Screen.getSize(self)
- return self.cont.width, self.cont.height
- end
- function Screen.dispose(self)
- term.clear()
- term.setCursorPos(1,1)
- end
- function Screen.add(self, child)
- self.cont:add(child)
- end
- function isInRange(px,py,sx,sy,dx,dy)
- return px>=sx and px<=dx and py>=sy and py<=dy
- end
- function Screen.pollEvents(self)
- if self.context==0 then
- -- Pull events from OS
- local event = {os.pullEvent()}
- -- Skip this event if flag set
- if self.ignoreNextEvent then
- self.ignoreNextEvent=false
- return
- end
- -- Terminate application on Alt+X
- if event[1]=="key" then
- if keys.getName(event[2])=="leftAlt" or keys.getName(event[2])=="rightAlt" then
- self.altDown=true
- end
- if keys.getName(event[2])=="x" and self.altDown then
- self.appParent:dispose()
- end
- end
- -- If Alt is up, set flag to false
- if event[1]=="key_up" then
- if keys.getName(event[2])=="leftAlt" or keys.getName(event[2])=="rightAlt" then
- self.altDown=false
- end
- end
- -- Send events to container
- if event[1]=="mouse_click" then
- local btn=event[2]
- local x=event[3]
- local y=event[4]
- if not(y==self.appBarHeight) then
- self.cont:onMouseDown(btn,x,y,self)
- else
- self.appBar:onMouseDown(btn,x,y)
- end
- end
- if event[1]=="mouse_scroll" then
- local btn=event[2]
- local x=event[3]
- local y=event[4]
- if not(y==self.appBarHeight) then
- self.cont:onMouseScroll(event[2],event[3],event[4],self)
- end
- end
- if event[1]=="mouse_up" then
- self.cont:onMouseUp(event[2],event[3],event[4],self)
- end
- if event[1]=="key" then
- self.cont:onKeyDown(event[2])
- end
- if event[1]=="key_up" then
- self.cont:onKeyUp(event[2])
- end
- else
- -- If context is not zero
- local event = {os.pullEvent()}
- local comp=self.c_alert
- if not (comp==nil) then
- if event[1]=="mouse_click" then
- comp:onMouseClick(self,event[3],event[4])
- end
- end
- end
- end
- function Screen.resize(self, w,h)
- self.cont:resize(w,h)
- end
- function Screen.move(self, x,y)
- self.cont.x=x
- self.cont.y=y
- end
- function Screen.resetFocus(self)
- self.cont:resetFocus()
- end
- function Screen.draw(self)
- self.cont:draw(true,self)
- if self.appBar then
- self.appBar:draw()
- end
- end
- -- CONTAINER
- function Container.getScreen(self)
- return self.screenOwner
- end
- function Container.resetFocus(self)
- for _,comp in ipairs(self.subComponents) do
- if comp.kind=="container" then
- comp:resetFocus()
- end
- end
- end
- function Container.onMouseDown(self,btn,x,y,owner)
- for _,comp in ipairs(self.subComponents) do
- if (isInRange(x,y,self.x+comp.x-1, self.y+comp.y-1, self.x+comp.x+comp.width-2, self.y+comp.y+comp.height-2)) then
- self.screenOwner:resetFocus()
- local globIndex=self.contId*100+_
- self.holdComp=globIndex
- self.screenOwner.focusedComp=globIndex
- self.focusedComp=globIndex
- comp:onMouseDown(btn,x,y,self)
- end
- end
- end
- function Container.onMouseUp(self,btn,x,y,owner)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if (isInRange(x,y,self.x+comp.x-1, self.y+comp.y-1, self.x+comp.x+comp.width-2, self.x+comp.y+comp.height-2)) then
- comp:onMouseClick(btn,x,y,self)
- comp:onMouseUp(btn,x,y,self)
- else
- if globIndex==self.holdComp then
- comp:onMouseUp(btn,x,y,self)
- end
- end
- end
- end
- function Container.onKeyDown(self,key)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if self.screenOwner.focusedComp==globIndex or self.focusedComp==globIndex then
- comp:onKeyDown(key)
- end
- end
- end
- function Container.onKeyUp(self,key)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if self.screenOwner.focusedComp==globIndex or self.focusedComp==globIndex then
- comp:onKeyUp(key)
- end
- end
- end
- function Container.add(self,comp)
- comp.screenOwner=self.screenOwner
- comp.container=self
- table.insert(self.subComponents,comp)
- end
- function Container.draw(self,focused,window)
- if not self.top then
- local surf=self:getSurface()
- if self.context==0 then
- surf:fillRect(1,1,self.width,self.height,' ',self.color)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if (comp.kind=="container") or (comp.kind=="scrollview") then
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- surf:drawSurfacePart(comp.x, comp.y, 1, 1, comp.width, comp.height, comp:getRSurface())
- else
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- end
- end
- if (self.top) then
- --surf:render(term)
- end
- elseif self.context==1 then
- local comp = window.c_alert
- comp:draw(self)
- surf:render(term)
- end
- else
- local surf=self:getSurface()
- if self.context==0 then
- surf:fillRect(1,1,self.width,self.height,' ',self.color)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if (comp.kind=="container") or (comp.kind=="scrollview") then
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- surf:drawSurfacePart(comp.x, comp.y, 1, 1, comp.width, comp.height, comp:getRSurface())
- else
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- end
- end
- if (self.top) then
- --surf:render(term)
- surf:render(term, self.x, self.y, 1, 1, self.width, self.height)
- end
- elseif self.context==1 then
- local comp = window.c_alert
- comp:draw(self)
- surf:render(term)
- end
- end
- end
- function Container.resize(self,w,h)
- self.graphics=surface.create(w,h," ",self.color,colors.black)
- self.width=w
- self.height=h
- end
- function Container.getSurface(self)
- return self.graphics
- end
- function Container.getRSurface(self)
- return self.graphics
- end
- function Container.onMouseScroll(self, dir, x, y, window)
- for _,comp in ipairs(self.subComponents) do
- if (isInRange(x,y,self.x+comp.x-1, self.y+comp.y-1, self.x+comp.x+comp.width-2, self.y+comp.y+comp.height-2)) then
- comp:onMouseScroll(dir,x,y,self)
- end
- end
- end
- local globContId=0
- function Container.new()
- local self=setmetatable({
- draw=Container.draw,
- color=colors.red,
- onMouseDown=Container.onMouseDown,
- onMouseUp=Container.onMouseUp,
- onKeyDown=Container.onKeyDown,
- resetFocus=Container.resetFocus,
- onKeyUp=Container.onKeyUp,
- add=Container.add,
- getScreen=Container.getScreen,
- onMouseScroll=Container.onMouseScroll,
- resize=Container.resize,
- getSurface=Container.getSurface,
- context=0,
- contId=globContId+1,
- x=1,
- y=1,
- kind="container",
- subComponents = {}
- }, Component)
- globContId=globContId+1
- self:resize(15,15)
- return self
- end
- -- SCROLLVIEW
- function ScrollView.onMouseDown(self,btn,x,y,owner)
- if x==self.x+self.width-1 then
- return
- end
- for _,comp in ipairs(self.subComponents) do
- if (isInRange(x,y,self.x+comp.x-1, self.y+comp.y-self.yOff-1, self.x+comp.x+comp.width-2, self.y+comp.y-self.yOff+comp.height-2)) then
- self.screenOwner:resetFocus()
- local globIndex=self.contId*100+_
- self.holdComp=globIndex
- self.screenOwner.focusedComp=globIndex
- self.focusedComp=globIndex
- comp:onMouseDown(btn,x,y,self)
- end
- end
- end
- function ScrollView.onMouseUp(self,btn,x,y,owner)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if (isInRange(x,y,self.x+comp.x-1, self.y+comp.y-self.yOff-1, self.x+comp.x+comp.width-2, self.y+comp.y-self.yOff+comp.height-2)) and globIndex==self.holdComp then
- comp:onMouseClick(btn,x,y,self)
- comp:onMouseUp(btn,x,y,self)
- else
- if globIndex==self.holdComp then
- comp:onMouseUp(btn,x,y,self)
- end
- end
- end
- end
- function ScrollView.draw(self)
- local surf=self:getRSurface()
- surf:fillRect(1,1,self.width-1,self.height,' ',self.color)
- for _,comp in ipairs(self.subComponents) do
- local globIndex=self.contId*100+_
- if (comp.kind=="container") or (comp.kind=="scrollview") then
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- surf:drawSurfacePart(comp.x, comp.y, 1, 1, comp.width, comp.height, comp:getRSurface())
- else
- comp:draw(globIndex==self.screenOwner.focusedComp, self)
- end
- end
- surf:drawSurfacePart(1,1,1,self.yOff+1,self.width-1,self.height+self.yOff,self:getSurface())
- surf:fillRect(self.width,1,self.width,self.height,' ',colors.white)
- local max=self:findMaxY()
- if max==0 then
- surf:fillRect(self.width,1,self.width,self.height,' ',colors.gray)
- else
- local sbe=math.floor((self.yOff+self.height-1)/max*self.height+1)
- local sbb=math.floor((self.yOff-1)/max*self.height+1)
- surf:fillRect(self.width,sbe,self.width,sbb,' ',colors.gray)
- end
- end
- function ScrollView.onMouseScroll(self,dir,x,y,parent)
- local yMax=self:findMaxY()
- if dir==1 then
- if self.yOff<yMax-self.height then
- self.yOff=self.yOff+1
- end
- end
- if dir==-1 then
- if self.yOff>0 then
- self.yOff=self.yOff-1
- end
- end
- end
- function ScrollView.findMaxY(self)
- local max=0
- for _,comp in ipairs(self.subComponents) do
- if comp.y+comp.height>max then max=comp.y+comp.height end
- end
- return max
- end
- function ScrollView.new(self)
- local self=setmetatable({
- yOff=0,
- onMouseDown=ScrollView.onMouseDown,
- add=Container.add,
- onMouseUp=ScrollView.onMouseUp,
- findMaxY=ScrollView.findMaxY,
- onMouseScroll=ScrollView.onMouseScroll,
- onKeyUp=Container.onKeyUp,
- onKeyDown=Container.onKeyDown,
- draw=ScrollView.draw,
- getSurface=function(self) return self.internalSurf end,
- getRSurface=function(self) return self.graphics end,
- resetFocus=Container.resetFocus,
- resize=Container.resize,
- width=15,
- context=0,
- height=10,
- x=1,
- y=1,
- color=colors.red,
- internalSurf,
- subComponents={},
- kind="scrollview",
- },Component)
- self:resize(15,10)
- self.contId=globContId+1
- globContId=globContId+1
- self.internalSurf=surface.create(self.width,100,' ',self.color,colors.black)
- return self
- end
- -- ALERT
- function Alert.onMouseClick(self,window,x,y)
- local wid,hei=window:getSize()
- local ml=math.max(#self.title,#self.message)
- local sx=math.floor(wid/2-ml/2-1)
- local sy=math.floor(hei/2-2)
- local dx=math.floor(wid/2+ml/2+1)
- local dy=math.floor(hei/2+2)
- if (x>=sx+1 and x<=sx+#" OK " and y==dy) then
- self:onResult()
- window.context=0
- window.cont.context=0
- window.c_alert=nil
- end
- end
- function Alert.draw(self,window)
- local wid,hei=term.getSize()
- local ml=math.max(#self.title,#self.message)
- local sx=math.floor(wid/2-ml/2-1)
- local sy=math.floor(hei/2-2)
- local dx=math.floor(wid/2+ml/2+1)
- local dy=math.floor(hei/2+2)
- local surf=window:getSurface()
- surf:fillRect(sx,sy,dx,dy,' ',ALERT_ACCENT)
- surf:fillRect(sx,sy+1,dx,dy,' ',ALERT_NORMAL)
- surf:drawText(sx+1,sy, self.title, ALERT_ACCENT, colors.black)
- surf:drawText(sx+1,sy+2,self.message,ALERT_NORMAL, colors.black)
- surf:drawText(sx+1,dy," OK ",ALERT_ACCENT,ALERT_ACCENT_TEXT)
- end
- function Alert.new(title, text)
- local self = setmetatable({
- onMouseClick=Alert.onMouseClick,
- draw=Alert.draw
- }, Alert)
- self.title=title
- self.message=text
- return self
- end
- function ConfirmDialog.onMouseClick(self,window,x,y)
- local wid,hei=window:getSize()
- local ml=math.max(#self.title,#self.message,#(" "..self.posBtn.." ")+3+#(" "..self.negBtn.." "))
- local sx=math.floor(wid/2-ml/2-1)
- local sy=math.floor(hei/2-2)
- local dx=math.floor(wid/2+ml/2+1)
- local dy=math.floor(hei/2+2)
- if (x>=sx+1 and x<=sx+#(" "..self.posBtn.." ") and y==dy) then
- self:onPositiveResult()
- window.context=0
- window.cont.context=0
- window.c_alert=nil
- end
- if (x>=sx+#(" "..self.posBtn.." ")+2 and x<=sx+#(" "..self.posBtn.." ")+1+#(" "..self.negBtn.." ") and y==dy) then
- self:onNegativeResult()
- window.context=0
- window.cont.context=0
- window.c_alert=nil
- end
- end
- function ConfirmDialog.draw(self,window)
- local wid,hei=window.screenOwner:getSize()
- local ml=math.max(#self.title,#self.message,#(" "..self.posBtn.." ")+3+#(" "..self.negBtn.." "))
- local sx=math.floor(wid/2-ml/2-1)
- local sy=math.floor(hei/2-2)
- local dx=math.floor(wid/2+ml/2+1)
- local dy=math.floor(hei/2+2)
- local surf=window:getSurface()
- surf:fillRect(sx,sy,dx,dy,' ',ALERT_ACCENT)
- surf:fillRect(sx,sy+1,dx,dy,' ',ALERT_NORMAL)
- surf:drawText(sx+1,sy, self.title, ALERT_ACCENT, colors.black)
- surf:drawText(sx+1,sy+2,self.message,ALERT_NORMAL, colors.black)
- surf:drawText(sx+1,dy,(" "..self.posBtn.." "),ALERT_ACCENT,ALERT_ACCENT_TEXT)
- surf:drawText(sx+2+#(" "..self.posBtn.." "),dy,(" "..self.negBtn.." "),ALERT_ACCENT,ALERT_ACCENT_TEXT)
- end
- function ConfirmDialog.new(title, text, posBtn, negBtn)
- local self = setmetatable({
- onMouseClick=ConfirmDialog.onMouseClick,
- draw=ConfirmDialog.draw,
- posBtn="Yes",
- negBtn="No",
- onPositiveResult=function(self) end,
- onNegativeResult=function(self) end,
- }, ConfirmDialog)
- self.title=title
- self.message=text
- if posBtn then
- self.posBtn=posBtn
- end
- if negBtn then
- self.negBtn=negBtn
- end
- return self
- end
- -- COMPONENT
- function Component.new()
- local self = setmetatable({}, Component)
- self.x=0
- self.y=0
- self.width=0
- self.height=0
- return self
- end
- -- BUTTON
- function Button.draw(self, focused)
- if self.width==0 or self.height==0 then
- return
- end
- local surf=self.container:getSurface()
- surf:fillRect(self.x, self.y, self.x+self.width-1, self.y+self.height-1, ' ', self.color)
- surf:drawText(self.x, self.y, string.sub(self.text,1,self.width), self.color, colors.white)
- end
- function Button.onMouseDown(self)
- self.color=self.colorPressed
- end
- function Button.onMouseUp(self)
- self.color=self.colorNormal
- end
- function Button.new()
- local self = setmetatable({
- color=BUTTON_COLOR_NORMAL,
- colorNormal=BUTTON_COLOR_NORMAL,
- colorPressed=BUTTON_COLOR_DOWN,
- draw=Button.draw,
- onMouseDown=Button.onMouseDown,
- onMouseUp=Button.onMouseUp,
- onMouseClick=Button.onMouseClick,
- width=8,
- height=1,
- x=1,
- y=1,
- kind="button",
- text="button"
- }, Component)
- return self
- end
- -- EditText
- function EditText.draw(self,focused)
- if self.width==0 or self.height==0 then
- return
- end
- local surf=self.container:getSurface()
- surf:fillRect(self.x, self.y, self.x+self.width-1, self.y+self.height-1, ' ', self.color)
- local si=(#self.text-(self.width-1))>0 and #self.text-(self.width-1) or 1
- local sd=(#self.text>self.width) and self.width or #self.text
- if not self.mask then
- surf:drawText(self.x,self.y,string.sub(self.text,si,#self.text),self.color,self.colorText)
- else
- if sd>0 then
- surf:fillRect(self.x,self.y,self.x+sd-1,self.y,'*',self.color,self.colorText)
- end
- end
- if (focused) then
- surf:drawText(sd+self.x,self.y,'_',self.color,self.colorText)
- end
- end
- function EditText.onKeyUp(self, key)
- if keys.getName(key)=="leftShift" or keys.getName(key)=="rightShift" then
- self.shiftDown=false
- return
- end
- end
- function isTypeable(key)
- if key>=2 and key<=11 then return true end
- if key>=16 and key<=27 then return true end
- if key>=30 and key<=40 then return true end
- if key>=44 and key<=53 then return true end
- if keys.getName(key)=="space" then return true end
- return false
- end
- function toKey(key,shiftDown)
- -- Handle special keys
- if keys.getName(key)=="leftBracket" then
- return shiftDown and "{" or "["
- end
- if keys.getName(key)=="rightBracket" then
- return shiftDown and "}" or "]"
- end
- if keys.getName(key)=="semiColon" then
- return shiftDown and ":" or ";"
- end
- if keys.getName(key)=="apostrophe" then
- return shiftDown and "\"" or "'"
- end
- if keys.getName(key)=="comma" then
- return shiftDown and "<" or ","
- end
- if keys.getName(key)=="period" then
- return shiftDown and ">" or "."
- end
- if keys.getName(key)=="slash" then
- return shiftDown and "?" or "/"
- end
- -- Numbers
- if key==2 then
- return shiftDown and "!" or "1"
- end
- if key==3 then
- return shiftDown and "@" or "2"
- end
- if key==4 then
- return shiftDown and "#" or "3"
- end
- if key==5 then
- return shiftDown and "$" or "4"
- end
- if key==6 then
- return shiftDown and "%" or "5"
- end
- if key==7 then
- return shiftDown and "^" or "6"
- end
- if key==8 then
- return shiftDown and "&" or "7"
- end
- if key==9 then
- return shiftDown and "*" or "8"
- end
- if key==10 then
- return shiftDown and "(" or "9"
- end
- if key==11 then
- return shiftDown and ")" or "0"
- end
- if shiftDown then
- return string.upper(keys.getName(key))
- end
- if keys.getName(key)=="space" then
- return " "
- end
- return keys.getName(key)
- end
- function EditText.onKeyDown(self, key)
- if (#self.text>0) and (keys.getName(key)=="backspace") then
- self.text=string.sub(self.text,1,#self.text-1)
- return
- end
- if keys.getName(key)=="leftShift" or keys.getName(key)=="rightShift" then
- self.shiftDown=true
- return
- end
- if self.limit==0 then
- if isTypeable(key) then
- self.text=self.text..toKey(key,self.shiftDown)
- self:onType(key)
- end
- else
- if isTypeable(key) and #self.text<self.limit then
- self.text=self.text..toKey(key,self.shiftDown)
- self:onType(key)
- end
- end
- end
- function EditText.new()
- local self = setmetatable({
- draw=EditText.draw,
- color=EDITTEXT_COLOR_NORMAL,
- colorText=EDITTEXT_TEXT_NORMAL,
- onKeyDown=EditText.onKeyDown,
- onKeyUp=EditText.onKeyUp,
- onType=function(self,key)end,
- x=1,
- y=1,
- width=17,
- height=1,
- limit=0,
- kind="edittext",
- text=""
- }, Component)
- return self
- end
- -- Label
- function ellipsisize(str,n)
- return string.sub(str,1,n-2)..".."
- end
- function Label.draw(self)
- local surf=self.container:getSurface()
- if self.width==0 then
- surf:drawText(self.x,self.y,self.text,self.container.color,self.color)
- else
- surf:drawText(self.x,self.y,ellipsisize(self.text,self.width),self.container.color,self.color)
- end
- end
- function Label.new()
- local self=setmetatable({
- draw=Label.draw,
- color=colors.black,
- width=0,
- height=0,
- x=1,
- y=1,
- text=""
- }, Component)
- return self
- end
- -- ListView
- function ListView.draw(self)
- local surf=self.container:getSurface()
- for _,item in ipairs(self.items) do
- local col=_==self.itemSelected and colors.cyan or self.bgColor
- surf:fillRect(self.x,self.y+_-1,self.x+self.width-1,self.y+_-1,' ',col,self.color)
- surf:drawText(self.x,self.y+_-1,item,col,self.color)
- end
- end
- function ListView.add(self, item)
- table.insert(self.items, item)
- self.height=self.height+1
- end
- function ListView.onMouseDown(self,btn, x, y, sender)
- local indexClicked=sender.kind=="scrollview" and (sender.yOff==0 and y-sender.y-self.y+2 or y-sender.y-self.y+2+sender.yOff) or y-1
- if indexClicked<=#self.items then
- self.itemSelected=indexClicked
- if self.selectable then
- self:onSelectItem(indexClicked)
- end
- end
- end
- function ListView.onMouseUp(self,btn,x,y,sender)
- local indexClicked=sender.kind=="scrollview" and (sender.yOff==0 and y-sender.y-self.y+2 or y-sender.y-self.y+2+sender.yOff) or y-1
- if indexClicked==self.itemSelected and not self.selectable then
- self:onClickItem(self.itemSelected)
- end
- if not self.selectable then
- self.itemSelected=0
- end
- end
- function ListView.new()
- local self=setmetatable({
- draw=ListView.draw,
- add=ListView.add,
- color=colors.black,
- bgColor=colors.white,
- onMouseDown=ListView.onMouseDown,
- onMouseUp=ListView.onMouseUp,
- onSelectItem=function(self, index) end,
- onClickItem=function(self, index) end,
- selectable=false,
- itemSelected=0,
- width=15,
- height=0,
- x=1,
- y=1,
- items={},
- }, Component)
- return self
- end
- -- PROGRESSBAR
- function ProgressBar.draw(self)
- if self.width<=0 or self.height<=0 then
- return
- end
- local surf=self.container:getSurface()
- local pw=math.min(math.floor((self.value/self.valueMax)*(self.width-1)),self.width-1)
- surf:fillRect(self.x,self.y,self.x+pw-1,self.height,' ',self.accent)
- surf:fillRect(self.x+pw,self.y,self.width-1,self.height,' ',self.color)
- end
- function ProgressBar.new()
- local self=setmetatable({
- draw=ProgressBar.draw,
- accent=ALERT_ACCENT,
- color=colors.white,
- width=15,
- height=1,
- x=1,
- y=1,
- value=0,
- valueMax=100,
- text=""
- }, Component)
- return self
- end
- -- COMBOBOX
- ComboBoxOverlay = {
- draw=function() end,
- onMouseClick=function() end,
- x,y
- }
- ComboBoxOverlay.__index=ComboBoxOverlay
- function ComboBoxOverlay.onMouseClick(self, window, x, y)
- if x>=self.x and x<=self.x+window.a_data.width-1 and y>=self.y and y<=self.y+#window.a_data.items-1 then
- window.cont.context=0
- window.context=0
- window.ignoreNextEvent=true
- local off=y-self.y+1
- window.a_data.selectedItem=off
- window.a_data:onSelectItem(off)
- else
- window.context=0
- window.ignoreNextEvent=true
- end
- end
- function ComboBoxOverlay.draw(self,window)
- local mw=window.screenOwner.a_data.width
- local mh=#window.screenOwner.a_data.items
- local surf=window:getSurface()
- surf:fillRect(self.x,self.y,self.x+mw-1,self.y+mh-1,' ',colors.white)
- for _,item in ipairs(window.screenOwner.a_data.items) do
- surf:drawText(self.x,self.y-1+_,item,colors.white,colors.black)
- end
- --window.cont:draw(self,nil, 1)
- end
- function ComboBoxOverlay.new()
- local self=setmetatable({
- onMouseClick=ComboBoxOverlay.onMouseClick,
- draw=ComboBoxOverlay.draw
- }, ComboBoxOverlay)
- return self
- end
- function ComboBox.onMouseClick(self,button,x,y,window)
- if #self.items>=2 then
- window:getScreen().cont.context=1
- window:getScreen().context=1
- window:getScreen().a_data=self
- local c_overlay=ComboBoxOverlay.new()
- c_overlay.x=self.x
- c_overlay.y=self.y
- window:getScreen().c_alert=c_overlay
- end
- end
- function ComboBox.draw(self)
- local surf=self.container:getSurface()
- surf:fillRect(self.x,self.y,self.x+self.width-2,self.y+self.height-1,' ',self.color)
- if not (self.items[self.selectedItem]==nil) then
- surf:drawText(self.x,self.y,self.items[self.selectedItem],self.color,colors.black)
- end
- surf:fillRect(self.width-1,self.y,self.width-1,self.y+self.height-1,'V',self.accent,colors.black)
- end
- function ComboBox.new()
- local self=setmetatable({
- draw=ComboBox.draw,
- onMouseClick=ComboBox.onMouseClick,
- accent=ALERT_ACCENT,
- color=colors.white,
- width=15,
- height=1,
- x=1,
- y=1,
- selectedItem=1,
- onSelectItem=function(self, index) end,
- items={},
- kind="combobox",
- }, Component)
- return self
- end
- -- IMAGEVIEW
- function ImageView.draw(self)
- local surf=self.container:getSurface()
- if self.fillBackground then
- surf:fillRect(self.x,self.y,self.width+self.x-1,self.y+self.height-1,' ',self.color)
- end
- if self.imgSurf then
- surf:drawSurfacePart(self.x, self.y, 1, 1, self.width, self.height, self.imgSurf)
- end
- end
- function ImageView.loadImage(self, file)
- self.file=file
- self.imgSurf=surface.load(file)
- end
- function ImageView.getSurface(self)
- return self.imgSurf
- end
- function ImageView.new(file)
- local self = setmetatable({}, Component)
- self.x=1
- self.y=1
- self.width=10
- self.height=10
- self.fillBackground=true
- self.color=colors.black
- if file then
- self.file=file
- self.imgSurf=surface.load(file)
- end
- self.kind="imageview"
- self.getSurface=ImageView.getSurface
- self.loadImage=ImageView.loadImage
- self.draw=ImageView.draw
- return self
- end
- -- CHECKBOX
- function CheckBox.draw(self)
- local surf=self.container:getSurface()
- surf:drawText(self.x,self.y,string.sub(self.text,1,self.width-2), WINDOW_BACKGROUND, colors.black)
- local c=self.state and 'X' or ' '
- surf:drawPixel(self.x+self.width-1,self.y,c,colors.white,colors.black)
- end
- function CheckBox.onMouseClick(self)
- if self.state then
- self.state=false
- else
- self.state=true
- end
- end
- function CheckBox.new()
- local self = setmetatable({}, Component)
- self.x=1
- self.y=1
- self.width=15
- self.height=1
- self.text="Checkbox"
- self.kind="checkbox"
- self.state=state
- self.onMouseClick=CheckBox.onMouseClick
- self.draw=CheckBox.draw
- return self
- end
- -- APPLICATION
- function Application.start(self, main_code)
- self.alive=true
- if (not (self.appWindow==nil)) then
- self.appWindow.appParent=self
- end
- local win_proc=(function()
- while self.alive do
- if self.appWindow==nil then
- sleep(1)
- else
- self.appWindow:draw()
- self.appWindow:pollEvents()
- end
- end
- end)
- local function wake_notifier()
- local myTimer = os.startTimer(0.01)
- end
- if (not (self.appWindow==nil)) then
- parallel.waitForAll(win_proc,main_code,wake_notifier)
- elseif (not (main_code==nil)) then
- main_code()
- end
- end
- function Application.dispose(self)
- if not (self.appWindow==nil) then
- self.appWindow:dispose()
- end
- self.alive=false
- end
- function Application.new()
- local self=setmetatable({},Application)
- return self
- end
Advertisement
Add Comment
Please, Sign In to add comment