Ladder: Add editor handles to drag length

install-platforms
Sven Eberhardt 2017-05-22 12:15:45 -04:00
parent 233d7edba4
commit cb86124d78
4 changed files with 65 additions and 9 deletions

View File

@ -98,3 +98,5 @@ local ActMap = {
Procedure = DFA_ATTACH, Procedure = DFA_ATTACH,
}, },
}; };
local Plane = 220;

View File

@ -7,6 +7,7 @@
*/ */
local segments; local segments;
local SegmentHeight = 8;
func Initialize() func Initialize()
{ {
@ -14,16 +15,32 @@ func Initialize()
CreateSegments(); CreateSegments();
} }
public func SetLength(int segment_count) private func GetSegmentCount()
{ {
if (GetLength(segments) == segment_count) return; return GetLength(segments);
}
public func SetLength(int segment_count, int extension_direction)
{
// Adjust the length of the ladder
// segment_count: New number of segments
// extension_direction: Which end of the ladder to expand/shrink. 0: Top and bottom. +1: Bottom. -1: Top
var old_segment_count = GetSegmentCount();
if (old_segment_count == segment_count) return;
if (segment_count < 2) return; if (segment_count < 2) return;
for (var segment in segments) for (var segment in segments)
segment->RemoveObject(); segment->RemoveObject();
var new_height = segment_count * 8; var new_height = segment_count * SegmentHeight;
SetShape(-3, new_height / -2, 5, new_height); SetShape(-3, new_height / -2, 5, new_height);
if (extension_direction)
{
var height_diff = (segment_count - old_segment_count) * SegmentHeight;
MovePosition(0, height_diff/2 * extension_direction);
}
CreateSegments(); CreateSegments();
} }
@ -33,10 +50,10 @@ public func SetLength(int segment_count)
func CreateSegments() func CreateSegments()
{ {
segments = []; segments = [];
var nr_segments = (GetBottom() - GetTop()) / 8; var nr_segments = (GetBottom() - GetTop()) / SegmentHeight;
for (var index = 0; index < nr_segments; index++) for (var index = 0; index < nr_segments; index++)
{ {
var y = GetTop() + index * 8; var y = GetTop() + index * SegmentHeight;
var segment = CreateObject(MetalLadderSegment, 0, y+4); var segment = CreateObject(MetalLadderSegment, 0, y+4);
segment->SetMaster(this, index); segment->SetMaster(this, index);
// Store the segments. // Store the segments.
@ -79,6 +96,39 @@ public func SaveScenarioObject(props)
return true; return true;
} }
/* Editor dragging */
private func GetTopHandle() { return [0, GetSegmentCount() * SegmentHeight / -2]; }
private func SetTopHandle(pt) { SetLength(Max(2, (GetBottom() - pt[1] + SegmentHeight/2) / SegmentHeight), -1); }
private func GetBottomHandle() { return [0, GetSegmentCount() * SegmentHeight / 2]; }
private func SetBottomHandle(pt) { SetLength(Max(2, (pt[1] - GetTop() + SegmentHeight/2) / SegmentHeight), +1); }
public func Definition(def)
{
// Drag handles for ladder length
if (!def.EditorProps) def.EditorProps = {};
def.EditorProps.top_handle = {
Name="$TopHandle$",
Type="point",
Relative=true,
HorizontalFix=true,
AsyncGet="GetTopHandle",
Set="SetTopHandle",
Priority=1,
Color=0x80ff00 };
def.EditorProps.bottom_handle = {
Name="$BottomHandle$",
Type="point",
Relative=true,
HorizontalFix=true,
AsyncGet="GetBottomHandle",
Set="SetBottomHandle",
Color=0xff8000 };
}
/*-- Properties --*/ /*-- Properties --*/
local Name = "$Name$"; local Name = "$Name$";
local Plane = 221;

View File

@ -1 +1,3 @@
Name=Leiter Name=Leiter
TopHandle=Oberkante
BottomHandle=Unterkante

View File

@ -1 +1,3 @@
Name=Ladder Name=Ladder
TopHandle=Top end
BottomHandle=Bottom end