...because I don't understand if it's polling on every frame that's the issue, or doing it inside love.draw().
I mean, both! But here's what I was trying to get across: In my opinion, you had your priorities wrong when it comes to improving this code--while checking the window size isn't great, it's not the biggest or most urgent issue.
I think the biggest problem is that you've got input, layout, and rendering all mixed together in a single process when these should be very well separated in a retained-mode UI. The constant polling is also an issue (and RNavega's post covers very it well imo), but with a cleaner architecture it shouldn't be too hard to swap in more event-driven inputs later down the line so I consider it secondary to fixing the former.
I will also give you a word of warning: Most Love2D UI libraries suck. If you're looking for a reference to study, I recommend sticking to well-worn frameworks outside of the ecosystem (here again, RNavega's got a decent set). The code example in the README of the library you've linked is creating 13 tables per frame, for no reason, just to draw 5 ui elements. And then there's stuff like this, which needs no introduction:
Code: Select all
function UI.draw_element(e, x, y, flow_down)
local width, height
if e.type == "label" then
width, height = draw_label(e, x, y)
elseif e.type == "button" then
width, height = draw_button(e, x, y)
elseif e.type == "slider" then
width, height = draw_slider(e, x, y)
elseif e.type == "dropdown" then
width, height = draw_dropdown(e, x, y)
elseif e.type == "inputbox" then
width, height = draw_inputbox(e, x, y)
I could complain further but I think at some point it's more mean than constructive, this is more than enough to illustrate my point. If you're taking notes, just be sure you're taking them from the right place.
With all that said, I think it's also important to think about your needs and priorities when it comes to UI.
The reality is that if your UI needs are very basic, nothing that I just wrote above matters at all. You can basically do whatever dogshit approach and not only will it be fine, it'll probably be
ideal due to being more efficient in terms of
development resources, even if it's inefficient in terms of system resources. God knows I've got some questionable patterns in my own UI code, which I'll pull out eventually--but until those shortcomings are actually relevant to my project I have much more important things to spend my dev time on.
On the other hand, if you're specifically trying to learn the ins and outs of UI, or you have super complex UI needs? Yes, this may be something to spend extra time and energy on. It's all about figuring out where your priorities are.