mirror of https://github.com/axmolengine/axmol.git
Merge pull request #9683 from dualface/v3_origin
add UIWidget support, fix typo
This commit is contained in:
commit
f673664851
|
@ -24,7 +24,7 @@ THE SOFTWARE.
|
|||
|
||||
local Layer = cc.Layer
|
||||
|
||||
function Layer:enableTouch(callback, isMultiTouches, swallowTouches)
|
||||
function Layer:onTouch(callback, isMultiTouches, swallowTouches)
|
||||
if type(isMultiTouches) ~= "boolean" then isMultiTouches = false end
|
||||
if type(swallowTouches) ~= "boolean" then swallowTouches = false end
|
||||
|
||||
|
@ -49,7 +49,7 @@ function Layer:enableTouch(callback, isMultiTouches, swallowTouches)
|
|||
return self
|
||||
end
|
||||
|
||||
function Layer:disableTouch()
|
||||
function Layer:removeTouch()
|
||||
self:unregisterScriptTouchHandler()
|
||||
self:setTouchEnabled(false)
|
||||
return self
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
--[[
|
||||
|
||||
Copyright (c) 2011-2014 chukong-inc.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
]]
|
||||
|
||||
local Widget = ccui.Widget
|
||||
|
||||
function Widget:onTouch(callback)
|
||||
self:addTouchEventListener(function(sender, state)
|
||||
local event = {x = 0, y = 0}
|
||||
if state == 0 then
|
||||
event.name = "began"
|
||||
elseif state == 1 then
|
||||
event.name = "moved"
|
||||
elseif state == 2 then
|
||||
event.name = "ended"
|
||||
else
|
||||
event.name = "cancelled"
|
||||
end
|
||||
event.target = sender
|
||||
callback(event)
|
||||
end)
|
||||
return self
|
||||
end
|
|
@ -44,6 +44,7 @@ require("cocos.framework.extends.NodeEx")
|
|||
require("cocos.framework.extends.SpriteEx")
|
||||
require("cocos.framework.extends.LayerEx")
|
||||
require("cocos.framework.extends.MenuEx")
|
||||
require("cocos.framework.extends.UIWidget")
|
||||
|
||||
require("cocos.framework.package_support")
|
||||
|
||||
|
|
|
@ -130,7 +130,7 @@ function GameView:onCreate()
|
|||
|
||||
-- add touch layer
|
||||
display.newLayer()
|
||||
:enableTouch(handler(self, self.onTouch))
|
||||
:onTouch(handler(self, self.onTouch))
|
||||
:addTo(self)
|
||||
|
||||
-- add background image
|
||||
|
|
|
@ -5,6 +5,18 @@ function ViewBase:ctor(app, name)
|
|||
self:enableNodeEvents()
|
||||
self.app_ = app
|
||||
self.name_ = name
|
||||
|
||||
-- check CSB resource file
|
||||
local res = rawget(self.class, "RESOURCE_FILENAME")
|
||||
if res then
|
||||
self:createResoueceNode(res)
|
||||
end
|
||||
|
||||
local binding = rawget(self.class, "RESOURCE_BINDING")
|
||||
if res and binding then
|
||||
self:createResoueceBinding(binding)
|
||||
end
|
||||
|
||||
if self.onCreate then self:onCreate() end
|
||||
end
|
||||
|
||||
|
@ -16,6 +28,35 @@ function ViewBase:getName()
|
|||
return self.name_
|
||||
end
|
||||
|
||||
function ViewBase:getResourceNode()
|
||||
return self.resourceNode_
|
||||
end
|
||||
|
||||
function ViewBase:createResoueceNode(resourceFilename)
|
||||
if self.resourceNode_ then
|
||||
self.resourceNode_:removeSelf()
|
||||
self.resourceNode_ = nil
|
||||
end
|
||||
self.resourceNode_ = cc.CSLoader:createNode(resourceFilename)
|
||||
assert(self.resourceNode_, string.format("ViewBase:createResoueceNode() - load resouce node from file \"%s\" failed", resourceFilename))
|
||||
self:addChild(self.resourceNode_)
|
||||
end
|
||||
|
||||
function ViewBase:createResoueceBinding(binding)
|
||||
assert(self.resourceNode_, "ViewBase:createResoueceBinding() - not load resource node")
|
||||
for nodeName, nodeBinding in pairs(binding) do
|
||||
local node = self.resourceNode_:getChildByName(nodeName)
|
||||
if nodeBinding.varname then
|
||||
self[nodeBinding.varname] = node
|
||||
end
|
||||
for _, event in ipairs(nodeBinding.events or {}) do
|
||||
if event.event == "touch" then
|
||||
node:onTouch(handler(self, self[event.method]))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function ViewBase:showWithScene(transition, time, more)
|
||||
self:setVisible(true)
|
||||
local scene = display.newScene(self.name_)
|
||||
|
|
Loading…
Reference in New Issue