文字漂浮的小功能 lua代码框架下实现

来源:互联网 发布:react 源码 component 编辑:程序博客网 时间:2024/05/21 15:45

功能:事件出发,向上飘出文本或者图片或者两个都有,渐隐消失

虽然是框架下实现的,不过基本逻辑都可以用。

写得不好,先留着。




viewr类的方法,具体实现功能

local this = {}
local ctrl = nil
local stringList = {}
this.gameObject = nil


function this.Init(_ctrl)
ctrl = _ctrl
this.gameObject = lua_function.LoadInstanceGameObject("popMessage")
this._endPos = lua_function.GetChild (this.gameObject,"endPos"):GetComponent("Transform")
end


--飘窗的动画控制
function this.animationController(_gameObject,_data)
if _gameObject == nil then
return
end
coroutine.start(function()
lua_function.MoveToWorld(_gameObject, _gameObject.transform.position.x, (_gameObject.transform.position.y + 300), _gameObject.transform.position.z,1.5, nil)
coroutine.wait(1)
lua_function.ChangeAlpha(_gameObject, 0, 0.5, nil)
coroutine.wait(0.5)
lua_function.Destroied(_gameObject)
-- remove掉stringList中储存的message
for k , v inpairs(stringList) do
if v == _data["msg"]then
table.remove(stringList, k)
end
end
end)
end



--参数
--messageType = 1 or 2 1纯文本 2带图片
--mas = "string"
--color = { r = 0, g = 0, b = 0}
--ima = "path"

function this.Draw(_data)
--判断没有消失的message里面有没有重复的,如果有重复的返回false,没有true
if isEnable(_data) ==false then
return
end


--根据messageType分类
if _data["messageType"] ==1 then
this.message_gameObject = lua_function.LoadInstanceGameObject("message")
lua_function.SetParent(this.message_gameObject, this.gameObject)
lua_function.SetLocalPosition(this.message_gameObject,0,0,0)
local _message_tex = this.message_gameObject:GetComponent("Text")
_message_tex.text = _data["msg"]
--默认颜色为白色
if _data["color"] ==nil then
_data["color"] = {r = 1, g = 1, b = 1, a = 1}
end
lua_function.SetColor(_message_tex.gameObject, _data["color"]["r"], _data["color"]["g"], _data["color"]["b"], _data["color"]["a"])

elseif _data["messageType"] ==2 then
this.message_gameObject = lua_function.LoadInstanceGameObject("messageAndImage")
--图片的情况可能没有msg,先判断,如果有,把字符串处理了
if _data["msg"] ~=nil then
lua_function.SetParent(this.message_gameObject, this.gameObject)
lua_function.SetLocalPosition(this.message_gameObject,0,0,0)
local _message_tex = this.message_gameObject:GetComponent("Text")
_message_tex.text = _data["msg"]
--默认颜色为白色
if _data["color"] ==nil then
_data["color"] = {r = 1, g = 1, b = 1, a = 1}
end
lua_function.SetColor(_message_tex.gameObject, _data["color"]["r"], _data["color"]["g"], _data["color"]["b"],1)
end
--type == 2 肯定有image 。处理图片,先加载图片图像,再根据字符串长短确定图片位置
local _image = lua_function.GetChild (this.message_gameObject,"image")

--确定位置
if _data["msg"] ==nil then
lua_function.SetLocalPosition(_image,0,0,0)
elseif #_data["msg"] <=12 then
lua_function.SetLocalPosition(_image,-70,0,0)--pos1的位置
elseif #_data["msg"] <=21 then
lua_function.SetLocalPosition(_image,-120,0,0)--pos2的位置
elseif #_data["msg"] <=33 then
lua_function.SetLocalPosition(_image,-160,0,0)--pos3的位置
end

--加载图片
lua_function.SetSprite (_image, _data["image"],true)
end
this.animationController(this.message_gameObject, _data)
end



--判断没有消失的message里面有没有重复的,如果有重复的返回false,没有true

function isEnable(_data)
for _ , v inpairs(stringList) do
if v == _data["msg"]then
return false
end
end
table.insert(stringList, _data["msg"])
return true
end

function this.Dispose()
end

return this

controller类的方法, 对外接口等


local this = {}
this.view = nil
this.isInit = false

function Init()
this.view = lua_manager.GetLua(lua_files.popMessage_view)
this.view.Init(this)
lua_event_listener.AddOnUIButtonClickListener("popMessage_controller", this)
isInit = true
end

--测试用
function this.Refresh()
if not isInit then
Init()
end
end

function InitTest()
if isInit then
return
end
Init()
end

--通用接口
function this.Text(_msg)
InitTest()
local x = {msg = _msg, messageType = 1}
this.view.Draw(x)
end

function this.TextWithColor(_msg,_color)
InitTest()
local x = {msg = _msg, color = _color, messageType =1}
this.view.Draw(x)
end

function this.Image(_image)
InitTest()
local x = {image = _image ,messageType = 2}
this.view.Draw(x)
end

function this.ImageWithText(_image,_msg)
InitTest()
local x = {msg = _msg, image = _image, messageType =2}
this.view.Draw(x)
end

function this.ImageWithTextWithColor(_image,_msg, _color)
InitTest()
local x = {msg = _msg, color = _color, image = _image, messageType =2}
this.view.Draw(x)
end

function this.getdefult(_type)
if _type == 1then
return "commonicon012"
elseif _type == 2 then
return "commonicon014"
elseif _type == 3 then
return "adven018"
end
end

--金币钻石经验特殊处理
function this.ShowCoin(_coin)
local _coinRes = this.getdefult(1)
this.ImageWithText(_coinRes, _coin)
end

function this.ShowCoinWithColor(_coin,_color)
local _coinRes = this.getdefult(1)
this.ImageWithTextWithColor(_coinRes, _coin, _color)
end

function this.ShowDiamond(_coin)
local _coinRes = this.getdefult(2)
this.ImageWithText(_coinRes, _coin)
end

function this.ShowDiamondWithColor(_coin,_color)
local _coinRes = this.getdefult(2)
this.ImageWithTextWithColor(_coinRes, _coin, _color)
end

function this.ShowExp(_coin)
local _coinRes = this.getdefult(3)
this.ImageWithText(_coinRes, _coin)
end

function this.ShowExpWithColor(_coin,_color)
local _coinRes = this.getdefult(3)
this.ImageWithTextWithColor(_coinRes, _coin, _color)
end


--测试的按钮
function this.OnClick(_func,_go)
if _func == "111"then
color = {r = 1, g = 1, b = 0, a = 1}
this.ShowDiamondWithColor("11111111111111", color)
elseif _func == "222" then
local y = {msg = "2222222" , color = { r = 0, g = 0, b = 1, a = 1}, messageType = 1}
this.view.Draw(y)
end
end


function this.Dispose()
this.view.Dispose()
lua_event_listener.RemoveOnUIButtonClickListener("popWindow_controller")
end


return this