|
|
- -- <*
- -- @Author: Saber
- -- @Description: 藏宝图 答题界面
- -- *>
- TreasureMapQuizView = TreasureMapQuizView or BaseClass(BaseView)
- local TreasureMapQuizView = TreasureMapQuizView
-
- function TreasureMapQuizView:__init()
- self.base_file = "treasureMap"
- self.layout_file = "TreasureMapQuizView"
- self.layer_name = "UI"
- self.destroy_imm = true
- self.use_background = true --全屏界面默认使用这个参数,非全屏界面自行设置
- self.hide_maincancas = false --全屏界面需要隐藏主UI
- self.change_scene_close = true --是否切换场景时关闭(弹出界面使用)
- self.model = TreasureMapModel:getInstance()
- self.ans_seq = {
- [1] = "A", [2] = "B", [3] = "C", [4] = "D",
- }
- self.ans_item = {}
- self.auto_close_time = 30 -- 几秒后自动关闭
- self.selected_index = false -- 是否已经选择了选项
- self.load_callback = function ()
- self:LoadSuccess()
- self:AddEvent()
- end
- self.open_callback = function ( )
- self:UpdateView()
- end
- self.destroy_callback = function ( )
- self:DestroySuccess()
- end
- end
-
- function TreasureMapQuizView:Open(data)
- self.data = data
- BaseView.Open(self)
- end
-
- function TreasureMapQuizView:LoadSuccess()
- local nodes = {
- "bg:raw", "quiz_role:raw", "close_btn:obj",
- "ans_con",
- "reward_con",
- "ques_lb:tmp",
- "sub_title:tmp",
- "auto_close:tmp",
- }
- self:GetChildren(nodes)
- lua_resM:setOutsideRawImage(self, self.bg_raw, GameResPath.GetViewBigBg("tm_quiz_bg"))
- lua_resM:setOutsideRawImage(self, self.quiz_role_raw, GameResPath.GetRoleBg("tm_quiz_role_431_618"))
- self.sub_title_tmp.text = string.format("没想到居然有人能找到这来,你运气不错嘛!\n来吧,出道题考考你!")
-
- self.awardItem = UIObjPool:getInstance():PopItem(UIObjPool.UIType.AwardItem, self.reward_con)
- self.awardItem:SetItemSize(78, 78)
- self.awardItem:SetAnchoredPosition(0, 0)
- end
-
- function TreasureMapQuizView:AddEvent()
- local function click_event(target)
- if target == self.close_btn_obj then
- self:Close()
- end
- end
- AddClickEvent(self.close_btn_obj, click_event)
-
- local function updateViewAfterSelectedAns()
- self:UpdateSelectedResult()
- end
- self:BindEvent(self.model, TreasureMapConst.UPDATE_QUIZ_VIEW, updateViewAfterSelectedAns)
- end
-
- function TreasureMapQuizView:UpdateView()
- self.quiz_cfg = Config.Treasuremapquiz[self.data.quiz_id]
- self.reward_cfg = Config.Treasuremaprewards[self.data.reward_id]
- self:UpdateBasicData()
- self:AutoCloseTimer()
- end
-
- -- 加载谜题等基础数据
- function TreasureMapQuizView:UpdateBasicData( )
- if self.quiz_cfg then -- 加载谜题和选项
- self.ques_lb_tmp.text = Trim(self.quiz_cfg.question)
- local ans_data = stringtotable(self.quiz_cfg.option)
- self.ans_data = RandomizeSequenceTableData(ans_data)
-
- self.ans_item_creator = self.ans_item_creator or self:AddUIComponent(UI.ItemListCreator)
- local info = {
- data_list = self.ans_data,
- item_con = self.ans_con,
- scroll_view = self.ans_con,
- prefab_ab_name = "treasureMap",
- prefab_res_name = "TreasureMapQuizAnsItem",
- item_width = 158,
- item_height = 42,
- start_y = -5,
- space_x = 30,
- space_y = 20,
- create_frequency = 0.01,
- alignment = UnityEngine.TextAnchor.UpperLeft,
- child_names = {
- "ans_btn:obj", "ans_btn/ans_lb:tmp",
- },
- on_update_item = function(item, i, v)
- item.ans_lb_tmp.text = string.format("%s.%s", self.ans_seq[i], Trim(v[2]))
- local function click_event(target)
- if target == item.ans_btn_obj then
- if self.selected_index then return end -- 选中了答案之后就不处理
- self.model:Fire(TreasureMapConst.REQUEST_CCMD_EVENT, 42408, self.data.quiz_id, v[1])
- self.selected_index = i
- end
- end
- AddClickEvent(item.ans_btn_obj, click_event)
- end,
- }
- self.ans_item_creator:UpdateItems(info)
- end
- -- 加载奖励节点
- if self.reward_cfg then
- local reward = stringtotable(self.reward_cfg.rewards)[1] or {}
- local goods_Id, lock = GoodsModel:getInstance():GetMappingTypeId(reward[1] , reward[2])
- self.awardItem:SetData(goods_Id, reward[3], nil, nil, lock)
- end
- end
-
- function TreasureMapQuizView:AutoCloseTimer( )
- local end_time = TimeUtil:getServerTime() + self.auto_close_time
- local function auto_close_time_func()
- local left_time = end_time - TimeUtil:getServerTime()
- if left_time > 0 then
- self.auto_close_tmp.text = self.selected_index
- and string.format("<color=%s>%s</color> 秒后自动关闭", ColorUtil.GREEN_DARK, left_time)
- or string.format("(回答正确可得双倍奖励,<color=%s>%s</color> 秒后自动选择)",
- ColorUtil.GREEN_DARK, left_time)
- else
- if not self.selected_index then
- self.model:Fire(TreasureMapConst.REQUEST_CCMD_EVENT, 42408, self.data.quiz_id, 1)
- else
- self:Close()
- end
- end
- end
- self:ClearAutoCloseTimer()
- self.auto_close_time_func_id = GlobalTimerQuest:AddPeriodQuest(auto_close_time_func, 0.5, -1)
- end
-
- function TreasureMapQuizView:ClearAutoCloseTimer( )
- if self.auto_close_time_func_id then
- GlobalTimerQuest:CancelQuest(self.auto_close_time_func_id)
- self.auto_close_time_func_id = nil
- end
- end
-
- -- 根据08协议的返回更新界面表现
- function TreasureMapQuizView:UpdateSelectedResult( )
- if self.quiz_cfg then -- 加载谜题和选项
- self.ans_item_creator:IterateItems(function( item, i )
- if i == self.selected_index then
- local ans_data = self.ans_data[i]
- local ans_currect = ans_data[1] == self.quiz_cfg.answer
- item.ans_lb_tmp.text = string.format("<color=%s>%s.%s</color>",
- ans_currect and ColorUtil.GREEN_DARK or ColorUtil.RED_DARK,
- self.ans_seq[i], Trim(ans_data[2]))
- -- 飘字表现
- Message.show(ans_currect and "回答正确,获得双倍奖励~" or "回答错误", ans_currect and "success" or "fault")
- end
- end)
- -- 重置自动关闭时间
- self.auto_close_time = 5
- self:AutoCloseTimer()
- end
- end
-
- function TreasureMapQuizView:DestroySuccess( )
- if self.awardItem then
- UIObjPool:getInstance():PushItem(UIObjPool.UIType.AwardItem, self.awardItem)
- self.awardItem = nil
- end
- self:ClearAutoCloseTimer()
- -- 没选的话自动选中第一个
- if not self.selected_index then
- self.model:Fire(TreasureMapConst.REQUEST_CCMD_EVENT, 42408, self.data.quiz_id, 1)
- end
- self.model._check_tm_tips = TreasureMapConst.Normal
- self.model:CheckTreasureTipUseViewOpen()
- end
|