\g可以切换字体

This commit is contained in:
ckcz123 2020-07-05 10:59:21 +08:00
parent abaacfa754
commit ba64fb0cac
6 changed files with 44 additions and 22 deletions

View File

@ -1747,7 +1747,7 @@ ctx: 要绘制到的画布
content: 要绘制的内容;转义字符不允许保留 \t, \b 和 \f content: 要绘制的内容;转义字符不允许保留 \t, \b 和 \f
config: 绘制配置项,目前暂时包含如下内容(均为可选) config: 绘制配置项,目前暂时包含如下内容(均为可选)
left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右 left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右
fontSize字体大小lineHeight行高time打字机间隔 fontSize字体大小lineHeight行高time打字机间隔font默认字体
返回值:绘制信息 返回值:绘制信息
calWidth: fn(name: string|CanvasRenderingContext2D, text: string, font?: string) -> number calWidth: fn(name: string|CanvasRenderingContext2D, text: string, font?: string) -> number

View File

@ -3561,7 +3561,7 @@ var terndefs_f6783a0a_522d_417e_8407_94c67b692e50 = [
"!type": "fn(name: string|CanvasRenderingContext2D, x?: number, y?: number, width?: number, height?: number)" "!type": "fn(name: string|CanvasRenderingContext2D, x?: number, y?: number, width?: number, height?: number)"
}, },
"drawTextContent": { "drawTextContent": {
"!doc": "绘制一段文字到某个画布上面<br/>ctx: 要绘制到的画布<br/>content: 要绘制的内容;转义字符不允许保留 \\t, \\b 和 \\f<br/>config: 绘制配置项,目前暂时包含如下内容(均为可选)<br/>left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右<br/>fontSize字体大小lineHeight行高time打字机间隔<br/>返回值:绘制信息", "!doc": "绘制一段文字到某个画布上面<br/>ctx: 要绘制到的画布<br/>content: 要绘制的内容;转义字符不允许保留 \\t, \\b 和 \\f<br/>config: 绘制配置项,目前暂时包含如下内容(均为可选)<br/>left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右<br/>fontSize字体大小lineHeight行高time打字机间隔font字体名<br/>返回值:绘制信息",
"!type": "fn(ctx: string|CanvasRenderingContext2D, content: string, config: ?)" "!type": "fn(ctx: string|CanvasRenderingContext2D, content: string, config: ?)"
}, },
"calWidth": { "calWidth": {

View File

@ -104,7 +104,7 @@ editor_blockly = function () {
MotaActionFunctions.parse( MotaActionFunctions.parse(
eval('obj=' + codeAreaHL.getValue().replace(/[<>&]/g, function (c) { eval('obj=' + codeAreaHL.getValue().replace(/[<>&]/g, function (c) {
return {'<': '&lt;', '>': '&gt;', '&': '&amp;'}[c]; return {'<': '&lt;', '>': '&gt;', '&': '&amp;'}[c];
}).replace(/\\(r|f|i|c|d|e|z)/g,'\\\\$1')), }).replace(/\\(r|f|i|c|d|e|g|z)/g,'\\\\$1')),
editor_blockly.entryType editor_blockly.entryType
); );
} }
@ -190,7 +190,7 @@ editor_blockly = function () {
return; return;
} }
var code = Blockly.JavaScript.workspaceToCode(editor_blockly.workspace); var code = Blockly.JavaScript.workspaceToCode(editor_blockly.workspace);
code = code.replace(/\\(i|c|d|e|z)/g, '\\\\$1'); code = code.replace(/\\(i|c|d|e|g|z)/g, '\\\\$1');
eval('var obj=' + code); eval('var obj=' + code);
if (this.checkAsync(obj) && confirm("警告!存在不等待执行完毕的事件但却没有用【等待所有异步事件处理完毕】来等待" + if (this.checkAsync(obj) && confirm("警告!存在不等待执行完毕的事件但却没有用【等待所有异步事件处理完毕】来等待" +
"它们执行完毕,这样可能会导致录像检测系统出问题。\n你要返回修改么")) return; "它们执行完毕,这样可能会导致录像检测系统出问题。\n你要返回修改么")) return;
@ -272,7 +272,7 @@ editor_blockly = function () {
return true; return true;
} }
var code = "[" + Blockly.JavaScript.blockToCode(b).replace(/\\(i|c|d|e|z)/g, '\\\\$1') + "]"; var code = "[" + Blockly.JavaScript.blockToCode(b).replace(/\\(i|c|d|e|g|z)/g, '\\\\$1') + "]";
eval("var obj="+code); eval("var obj="+code);
if (obj.length == 0) return true; if (obj.length == 0) return true;
obj = obj[0]; obj = obj[0];
@ -654,6 +654,7 @@ editor_blockly = function () {
namesObj.allColors = ["aqua青色", "black黑色", "blue蓝色", "fuchsia品红色", "gray灰色", "green深绿色", "lime绿色", namesObj.allColors = ["aqua青色", "black黑色", "blue蓝色", "fuchsia品红色", "gray灰色", "green深绿色", "lime绿色",
"maroon深红色", "navy深蓝色", "gold金色", "olive黄褐色", "orange橙色", "purple品红色", "maroon深红色", "navy深蓝色", "gold金色", "olive黄褐色", "orange橙色", "purple品红色",
"red红色", "silver淡灰色", "teal深青色", "white白色", "yellow黄色"]; "red红色", "silver淡灰色", "teal深青色", "white白色", "yellow黄色"];
namesObj.allFonts = [main.styles.font].concat(main.fonts);
namesObj.allDoors = ["this"].concat(Object.keys(maps_90f36752_8815_4be8_b32b_d7fad1d0542e) namesObj.allDoors = ["this"].concat(Object.keys(maps_90f36752_8815_4be8_b32b_d7fad1d0542e)
.map(function (key) { return maps_90f36752_8815_4be8_b32b_d7fad1d0542e[key]; }) .map(function (key) { return maps_90f36752_8815_4be8_b32b_d7fad1d0542e[key]; })
.filter(function (one) { return one.doorInfo != null; }) .filter(function (one) { return one.doorInfo != null; })
@ -708,9 +709,18 @@ editor_blockly = function () {
} }
} }
// 对\g进行补全
index = content.lastIndexOf("\\g[");
if (index >= 0) {
var after = content.substring(index + 3);
if (after.indexOf("]") < 0) {
return filter(namesObj.allFonts, after);
}
}
// 对\进行补全! // 对\进行补全!
if (content.charAt(content.length - 1) == '\\') { if (content.charAt(content.length - 1) == '\\') {
return ["n换行", "f立绘", "r变色", "i图标", "z暂停打字", "t标题图标", "b对话框", "c字体大小", "d粗体", "e斜体"]; return ["n换行", "f立绘", "r变色", "i图标", "z暂停打字", "t标题图标", "b对话框", "c字体大小", "d粗体", "e斜体", "g字体"];
} }
return []; return [];

View File

@ -450,7 +450,7 @@ function omitedcheckUpdateFunction(event) {
} }
} }
try { try {
var code = Blockly.JavaScript.workspaceToCode(workspace).replace(/\\(i|c|d|e|z)/g, '\\\\$1'); var code = Blockly.JavaScript.workspaceToCode(workspace).replace(/\\(i|c|d|e|g|z)/g, '\\\\$1');
editor_blockly.setValue(code); editor_blockly.setValue(code);
} catch (error) { } catch (error) {
editor_blockly.setValue(String(error)); editor_blockly.setValue(String(error));

View File

@ -1036,11 +1036,11 @@ ui.prototype._getDrawableIconInfo = function (id) {
return [image,icon]; return [image,icon];
} }
ui.prototype._buildFont = function (fontSize, bold, italic) { ui.prototype._buildFont = function (fontSize, bold, italic, font) {
var textAttribute = core.status.textAttribute || core.initStatus.textAttribute, var textAttribute = core.status.textAttribute || core.initStatus.textAttribute,
globalAttribute = core.status.globalAttribute || core.initStatus.globalAttribute; globalAttribute = core.status.globalAttribute || core.initStatus.globalAttribute;
if (bold == null) bold = textAttribute.bold; if (bold == null) bold = textAttribute.bold;
return (bold?"bold ":"") + (italic?"italic ":"") + (fontSize || textAttribute.textfont) + "px " + globalAttribute.font; return (bold?"bold ":"") + (italic?"italic ":"") + (fontSize || textAttribute.textfont) + "px " + (font || globalAttribute.font);
} }
////// 绘制一段文字到某个画布上面 ////// 绘制一段文字到某个画布上面
@ -1048,11 +1048,12 @@ ui.prototype._buildFont = function (fontSize, bold, italic) {
// content要绘制的内容转义字符目前只允许留 \n, \r[...], \i[...], \c[...], \d, \e // content要绘制的内容转义字符目前只允许留 \n, \r[...], \i[...], \c[...], \d, \e
// config绘制配置项目前暂时包含如下内容均为可选 // config绘制配置项目前暂时包含如下内容均为可选
// left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右 // left, top起始点位置maxWidth单行最大宽度color默认颜色align左中右
// fontSize字体大小lineHeight行高time打字机间隔 // fontSize字体大小lineHeight行高time打字机间隔font字体类型
ui.prototype.drawTextContent = function (ctx, content, config) { ui.prototype.drawTextContent = function (ctx, content, config) {
ctx = core.getContextByName(ctx); ctx = core.getContextByName(ctx);
// 设置默认配置项 // 设置默认配置项
var textAttribute = core.status.textAttribute || core.initStatus.textAttribute; var textAttribute = core.status.textAttribute || core.initStatus.textAttribute;
var globalAttribute = core.status.globalAttribute || core.initStatus.globalAttribute;
config = core.clone(config || {}); config = core.clone(config || {});
config.left = config.left || 0; config.left = config.left || 0;
config.right = config.left + (config.maxWidth == null ? (ctx != null ? ctx.canvas.width : core.__PIXELS__) : config.maxWidth) config.right = config.left + (config.maxWidth == null ? (ctx != null ? ctx.canvas.width : core.__PIXELS__) : config.maxWidth)
@ -1063,6 +1064,7 @@ ui.prototype.drawTextContent = function (ctx, content, config) {
config.align = config.align || textAttribute.align || "left"; config.align = config.align || textAttribute.align || "left";
config.fontSize = config.fontSize || textAttribute.textfont; config.fontSize = config.fontSize || textAttribute.textfont;
config.lineHeight = config.lineHeight || (config.fontSize * 1.3); config.lineHeight = config.lineHeight || (config.fontSize * 1.3);
config.defaultFont = config.font = config.font || globalAttribute.font;
config.time = config.time || 0; config.time = config.time || 0;
config.interval = config.interval == null ? (textAttribute.interval || 0) : config.interval; config.interval = config.interval == null ? (textAttribute.interval || 0) : config.interval;
@ -1080,7 +1082,7 @@ ui.prototype.drawTextContent = function (ctx, content, config) {
// 创建一个新的临时画布 // 创建一个新的临时画布
var tempCtx = core.createCanvas('__temp__', 0, 0, ctx==null?1:ctx.canvas.width, ctx==null?1:ctx.canvas.height, -1); var tempCtx = core.createCanvas('__temp__', 0, 0, ctx==null?1:ctx.canvas.width, ctx==null?1:ctx.canvas.height, -1);
tempCtx.textBaseline = 'top'; tempCtx.textBaseline = 'top';
tempCtx.font = this._buildFont(config.fontSize, config.bold, config.italic); tempCtx.font = this._buildFont(config.fontSize, config.bold, config.italic, config.font);
tempCtx.fillStyle = config.color; tempCtx.fillStyle = config.color;
config = this._drawTextContent_draw(ctx, tempCtx, content, config); config = this._drawTextContent_draw(ctx, tempCtx, content, config);
core.deleteCanvas('__temp__'); core.deleteCanvas('__temp__');
@ -1157,21 +1159,17 @@ ui.prototype._drawTextContent_drawChar = function (tempCtx, content, config, ch)
if (ch == '\\') { if (ch == '\\') {
var c = content.charAt(config.index); var c = content.charAt(config.index);
if (c == 'i') return this._drawTextContent_drawIcon(tempCtx, content, config); if (c == 'i') return this._drawTextContent_drawIcon(tempCtx, content, config);
if (c == 'c') return this._drawTextContent_changeFont(tempCtx, content, config); if (c == 'c') return this._drawTextContent_changeFontSize(tempCtx, content, config);
if (c == 'd' || c == 'e') { if (c == 'd' || c == 'e') {
config.index++; config.index++;
if (c == 'd') config.bold = !config.bold; if (c == 'd') config.bold = !config.bold;
if (c == 'e') config.italic = !config.italic; if (c == 'e') config.italic = !config.italic;
tempCtx.font = this._buildFont(config.currfont, config.bold, config.italic); tempCtx.font = this._buildFont(config.currfont, config.bold, config.italic, config.font);
return true; return true;
} }
if (c == 'g') return this._drawTextContent_changeFont(tempCtx, content, config);
if (c == 'z') return this._drawTextContent_emptyChar(tempCtx, content, config); if (c == 'z') return this._drawTextContent_emptyChar(tempCtx, content, config);
} }
// \\e 斜体切换
if (ch == '\\' && content.charAt(config.index)=='e') {
config.italic = !config.italic;
tempCtx.font = this._buildFont(config.fontSize, config.bold, config.italic);
}
// 检查是不是自动换行 // 检查是不是自动换行
var charwidth = core.calWidth(tempCtx, ch) + config.interval; var charwidth = core.calWidth(tempCtx, ch) + config.interval;
if (config.maxWidth != null && config.offsetX + charwidth > config.maxWidth) { if (config.maxWidth != null && config.offsetX + charwidth > config.maxWidth) {
@ -1228,7 +1226,7 @@ ui.prototype._drawTextContent_changeColor = function (tempCtx, content, config)
return this._drawTextContent_next(tempCtx, content, config); return this._drawTextContent_next(tempCtx, content, config);
} }
ui.prototype._drawTextContent_changeFont = function (tempCtx, content, config) { ui.prototype._drawTextContent_changeFontSize = function (tempCtx, content, config) {
config.index++; config.index++;
// 检查是不是 [] // 检查是不是 []
var index = config.index, index2; var index = config.index, index2;
@ -1240,7 +1238,21 @@ ui.prototype._drawTextContent_changeFont = function (tempCtx, content, config) {
} }
else config.currfont = config.fontSize; else config.currfont = config.fontSize;
config.lineMaxHeight = Math.max(config.lineMaxHeight, config.currfont + config.lineMargin); config.lineMaxHeight = Math.max(config.lineMaxHeight, config.currfont + config.lineMargin);
tempCtx.font = this._buildFont(config.currfont, config.bold, config.italic); tempCtx.font = this._buildFont(config.currfont, config.bold, config.italic, config.font);
return this._drawTextContent_next(tempCtx, content, config);
}
ui.prototype._drawTextContent_changeFont = function (tempCtx, content, config) {
config.index++;
// 检查是不是 []
var index = config.index, index2;
if (content.charAt(index) == '[' && ((index2=content.indexOf(']', index))>=0)) {
var str = content.substring(index+1, index2);
if (str=="") config.font = config.defaultFont;
else config.font = str;
config.index = index2 + 1;
} else config.font = config.defaultFont;
tempCtx.font = this._buildFont(config.currfont, config.bold, config.italic, config.font);
return this._drawTextContent_next(tempCtx, content, config); return this._drawTextContent_next(tempCtx, content, config);
} }
@ -1295,7 +1307,7 @@ ui.prototype.getTextContentHeight = function (content, config) {
} }
ui.prototype._getRealContent = function (content) { ui.prototype._getRealContent = function (content) {
return content.replace(/(\r|\\(r|c|d|e|z))(\[.*?])?/g, "").replace(/(\\i)(\[.*?])?/g, "占1"); return content.replace(/(\r|\\(r|c|d|e|g|z))(\[.*?])?/g, "").replace(/(\\i)(\[.*?])?/g, "占1");
} }
////// 绘制一个对话框 ////// ////// 绘制一个对话框 //////

2
runtime.d.ts vendored
View File

@ -2169,7 +2169,7 @@ declare class ui {
* @param content \t, \b \f * @param content \t, \b \f
* @param config * @param config
* left, topmaxWidthcoloralign * left, topmaxWidthcoloralign
* fontSizelineHeighttime * fontSizelineHeighttimefont
* @returns * @returns
*/ */
drawTextContent(ctx: string | CanvasRenderingContext2D, content: string, config: any): any drawTextContent(ctx: string | CanvasRenderingContext2D, content: string, config: any): any