fork/m-overlay

git clone https://git.julia.blue/fork/m-overlay

blackops7799I lost track of what I'm even doing lola7d6bcc

GUI
2.7 KiB113 linesraw
1local graphics = love.graphics
2
3local setColor = graphics.setColor
4
5-- Love2D changed color values to be 0-1
6-- Allow 0-255 values again..
7function graphics.setColor(r,g,b,a)
8	if type(r) == "table" then
9		r, g, b, a = r.r, r.g, r.b, r.a
10	else
11		r, g, b, a = r or 255, g or 255, b or 255, a or 255
12	end
13	setColor(r/255,g/255,b/255,a/255)
14end
15
16-- Draw an image using width and height in pixels
17-- OriginX and OriginY should be between 0 and 1
18-- An origin of 0.5, 0.5 would be the center
19function graphics.easyDraw(obj, x, y, rotation, width, height, originX, originY, ...)
20	if not obj then return end
21
22	local objW, objH = obj:getWidth(), obj:getHeight()
23
24	rotation = rotation or 0
25	width = width or objW
26	height = height or objH
27	originX = originX or 0
28	originY = originY or 0
29
30	local scaledW = width / objW
31	local scaledH = height / objH
32	graphics.draw(obj, x, y, rotation, scaledW, scaledH, objW * originX, objH * originY, ...)
33end
34
35--applies a transformation that maps 
36--  0,0 => ox, oy
37--  1,0 => xx, xy
38--  0,1 => yx, yy
39-- via love.graphics.translate, .rotate and .scale
40
41local sqrt = math.sqrt
42local atan2 = math.atan2
43local acos = math.acos
44local tan = math.tan
45local cos = math.cos
46local sin = math.sin
47
48function graphics.transform(ox, oy, xx, xy, yx, yy)
49
50	local ex, ey, fx,fy = xx-ox, xy-oy, yx-ox, yy-oy
51	if ex*fy<ey*fx then ex,ey,fx,fy=fx,fy,ex,ey end
52	local e,f = math.sqrt(ex*ex+ey*ey), math.sqrt(fx*fx+fy*fy)
53
54	ex,ey = ex/e, ey/e
55	fx,fy = fx/f, fy/f
56
57	local desiredOrientation=math.atan2(ey+fy,ex+fx)
58	local desiredAngle=math.acos(ex*fx+ey*fy)/2
59	local z=math.tan(desiredAngle)
60	local distortion=math.sqrt((1+z*z)/2)
61
62	graphics.translate(ox, oy)
63	graphics.rotate(desiredOrientation)
64	graphics.scale(1, z)
65	graphics.rotate(-math.pi/4)
66	graphics.scale(e/distortion,f/distortion)
67
68end
69
70function graphics.innerRectangle(x, y, w, h, ...)
71	local lw = graphics.getLineWidth()
72
73	x = x + math.ceil(lw/2)
74	y = y + math.floor(lw/2)
75	w = w - lw
76	h = h - lw
77
78	graphics.rectangle("line", x, y, w, h, ...)
79end
80
81-- Draw a rectangle to fit AROUND the width and height
82function graphics.outlineRectangle(x, y, w, h, ...)
83	local lw = graphics.getLineWidth()
84
85	x = x + math.ceil(lw/2)	 - lw
86	y = y + math.floor(lw/2) - lw
87	w = w + lw * 2
88	h = h + lw * 2
89
90	graphics.rectangle("line", x, y, w, h, ...)
91end
92
93function graphics.textOutlinef(text, width, x, y, ...)
94	local steps = width * 2 / 3
95	if steps < 1 then steps = 1 end
96
97	for _x = -width, width, steps do
98		for _y = -width, width, steps do
99			graphics.printf(text, x + _x, y + _y, ...)
100		end
101	end
102end
103
104function graphics.textOutline(text, width, x, y, ...)
105	local steps = width * 2 / 3
106	if steps < 1 then steps = 1 end
107
108	for _x = -width, width, steps do
109		for _y = -width, width, steps do
110			graphics.print(text, x + _x, y + _y, ...)
111		end
112	end
113end