summaryrefslogtreecommitdiff
path: root/main.lua
blob: fa41c006eaa76057623564e1c35803cd7fb8d3e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
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 hovered(set)
	-- returns which of the things in set is hovered over
	-- or nil if no such thing
	local mx,my = util.mouse_pos()
	for s in pairs(set) do
		if s:contains(mx,my) then
			return s
		end
	end
	return nil
end

local Port = class()
Port.R = 0.1
function Port.make(cls, x,y, num)
	return setmetatable({
		x=x, y=y, n=num,
		-- map of
		conns={},
	},cls)
end
function Port.draw(self, istate)
	local c = {0,0,0}
	if istate == 'hover' then
		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)
	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)
	return d <= self.R
end
function Port.__tostring(self)
	return 'p#'..self.n
end

local Wire = class()
function Wire.make(cls, p1,p2, color)
	return setmetatable({
		color = color,
		p1 = p1, p2 = p2,
		curve = catenary.catenary(p1.x,p1.y,p2.x,p2.y),
	}, cls)
end
function Wire.draw(self, istate)
	G.setColor(self.color)
	G.line(self.curve)
end

local EditHandle = class()
EditHandle.R = 0.04
EditHandle.D = 0.2
function EditHandle.make(cls, theta,p,q,w,pg)
	local self = setmetatable({
		x = cls.D * math.cos(theta) + p.x,
		y = cls.D * math.sin(theta) + p.y,
		color = w.color,
		here = p,
		there = q,
		pg = pg,
	},cls)
	self.curve = catenary.catenary(
		self.x,self.y, self.there.x,self.there.y)
	return self
end
function EditHandle.draw(self)
	G.setColor(self.color)
	G.circle('fill',self.x,self.y,self.R)
	G.line(self.curve)
end
function EditHandle.contains(self, px,py)
	local d = math.sqrt((self.x - px)^2 + (self.y - py)^2)
	return d <= self.R
end
function EditHandle.remove_wire(self)
	self.pg:remove_conn(self.here,self.there)
end

local PortGraph = class()
function PortGraph.make(cls)
	return setmetatable({
		-- set of ports, set of wires
		ports={}, wires={},
		state='normal', selected=nil,
		wire_n = 1,
	},cls)
end
function PortGraph.add(self,...)
	local p = Port:make(...)
	self.ports[p] = true
end
function PortGraph.istate_of_port(self,port)
	if self.state == 'joining' and self.selected == port then
		return 'selected'
	elseif self.state == 'editing' and self.selected == port then
		return 'editing'
	elseif port:contains(util.mouse_pos()) then
		return 'hover'
	else
		return 'normal'
	end
end
function PortGraph.istate_of_wire(self, wire)
	return 'normal'
end
function PortGraph.draw(self)
	for p in pairs(self.ports) do
		local istate = self:istate_of_port(p)
		p:draw(istate)
	end
	for w in pairs(self.wires) do
		if self.state == 'editing' then
			if self.selected == w.p1 or self.selected == w.p2 then
				goto next
			end
		end
		local istate = self:istate_of_wire(w)
		w:draw(istate)
		::next::
	end

	if self.state == 'joining' then
		local p = self.selected
		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

	if self.state == 'editing' then
		for h in pairs(self.handles) do
			h:draw()
		end
	end
end

function PortGraph.make_edit_handles(self)
	local p = self.selected
	local n = 0
	for _ in pairs(p.conns) do n = n + 1 end
	local hs = {}
	local D = 0.2

	local i = 0
	for q,w in pairs(p.conns) do
		local theta = tau * i/n
		i = i + 1
		local h = EditHandle:make(theta,p,q,w,self)

		hs[h] = true
	end

	return hs
end

function PortGraph.click(self,button)
	if self.state == 'normal' then
		local p = hovered(self.ports)
		if p then
			if button == 1 then
				self.state = 'joining'
				self.selected = p
			elseif button == 2 then
				self.selected = p
				self.handles = self:make_edit_handles()
				self.state = 'editing'
			end
		end
	elseif self.state == 'joining' then
		if button == 2 then
			self.state = 'normal'
			self.selected = nil
		elseif button == 1 then
			local p = hovered(self.ports)
			if p then
				self:add_conn(self.selected,p)
				self.state = 'normal'
				self.selected = nil
			end
		end
	elseif self.state == 'editing' then
		-- rmb on other port -> edit on that port instead
		-- rmb on wire blob -> delete that wire, stay in edit mode
		-- lmb on wire blob -> delete that wire, go to joining mode on connected port
		-- anything anywhere else -> back to normal mode
		local p = hovered(self.ports)
		local h = hovered(self.handles)
		if button == 2 and p then
			self.selected = p
			self.handles = self:make_edit_handles()
		elseif button == 2 and h then
			h:remove_wire()
			self.handles = self:make_edit_handles()
		elseif button == 1 and h then
			self.selected = h.there
			h:remove_wire()
			self.handles = nil
			self.state = 'joining'
		else
			self.state = 'normal'
			self.selected = nil
			self.handles = nil
		end

	end
end
function PortGraph.connection(self, p1,p2)
	-- returns the wire connecting p1 and p2, if it exists (truthy)
	-- or false otherwise
	local w1 = p1.conns[p2]
	local w2 = p2.conns[p1]
	-- better safe than sorry
	assert(w1==w2, "wire bidirectionality inconsistency")
	if w1 then
		assert((w1.p1 == p1 and w1.p2 == p2)
			or (w1.p1 == p2 and w1.p2 == p1),
		"wire backlink inconsistency")
	end
	return w1
end
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, util.phi_color(self.wire_n))
	self.wires[wire] = true
	p1.conns[p2] = wire
	p2.conns[p1] = wire
	self.wire_n = self.wire_n + 1
end
function PortGraph.remove_conn(self,p1,p2)
	local wire = self:connection(p1,p2)
	assert(wire,string.format("%s and %s not connected!", p1,p2))
	self.wires[wire] = nil
	p1.conns[p2] = nil
	p2.conns[p1] = nil
end

local pg = PortGraph:make()
local n = 10
for i = 1,n do
	local theta = tau * i/n
	pg:add(math.cos(theta), math.sin(theta), i)
end

function love.mousepressed(x,y,b)
	local ok, err = pcall(pg.click, pg, b)
	if not ok then
		texts.add(err,util.mouse_pos())
	end
end

function love.draw()
	local W,H = G.getDimensions()
	G.clear(1,1,1)
	G.setColor(0,0,0)
	G.origin()
	G.setLineWidth(0.01)
	G.translate(W/2,H/2)
	G.scale(ZOOM)

	pg:draw()

	texts.draw()

end

function love.update(dt)
	texts.update(dt)
end