From bf3fbc24a50c2f4d732fd559f1a81523eeb066c6 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 5 Jan 2024 03:50:58 +0000 Subject: refactor, nicer errors --- main.lua | 48 +++++++++++++++++++++--------------------------- texts.lua | 35 +++++++++++++++++++++++++++++++++++ util.lua | 27 +++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 27 deletions(-) create mode 100644 texts.lua create mode 100644 util.lua diff --git a/main.lua b/main.lua index 6888be6..ff0d56d 100644 --- a/main.lua +++ b/main.lua @@ -1,20 +1,17 @@ local class = require 'class' local hsluv = require 'hsluv' local catenary = require 'catenary' +local util = require 'util' +local texts = require 'texts' local tau = 2*math.pi local ZOOM = 200 local G = love.graphics -local function mouse_pos() - -- in world space - return G.inverseTransformPoint(love.mouse.getPosition()) -end - local function hovered(set) -- returns which of the things in set is hovered over -- or nil if no such thing - local mx,my = mouse_pos() + local mx,my = util.mouse_pos() for s in pairs(set) do if s:contains(mx,my) then return s @@ -23,20 +20,6 @@ local function hovered(set) return nil end -local function write_at(text,x,y) - local tx,ty = G.transformPoint(x,y) - G.push() - G.origin() - G.print(text,tx,ty) - G.pop() -end - -local function phi_color(n) - local phi = (1+math.sqrt(5))/2 - local h = (360*phi*n)%360 - return hsluv.hsluv_to_rgb({h, 80, 60}) -end - local Port = class() Port.R = 0.1 function Port.make(cls, x,y, num) @@ -52,10 +35,12 @@ function Port.draw(self, istate) c[1] = 1 elseif istate == 'selected' then c[2] = 1 + elseif istate == 'editing' then + c[3] = 1 end love.graphics.setColor(c) G.circle('line',self.x,self.y,self.R) - write_at(self.n, self.x, self.y) + util.write_at(self.n, self.x, self.y) end function Port.contains(self, px,py) local d = math.sqrt((self.x - px)^2 + (self.y - py)^2) @@ -109,7 +94,9 @@ end function PortGraph.istate_of_port(self,port) if self.state == 'joining' and self.selected == port then return 'selected' - elseif port:contains(mouse_pos()) then + elseif self.state == 'editing' and self.selected == port then + return 'editing' + elseif port:contains(util.mouse_pos()) then return 'hover' else return 'normal' @@ -130,8 +117,8 @@ function PortGraph.draw(self) if self.state == 'joining' then local p = self.selected - G.setColor(phi_color(self.wire_n)) - local mx,my = mouse_pos() + G.setColor(util.phi_color(self.wire_n)) + local mx,my = util.mouse_pos() G.line(catenary.catenary(p.x,p.y,mx,my)) end @@ -159,7 +146,6 @@ function PortGraph.make_edit_handles(self) color = w.color, here = p, there = q, - w = w, pg = self, },EditHandle) hs[h] = true @@ -237,7 +223,7 @@ function PortGraph.add_conn(self, p1,p2) assert(not self:connection(p1,p2), string.format("%s and %s already connected!", p1,p2)) - local wire = Wire:make(p1, p2, phi_color(self.wire_n)) + local wire = Wire:make(p1, p2, util.phi_color(self.wire_n)) self.wires[wire] = true p1.conns[p2] = wire p2.conns[p1] = wire @@ -259,7 +245,10 @@ for i = 1,n do end function love.mousepressed(x,y,b) - pg:click(b) + local ok, err = pcall(pg.click, pg, b) + if not ok then + texts.add(err,util.mouse_pos()) + end end function love.draw() @@ -273,5 +262,10 @@ function love.draw() pg:draw() + texts.draw() + end +function love.update(dt) + texts.update(dt) +end diff --git a/texts.lua b/texts.lua new file mode 100644 index 0000000..9f9ff97 --- /dev/null +++ b/texts.lua @@ -0,0 +1,35 @@ +local util = require 'util' + +local texts = {} + +local function add(text,x,y) + table.insert(texts,{ + x=x,y=y,life=3,text=text + }) +end + +local function draw() + for i=#texts,1,-1 do + local t = texts[i] + util.write_at(t.text,t.x,t.y) + end +end + +local SPEED = 0.1 + +local function update(dt) + for i=#texts,1,-1 do + local t = texts[i] + t.y = t.y - SPEED * dt + t.life = t.life - dt + if t.life < 0 then + table.remove(texts, i) + end + end +end + +return { + add=add, + update=update, + draw=draw, +} diff --git a/util.lua b/util.lua new file mode 100644 index 0000000..6172d84 --- /dev/null +++ b/util.lua @@ -0,0 +1,27 @@ +local hsluv = require 'hsluv' + +local G = love.graphics +local function mouse_pos() + -- in world space + return G.inverseTransformPoint(love.mouse.getPosition()) +end + +local function write_at(text,x,y) + local tx,ty = G.transformPoint(x,y) + G.push() + G.origin() + G.print(text,tx,ty) + G.pop() +end + +local function phi_color(n) + local phi = (1+math.sqrt(5))/2 + local h = (360*phi*n)%360 + return hsluv.hsluv_to_rgb({h, 80, 60}) +end + +return { + mouse_pos = mouse_pos, + write_at = write_at, + phi_color = phi_color, +} -- cgit v1.2.3