Advertisement
Mikewin

ssc.lib v1

Sep 23rd, 2015
114
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --=====================STRUCT_TOUCH===================
  2. struct_touch = class()
  3. --this is primitive struct_ class with some service functions for screen class
  4. --it is represents standart Codea touch class as variable not a constant(as touch class is)
  5. function struct_touch:init(touch)
  6.     self.id = touch.id
  7.     self.x = touch.x
  8.     self.y = touch.y
  9.     self.prevX = touch.prevX
  10.     self.prevY = touch.prevY
  11.     self.deltaX = touch.deltaX
  12.     self.deltaY = touch.deltaY
  13.     self.state = touch.state
  14.     self.tapCount = touch.tapCount
  15. end
  16. --defines new zero of cordinate axis
  17. function struct_touch:newzero(position)
  18.     self.x = self.x - position.x
  19.     self.y = self.y - position.y
  20. end
  21. --check inbound conditions for almost every spriteMode
  22. function struct_touch:inbounds(width, height)
  23.     if spriteMode() == CENTER or spriteMode() == RADIUS then
  24.         return self.x < width/2 and self.y < height/2
  25.     else
  26.         return math.abs(self.x-width/2)<width/2 and math.abs(self.y-height/2)<height/2
  27.     end
  28. end
  29. --updating without constructing new object
  30. function struct_touch:update(touch)
  31.     self.id = touch.id
  32.     self.x = touch.x
  33.     self.y = touch.y
  34.     self.prevX = touch.prevX
  35.     self.prevY = touch.prevY
  36.     self.deltaX = touch.deltaX
  37.     self.deltaY = touch.deltaY
  38.     self.state = touch.state
  39.     self.tapCount = touch.tapCount
  40. end
  41. --=====================SCREEN===================
  42. screen = class()
  43. --[[
  44. main class of the screen lib
  45. represents screen(or its fixed parts) as a frame and auto-detects WIDTH and HEIGHT constants
  46. changing and saves self width, height and position
  47. if position or width/height values equals 1 or less then it will be used in proportion
  48. with main screen values of WIDTH and HEIGHT else it will be used as precise value in pixels
  49. ]]
  50. function screen:init(Width, Height, Position, UserValues)
  51.     --default size is float fullscreen
  52.     self.floatsize = vec2(1,1)
  53.     self.size = vec2(WIDTH, HEIGHT)
  54.     self.globalWH = vec2(WIDTH, HEIGHT)
  55.     --value that allows to not check status of all self correcting variables at every draw-event
  56.     self.updater = false
  57.     if Width or Height then
  58.         if Width then
  59.             if Width <= 1 then
  60.                 self.floatsize.x = Width
  61.                 self.size.x = WIDTH * Width
  62.                 self.updater = true
  63.             else
  64.                 self.floatsize.x = nil
  65.                 self.size.x = Width
  66.             end
  67.         end
  68.         if Height then
  69.             if Height <= 1 then
  70.                 self.floatsize.y = Height
  71.                 self.size.y = HEIGHT * Height
  72.                 self.updater = true
  73.             else
  74.                 self.floatsize.y = nil
  75.                 self.size.y = Height
  76.             end
  77.         end
  78.     end
  79.     self.i = image(self.size.x, self.size.y)
  80.     --default position is fixed in (0,0) screen pixel
  81.     self.floatposition = vec2(nil,nil)
  82.     self.pos = vec2(0,0)
  83.     if Position then
  84.         if Position.x <= 1 then
  85.             self.floatposition.x = Position.x
  86.             self.pos.x = self.floatposition.x*WIDTH
  87.             self.updater = true
  88.         else
  89.             self.pos.x = Position.x
  90.         end
  91.         if Position.y <= 1 then
  92.             self.floatposition.y = Position.y
  93.             self.pos.y = self.floatposition.y*HEIGHT
  94.             self.updater = true
  95.         else
  96.             self.pos.y = Position.y
  97.         end
  98.     end
  99. --array of objects that are visible and toucheble in this frame
  100.     self.objects = {}
  101.     self.visible = true
  102.     self.enable = true
  103. --force to copy last frame every draw call if true
  104.     self.copylastframe = false
  105.     self.background = color(0,255)
  106. --reserved memory for struct_touch
  107.     self.CurentTouch = struct_touch(CurrentTouch)
  108. --[[
  109. this functions calls every time before drawing all objects in frame, also it calls every
  110. time in touch event before objects get struct_touch from frame, it have self(screen) as first
  111. parameter and original touch as second parameter at call, if this function returns false statment
  112. then render(or touch event) skips, and CallLast starts working
  113. ]]
  114.     self.CallFirst = function() return true end
  115. --[[
  116. this functions calls every time after drawing all objects in frame, also it calls every
  117. time in touch event after objects get struct_touch from frame, it have self(screen) as first
  118.  parameter and original touch as second parameter at call
  119. ]]
  120.     self.CallLast = function() return true end
  121. --[[
  122. UserValues is a table of local(for this screen) values defined by user, it MUST be table,
  123. keys are the same as in user-defined table, global values that have the same key automatically
  124. swaps in touch and draw events before CallFirst calls and after CallLast calls
  125. ]]
  126.     self.UserValues = {}
  127.     local varCount = 0
  128.     if type(UserValues) == "table" then
  129.         for k,v in pairs(UserValues) do
  130.             self.UserValues[k] = v
  131.             varCount = varCount + 1
  132.         end
  133.     else
  134.         --for not tables
  135.         self.UserValues = nil
  136.     end
  137.     if varCount == 0 then
  138.         --for zero value tables (aka {})
  139.         self.UserValues = nil
  140.     end
  141. --buffer for global swaping
  142.     self.buff = nil
  143. end
  144. --draw-event handler
  145. function screen:draw()
  146.     if not self.enable or not self.visible then
  147.         return
  148.     end
  149.     if ((self.globalWH.x ~= WIDTH) or (self.globalWH.y ~= HEIGHT)) and self.updater then
  150.         self:update()
  151.     end
  152.     pushStyle()
  153.     setContext(self.i)
  154.     if not self.copylastframe then
  155.         fill(self.background)
  156.         rect(0,0,self.size.x,self.size.y)
  157.     end
  158.     if self.UserValues then
  159.         for k,v in pairs(self.UserValues) do
  160.             if _G[k] then
  161.                 self.buff = _G[k]
  162.                 _G[k] = v
  163.                 self.UserValues[k] = self.buff
  164.             end
  165.         end
  166.     end
  167.     if self.CallFirst(self) then
  168.         for _,v in pairs(self.objects) do
  169.             v:draw()
  170.         end
  171.     end
  172.     self.CallLast(self)
  173.     if self.UserValues then
  174.         for k,v in pairs(self.UserValues) do
  175.             if _G[k] then
  176.                 self.buff = _G[k]
  177.                 _G[k] = v
  178.                 self.UserValues[k] = self.buff
  179.             end
  180.         end
  181.     end
  182.     setContext()
  183.     popStyle()
  184.     sprite(self.i, self.pos.x, self.pos.y)
  185. end
  186. --touch-event handler
  187. function screen:touched(touch)
  188.     if not self.enable then
  189.         return
  190.     end
  191.     self.CurentTouch:update(touch)
  192.     self.CurentTouch:newzero(self.pos)
  193.     if not self.CurentTouch:inbounds(self.size.x, self.size.y) then
  194.         return false
  195.     end
  196.     if self.UserValues then
  197.         for k,v in pairs(self.UserValues) do
  198.             if _G[k] then
  199.                 self.buff = _G[k]
  200.                 _G[k] = v
  201.                 self.UserValues[k] = self.buff
  202.             end
  203.         end
  204.     end
  205.     if self.CallFirst(self, touch) then
  206.         for _,v in pairs(self.objects) do
  207.             v:touched(self.CurentTouch)
  208.         end
  209.     end
  210.     self.CallLast(self, touch)
  211.     if self.UserValues then
  212.         for k,v in pairs(self.UserValues) do
  213.             if _G[k] then
  214.                 self.buff = _G[k]
  215.                 _G[k] = v
  216.                 self.UserValues[k] = self.buff
  217.             end
  218.         end
  219.     end
  220.     return true
  221. end
  222. --function that updates width, height and position
  223. function screen:update()
  224.     self.globalWH.x = WIDTH
  225.     self.globalWH.y = HEIGHT
  226.     if self.floatsize.x then
  227.         self.size.x = WIDTH*self.floatsize.x
  228.     end
  229.     if self.floatsize.y then
  230.         self.size.y = HEIGHT*self.floatsize.y
  231.     end
  232.     self.i = image(self.size.x, self.size.y)
  233.     if self.floatposition.x then
  234.         self.pos.x = self.floatposition.x*WIDTH
  235.     end
  236.     if self.floatposition.y then
  237.         self.pos.y = self.floatposition.y*HEIGHT
  238.     end
  239. end
  240. --clears screen settings to default
  241. function screen:clear()
  242.     self.i = image(self.size.x, self.size.y)
  243.     self.objects = {}
  244.     self.visible = true
  245.     self.enable = true
  246.     self.background = color(0,255)
  247.     self.copylastframe = false
  248.     self.CallFirst = function() return true end
  249.     self.CallLast = function() return true end
  250. end
  251. --adds new object to object list
  252. function screen:push(obj)
  253.     table.insert(self.objects, obj)
  254. end
  255. --delete last object (or object on position i) from object list, returns it
  256. function screen:pop(i)
  257.     return table.remove(self.objects, i)
  258. end
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement