Commit e5f1c4f8 authored by limingzhe's avatar limingzhe

feat: 增加语法评测

parent 404a936f
......@@ -841,48 +841,67 @@ export class RichTextOld extends Label {
export class RichText extends Label {
topH = 8;
topH = 0;
disH = 10;
offW = 10;
private rowCount = 1;
row = 1;
textBaseline = "middle";
isShowWordBg = false;
wordBgData;
constructor(ctx?: any) {
super(ctx);
}
// this.dataArr = dataArr;
}
getLineNum() {
this.drawSelf();
return this.rowCount;
return this.row;
}
getAreaHeight() {
this.drawSelf();
const tmpTextH = this.rowCount * this.fontSize;
const tmpDisH = (this.rowCount - 1) * this.disH
const tmpTextH = this.row * this.fontSize;
const tmpDisH = (this.row - 1) * this.disH
return tmpTextH + tmpDisH;
}
// addLabelMask(mask) {
// this._labelMaskArr.push(mask);
// this._createLabelOffCtx();
// }
getSubTextRectGroup(text, targetIndex = 0) {
const rectGroup = [];
const subTextArr = text.split(' ');
let baseIndex = targetIndex;
console.log('subTextArr: ', subTextArr);
for (let i=0; i<subTextArr.length; i++) {
const subtText = subTextArr[i];
const subData = this.getSubTextRect(subtText, baseIndex)
if (subData) {
rectGroup.push(subData);
baseIndex = Number( subData.index );
}
}
// _createLabelOffCtx() {
// if (!this._labelOffCtx) {
// this._labelOffCanvas = document.createElement('canvas'); // 创建一个新的canvas
// this._labelOffCanvas.width = this.width; // 创建一个正好包裹住一个粒子canvas
// this._labelOffCanvas.height = this.height;
// this._labelOffCtx = this._labelOffCanvas.getContext('2d');
// }
// }
return rectGroup;
}
getSubTextRect(subText, targetIndex=0) {
subText = subText.trim();
if (!subText) {
return;
}
this.isShowWordBg = true;
this.update();
getSubTextRect(subText) {
const tmpLabel = new RichText();
tmpLabel.fontSize = this.fontSize;
......@@ -892,22 +911,31 @@ export class RichText extends Label {
tmpLabel.fontWeight = this.fontWeight;
tmpLabel.width = this.width;
tmpLabel.height = this.height;
console.log('subText: ', subText);
console.log('this.text: ', this.text);
// const indexArr = searchSubStr(this.text, subText);
// console.log('indexArr: ', indexArr);
// const index = indexArr[targetIndex];
const index = this.text.indexOf(subText);
tmpLabel.text = this.text.substring(0, index);
tmpLabel.refreshSize();
const index = this.text.indexOf(subText, targetIndex);
const x = tmpLabel.width;
console.log('index: ', index);
if (index == -1) {
return;
}
// console.log('this.wordBgData: ', this.wordBgData);
// const index = this.text.indexOf(subText);
// console.log('!!!index: ', index);
tmpLabel.text = subText;
tmpLabel.refreshSize();
const data = this.wordBgData[index.toString()];
// console.log('!!!wordBgData: ', this.wordBgData);
// console.log('!!!data: ', data);
const width = tmpLabel.width;
return data;
const y = this.fontSize / 2;
const height = this.fontSize;
return {x, y, width, height}
}
......@@ -930,7 +958,6 @@ export class RichText extends Label {
curCtx = this._offCtx;
}
curCtx.font = `${this.fontSize}px ${this.fontName}`;
curCtx.textAlign = this.textAlign;
curCtx.textBaseline = this.textBaseline;
......@@ -943,40 +970,68 @@ export class RichText extends Label {
const chr = this.text.split(' ');
let temp = '';
const rows = [];
const row = [];
const w = selfW - this.offW * 2;
const disH = (this.fontSize + this.disH) * this.scaleY;
let isPushWordData = false;
if (this.isShowWordBg && !this.wordBgData) {
this.wordBgData = {};
isPushWordData = true;
}
let wordIndex = -1;
for (const c of chr) {
if (isPushWordData) {
}
for (const c of chr) {
if (curCtx.measureText(temp).width < w && curCtx.measureText(temp + (c)).width <= w) {
if (c == '\n') {
row.push(temp);
temp = '';
} else if (curCtx.measureText(temp).width < w && curCtx.measureText(temp + (c)).width <= w) {
temp += ' ' + c;
} else {
rows.push(temp);
row.push(temp);
temp = ' ' + c;
}
if (isPushWordData) {
wordIndex = this.text.indexOf(c, wordIndex+1);
const key = wordIndex.toString();
const rectX = curCtx.measureText(temp).width
const rectY = ( row.length ) * disH / this.scaleY
const rectW = curCtx.measureText(c).width;
const rectH = this.fontSize;
this.wordBgData[key] = {rect: {x: rectX, y: rectY, width: rectW, height: rectH}, text: c, index: wordIndex}
}
}
rows.push(temp);
row.push(temp);
this.rowCount = rows.length;
this.row = row.length;
const x = 0;
const y = this.topH;
const y = this.topH//-row.length * disH / 2;
// for (let b = 0 ; b < row.length; b++) {
// curCtx.strokeText(row[b], x, y + ( b + 1 ) * disH ); // 每行字体y坐标间隔20
// }
if (this._outlineFlag) {
curCtx.lineWidth = this._outLineWidth;
curCtx.strokeStyle = this._outLineColor;
for (let b = 0 ; b < rows.length; b++) {
curCtx.strokeText(rows[b], x, y + ( b + 0 ) * disH / this.scaleY ); // 每行字体y坐标间隔20
for (let b = 0 ; b < row.length; b++) {
curCtx.strokeText(row[b], x, y + ( b + 0 ) * disH / this.scaleY ); // 每行字体y坐标间隔20
}
// curCtx.strokeText(this.text, 0, 0);
}
for (let b = 0 ; b < rows.length; b++) {
curCtx.fillText(rows[b], x, y + ( b + 0 ) * disH / this.scaleY ); // 每行字体y坐标间隔20
// curCtx.fillStyle = '#ff7600';
for (let b = 0 ; b < row.length; b++) {
curCtx.fillText(row[b], x, y + ( b + 0 ) * disH / this.scaleY ); // 每行字体y坐标间隔20
}
......@@ -991,7 +1046,6 @@ export class RichText extends Label {
this._maskSprArr[i].ctx = this._offCtx;
this._maskSprArr[i].update();
}
// console.log('aaaaa1');
}
......@@ -1002,10 +1056,9 @@ export class RichText extends Label {
}
drawSelf() {
super.drawSelf();
this.drawText();
}
}
......
This diff is collapsed.
......@@ -32,6 +32,8 @@ const res = [
['info_icon', "assets/play/info_icon.png"],
['info_bg', "assets/play/info_bg.png"],
['ai_icon', "assets/play/ai_icon.png"],
];
......@@ -42,6 +44,8 @@ const resAudio = [
['submit', "assets/play/music/submit.mp3"],
['more', "assets/play/music/more.mp3"],
];
export {res, resAudio};
......
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