重构字体模块,支持多字体
控制台应用等宽字体
This commit is contained in:
@@ -1,67 +1,60 @@
|
||||
local gc=love.graphics
|
||||
local set=gc.setFont
|
||||
local fontCache={}
|
||||
local currentFontSize
|
||||
local fontFiles,fontCache={},{}
|
||||
local defaultFont,defaultFallBack
|
||||
local curFont=false--Current using font object
|
||||
|
||||
local FONT={}
|
||||
function FONT.set(s)
|
||||
if s~=currentFontSize then
|
||||
if not fontCache[s]then
|
||||
fontCache[s]=gc.setNewFont(s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
end
|
||||
set(fontCache[s])
|
||||
currentFontSize=s
|
||||
end
|
||||
end
|
||||
function FONT.get(s)
|
||||
function FONT.setDefault(name)defaultFont=name end
|
||||
function FONT.setFallback(name)defaultFallBack=name end
|
||||
function FONT.rawget(s)
|
||||
if not fontCache[s]then
|
||||
fontCache[s]=gc.setNewFont(s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
end
|
||||
return fontCache[s]
|
||||
end
|
||||
function FONT.reset()
|
||||
for s in next,fontCache do
|
||||
fontCache[s]=gc.setNewFont(s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
end
|
||||
function FONT.rawset(s)
|
||||
set(fontCache[s]or FONT.rawget(s))
|
||||
end
|
||||
|
||||
function FONT.load(mainFont,secFont)
|
||||
assert(love.filesystem.getInfo(mainFont),"Font file '"..mainFont.."' not exist!")
|
||||
mainFont=love.filesystem.newFile(mainFont)
|
||||
if secFont and love.filesystem.getInfo(secFont)then
|
||||
secFont=love.filesystem.newFile(secFont)
|
||||
else
|
||||
secFont=false
|
||||
end
|
||||
function FONT.set(s)
|
||||
if s~=currentFontSize then
|
||||
if not fontCache[s]then
|
||||
fontCache[s]=gc.setNewFont(mainFont,s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
if secFont then
|
||||
fontCache[s]:setFallbacks(gc.setNewFont(secFont,s,'light',gc.getDPIScale()*SCR.k*2))
|
||||
end
|
||||
end
|
||||
set(fontCache[s])
|
||||
currentFontSize=s
|
||||
end
|
||||
end
|
||||
function FONT.get(s)
|
||||
if not fontCache[s]then
|
||||
fontCache[s]=gc.setNewFont(mainFont,s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
if secFont then
|
||||
fontCache[s]:setFallbacks(gc.setNewFont(secFont,s,'light',gc.getDPIScale()*SCR.k*2))
|
||||
end
|
||||
end
|
||||
return fontCache[s]
|
||||
end
|
||||
function FONT.reset()
|
||||
for s in next,fontCache do
|
||||
fontCache[s]=gc.setNewFont(mainFont,s,'light',gc.getDPIScale()*SCR.k*2)
|
||||
if secFont then
|
||||
fontCache[s]:setFallbacks(gc.setNewFont(secFont,s,'light',gc.getDPIScale()*SCR.k*2))
|
||||
end
|
||||
end
|
||||
function FONT.load(fonts)
|
||||
for name,path in next,fonts do
|
||||
assert(love.filesystem.getInfo(path),("Font file $1($2) not exist!"):repD(name,path))
|
||||
fontFiles[name]=love.filesystem.newFile(path)
|
||||
fontCache[name]={}
|
||||
end
|
||||
FONT.reset()
|
||||
end
|
||||
function FONT.get(size,name)
|
||||
if not name then name=defaultFont end
|
||||
local f=fontCache[name][size]
|
||||
if not f then
|
||||
f=gc.setNewFont(fontFiles[name],size,'light',gc.getDPIScale()*SCR.k*2)
|
||||
if defaultFallBack and name~=defaultFallBack then
|
||||
f:setFallbacks(FONT.get(size,defaultFallBack))
|
||||
end
|
||||
fontCache[name][size]=f
|
||||
end
|
||||
return f
|
||||
end
|
||||
function FONT.set(size,name)
|
||||
if not name then name=defaultFont end
|
||||
|
||||
local f=fontCache[name][size]
|
||||
if f~=curFont then
|
||||
curFont=f or FONT.get(size,name)
|
||||
set(curFont)
|
||||
end
|
||||
end
|
||||
function FONT.reset()
|
||||
for name,cache in next,fontCache do
|
||||
if type(cache)=='table'then
|
||||
for size in next,cache do
|
||||
cache[size]=FONT.get(size,name)
|
||||
end
|
||||
else
|
||||
fontCache[name]=FONT.rawget(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return FONT
|
||||
|
||||
@@ -95,6 +95,7 @@ do--function GC.DO(L)
|
||||
setLJ="setLineJoin",
|
||||
|
||||
print="print",
|
||||
rawFT=function(...)FONT.rawset(...)end,
|
||||
setFT=function(...)FONT.set(...)end,
|
||||
mText=GC.mStr,
|
||||
mDraw=GC.draw,
|
||||
|
||||
@@ -527,17 +527,17 @@ local wsBottomImage do
|
||||
wsBottomImage=GC.DO(L)
|
||||
end
|
||||
local ws_deadImg=GC.DO{20,20,
|
||||
{'setFT',20},
|
||||
{'rawFT',20},
|
||||
{'setCL',1,.3,.3},
|
||||
{'mText',"X",11,-1},
|
||||
}
|
||||
local ws_connectingImg=GC.DO{20,20,
|
||||
{'setFT',20},
|
||||
{'rawFT',20},
|
||||
{'setLW',3},
|
||||
{'mText',"C",11,-1},
|
||||
}
|
||||
local ws_runningImg=GC.DO{20,20,
|
||||
{'setFT',20},
|
||||
{'rawFT',20},
|
||||
{'setCL',.5,1,0},
|
||||
{'mText',"R",11,-1},
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ local clearIcon=GC.DO{40,40,
|
||||
{'fRect',11,14,18,21},
|
||||
}
|
||||
local sureIcon=GC.DO{40,40,
|
||||
{'setFT',35},
|
||||
{'rawFT',35},
|
||||
{'mText',"?",20,0},
|
||||
}
|
||||
local smallerThen=GC.DO{20,20,
|
||||
@@ -82,7 +82,7 @@ function text:draw()
|
||||
end
|
||||
end
|
||||
end
|
||||
function WIDGET.newText(D)--name,x,y[,fText][,color][,font=30][,align='M'][,hideF][,hide]
|
||||
function WIDGET.newText(D)--name,x,y[,fText][,color][,font=30][,fType][,align='M'][,hideF][,hide]
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
x= D.x,
|
||||
@@ -91,6 +91,7 @@ function WIDGET.newText(D)--name,x,y[,fText][,color][,font=30][,align='M'][,hide
|
||||
fText=D.fText,
|
||||
color=D.color and(COLOR[D.color]or D.color)or COLOR.Z,
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
align=D.align or'M',
|
||||
hideF=D.hideF,
|
||||
}
|
||||
@@ -139,7 +140,7 @@ function button:reset()
|
||||
end
|
||||
function button:setObject(obj)
|
||||
if type(obj)=='string'or type(obj)=='number'then
|
||||
self.obj=gc.newText(FONT.get(self.font),obj)
|
||||
self.obj=gc.newText(FONT.get(self.font,self.fType),obj)
|
||||
elseif obj then
|
||||
self.obj=obj
|
||||
end
|
||||
@@ -209,7 +210,7 @@ function button:draw()
|
||||
end
|
||||
end
|
||||
function button:getInfo()
|
||||
return("x=%d,y=%d,w=%d,h=%d,font=%d"):format(self.x+self.w*.5,self.y+self.h*.5,self.w,self.h,self.font)
|
||||
return("x=%d,y=%d,w=%d,h=%d,font=%d"):format(self.x+self.w*.5,self.y+self.h*.5,self.w,self.h,self.font,self.fType)
|
||||
end
|
||||
function button:press(_,_,k)
|
||||
self.code(k)
|
||||
@@ -225,7 +226,7 @@ function button:press(_,_,k)
|
||||
SFX.play('button')
|
||||
end
|
||||
end
|
||||
function WIDGET.newButton(D)--name,x,y,w[,h][,fText][,color][,font=30][,sound=true][,align='M'][,edge=0],code[,hideF][,hide]
|
||||
function WIDGET.newButton(D)--name,x,y,w[,h][,fText][,color][,font=30][,fType][,sound=true][,align='M'][,edge=0],code[,hideF][,hide]
|
||||
if not D.h then D.h=D.w end
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
@@ -246,6 +247,7 @@ function WIDGET.newButton(D)--name,x,y,w[,h][,fText][,color][,font=30][,sound=tr
|
||||
fText=D.fText,
|
||||
color=D.color and(COLOR[D.color]or D.color)or COLOR.Z,
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
align=D.align or'M',
|
||||
edge= D.edge or 0,
|
||||
sound=D.sound~=false,
|
||||
@@ -268,7 +270,7 @@ function key:reset()
|
||||
end
|
||||
function key:setObject(obj)
|
||||
if type(obj)=='string'or type(obj)=='number'then
|
||||
self.obj=gc.newText(FONT.get(self.font),obj)
|
||||
self.obj=gc.newText(FONT.get(self.font,self.fType),obj)
|
||||
elseif obj then
|
||||
self.obj=obj
|
||||
end
|
||||
@@ -331,7 +333,7 @@ function key:draw()
|
||||
end
|
||||
end
|
||||
function key:getInfo()
|
||||
return("x=%d,y=%d,w=%d,h=%d,font=%d"):format(self.x+self.w*.5,self.y+self.h*.5,self.w,self.h,self.font)
|
||||
return("x=%d,y=%d,w=%d,h=%d,font=%d"):format(self.x+self.w*.5,self.y+self.h*.5,self.w,self.h,self.font,self.fType)
|
||||
end
|
||||
function key:press(_,_,k)
|
||||
self.code(k)
|
||||
@@ -339,7 +341,7 @@ function key:press(_,_,k)
|
||||
SFX.play('key')
|
||||
end
|
||||
end
|
||||
function WIDGET.newKey(D)--name,x,y,w[,h][,fText][,fShade][,noFrame][,color][,font=30][,sound=true][,align='M'][,edge=0],code[,hideF][,hide]
|
||||
function WIDGET.newKey(D)--name,x,y,w[,h][,fText][,fShade][,noFrame][,color][,font=30][,fType][,sound=true][,align='M'][,edge=0],code[,hideF][,hide]
|
||||
if not D.h then D.h=D.w end
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
@@ -362,6 +364,7 @@ function WIDGET.newKey(D)--name,x,y,w[,h][,fText][,fShade][,noFrame][,color][,fo
|
||||
noFrame=D.noFrame,
|
||||
color= D.color and(COLOR[D.color]or D.color)or COLOR.Z,
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
sound= D.sound~=false,
|
||||
align= D.align or'M',
|
||||
edge= D.edge or 0,
|
||||
@@ -430,7 +433,7 @@ function switch:draw()
|
||||
gc_draw(obj,x-12-ATV,y,nil,min(self.lim/obj:getWidth(),1),1,obj:getWidth(),obj:getHeight()*.5)
|
||||
end
|
||||
function switch:getInfo()
|
||||
return("x=%d,y=%d,font=%d"):format(self.x,self.y,self.font)
|
||||
return("x=%d,y=%d,font=%d"):format(self.x,self.y,self.font,self.fType)
|
||||
end
|
||||
function switch:press()
|
||||
self.code()
|
||||
@@ -438,7 +441,7 @@ function switch:press()
|
||||
SFX.play('touch')
|
||||
end
|
||||
end
|
||||
function WIDGET.newSwitch(D)--name,x,y[,lim][,fText][,color][,font=30][,sound=true][,disp],code[,hideF][,hide]
|
||||
function WIDGET.newSwitch(D)--name,x,y[,lim][,fText][,color][,font=30][,fType][,sound=true][,disp],code[,hideF][,hide]
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
|
||||
@@ -453,6 +456,7 @@ function WIDGET.newSwitch(D)--name,x,y[,lim][,fText][,color][,font=30][,sound=tr
|
||||
fText=D.fText,
|
||||
color=D.color and(COLOR[D.color]or D.color)or COLOR.Z,
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
sound=D.sound~=false,
|
||||
disp= D.disp,
|
||||
code= D.code,
|
||||
@@ -595,7 +599,7 @@ end
|
||||
function slider:arrowKey(k)
|
||||
self:scroll((k=="left"or k=="up")and -1 or 1)
|
||||
end
|
||||
function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,font=30][,change],disp[,show],code,hide
|
||||
function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,font=30][,fType][,change],disp[,show],code,hide
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
|
||||
@@ -617,6 +621,7 @@ function WIDGET.newSlider(D)--name,x,y,w[,lim][,fText][,color][,unit][,smooth][,
|
||||
unit= D.unit or 1,
|
||||
smooth=false,
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
change=D.change,
|
||||
disp= D.disp,
|
||||
code= D.code,
|
||||
@@ -863,7 +868,7 @@ function inputBox:draw()
|
||||
|
||||
--Drawable
|
||||
local f=self.font
|
||||
FONT.set(f)
|
||||
FONT.set(f,self.fType)
|
||||
if self.obj then
|
||||
mDraw_Y(self.obj,x-12-self.obj:getWidth(),y+h*.5)
|
||||
end
|
||||
@@ -906,7 +911,7 @@ function inputBox:keypress(k)
|
||||
self.value=t
|
||||
end
|
||||
end
|
||||
function WIDGET.newInputBox(D)--name,x,y,w[,h][,font=30][,secret][,regex][,limit],hide
|
||||
function WIDGET.newInputBox(D)--name,x,y,w[,h][,font=30][,fType][,secret][,regex][,limit],hide
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
|
||||
@@ -922,6 +927,7 @@ function WIDGET.newInputBox(D)--name,x,y,w[,h][,font=30][,secret][,regex][,limit
|
||||
},
|
||||
|
||||
font= D.font or int(D.h/7-1)*5,
|
||||
fType=D.fType,
|
||||
secret=D.secret==true,
|
||||
regex= D.regex,
|
||||
limit= D.limit,
|
||||
@@ -1022,7 +1028,7 @@ function textBox:draw()
|
||||
gc_rectangle('line',x,y,w,h,3)
|
||||
|
||||
--Texts
|
||||
FONT.set(self.font)
|
||||
FONT.set(self.font,self.fType)
|
||||
gc_push('transform')
|
||||
gc_translate(x,y)
|
||||
|
||||
@@ -1054,7 +1060,7 @@ end
|
||||
function textBox:getInfo()
|
||||
return("x=%d,y=%d,w=%d,h=%d"):format(self.x+self.w*.5,self.y+self.h*.5,self.w,self.h)
|
||||
end
|
||||
function WIDGET.newTextBox(D)--name,x,y,w,h[,font=30][,lineH][,fix],hide
|
||||
function WIDGET.newTextBox(D)--name,x,y,w,h[,font=30][,fType][,lineH][,fix],hide
|
||||
local _={
|
||||
name= D.name or"_",
|
||||
|
||||
@@ -1076,6 +1082,7 @@ function WIDGET.newTextBox(D)--name,x,y,w,h[,font=30][,lineH][,fix],hide
|
||||
h= D.h,
|
||||
|
||||
font= D.font or 30,
|
||||
fType=D.fType,
|
||||
fix= D.fix,
|
||||
texts={},
|
||||
hideF=D.hideF,
|
||||
|
||||
Reference in New Issue
Block a user