/*─────────────────────────────────────────────
リアルタイム追随・等面積グリッドシステム
(ドラッグ中も即座に反映)
─────────────────────────────────────────────*/
-- グローバル変数
global EA_GridPoints = #() -- ヘルパー格納用
global EA_Cols = 6 -- 列数
global EA_Rows = 5 -- 行数
global EA_IsUpdating = false -- 更新中フラグ
global EA_CallbackID = undefined -- コールバックID
/*------------------------------------------------
グリッド生成関数
------------------------------------------------*/
fn EA_CreateGrid startPos endPos =
(
-- 既存のヘルパーがあれば削除
for p in EA_GridPoints where isValidNode p do delete p
EA_GridPoints = #()
local xSpacing = (endPos.x - startPos.x) / (EA_Cols - 1)
local ySpacing = (endPos.y - startPos.y) / (EA_Rows - 1)
for r = 1 to EA_Rows do
(
for c = 1 to EA_Cols do
(
local pos = [ startPos.x + (c-1)*xSpacing,
startPos.y + (r-1)*ySpacing,
0 ]
local pt = (point pos:pos size:5 name:("EA_Pt_" + c as string + "_" + r as string))
append EA_GridPoints pt
)
)
)
/*------------------------------------------------
グリッド再レイアウト関数 (境界補間方式)
------------------------------------------------*/
fn EA_RelayoutGrid =
(
if EA_GridPoints.count == 0 or EA_IsUpdating then return()
EA_IsUpdating = true
-- 描画更新と編集を一時停止(無限ループ回避)
with redraw off suspendEditing()
(
-- newPositionsは二次元配列を[[...],[...],…]として各行ごとに格納する
local newPositions = for r = 1 to EA_Rows collect (for c = 1 to EA_Cols collect [0,0,0])
-- 行方向の補間
for r = 1 to EA_Rows do
(
-- 各行の左端と右端のヘルパーの位置
local leftPos = EA_GridPoints[(r-1)*EA_Cols + 1].pos
local rightPos = EA_GridPoints[(r-1)*EA_Cols + EA_Cols].pos
for c = 1 to EA_Cols do
(
local t = 0.0
if EA_Cols > 1 then t = (c-1) as float / (EA_Cols - 1)
local rowInterp = (1-t)*leftPos + t*rightPos
newPositions[r][c] = rowInterp
)
)
-- 列方向の補間、補間結果と先の行補間結果の平均値を算出
for c = 1 to EA_Cols do
(
local bottomPos = EA_GridPoints[c].pos
local topPos = EA_GridPoints[(EA_Rows-1)*EA_Cols + c].pos
for r = 1 to EA_Rows do
(
local t = 0.0
if EA_Rows > 1 then t = (r-1) as float / (EA_Rows - 1)
local colInterp = (1-t)*bottomPos + t*topPos
local avgPos = (newPositions[r][c] + colInterp) * 0.5
newPositions[r][c] = avgPos
)
)
-- 各グリッドヘルパーの位置を更新
for r = 1 to EA_Rows do
(
for c = 1 to EA_Cols do
(
local idx = (r-1)*EA_Cols + c
if isValidNode EA_GridPoints[idx] then
(
with animate off
EA_GridPoints[idx].pos = newPositions[r][c]
)
)
)
)
resumeEditing()
EA_IsUpdating = false
)
/*------------------------------------------------
ノードイベントコールバック関数
※ ドラッグ中もリアルタイムで反応
------------------------------------------------*/
fn EA_NodeEventCallback evType obj =
(
-- ドラッグ中も含めたノード移動イベント
if evType == #move then
(
-- 移動したノードがグリッドヘルパーかチェック
local isGridHelper = false
for pt in EA_GridPoints where pt == obj do
(
isGridHelper = true
exit
)
-- グリッドヘルパーなら再レイアウト
if isGridHelper and not EA_IsUpdating then
(
EA_RelayoutGrid()
)
)
-- イベントを継続
OK
)
/*------------------------------------------------
コールバック登録/解除関数
------------------------------------------------*/
fn EA_RegisterCallback =
(
-- 既存のコールバックを解除
if EA_CallbackID != undefined do
unRegisterTimeCallback EA_CallbackID
-- ノードイベントコールバックを登録
callbacks.removeScripts id:#EA_NodeCallback
callbacks.addScript #nodePostMove "EA_RelayoutGrid()" id:#EA_NodeCallback
-- ドラッグ中のリアルタイム更新用コールバック
EA_CallbackID = registerTimeCallback 0.05 EA_RelayoutGrid
)
fn EA_RemoveCallback =
(
-- コールバックを解除
callbacks.removeScripts id:#EA_NodeCallback
if EA_CallbackID != undefined do
unRegisterTimeCallback EA_CallbackID
EA_CallbackID = undefined
)
/*------------------------------------------------
メイン処理
------------------------------------------------*/
(
-- 初期グリッドの生成(左下:[0,0,0] ~ 右上:[500,400,0])
EA_CreateGrid [0,0,0] [500,400,0]
-- コールバック登録
EA_RegisterCallback()
format "リアルタイム追随・等面積グリッドシステムを生成しました。\nドラッグ中も内部ヘルパーがリアルタイムで追随します。\n"
)