Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --=====================STRUCT_TOUCH===================
- struct_touch = class()
- --this is primitive struct_ class with some service functions for screen class
- --it is represents standart Codea touch class as variable not a constant(as touch class is)
- function struct_touch:init(touch)
- self.id = touch.id
- self.x = touch.x
- self.y = touch.y
- self.prevX = touch.prevX
- self.prevY = touch.prevY
- self.deltaX = touch.deltaX
- self.deltaY = touch.deltaY
- self.state = touch.state
- self.tapCount = touch.tapCount
- end
- --defines new zero of cordinate axis
- function struct_touch:newzero(position)
- self.x = self.x - position.x
- self.y = self.y - position.y
- end
- --check inbound conditions for almost every spriteMode
- function struct_touch:inbounds(width, height)
- if spriteMode() == CENTER or spriteMode() == RADIUS then
- return self.x < width/2 and self.y < height/2
- else
- return math.abs(self.x-width/2)<width/2 and math.abs(self.y-height/2)<height/2
- end
- end
- --updating without constructing new object
- function struct_touch:update(touch)
- self.id = touch.id
- self.x = touch.x
- self.y = touch.y
- self.prevX = touch.prevX
- self.prevY = touch.prevY
- self.deltaX = touch.deltaX
- self.deltaY = touch.deltaY
- self.state = touch.state
- self.tapCount = touch.tapCount
- end
- --=====================SCREEN===================
- screen = class()
- --[[
- main class of the screen lib
- represents screen(or its fixed parts) as a frame and auto-detects WIDTH and HEIGHT constants
- changing and saves self width, height and position
- if position or width/height values equals 1 or less then it will be used in proportion
- with main screen values of WIDTH and HEIGHT else it will be used as precise value in pixels
- ]]
- function screen:init(Width, Height, Position, UserValues)
- --default size is float fullscreen
- self.floatsize = vec2(1,1)
- self.size = vec2(WIDTH, HEIGHT)
- self.globalWH = vec2(WIDTH, HEIGHT)
- --value that allows to not check status of all self correcting variables at every draw-event
- self.updater = false
- if Width or Height then
- if Width then
- if Width <= 1 then
- self.floatsize.x = Width
- self.size.x = WIDTH * Width
- self.updater = true
- else
- self.floatsize.x = nil
- self.size.x = Width
- end
- end
- if Height then
- if Height <= 1 then
- self.floatsize.y = Height
- self.size.y = HEIGHT * Height
- self.updater = true
- else
- self.floatsize.y = nil
- self.size.y = Height
- end
- end
- end
- self.i = image(self.size.x, self.size.y)
- --default position is fixed in (0,0) screen pixel
- self.floatposition = vec2(nil,nil)
- self.pos = vec2(0,0)
- if Position then
- if Position.x <= 1 then
- self.floatposition.x = Position.x
- self.pos.x = self.floatposition.x*WIDTH
- self.updater = true
- else
- self.pos.x = Position.x
- end
- if Position.y <= 1 then
- self.floatposition.y = Position.y
- self.pos.y = self.floatposition.y*HEIGHT
- self.updater = true
- else
- self.pos.y = Position.y
- end
- end
- --array of objects that are visible and toucheble in this frame
- self.objects = {}
- self.visible = true
- self.enable = true
- --force to copy last frame every draw call if true
- self.copylastframe = false
- self.background = color(0,255)
- --reserved memory for struct_touch
- self.CurentTouch = struct_touch(CurrentTouch)
- --[[
- this functions calls every time before drawing all objects in frame, also it calls every
- time in touch event before objects get struct_touch from frame, it have self(screen) as first
- parameter and original touch as second parameter at call, if this function returns false statment
- then render(or touch event) skips, and CallLast starts working
- ]]
- self.CallFirst = function() return true end
- --[[
- this functions calls every time after drawing all objects in frame, also it calls every
- time in touch event after objects get struct_touch from frame, it have self(screen) as first
- parameter and original touch as second parameter at call
- ]]
- self.CallLast = function() return true end
- --[[
- UserValues is a table of local(for this screen) values defined by user, it MUST be table,
- keys are the same as in user-defined table, global values that have the same key automatically
- swaps in touch and draw events before CallFirst calls and after CallLast calls
- ]]
- self.UserValues = {}
- local varCount = 0
- if type(UserValues) == "table" then
- for k,v in pairs(UserValues) do
- self.UserValues[k] = v
- varCount = varCount + 1
- end
- else
- --for not tables
- self.UserValues = nil
- end
- if varCount == 0 then
- --for zero value tables (aka {})
- self.UserValues = nil
- end
- --buffer for global swaping
- self.buff = nil
- end
- --draw-event handler
- function screen:draw()
- if not self.enable or not self.visible then
- return
- end
- if ((self.globalWH.x ~= WIDTH) or (self.globalWH.y ~= HEIGHT)) and self.updater then
- self:update()
- end
- pushStyle()
- setContext(self.i)
- if not self.copylastframe then
- fill(self.background)
- rect(0,0,self.size.x,self.size.y)
- end
- if self.UserValues then
- for k,v in pairs(self.UserValues) do
- if _G[k] then
- self.buff = _G[k]
- _G[k] = v
- self.UserValues[k] = self.buff
- end
- end
- end
- if self.CallFirst(self) then
- for _,v in pairs(self.objects) do
- v:draw()
- end
- end
- self.CallLast(self)
- if self.UserValues then
- for k,v in pairs(self.UserValues) do
- if _G[k] then
- self.buff = _G[k]
- _G[k] = v
- self.UserValues[k] = self.buff
- end
- end
- end
- setContext()
- popStyle()
- sprite(self.i, self.pos.x, self.pos.y)
- end
- --touch-event handler
- function screen:touched(touch)
- if not self.enable then
- return
- end
- self.CurentTouch:update(touch)
- self.CurentTouch:newzero(self.pos)
- if not self.CurentTouch:inbounds(self.size.x, self.size.y) then
- return false
- end
- if self.UserValues then
- for k,v in pairs(self.UserValues) do
- if _G[k] then
- self.buff = _G[k]
- _G[k] = v
- self.UserValues[k] = self.buff
- end
- end
- end
- if self.CallFirst(self, touch) then
- for _,v in pairs(self.objects) do
- v:touched(self.CurentTouch)
- end
- end
- self.CallLast(self, touch)
- if self.UserValues then
- for k,v in pairs(self.UserValues) do
- if _G[k] then
- self.buff = _G[k]
- _G[k] = v
- self.UserValues[k] = self.buff
- end
- end
- end
- return true
- end
- --function that updates width, height and position
- function screen:update()
- self.globalWH.x = WIDTH
- self.globalWH.y = HEIGHT
- if self.floatsize.x then
- self.size.x = WIDTH*self.floatsize.x
- end
- if self.floatsize.y then
- self.size.y = HEIGHT*self.floatsize.y
- end
- self.i = image(self.size.x, self.size.y)
- if self.floatposition.x then
- self.pos.x = self.floatposition.x*WIDTH
- end
- if self.floatposition.y then
- self.pos.y = self.floatposition.y*HEIGHT
- end
- end
- --clears screen settings to default
- function screen:clear()
- self.i = image(self.size.x, self.size.y)
- self.objects = {}
- self.visible = true
- self.enable = true
- self.background = color(0,255)
- self.copylastframe = false
- self.CallFirst = function() return true end
- self.CallLast = function() return true end
- end
- --adds new object to object list
- function screen:push(obj)
- table.insert(self.objects, obj)
- end
- --delete last object (or object on position i) from object list, returns it
- function screen:pop(i)
- return table.remove(self.objects, i)
- end
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement