整理Zframework代码

给所有模块进行更合理的分类
解除部分模块之间的依赖
修复image和bgm模块两个一般不会触发的小问题
close #209
This commit is contained in:
MrZ626
2021-08-16 13:43:51 +08:00
parent fc8d19756d
commit 42620bf739
8 changed files with 71 additions and 142 deletions

View File

@@ -24,8 +24,24 @@ function BGM.init(list)
function BGM.getList()return simpList end function BGM.getList()return simpList end
local count=#simpList local count=#simpList
function BGM.getCount()return count end function BGM.getCount()return count end
function BGM.loadAll()for name in next,Sources do load(name)end end
local function load(name)
if type(Sources[name])=='string'then
if love.filesystem.getInfo(Sources[name])then
Sources[name]=love.audio.newSource(Sources[name],'stream')
Sources[name]:setLooping(true)
Sources[name]:setVolume(0)
return true
else
MES.new('warn',"No BGM file: "..Sources[name],5)
end
elseif Sources[name]then
return true
elseif name then
MES.new('warn',"No BGM: "..name,5)
end
end
function BGM.loadAll()for name in next,Sources do load(name)end end
local function fadeOut(src) local function fadeOut(src)
while true do while true do
coroutine.yield() coroutine.yield()
@@ -51,22 +67,6 @@ function BGM.init(list)
local function removeCurFadeOut(task,code,src) local function removeCurFadeOut(task,code,src)
return task.code==code and task.args[1]==src return task.code==code and task.args[1]==src
end end
local function load(name)
if type(Sources[name])=='string'then
if love.filesystem.getInfo(Sources[name])then
Sources[name]=love.audio.newSource(Sources[name],'stream')
Sources[name]:setLooping(true)
Sources[name]:setVolume(0)
return true
else
MES.new('warn',"No BGM file: "..Sources[name],5)
end
elseif Sources[name]then
return true
elseif name then
MES.new('warn',"No BGM: "..name,5)
end
end
function BGM.play(name) function BGM.play(name)
if not name then name=BGM.default end if not name then name=BGM.default end
if not load(name)then return end if not load(name)then return end

View File

@@ -1,72 +0,0 @@
local gc=love.graphics
local cmds={
origin="origin",
move="translate",
scale="scale",
rotate="rotate",
shear="shear",
clear="clear",
setCL="setColor",
setCM="setColorMask",
setLW="setLineWidth",
setLS="setLineStyle",
setLJ="setLineJoin",
print="print",
setFT=setFont,
mText=GC.str,
mDraw=GC.draw,
mOutDraw=GC.outDraw,
draw="draw",
line="line",
fRect=function(...)gc.rectangle('fill',...)end,
dRect=function(...)gc.rectangle('line',...)end,
fCirc=function(...)gc.circle('fill',...)end,
dCirc=function(...)gc.circle('line',...)end,
fElps=function(...)gc.ellipse('fill',...)end,
dElps=function(...)gc.ellipse('line',...)end,
fPoly=function(...)gc.polygon('fill',...)end,
dPoly=function(...)gc.polygon('line',...)end,
dPie=function(...)gc.arc('line',...)end,
dArc=function(...)gc.arc('line','open',...)end,
dBow=function(...)gc.arc('line','closed',...)end,
fPie=function(...)gc.arc('fill',...)end,
fArc=function(...)gc.arc('fill','open',...)end,
fBow=function(...)gc.arc('fill','closed',...)end,
}
local sizeLimit=gc.getSystemLimits().texturesize
return function(L)
gc.push()
::REPEAT_tryAgain::
local success,canvas=pcall(gc.newCanvas,math.min(L[1],sizeLimit),math.min(L[2],sizeLimit))
if not success then
sizeLimit=math.floor(sizeLimit*.8)
goto REPEAT_tryAgain
end
gc.setCanvas(canvas)
gc.origin()
gc.setColor(1,1,1)
gc.setLineWidth(1)
for i=3,#L do
local cmd=L[i][1]
if type(cmd)=='boolean'and cmd then
table.remove(L[i],1)
cmd=L[i][1]
end
if type(cmd)=='string'then
local func=cmds[cmd]
if type(func)=='string'then func=gc[func]end
if func then
func(unpack(L[i],2))
else
error("No gc command: "..cmd)
end
end
end
gc.setCanvas()
gc.pop()
return canvas
end

View File

@@ -1,7 +1,5 @@
local gc=love.graphics local gc=love.graphics
local setColor=gc.setColor local setColor,printf,draw=gc.setColor,gc.printf,gc.draw
local printf=gc.printf
local draw=gc.draw
local GC={} local GC={}
function GC.str(obj,x,y)printf(obj,x-626,y,1252,'center')end function GC.str(obj,x,y)printf(obj,x-626,y,1252,'center')end
function GC.simpX(obj,x,y)draw(obj,x-obj:getWidth()*.5,y)end function GC.simpX(obj,x,y)draw(obj,x-obj:getWidth()*.5,y)end
@@ -97,7 +95,7 @@ do--function GC.DO(L)
setLJ="setLineJoin", setLJ="setLineJoin",
print="print", print="print",
setFT=setFont, setFT=function(...)setFont(...)end,
mText=GC.str, mText=GC.str,
mDraw=GC.draw, mDraw=GC.draw,
mOutDraw=GC.outDraw, mOutDraw=GC.outDraw,

View File

@@ -12,7 +12,7 @@ function IMG.init(list)
elseif type(list[name])=='string'then elseif type(list[name])=='string'then
self[name]=love.graphics.newImage(list[name]) self[name]=love.graphics.newImage(list[name])
else else
MES.new('warn',"No BGM: "..name,5) MES.new('warn',"No IMG: "..name,5)
self[name]=null self[name]=null
end end
return self[name] return self[name]

View File

@@ -3,44 +3,63 @@ EDITING=""
LOADED=false LOADED=false
ERRDATA={} ERRDATA={}
SCR= require'Zframework.screen' --Pure lua modules (basic)
COLOR= require'Zframework.color' COLOR= require'Zframework.color'
SCN= require'Zframework.scene' TABLE= require'Zframework.tableExtend'
WS= require'Zframework.websocket' STRING= require'Zframework.stringExtend'
PROFILE=require'Zframework.profile'
JSON= require'Zframework.json'
do--Add pcall & MES for JSON lib
local encode,decode=JSON.encode,JSON.decode
JSON.encode=function(val)
local a,b=pcall(encode,val)
if a then
return b
elseif MES then
MES.traceback()
end
end
JSON.decode=function(str)
local a,b=pcall(decode,str)
if a then
return b
elseif MES then
MES.traceback()
end
end
end
require'Zframework.setFont' --Pure lua modules (complex)
LOADLIB=require'Zframework.loadLib'
TASK= require'Zframework.task'
WS= require'Zframework.websocket'
LANG= require'Zframework.languages'
THEME= require'Zframework.theme'
--Love-based modules (basic)
FILE= require'Zframework.file'
WHEELMOV=require'Zframework.wheelScroll'
SCR= require'Zframework.screen'
SCN= require'Zframework.scene'
LIGHT= require'Zframework.light'
--Love-based modules (complex)
GC=require'Zframework.gcExtend' GC=require'Zframework.gcExtend'
mStr=GC.str mStr=GC.str
mText=GC.simpX mText=GC.simpX
mDraw=GC.draw mDraw=GC.draw
require'Zframework.setFont'
LOADLIB=require'Zframework.loadLib'
WHEELMOV=require'Zframework.wheelScroll'
JSON= require'Zframework.json'
TABLE= require'Zframework.tableExtend'
STRING= require'Zframework.stringExtend'
VIB= require'Zframework.vibrate'
SFX= require'Zframework.sfx'
LIGHT= require'Zframework.light'
BG= require'Zframework.background'
WIDGET= require'Zframework.widget'
TEXT= require'Zframework.text' TEXT= require'Zframework.text'
SYSFX= require'Zframework.sysFX' SYSFX= require'Zframework.sysFX'
MES= require'Zframework.message' MES= require'Zframework.message'
BG= require'Zframework.background'
WIDGET= require'Zframework.widget'
VIB= require'Zframework.vibrate'
SFX= require'Zframework.sfx'
IMG= require'Zframework.image' IMG= require'Zframework.image'
BGM= require'Zframework.bgm' BGM= require'Zframework.bgm'
VOC= require'Zframework.voice' VOC= require'Zframework.voice'
LANG= require'Zframework.languages'
TASK= require'Zframework.task'
FILE= require'Zframework.file'
PROFILE=require'Zframework.profile'
THEME= require'Zframework.theme'
local ms,kb=love.mouse,love.keyboard local ms,kb=love.mouse,love.keyboard
local gc=love.graphics local gc=love.graphics

View File

@@ -29,7 +29,7 @@ local json = {}
-- Encode -- Encode
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
local encode local _encode
local escape_char_map = { local escape_char_map = {
["\\"] = "\\", ["\\"] = "\\",
@@ -70,7 +70,7 @@ local function encode_table(val, stack)
end end
if n ~= #val then error("invalid table: sparse array") end if n ~= #val then error("invalid table: sparse array") end
-- Encode -- Encode
for _, v in ipairs(val) do ins(res, encode(v, stack)) end for _, v in ipairs(val) do ins(res, _encode(v, stack)) end
stack[val] = nil stack[val] = nil
return "[" .. table.concat(res, ",") .. "]" return "[" .. table.concat(res, ",") .. "]"
@@ -80,7 +80,7 @@ local function encode_table(val, stack)
if type(k) ~= 'string' then if type(k) ~= 'string' then
error("invalid table: mixed or invalid key types") error("invalid table: mixed or invalid key types")
end end
ins(res, encode(k, stack) .. ":" .. encode(v, stack)) ins(res, _encode(k, stack) .. ":" .. _encode(v, stack))
end end
stack[val] = nil stack[val] = nil
return "{" .. table.concat(res, ",") .. "}" return "{" .. table.concat(res, ",") .. "}"
@@ -107,21 +107,14 @@ local type_func_map = {
['boolean'] = tostring ['boolean'] = tostring
} }
encode = function(val, stack) _encode = function(val, stack)
local t = type(val) local t = type(val)
local f = type_func_map[t] local f = type_func_map[t]
if f then return f(val, stack) end if f then return f(val, stack) end
error("unexpected type '" .. t .. "'") error("unexpected type '" .. t .. "'")
end end
function json.encode(val) json.encode=_encode
local a,b=pcall(encode,val)
if a then
return b
elseif MES then
MES.traceback()
end
end
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
-- Decode -- Decode
@@ -335,7 +328,7 @@ function parse(str, idx)
decode_error(str, idx, "unexpected character '" .. chr .. "'") decode_error(str, idx, "unexpected character '" .. chr .. "'")
end end
local function decode(str) function json.decode(str)
if type(str) ~= 'string' then if type(str) ~= 'string' then
error("expected argument of type string, got " .. type(str)) error("expected argument of type string, got " .. type(str))
end end
@@ -344,12 +337,4 @@ local function decode(str)
if idx <= #str then decode_error(str, idx, "trailing garbage") end if idx <= #str then decode_error(str, idx, "trailing garbage") end
return res return res
end end
function json.decode(str)
local a,b=pcall(decode,str)
if a then
return b
elseif MES then
MES.traceback()
end
end
return json return json

View File

@@ -1,6 +1,5 @@
local gc=love.graphics local gc=love.graphics
local abs=math.abs local abs=math.abs
local SCR=SCR
local scenes={} local scenes={}

View File

@@ -37,7 +37,7 @@ love.setDeprecationOutput(false)
love.keyboard.setKeyRepeat(true) love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(false) love.keyboard.setTextInput(false)
love.mouse.setVisible(false) love.mouse.setVisible(false)
if SYSTEM=='Android'then if SYSTEM=='Android'or SYSTEM=='iOS'then
local w,h,f=love.window.getMode() local w,h,f=love.window.getMode()
f.resizable=false f.resizable=false
love.window.setMode(w,h,f) love.window.setMode(w,h,f)