Hey guys, i’m fairly new here, and i made a button class, not to annoy you guys, but just to let you guys check it out, and maybe even getting some feedback? I love it when people tell me how i can do something better, so here’s my first codea class ever
Button = class() function Button:init(x, y, w, h) self.x = x self.y = y self.w = w self.h = h --button color self.clr = color(255, 255, 255, 255) self.clrpress = color(35, 88, 220, 255) --button sprite-icons self.img = '' self.imgp = self.img --button shape self.ellipse = false --text color self.txtclr = color(0, 0, 0, 255) self.txtclrpress = self.txtclr --text self.lbl = '' --text font self.font = "ArialMT" self.fontSize = 17 --border self.border = 2 self.borderc = color(0, 0, 0, 255) --function on press self.func = nil --button pressed? self.pressed = false --set 'temporary' colors self.clrc = self.clr self.txtclrc = self.txtclr self.spritec = self.img end function Button:updateVars() self.clrc = self.clr self.txtclrc = self.txtclr self.spritec = self.img end function Button:draw() fill(self.clrc) font(self.font) fontSize(self.fontSize) stroke(self.borderc) strokeWidth(self.border) if self.ellipse == false then rect(self.x, self.y, self.w, self.h) else ellipse(self.x, self.y, self.w, self.h) end fill(self.txtclrc) text(self.lbl, self.x, self.y) sprite(self.spritec, self.x, self.y) end function Button:touched(Touch) if (self.ellipse and math.pow(Touch.x - self.x, 2)/math.pow(self.w/2, 2) + math.pow(Touch.y - self.y, 2)/math.pow(self.h/2, 2) <= 1) or (self.ellipse == false and Touch.x >= self.x - self.w/2 and Touch.x <= self.x + self.w/2 and Touch.y >= self.y - self.h/2 and Touch.y <= self.y + self.h/2) then if Touch.state == BEGAN then self.pressed = 1 self.spritec = self.imgp self.clrc = self.clrpress self.txtclrc = self.txtclrpress end if Touch.state == ENDED and self.pressed == 1 then if self.func then _G[self.func]() end end end if Touch.state == ENDED then self.pressed = 0 self.spritec = self.img self.clrc = self.clr self.txtclrc = self.txtclr end end ``` and here's working example (Main class)-- Control -- Use this function to perform your initial setup function setup() a = 0 print("Welcome to Ultimate-Fail-Controls") ellipseMode(CENTER) rectMode(CENTER) but1 = Button(250, 50, 500, 75) but1.clrpress = color(36, 83, 243, 255) but1.clr = color(255, 255, 0, 255) but1.ellipse = true but1.lbl = 'THIS IS AN ELLIPSE SHAPED BUTTON' but1.img = "Cargo Bot:Claw Middle" but1.func = 'lol' but1:updateVars() but2 = Button(WIDTH/2, HEIGHT/2, 130, 35) but2.lbl = 'Another button' but2.txtclrpress = color(255, 255, 255, 255) end -- This function gets called once every frame function draw() background(255, 0, 0, 255) but1:draw() but2:draw() end function touched(t) but1:touched(t) but2:touched(t) end function lol() a = a + 1 print(a) end ``` thx in advance guys