状态栏事件优化。

This commit is contained in:
liteng 2018-09-27 07:52:35 +08:00
parent b24180712d
commit 23253fdaac
3 changed files with 48 additions and 75 deletions

View File

@ -14,11 +14,8 @@ StatusBar.prototype = Object.create(UI.Control.prototype);
StatusBar.prototype.constructor = StatusBar;
StatusBar.prototype.render = function () {
var _this = this;
var data = {
xtype: 'div',
id: 'statusBar',
parent: this.parent,
cls: 'statusBar',
children: [{
@ -29,6 +26,7 @@ StatusBar.prototype.render = function () {
}, {
xtype: 'text',
id: 'objectsText',
scope: this.id,
text: '0' // 物体数
}, {
xtype: 'label',
@ -36,6 +34,7 @@ StatusBar.prototype.render = function () {
}, {
xtype: 'text',
id: 'verticesText',
scope: this.id,
text: '0' // 顶点数
}, {
xtype: 'label',
@ -43,6 +42,7 @@ StatusBar.prototype.render = function () {
}, {
xtype: 'text',
id: 'trianglesText',
scope: this.id,
text: '0' // 三角形数
}]
}]
@ -50,6 +50,51 @@ StatusBar.prototype.render = function () {
var control = UI.create(data);
control.render();
this.app.on('objectAdded.' + this.id, this.onUpdateInfo.bind(this));
this.app.on('objectRemoved.' + this.id, this.onUpdateInfo.bind(this));
this.app.on('geometryChanged.' + this.id, this.onUpdateInfo.bind(this));
};
StatusBar.prototype.onUpdateInfo = function () {
var editor = this.app.editor;
var scene = editor.scene;
var objects = 0, vertices = 0, triangles = 0;
for (var i = 0, l = scene.children.length; i < l; i++) {
var object = scene.children[i];
object.traverseVisible(function (object) {
objects++;
if (object instanceof THREE.Mesh) {
var geometry = object.geometry;
if (geometry instanceof THREE.Geometry) {
vertices += geometry.vertices.length;
triangles += geometry.faces.length;
} else if (geometry instanceof THREE.BufferGeometry) {
if (geometry.index !== null) {
vertices += geometry.index.count * 3;
triangles += geometry.index.count;
} else {
vertices += geometry.attributes.position.count;
triangles += geometry.attributes.position.count / 3;
}
}
}
});
}
var objectsText = UI.get('objectsText', this.id);
var verticesText = UI.get('verticesText', this.id);
var trianglesText = UI.get('trianglesText', this.id);
objectsText.setValue(objects.format());
verticesText.setValue(vertices.format());
trianglesText.setValue(triangles.format());
};
export default StatusBar;

View File

@ -16,7 +16,6 @@ import AddPhysicsWallEvent from './menu/physics/AddPhysicsWallEvent';
import AddPhysicsClothEvent from './menu/physics/AddPhysicsClothEvent';
import TransformControlsEvent from './viewport/TransformControlsEvent';
import UpdateSceneStatusEvent from './statusBar/UpdateSceneStatusEvent';
import ObjectEvent from './viewport/ObjectEvent';
import PickEvent from './PickEvent';
import EditorControlsEvent from './viewport/EditorControlsEvent';
@ -46,7 +45,6 @@ function EventDispatcher(app) {
// viewport中的事件
new TransformControlsEvent(this.app),
new UpdateSceneStatusEvent(this.app),
new ObjectEvent(this.app),
new PickEvent(this.app),
new EditorControlsEvent(this.app)

View File

@ -1,70 +0,0 @@
import BaseEvent from '../BaseEvent';
import UI from '../../ui/UI';
/**
* 更新场景编辑区信息事件
* @author tengge / https://github.com/tengge1
* @param {*} app
*/
function UpdateSceneStatusEvent(app) {
BaseEvent.call(this, app);
}
UpdateSceneStatusEvent.prototype = Object.create(BaseEvent.prototype);
UpdateSceneStatusEvent.prototype.constructor = UpdateSceneStatusEvent;
UpdateSceneStatusEvent.prototype.start = function () {
var _this = this;
this.app.on('objectAdded.' + this.id, this.onUpdateInfo.bind(this));
this.app.on('objectRemoved.' + this.id, this.onUpdateInfo.bind(this));
this.app.on('geometryChanged.' + this.id, this.onUpdateInfo.bind(this));
};
UpdateSceneStatusEvent.prototype.stop = function () {
this.app.on('objectAdded.' + this.id, null);
this.app.on('objectRemoved.' + this.id, null);
this.app.on('geometryChanged.' + this.id, null);
};
UpdateSceneStatusEvent.prototype.onUpdateInfo = function () {
var editor = this.app.editor;
var scene = editor.scene;
var objects = 0, vertices = 0, triangles = 0;
for (var i = 0, l = scene.children.length; i < l; i++) {
var object = scene.children[i];
object.traverseVisible(function (object) {
objects++;
if (object instanceof THREE.Mesh) {
var geometry = object.geometry;
if (geometry instanceof THREE.Geometry) {
vertices += geometry.vertices.length;
triangles += geometry.faces.length;
} else if (geometry instanceof THREE.BufferGeometry) {
if (geometry.index !== null) {
vertices += geometry.index.count * 3;
triangles += geometry.index.count;
} else {
vertices += geometry.attributes.position.count;
triangles += geometry.attributes.position.count / 3;
}
}
}
});
}
var objectsText = UI.get('objectsText');
var verticesText = UI.get('verticesText');
var trianglesText = UI.get('trianglesText');
objectsText.setValue(objects.format());
verticesText.setValue(vertices.format());
trianglesText.setValue(triangles.format());
};
export default UpdateSceneStatusEvent;