Added support for external Javascript source files.

Added example of using external JS source files.
This commit is contained in:
Florian Rival
2014-11-24 23:39:06 +01:00
parent e999f953b9
commit 0622a69aec
9 changed files with 260 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

View File

@@ -0,0 +1,195 @@
<?xml version="1.0" encoding="ISO-8859-1" ?>
<project firstLayout="">
<gdVersion build="75" major="3" minor="5" revision="0" />
<properties linuxExecutableFilename="" macExecutableFilename="" useExternalSourceFiles="true" winExecutableFilename="" winExecutableIconFile="">
<name>Project</name>
<author></author>
<windowWidth>800</windowWidth>
<windowHeight>600</windowHeight>
<latestCompilationDirectory></latestCompilationDirectory>
<maxFPS>60</maxFPS>
<minFPS>10</minFPS>
<verticalSync>false</verticalSync>
<extensions>
<extension name="BuiltinObject" />
<extension name="BuiltinAudio" />
<extension name="BuiltinVariables" />
<extension name="BuiltinTime" />
<extension name="BuiltinMouse" />
<extension name="BuiltinKeyboard" />
<extension name="BuiltinJoystick" />
<extension name="BuiltinCamera" />
<extension name="BuiltinWindow" />
<extension name="BuiltinFile" />
<extension name="BuiltinNetwork" />
<extension name="BuiltinScene" />
<extension name="BuiltinAdvanced" />
<extension name="Sprite" />
<extension name="BuiltinCommonInstructions" />
<extension name="BuiltinCommonConversions" />
<extension name="BuiltinStringInstructions" />
<extension name="BuiltinMathematicalTools" />
<extension name="BuiltinExternalLayouts" />
</extensions>
<platforms>
<platform name="GDevelop JS platform" />
</platforms>
<currentPlatform>GDevelop JS platform</currentPlatform>
</properties>
<resources>
<resources>
<resource alwaysLoaded="false" file="Pea-Standard.png" kind="image" name="Pea-Standard.png" smoothed="true" userAdded="true" />
</resources>
<resourceFolders />
</resources>
<objects />
<objectsGroups />
<variables />
<layouts>
<layout b="209" disableInputWhenNotFocused="true" mangledName="New_32scene" name="New scene" oglFOV="90.000000" oglZFar="500.000000" oglZNear="1.000000" r="209" standardSortMethod="true" stopSoundsOnStartup="true" title="" v="209">
<uiSettings associatedLayout="" grid="false" gridB="255" gridG="180" gridHeight="32" gridOffsetX="0" gridOffsetY="0" gridR="158" gridWidth="32" snap="true" windowMask="false" zoomFactor="1.000000" />
<objectsGroups />
<variables />
<instances>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="411.999969" y="203.500031" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="90.000000" y="97.500015" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="468.000000" y="303.500000" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="572.000000" y="97.500015" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="147.000000" y="300.500031" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="315.000000" y="140.500031" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="200.999985" y="205.500031" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
<instance angle="0.000000" customSize="false" height="0.000000" layer="" locked="false" name="Pea" width="0.000000" x="417.000031" y="65.500015" zOrder="1">
<numberProperties />
<stringProperties />
<initialVariables />
</instance>
</instances>
<objects>
<object name="Pea" type="Sprite">
<variables />
<automatisms />
<animations>
<animation useMultipleDirections="false">
<directions>
<direction looping="false" timeBetweenFrames="1.000000">
<sprites>
<sprite hasCustomCollisionMask="false" image="Pea-Standard.png">
<points />
<originPoint name="origine" x="0.000000" y="0.000000" />
<centerPoint automatic="true" name="centre" x="18.500000" y="18.500000" />
<customCollisionMask>
<polygon>
<vertice x="0.000000" y="0.000000" />
<vertice x="37.000000" y="0.000000" />
<vertice x="37.000000" y="37.000000" />
<vertice x="0.000000" y="37.000000" />
</polygon>
</customCollisionMask>
</sprite>
</sprites>
</direction>
</directions>
</animation>
</animations>
</object>
</objects>
<events>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::Comment</type>
<color b="0" g="255" r="128" textB="0" textG="0" textR="0" />
<comment>Here, we use functions defined in external source files.</comment>
<comment2></comment2>
</event>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::Comment</type>
<color b="109" g="230" r="255" textB="0" textG="0" textR="0" />
<comment>Custom javascript code can access to the specified objects and to the scene:</comment>
<comment2></comment2>
</event>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::JsCode</type>
<inlineCode>animateObjects(runtimeScene, objects);</inlineCode>
<parameterObjects>Pea</parameterObjects>
</event>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::Comment</type>
<color b="109" g="230" r="255" textB="0" textG="0" textR="0" />
<comment>Of course, objects are picked as in any other event: here, a window is displaying the coordinates of the pea that was clicked.</comment>
<comment2></comment2>
</event>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::Standard</type>
<conditions>
<condition>
<type inverted="false" value="SourisSurObjet" />
<parameters>
<parameter>Pea</parameter>
<parameter></parameter>
<parameter></parameter>
</parameters>
<subConditions />
</condition>
<condition>
<type inverted="false" value="SourisBouton" />
<parameters>
<parameter></parameter>
<parameter>Left</parameter>
</parameters>
<subConditions />
</condition>
</conditions>
<actions />
<events>
<event disabled="false" folded="false">
<type>BuiltinCommonInstructions::JsCode</type>
<inlineCode>alertObjectPosition(objects);</inlineCode>
<parameterObjects>Pea</parameterObjects>
</event>
</events>
</event>
</events>
<layers>
<layer name="" visibility="true">
<cameras>
<camera defaultSize="true" defaultViewport="true" height="0.000000" viewportBottom="1.000000" viewportLeft="0.000000" viewportRight="1.000000" viewportTop="0.000000" width="0.000000" />
</cameras>
</layer>
</layers>
<automatismsSharedData />
</layout>
</layouts>
<externalEvents />
<externalLayouts />
<externalSourceFiles>
<sourceFile filename="animate-objects.js" gdManaged="false" language="Javascript" lastBuildTimeStamp="0" />
<sourceFile filename="alert-position.js" gdManaged="false" language="Javascript" lastBuildTimeStamp="0" />
</externalSourceFiles>
</project>

View File

@@ -0,0 +1,8 @@
alertObjectPosition = function(objects) {
if (!objects[0]) {
return;
}
window.alert("You clicked on object at position "+
objects[0].getX()+";"+objects[0].getY()+"!");
}

View File

@@ -0,0 +1,5 @@
animateObjects = function(runtimeScene, objects) {
for(var i = 0;i<objects.length;++i) {
objects[i].setAngle(Math.cos(runtimeScene.getTimeFromStart()/100)*90);
}
}

File diff suppressed because one or more lines are too long

View File

@@ -42,6 +42,7 @@ JsCodeEventDialog::JsCodeEventDialog(wxWindow* parent, gdjs::JsCodeEvent & event
codeEdit->SetKeyWords(0, wxT("break case catch continue default delete do else finally for function if in instanceof new return switch this throw try typeof var void while with"));
codeEdit->SetKeyWords(1, wxT("null true false"));
codeEdit->SetKeyWords(2, wxT("push join slice splice Math exec match split length toString alert window"));
codeEdit->SetTabWidth(4);
codeEdit->SetMarginWidth (MARGIN_LINE_NUMBERS, 20);
codeEdit->StyleSetForeground (wxSTC_STYLE_LINENUMBER, wxColour (75, 75, 75) );

View File

@@ -25,6 +25,7 @@
#include "GDCore/Serialization/Serializer.h"
#include "GDCore/Serialization/SerializerElement.h"
#include "GDCore/PlatformDefinition/ExternalEvents.h"
#include "GDCore/PlatformDefinition/SourceFile.h"
#include "GDCore/IDE/wxTools/RecursiveMkDir.h"
#include "GDCore/IDE/ProjectResourcesCopier.h"
#include "GDCore/IDE/ProjectStripper.h"
@@ -85,6 +86,13 @@ bool Exporter::ExportLayoutForPreview(gd::Project & project, gd::Layout & layout
if ( !ExportEventsCode(exportedProject, fs.GetTempDir()+"/GDTemporaries/JSCodeTemp/", includesFiles) )
return false;
//Export source files
if ( !ExportExternalSourceFiles(exportedProject, fs.GetTempDir()+"/GDTemporaries/JSCodeTemp/", includesFiles) )
{
gd::LogError(_("Error during exporting! Unable to export source files:\n")+lastError);
return false;
}
//Strip the project (*after* generating events as the events may use stripped things (objects groups...))
gd::ProjectStripper::StripProject(exportedProject);
exportedProject.SetFirstLayout(layout.GetName());
@@ -404,6 +412,28 @@ bool Exporter::ExportEventsCode(gd::Project & project, std::string outputDir, st
return true;
}
bool Exporter::ExportExternalSourceFiles(gd::Project & project, std::string outputDir, std::vector<std::string> & includesFiles)
{
const std::vector < boost::shared_ptr<gd::SourceFile> > & allFiles = project.GetAllSourceFiles();
for (unsigned int i = 0;i<allFiles.size();++i)
{
if (allFiles[i] == boost::shared_ptr<gd::SourceFile>() ) continue;
if (allFiles[i]->GetLanguage() != "Javascript" ) continue;
gd::SourceFile & file = *allFiles[i];
std::string filename = file.GetFileName();
fs.MakeAbsolute(filename, fs.DirNameFrom(project.GetProjectFile()));
std::string outFilename = "ext-code"+gd::ToString(i)+".js";
if (!fs.CopyFile(filename, outputDir+outFilename))
gd::LogWarning(_("Could not copy external file") + filename);
InsertUnique(includesFiles, outputDir+outFilename);
}
return true;
}
bool Exporter::ExportIncludesAndLibs(std::vector<std::string> & includesFiles, std::string exportDir, bool minify)
{
#if !defined(GD_NO_WX_GUI)
@@ -563,7 +593,14 @@ bool Exporter::ExportWholeProject(gd::Project & project, std::string exportDir,
//Export events
if ( !ExportEventsCode(exportedProject, fs.GetTempDir()+"/GDTemporaries/JSCodeTemp/", includesFiles) )
{
gd::LogError(_("Error during exporting: Unable to export events ( "+lastError+")."));
gd::LogError(_("Error during exporting! Unable to export events:\n")+lastError);
return false;
}
//Export source files
if ( !ExportExternalSourceFiles(exportedProject, fs.GetTempDir()+"/GDTemporaries/JSCodeTemp/", includesFiles) )
{
gd::LogError(_("Error during exporting! Unable to export source files:\n")+lastError);
return false;
}

View File

@@ -118,6 +118,18 @@ private:
*/
bool ExportEventsCode(gd::Project & project, std::string outputDir, std::vector<std::string> & includesFiles);
/**
* \brief Copy the external source files used by the game into the export directory, and add them into files
* to be included.
*
* Files are named "ext-codeX.js", X being the number of the layout in the project.
* \param project The project with resources to be exported.
* \param outputDir The directory where the events code must be generated.
* \param includesFiles A reference to a vector that will be filled with JS files to be exported along with the project.
* ( including "ext-codeX.js" files ).
*/
bool ExportExternalSourceFiles(gd::Project & project, std::string outputDir, std::vector<std::string> & includesFiles);
/**
* \brief Generate the standard index file and save it to the export directory.
*