Some fixes

This commit is contained in:
Federico Cecchetto 2021-09-13 04:08:11 +02:00
parent 7eb372c539
commit bd3cdbb23d
3 changed files with 63 additions and 57 deletions

View File

@ -757,6 +757,7 @@ namespace ui_scripting::lua
{ {
this->state_.collect_garbage(); this->state_.collect_garbage();
this->scheduler_.clear(); this->scheduler_.clear();
this->event_handler_.clear();
this->state_ = {}; this->state_ = {};
} }

View File

@ -358,15 +358,15 @@ namespace ui_scripting::lua::engine
void start() void start()
{ {
clear_menus();
get_scripts().clear(); get_scripts().clear();
clear_menus();
load_scripts(); load_scripts();
} }
void stop() void stop()
{ {
clear_menus();
get_scripts().clear(); get_scripts().clear();
clear_menus();
} }
void ui_event(const std::string& type, const std::vector<int>& arguments) void ui_event(const std::string& type, const std::vector<int>& arguments)

View File

@ -1,86 +1,91 @@
function element:animate(state, animationtime) function element:animate(name, state, animationtime)
self:notify("cancel_animation") local start = {
x = self.x,
y = self.y,
w = self.w,
h = self.h,
color = self.color,
backcolor = self.backcolor,
bordercolor = self.bordercolor,
borderwidth = self.borderwidth,
fontsize = self.fontsize
}
local doanimation = function() local _end = {}
local start = { for k, v in pairs(start) do
x = self.x, _end[k] = state[k] or v
y = self.y, end
w = self.w,
h = self.h,
color = self.color,
backcolor = self.backcolor,
bordercolor = self.bordercolor,
borderwidth = self.borderwidth,
fontsize = self.fontsize
}
local _end = {}
for k, v in pairs(start) do
_end[k] = state[k] or v
end
local diffs = {} local diffs = {}
for k, v in pairs(_end) do for k, v in pairs(_end) do
if (type(v) == "table") then if (type(v) == "table") then
local value = {} local value = {}
local different = false
for _k, _v in pairs(v) do for _k, _v in pairs(v) do
value[_k] = _v - start[k][_k] value[_k] = _v - start[k][_k]
if (value[_k] ~= 0) then
different = true
end end
end
if (different) then
diffs[k] = value diffs[k] = value
else end
else
local value = v - start[k]
if (value ~= 0) then
diffs[k] = v - start[k] diffs[k] = v - start[k]
end end
end end
end
local timeout = nil
local interval = nil
local starttime = game:time()
interval = game:onframe(function() local timeout = nil
local time = game:time() local interval = nil
local percentage = (time - starttime) / animationtime local starttime = game:time()
if (percentage >= 1) then
for k, v in pairs(_end) do interval = game:onframe(function()
self[k] = v local time = game:time()
end local percentage = (time - starttime) / animationtime
return
if (percentage >= 1) then
for k, v in pairs(diffs) do
self[k] = _end[k]
end end
else
for k, v in pairs(diffs) do for k, v in pairs(diffs) do
if (type(v) == "table") then if (type(v) == "table") then
local value = {} local value = {}
for _k, _v in pairs(v) do for _k, _v in pairs(v) do
value[_k] = start[k][_k] + _v * percentage value[_k] = start[k][_k] + _v * percentage
end end
self[k] = value self[k] = value
else else
self[k] = start[k] + v * percentage self[k] = start[k] + v * percentage
end end
end end
end) end
end)
timeout = game:ontimeout(function() timeout = game:ontimeout(function()
interval:clear() interval:clear()
for k, v in pairs(_end) do for k, v in pairs(diffs) do
self[k] = v self[k] = _end[k]
end end
end, animationtime) end, animationtime)
self:onnotifyonce("cancel_animation", function() self:onnotifyonce("cancel_animation", function(_name)
if (name == _name) then
timeout:clear() timeout:clear()
interval:clear() interval:clear()
end) end
end end)
game:ontimeout(doanimation, 0)
end end
function element:cancelanimations(callback) function element:cancelanimations(name, callback)
self:notify("cancel_animation") self:notify("cancel_animation", name)
if (type(callback) == "function") then if (type(callback) == "function") then
game:ontimeout(callback, 0) game:ontimeout(callback, 0)
end end