local OFFSET_X = 20 local OFFSET_OUT_X = 37 local BaseLayer = require("luaScript.ModuleEapSdk.BaseLayers.BaseLayer") local LayerFloat = EapClass("LayerFloat", BaseLayer) function LayerFloat.createWithData(__node, __zorder) local layer = LayerFloat.new() layer.m_parent = __node __node:addChild(layer, __zorder) return layer end function LayerFloat:myShow() self:addListener(true) self:initUI() end function LayerFloat:myClose() end function LayerFloat:initUI() self.imgPath = { "res/ModuleEapSdk/eapsdk_wj_icon_float.png", "res/ModuleEapSdk/eapsdk_wj_icon_warn.png", } local fix_x = display.width local fix_y = display.cy self.imgFloat = G_EapAbbreviation.createImageView(self.imgPath[1], self, cc.p(fix_x-OFFSET_X, fix_y+200)) self.imgFloat:setScale(0.7) self.imgWarn = G_EapAbbreviation.createImageView(self.imgPath[2], self.imgFloat, cc.p(20, 80), false, 3, 1104, cc.p(1, 0.5)) self.imgWarn:setScale(0.9) self.m_FloatState = 1 --悬浮球位置 1:右,2:上 3:左 4:下 end --悬浮球伸缩 function LayerFloat:setFloatOut(__state) if __state then if self.m_FloatState == 1 then self.imgFloat:setPositionX(display.width-OFFSET_OUT_X) elseif self.m_FloatState == 2 then self.imgFloat:setPositionY(display.height-OFFSET_OUT_X) elseif self.m_FloatState == 3 then self.imgFloat:setPositionX(OFFSET_OUT_X) elseif self.m_FloatState == 4 then self.imgFloat:setPositionY(OFFSET_OUT_X) end else if self.m_FloatState == 1 then self.imgFloat:setPositionX(display.width-OFFSET_X) elseif self.m_FloatState == 2 then self.imgFloat:setPositionY(display.height-OFFSET_X) elseif self.m_FloatState == 3 then self.imgFloat:setPositionX(OFFSET_X + 40) elseif self.m_FloatState == 4 then self.imgFloat:setPositionY(OFFSET_X) end end end function LayerFloat:setWarnVisible(__state) self.imgWarn:setVisible(__state) end function LayerFloat:onTouchBegin(__touch) if G_LayerMain and G_LayerMain:isVisible() then self.beginPos = __touch:getLocation() -- local rectFloat = self.imgFloat:getBoundingBox() local img_pos = self.imgFloat:getPosition() local r_x = img_pos.x - 60 local r_y = img_pos.y - 60 local r_w = 120 local r_h = 120 local rectFloat = cc.rect(r_x, r_y, r_w, r_h) if cc.rectContainsPoint(rectFloat, self.beginPos) then self._listener:setSwallowTouches(true) return true end end end function LayerFloat:onTouchMove(__touch) local curPos = __touch:getLocation() self.imgFloat:setPosition(curPos) end function LayerFloat:onTouchEnd(__touch) local curPos = __touch:getLocation() local isClick = false if math.abs(curPos.x - self.beginPos.x) < 20 and math.abs(curPos.y - self.beginPos.y) < 20 then print("LayerFloat:onTouchEnd =========== ") isClick = true self:setWarnVisible(false) G_LayerMain:showBoard(true) G_EapSocketLogin.request_markLog(MARK_LOG10001) end local w_ratio = curPos.x/display.width if w_ratio > 0.5 then w_ratio = 1 - w_ratio end local h_ratio = curPos.y/display.height if h_ratio > 0.5 then h_ratio = 1 - h_ratio end local offset = OFFSET_X if self.imgWarn:isVisible() then offset = OFFSET_OUT_X end if w_ratio < h_ratio then if curPos.x < display.cx then self.imgFloat:setPositionX(offset + 40) self.m_FloatState = 3 else self.imgFloat:setPositionX(display.width-offset) self.m_FloatState = 1 end else if curPos.y < display.cy then self.imgFloat:setPositionY(offset) self.m_FloatState = 4 else self.imgFloat:setPositionY(display.height-offset) self.m_FloatState = 2 end end if isClick then self:setFloatOut(false) end self._listener:setSwallowTouches(false) end function LayerFloat:onTouchCancel(__touch) if self.isTouching then local curPos = __touch:getLocation() if math.abs(curPos.x - self.beginPos.x) < 20 and math.abs(curPos.y - self.beginPos.y) < 20 then self:setWarnVisible(false) G_LayerMain:showBoard(true) else local w_ratio = curPos.x/display.width if w_ratio > 0.5 then w_ratio = 1 - w_ratio end local h_ratio = curPos.y/display.height if h_ratio > 0.5 then h_ratio = 1 - h_ratio end if w_ratio < h_ratio then if curPos.x < display.cx then self.imgFloat:setPositionX(OFFSET_X) else self.imgFloat:setPositionX(display.width-OFFSET_X) end else if curPos.y < display.cy then self.imgFloat:setPositionY(OFFSET_X) else self.imgFloat:setPositionY(display.height-OFFSET_X) end end end end self._listener:setSwallowTouches(false) end return LayerFloat