-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgfx_util.lua
272 lines (231 loc) · 6.09 KB
/
gfx_util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
local gfx_util = {}
---@param text string
---@param x number
---@param baseline number
---@param limit number
---@param scale number
---@param ax string
function gfx_util.printBaseline(text, x, baseline, limit, scale, ax)
local font = love.graphics.getFont()
local y = baseline - font:getBaseline() * scale
local status, err = pcall(love.graphics.printf, text, x, y, limit, ax, 0, scale)
if not status then
love.graphics.printf(err, x, y, limit, ax, 0, scale)
end
end
---@param drawable any
---@param x number
---@param y number
---@param w number
---@param h number
---@param locate string
function gfx_util.drawFrame(drawable, x, y, w, h, locate)
local dw = drawable:getWidth()
local dh = drawable:getHeight()
local s = 1
local s1 = w / h <= dw / dh
local s2 = w / h >= dw / dh
if locate == "out" and s1 or locate == "in" and s2 then
s = h / dh
elseif locate == "out" and s2 or locate == "in" and s1 then
s = w / dw
end
love.graphics.draw(drawable, x + (w - dw * s) / 2, y + (h - dh * s) / 2, 0, s)
end
---@param text string
---@param x number
---@param y number
---@param w number
---@param h number
---@param ax string
---@param ay string
function gfx_util.printFrame(text, x, y, w, h, ax, ay)
if w < 0 then
x, w = w, -w
end
if h < 0 then
y, h = h, -h
end
local font = love.graphics.getFont()
local _, wrappedText = font:getWrap(text, w)
local height = font:getHeight() * font:getLineHeight() * #wrappedText
if ay == "center" then
y = y + (h - height) / 2
elseif ay == "bottom" then
y = y + h - height
end
love.graphics.printf(text, x, y, w, ax, 0)
end
-- https://love2d.org/wiki/Gradients
---@param dir string
---@param ... table
---@return love.Mesh
function gfx_util.newGradient(dir, ...)
local isHorizontal = true
if dir == "vertical" then
isHorizontal = false
elseif dir ~= "horizontal" then
error("bad argument #1 to 'gradient' (invalid value)", 2)
end
local colorLen = select("#", ...)
if colorLen < 2 then
error("color list is less than two", 2)
end
local meshData = {}
if isHorizontal then
for i = 1, colorLen do
local color = select(i, ...)
local x = (i - 1) / (colorLen - 1)
meshData[#meshData + 1] = {x, 1, x, 1, unpack(color)}
meshData[#meshData + 1] = {x, 0, x, 0, unpack(color)}
end
else
for i = 1, colorLen do
local color = select(i, ...)
local y = (i - 1) / (colorLen - 1)
meshData[#meshData + 1] = {1, y, 1, y, unpack(color)}
meshData[#meshData + 1] = {0, y, 0, y, unpack(color)}
end
end
return love.graphics.newMesh(meshData, "strip", "static")
end
---@param r number?
---@param g number?
---@param b number?
---@param a number?
---@return love.Image
function gfx_util.newPixel(r, g, b, a)
local imageData = love.image.newImageData(1, 1)
imageData:setPixel(0, 0, r or 1, g or 1, b or 1, a or 1)
return love.graphics.newImage(imageData)
end
local transform
local args = {}
---@param ... any
---@return love.Transform
function gfx_util.transform(...)
transform = transform or love.math.newTransform()
local t = ...
if type(t) ~= "table" then
return transform:setTransformation(...)
end
for i = 1, 9 do
local value = t[i]
if type(value) == "table" then
local w, h = love.graphics.getDimensions()
args[i] = value[1] * w + value[2] * h + (value[3] or 0)
elseif type(value) == "number" then
args[i] = value
elseif type(value) == "function" then
args[i] = value()
else
error("Invalid value: " .. i)
end
end
return transform:setTransformation(unpack(args))
end
local canvases = {}
---@param w number
---@param h number
---@return love.Canvas
local function newCanvas(w, h)
local _, _, flags = love.window.getMode()
return love.graphics.newCanvas(w, h, {msaa = flags.msaa})
end
---@param key any
---@return love.Canvas
function gfx_util.getCanvas(key)
local w, h = love.graphics.getDimensions()
if not canvases[key] then
canvases[key] = newCanvas(w, h)
return canvases[key]
end
local canvas = canvases[key]
if canvas:getWidth() ~= w or canvas:getHeight() ~= h then
canvas:release()
canvases[key] = newCanvas(w, h)
end
return canvases[key]
end
local colorShader1
function gfx_util.setInverseColorScale()
colorShader1 = colorShader1 or love.graphics.newShader([[
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
vec4 pixel = Texel(texture, texture_coords);
return color * (pixel - 1) + 1;
}
]])
love.graphics.setShader(colorShader1)
end
local colorShader2
---@param r number|table
---@param g number?
---@param b number?
---@param a number?
function gfx_util.setPixelColor(r, g, b, a)
if type(r) == "number" then
r = {r, g, b, a}
end
colorShader2 = colorShader2 or love.graphics.newShader([[
extern vec4 pixel;
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 screen_coords) {
return pixel;
}
]])
love.graphics.setShader(colorShader2)
colorShader2:send("pixel", r)
end
---@param offset number
---@param size number
---@param _w table
---@return table
---@return table
function gfx_util.layout(offset, size, _w)
local w = {}
local x = {}
local piexls = 0
for i = 1, #_w do
local v = _w[i]
if v ~= "*" and v > 0 then
piexls = piexls + _w[i]
end
end
local pos = offset
local fill = false
for i = 1, #_w do
local v = _w[i]
w[i] = v
if v == "*" then
w[i] = 0
assert(not fill)
fill = true
elseif v < 0 then
w[i] = -v * (size - piexls)
end
pos = pos + w[i]
end
local rest = size - pos
pos = offset
for i = 1, #_w do
if w[i] == 0 then
w[i] = rest
end
x[i] = pos
pos = pos + w[i]
end
return x, w
end
---@param imageData love.ImageData
---@return love.ImageData
function gfx_util.limitImageData(imageData)
local size = love.graphics.getSystemLimits().texturesize
local w, h = imageData:getDimensions()
if w <= size and h <= size then
return imageData
end
local _w, _h = math.min(w, size), math.min(h, size)
local _imageData = love.image.newImageData(_w, _h)
_imageData:paste(imageData, 0, 0, 0, 0, _w, _h)
return _imageData
end
return gfx_util