Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
GigsD4X committed Apr 11, 2017
2 parents 5943db6 + 00801b9 commit bb445fe
Show file tree
Hide file tree
Showing 13 changed files with 3,077 additions and 2,956 deletions.
212 changes: 109 additions & 103 deletions BoundingBoxModule.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
-- Libraries
Core = require(script.Parent.Core);
Support = Core.Support;
local Core = require(script.Parent.Core);
local Support = Core.Support;

BoundingBoxModule = {};
-- Initialize module
local BoundingBoxModule = {};

-- Initialize internal module state
local StaticParts = {};
local StaticPartsIndex = {};
local StaticPartMonitors = {};
local RecalculateStaticExtents = true;
local AggregatingStaticParts = false;
local StaticPartAggregators = {};

function BoundingBoxModule.StartBoundingBox(HandleAttachmentCallback)
-- Creates and starts a selection bounding box
Expand Down Expand Up @@ -116,109 +125,104 @@ function BoundingBoxModule.ClearBoundingBox()

end;

StaticParts = {};
StaticPartMonitors = {};
RecalculateStaticExtents = true;
AggregatingStaticParts = false;
StaticPartAggregators = {};
function AddStaticParts(Parts)
-- Adds the static parts to the list for state tracking

function AddStaticPart(Part)
-- Adds the static part to the list for state tracking
-- Add each given part
for _, Part in pairs(Parts) do

-- Make sure the part isn't already in the list
if Support.IsInTable(StaticParts, Part) then
return;
end;
-- Ensure part isn't already indexed, and verify it is static
if not StaticPartsIndex[Part] and Part.Anchored then

-- Add part to static index
StaticPartsIndex[Part] = true;

-- Add the part to the list
table.insert(StaticParts, Part);
-- Start monitoring part for changes
StaticPartMonitors[Part] = Part.Changed:connect(function (Property)

-- Starts monitoring the part
StaticPartMonitors[Part] = Part.Changed:connect(function (Property)
-- Trigger static extent recalculations on position or size changes
if Property == 'CFrame' or Property == 'Size' then
RecalculateStaticExtents = true;

if Property == 'CFrame' or Property == 'Size' then
RecalculateStaticExtents = true;
-- Remove part from static index if it becomes mobile
elseif Property == 'Anchored' and not Part.Anchored then
RemoveStaticParts { Part };
end;

end);

elseif Property == 'Anchored' and not Part.Anchored then
RemoveStaticPart(Part);
end;

end);
end;

-- Update the static parts list
StaticParts = Support.Keys(StaticPartsIndex);

-- Recalculate the extents including this new part
-- Recalculate static extents to include added parts
RecalculateStaticExtents = true;

end;

function RemoveStaticPart(Part)
-- Removes the part from the static part tracking list
function RemoveStaticParts(Parts)
-- Removes the given parts from the static parts index

-- Get the part's key in the list
local PartKey = Support.FindTableOccurrence(StaticParts, Part);
-- Remove each given part
for _, Part in pairs(Parts) do

-- Remove it from the list
if PartKey then
StaticParts[PartKey] = nil;
end;
-- Remove part from static parts index
StaticPartsIndex[Part] = nil;

-- Clean up the part's change monitors
if StaticPartMonitors[Part] then
StaticPartMonitors[Part]:disconnect();
StaticPartMonitors[Part] = nil;
end;

-- Clear its state monitors
if StaticPartMonitors[Part] then
StaticPartMonitors[Part]:disconnect();
StaticPartMonitors[Part] = nil;
end;

-- Update the static parts list
StaticParts = Support.Keys(StaticPartsIndex);

-- Recalculate static extents to exclude removed parts
RecalculateStaticExtents = true;

end;

function StartAggregatingStaticParts()
-- Begins to look for and identify static parts

-- Add current static parts
for _, Part in pairs(Core.Selection.Items) do
if Part.Anchored then
AddStaticPart(Part);
end;
-- Add current qualifying parts to static parts index
AddStaticParts(Core.Selection.Items);

-- Watch for parts that become anchored
-- Watch for parts that become static
for _, Part in pairs(Core.Selection.Items) do
table.insert(StaticPartAggregators, Part.Changed:connect(function (Property)
if Property == 'Anchored' and Part.Anchored then
AddStaticPart(Part);
AddStaticParts { Part };
end;
end));
end;

-- Add newly selected anchored parts
-- Watch newly selected parts
table.insert(StaticPartAggregators, Core.Selection.ItemsAdded:connect(function (Parts)

-- Go through each selected part
for _, Part in pairs(Parts) do
-- Add qualifying parts to static parts index
AddStaticParts(Parts);

-- Only add anchored, static parts
if Part.Anchored then
AddStaticPart(Part);
end;

-- Watch for parts that become anchored
-- Watch for parts that become anchored
for _, Part in pairs(Parts) do
table.insert(StaticPartAggregators, Part.Changed:connect(function (Property)
if Property == 'Anchored' and Part.Anchored then
AddStaticPart(Part);
AddStaticParts { Part };
end;
end));

end;

end));

-- Remove deselected parts
-- Remove deselected parts from static parts index
table.insert(StaticPartAggregators, Core.Selection.ItemsRemoved:connect(function (Parts)

-- Remove the items
for _, Part in pairs(Parts) do
RemoveStaticPart(Part);
end;

-- Recalculate static extents without the removed parts
RecalculateStaticExtents = true;

RemoveStaticParts(Parts);
end));

end;
Expand All @@ -240,6 +244,7 @@ function StopAggregatingStaticParts()

-- Clear all static part information
StaticParts = {};
StaticPartsIndex = {};
BoundingBoxModule.StaticExtents = nil;

end;
Expand All @@ -250,6 +255,7 @@ local table_insert = table.insert;
local CFrame_toWorldSpace = CFrame.new().toWorldSpace;
local math_min = math.min;
local math_max = math.max;
local unpack = unpack;

function BoundingBoxModule.CalculateExtents(Items, StaticExtents, ExtentsOnly)
-- Returns the size and position of a box covering all items in `Items`
Expand Down Expand Up @@ -279,45 +285,45 @@ function BoundingBoxModule.CalculateExtents(Items, StaticExtents, ExtentsOnly)
local Corner;
local XPoints, YPoints, ZPoints = {}, {}, {};

Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(SizeX, SizeY, SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(-SizeX, SizeY, SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);

Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(SizeX, -SizeY, SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(SizeX, SizeY, -SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(-SizeX, SizeY, -SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(-SizeX, -SizeY, SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(SizeX, -SizeY, -SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = CFrame_toWorldSpace(PartCFrame, CFrame_new(-SizeX, -SizeY, -SizeZ));
table_insert(XPoints, Corner['x']);
table_insert(YPoints, Corner['y']);
table_insert(ZPoints, Corner['z']);
Corner = PartCFrame * CFrame_new(SizeX, SizeY, SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(-SizeX, SizeY, SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(SizeX, -SizeY, SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(SizeX, SizeY, -SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(-SizeX, SizeY, -SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(-SizeX, -SizeY, SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(SizeX, -SizeY, -SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

Corner = PartCFrame * CFrame_new(-SizeX, -SizeY, -SizeZ);
table_insert(XPoints, Corner.x);
table_insert(YPoints, Corner.y);
table_insert(ZPoints, Corner.z);

-- Reduce gathered points to min/max extents
MinX = math_min(MinX, unpack(XPoints));
Expand Down
13 changes: 11 additions & 2 deletions Core.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ function CloneSelection()
Unapply = function (HistoryRecord)
-- Reverts this change

-- Deselect the clones
Selection.Remove(HistoryRecord.Clones, false);

-- Remove the clones
SyncAPI:Invoke('Remove', HistoryRecord.Clones);

Expand Down Expand Up @@ -400,15 +403,21 @@ function DeleteSelection()
Apply = function (HistoryRecord)
-- Applies this change

-- Deselect the parts
Selection.Remove(HistoryRecord.Parts, false);

-- Remove the parts
SyncAPI:Invoke('Remove', HistoryRecord.Parts);

end;

};

-- Deselect parts before deleting
Selection.Remove(HistoryRecord.Parts, false);

-- Perform the removal
SyncAPI:Invoke('Remove', Selection.Items);
SyncAPI:Invoke('Remove', HistoryRecord.Parts);

-- Register the history record
History.Add(HistoryRecord);
Expand Down Expand Up @@ -458,7 +467,7 @@ function PrismSelect()
for _, Part in pairs(Selection.Items) do
local TouchingParts = Part:GetTouchingParts();
for _, TouchingPart in pairs(TouchingParts) do
if not Selection.Find(TouchingPart) then
if not Selection.IsSelected(TouchingPart) then
Parts[TouchingPart] = true;
end;
end;
Expand Down
Loading

0 comments on commit bb445fe

Please sign in to comment.