local Pos = require'r.pos' local Rect = require'r.rect' local class = require'r.class' local G = love.graphics local utf8 = require'utf8' local logo = G.newImage"logo.png" logo:setFilter'nearest' local function c(...) return {love.math.colorFromBytes(...)} end -- taken from breadquest local colors = { c(255,64,64), c(255,128,0), c(192,192,64), c(0,192,0), c(0,192,192), c(64,64,255), c(192,0,192), c(128,128,128)} local state = {color=1,focused=nil,name='Susan'} local LogoImage = {} function LogoImage.draw() G.setColor(1,1,1) G.draw(logo, 120, 50, 0, 12) end local ColorButton = class() function ColorButton.make(cls, idx) local size, gap = 40, 10 local tl = Pos(100,240) + (idx-1)*Pos(size+gap,0) local bb = Rect:from_pos(tl,tl+Pos(size,size)) return setmetatable({bb=bb,idx=idx},cls) end function ColorButton.draw(self) G.setColor(colors[self.idx]) self.bb:draw'fill' if self.idx == state.color then G.setColor(0,0,0) local d=5 G.line(self.bb.x0+d,self.bb.y0+d,self.bb.x1-d,self.bb.y1-d) G.line(self.bb.x0+d,self.bb.y1-d,self.bb.x1-d,self.bb.y0+d) end end function ColorButton.click(self) state.color = self.idx end local font = G.newFont(24) local NameField = class() function NameField.make(cls,org) local self = setmetatable({org=org},cls) self:setbb() return self end function NameField.setbb(self) local width = 12+math.max(200,font:getWidth(state.name)) self.bb=Rect:from_xywh(self.org.x,self.org.y,width,font:getHeight()) end function NameField.draw(self) G.setColor(0,0,0) self.bb:draw'line' local d=3 G.setFont(font) G.print(state.name,self.bb.x0+d,self.bb.y0) if self == state.focused then local w = 2*d+self.bb.x0+font:getWidth(state.name) G.line(w,self.bb.y0+d,w,self.bb.y1-d) end end function NameField.input(self,text) state.name = state.name .. text self:setbb() end function NameField.key(self,key) if key == 'backspace' and #state.name > 0 then state.name = state.name:sub(1,utf8.offset(state.name,-1)-1) self:setbb() end end local things = { NameField(Pos(100,400)), LogoImage } for i=1,#colors do table.insert(things, ColorButton(i)) end local function draw_ui() for _,thing in ipairs(things) do if thing.draw then thing:draw() end if thing.bb and thing.bb:has(Pos(love.mouse.getPosition())) then G.setColor(0,0,0) thing.bb:draw'line' end end end local function which(p) for _,thing in ipairs(things) do if thing.bb and thing.bb:has(p) then return thing end end end function love.draw() G.clear(1,1,1) draw_ui() end function love.mousepressed(x,y,button) local thing = which(Pos(x,y)) if thing then state.focused = thing if thing.click then thing:click() end end end love.keyboard.setKeyRepeat(true) function love.textinput(text) if state.focused and state.focused.input then state.focused:input(text) end end function love.keypressed(key) if state.focused and state.focused.key then state.focused:key(key) end end