Last active
May 1, 2026 16:15
-
-
Save leeirvinekezzico/95f1c20e0693d907aebfb36062e20bfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Text(text, style) | |
| return { | |
| state = { | |
| text = text, | |
| color = style.color or hexToColor(0xFFFFFF), | |
| -- make the font really big for best rendering | |
| font = style.font or love.graphics.newFont("assets/fonts/joystix.ttf", 120), | |
| size = style.size or 30.0, | |
| }, | |
| draw = function(self, w, h) | |
| local font_scale = (self.state.size or 30.0) / 60.0 | |
| local maxWidth, wrappedtext = self.state.font:getWrap( self.state.text, w / font_scale ) | |
| local textWidth = maxWidth | |
| local textHeight = self.state.font:getHeight(self.state.text) * #wrappedtext | |
| local align = style.align or "center" -- top, center, bottom | |
| local justify = style.justify or "center" -- left, center, right | |
| love.graphics.push("all") | |
| if DEBUG then | |
| love.graphics.setColor(0, 1, 0, 0.5) | |
| love.graphics.rectangle("fill", 0, 0, w, h) | |
| end | |
| -- using a large font and scaling it down to get better visual quality | |
| love.graphics.scale(font_scale, font_scale) | |
| love.graphics.setColor(self.state.color) | |
| love.graphics.setFont(self.state.font) | |
| if align == "center" then | |
| love.graphics.translate(0, -textHeight * 0.5 + h / font_scale * 0.5) | |
| elseif align == "bottom" then | |
| love.graphics.translate(0, h / font_scale - textHeight) | |
| end | |
| -- Text limit is used to wrap text. (argument 4 of love.graphics.printf) | |
| -- Text limit will be the width (w) of the container. | |
| -- setting limit to w / font_scale because we are scaling the text up by font_scale above, | |
| -- so we need to inverse scale the limit by the same amount. | |
| love.graphics.printf(self.state.text, 0, 0, w / font_scale, justify) | |
| love.graphics.pop() | |
| end, | |
| } | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment