api.lua 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. class = require("class")
  2. require("offsets")
  3. _LK12VER = "V0.0.1 PRE"
  4. _LK12VERC = 10
  5. --Cursors--
  6. if love.system.getOS() == "Android" or love.system.getOS() == "iOS" then
  7. love.mouse.newCursor = function() end
  8. love.mouse.setCursor = function() end
  9. _IsMobile = true
  10. end
  11. --[[function SetCursor(name)
  12. love.mouse.setCursor(_Cursors[name or ""] or _Cursors["normal"])
  13. end]]
  14. --Internal Variables--
  15. _Font = love.graphics.newImageFont("/font.png",'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"\'`-_/1234567890!?[](){}.,;:<>+=%#^*~ ',1)
  16. --love.graphics.newFont("font.ttf",20)
  17. _ShouldDraw = false
  18. _ForceDraw = false
  19. _ScreenTitle = "Liko 12"
  20. _ColorSet = { --Pico8 Colorset, Should add other 16 color because it's double the width of pico8
  21. {0,0,0,255}, --Black 1
  22. {28,43,83,255}, --Dark Blue 2
  23. {127,36,84,255}, --Dark Red 3
  24. {0,135,81,255}, --Dark Green 4
  25. {171,82,54,255}, --Brown 5
  26. {96,88,79,255}, --Dark Gray 6
  27. {195,195,198,255}, --Gray 7
  28. {255,241,233,255}, --White 8
  29. {237,27,81,255}, --Red 9
  30. {250,162,27,255}, --Orange 10
  31. {247,236,47,255}, --Yellow 11
  32. {93,187,77,255}, --Green 12
  33. {81,166,220,255}, --Blue 13
  34. {131,118,156,255}, --Purple 14
  35. {241,118,166,255}, --Pink 15
  36. {252,204,171,255} --Human Skin 16
  37. }
  38. _ColorSet[0] = {0,0,0,0}
  39. --Callbacks--
  40. function _startup() end --Called at the start of the program
  41. function _update(dt) end --Called when the program updates
  42. function _mpress(x,y,button,it) end --Called when a mouse button is pressed
  43. function _mmove(x,y,dx,dy,it,iw) end --Called when the mouse moves
  44. function _mrelease(x,y,button,it) end --Called when a mouse button is released
  45. function _tpress(id,x,y,dx,dy,button,pressure) end --Called when the screen is touched
  46. function _tmove(id,x,y,dx,dy,pressure) end --Called when the screen touch moves
  47. function _trelease(id,x,y,dx,dy,pressure) end --Called when the screen touch releases
  48. function _kpress(key,scancode,isrepeat) end --Called when a key is pressed
  49. function _krelease(key,scancode) end --Called when a key is released
  50. function _tinput(text) end --Called when text input, uses utf8 format
  51. --Autorun Callbacks--
  52. function _auto_startup() _startup() end --Called at the start of the program
  53. function _auto_update(dt) _update(dt) end --Called when the program updates
  54. function _auto_mpress(x,y,button,it) _mpress(x,y,button,it) end --Called when a mouse button is pressed
  55. function _auto_mmove(x,y,dx,dy,it) _mmove(x,y,dx,dy,it) end --Called when the mouse moves
  56. function _auto_mrelease(x,y,button,it) _mrelease(x,y,button,it) end --Called when a mouse button is released
  57. function _auto_tpress(id,x,y,button,pressure) _tpress(id,x,y,button,pressure) end --Called when the screen is touched
  58. function _auto_tmove(id,x,y,pressure) _tmove(id,x,y,pressure) end --Called when the screen touch moves
  59. function _auto_trelease(id,x,y,pressure) _trelease(id,x,y,pressure) end --Called when the screen touch releases
  60. function _auto_kpress(key,scancode,isrepeat) _kpress(key,scancode,isrepeat) end --Called when a key is pressed
  61. function _auto_krelease(key,scancode) _krelease(key,scancode) end --Called when a key is released
  62. function _auto_tinput(text) _tinput(text) end --Called when text input, uses utf8 format
  63. --Internal Funtions--
  64. function _ScreenToLiko(x,y)
  65. x, y = x-_ScreenX, y-_ScreenY
  66. return floor(x/_ScreenScaleX)+1, floor(y/_ScreenScaleY)+1
  67. end
  68. function _GetColor(c) return _ColorSet[c or 1] end
  69. function _GetColorID(r,g,b,a)
  70. for id,col in pairs(_ColorSet) do
  71. if col[1] == r and col[2] == g and col[3] == b and col[4] == (a or 255) then
  72. return id
  73. end
  74. end
  75. return false
  76. end
  77. --API Functions--
  78. --Graphics Section--
  79. function clear(c) --Clears the screen (fills it with a specific color)
  80. color(c or 1)
  81. rect(1,1,192,128)
  82. _ShouldDraw = true
  83. end
  84. function color(id)
  85. love.graphics.setColor(_ColorSet[id or 1] or _ColorSet[1])
  86. end
  87. function stroke(width) --Sets the lines and the points width
  88. love.graphics.setPointSize(width or 1)
  89. love.graphics.setLineWidth(width or 1)
  90. end
  91. function points(...) --Draws the points: x1,y1, x2, y2, ...
  92. local args = {...}
  93. if not (#args % 2 == 0) then color(args[#args]) table.remove(args,#args) end
  94. for k,v in ipairs(args) do if (k % 2 == 0) then args[k] = v + _goffset.pointY else args[k] = v + _goffset.pointX end end
  95. love.graphics.points(unpack(args))
  96. _ShouldDraw = true
  97. end
  98. point = points
  99. function line(...)
  100. local args = {...}
  101. if not (#args % 2 == 0) then color(args[#args]) table.remove(args,#args) end
  102. love.graphics.line(unpack(args))
  103. _ShouldDraw = true
  104. end
  105. lines = line
  106. function circle(x,y,r,s,c) --x,y,radius,segments,color
  107. if c then color(c) end
  108. love.graphics.circle("fill",x,y,r,s)
  109. _ShouldDraw = true
  110. end
  111. function circle_line(x,y,r,s,c) --x,y,radius,segments,color
  112. if c then color(c) end
  113. love.graphics.circle("line",x,y,r,s)
  114. _ShouldDraw = true
  115. end
  116. function rect(x,y,w,h,c)
  117. if c then color(c) end
  118. local x,y = x + _goffset.rectX, y + _goffset.rectY
  119. love.graphics.rectangle("fill",x,y,w,h)
  120. _ShouldDraw = true
  121. end
  122. function rect_line(x,y,w,h,c)
  123. if c then color(c) end
  124. local x,y = x + _goffset.rect_lineX, y + _goffset.rect_lineY
  125. local w, h = w + _goffset.rect_lineW, h + _goffset.rect_lineH
  126. love.graphics.rectangle("line",x,y,w,h)
  127. --love.graphics.line(x,y,x+w,y,x+w,y+h,x,y+h,x,y)
  128. _ShouldDraw = true
  129. end
  130. cprint = print --Console Print
  131. function print(text,lx,ly)
  132. love.graphics.print(text, floor((lx or 1)+_goffset.printX), floor((ly or 1)+_goffset.printY)) _ShouldDraw = true --_goffset.rectX
  133. end
  134. function print_grid(text,lx,ly)
  135. love.graphics.print(text, floor(((lx or 1)*8-6)+_goffset.printX), floor(((ly or 1)*8-6)+_goffset.printY)) _ShouldDraw = true
  136. end
  137. --Image Section--
  138. Image = class("Liko12.image")
  139. function Image:initialize(path) if type(path) == "string" then self.image = love.graphics.newImage(path) else self.image = love.graphics.newImage(path.imageData) end end
  140. function Image:draw(x,y,r,sx,sy,quad) love.graphics.setColor(255,255,255,255) if quad then love.graphics.draw(self.image,quad,x+_goffset.quadX,y+_goffset.quadY,r,sx,sy) else love.graphics.draw(self.image,x+_goffset.imageX,y+_goffset.imageY,r,sx,sy) end _ShouldDraw = true return self end
  141. function Image:size() return self.image:getDimensions() end
  142. function Image:width() return self.image:getWidth() end
  143. function Image:height() return self.image:getHeight() end
  144. function Image:data() return ImageData(self.image:getData()) end
  145. function Image:quad(x,y,w,h) return love.graphics.newQuad(x-1,y-1,w or self:width(),h or self:height(),self:width(),self:height()) end
  146. ImageData = class("Liko12.imageData")
  147. function ImageData:initialize(w,h) if h then self.imageData = love.image.newImageData(w or 192, h or 128) elseif type(w) == "string" then self.imageData = love.image.newImageData(love.filesystem.newFileData(w,"spritemap","base64")) else self.imageData = w end end
  148. function ImageData:size() return self.imageData:getDimensions() end
  149. function ImageData:getPixel(x,y) return self.imageData:getPixel((x or 1)-1,(y or 1)-1) end
  150. function ImageData:setPixel(x,y,c) self.imageData:setPixel((x or 1)-1,(y or 1)-1,unpack(_GetColor(c))) return self end
  151. function ImageData:map(mf)
  152. self.imageData:mapPixel(
  153. function(x,y,r,g,b,a)
  154. local newCol = mf(x+1,y+1,_GetColorID(r,g,b,a))
  155. newCol = newCol and _GetColor(newCol) or {r,g,b,a}
  156. return unpack(newCol)
  157. end)
  158. return self
  159. end
  160. function ImageData:height() return self.imageData:getHeight() end
  161. function ImageData:width() return self.imageData:getWidth() end
  162. function ImageData:paste(sprData,dx,dy,sx,sy,sw,sh) self.imageData:paste(sprData.imageData,(dx or 1)-1,(dy or 1)-1,(sx or 1)-1,(sy or 1)-1,sw or sprData:width(), sh or sprData:height()) return self end
  163. function ImageData:quad(x,y,w,h) return love.graphics.newQuad(x-1,y-1,w or self:width(),h or self:height(),self:width(),self:height()) end
  164. function ImageData:image() return Image(self) end
  165. function ImageData:export(filename) return self.imageData:encode("png",filename and (filename..".png") or nil) end
  166. function ImageData:enlarge(scale)
  167. local scale = floor(scale or 1)
  168. if scale <= 0 then scale = 1 end --Protection
  169. if scale == 1 then return self end
  170. local newData = ImageData(self:width()*scale,self:height()*scale)
  171. self:map(function(x,y,c)
  172. for iy=1, scale do for ix=1, scale do
  173. newData:setPixel((x-1)*scale + ix,(y-1)*scale + iy,c)
  174. end end
  175. end)
  176. return newData
  177. end
  178. SpriteSheet = class("Liko12.spriteSheet")
  179. function SpriteSheet:initialize(img,w,h)
  180. self.img, self.w, self.h = img, w, h
  181. self.cw, self.ch, self.quads = self.img:width()/self.w, self.img:height()/self.h, {}
  182. for y=1,self.h do for x=1,self.w do
  183. table.insert(self.quads,self.img:quad(x*self.cw-(self.cw-1),y*self.ch-(self.ch-1),self.cw,self.ch))
  184. end end
  185. end
  186. function SpriteSheet:image() return self.img end
  187. function SpriteSheet:data() return self.img:data() end
  188. function SpriteSheet:quad(id) return self.quads[id] end
  189. function SpriteSheet:rect(id) local x,y,w,h = self.quads[id]:getViewport() return x+1,y+1,w,h end
  190. function SpriteSheet:draw(id,x,y,r,sx,sy) self.img:draw(x,y,r,sx,sy,self.quads[id]) _ShouldDraw = true return self end
  191. function SpriteSheet:extract(id) return ImageData(8,8):paste(self:data(),1,1,self:rect(id)) end
  192. function Sprite(id,x,y,r,sx,sy,sheet) (sheet or SpriteMap):draw(id,x,y,r,sx,sy) end
  193. function SpriteGroup(id,x,y,w,h,sx,sy,sheet)
  194. local sx,sy = floor(sx or 1), floor(sy or 1)
  195. for spry = 1, h or 1 do for sprx = 1, w or 1 do
  196. (sheet or SpriteMap):draw((id-1)+sprx+(spry*24-24),x+(sprx*sx*8-sx*8),y+(spry*sy*8-sy*8),0,sx,sy)
  197. end end
  198. end
  199. EditorSheet = SpriteSheet(Image("/editorsheet.png"),24,12)
  200. --Cursor Section--
  201. _CurrentCursor = "normal"
  202. _Cursors = {}
  203. _CachedCursors = {}
  204. function newCursor(data,name,hotx,hoty)
  205. _Cursors[name] = {data = data, hotx = hotx or 1, hoty = hoty or 1}
  206. _CachedCursors[name or "custom"] = love.mouse.newCursor(_Cursors[name].data:enlarge(_ScreenScale).imageData,(_Cursors[name].hotx-1)*_ScreenScale,(_Cursors[name].hoty-1)*_ScreenScale)
  207. end
  208. function loadDefaultCursors()
  209. newCursor(EditorSheet:extract(1),"normal",2,2)
  210. newCursor(EditorSheet:extract(2),"handrelease",3,2)
  211. newCursor(EditorSheet:extract(3),"handpress",3,4)
  212. newCursor(EditorSheet:extract(4),"hand",5,5)
  213. newCursor(EditorSheet:extract(5),"cross",4,4)
  214. setCursor(_CurrentCursor)
  215. end
  216. function setCursor(name)
  217. if not _CachedCursors[name] then _CachedCursors[name or "custom"] = love.mouse.newCursor(_Cursors[name].data:enlarge(_ScreenScale).imageData,(_Cursors[name].hotx-1)*_ScreenScale,(_Cursors[name].hoty-1)*_ScreenScale) end
  218. love.mouse.setCursor(_CachedCursors[name]) _CurrentCursor = name or "custom"
  219. end
  220. function clearCursorsCache() _CachedCursors = {} setCursor(_CurrentCursor) end
  221. --Math Section--
  222. ostime = os.time
  223. function rand_seed(newSeed)
  224. love.math.setRandomSeed(newSeed)
  225. end
  226. function rand(minV,maxV) return love.math.random(minV,maxV) end
  227. function floor(num) return math.floor(num) end
  228. --Gui Function--
  229. function isInRect(x,y,rect)
  230. if x >= rect[1] and y >= rect[2] and x <= rect[1]+rect[3] and y <= rect[2]+rect[4] then return true end return false
  231. end
  232. function whereInGrid(x,y, grid) --Grid X, Grid Y, Grid Width, Grid Height, NumOfCells in width, NumOfCells in height
  233. local gx,gy,gw,gh,cw,ch = unpack(grid)
  234. if isInRect(x,y,{gx,gy,gw,gh}) then
  235. local clw, clh = floor(gw/cw), floor(gh/ch)
  236. local x, y = x-gx, y-gy
  237. local hx = floor(x/clw)+1 hx = hx <= cw and hx or hx-1
  238. local hy = floor(y/clh)+1 hy = hy <= ch and hy or hy-1
  239. return hx,hy
  240. end
  241. return false, false
  242. end
  243. --FileSystem Function--
  244. FS = {}
  245. function FS.write(path,data)
  246. return love.filesystem.write(path,data)
  247. end
  248. function FS.read(path) return love.filesystem.read(path) end
  249. --Misc Functions--
  250. function keyrepeat(state) love.keyboard.setKeyRepeat(state) end
  251. function showkeyboard(state) love.keyboard.setTextInput(state) end
  252. function isMobile() return _isMobile or false end
  253. --Spritesheet--
  254. SpriteMap = SpriteSheet(ImageData(24*8,12*8):image(),24,12)
  255. --[[_ImageSheet = {
  256. IMG = ImageData(24*8,12*8)
  257. }]]