extensions/{graphics,ui}: Implement event handling class model
This commit is contained in:
parent
3ea0544172
commit
4dc1d10c82
@ -51,7 +51,36 @@ local function scene_entity_removed(scene, entity)
|
||||
-- TODO
|
||||
end
|
||||
|
||||
M.safe.EventHandler = polybox.wrap_class("EventHandler", {
|
||||
constructor = function()
|
||||
return EventHandler()
|
||||
end,
|
||||
})
|
||||
|
||||
M.safe.EventDispatcher = polybox.wrap_class("EventDispatcher", {
|
||||
inherited_from_by_wrapper = M.safe.EventHandler,
|
||||
constructor = function()
|
||||
return EventDispatcher()
|
||||
end,
|
||||
instance = {
|
||||
addEventListener = function(safe, listener_safe, callback, eventCode)
|
||||
local unsafe = polybox.check_type(safe, "EventDispatcher")
|
||||
local listener_unsafe = polybox.check_type(listener_safe, "EventHandler")
|
||||
polybox.check_type(callback, "function")
|
||||
polybox.check_type(eventCode, "number")
|
||||
local function callback_wrapper(listener_unsafe, event_unsafe)
|
||||
-- Ignore listener_unsafe and just pass listener_safe
|
||||
-- TODO: Security: event probably isn't safe
|
||||
log:verbose("EventDispatcher: event="..dump(event))
|
||||
callback(listener_safe, event)
|
||||
end
|
||||
unsafe:addEventListener(listener_unsafe, callback_wrapper, eventCode)
|
||||
end,
|
||||
},
|
||||
})
|
||||
|
||||
M.safe.Scene = polybox.wrap_class("Scene", {
|
||||
inherited_from_by_wrapper = M.safe.EventDispatcher,
|
||||
constructor = function(sceneType, virtualScene)
|
||||
polybox.check_enum(sceneType,
|
||||
{Scene.SCENE_3D, Scene.SCENE_2D, Scene.SCENE_2D_TOPLEFT})
|
||||
@ -94,6 +123,7 @@ M.safe.Scene = polybox.wrap_class("Scene", {
|
||||
})
|
||||
|
||||
M.safe.Entity = polybox.wrap_class("Entity", {
|
||||
inherited_from_by_wrapper = M.safe.EventDispatcher,
|
||||
constructor = function()
|
||||
return Entity()
|
||||
end,
|
||||
|
@ -7,6 +7,21 @@ local log = buildat.Logger("extension/ui")
|
||||
local dump = buildat.dump
|
||||
local M = {safe = {}}
|
||||
|
||||
M.safe.UIEvent = polybox.wrap_class("UIEvent", {
|
||||
constructor = function()
|
||||
return UIEvent()
|
||||
end,
|
||||
class = {
|
||||
CLICK_EVENT = UIEvent.CLICK_EVENT,
|
||||
CLOSE_EVENT = UIEvent.CLOSE_EVENT,
|
||||
OK_EVENT = UIEvent.OK_EVENT,
|
||||
CANCEL_EVENT = UIEvent.CANCEL_EVENT,
|
||||
CHANGE_EVENT = UIEvent.CHANGE_EVENT,
|
||||
YES_EVENT = UIEvent.YES_EVENT,
|
||||
NO_EVENT = UIEvent.NO_EVENT,
|
||||
},
|
||||
})
|
||||
|
||||
M.safe.UIElement = polybox.wrap_class("UIElement", {
|
||||
inherited_from_by_wrapper = graphics.safe.Entity,
|
||||
constructor = function()
|
||||
|
@ -6,10 +6,15 @@ local log = buildat.Logger("minigame")
|
||||
local dump = buildat.dump
|
||||
log:info("minigame/init.lua loaded")
|
||||
|
||||
local mouse_grabbed = false
|
||||
|
||||
local cereal = require("buildat/extension/cereal")
|
||||
local graphics = require("buildat/extension/graphics")
|
||||
local ui = require("buildat/extension/ui")
|
||||
local experimental = require("buildat/extension/experimental")
|
||||
local keyinput = require("buildat/extension/keyinput")
|
||||
local mouseinput = require("buildat/extension/mouseinput")
|
||||
local joyinput = require("buildat/extension/joyinput")
|
||||
|
||||
local scene = graphics.Scene(graphics.Scene.SCENE_3D)
|
||||
ground = graphics.ScenePrimitive(graphics.ScenePrimitive.TYPE_PLANE, 10,10)
|
||||
@ -43,15 +48,30 @@ function SomeUI:SomeUI()
|
||||
ui.UIElement.UIElement(self)
|
||||
self:Resize(100, 60)
|
||||
self:setPosition(640-100, 0)
|
||||
|
||||
self.button_grab_mouse = ui.UIButton("Mouse", 100, 30)
|
||||
self.button_grab_mouse:setPosition(0, 0)
|
||||
self:addChild(self.button_grab_mouse)
|
||||
|
||||
self.button_foo = ui.UIButton("Foo", 100, 30)
|
||||
self.button_foo:setPosition(0, 30)
|
||||
self:addChild(self.button_foo)
|
||||
end
|
||||
|
||||
function SomeUI:on_button(e)
|
||||
self.button_grab_mouse:addEventListener(self, function(self, e)
|
||||
log:info("SomeUI:on_button_grab_mouse()")
|
||||
if not mouse_grabbed then
|
||||
mouse_grabbed = true
|
||||
mouseinput.show_cursor(false)
|
||||
end
|
||||
end, ui.UIEvent.CLICK_EVENT)
|
||||
|
||||
self.button_foo:addEventListener(self, function(self, e)
|
||||
log:info("SomeUI:on_button_foo()")
|
||||
if not mouse_grabbed then
|
||||
mouse_grabbed = true
|
||||
mouseinput.show_cursor(false)
|
||||
end
|
||||
end, ui.UIEvent.CLICK_EVENT)
|
||||
end
|
||||
|
||||
scene2d.rootEntity.processInputEvents = true
|
||||
@ -146,11 +166,6 @@ buildat.sub_packet("minigame:update", function(data)
|
||||
end
|
||||
end)
|
||||
|
||||
local keyinput = require("buildat/extension/keyinput")
|
||||
local mouseinput = require("buildat/extension/mouseinput")
|
||||
local joyinput = require("buildat/extension/joyinput")
|
||||
local mouse_grabbed = false
|
||||
|
||||
keyinput.sub(function(key, state)
|
||||
if key == keyinput.KEY_LEFT then
|
||||
if state == "down" then
|
||||
@ -189,10 +204,6 @@ end)
|
||||
|
||||
mouseinput.sub_down(function(button, x, y)
|
||||
log:info("mouse down: "..button..", "..x..", "..y)
|
||||
if not mouse_grabbed then
|
||||
mouse_grabbed = true
|
||||
mouseinput.show_cursor(false)
|
||||
end
|
||||
end)
|
||||
|
||||
mouseinput.sub_move(function(x, y)
|
||||
|
Loading…
x
Reference in New Issue
Block a user