r3-legacy/src/game-lib-custom-code.js

142 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-01-13 16:19:51 +01:00
/**
* Creates a CustomCode object
2017-12-02 11:54:04 +01:00
* @param apiCustomCode GameLib.API.CustomCode
2017-01-13 16:19:51 +01:00
* @constructor
*/
2017-12-02 11:54:04 +01:00
GameLib.CustomCode = function(
apiCustomCode
2017-01-13 16:19:51 +01:00
) {
if (GameLib.Utils.UndefinedOrNull(apiCustomCode)) {
apiCustomCode = {};
}
2017-12-02 11:54:04 +01:00
if (apiCustomCode instanceof GameLib.CustomCode) {
return apiCustomCode;
}
2017-12-02 11:54:04 +01:00
GameLib.API.CustomCode.call(
2017-01-13 16:19:51 +01:00
this,
apiCustomCode.id,
apiCustomCode.name,
2017-08-24 22:20:40 +02:00
apiCustomCode.eventId,
2017-01-13 16:19:51 +01:00
apiCustomCode.code,
2017-08-24 22:20:40 +02:00
apiCustomCode.parentEntity
2017-01-13 16:19:51 +01:00
);
this.editor = null;
2017-08-24 22:20:40 +02:00
2017-06-16 15:49:53 +02:00
GameLib.Component.call(
this,
2017-08-24 22:20:40 +02:00
GameLib.Component.COMPONENT_CUSTOM_CODE
2017-06-16 15:49:53 +02:00
);
2017-01-13 16:19:51 +01:00
};
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype = Object.create(GameLib.API.CustomCode.prototype);
GameLib.CustomCode.prototype.constructor = GameLib.CustomCode;
2017-01-13 16:19:51 +01:00
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype.createInstance = function() {
2017-10-23 14:52:35 +02:00
try {
2017-10-23 14:52:35 +02:00
this.instance = new Function('data', this.code).bind(this);
} catch (error) {
/**
2017-10-23 14:52:35 +02:00
* Set the instance to true here to indicate that even though the compilation failed, the instance will be fine and
* this component loaded fine.
*/
2017-10-23 14:52:35 +02:00
this.instance = new Function('data', "console.log('compilation failed for : " + this.name + "');").bind(this);
}
2017-10-23 14:52:35 +02:00
GameLib.Component.prototype.createInstance.call(this);
2017-01-13 16:19:51 +01:00
};
/**
* Updates the instance with the current state
*/
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype.updateInstance = function() {
2017-10-23 14:52:35 +02:00
try {
this.instance = new Function('data', this.code).bind(this);
this.publish(
GameLib.Event.COMPILE_SUCCESS,
{
component : this
}
)
} catch (error) {
2017-10-23 14:52:35 +02:00
this.instance = new Function('data', "console.log('compilation update failed for : " + this.name + "');").bind(this);
this.publish(
GameLib.Event.COMPILE_FAILED,
{
component : this
}
)
}
2017-10-23 14:52:35 +02:00
2017-01-13 16:19:51 +01:00
};
/**
2017-12-02 11:54:04 +01:00
* Converts a GameLib.CustomCode to a new GameLib.API.CustomCode
* @returns {GameLib.API.CustomCode}
2017-01-13 16:19:51 +01:00
*/
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype.toApiObject = function() {
2017-01-13 16:19:51 +01:00
2017-12-02 11:54:04 +01:00
return new GameLib.API.CustomCode(
2017-01-13 16:19:51 +01:00
this.id,
this.name,
2017-08-24 22:20:40 +02:00
this.eventId,
2017-01-13 16:19:51 +01:00
this.code,
2017-08-24 22:20:40 +02:00
GameLib.Utils.IdOrNull(this.parentEntity)
2017-01-13 16:19:51 +01:00
);
};
/**
2017-12-02 11:54:04 +01:00
* Converts from an Object CustomCode to a GameLib.CustomCode
2017-01-13 16:19:51 +01:00
* @param objectCustomCode Object
2017-12-02 11:54:04 +01:00
* @returns {GameLib.CustomCode}
2017-01-13 16:19:51 +01:00
* @constructor
*/
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.FromObject = function(objectCustomCode) {
var apiCustomCode = GameLib.API.CustomCode.FromObject(objectCustomCode);
return new GameLib.CustomCode(
2017-01-13 16:19:51 +01:00
apiCustomCode
);
};
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype.launchEditor = function(){
2017-10-09 12:45:29 +02:00
GameLib.Event.Emit(
GameLib.Event.GET_RUNTIME,
2017-10-09 12:45:29 +02:00
null,
function(runtime) {
this.coder = runtime.coder;
2017-10-09 12:45:29 +02:00
this.coder.isNotCodeMirrorThrow();
}.bind(this)
);
this.editor = this.coder.instance(
2017-08-24 22:20:40 +02:00
document.body,
{
value : this.code,
mode : 'javascript',
lineNumbers : true,
2017-09-19 21:27:11 +02:00
scrollbarStyle : 'overlay',
indentWithTabs: true,
indentUnit : 4
2017-08-24 22:20:40 +02:00
}
);
this.editor.on('change', function(){
this.code = this.editor.getValue();
this.updateInstance();
}.bind(this))
};
2017-12-02 11:54:04 +01:00
GameLib.CustomCode.prototype.closeEditor = function(){
var dom = this.editor.getWrapperElement();
dom.parentElement.removeChild(dom);
2017-01-13 16:19:51 +01:00
};