mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 15:52:40 +08:00
Merge branch 'master' of github.com:cocos-creator/engine
This commit is contained in:
@@ -55,6 +55,19 @@ var getAudioFromId = function (id) {
|
||||
return id2audio[id];
|
||||
};
|
||||
|
||||
/**
|
||||
* !#en cc.audioEngine is the singleton object, it provide simple audio APIs.
|
||||
* !#zh
|
||||
* cc.audioengine是单例对象。<br/>
|
||||
* 主要用来播放音频,播放的时候会返回一个 audioID,之后都可以通过这个 audioID 来操作这个音频对象。<br/>
|
||||
* 不使用的时候,请使用 cc.audioEngine.uncache(filePath); 进行资源释放 <br/>
|
||||
* 注意:<br/>
|
||||
* 在 Android 系统浏览器上,不同浏览器,不同版本的效果不尽相同。<br/>
|
||||
* 比如说:大多数浏览器都需要用户物理交互才可以开始播放音效,有一些不支持 WebAudio,<br/>
|
||||
* 有一些不支持多音轨播放。总之如果对音乐依赖比较强,请做尽可能多的测试。
|
||||
* @class audioEngine
|
||||
* @static
|
||||
*/
|
||||
var audioEngine = {
|
||||
|
||||
AudioState: Audio.State,
|
||||
@@ -74,7 +87,7 @@ var audioEngine = {
|
||||
* @return {Number} audioId
|
||||
* @example
|
||||
* //example
|
||||
* cc.audioEngine.(path, false, 0.5);
|
||||
* var audioID = cc.audioEngine.play(path, false, 0.5);
|
||||
*/
|
||||
play: function (filePath, loop, volume/*, profile*/) {
|
||||
var audio = getAudioFromPath(filePath);
|
||||
@@ -250,9 +263,10 @@ var audioEngine = {
|
||||
* !#en Pause playing audio.
|
||||
* !#zh 暂停正在播放音频。
|
||||
* @method pause
|
||||
* @param {Number} audioID - The return value of function play.
|
||||
* @example
|
||||
* //example
|
||||
* cc.audioEngine.pause(id);
|
||||
* cc.audioEngine.pause(audioID);
|
||||
*/
|
||||
pause: function (audioID) {
|
||||
var audio = getAudioFromId(audioID);
|
||||
@@ -356,6 +370,7 @@ var audioEngine = {
|
||||
* !#en Set up an audio can generate a few examples.
|
||||
* !#zh 设置一个音频可以设置几个实例
|
||||
* @method setMaxAudioInstance
|
||||
* @param {Number} num a number of instances to be created from within an audio
|
||||
* @example
|
||||
* //example
|
||||
* cc.audioEngine.setMaxAudioInstance(20);
|
||||
@@ -368,6 +383,7 @@ var audioEngine = {
|
||||
* !#en Getting audio can produce several examples.
|
||||
* !#zh 获取一个音频可以设置几个实例
|
||||
* @method getMaxAudioInstance
|
||||
* @return {Number} a number of instances to be created from within an audio
|
||||
* @example
|
||||
* //example
|
||||
* cc.audioEngine.getMaxAudioInstance();
|
||||
@@ -453,4 +469,4 @@ var audioEngine = {
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = cc.audioEngine = audioEngine;
|
||||
module.exports = cc.audioEngine = audioEngine;
|
||||
|
||||
@@ -623,8 +623,8 @@ var Button = cc.Class({
|
||||
if(this._sprite) {
|
||||
this._sprite._sgNode.setState(0);
|
||||
}
|
||||
if(this.enableAutoGrayEffect) {
|
||||
if(! (this.transition === Transition.SPRITE && this.disabledSprite)) {
|
||||
if(this.enableAutoGrayEffect && this.transition !== Transition.COLOR) {
|
||||
if(!(this.transition === Transition.SPRITE && this.disabledSprite)) {
|
||||
if(this._sprite && !this.interactable) {
|
||||
this._sprite._sgNode.setState(1);
|
||||
}
|
||||
|
||||
@@ -153,6 +153,23 @@ var RichText = cc.Class({
|
||||
notify: function (oldValue) {
|
||||
if(this.lineHeight === oldValue) return;
|
||||
|
||||
this._layoutDirty = true;
|
||||
this._updateRichTextStatus();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* !#en The image atlas for the img tag. For each src value in the img tag, there should be a valid spriteFrame in the image atlas.
|
||||
* !#zh 对于 img 标签里面的 src 属性名称,都需要在 imageAtlas 里面找到一个有效的 spriteFrame,否则 img tag 会判定为无效。
|
||||
* @property {cc.SpriteAtlas} imageAtlas
|
||||
*/
|
||||
imageAtlas: {
|
||||
default: null,
|
||||
type: cc.SpriteAtlas,
|
||||
tooltip: 'i18n:COMPONENT.richtext.image_atlas',
|
||||
notify: function(oldValue) {
|
||||
if(this.imageAtlas === oldValue) return;
|
||||
|
||||
this._layoutDirty = true;
|
||||
this._updateRichTextStatus();
|
||||
}
|
||||
@@ -376,18 +393,24 @@ var RichText = cc.Class({
|
||||
} else {
|
||||
if (oldItem.style) {
|
||||
if (newItem.style) {
|
||||
if(oldItem.style.size != newItem.style.size
|
||||
|| oldItem.style.italic !== newItem.style.italic) {
|
||||
if(oldItem.style.size !== newItem.style.size
|
||||
|| oldItem.style.italic !== newItem.style.italic
|
||||
|| oldItem.style.isImage !== newItem.style.isImage) {
|
||||
return true;
|
||||
}
|
||||
if(oldItem.style.isImage === newItem.style.isImage) {
|
||||
if(oldItem.style.src !== newItem.style.src) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if(oldItem.style.size || oldItem.style.italic) {
|
||||
if(oldItem.style.size || oldItem.style.italic || oldItem.style.isImage) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (newItem.style) {
|
||||
if(newItem.style.size || newItem.style.italic) {
|
||||
if(newItem.style.size || newItem.style.italic || newItem.style.isImage) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -397,6 +420,70 @@ var RichText = cc.Class({
|
||||
return false;
|
||||
},
|
||||
|
||||
_onSpriteFrameLoaded: function (event, spriteFrame) {
|
||||
var newSpriteFrame;
|
||||
if(spriteFrame) {
|
||||
newSpriteFrame = spriteFrame;
|
||||
} else {
|
||||
newSpriteFrame = event.target;
|
||||
}
|
||||
var sprite = newSpriteFrame.__sprite;
|
||||
sprite.setSpriteFrame(newSpriteFrame);
|
||||
},
|
||||
|
||||
_applySpriteFrame: function (spriteFrame) {
|
||||
if(spriteFrame) {
|
||||
if(spriteFrame.textureLoaded()) {
|
||||
this._onSpriteFrameLoaded(null, spriteFrame);
|
||||
} else {
|
||||
spriteFrame.once('load', this._onSpriteFrameLoaded, this);
|
||||
spriteFrame.ensureLoadTexture();
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
_addRichTextImageElement: function (richTextElement) {
|
||||
var spriteFrameName = richTextElement.style.src;
|
||||
var spriteFrame = this.imageAtlas.getSpriteFrame(spriteFrameName);
|
||||
if(spriteFrame) {
|
||||
var sprite = new cc.Scale9Sprite();
|
||||
sprite.setAnchorPoint(cc.p(0, 0));
|
||||
spriteFrame.__sprite = sprite;
|
||||
this._sgNode.addChild(sprite);
|
||||
this._labelSegments.push(sprite);
|
||||
|
||||
var spriteRect = spriteFrame.getRect();
|
||||
var scaleFactor = 1;
|
||||
if(spriteRect.height > this.lineHeight) {
|
||||
scaleFactor = this.lineHeight / spriteRect.height;
|
||||
}
|
||||
if(this.maxWidth > 0) {
|
||||
if(this._lineOffsetX + spriteRect.width * scaleFactor > this.maxWidth) {
|
||||
this._updateLineInfo();
|
||||
}
|
||||
this._lineOffsetX += spriteRect.width * scaleFactor;
|
||||
|
||||
} else {
|
||||
this._lineOffsetX += spriteRect.width * scaleFactor;
|
||||
if(this._lineOffsetX > this._labelWidth) {
|
||||
this._labelWidth = this._lineOffsetX;
|
||||
}
|
||||
}
|
||||
this._applySpriteFrame(spriteFrame);
|
||||
sprite.setContentSize(cc.size(spriteRect.width * scaleFactor,
|
||||
spriteRect.height * scaleFactor));
|
||||
sprite._lineCount = this._lineCount;
|
||||
|
||||
if(richTextElement.style.event) {
|
||||
if(richTextElement.style.event.click) {
|
||||
sprite._clickHandler = richTextElement.style.event.click;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
cc.warn('Invalid RichText img tag! The sprite frame name can\'t be found in the ImageAtlas!');
|
||||
}
|
||||
},
|
||||
|
||||
_updateRichText: function () {
|
||||
if (!this.enabled) return;
|
||||
|
||||
@@ -424,6 +511,10 @@ var RichText = cc.Class({
|
||||
this._updateLineInfo();
|
||||
continue;
|
||||
}
|
||||
if(richTextElement.style && richTextElement.style.isImage && this.imageAtlas) {
|
||||
this._addRichTextImageElement(richTextElement);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
var multilineTexts = text.split("\n");
|
||||
|
||||
@@ -531,6 +622,10 @@ var RichText = cc.Class({
|
||||
|
||||
var positionY = (totalLineCount - lineCount) * this.lineHeight;
|
||||
|
||||
if(label instanceof cc.Scale9Sprite) {
|
||||
positionY += (this.lineHeight - label.getContentSize().height) / 2;
|
||||
}
|
||||
|
||||
label.setPositionY(positionY);
|
||||
|
||||
if (lineCount === nextLineIndex) {
|
||||
@@ -541,6 +636,8 @@ var RichText = cc.Class({
|
||||
},
|
||||
|
||||
_applyTextAttribute: function (label) {
|
||||
if(label instanceof cc.Scale9Sprite) return;
|
||||
|
||||
var index = label._styleIndex;
|
||||
label.setLineHeight(this.lineHeight);
|
||||
label.setVerticalAlign(VerticalAlign.CENTER);
|
||||
|
||||
@@ -611,8 +611,6 @@ var Sprite = cc.Class({
|
||||
expectedW = rect.width;
|
||||
expectedH = rect.height;
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
if (expectedW !== actualSize.width || expectedH !== actualSize.height) {
|
||||
|
||||
@@ -79,6 +79,7 @@ cc.HtmlTextParser.prototype = {
|
||||
var tagName;
|
||||
var nextSpace;
|
||||
var eventObj;
|
||||
var eventHanlderString;
|
||||
if (header) {
|
||||
tagName = header[0];
|
||||
attribute = attribute.substring(tagName.length).trim();
|
||||
@@ -101,17 +102,63 @@ cc.HtmlTextParser.prototype = {
|
||||
|
||||
//tag has event arguments
|
||||
if(nextSpace > -1) {
|
||||
var eventHanlderString = attribute.substring(nextSpace+1).trim();
|
||||
eventHanlderString = attribute.substring(nextSpace+1).trim();
|
||||
eventObj = this._processEventHandler(eventHanlderString);
|
||||
obj.event = eventObj;
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
header = attribute.match(/^(br(\s)*\/)/);
|
||||
if(header && header[0].length > 0) {
|
||||
tagName = header[0].trim();
|
||||
if(tagName.startsWith("br") && tagName[tagName.length-1] === "/") {
|
||||
obj.isNewLine = true;
|
||||
this._resultObjectArray.push({text: "", style: {newline: true}});
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
header = attribute.match(/^(img(\s)*src(\s)*=[^>]+\/)/);
|
||||
if(header && header[0].length > 0) {
|
||||
tagName = header[0].trim();
|
||||
if(tagName.startsWith("img") && tagName[tagName.length-1] === "/") {
|
||||
var srcBegin = tagName.indexOf('=');
|
||||
var remainingArgument = tagName.substring(srcBegin+1).trim();
|
||||
var imageSrc;
|
||||
var endQuotIndex = -1;
|
||||
var isValidImageTag = false;
|
||||
switch(remainingArgument[0]){
|
||||
case "'":
|
||||
endQuotIndex = remainingArgument.indexOf("'", 1);
|
||||
if(endQuotIndex > -1) {
|
||||
imageSrc = remainingArgument.substring(1, endQuotIndex);
|
||||
isValidImageTag = true;
|
||||
}
|
||||
break;
|
||||
case "\"":
|
||||
endQuotIndex = remainingArgument.indexOf("\"", 1);
|
||||
if(endQuotIndex > -1) {
|
||||
imageSrc = remainingArgument.substring(1, endQuotIndex);
|
||||
isValidImageTag = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
//invalid tag
|
||||
break;
|
||||
}
|
||||
if(isValidImageTag) {
|
||||
obj.isImage = true;
|
||||
obj.src = imageSrc;
|
||||
eventHanlderString = remainingArgument.substring(endQuotIndex+1).trim();
|
||||
if(eventHanlderString.match(eventRegx)){
|
||||
eventObj = this._processEventHandler(eventHanlderString);
|
||||
obj.event = eventObj;
|
||||
}
|
||||
this._resultObjectArray.push({text: "", style: obj});
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -194,7 +241,7 @@ cc.HtmlTextParser.prototype = {
|
||||
if (this._stack.length === 0){
|
||||
this._stack.push(obj);
|
||||
} else {
|
||||
if(obj.isNewLine) {
|
||||
if(obj.isNewLine || obj.isImage) {
|
||||
return;
|
||||
}
|
||||
//for nested tags
|
||||
|
||||
@@ -324,3 +324,71 @@ test('test br tag', function () {
|
||||
{text: "world"}
|
||||
], "newline element test");
|
||||
});
|
||||
|
||||
test('test image tag', function () {
|
||||
var imageTest1 = "<img src='weapon' />";
|
||||
|
||||
deepEqual(parser.parse(imageTest1),
|
||||
[
|
||||
{text: "",
|
||||
style: {isImage: true, src: "weapon"}}
|
||||
], "image element test 1");
|
||||
|
||||
var imageTest2 = '<img src = "weapon"/>';
|
||||
|
||||
deepEqual(parser.parse(imageTest2),
|
||||
[
|
||||
{text: "", style: {isImage: true, src: "weapon"}}
|
||||
], "image element test 2");
|
||||
|
||||
var imageTest3 = "hello, <b>world<img src='nihao' /></>";
|
||||
|
||||
deepEqual(parser.parse(imageTest3),
|
||||
[
|
||||
{text: "hello, "},
|
||||
{text: "world", style : {bold: true}},
|
||||
{text: "", style: {isImage: true, src: "nihao"}}
|
||||
], "image element test");
|
||||
|
||||
var imageTest4 = "hello, <b>world<img src=nihao / ></>";
|
||||
deepEqual(parser.parse(imageTest4),
|
||||
[
|
||||
{text: "hello, "},
|
||||
{text: "world", style : {bold: true}},
|
||||
], "image element test");
|
||||
|
||||
var imageTest5 = "hello, <b>world<on click='handler'><img src=nihao / ></on></b>";
|
||||
deepEqual(parser.parse(imageTest5),
|
||||
[
|
||||
{text: "hello, "},
|
||||
{text: "world", style : {bold: true}},
|
||||
], "image element event test");
|
||||
|
||||
var imageTest6 = "hello, <b>world<img src='head' click='handler' /></b>";
|
||||
deepEqual(parser.parse(imageTest6),
|
||||
[
|
||||
{text: "hello, "},
|
||||
{text: "world", style : {bold: true}},
|
||||
{text: "", style: {isImage: true, src: "head",
|
||||
event: {click: "handler"}}}
|
||||
], "image element event test");
|
||||
|
||||
|
||||
var invalidImageTest1 = "<img src='hello'></img>";
|
||||
deepEqual(parser.parse(invalidImageTest1),
|
||||
[], "image element invalid test");
|
||||
|
||||
var invalidImageTest2 = "<img src='world'>";
|
||||
deepEqual(parser.parse(invalidImageTest2),
|
||||
[], "image element invalid test");
|
||||
|
||||
var invalidImageTest3 = "<image src='world' />";
|
||||
deepEqual(parser.parse(invalidImageTest3),
|
||||
[], "image element invalid test");
|
||||
|
||||
|
||||
var invalidImageTest4 = "<b><u><img src='world' click='handler' /></b></u>";
|
||||
deepEqual(parser.parse(invalidImageTest4),
|
||||
[{text: "", style: {isImage: true, src: 'world',
|
||||
event: {click: 'handler'}}}], "image element invalid test");
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user