hengyang_client/lua_probject/base_project/Game/View/ViewUtil.lua

497 lines
14 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

ViewUtil = {
Num_Converted = { "", "", "", "" },
Sex_Chat = {
"man",
"woman"
},
Fix_Msg_Chat = {
"快点啊我等的花都谢了!",
"怎么又断线了,网络这么差!",
"不要走决战到天亮!",
"哎,各位,不好意思啊,我得离开一会儿啊",
"不要吵了,不要吵了,吵撒嘛吵,专心玩游戏嘛",
"你的牌打的也太好咧",
"和你合作真是太愉快了"
}
}
function ViewUtil.GetPos(self_seat, seat, people_num)
if self_seat == 0 then return seat end
local cur_seat = self_seat
local tem = seat - (cur_seat - 1)
if (tem <= 0) then tem = tem + people_num end
return tem
end
-- 隐藏手机号
function ViewUtil.phone_hide(phone)
return string.sub(phone, 1, 3) .. "****" .. string.sub(phone, 8)
end
-- 隐藏身份证
function ViewUtil.identity_hide(identity)
return string.sub(identity, 1, 8) .. "****" .. string.sub(identity, 13)
end
local modal_wait_win = nil
-- local modal_wait_win_url = "ui://Common/GlobalModalWaiting"
function ViewUtil.ShowModalWait(blur_view, title, time)
ViewUtil.CloseModalWait2()
ViewUtil.continue = coroutine.start(
function()
coroutine.wait(time or 1)
ModalWaitingWindow.ShowModal(title)
end
)
end
function ViewUtil.CloseModalWait()
if ViewUtil.continue then
coroutine.stop(ViewUtil.continue)
ViewUtil.continue = nil
end
ModalWaitingWindow.CloseModal()
end
function ViewUtil.ShowModalWait2(blur_view, time)
ViewUtil.CloseModalWait2()
ViewUtil.continue = coroutine.start(
function()
coroutine.wait(tonumber(time) or 0.01)
ModalWaitingWindow2.ShowModal()
end
)
ViewUtil.autoCloseContinue = coroutine.start(
function()
coroutine.wait(5)
ModalWaitingWindow2.CloseModal()
end
)
end
function ViewUtil.CloseModalWait2()
if ViewUtil.continue then
coroutine.stop(ViewUtil.continue)
ViewUtil.continue = nil
end
if ViewUtil.autoCloseContinue then
coroutine.stop(ViewUtil.autoCloseContinue)
ViewUtil.autoCloseContinue = nil
end
ModalWaitingWindow2.CloseModal()
end
local _defaultTooltipWin = nil
local function __waittips(obj)
_defaultTooltipWin:Dispose()
_defaultTooltipWin = nil
end
local _tip_timer
local function __ShowTips(msg, time)
time = time or 3
if _defaultTooltipWin then
_tip_timer:Stop()
_defaultTooltipWin:Dispose()
_defaultTooltipWin = nil
end
_defaultTooltipWin = UIPackage.CreateObjectFromURL("ui://Common/ToolsTip")
_defaultTooltipWin.touchable = false
_defaultTooltipWin.text = msg
GRoot.inst:AddChild(_defaultTooltipWin)
_defaultTooltipWin:Center()
_tip_timer = Timer.New(function()
__waittips()
end, time, 1, true)
_tip_timer:Start()
end
function ViewUtil.ErrorTip(code, tip, time)
if (Table_Error_code_Map[code] ~= nil) then
__ShowTips(string.format("%s", Table_Error_code_Map[code].note), time)
else
__ShowTips(tip, time)
end
end
function ViewUtil.ErrorMsg(_root_view, code, tip, url)
local msg = tip
if (Table_Error_code_Map[code] ~= nil) then
msg = string.format("%s", Table_Error_code_Map[code].note)
end
local _curren_msg = MsgWindow.new(_root_view, msg, MsgWindow.MsgMode.OnlyOk, url)
_curren_msg:Show()
end
function ViewUtil.ShowTips(msg)
__ShowTips(msg)
end
function ViewUtil.ShowBannerOnScreenCenter(msg, s)
local time = s or 0.8
if ViewUtil._banner then
ViewUtil._banner:Dispose()
end
local banner = UIPackage.CreateObjectFromURL("ui://Common/Pop_tips")
banner:GetChild("tex_tip").text = msg
GRoot.inst:AddChild(banner)
banner:Center()
ViewUtil._banner = banner
local timer = Timer.New(function()
banner:Dispose()
end, time, 1, true)
timer:Start()
end
function ViewUtil.PlaySoundBg()
GameApplication.Instance:PlayMuisc("base/common/sound/bgmusic1.mp3")
end
function ViewUtil.HandCardSort(a, b)
local sort_a = a
local sort_b = b
if a >= 400 then sort_a = sort_a - 1000 end
if b >= 400 then sort_b = sort_b - 1000 end
return sort_a < sort_b
end
function ViewUtil.HandCardSort2(a, b)
a = tonumber(string.sub(a, 2))
b = tonumber(string.sub(b, 2))
---- print(a)
---- print(b)
return a < b
end
function ViewUtil.HandCardSort3(a, b)
local sort_a = a[1] or 101
local sort_b = b[1] or 101
return sort_a < sort_b
end
function ViewUtil.CardPos(obj, area, oder, index, offset, isAdd, padding)
offset = offset or 0
padding = padding or 0
if oder == AreaOderType.left_right then
obj.x = index * (obj.width + padding) + offset
elseif oder == AreaOderType.up_down then
obj.y = index * (obj.height - padding) + offset
elseif oder == AreaOderType.right_left then
obj.x = (area.width - obj.width) - index * (obj.width + padding) - offset
elseif oder == AreaOderType.down_up then
if isAdd then
obj.y = area.height - obj.height - index * (obj.height + padding) * 1.5 - offset
else
obj.y = area.height - obj.height - index * (obj.height + padding) - offset
end
--obj.y = area.height - obj.height - index * obj.height - offset
end
end
local _current_group = nil
function ViewUtil.PlayMuisc(group, path)
if group ~= _current_group then
if _current_group then
ResourcesManager.UnLoadGroup(_current_group)
end
_current_group = group
end
GameApplication.Instance:PlayMuisc(group, path)
end
function ViewUtil.PlaySound(group, path)
if group ~= _current_group then
if _current_group then
ResourcesManager.UnLoadGroup(_current_group)
end
_current_group = group
end
GameApplication.Instance:PlaySound(group, path)
end
function ViewUtil.HideID(id)
local len = string.len(id)
return "****" .. string.sub(id, len - 1, len)
end
-- 检测麻将牌前缀名
function get_majiang_prefix(game_id)
-- 加载牌型数据
local ct = 1
if DataManager.CurrenRoom and DataManager.CurrenRoom.card_type then
ct = DataManager.CurrenRoom.card_type
--printlog("get_majiang_prefix=============1111")
elseif game_id then
--printlog("get_majiang_prefix=============1111222222222222")
local ct_data = nil
local json_data = Utils.LoadLocalFile("CardTypeData")
if json_data then
ct_data = json.decode(json_data)
else
ct_data = {}
end
ct = ct_data[tostring(game_id)] or 0
end
local prefix = ct == 0 and "" or "b"
--printlog("get_majiang_prefix===>>>",prefix,ct)
return prefix
end
function get_majiang_card_type(game_id)
local ct_data = nil
local json_data = Utils.LoadLocalFile("CardTypeData")
if json_data then
ct_data = json.decode(json_data)
else
ct_data = {}
end
ct = ct_data[tostring(game_id)] or 0
return ct
end
-- 数据转换成3位小数 data to a decimal
function d2ad(data)
return data / 1000
end
-- 3位小数转换成数据 a decimal to data
function ad2d(decimal)
return decimal * 1000
end
-- 获取gps
function get_gps(callback, data)
local _localAddress = data and data._callback_lad or nil
if Application.platform == RuntimePlatform.IPhonePlayer or Application.platform == RuntimePlatform.Android then
local s, e = pcall(function()
Utils.LocalAddress(function(result, latitude, longitude)
if _localAddress then _localAddress(result, latitude, longitude) end
if not result or (latitude == 0 and longitude == 0) then return end
DataManager.SelfUser.location = Location.new(tostring(longitude) .. "," .. tostring(latitude))
if callback then callback() end
end)
end)
if not s then
---- print("Error" .. e)
end
elseif Application.platform == RuntimePlatform.WindowsPlayer or Application.platform == RuntimePlatform.WindowsEditor then
-- DataManager.SelfUser.location = Location.new(string.format("%s,%s", math.random(10,20), math.random(10,20)))
DataManager.SelfUser.location = Location.new("10,10")
if callback then callback() end
end
end
--依据宽度截断字符
function ViewUtil.stringEllipsis(szText, size, full)
full = full or false
size = size or 4
--截断结果
local szResult = "..."
--完成判断
local bOK = false
local i = 1
local char_count = 0
while true do
local cur = string.sub(szText, i, i)
local byte = string.byte(cur)
if byte == nil then
break
end
if char_count >= size and full == false then
bOK = true
break
end
if byte > 128 then
i = i + 3
char_count = char_count + 1
elseif byte ~= 32 then
if string.byte('A') <= byte and byte <= string.byte('Z') then
char_count = char_count + 1
else
char_count = char_count + 0.5
end
i = i + 1
else
i = i + 1
char_count = char_count + 0.5
end
end
if i ~= 1 then
szResult = string.sub(szText, 1, i - 1)
if (bOK) then
szResult = szResult .. "..."
end
end
return szResult
end
-------------------------lingmeng---------------------------
function ViewUtil.ShowOneChooose(data, callback)
local pop_oneChoose = UIPackage.CreateObjectFromURL("ui://Common/pop_oneChoose")
pop_oneChoose:GetChild('text_show').text = data.showText
GRoot.inst:AddChild(pop_oneChoose)
pop_oneChoose:Center()
local btn_center = pop_oneChoose:GetChild('btn_center')
if data.btnType then
if type(data.btnType) == "number" then
btn_center.icon = string.format("ui://Common/btn_%d", data.btnType)
else
btn_center.icon = data.btnType
end
end
btn_center.onClick:Add(function()
GRoot.inst:RemoveChild(pop_oneChoose)
if callback then
callback()
end
end)
end
function ViewUtil.ShowTwoChooose(showText, leftCallback, rightCallback)
local pop_twoChoose = UIPackage.CreateObjectFromURL("ui://Common/pop_twoChoose")
pop_twoChoose:GetChild('text_show').text = showText
GRoot.inst:AddChild(pop_twoChoose)
pop_twoChoose:Center()
local btn_left = pop_twoChoose:GetChild('btn_leftChoose')
btn_left.onClick:Add(function()
GRoot.inst:RemoveChild(pop_twoChoose)
if leftCallback then
leftCallback()
end
end)
local btn_right = pop_twoChoose:GetChild('btn_rightChoose')
btn_right.onClick:Add(function()
GRoot.inst:RemoveChild(pop_twoChoose)
if rightCallback then
rightCallback()
end
end)
end
function ViewUtil.ShowOneInput(data, callback)
local data = data or {}
local pop_oneInput = UIPackage.CreateObjectFromURL("ui://Common/pop_oneInput")
pop_oneInput:GetChild('title').text = data.titleText or ""
pop_oneInput:GetChild('label_detial').text = data.showText or ""
pop_oneInput:GetChild('input_oneInput').promptText = data.promptText or ""
pop_oneInput:GetChild('input_oneInput').text = data.text or ""
GRoot.inst:AddChild(pop_oneInput)
pop_oneInput:Center()
local btn_center = pop_oneInput:GetChild('btn_center')
if data.btnType then
if type(data.btnType) == "number" then
btn_center.icon = string.format("ui://Common/btn_%d", data.btnType)
else
btn_center.icon = data.btnType
end
end
btn_center.onClick:Add(function()
GRoot.inst:RemoveChild(pop_oneInput)
if callback then
callback(pop_oneInput:GetChild('input_oneInput').text)
end
end)
local btn_center = pop_oneInput:GetChild('btn_close')
btn_center.onClick:Add(function()
GRoot.inst:RemoveChild(pop_oneInput)
end)
end
function ViewUtil.__share(data)
local url = DataManager.DownLink or "https://ttfenfa.com/index.php/login"
local json_data = {}
json_data['title'] = data.title
local mediaObject = {}
mediaObject['url'] = url
mediaObject['type'] = 0
json_data['mediaObject'] = mediaObject
json_data['description'] = data.description
json_data['scene'] = data.type
local filename = "icon96.png"
mediaObject["path"] = Application.persistentDataPath
mediaObject["filename"] = filename
GameApplication.Instance:ShareLink(1, json.encode(json_data), function(...)
end)
end
function ViewUtil.__openWx()
GameApplication.Instance:WXOnlyOpen(1, function(...)
end)
end
--确保页面加载后再渲染初始化数据
function ViewUtil.LoadPage(loader, url, callback)
loader.url = url
if loader.component then
callback(loader.component)
else
FrameTimer.New(function()
if loader.component then
callback(loader.component)
end
end, 1):Start()
end
end
local function RecursionBinarySearch(table, num, type, left, right)
if not table then
return nil
end
if #table == 0 then
return 1
end
left = left or 1
right = right or #table
if left == right then
if type == 0 then
return num >= table[left] and left + 1 or left
else
return num <= table[left] and left + 1 or left
end
end
if type == 0 then
local index = math.ceil((left + right) / 2)
if num >= table[index] then
left = index
else
right = index - 1
end
else
local index = math.floor((left + right) / 2)
if num <= table[index] then
left = index + 1
else
right = index
end
end
return RecursionBinarySearch(table, num, type or 0, left, right)
end
---comment 二分法查找下标
---@param table 查询表
---@param num 当前值
---@param type 类型 0为升序1为降序
---@return integer|nil
function ViewUtil.BinarySearch(table, num, type)
return RecursionBinarySearch(table, num, type)
end