Permalink
Browse files

Added level data interface in Weltmeister.

  • Loading branch information...
1 parent 6a75e77 commit 9e806378692c0f8292e5c8bdcb24f8ad3eb741e8 @xdissent committed Jun 12, 2011
Showing with 213 additions and 5 deletions.
  1. +59 −0 weltmeister.css
  2. +154 −5 weltmeister.js
View
@@ -0,0 +1,59 @@
+
+#layerContainer {
+ position: relative;
+}
+
+#levelDataProperties{
+ margin-top: 10px;
+ border-top: 1px solid #333;
+ min-height: 28px;
+ max-height: 140px;
+ overflow: auto;
+}
+
+div.levelDataProperty {
+ color: #aaa;
+ padding: 2px 0;
+ border-bottom: 1px solid #333;
+ cursor: pointer;
+}
+
+div.levelDataProperty:hover {
+ background-color: #333;
+}
+
+div.levelDataProperty .key {
+ width: 55%;
+ display: block;
+ float: left;
+ margin: 0 0px 0 0;
+ padding: 0;
+ text-align: right;
+ color: #fff;
+ overflow: hidden;
+}
+
+div.levelDataProperty .value {
+ padding: 0 0 0 2px;
+ color: #fff;
+}
+
+
+dl#levelDataPropertyInput {
+ padding: 8px 0;
+}
+
+dl#levelDataPropertyInput dt {
+ width: 40px;
+ display: block;
+ float: left;
+ margin: 0 4px 0 0;
+ padding: 4px 0 0 0;
+ text-align: right;
+}
+
+dl#levelDataPropertyInput dd {
+ display: block;
+ margin: 0;
+ padding: 2px 0;
+}
View
@@ -7,18 +7,167 @@ ig.module(
.defines(function() {
wm.Weltmeister.inject({
- _data_hooks: [],
+ _dataHooks: [],
+
+ _levelData: {},
+
+ init: function() {
+
+ // Load Weltmeister plugin CSS
+ $('<link />', {
+ rel: 'stylesheet',
+ type: 'text/css',
+ href: wm.config.project.modulePath + 'plugins/levelData/weltmeister.css'
+ }).appendTo('head');
+
+ // Create Level Data Container
+ $('<div id="levelDataContainer"> \
+ <h2>Level Settings</h2> \
+ <div id="levelDataProperties"></div> \
+ <dl id="levelDataPropertyInput"> \
+ <dt>Key:</dt><dd><input type="text" class="text" id="levelDataKey"/></dd> \
+ <dt>Value:</dt><dd><input type="text" class="text" id="levelDataValue"/></dd> \
+ </dl> \
+ </div>').insertBefore('#layerContainer');
+
+ $('#levelDataKey').bind( 'keydown', function(ev){
+ if( ev.which == 13 ){
+ $('#levelDataValue').focus();
+ return false;
+ }
+ return true;
+ });
+ $('#levelDataValue').bind( 'keydown', this.setLevelData.bind(this) );
+
+ this.parent();
+ },
+
+ loadNew: function() {
+ this._levelData = {};
+ this.loadLevelData();
+ this.parent();
+ },
+
+ loadResponse: function( data ) {
+
+ // extract JSON from a module's JS
+ var jsonMatch = data.match( /\/\*JSON\[\*\/([\s\S]*?)\/\*\]JSON\*\// );
+ var parsedData = $.parseJSON( jsonMatch ? jsonMatch[1] : data );
+
+ this._levelData = parsedData.data ? parsedData.data : {};
+ this.loadLevelData();
+
+ return this.parent(data);
+ },
+
+ loadLevelData: function() {
+ var html = '';
+ html += this.loadLevelDataRecursive(this._levelData);
+ $('#levelDataProperties').html(html);
+ $('.levelDataProperty').bind('mouseup', this.selectLevelData);
+ },
+
+
+ loadLevelDataRecursive: function( settings, path ) {
+ path = path || "";
+ var html = "";
+ for( key in settings ) {
+ var value = settings[key];
+ if( typeof(value) == 'object' ) {
+ html += this.loadLevelDataRecursive( value, path + key + "." );
+ }
+ else {
+ html += '<div class="levelDataProperty"><span class="key">'+path+key+'</span>:<span class="value">'+value+'</span></div>';
+ }
+ }
+
+ return html;
+ },
+
+
+ setLevelData: function( ev ) {
+ if( ev.which != 13 ) {
+ return true;
+ }
+ var key = $('#levelDataKey').val();
+ var value = $('#levelDataValue').val();
+ var floatVal = parseFloat(value);
+ if( value == floatVal ) {
+ value = floatVal;
+ }
+
+ this.writeSettingAtPath( this._levelData, key, value );
+
+ ig.game.setModified();
+ ig.game.draw();
+
+ $('#levelDataKey').val('');
+ $('#levelDataValue').val('');
+ $('#levelDataValue').blur();
+ this.loadLevelData();
+
+ $('#levelDataKey').focus();
+ return false;
+ },
+
+
+ writeSettingAtPath: function( root, path, value ) {
+ path = path.split('.');
+ var cur = root;
+ for( var i = 0; i < path.length; i++ ) {
+ var n = path[i];
+ if( i < path.length-1 && typeof(cur[n]) != 'object' ) {
+ cur[n] = {};
+ }
+
+ if( i == path.length-1 ) {
+ cur[n] = value;
+ }
+ cur = cur[n];
+ }
+
+ this.trimObject( root );
+ },
+
+
+ trimObject: function( obj ) {
+ var isEmpty = true;
+ for( var i in obj ) {
+ if(
+ (obj[i] === "") ||
+ (typeof(obj[i]) == 'object' && this.trimObject(obj[i]))
+ ) {
+ delete obj[i];
+ }
+
+ if( typeof(obj[i]) != 'undefined' ) {
+ isEmpty = false;
+ }
+ }
+
+ return isEmpty;
+ },
+
+
+ selectLevelData: function( ev ) {
+ $('#levelDataKey').val( $(this).children('.key').text() );
+ $('#levelDataValue').val( $(this).children('.value').text() );
+ $('#levelDataValue').select();
+ },
+
registerDataHook: function(hook) {
if (typeof hook == 'function') {
- this._data_hooks.push(hook);
+ this._dataHooks.push(hook);
}
},
+
save: function( dialog, path ) {
this.filePath = path;
this.fileName = path.replace(/^.*\//,'');
var data = {
'entities': this.entities.getSaveData(),
- 'layer': []
+ 'layer': [],
+ 'data': this._levelData
};
var resources = [];
@@ -30,8 +179,8 @@ wm.Weltmeister.inject({
}
}
- for (var i = 0; i < this._data_hooks.length; i++) {
- data = this._data_hooks[i](data);
+ for (var i = 0; i < this._dataHooks.length; i++) {
+ data = this._dataHooks[i](data);
}
var dataString = $.toJSON(data);

0 comments on commit 9e80637

Please sign in to comment.