1
0
forked from 0ad/0ad

Auto-reloading of Atlas UI scripts

This was SVN commit r5165.
This commit is contained in:
Ykkrosh 2007-06-11 00:06:28 +00:00
parent 5b904a69c9
commit aa5bdfe426
3 changed files with 86 additions and 18 deletions

View File

@ -1,6 +1,11 @@
/*
Functions defined here are global, and accessible by any code.
*/
function getScriptFilename(name)
{
var relativePath = 'tools/atlas/scripts/' + name + '.js';
var filename = new wxFileName(relativePath, wxPathFormat.UNIX);
filename.normalize(wxPathNormalize.DOTS | wxPathNormalize.ABSOLUTE | wxPathNormalize.TILDE,
Atlas.GetDataDirectory()); // equivalent to MakeAbsolute(dir);
return filename;
}
/**
* Loads and executes a script from disk. The script runs in a separate scope,
@ -8,15 +13,79 @@ Functions defined here are global, and accessible by any code.
*/
function loadScript(name, window)
{
var relativePath = 'tools/atlas/scripts/' + name + '.js';
var filename = new wxFileName(relativePath, wxPathFormat.UNIX);
filename.normalize(wxPathNormalize.DOTS | wxPathNormalize.ABSOLUTE | wxPathNormalize.TILDE,
Atlas.GetDataDirectory()); // equivalent to MakeAbsolute(dir);
var filename = getScriptFilename(name);
var file = new wxFFile(filename.fullPath);
var script = file.readAll(); // TODO: handle errors
file.close();
var script = Atlas.LoadScript(name+'.js', script);
scriptReloader.add(name, window, filename);
script.init(window); // TODO: use a variable list of arguments
return script;
}
function loadXML(name)
{
var relativePath = 'tools/atlas/' + name + '.xml';
var filename = new wxFileName(relativePath, wxPathFormat.UNIX);
filename.normalize(wxPathNormalize.DOTS | wxPathNormalize.ABSOLUTE | wxPathNormalize.TILDE,
Atlas.GetDataDirectory()); // equivalent to MakeAbsolute(dir);
var file = new wxFFile(filename.fullPath);
var xml = file.readAll(); // TODO: handle errors
// TODO: complain (or work) nicely if the XML file starts with
// "<?xml ...?>" (which E4X doesn't like parsing)
file.close();
return eval(xml);
}
function init() { /* dummy function to make the script reloader happy */ }
var scriptReloader = {
timer: new wxTimer(),
scripts: [], // [ [filename,window], ... ]
notify: function ()
{
for each (var script in scriptReloader.scripts)
{
var mtime = script.filename.modificationTime;
if (mtime - script.mtime != 0)
{
print('*** Modifications detected - reloading "' + script.name + '"...\n');
script.mtime = mtime;
if (script.name == 'main')
{
// Special case for this file to reload itself
var obj = loadScript(script.name, null);
// Copy the important state into the new version of this file
obj.scriptReloader.scripts = scriptReloader.scripts;
// Stop this one
scriptReloader.timer.stop();
}
else
{
script.window.destroyChildren();
loadScript(script.name, script.window);
script.window.layout();
}
}
}
},
add: function (name, window, filename)
{
for each (var script in this.scripts)
if (script.name == name)
return; // stop if this is already loaded
this.scripts.push({ name:name, window:window, filename:filename, mtime:filename.modificationTime });
}
};
scriptReloader.timer.onNotify = scriptReloader.notify;
scriptReloader.timer.start(1000);
scriptReloader.add('main', null, getScriptFilename('main'));
// Export global functions:
global.loadScript = loadScript;
global.loadXML = loadXML;

View File

@ -39,17 +39,16 @@ function setupSimTest(window)
[ 'Reset', function () { reset() }, function () { return state != 'inactive' } ],
];
var statBox = new wxStaticBox(window, -1, 'Simulation test');
var sizer = new wxStaticBoxSizer(statBox, wxOrientation.HORIZONTAL);
for (var i in buttons)
var sizer = new wxStaticBoxSizer(new wxStaticBox(window, -1, 'Simulation test'), wxOrientation.HORIZONTAL);
for each (var b in buttons)
{
var button = new wxButton(window, -1, buttons[i][0]);
button.onClicked = buttons[i][1];
var button = new wxButton(window, -1, b[0]);
button.onClicked = b[1];
sizer.add(button, 1);
buttons[i][3] = button;
b[3] = button;
}
updateEnableStatus();
window.sizer.add(sizer, 0, wxStretch.EXPAND | wxDirection.LEFT|wxDirection.RIGHT, 4);
window.sizer.add(sizer, 0, wxStretch.EXPAND | wxDirection.LEFT|wxDirection.RIGHT, 2);
}
function generateRMS(name)
@ -70,7 +69,7 @@ function init(window)
var button = new wxButton(window, -1, 'Generate empty map');
button.onClicked = function () { Atlas.Message.GenerateMap(9); };
window.sizer.add(button, 0, wxDirection.ALL, 4);
window.sizer.add(button, 0, wxDirection.ALL, 2);
var sizer = new wxBoxSizer(wxOrientation.HORIZONTAL);
var rmsFilename = new wxTextCtrl(window, -1, 'cantabrian_highlands');
@ -78,7 +77,7 @@ function init(window)
rmsButton.onClicked = function () { generateRMS(rmsFilename.value) }
sizer.add(rmsFilename, 1);
sizer.add(rmsButton, 0);
window.sizer.add(sizer, 0, wxStretch.EXPAND | wxDirection.ALL, 4);
window.sizer.add(sizer, 0, wxStretch.EXPAND | wxDirection.ALL, 2);
setupSimTest(window);
}

View File

@ -287,7 +287,7 @@ void SectionLayout::Build(ScenarioEditor& scenarioEditor)
m_PageMappings.insert(std::make_pair(name, (int)m_SidebarBook->GetPageCount()-1));
ADD_SIDEBAR_SCRIPT(_T("map"), _T("map.png"), _("Map"));
ADD_SIDEBAR_SCRIPT(_T("terrain"), _T("terrain.png"), _("Terrain"));
//ADD_SIDEBAR_SCRIPT(_T("terrain"), _T("terrain.png"), _("Terrain"));
ADD_SIDEBAR(TerrainSidebar, _T("terrain.png"), _("Terrain"));
ADD_SIDEBAR(ObjectSidebar, _T("object.png"), _("Object"));
ADD_SIDEBAR(EnvironmentSidebar, _T("environment.png"), _("Environment"));