complete rewrite
This commit is contained in:
125
src/main.lua
125
src/main.lua
@@ -1,24 +1,115 @@
|
||||
local Graphoon = require "lib.Graphoon"
|
||||
local screen_width, screen_height = love.graphics.getDimensions()
|
||||
-- print(screen_width, screen_height)
|
||||
local node_radius = 5
|
||||
local safe_zone = 15
|
||||
|
||||
local graph = Graphoon.Graph.new()
|
||||
-- programmatically determined later
|
||||
local translation = {0, 0} -- x, z
|
||||
|
||||
-- it just crashes :D
|
||||
graph:addNode("Name", 100, 100, true) -- magic numbers are a position and anchor to that position
|
||||
graph:addNode("Another")
|
||||
graph:connectIDs("Name", "Another")
|
||||
|
||||
|
||||
-- TODO load from JSON instead of defining here!
|
||||
|
||||
-- NODES
|
||||
-- name = {x, y, z, display_name}
|
||||
local nodes = {
|
||||
TestA = {100, 0, 100},
|
||||
TestB = {150, 0, 200},
|
||||
TestC = {300, 0, -200, true},
|
||||
TestE = {1200, 0, 200, "Displayed Name"},
|
||||
TestFarLeft = {-200, 0, 50},
|
||||
TestFarBottomRight = {1200, 0, 2000, true},
|
||||
|
||||
-- for testing automatic scaling/positioning
|
||||
TopLeft = {0, 0, 0},
|
||||
TopRight = {screen_width, 0, 0},
|
||||
BottomLeft = {0, 0, screen_height},
|
||||
BottomRight = {screen_width, 0, screen_height},
|
||||
}
|
||||
|
||||
-- EDGES ARE NON-DIRECTIONAL
|
||||
-- {name_a, name_b}
|
||||
local edges = {
|
||||
{"TestA", "TestB"},
|
||||
{"TestA", "TestC"},
|
||||
{"TestB", "TestC"},
|
||||
{"TestC", "TestE"},
|
||||
}
|
||||
|
||||
|
||||
|
||||
-- find map size
|
||||
local minimums = {math.huge, math.huge} -- x, z
|
||||
local maximums = {-math.huge, -math.huge} -- x, z
|
||||
for _, node in pairs(nodes) do
|
||||
if node[1] > maximums[1] then
|
||||
maximums[1] = node[1]
|
||||
end
|
||||
if node[1] < minimums[1] then
|
||||
minimums[1] = node[1]
|
||||
end
|
||||
if node[3] > maximums[2] then
|
||||
maximums[2] = node[3]
|
||||
end
|
||||
if node[3] < minimums[2] then
|
||||
minimums[2] = node[3]
|
||||
end
|
||||
end
|
||||
-- print(minimums[1], minimums[2], maximums[1], maximums[2])
|
||||
|
||||
-- find scale factor
|
||||
local distance_x = maximums[1] - minimums[1]
|
||||
local distance_z = maximums[2] - minimums[2]
|
||||
-- print(distance_x, distance_z)
|
||||
local scale = math.min((screen_width - safe_zone * 2) / distance_x, (screen_height - safe_zone * 2) / distance_z)
|
||||
local translation = {-minimums[1] + safe_zone / scale, -minimums[2] + safe_zone / scale}
|
||||
|
||||
|
||||
|
||||
local font = love.graphics.getFont()
|
||||
function love.draw()
|
||||
graph:draw(function(node)
|
||||
local x, y = node:getPosition()
|
||||
love.graphics.circle("fill", x, y, 5)
|
||||
end,
|
||||
function(edge)
|
||||
local origin_x, origin_y = edge.origin:getPosition()
|
||||
local target_x, target_y = edge.target:getPosition()
|
||||
love.graphics.line(origin_x, origin_y, target_x, target_y)
|
||||
end)
|
||||
love.graphics.scale(scale)
|
||||
love.graphics.translate(unpack(translation))
|
||||
|
||||
-- draw edges below nodes
|
||||
love.graphics.setColor(0.33, 0.33, 0.33, 1)
|
||||
for index, edge in ipairs(edges) do
|
||||
local node_a = nodes[edge[1]]
|
||||
local node_b = nodes[edge[2]]
|
||||
if (not node_a) or (not node_b) then
|
||||
error("Edge " .. index .. " names nonexistent node(s): " .. edge[1] .. "->" .. node_a .. ", " .. edge[2] .. "->" .. node_b)
|
||||
end
|
||||
love.graphics.line(node_a[1], node_a[3], node_b[1], node_b[3])
|
||||
end
|
||||
|
||||
-- draw nodes and label them
|
||||
love.graphics.setColor(1, 1, 1, 1)
|
||||
for name, node in pairs(nodes) do
|
||||
love.graphics.circle("fill", node[1], node[3], node_radius / scale)
|
||||
if node[4] then
|
||||
if type(node[4]) == "string" then
|
||||
name = node[4]
|
||||
end
|
||||
-- love.graphics.print(name, node[1] + node_radius / scale, node[3] + node_radius / scale, 0, 1 / scale, 1 / scale)
|
||||
local text_x, text_y = node[1] + node_radius / scale, node[3] + node_radius / scale
|
||||
|
||||
local difference = node[1] + font:getWidth(name) - maximums[1]
|
||||
if difference > 0 then
|
||||
text_x = node[1] - (node_radius + difference) / scale
|
||||
end
|
||||
|
||||
local difference = node[3] + font:getHeight() - maximums[2]
|
||||
if difference > 0 then
|
||||
text_y = node[3] - (node_radius + difference) / scale
|
||||
end
|
||||
|
||||
love.graphics.print(name, text_x, text_y, 0, 1 / scale, 1 / scale)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function love.update(dt)
|
||||
graph:update(dt)
|
||||
function love.keypressed(key)
|
||||
if key == "escape" then
|
||||
love.event.quit()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user