Compare commits

...

2 Commits

Author SHA1 Message Date
teverse 9e722ea708 Merge branch 'master' of https://github.com/teverse/teverse 2019-11-16 12:20:34 +00:00
teverse 13dd54e398 selection/hand improvements 2019-11-16 12:20:30 +00:00
2 changed files with 83 additions and 5 deletions

View File

@ -2,6 +2,15 @@
local controller = {}
local boundingBox = engine.construct("block", workspace, {
name = "_bounding",
wireframe = true,
static = true,
physics = false,
workshopLocked = true,
position = vector3(0, -100, 0)
})
controller.selection = {}
controller.callbacks = {}
@ -52,4 +61,63 @@ controller.hasSelection = function()
return #controller.selection > 0
end
local boundingEvents = {}
local function boundUpdate()
--inefficient, is called for each change
local bounds = aabb()
if #controller.selection > 0 then
bounds.min = controller.selection[1].position
bounds.max = controller.selection[1].position
end
for _,v in pairs(controller.selection) do
bounds:expand(v.position + (v.size/2))
bounds:expand(v.position - (v.size/2))
end
boundingBox.position = bounds:getCentre()
boundingBox.size = bounds.max - bounds.min
end
controller.registerCallback(function()
for _,v in pairs(boundingEvents) do
v:disconnect()
end
boundingEvents = {}
local bounds = aabb()
if #controller.selection > 0 then
bounds.min = controller.selection[1].position
bounds.max = controller.selection[1].position
end
for _,v in pairs(controller.selection) do
bounds:expand(v.position + (v.size/2))
bounds:expand(v.position - (v.size/2))
table.insert(boundingEvents, v:changed(boundUpdate))
end
boundingBox.position = bounds:getCentre()
boundingBox.size = bounds.max - bounds.min
end)
local keybinder = require("tevgit:workshop/controllers/core/keybinder.lua")
local history = require("tevgit:workshop/controllers/core/history.lua")
keybinder:bind({
name = "delete",
key = enums.key.delete,
action = function()
history.beginAction(controller.selection, "Delete")
for _,v in pairs(controller.selection) do
v:destroy()
end
history.endAction()
end
})
return controller

View File

@ -22,7 +22,7 @@ return {
if not inputObj.systemHandled then
-- This is not a gui event, let's continue.
local hit = engine.physics:rayTestScreen(engine.input.mousePosition)
if hit then
if hit and not hit.object.workshopLocked then
-- user has clicked a object in 3d space.
if selection.isSelected(hit.object) then
-- user clicked a selected object,
@ -30,9 +30,17 @@ return {
-- calculate the 'centre' of the selection
local bounds = aabb()
for _,v in pairs(selection.selection) do
bounds:expand(v.position)
if #selection.selection > 0 then
bounds.min = selection.selection[1].position
bounds.max = selection.selection[1].position
end
for _,v in pairs(selection.selection) do
bounds:expand(v.position + (v.size/2))
bounds:expand(v.position - (v.size/2))
end
local centre = bounds:getCentre()
-- calculate the mouse's offset from the centre
@ -58,11 +66,11 @@ return {
-- fire a ray, exclude selected items.
local hits, didExclude = engine.physics:rayTestScreenAllHits(engine.input.mousePosition, selection.selection)
if (#hits > 0) then
local newCentre = hits[1].hitPosition + mouseOffset
local newCentre = hits[1].hitPosition - mouseOffset
local avgPos = vector3(0,0,0)
for _,v in pairs(selection.selection) do
if offsets[v] then
v.position = newCentre - offsets[v]
v.position = newCentre + offsets[v]
avgPos = avgPos + v.position
end
end
@ -84,6 +92,8 @@ return {
selection.setSelection(hit.object)
end
end
else
selection.setSelection({})
end
end
end)