Browse Source

Adding a native project + component example, with Atomic Editor inspector and serialization support

Josh Engebretson 8 years ago
parent
commit
f555a047d1

+ 8 - 1
Script/AtomicEditor/ui/frames/inspector/CreateComponentButton.ts

@@ -20,6 +20,12 @@
 // THE SOFTWARE.
 //
 
+// Native components aren't self registering with the editor, so we need to
+// add them to the component menu.
+// TODO: self registration of native components
+var myProjectSource = new Atomic.UIMenuItemSource();
+myProjectSource.addItem(new Atomic.UIMenuItem("MyNativeComponent", "MyNativeComponent"));
+
 var audioCreateSource = new Atomic.UIMenuItemSource();
 
 audioCreateSource.addItem(new Atomic.UIMenuItem("SoundListener", "SoundListener"));
@@ -97,10 +103,11 @@ var editorCreateSource = new Atomic.UIMenuItemSource();
 
 editorCreateSource.addItem(new Atomic.UIMenuItem("CubemapGenerator", "CubemapGenerator"));
 
-
 var componentCreateSource = new Atomic.UIMenuItemSource();
 
+// TODO: self registration of native components/component groups
 var sources = {
+    MyProject: myProjectSource, // add project menu source
     Audio: audioCreateSource,
     "2D": _2DCreateSource,
     Geometry: geometryCreateSource,

+ 5 - 0
Script/Packages/MyProject/MyProject.json

@@ -0,0 +1,5 @@
+{
+	"name" : "MyProject",
+	"sources" : ["Source/MyProject"],
+	"classes" : ["MyNativeComponent"]
+}

+ 8 - 0
Script/Packages/MyProject/Package.json

@@ -0,0 +1,8 @@
+
+{
+  "name" : "MyProject",
+  "namespace" : "Atomic",
+  "dependencies" : ["Script/Packages/Atomic"],
+  "modules" : ["MyProject"],
+  "platforms" : ["WINDOWS", "MACOSX", "LINUX"]
+}

+ 8 - 1
Source/AtomicApp/AppBase.cpp

@@ -61,7 +61,7 @@ namespace Atomic
 
             arguments_ = GetArguments();
         }
-        
+
     }
 
     AppBase::~AppBase()
@@ -96,6 +96,10 @@ namespace Atomic
         }
     }
 
+    // Link our project module in, this is an example so doing in AppBase
+    // which is shared by the Atomic Editor and Player, including managed C# player
+    void RegisterMyProjectModule(Context* context);
+
     void AppBase::Setup()
     {
         Application::Setup();
@@ -105,6 +109,9 @@ namespace Atomic
         RegisterEnvironmentLibrary(context_);
 #endif
 
+        // register project module
+        RegisterMyProjectModule(context_);
+
         ProcessArguments();
 
         // Read the engine configuration

+ 3 - 1
Source/AtomicEditor/CMakeLists.txt

@@ -164,7 +164,9 @@ if (TARGET_PROPERTIES)
     set_target_properties(AtomicEditor PROPERTIES ${TARGET_PROPERTIES})
 endif ()
 
-target_link_libraries(AtomicEditor Atomic)
+# We need to link our project module into the editor, which will make it
+# available both in edit and play modes
+target_link_libraries(AtomicEditor MyProject Atomic)
 
 add_dependencies(${CEF_TARGET} AtomicToolCheckScripts)
 

+ 3 - 1
Source/AtomicNET/NETNative/CMakeLists.txt

@@ -27,7 +27,9 @@ add_library(AtomicNETNative SHARED ${SOURCE_FILES} ${CSHARP_BINDINGS_SOURCE})
 if (ATOMIC_DESKTOP)
     add_dependencies(AtomicNETNative AtomicToolCheckScripts)
 endif ()
-target_link_libraries(AtomicNETNative AtomicApp AtomicNETScriptBindings AtomicNETScript AtomicJS AtomicPlayerLib AtomicPlayerJS Atomic)
+
+# We need to link our project module into the AtomicNET, unless we optionally disable C# support
+target_link_libraries(AtomicNETNative MyProject AtomicApp AtomicNETScriptBindings AtomicNETScript AtomicJS AtomicPlayerLib AtomicPlayerJS Atomic)
 target_include_directories(AtomicNETNative PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
 
 if (APPLE)

+ 2 - 1
Source/AtomicPlayer/Application/CMakeLists.txt

@@ -27,7 +27,8 @@ endif()
 endif()
 
 if (NOT IOS)
-    target_link_libraries(AtomicPlayer AtomicJS AtomicPlayerLib AtomicPlayerJS Atomic)
+    # We need to link our project module into the deployed player
+    target_link_libraries(AtomicPlayer MyProject AtomicJS AtomicPlayerLib AtomicPlayerJS Atomic)
 endif()
 
 if (WIN32)

+ 3 - 0
Source/CMakeLists.txt

@@ -27,6 +27,9 @@ if (ATOMIC_DOTNET)
     add_subdirectory(AtomicNET)
 endif ()
 
+# Add our project native source module
+add_subdirectory(MyProject)
+
 if (ATOMIC_DESKTOP)
     if (ATOMIC_DOTNET OR ATOMIC_JAVASCRIPT)
         add_subdirectory(AtomicTool)

+ 11 - 0
Source/MyProject/CMakeLists.txt

@@ -0,0 +1,11 @@
+
+file(GLOB_RECURSE SOURCE_FILES *.cpp *.h)
+
+# in order to make our native components available in the Atomic Editor component creation menu, they need to be accessible from TypeScript, so bindings are generated.  This won't be necessary once native components are self registering
+file (GLOB JAVASCRIPT_BINDINGS_SOURCE ${ATOMIC_SOURCE_DIR}/Artifacts/Build/Source/Generated/Javascript/Packages/MyProject/*.cpp)
+
+# define our project library
+add_library(MyProject ${SOURCE_FILES} ${JAVASCRIPT_BINDINGS_SOURCE})
+
+# and link in Atomic (again, AtomicJS optional)
+target_link_libraries(MyProject AtomicJS Atomic)

+ 70 - 0
Source/MyProject/MyNativeComponent.cpp

@@ -0,0 +1,70 @@
+//
+// Copyright (c) 2017 THUNDERBEAST GAMES LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include <Atomic/Core/Context.h>
+#include <Atomic/Scene/Scene.h>
+#include <Atomic/Scene/SceneEvents.h>
+
+#include "MyNativeComponent.h"
+
+namespace Atomic
+{
+
+MyNativeComponent::MyNativeComponent(Context *context) : Component(context),
+    speed_(1.0f)
+{
+}
+
+MyNativeComponent::~MyNativeComponent()
+{
+
+}
+
+void MyNativeComponent::OnSceneSet(Scene* scene)
+{
+    if (scene)
+    {
+        SubscribeToEvent(scene, E_SCENEUPDATE, ATOMIC_HANDLER(MyNativeComponent, HandleSceneUpdate));
+    }
+    else
+    {
+        UnsubscribeFromEvent(E_SCENEUPDATE);
+    }
+
+}
+
+
+void MyNativeComponent::HandleSceneUpdate(StringHash eventType, VariantMap& eventData)
+{
+    using namespace SceneUpdate;
+    GetNode()->Yaw(speed_ * 75.0f * eventData[P_TIMESTEP].GetFloat());
+}
+
+void MyNativeComponent::RegisterObject(Context* context)
+{
+    context->RegisterFactory<MyNativeComponent>();
+    ATOMIC_ATTRIBUTE("Speed", float, speed_, 1.0f, AM_DEFAULT);
+
+}
+
+
+}

+ 59 - 0
Source/MyProject/MyNativeComponent.h

@@ -0,0 +1,59 @@
+//
+// Copyright (c) 2017 THUNDERBEAST GAMES LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#pragma once
+
+#include <Atomic/Scene/Component.h>
+
+namespace Atomic
+{
+
+/// Simple Native Component example which spins a node
+/// using Speed set in Atomic Editor inspector
+class MyNativeComponent : public Component
+{
+    ATOMIC_OBJECT(MyNativeComponent, Component)
+
+public:
+    /// Construct.
+    MyNativeComponent(Context* context);
+
+    /// Destruct.
+    virtual ~MyNativeComponent();
+
+    /// Register object factory.
+    static void RegisterObject(Context* context);
+
+protected:
+
+    virtual void OnSceneSet(Scene* scene);
+
+private:
+
+    void HandleSceneUpdate(StringHash eventType, VariantMap& eventData);
+
+    float speed_;
+
+};
+
+
+}

+ 41 - 0
Source/MyProject/MyProjectModule.cpp

@@ -0,0 +1,41 @@
+//
+// Copyright (c) 2017 THUNDERBEAST GAMES LLC
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+//
+
+#include <AtomicJS/Javascript/JSVM.h>
+#include "MyNativeComponent.h"
+
+namespace Atomic
+{
+
+// Standard native module registration
+void RegisterMyProjectModule(Context* context)
+{
+    // JS support for Atomic Editor, unneccessary once native components
+    // are self registering
+    extern void jsb_package_myproject_init(JSVM*);
+    JSVM::RegisterPackage(jsb_package_myproject_init);
+
+    // tell context about our native component
+    MyNativeComponent::RegisterObject(context);
+}
+
+}