Today I faced with impossibility to remove listener on window events. Defold API has only window.set_listener
method for add callback and has no method to remove it.
I made the library for simple adding and removing listeners to any script with message receiver (with on_message method) for the window events:
- window.WINDOW_EVENT_FOCUS_LOST
- window.WINDOW_EVENT_FOCUS_GAINED
- window.WINDOW_EVENT_RESIZED
https://github.com/AGulev/ag_window
function init(self)
...
ag_window.add_focus_listener(msg.url()) -- for the lost and gained events
ag_window.add_resize_listener(msg.url()) -- for the resize event
...
end
function on_message(self, message_id, message, sender)
...
if message_id == ag_window.FOCUS_LOST then
print("focus lost")
elseif message_id == ag_window.FOCUS_GAINED then
print("focus gained")
elseif message_id == ag_window.RESIZED then
print("Resized", message.width, message.height)
end
...
end
function final(self)
...
ag_window.remove_focus_listener(msg.url())
ag_window.remove_resize_listener(msg.url())
...
end
With example project, of course.