Re: DoLayout event
Posted: Tue May 07, 2024 2:50 am
You can "break" out of a loop any time you want.
Searching for a layer will work much faster than creating a new one even if you have to iterate through all the layers in the project.MehdiZangenehBar wrote: ↑Tue May 07, 2024 1:53 amAre you sure it took that amount of memory? Any function to calculate that?
I'm not sure, Can you prove that?
I did that, almost nothing.
Code: Select all
-- **************************************************
-- General information about this script
-- **************************************************
ScriptName = "TestScript"
TestScript = {}
function TestScript:Name()
return 'Name'
end
function TestScript:Version()
return 'Version'
end
function TestScript:UILabel()
return 'UILabel'
end
function TestScript:Creator()
return 'Creator'
end
function TestScript:Description()
return 'Description'
end
-- **************************************************
-- Is Relevant / Is Enabled
-- **************************************************
function TestScript:IsRelevant(moho)
return true
end
function TestScript:IsEnabled(moho)
return true
end
-- **************************************************
-- Variables
-- **************************************************
local suspend_update_widgets = false
local suspend_do_layout = false
local last_layer_id = 0
local last_layer_type = 0
-- **************************************************
-- Functions
-- **************************************************
function ForceDoLayout()
m_u = collectgarbage("count")
suspend_update_widgets = true
suspend_do_layout = true
local helper = MOHO.ScriptInterfaceHelper:new_local()
local moho = helper:MohoObject()
local drawingToolsNonZero = MOHO.MohoGlobals.DisableDrawingToolsNonZero
if not drawingToolsNonZero then
MOHO.MohoGlobals.DisableDrawingToolsNonZero = true
end
local temp_layer = nil
if moho.layer:LayerType() == MOHO.LT_GROUP then temp_layer = moho:CreateNewLayer(MOHO.LT_UNKNOWN, false) end
local frame = moho.frame
if frame == 0 then
moho:SetCurFrame(1)
if temp_layer == nil then suspend_do_layout = false end
moho:SetCurFrame(0)
elseif frame ~= 0 then
moho:SetCurFrame(0)
if temp_layer == nil then suspend_do_layout = false end
moho:SetCurFrame(frame)
end
suspend_do_layout = false
if temp_layer ~= nil then moho:DeleteLayer(temp_layer) end
suspend_update_widgets = false
if not drawingToolsNonZero then
MOHO.MohoGlobals.DisableDrawingToolsNonZero = drawingToolsNonZero
end
helper:delete()
print('Memory usage: ' .. tostring(collectgarbage("count") - m_u) .. ' kb')
end
-- **************************************************
-- Events
-- **************************************************
function TestScript:OnMouseDown(moho, mouseEvent)
end
function TestScript:DoLayout(moho, layout)
if suspend_do_layout == true then return end
print('TestScript:DoLayout')
last_layer_id = moho.layer:UUID()
last_layer_type = moho.layer:LayerType()
end
function TestScript:UpdateWidgets(moho)
if suspend_update_widgets == true then return end
print('TestScript:UpdateWidgets')
local layer_id = moho.layer:UUID()
local layer_type = moho.layer:LayerType()
if last_layer_id ~= layer_id and last_layer_type == layer_type then
print('Layer with same type changed. -----------------------------------')
ForceDoLayout()
print('--------------------------------------------------------------------')
last_layer_id = layer_id
last_layer_type = layer_type
end
end