Skip to content

Commit

Permalink
feat: add util/util
Browse files Browse the repository at this point in the history
  • Loading branch information
06wj committed Jun 29, 2017
1 parent 2d838b0 commit 5397509
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 35 deletions.
2 changes: 1 addition & 1 deletion examples/drag.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h1>drag</h1>
}).addTo(stage);

//mix drag
Hilo.copy(bmp, Hilo.drag);
Hilo.util.copy(bmp, Hilo.drag);
bmp.startDrag([container.x, container.y, container.width - bmp.width, container.height - bmp.height]);

bmp.on("dragStart", function(e){
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
"files": [
"core/Hilo.js",
"core/Class.js",
"util/util.js",
"util/browser.js",
"geom/Matrix.js",
"event/EventMixin.js",
"view/Drawable.js",
Expand Down
7 changes: 7 additions & 0 deletions src/core/Hilo.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var Hilo = (function(){
var win = window, doc = document, docElem = doc.documentElement,
uid = 0;

var hasWarnedDict = {};
return {
/**
* Hilo version
Expand Down Expand Up @@ -74,6 +75,7 @@ return {
/**
* @language=en
* Simple shallow copy objects.
* @deprecated use Hilo.util.copy instead
* @param {Object} target Target object to copy to.
* @param {Object} source Source object to copy.
* @param {Boolean} strict Indicates whether replication is undefined property, default is false, i.e., undefined attributes are not copied.
Expand All @@ -82,6 +84,7 @@ return {
/**
* @language=zh
* 简单的浅复制对象。
* @deprecated 使用 Hilo.util.copy
* @param {Object} target 要复制的目标对象。
* @param {Object} source 要复制的源对象。
* @param {Boolean} strict 指示是否复制未定义的属性,默认为false,即不复制未定义的属性。
Expand All @@ -93,6 +96,10 @@ return {
target[key] = source[key];
}
}
if(!hasWarnedDict.copy){
hasWarnedDict.copy = true;
console.warn('Hilo.copy has been Deprecated! Use Hilo.util.copy instead.');
}
return target;
},

Expand Down
8 changes: 4 additions & 4 deletions src/flash/FlashAdaptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ var Adaptor = {
*/
init:function(option){
var mixOption = {};
Hilo.copy(mixOption, defaultOption);
Hilo.copy(mixOption, window.hiloFlashOption||{});
Hilo.copy(mixOption, option||{});
Hilo.util.copy(mixOption, defaultOption);
Hilo.util.copy(mixOption, window.hiloFlashOption||{});
Hilo.util.copy(mixOption, option||{});

var that = this;

Expand Down Expand Up @@ -127,7 +127,7 @@ var Adaptor = {
if(!properties.width) width = (viewport && viewport.width) || 320;
if(!properties.height) height = (viewport && viewport.height) || 480;

that._insertSwf(Hilo.copy(option, {
that._insertSwf(Hilo.util.copy(option, {
container:container,
width:width * (this.scaleX||1),
height:height * (this.scaleY||1)
Expand Down
6 changes: 3 additions & 3 deletions src/game/Camera.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @class Camera.
* @param {Object} properties The properties to create a view object, contains all writeable props of this class
* @module hilo/game/Camera
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Number} width The width of the camera.
* @property {Number} height The height of the camera.
* @property {Object} scroll The scrolling value of the camera {x:0, y:0}.
Expand All @@ -23,8 +23,8 @@
* @class Camera类表示摄像机。
* @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
* @module hilo/game/Camera
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Number} width 镜头宽
* @property {Number} height 镜头高
* @property {Object} scroll 滚动值 {x:0, y:0}
Expand All @@ -46,7 +46,7 @@ var Camera = Class.create(/** @lends Camera.prototype */{
y:0
};

Hilo.copy(this, properties);
util.copy(this, properties);
},
/**
* @language=en
Expand Down
6 changes: 3 additions & 3 deletions src/game/Camera3d.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
* @language=en
* @class Camera3d is a pseudo-3d camera.
* @module hilo/game/Camera3d
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Number} fv The distance of the fov(The distance between eyes and the Z plane,it determines the scale ratio of the 3d object).
* @property {Number} fx The x position of the screen viewpoint(The distance between the screen viewpoint and the screen left top corner on the x axis).
* @property {Number} fy The y position of the screen viewpoint(The distance between the screen viewpoint and the screen left top corner on the y axis).
Expand All @@ -25,8 +25,8 @@
* @language=zh
* @class Camera3d 伪3D虚拟摄像机。
* @module hilo/game/Camera3d
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Number} fv 镜头视点距离(屏幕视点相对眼睛距离,绝对了坐标缩放比例)。
* @property {Number} fx 镜头视点X(屏幕视点相对屏幕左上角X距离)。
* @property {Number} fy 镜头视点Y(屏幕视点相对屏幕左上角Y距离)。
Expand Down Expand Up @@ -75,7 +75,7 @@ var Camera3d = (function(){
properties.rotationY = properties.rotationY || 0;
properties.rotationZ = properties.rotationZ || 0;

Hilo.copy(this, properties);
util.copy(this, properties);
},

/**
Expand Down
4 changes: 3 additions & 1 deletion src/game/ParticleSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @requires hilo/view/View
* @requires hilo/view/Container
* @requires hilo/view/Drawable
* @requires hilo/util/util
* @property {Number} [emitTime=0.2] Emit time interval(in second).
* @property {Number} [emitTimeVar=0] Emit time interval variances.
* @property {Number} [emitNum=10] Emit number.
Expand Down Expand Up @@ -52,6 +53,7 @@
* @requires hilo/view/Container
* @requires hilo/view/Bitmap
* @requires hilo/view/Drawable
* @requires hilo/util/util
* @property {Number} [emitTime=0.2] 发射间隔
* @property {Number} [emitTimeVar=0] 发射间隔变化量
* @property {Number} [emitNum=10] 每次发射数量
Expand Down Expand Up @@ -144,7 +146,7 @@ var ParticleSystem = (function(){
* @param {Object} cfg
*/
reset: function(cfg) {
Hilo.copy(this, cfg);
util.copy(this, cfg);
this.particle.system = this;
if(this.totalTime <= 0){
this.totalTime = Infinity;
Expand Down
6 changes: 3 additions & 3 deletions src/media/HTMLAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* Limits: iOS platform requires user action events to start playing, and many Android browser can only play one audio at a time.
* @param {Object} properties create object properties, include all writable properties of this class.
* @module hilo/media/HTMLAudio
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @requires hilo/event/EventMixin
* @property {String} src The source of the playing audio.
* @property {Boolean} loop Is loop playback, default value is false.
Expand All @@ -28,8 +28,8 @@
* 使用限制:iOS平台需用户事件触发才能播放,很多Android浏览器仅能同时播放一个音频。
* @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
* @module hilo/media/HTMLAudio
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @requires hilo/event/EventMixin
* @property {String} src 播放的音频的资源地址。
* @property {Boolean} loop 是否循环播放。默认为false。
Expand All @@ -43,7 +43,7 @@
var HTMLAudio = Class.create(/** @lends HTMLAudio.prototype */{
Mixes: EventMixin,
constructor: function(properties){
Hilo.copy(this, properties, true);
util.copy(this, properties, true);

this._onAudioEvent = this._onAudioEvent.bind(this);
},
Expand Down
6 changes: 3 additions & 3 deletions src/media/WebAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
* Compatibility:iOS6+、Chrome33+、Firefox28+ supported,but all Android browsers do not support.
* @param {Object} properties create object properties, include all writable properties of this class.
* @module hilo/media/WebAudio
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @requires hilo/event/EventMixin
* @property {String} src The source of the playing audio.
* @property {Boolean} loop Is loop playback, default value is false.
Expand All @@ -28,8 +28,8 @@
* 兼容情况:iOS6+、Chrome33+、Firefox28+支持,但Android浏览器均不支持。
* @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
* @module hilo/media/WebAudio
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @requires hilo/event/EventMixin
* @property {String} src 播放的音频的资源地址。
* @property {Boolean} loop 是否循环播放。默认为false。
Expand All @@ -48,7 +48,7 @@ var context = AudioContext ? new AudioContext() : null;
return Class.create(/** @lends WebAudio.prototype */{
Mixes: EventMixin,
constructor: function(properties){
Hilo.copy(this, properties, true);
util.copy(this, properties, true);

this._init();
},
Expand Down
6 changes: 3 additions & 3 deletions src/media/WebSound.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
* @class Audio playing manager.
* @static
* @module hilo/media/WebSound
* @requires hilo/core/Hilo
* @requires hilo/media/HTMLAudio
* @requires hilo/media/WebAudio
* @requires hilo/util/util
*/
/**
* @language=zh
Expand All @@ -46,9 +46,9 @@
* @class 声音播放管理器。
* @static
* @module hilo/media/WebSound
* @requires hilo/core/Hilo
* @requires hilo/media/HTMLAudio
* @requires hilo/media/WebAudio
* @requires hilo/util/util
*/
var WebSound = {
_audios: {},
Expand Down Expand Up @@ -132,7 +132,7 @@ var WebSound = {
_normalizeSource: function(source){
var result = {};
if(typeof source === 'string') result = {src:source};
else Hilo.copy(result, source);
else util.copy(result, source);
return result;
}

Expand Down
6 changes: 3 additions & 3 deletions src/renderer/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* @class Renderer Renderer is the base class of renderer.
* @param {Object} properties The properties to create a renderer, contains all writeable props of this class.
* @module hilo/renderer/Renderer
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Object} canvas The canvas of renderer. It can be a dom element, such as a div element, or a canvas element. readonly.
* @property {Object} stage The stage of renderer, readonly.
* @property {String} renderType The render type of renderer, readonly.
Expand All @@ -20,16 +20,16 @@
* @class 渲染器抽象基类。
* @param {Object} properties 创建对象的属性参数。可包含此类所有可写属性。
* @module hilo/renderer/Renderer
* @requires hilo/core/Hilo
* @requires hilo/core/Class
* @requires hilo/util/util
* @property {Object} canvas 渲染器对应的画布。它可能是一个普通的DOM元素,比如div,也可以是一个canvas画布元素。只读属性。
* @property {Object} stage 渲染器对应的舞台。只读属性。
* @property {String} renderType 渲染方式。只读属性。
*/
var Renderer = Class.create(/** @lends Renderer.prototype */{
constructor: function(properties){
properties = properties || {};
Hilo.copy(this, properties, true);
util.copy(this, properties, true);
},

renderType:null,
Expand Down
8 changes: 4 additions & 4 deletions src/util/drag.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
* example:
* <pre>
* var bmp = new Bitmap({image:img});
* Hilo.copy(bmp, Hilo.drag);
* Hilo.util.copy(bmp, Hilo.drag);
* bmp.startDrag([0, 0, 550, 400]);
* </pre>
* @class drag A mixin that contains drag method.You can mix drag method to the visual target by use Class.mix(target, drag) or Hilo.copy(target, drag).
* @class drag A mixin that contains drag method.You can mix drag method to the visual target by use Class.mix(target, drag) or Hilo.util.copy(target, drag).
* @mixin
* @static
* @module hilo/util/drag
Expand All @@ -27,10 +27,10 @@
* 使用示例:
* <pre>
* var bmp = new Bitmap({image:img});
* Hilo.copy(bmp, Hilo.drag);
* Hilo.util.copy(bmp, Hilo.drag);
* bmp.startDrag([0, 0, 550, 400]);
* </pre>
* @class drag是一个包含拖拽功能的mixin。可以通过 Class.mix(view, drag)或Hilo.copy(view, drag)来为view增加拖拽功能。
* @class drag是一个包含拖拽功能的mixin。可以通过 Class.mix(view, drag)或Hilo.util.copy(view, drag)来为view增加拖拽功能。
* @mixin
* @static
* @module hilo/util/drag
Expand Down
44 changes: 44 additions & 0 deletions src/util/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Hilo
* Copyright 2015 alibaba.com
* Licensed under the MIT License
*/

/**
* @language=en
* @class util method set
* @static
* @module hilo/util/util
*/
/**
* @language=zh
* @class 工具方法集合
* @static
* @module hilo/util/util
*/
var util = {
/**
* @language=en
* Simple shallow copy objects.
* @param {Object} target Target object to copy to.
* @param {Object} source Source object to copy.
* @param {Boolean} strict Indicates whether replication is undefined property, default is false, i.e., undefined attributes are not copied.
* @returns {Object} Object after copying.
*/
/**
* @language=zh
* 简单的浅复制对象。
* @param {Object} target 要复制的目标对象。
* @param {Object} source 要复制的源对象。
* @param {Boolean} strict 指示是否复制未定义的属性,默认为false,即不复制未定义的属性。
* @returns {Object} 复制后的对象。
*/
copy: function(target, source, strict){
for(var key in source){
if(!strict || target.hasOwnProperty(key) || target[key] !== undefined){
target[key] = source[key];
}
}
return target;
}
};
4 changes: 3 additions & 1 deletion src/view/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
* @requires hilo/core/Class
* @requires hilo/view/View
* @requires hilo/view/Drawable
* @requires hilo/util/util
* @property {Object} upState The property of button 'up' state or collections of its drawable properties.
* @property {Object} overState The property of button 'over' state or collections of its drawable properties.
* @property {Object} downState The property of button 'down' state or collections of its drawable properties.
Expand Down Expand Up @@ -62,6 +63,7 @@
* @requires hilo/core/Class
* @requires hilo/view/View
* @requires hilo/view/Drawable
* @requires hilo/util/util
* @property {Object} upState 按钮弹起状态的属性或其drawable的属性的集合。
* @property {Object} overState 按钮经过状态的属性或其drawable的属性的集合。
* @property {Object} downState 按钮按下状态的属性或其drawable的属性的集合。
Expand Down Expand Up @@ -148,7 +150,7 @@

if(stateObj){
this.drawable.init(stateObj);
Hilo.copy(this, stateObj, true);
util.copy(this, stateObj, true);
}
}

Expand Down
Loading

0 comments on commit 5397509

Please sign in to comment.