local Button = {} Button.__index = Button --- Constructor --- @param x number --- @param y number --- @param width number --- @param height number --- @param text string --- @param color Color --- @param callback function | nil --- @return Button function Button.new(x, y, width, height, text, color, callback) local self = setmetatable({}, Button) self.x = x self.y = y self.width = width self.height = height self.text = text self.color = color self.callback = callback or function() end self.clicked = false return self end --- Update function --- @param engine Engine function Button:update(engine) local mouseX = engine:getMouseX() local mouseY = engine:getMouseY() local isHovered = mouseX >= self.x and mouseX <= (self.x + self.width) and mouseY >= self.y and mouseY <= (self.y + self.height) if isHovered and engine:isMouseLeftDown() then if not self.clicked then self.callback() self.clicked = true end else self.clicked = false end end --- Draw function --- @param engine Engine function Button:draw(engine) local mouseX = engine:getMouseX() local mouseY = engine:getMouseY() local isHovered = mouseX >= self.x and mouseX <= (self.x + self.width) and mouseY >= self.y and mouseY <= (self.y + self.height) local drawColor = isHovered and Color.new(self.color.r + 50, self.color.g + 50, self.color.b + 50) or self.color engine:setColor(drawColor) engine:fillRect(self.x, self.y, self.width, self.height) engine:setColor(Color.new(0, 0, 0)) -- Black text local charWidth = 8 local textWidth = #self.text * charWidth local textX = self.x + (self.width - textWidth) / 2 local textY = self.y + (self.height - 8) / 2 engine:drawText(self.text, textX, textY) end return Button