重新加入事件系统。

This commit is contained in:
liteng 2018-06-15 20:44:44 +08:00
parent 709d925c53
commit 0849af55f8
7 changed files with 121 additions and 2 deletions

View File

@ -1,4 +1,5 @@
import Editor from './Editor';
import EventDispatcher from './event/EventDispatcher';
import Viewport from './ui/Viewport';
import Script from './core/Script';
import Player from './core/Player';
@ -13,7 +14,11 @@ import RemoveObjectCommand from './command/RemoveObjectCommand';
*/
function Application(container) {
var editor = new Editor();
this.container = container;
this.event = new EventDispatcher(this);
// editor
var editor = new Editor(this);
this.editor = editor;
// dom

View File

@ -10,7 +10,7 @@ import SceneUtils from './utils/SceneUtils';
* @author mrdoob / http://mrdoob.com/
*/
function Editor() {
function Editor(app) {
this.DEFAULT_CAMERA = new THREE.PerspectiveCamera(50, 1, 0.1, 10000);
this.DEFAULT_CAMERA.name = 'Camera';

27
src/event/BaseEvent.js Normal file
View File

@ -0,0 +1,27 @@
import NotImplementedException from '../exception/NotImplementedException';
/**
* 事件基类
*/
class BaseEvent {
constructor(app) {
this.app = app;
}
/**
* 开始执行
*/
start() {
throw NotImplementedException();
}
/**
* 停止执行
*/
stop() {
throw NotImplementedException();
}
}
export default BaseEvent;

View File

@ -0,0 +1,75 @@
import { dispatch } from '../third_party';
import EventList from './EventList';
/**
* 事件执行器
*/
class EventDispatcher {
constructor(app) {
this.app = app;
this.dispatch = dispatch.apply(dispatch, EventList);
// this.domElement = this.app.container;
// this.addDomEventListener();
}
/**
* 执行事件
* @param {*} eventName
* @param {*} _this
* @param {*} others
*/
call(eventName, _this, ...others) {
this.dispatch.call(eventName, _this, ...others);
}
/**
* 监听事件
* @param {*} eventName
* @param {*} callback
*/
on(eventName, callback) {
this.dispatch.on(eventName, callback);
}
/**
* 监听dom事件
*/
addDomEventListener() {
var container = this.app.container;
this.domElement.addEventListener('click', (event) => {
this.dispatch.call('click', this, event);
});
this.domElement.addEventListener('contextmenu', (event) => {
this.dispatch.call('contextmenu', this, event);
event.preventDefault();
return false;
});
this.domElement.addEventListener('dblclick', (event) => {
this.dispatch.call('dblclick', this, event);
});
this.domElement.addEventListener('keydown', (event) => {
this.dispatch.call('keydown', this, event);
});
this.domElement.addEventListener('keyup', (event) => {
this.dispatch.call('keyup', this, event);
});
this.domElement.addEventListener('mousedown', (event) => {
this.dispatch.call('mousedown', this, event);
});
this.domElement.addEventListener('mousemove', (event) => {
this.dispatch.call('mousemove', this, event);
});
this.domElement.addEventListener('mouseup', (event) => {
this.dispatch.call('mouseup', this, event);
});
this.domElement.addEventListener('mousewheel', (event) => {
this.dispatch.call('mousewheel', this, event);
});
window.addEventListener('resize', (event) => {
this.dispatch.call('resize', this, event);
});
}
}
export default EventDispatcher;

8
src/event/EventList.js Normal file
View File

@ -0,0 +1,8 @@
/**
* 自定义事件列表
*/
var EventList = [
];
export default EventList;

3
src/event/index.js Normal file
View File

@ -0,0 +1,3 @@
export { default as EventList } from './EventList';
export { default as BaseEvent } from './BaseEvent';
export { default as EventDispatcher } from './EventDispatcher';

View File

@ -1,5 +1,6 @@
export * from './third_party';
import { } from './polyfills';
export * from './event/index';
export * from './core/index';
export * from './command/index';
export * from './ui/index';