Commit cae7f496 authored by 李维's avatar 李维

修复切换时偶现的背景消失的问题

parent c5d5ac15
...@@ -198,7 +198,7 @@ export default class NewClass extends cc.Component { ...@@ -198,7 +198,7 @@ export default class NewClass extends cc.Component {
initCustomBg(bg_image_url) { initCustomBg(bg_image_url) {
console.log("Iniit", bg_image_url) console.log("Iniit", bg_image_url)
if(!bg_image_url) { if (!bg_image_url) {
// 没有背景图 还原默认图 // 没有背景图 还原默认图
cc.find("bg/bg_default", this.node).active = true; cc.find("bg/bg_default", this.node).active = true;
cc.find("bg/bg", this.node).active = false; cc.find("bg/bg", this.node).active = false;
...@@ -206,12 +206,13 @@ export default class NewClass extends cc.Component { ...@@ -206,12 +206,13 @@ export default class NewClass extends cc.Component {
} }
const canvas = cc.find("Canvas"); const canvas = cc.find("Canvas");
const bg = cc.find("bg/bg", this.node); const bg = cc.find("bg/bg", this.node);
getSprNodeByUrl(bg_image_url, (sf)=>{ getSprNodeByUrl(bg_image_url, (sf) => {
const sx = canvas.width / sf.node.width; const sx = canvas.width / sf.node.width;
const sy = canvas.height / sf.node.height; const sy = canvas.height / sf.node.height;
bg.getComponent(cc.Sprite).spriteFrame = sf.spriteFrame; bg.getComponent(cc.Sprite).spriteFrame = sf.spriteFrame;
bg.scale = Math.max(sx, sy); bg.scale = Math.max(sx, sy);
cc.find("bg/bg_default", this.node).active = false; cc.find("bg/bg_default", this.node).active = false;
cc.find("bg/bg", this.node).active = true;
}) })
} }
......
...@@ -8,333 +8,334 @@ ...@@ -8,333 +8,334 @@
import { getSprNodeByUrl, setSprNodeMaxLen, asyncCallNetworkApiGet, asyncDelay } from "./util"; import { getSprNodeByUrl, setSprNodeMaxLen, asyncCallNetworkApiGet, asyncDelay } from "./util";
const {ccclass, property} = cc._decorator; const { ccclass, property } = cc._decorator;
@ccclass @ccclass
export default class NewClass extends cc.Component { export default class NewClass extends cc.Component {
@property(cc.Node) @property(cc.Node)
Viewer: cc.Node = null; Viewer: cc.Node = null;
@property(cc.Node) @property(cc.Node)
unitScrollView: cc.Node = null; unitScrollView: cc.Node = null;
@property(cc.Node) @property(cc.Node)
loading: cc.Node = null; loading: cc.Node = null;
@property(cc.Node) @property(cc.Node)
btn_preview: cc.Node = null; btn_preview: cc.Node = null;
@property(cc.Node) @property(cc.Node)
btn_works: cc.Node = null; btn_works: cc.Node = null;
@property(cc.Node) @property(cc.Node)
LessonBrief: cc.Node = null; LessonBrief: cc.Node = null;
handleClickBackBtn() { handleClickBackBtn() {
const vc = this.Viewer.getComponent("PageViewer"); const vc = this.Viewer.getComponent("PageViewer");
vc.toView("HomePage", "leftIn") vc.toView("HomePage", "leftIn")
} }
// LIFE-CYCLE CALLBACKS: // LIFE-CYCLE CALLBACKS:
// onLoad () {} // onLoad () {}
async start () { async start() {
this.initBg(); this.initBg();
this.listItem = cc.find("view/content/item", this.unitScrollView); this.listItem = cc.find("view/content/item", this.unitScrollView);
} }
async onEnable() { async onEnable() {
await this.initView(); await this.initView();
} }
async initView() {
this.loading.active = true;
this.initTitle();
await this.initUnitListView();
this.loading.active = false;
}
initBg() { async initView() {
const canvas = cc.find("Canvas"); this.loading.active = true;
const bg = cc.find("bg/bg_default", this.node); this.initTitle();
await this.initUnitListView();
const sx = canvas.width / bg.width; this.loading.active = false;
const sy = canvas.height / bg.height; }
bg.scale = Math.max(sx, sy);
}
initCustomBg(bg_image_url) {
if(!bg_image_url) {
// 没有背景图 还原默认图
cc.find("bg/bg_default", this.node).active = true;
cc.find("bg/bg", this.node).active = false;
return
}
const canvas = cc.find("Canvas");
const bg = cc.find("bg/bg", this.node);
getSprNodeByUrl(bg_image_url, (sf)=>{
const sx = canvas.width / sf.node.width;
const sy = canvas.height / sf.node.height;
bg.getComponent(cc.Sprite).spriteFrame = sf.spriteFrame;
bg.scale = Math.max(sx, sy);
cc.find("bg/bg_default", this.node).active = false;
})
}
initBg() {
const canvas = cc.find("Canvas");
const bg = cc.find("bg/bg_default", this.node);
async initTitle() { const sx = canvas.width / bg.width;
const titleLabelNode = cc.find("bg/bg_title/label", this.node); const sy = canvas.height / bg.height;
const titleLabel = titleLabelNode.getComponent(cc.Label); bg.scale = Math.max(sx, sy);
titleLabel.string = this.node['pData'].name; }
const bgTitle = titleLabelNode.parent; initCustomBg(bg_image_url) {
if (!bgTitle['baseW']) { if (!bg_image_url) {
bgTitle['baseW'] = bgTitle.width; // 没有背景图 还原默认图
} cc.find("bg/bg_default", this.node).active = true;
const baseW = bgTitle['baseW']; cc.find("bg/bg", this.node).active = false;
return
await asyncDelay(0.01);
if (titleLabelNode.width * 1.2 > baseW) {
bgTitle.width = titleLabelNode.width * 1.2;
// titleLabelNode.scale = baswW * 0.9 / titleLabelNode.width;
} else {
bgTitle.width = baseW;
}
} }
const canvas = cc.find("Canvas");
syllabusLockStatusStore = {}; // 课件是否解锁的存储对象 { 99343: 'locked', 19834: 'unlock' } const bg = cc.find("bg/bg", this.node);
syllabusFinishedStore = {}; // 课件是否完成的存储对象 { 99343: 'finished', 19834: 'unfinish' } getSprNodeByUrl(bg_image_url, (sf) => {
const sx = canvas.width / sf.node.width;
async getAndStoreSyllabusStatus(tree) { const sy = canvas.height / sf.node.height;
const oneFlow = []; // 第一层需要获取解锁状态 bg.getComponent(cc.Sprite).spriteFrame = sf.spriteFrame;
const twoFlow = []; // 第二层需要获取完成状态 bg.scale = Math.max(sx, sy);
tree.forEach(item => { cc.find("bg/bg_default", this.node).active = false;
item.children.forEach(one => { cc.find("bg/bg", this.node).active = true;
oneFlow.push(one.id); })
one.children.forEach(two => { }
twoFlow.push(two.id);
});
}); async initTitle() {
const titleLabelNode = cc.find("bg/bg_title/label", this.node);
const titleLabel = titleLabelNode.getComponent(cc.Label);
titleLabel.string = this.node['pData'].name;
const bgTitle = titleLabelNode.parent;
if (!bgTitle['baseW']) {
bgTitle['baseW'] = bgTitle.width;
}
const baseW = bgTitle['baseW'];
await asyncDelay(0.01);
if (titleLabelNode.width * 1.2 > baseW) {
bgTitle.width = titleLabelNode.width * 1.2;
// titleLabelNode.scale = baswW * 0.9 / titleLabelNode.width;
} else {
bgTitle.width = baseW;
}
}
syllabusLockStatusStore = {}; // 课件是否解锁的存储对象 { 99343: 'locked', 19834: 'unlock' }
syllabusFinishedStore = {}; // 课件是否完成的存储对象 { 99343: 'finished', 19834: 'unfinish' }
async getAndStoreSyllabusStatus(tree) {
const oneFlow = []; // 第一层需要获取解锁状态
const twoFlow = []; // 第二层需要获取完成状态
tree.forEach(item => {
item.children.forEach(one => {
oneFlow.push(one.id);
one.children.forEach(two => {
twoFlow.push(two.id);
}); });
const {rows} = await asyncCallNetworkApiGet(`/api/kyxq/v1/user/syllabus/status`, {syllabuses: [...oneFlow, ...twoFlow].join(`,`)}); });
for (let i = 0; i < rows.length; i++) { });
const row = rows[i]; const { rows } = await asyncCallNetworkApiGet(`/api/kyxq/v1/user/syllabus/status`, { syllabuses: [...oneFlow, ...twoFlow].join(`,`) });
if (oneFlow.includes(row.id)) { for (let i = 0; i < rows.length; i++) {
this.syllabusLockStatusStore[row.id] = row.unlock == 0 ? 'locked' : 'unlock'; const row = rows[i];
} if (oneFlow.includes(row.id)) {
if (twoFlow.includes(row.id)) { this.syllabusLockStatusStore[row.id] = row.unlock == 0 ? 'locked' : 'unlock';
this.syllabusFinishedStore[row.id] = row.end == 0 ? 'unfinish' : 'finished'; }
} if (twoFlow.includes(row.id)) {
} this.syllabusFinishedStore[row.id] = row.end == 0 ? 'unfinish' : 'finished';
}
} }
configData = [] }
async initUnitListView() {
const svNode = this.unitScrollView; configData = []
svNode.opacity = 0; async initUnitListView() {
const resData = await this.Viewer.getComponent("PageViewer").getSyllabusTreeByPid(this.node['syllabus_pid']) const svNode = this.unitScrollView;
svNode.opacity = 0;
let rows = [] const resData = await this.Viewer.getComponent("PageViewer").getSyllabusTreeByPid(this.node['syllabus_pid'])
this.configData = null
let bgImageUrl = null; let rows = []
for (let v of resData.rows) { this.configData = null
if (v.name == "config") { let bgImageUrl = null;
// 课程配置文件 for (let v of resData.rows) {
const _strData = (v.children && v.children[0] && v.children[0].template_name == "sn_config" && v.children[0].data) ? v.children && v.children[0] && v.children[0].data : "{}" if (v.name == "config") {
this.configData = JSON.parse(_strData) // 课程配置文件
bgImageUrl = this.configData.bg_image_url const _strData = (v.children && v.children[0] && v.children[0].template_name == "sn_config" && v.children[0].data) ? v.children && v.children[0] && v.children[0].data : "{}"
this.configData = JSON.parse(_strData)
} else { bgImageUrl = this.configData.bg_image_url
// 课程数据
rows.push(v) } else {
} // 课程数据
} rows.push(v)
}
if (rows.length == 0) {
const svNode = this.unitScrollView;
const sv = svNode.getComponent(cc.ScrollView);
const content = sv.content;
content.removeAllChildren();
return;
}
this.initCustomBg(bgImageUrl);
await this.getAndStoreSyllabusStatus(rows);
// const svNode = this.unitScrollView;
const view = cc.find("view", svNode);
view.width = svNode.width;
view.height = svNode.height;
const itemArr = this.getUnitItemDataArr(rows);
const sv = svNode.getComponent(cc.ScrollView);
this.initScrollViewItem(sv, itemArr, 1);
const content = sv.content;
const canvas = cc.find("Canvas");
if (content.width > canvas.width) {
content.x = -canvas.width / 2;
} else {
content.x = -content.width / 2;
}
svNode.opacity = 255;
this.refreshConfig()
} }
// 刷新右上角“课程简介”按钮显示隐藏 if (rows.length == 0) {
refreshConfig() { const svNode = this.unitScrollView;
if (this.configData && this.configData.introduces.length > 0) { const sv = svNode.getComponent(cc.ScrollView);
this.btn_preview.active = true const content = sv.content;
} else { content.removeAllChildren();
this.btn_preview.active = false return;
}
} }
listItem; this.initCustomBg(bgImageUrl);
getUnitItemDataArr(dataArr) { await this.getAndStoreSyllabusStatus(rows);
let listItem = this.listItem; // const svNode = this.unitScrollView;
let subItem = cc.find("sub_item", listItem); const view = cc.find("view", svNode);
let listLabel = cc.find("label", listItem); view.width = svNode.width;
view.height = svNode.height;
const itemArr = [];
for (let i=0; i<dataArr.length; i++) { const itemArr = this.getUnitItemDataArr(rows);
const data = dataArr[i]; const sv = svNode.getComponent(cc.ScrollView);
const itemNode = cc.instantiate(listItem); this.initScrollViewItem(sv, itemArr, 1);
itemNode['data'] = data;
itemNode.removeAllChildren(); const content = sv.content;
const canvas = cc.find("Canvas");
const subArr = data.children; if (content.width > canvas.width) {
const baseX = subItem.x; content.x = -canvas.width / 2;
const baseY = subItem.y; } else {
const disX = 530; content.x = -content.width / 2;
let lastX = 0;
for (let j=0; j<subArr.length; j++) {
const subNode = cc.instantiate(subItem)
subNode.x = baseX + j * disX;
subNode.y = baseY;
itemNode.addChild(subNode);
lastX = subNode.x;
const subData = subArr[j];
getSprNodeByUrl(subData.cover, (spr) => {
const sprNode = spr.node;
const picLayer = cc.find("mask/pic_frame", subNode);
picLayer.addChild(sprNode);
setSprNodeMaxLen(sprNode, picLayer.width, picLayer.height, true);
})
const pointBase = cc.find("point", subNode);
const pointLayer = cc.find("pint_layer", subNode);
const pointDisX = 57;
pointBase.opacity = 0;
for (let k = 0; k < 8; k++) {
const clsData = subData.children[k];
const pointNode = cc.instantiate(pointBase);
pointNode.opacity = 255;
if (clsData) {
const finished = this.syllabusLockStatusStore[clsData.id]==='finished';
if (!finished) {
const finishIcon = cc.find("blue", pointNode);
finishIcon.active = false;
}
} else {
const finishIcon = cc.find("blue", pointNode);
finishIcon.active = false;
}
pointLayer.addChild(pointNode);
pointNode.x = pointBase.x + k * pointDisX;
}
// 为方便测试,先全部解锁
const lockCover = cc.find("bg_cover_mask", subNode);
lockCover.active = false;
// 下面是正常解锁逻辑
// const unLock = this.syllabusLockStatusStore[subData.id]==='unlock';
// lockCover.active = !unLock;
// if (i==0&&j==0) {
// // 说明是第一个
// lockCover.active = false;
// }
const labelNode = cc.find("label", subNode);
const label = labelNode.getComponent(cc.Label);
label.string = subData.name;
subNode.addComponent(cc.Button);
subNode.on("click", () => {
const vc = this.Viewer.getComponent("PageViewer");
vc.missionPage.attr({syllabus_pid: subData.id});
vc.missionPage.attr({pData: subData});
vc.toView("MissionPage", "rightIn")
})
}
const labelNode = cc.instantiate(listLabel);
const label = labelNode.getComponent(cc.Label);
label.string = data.name//"Unit " + (i + 1);
itemNode.addChild(labelNode);
// if (subArr.length > 2) {
itemNode.width = lastX + 360;
setTimeout(() => {
itemNode.width = lastX + 360;
}, 1);
// }
itemArr.push(itemNode);
}
return itemArr;
} }
svNode.opacity = 255;
initScrollViewItem(sv, itemArr, row=1) { this.refreshConfig()
sv.scrollToTop(); }
const content = sv.content;
content.width = sv.node.width; // 刷新右上角“课程简介”按钮显示隐藏
content.height = sv.node.height; refreshConfig() {
content.removeAllChildren(); if (this.configData && this.configData.introduces.length > 0) {
const tempH = content.height / row; this.btn_preview.active = true
const disY = tempH * 0.95; } else {
const baseY = (row - 1) * 0.5 * disY; this.btn_preview.active = false
let rowCount = 0;
const disX = 40;
let colCount = 0;
let lastX = 0;
for (let i=0; i<itemArr.length; i++) {
const item = itemArr[i];
item.x = lastX + disX;
item.y = baseY - rowCount * disY;
lastX = item.x + item.width * item.scale;
content.addChild(itemArr[i]);
const tempW = Math.abs(item.x + disX + item.width / 2 * item.scale);
content.width = lastX + disX;
rowCount++;
if (rowCount >= row) {
rowCount = 0;
colCount++;
}
}
}
onClickPreview() {
this.LessonBrief.active = true
this.LessonBrief.getComponent("LessonBrief").setData(this.configData.introduces)
this.LessonBrief.getComponent("LessonBrief").refreshUI()
} }
}
listItem;
getUnitItemDataArr(dataArr) {
let listItem = this.listItem;
let subItem = cc.find("sub_item", listItem);
let listLabel = cc.find("label", listItem);
const itemArr = [];
for (let i = 0; i < dataArr.length; i++) {
const data = dataArr[i];
const itemNode = cc.instantiate(listItem);
itemNode['data'] = data;
itemNode.removeAllChildren();
const subArr = data.children;
const baseX = subItem.x;
const baseY = subItem.y;
const disX = 530;
let lastX = 0;
for (let j = 0; j < subArr.length; j++) {
const subNode = cc.instantiate(subItem)
subNode.x = baseX + j * disX;
subNode.y = baseY;
itemNode.addChild(subNode);
lastX = subNode.x;
const subData = subArr[j];
getSprNodeByUrl(subData.cover, (spr) => {
const sprNode = spr.node;
const picLayer = cc.find("mask/pic_frame", subNode);
picLayer.addChild(sprNode);
setSprNodeMaxLen(sprNode, picLayer.width, picLayer.height, true);
})
onClickMyWork(evt: cc.Event) { const pointBase = cc.find("point", subNode);
const syllabusId = this.node['syllabus_pid'] const pointLayer = cc.find("pint_layer", subNode);
if (!syllabusId) { const pointDisX = 57;
global.middleLayer.showTips(`未制作`); pointBase.opacity = 0;
return;
for (let k = 0; k < 8; k++) {
const clsData = subData.children[k];
const pointNode = cc.instantiate(pointBase);
pointNode.opacity = 255;
if (clsData) {
const finished = this.syllabusLockStatusStore[clsData.id] === 'finished';
if (!finished) {
const finishIcon = cc.find("blue", pointNode);
finishIcon.active = false;
}
} else {
const finishIcon = cc.find("blue", pointNode);
finishIcon.active = false;
}
pointLayer.addChild(pointNode);
pointNode.x = pointBase.x + k * pointDisX;
} }
const vc = this.Viewer.getComponent("PageViewer");
vc[`libraryVideoPage`].attr({syllabus_pid: this.node['syllabus_pid'], showMyWork:true}); // 为方便测试,先全部解锁
vc[`libraryVideoPage`].attr({fromPage: "UnitPage"}); const lockCover = cc.find("bg_cover_mask", subNode);
vc.toView(`LibraryVideoPage`, "rightIn"); lockCover.active = false;
// 下面是正常解锁逻辑
// const unLock = this.syllabusLockStatusStore[subData.id]==='unlock';
// lockCover.active = !unLock;
// if (i==0&&j==0) {
// // 说明是第一个
// lockCover.active = false;
// }
const labelNode = cc.find("label", subNode);
const label = labelNode.getComponent(cc.Label);
label.string = subData.name;
subNode.addComponent(cc.Button);
subNode.on("click", () => {
const vc = this.Viewer.getComponent("PageViewer");
vc.missionPage.attr({ syllabus_pid: subData.id });
vc.missionPage.attr({ pData: subData });
vc.toView("MissionPage", "rightIn")
})
}
const labelNode = cc.instantiate(listLabel);
const label = labelNode.getComponent(cc.Label);
label.string = data.name//"Unit " + (i + 1);
itemNode.addChild(labelNode);
// if (subArr.length > 2) {
itemNode.width = lastX + 360;
setTimeout(() => {
itemNode.width = lastX + 360;
}, 1);
// }
itemArr.push(itemNode);
}
return itemArr;
}
initScrollViewItem(sv, itemArr, row = 1) {
sv.scrollToTop();
const content = sv.content;
content.width = sv.node.width;
content.height = sv.node.height;
content.removeAllChildren();
const tempH = content.height / row;
const disY = tempH * 0.95;
const baseY = (row - 1) * 0.5 * disY;
let rowCount = 0;
const disX = 40;
let colCount = 0;
let lastX = 0;
for (let i = 0; i < itemArr.length; i++) {
const item = itemArr[i];
item.x = lastX + disX;
item.y = baseY - rowCount * disY;
lastX = item.x + item.width * item.scale;
content.addChild(itemArr[i]);
const tempW = Math.abs(item.x + disX + item.width / 2 * item.scale);
content.width = lastX + disX;
rowCount++;
if (rowCount >= row) {
rowCount = 0;
colCount++;
}
}
}
onClickPreview() {
this.LessonBrief.active = true
this.LessonBrief.getComponent("LessonBrief").setData(this.configData.introduces)
this.LessonBrief.getComponent("LessonBrief").refreshUI()
}
onClickMyWork(evt: cc.Event) {
const syllabusId = this.node['syllabus_pid']
if (!syllabusId) {
global.middleLayer.showTips(`未制作`);
return;
} }
// update (dt) {} const vc = this.Viewer.getComponent("PageViewer");
vc[`libraryVideoPage`].attr({ syllabus_pid: this.node['syllabus_pid'], showMyWork: true });
vc[`libraryVideoPage`].attr({ fromPage: "UnitPage" });
vc.toView(`LibraryVideoPage`, "rightIn");
}
// update (dt) {}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment