Commit 0504c46b authored by Tt's avatar Tt

feat(play): 实现问题图片展示并优化列表初始化逻辑

parent 528f51a3
...@@ -496,14 +496,17 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -496,14 +496,17 @@ export class PlayComponent implements OnInit, OnDestroy {
} }
initListItem() { initListItem() {
// // 创建list_item节点作为滑动列表容器 // 获取questions数据
const questions = this.g_formData.questions || [];
console.log(questions)
const totalItems = questions.length;
// 创建list_item节点作为滑动列表容器
const listItem = this.g_cartoon.createCartoonElement('list_item', 'ShapeRectNew'); const listItem = this.g_cartoon.createCartoonElement('list_item', 'ShapeRectNew');
// 设置固定高度为800 // 设置固定高度为600
const listHeight = 600; const listHeight = 600;
// 每列最多2个子节点 // 每列最多2个子节点
const maxItemsPerColumn = 2; const maxItemsPerColumn = 2;
// 子节点数量
const totalItems = 20;
// 计算需要的列数 // 计算需要的列数
const columnsNeeded = Math.ceil(totalItems / maxItemsPerColumn); const columnsNeeded = Math.ceil(totalItems / maxItemsPerColumn);
// 每个子节点的宽度和高度 // 每个子节点的宽度和高度
...@@ -516,7 +519,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -516,7 +519,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 初始化list_item - 将锚点设置为左侧 (0, 0.5) // 初始化list_item - 将锚点设置为左侧 (0, 0.5)
listItem.ref.init(null, 0, 0.5); listItem.ref.init(null, 0, 0.5);
listItem.ref.x = 0;//listWidth / 2; // 设置初始位置,使列表居中显示 listItem.ref.x = 0;
listItem.ref.y = this.g_canvasHeight / 2 + 100; listItem.ref.y = this.g_canvasHeight / 2 + 100;
listItem.ref.fillColor = 'rgba(231, 193, 193, 0.5)'; listItem.ref.fillColor = 'rgba(231, 193, 193, 0.5)';
// 设置list_item的宽高 // 设置list_item的宽高
...@@ -531,6 +534,8 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -531,6 +534,8 @@ export class PlayComponent implements OnInit, OnDestroy {
// 创建子节点 // 创建子节点
for (let i = 0; i < totalItems; i++) { for (let i = 0; i < totalItems; i++) {
const questionData = questions[i];
// 计算当前子节点所在的列和行 // 计算当前子节点所在的列和行
const column = Math.floor(i / maxItemsPerColumn); const column = Math.floor(i / maxItemsPerColumn);
const row = i % maxItemsPerColumn; const row = i % maxItemsPerColumn;
...@@ -543,6 +548,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -543,6 +548,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 存储数据索引和选中状态 // 存储数据索引和选中状态
itemNode.ref.dataIndex = i; itemNode.ref.dataIndex = i;
itemNode.ref.isSelected = false; itemNode.ref.isSelected = false;
itemNode.ref.data = questionData; // 存储问题数据
// 计算子节点位置 - 基于左锚点的坐标计算 // 计算子节点位置 - 基于左锚点的坐标计算
// X坐标:从左到右排列,考虑间距,基于listItem的左边界 // X坐标:从左到右排列,考虑间距,基于listItem的左边界
...@@ -555,19 +561,29 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -555,19 +561,29 @@ export class PlayComponent implements OnInit, OnDestroy {
itemNode.ref.y = itemY; itemNode.ref.y = itemY;
itemNode.ref.scaleX = itemWidth / itemNode.ref.width; itemNode.ref.scaleX = itemWidth / itemNode.ref.width;
itemNode.ref.scaleY = itemHeight / itemNode.ref.height; itemNode.ref.scaleY = itemHeight / itemNode.ref.height;
// 创建图片元素显示question的image
// 为每个itemNode添加序号文字子节点 if (questionData.image) {
const textElement = this.g_cartoon.createCartoonElement(`list_item_text_${i}`, 'Label'); const imageElement = this.g_cartoon.createCartoonElement(`list_item_image_${i}`, 'MySprite');
// 设置文字内容为对应的序号(从1开始) const img = this.g_cartoon.images.get(questionData.image)
textElement.ref.text = (i + 1).toString(); this.g_cartoon.preloadImage(questionData.image).then((img: any) => {
// 设置字号为80,颜色为黑色 // 图片加载完成后初始化sprite
textElement.ref.fontSize = 80 * this.g_mapScale; imageElement.ref.init(img);
textElement.ref.fontColor = '#000000'; // 设置图片位置居中
// 设置文字位置与itemNode相同 imageElement.ref.x = 0;
textElement.ref.x = 0//itemX; imageElement.ref.y = 0;
textElement.ref.y = 0//itemY; // 根据背景卡片尺寸调整图片大小,保持适当的边距
// 设置文本对齐方式为居中 const padding = 20; // 边距
textElement.ref.textAlign = 'center'; const maxWidth = itemWidth - padding * 2;
const maxHeight = itemHeight - padding * 2;
const scaleX = maxWidth / img.width;
const scaleY = maxHeight / img.height;
const scale = Math.min(scaleX, scaleY); // 保持宽高比
imageElement.ref.scaleX = scale;
imageElement.ref.scaleY = scale;
})
// 将图片元素添加到itemNode
itemNode.ref.addChild(imageElement.ref);
}
// 添加选择标记(dui_hao图片) // 添加选择标记(dui_hao图片)
const selectMarkElement = this.g_cartoon.createCartoonElement(`list_item_select_${i}`, 'MySprite'); const selectMarkElement = this.g_cartoon.createCartoonElement(`list_item_select_${i}`, 'MySprite');
...@@ -591,8 +607,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -591,8 +607,7 @@ export class PlayComponent implements OnInit, OnDestroy {
} }
}; };
// 渲染子节点、文字和选择标记 // 渲染子节点和选择标记
itemNode.ref.addChild(textElement.ref);
itemNode.ref.addChild(selectMarkElement.ref); itemNode.ref.addChild(selectMarkElement.ref);
listItem.ref.addChild(itemNode.ref); listItem.ref.addChild(itemNode.ref);
} }
...@@ -1536,12 +1551,10 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1536,12 +1551,10 @@ export class PlayComponent implements OnInit, OnDestroy {
// 初始化默认数据 // 初始化默认数据
// ---------------------------------- // ----------------------------------
initDefaultData() { initDefaultData() {
if (Object.keys(this.g_formData).length === 0) { console.log("Init default form data")
// console.log("Init default form data")
this.g_formData = defaultFormData; this.g_formData = defaultFormData;
console.log("this.g_formData", this.g_formData) console.log("this.g_formData", this.g_formData)
} }
}
// ---------------------------------- // ----------------------------------
......
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