AdventureBookView = AdventureBookView or BaseClass(BaseView)
|
|
local AdventureBookView = AdventureBookView
|
|
AdventureBookView.MOVE_OFFSET = 15
|
|
AdventureBookView.ITEM_OFFSET = 380
|
|
|
|
local math_abs = math.abs
|
|
|
|
function AdventureBookView:__init()
|
|
self.base_file = "adventureBook"
|
|
self.layout_file = "AdventureBookView"
|
|
self.layer_name = "Activity"
|
|
|
|
self.append_to_ctl_queue = true
|
|
self.use_background = true
|
|
|
|
self.model = AdventureBookModel:getInstance()
|
|
self.item_list = {}
|
|
|
|
--self.use_local_view = true
|
|
|
|
self.curr_index = -1
|
|
self.max_book_index = 10
|
|
|
|
self.is_moving = false
|
|
self.drag_begin_x = 0
|
|
self.drag_offset_x = 0
|
|
self.is_response_click = true -- 点击事件会在onDragend执行,如果是拖动的话不用执行点击
|
|
|
|
self.load_callback = function()
|
|
self:LoadSuccess()
|
|
end
|
|
|
|
self.open_callback = function()
|
|
self:InitView()
|
|
end
|
|
|
|
self.close_callback = function()
|
|
end
|
|
|
|
self.destroy_callback = function()
|
|
self:Remove()
|
|
end
|
|
end
|
|
|
|
function AdventureBookView:Open(type)
|
|
self.type = type
|
|
BaseView.Open(self)
|
|
end
|
|
|
|
function AdventureBookView:LoadSuccess()
|
|
|
|
self.transform.localPosition = Vector3.zero
|
|
self.transform.sizeDelta = Vector3.zero
|
|
|
|
|
|
if ClientConfig.iphone_x_model then
|
|
self.transform.offsetMin = Vector2(ClientConfig.iphone_x_offset_left,0)
|
|
self.transform.offsetMax = Vector2(-ClientConfig.iphone_x_offset_right,0)
|
|
end
|
|
|
|
self.bg = self:GetChildImages({
|
|
"bg"
|
|
})
|
|
|
|
self.item_parent = self:GetChildTransforms({
|
|
"Mask/ItemParent"
|
|
})
|
|
|
|
self.bg.transform.sizeDelta = Vector2(ScreenWidth, ScreenHeight)
|
|
lua_resM:setOutsideImageSprite(self,self.bg,GameResPath.GetJpgImage("zjui_bg.jpg"))
|
|
|
|
self.backBtn,
|
|
self.tipBtn,
|
|
self.leftBtn,
|
|
self.rightBtn,
|
|
self.touch_btn = self:GetChildGameObjects({
|
|
"backBtn",
|
|
"tipBtn",
|
|
"leftBtn",
|
|
"rightBtn",
|
|
"Mask/Touch"
|
|
})
|
|
|
|
self:InitEvent()
|
|
end
|
|
|
|
function AdventureBookView:InitView()
|
|
self.max_book_index = TableSize(Config.Adventurebook)
|
|
self:SwitchView(self.type)
|
|
end
|
|
|
|
function AdventureBookView:InitEvent()
|
|
local function onBtnClickHandler(target)
|
|
if target == self.backBtn then
|
|
self:Close()
|
|
elseif target == self.tipBtn then
|
|
EventSystem.Fire(GlobalEventSystem,EventName.OPEN_INSTRUCTION_VIEW, 601)
|
|
elseif target == self.leftBtn then
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
self:ShowPreItem()
|
|
|
|
elseif target == self.rightBtn then
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
|
|
self:ShowNextItem()
|
|
end
|
|
|
|
end
|
|
AddClickEvent(self.backBtn, onBtnClickHandler, 1)
|
|
AddClickEvent(self.tipBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
|
|
AddClickEvent(self.leftBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
|
|
AddClickEvent(self.rightBtn, onBtnClickHandler, LuaSoundManager.SOUND_UI.SWITCH)
|
|
|
|
local function onDragBegin(target,x,y)
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
self:OnDragBegin(x,y)
|
|
end
|
|
local function onDragEnd(target,x,y)
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
self:OnDragEnd(x,y)
|
|
end
|
|
local function onDrag(target,x, y)
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
self:OnDrag(x, y)
|
|
end
|
|
|
|
local function onTouchClickHandler(target,x,y)
|
|
if target == self.touch_btn then
|
|
if self.is_moving then
|
|
return
|
|
end
|
|
self:OnTouchClick(x,y)
|
|
end
|
|
end
|
|
|
|
AddClickEvent(self.touch_btn, onTouchClickHandler,LuaSoundManager.SOUND_UI.SWITCH)
|
|
AddDragBeginEvent(self.touch_btn,onDragBegin)
|
|
AddDragEndEvent(self.touch_btn,onDragEnd)
|
|
AddDragEvent(self.touch_btn,onDrag)
|
|
|
|
--绑定更新item显示的方法
|
|
local function onUpdateItem(type, subtype)
|
|
self:SwitchView(type)
|
|
end
|
|
self.update_item_id = self.model:Bind(AdventureBookModel.UPDATE_ADVENTURE_BOOK_INFO, onUpdateItem)
|
|
end
|
|
|
|
function AdventureBookView:OnTouchClick(x,y)
|
|
if not self.is_response_click then
|
|
return
|
|
end
|
|
|
|
x,y = ScreenToViewportPoint(x,y)
|
|
x = x - ScreenWidth / 2
|
|
y = y - ScreenHeight / 2
|
|
|
|
if y > -310 and y < 190 then
|
|
local is_click_center = x >= -170 and x <= 170
|
|
local is_click_left = x >= -500 and x <= -260
|
|
local is_click_right = x >= 260 and x <= 500
|
|
if is_click_center or is_click_left or is_click_right then
|
|
if is_click_left then
|
|
self:ShowPreItem()
|
|
elseif is_click_right then
|
|
self:ShowNextItem()
|
|
elseif is_click_center then
|
|
local click_item,pos
|
|
for index,item in pairs(self.item_list) do
|
|
pos = item:GetAnchoredPosition()
|
|
if pos and pos.x >= -50 and pos.x <= 50 then
|
|
click_item = item
|
|
break
|
|
end
|
|
end
|
|
if click_item then
|
|
click_item:OnClickItem()
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
function AdventureBookView:ShowPreItem()
|
|
for index,item in ipairs(self.item_list) do
|
|
item:DragBegin()
|
|
end
|
|
|
|
self:SetMoveEndState(AdventureBookView.ITEM_OFFSET)
|
|
end
|
|
|
|
function AdventureBookView:ShowNextItem()
|
|
for index,item in ipairs(self.item_list) do
|
|
item:DragBegin()
|
|
end
|
|
|
|
self:SetMoveEndState(-AdventureBookView.ITEM_OFFSET)
|
|
end
|
|
|
|
function AdventureBookView:OnDragBegin(x,y)
|
|
self.is_response_click = false
|
|
self.drag_begin_x = x
|
|
|
|
for index,item in ipairs(self.item_list) do
|
|
item:DragBegin()
|
|
end
|
|
end
|
|
|
|
function AdventureBookView:OnDrag(x,y)
|
|
self.drag_offset_x = x - self.drag_begin_x
|
|
|
|
for index,item in ipairs(self.item_list) do
|
|
item:Drag(self.drag_offset_x)
|
|
end
|
|
end
|
|
|
|
function AdventureBookView:OnDragEnd(x,y)
|
|
self.is_response_click = true
|
|
local min_distance = -1
|
|
local pos
|
|
for index,item in ipairs(self.item_list) do
|
|
item:DragEnd()
|
|
pos = item:GetAnchoredPosition()
|
|
if pos then
|
|
if min_distance == -1 or math_abs(min_distance) > math_abs(pos.x) then
|
|
min_distance = pos.x
|
|
end
|
|
end
|
|
end
|
|
|
|
if min_distance ~= -1 then
|
|
self:SetMoveEndState( -min_distance)
|
|
end
|
|
end
|
|
|
|
--设置移动到最后的状态
|
|
function AdventureBookView:SetMoveEndState(distance)
|
|
self.is_moving = true
|
|
self.drag_begin_x = 0
|
|
self.move_end_dis = 0
|
|
self.move_end_total_dis = distance
|
|
self.move_end_offset = distance > 0 and AdventureBookView.MOVE_OFFSET or - AdventureBookView.MOVE_OFFSET
|
|
self:StartUpdateBeat()
|
|
end
|
|
|
|
function AdventureBookView:StartUpdateBeat( )
|
|
Runner:getInstance():AddRunObj(self, 1)
|
|
end
|
|
|
|
function AdventureBookView:RemoveUpdateBeat( )
|
|
Runner.Instance:RemoveRunObj(self)
|
|
end
|
|
|
|
--update是模拟drag的过程
|
|
function AdventureBookView:Update(now_time, elapse_time)
|
|
if self.is_moving then
|
|
self.move_end_dis = self.move_end_dis + self.move_end_offset
|
|
self:OnDrag(self.move_end_dis,0)
|
|
if math_abs(self.move_end_dis - self.move_end_total_dis) <= AdventureBookView.MOVE_OFFSET then
|
|
|
|
self:OnDrag(self.move_end_total_dis,0)
|
|
|
|
for index,item in ipairs(self.item_list) do
|
|
item:DragEnd()
|
|
end
|
|
self:RemoveUpdateBeat()
|
|
setTimeout(function()
|
|
self.is_moving = false
|
|
end,0.1)
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
function AdventureBookView:SwitchView(type)
|
|
|
|
local book_list = self.model:GetAllBookByType(type)
|
|
local show_index = self.model:GetBookShowIndex(type)
|
|
local show_list = {}
|
|
local real_index,data
|
|
for i = show_index - 2,show_index + 1 do
|
|
real_index = i
|
|
if real_index > self.max_book_index then
|
|
real_index = real_index - self.max_book_index
|
|
elseif real_index < 1 then
|
|
real_index = real_index + self.max_book_index
|
|
end
|
|
data = book_list[real_index]
|
|
if data then
|
|
table.insert(show_list,data)
|
|
end
|
|
end
|
|
|
|
local len = #show_list
|
|
local item,X
|
|
for i,v in ipairs(show_list) do
|
|
item = self.item_list[i]
|
|
if item == nil then
|
|
item = AdventureBookItem.New(self.item_parent)
|
|
table.insert(self.item_list,item)
|
|
end
|
|
item:SetVisible(true)
|
|
item:SetData(v)
|
|
x = (3 - i) * - AdventureBookView.ITEM_OFFSET
|
|
item:SetDragBeginX(x)
|
|
item:SetAnchoredPosition(x,0)
|
|
end
|
|
|
|
for i=#show_list + 1,#self.item_list do
|
|
self.item_list[i]:SetVisible(false)
|
|
end
|
|
end
|
|
|
|
function AdventureBookView:Remove()
|
|
|
|
if self.update_item_id then
|
|
self.model:UnBind(self.update_item_id )
|
|
self.update_item_id = nil
|
|
end
|
|
|
|
for i, v in pairs(self.item_list) do
|
|
v:DeleteMe()
|
|
v = nil
|
|
end
|
|
self.item_list = {}
|
|
|
|
self:RemoveUpdateBeat()
|
|
end
|