2019-05-27

2019-05-27  本文已影响0人  Wintersnowcream

简单数字键盘封装:

--

-- Author: XJW

-- Date: 2018-08-22 15:39:29

--

local uihelper = require("lib/ui")

local gmui = require("gmui")

local uicreator = require("other/uicreator")

local win_size = cc.Director:getInstance():getWinSize()

local instance = instance or nil

local InputLayer = class("InputLayer",function  ()

    local self = require("ui/input_layer").create().root

    local t = tolua.getpeer(self)

    if not t then

        t = {}

        tolua.setpeer(self,t)

    end

    setmetatable(t,InputLayer)

    return self

end)

function InputLayer:ctor(...)

    self:__ondeclare_var(...)

    self:registerScriptHandler(handler(self, self.__eventbus))

    self:__oninit(...)

end

function InputLayer:__eventbus(e)

    if e == "enter" then

        self:__enter()

    elseif e == "exit" then

        self:__exit()

    end

end

function InputLayer:__ondeclare_var(...)

    --todo

    uihelper:addSwallowEventListener(self)

    gmui:addTransLayer(self)

end

function InputLayer:__oninit(...)

    --todo

    self:getChildByName("desc"):setString(self.__desc)

    self.__score_num = self:getChildByName("num"):setString("0")

    -- local copy_btn = self:getChildByName("copy_btn"):setVisible(self.__isCopy)

end

function InputLayer:__enter(...)

    --todo

    self:__render()

end

function InputLayer:__exit(...)

    --todo

    self:stopAllActions()

end

function InputLayer:__render()

    -- body

    local txt_label = self:getChildByName("txt_label")

    -- self.num_test = 0

    local seq = cc.Sequence:create(cc.CallFunc:create(function()

        -- body

        self.__clibordStr = You:getClipboard()

        -- self.__clibordStr = "              100"

        local limt_str = sg.String.limitString(self.__clibordStr, 8)

        if self.__clibordStr:len() > 8 then

            --todo

            local num  = tonumber(self.__clibordStr)

            if num then

                txt_label:setString(num.."")

            else

                txt_label:setString(limt_str.."...")

            end

            -- txt_label:setString(limt_str.."...")

        else

            local num  = tonumber(self.__clibordStr)

            if num then

                --todo

                txt_label:setString(num.."")

            else

              txt_label:setString(limt_str)

            end

            -- txt_label:setString(limt_str)

        end

        -- self.num_test = self.num_test + 1

        -- txt_label:setString(self.num_test)

    end),cc.DelayTime:create(3.0))

    if self.__isCopy == true then

        self:runAction(cc.RepeatForever:create(seq))   

    end

    for i=0,9 do

        local btn = self:getChildByName("btn_"..i)

        uihelper:setClickEvent3(btn, {

            endedEvent = function(sender)

                --todo

                local str = self.__score_num:getString()

                local num

                if string.find(str,"-") == 1 and str:len() == 1 then

                  self.__score_num:setString("0")

                  num = tonumber(self.__score_num:getString()) * (-10)

                  -- print("======="..num)

              else

                    num = tonumber(self.__score_num:getString()) * 10

                end

                -- local num = tonumber(self.__score_num:getString()) * 10

                if num < 0 then

                    num = num - i

                elseif num == 0 then

                    if string.find(str,"-") == 1 then

                        num = num - i

                    else

                        num = num + i

                    end

                else

                    num = num + i

                end

                local len = string.len(tostring(math.abs(num)))

                if len > self.__max then

                    --todo

                    local dialog = uicreator:createInfoDlg("最多" .. self.__max .. "位数~")

                    uihelper:addSwallowEventListener(dialog)

                    return

                end

                self.__score_num:setString(num.."")

            end})

    end

    local delete_btn = self:getChildByName("btn_delete")

    uihelper:setClickEvent3(delete_btn, {

        endedEvent = function(sender)

            --todo

            local str = self.__score_num:getString()

            local num = tonumber(self.__score_num:getString())

            local is_nagative = false

            local new_num

            if string.find(str,"-") == 1 then

                is_nagative = false

                new_num = (math.abs(num) - math.abs(num) % 10) / 10

            else

                new_num = (num - num % 10) / 10

                is_nagative = true

            end

            if is_nagative == true then

                self.__score_num:setString(""..new_num)

            else

                self.__isAddTouched = false

                if new_num == 0 then

                    --todo

                    self.__score_num:setString(""..new_num)

                else

                    self.__score_num:setString("-"..new_num)

                end

            end

        end})

    local btn_sure = self:getChildByName("btn_sure")

    uihelper:setClickEvent3(btn_sure, {

        endedEvent = function(sender)

            --todo

            if self.__sureBack then

                --todo

                local num = tonumber(self.__score_num:getString())

                if num then

                    --todo

                    self.__sureBack(num)

                else

                    self.__sureBack(0)

                end

                -- self.__sureBack(num)

                if self.__isQuit then

                    --todo

                    self:removeFromParent()

                    instance = nil

                end

            end

        end})

    local btn_add = self:getChildByName("add")

    btn_add:setEnabled(self.__isInputNegative)

    uihelper:setClickEvent3(btn_add, {

        endedEvent = function(sender)

            local str = self.__score_num:getString()

            local num = tonumber(self.__score_num:getString())

            if num then

                if self.__isAddTouched == false then

                    self.__isAddTouched = true

                    if num == 0 and str:len() == 1 then

                        self.__score_num:setString("-")

                    else

                        self.__score_num:setString("-"..math.abs(num))

                    end

                else

                    self.__isAddTouched = false

                    self.__score_num:setString(math.abs(num))

                end

            end

        end})

    local btn_close = self:getChildByName("btn_close")

    uihelper:setClickEvent3(btn_close, {

        endedEvent = function(sender)

            --todo

            if not tolua.isnull(instance) then

                --todo

                self:removeFromParent()

                instance = nil

            end

        end})

    local copy_btn = self:getChildByName("copy_btn")

    uihelper:setClickEvent3(copy_btn, {

        endedEvent = function(sender)

            --todo

            self:CopyStrLimit(self.__clibordStr)

        end})

    copy_btn:setVisible(self.__isCopy)

    self:getChildByName("copy_title_txt"):setVisible(self.__isCopy)

    self:getChildByName("txt_label"):setVisible(self.__isCopy)

    self:getChildByName("copy_bg"):setVisible(self.__isCopy)

end

--粘贴板复制内容的条件限制

function InputLayer:CopyStrLimit(c_str)

    -- body

    if c_str == "" then

        --todo

        local dialog = uicreator:createInfoDlg("粘贴内容不能为空")

        uihelper:addSwallowEventListener(dialog)

    else

        c_str = tonumber(c_str)

        if not c_str then

            local dialog = uicreator:createInfoDlg("粘贴内容不合法")

            uihelper:addSwallowEventListener(dialog)

        else

            local len = tostring(c_str):len()

            if c_str < 0 then

                --todo

                len = len - 1

            end

            if len > self.__max then

                local dialog = uicreator:createInfoDlg("粘贴内容不合法")

                uihelper:addSwallowEventListener(dialog)

            else

                self.__score_num:setString(c_str.."")

            end

        end

    end

end

function InputLayer:ShowInputLayer(sureBack, max, desc,isInputNegative,isCopy,isQuit)

    -- body

    self.__sureBack = sureBack

    self.__max = max or 12

    self.__desc = desc or ""

    self.__isQuit = true

    self.__isInputNegative = isInputNegative or false

    self.__isAddTouched = false

    self.__isCopy = isCopy or false

    self.__clibordStr = self.__clibordStr or ""

    if tolua.isnull(instance) then

        --todo

        instance = InputLayer.new()

    end

    cc.Director:getInstance():getRunningScene():addChild(instance)

end

return InputLayer

上一篇下一篇

猜你喜欢

热点阅读