Skip to content

Instantly share code, notes, and snippets.

@keithjohnston
Created November 27, 2016 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithjohnston/bbc7512198438fabbcb7b929417d3ee3 to your computer and use it in GitHub Desktop.
Save keithjohnston/bbc7512198438fabbcb7b929417d3ee3 to your computer and use it in GitHub Desktop.
AEEditorApp.cpp
//
// Copyright (c) 2014-2016 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/Input/Input.h>
#include <Atomic/UI/UI.h>
#include <AtomicJS/Javascript/Javascript.h>
#include <Atomic/IPC/IPC.h>
// This can be removed once bone hack is fixed
#include <Atomic/Graphics/AnimatedModel.h>
#include <ToolCore/License/LicenseSystem.h>
#include <ToolCore/ToolSystem.h>
#include <ToolCore/ToolEnvironment.h>
#include <ToolCore/NETTools/NETBuildSystem.h>
#include "../EditorMode/AEEditorMode.h"
#include "../EditorMode/AEEditorNETService.h"
#include "../Components/EditorComponents.h"
#include "AEEditorPrefs.h"
#include "AEEditorApp.h"
#include <Atomic/UI/UI.h>
#include <Atomic/UI/UIView.h>
#include <Atomic/UI/UIButton.h>
#include <Atomic/UI/UILayout.h>
#include <Atomic/UI/UIWindow.h>
#include <Atomic/Core/CoreEvents.h>
#include <Atomic/Graphics/Graphics.h>
#include <Atomic/UI/UIEvents.h>
#include <Atomic/Graphics/GraphicsEvents.h>
#include <Atomic/Scene/Scene.h>
#include <Atomic/Graphics/Camera.h>
#include <Atomic/Graphics/Texture2D.h>
#include <Atomic/Graphics/Octree.h>
#include <Atomic/Graphics/Renderer.h>
#include <Atomic/Graphics/Viewport.h>
#include <Atomic/Graphics/DebugRenderer.h>
#include <Atomic/Atomic2D/Sprite2D.h>
#include <Atomic/Atomic2D/StaticSprite2D.h>
#include <Atomic/Resource/ResourceCache.h>
using namespace ToolCore;
// Fix these externs
namespace Atomic
{
void jsapi_init_webview(JSVM* vm, const VariantMap& engineParameters);
extern void jsb_package_atomicnetscript_init(JSVM* vm);
}
namespace ToolCore
{
extern void jsapi_init_toolcore(JSVM* vm);
}
using namespace ToolCore;
namespace AtomicEditor
{
extern void jsapi_init_editor(JSVM* vm);
AEEditorApp::AEEditorApp(Context* context) :
AppBase(context)
{
}
AEEditorApp::~AEEditorApp()
{
}
void AEEditorApp::Setup()
{
context_->RegisterSubsystem(new AEEditorPrefs(context_));
context_->SetEditorContext(true);
AppBase::Setup();
RegisterEditorComponentLibrary(context_);
// Register IPC system
context_->RegisterSubsystem(new IPC(context_));
ToolEnvironment* env = new ToolEnvironment(context_);
context_->RegisterSubsystem(env);
env->Initialize();
ToolSystem* system = new ToolSystem(context_);
context_->RegisterSubsystem(system);
engineParameters_["WindowTitle"] = "AtomicEditor";
engineParameters_["WindowResizable"] = true;
engineParameters_["FullScreen"] = false;
engineParameters_["LogLevel"] = LOG_DEBUG;
FileSystem* filesystem = GetSubsystem<FileSystem>();
engineParameters_["LogName"] = filesystem->GetAppPreferencesDir("AtomicEditor", "Logs") + "AtomicEditor.log";
#ifdef ATOMIC_PLATFORM_OSX
engineParameters_["WindowIcon"] = "Images/AtomicLogo32.png";
#endif
#ifdef ATOMIC_DEV_BUILD
engineParameters_["ResourcePrefixPaths"] = "";
String resourcePaths = env->GetCoreDataDir() + ";" + env->GetEditorDataDir();
// for dev builds, add the compile editor scripts from artifacts
resourcePaths += ";" + env->GetRootSourceDir() + "Artifacts/Build/Resources/EditorData/";
engineParameters_["ResourcePaths"] = resourcePaths;
#else
#ifdef ATOMIC_PLATFORM_OSX
engineParameters_["ResourcePrefixPaths"] = filesystem->GetProgramDir() + "../Resources";
#else
engineParameters_["ResourcePrefixPaths"] = filesystem->GetProgramDir() + "Resources";
#endif
engineParameters_["ResourcePaths"] = "CoreData;EditorData";
#endif // ATOMIC_DEV_BUILD
GetSubsystem<AEEditorPrefs>()->ReadPreferences(engineParameters_);
// Register JS packages
JSVM::RegisterPackage(jsapi_init_toolcore);
JSVM::RegisterPackage(jsapi_init_editor);
JSVM::RegisterPackage(jsb_package_atomicnetscript_init);
#ifdef ATOMIC_WEBVIEW
JSVM::RegisterPackage(jsapi_init_webview, engineParameters_);
#endif
}
void AEEditorApp::Start()
{
GetSubsystem<AEEditorPrefs>()->ValidateWindow();
context_->RegisterSubsystem(new EditorMode(context_));
context_->RegisterSubsystem(new NETBuildSystem(context_));
context_->RegisterSubsystem(new EditorNETService(context_));
AppBase::Start();
vm_->SetModuleSearchPaths("AtomicEditor/JavaScript;AtomicEditor/EditorScripts;AtomicEditor/EditorScripts/AtomicEditor");
// move UI initialization to JS
UI* ui = GetSubsystem<UI>();
ui->Initialize("AtomicEditor/resources/language/lng_en.tb.txt");
/*
duk_get_global_string(vm_->GetJSContext(), "require");
duk_push_string(vm_->GetJSContext(), "main");
if (duk_pcall(vm_->GetJSContext(), 1) != 0)
{
vm_->SendJSErrorEvent();
ErrorExit("Error executing main.js");
}
*/
GetSubsystem<LicenseSystem>()->Initialize();
Input* input = GetSubsystem<Input>();
// Ensure exclusive fullscreen is disabled in Editor application
input->SetToggleFullscreen(false);
input->SetMouseVisible(true);
scene = new Scene(GetContext());
octree = scene->CreateComponent<Octree>();
Graphics* graphics = GetSubsystem<Graphics>();
Node* cameraNode = scene->CreateChild("Camera");
cameraNode->SetPosition(Vector3(0.0f, 0.0f, -10.0f));
camera = cameraNode->CreateComponent<Camera>();
camera->SetOrthographic(true);
camera->SetOrthoSize((float)graphics->GetHeight() * PIXEL_SIZE);
//ResourceCache* cache = GetSubsystem<ResourceCache>();
//Texture2D* testTex = cache->GetResource<Texture2D>("AtomicEditor/editor/images/newproject_2d.png");
Image* im = new Image(GetContext());
im->SetSize(16,16,4);
Color color(1.0,0.0,0.0,1.0);
for (int i=0;i<16;i++ ) {
for (int j=0;j<16;j++ ) {
im->SetPixel(i,j,color);
}
}
unsigned char data[256];
Texture2D* testTex = new Texture2D(GetContext());
testTex->SetFilterMode(TextureFilterMode::FILTER_NEAREST);
testTex->SetNumLevels(1);
testTex->SetSize(16,16, Graphics::GetRGBAFormat(), TEXTURE_STATIC);
testTex->SetData(0,0,0,16,16,im->GetData());
float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE;
float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE;
Sprite2D* sp = new Sprite2D(GetContext());
sp->SetTexture(testTex);
sp->SetRectangle(IntRect(0,0,16,16));
SharedPtr<Node> spriteNode(scene->CreateChild("StaticSprite2D"));
StaticSprite2D* staticSprite = spriteNode->CreateComponent<StaticSprite2D>();
spriteNode->SetPosition(Vector3(0, 1, 0.0f));
//spriteNode->SetRotation2D(45.0);
staticSprite->SetSprite(sp);
octree->AddDrawable(staticSprite);
Renderer* renderer = GetSubsystem<Renderer>();
DebugRenderer* debug = scene->CreateComponent<DebugRenderer>();
// Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
viewport = new Viewport(GetContext(), scene, cameraNode->GetComponent<Camera>());
//viewport->SetDrawDebug(true);
renderer->SetViewport(0, viewport);
SubscribeToEvent(E_MOUSEBUTTONDOWN, ATOMIC_HANDLER(AEEditorApp, HandleMouseButtonDown));
SubscribeToEvent(E_MOUSEBUTTONUP, ATOMIC_HANDLER(AEEditorApp, HandleMouseButtonUp));
SubscribeToEvent(E_MOUSEMOVE, ATOMIC_HANDLER(AEEditorApp, HandleMouseMoved));
}
void AEEditorApp::Stop()
{
IPC* ipc = GetSubsystem<IPC>();
if (ipc)
{
ipc->Shutdown();
}
AppBase::Stop();
}
void AEEditorApp::ProcessArguments()
{
AppBase::ProcessArguments();
}
void AEEditorApp::HandleMouseButtonDown(StringHash eventType, VariantMap& eventData)
{
printf("Mouse down!\n");
Input* input = GetSubsystem<Input>();
Graphics* graphics = GetSubsystem<Graphics>();
IntVector2 pos = input->GetMousePosition();
float mousePosX = (float)pos.x_ / (float)graphics->GetWidth();
float mousePosY = (float)pos.y_ / (float)graphics->GetHeight();
printf("Casting ray at %f,%f\n",mousePosX,mousePosY);
Ray ray = camera->GetScreenRay(mousePosX, mousePosY);
PODVector<RayQueryResult> result;
RayOctreeQuery query(result, ray, RAY_TRIANGLE, M_INFINITY, DRAWABLE_ANY);
octree->RaycastSingle(query);
if (result.Size()>0)
{
printf("Got %d results\n", result.Size());
RayQueryResult r = result[0];
dragObject = r.drawable_;
}
else
{
dragObject = NULL;
}
}
void AEEditorApp::HandleMouseButtonUp(StringHash eventType, VariantMap& eventData)
{
printf("Mouse up!\n");
if (dragObject)
{
Node* n = dragObject->GetNode();
n->SetPosition(Vector3(0,1,0));
}
dragObject = NULL;
}
void AEEditorApp::HandleMouseMoved(StringHash eventType, VariantMap& eventData)
{
int x = eventData[MouseMove::P_X].GetInt();
int y = eventData[MouseMove::P_Y].GetInt();
printf("Mouse moved: %d, %d\n", x, y);
Graphics* graphics = GetSubsystem<Graphics>();
float halfWidth = graphics->GetWidth() * 0.5f * PIXEL_SIZE;
float halfHeight = graphics->GetHeight() * 0.5f * PIXEL_SIZE;
float mousePosX = (float)x / (float)graphics->GetWidth();
float mousePosY = (float)y / (float)graphics->GetHeight();
Vector3 worldPoint = viewport->ScreenToWorldPoint(x, y, 0.0f);
printf("Mouse moved: %f, %f\n", mousePosX, mousePosY);
if (dragObject)
{
Node* n = dragObject->GetNode();
printf("New loc: %f, %f", worldPoint.x_, worldPoint.y_);
n->SetPosition(worldPoint);
}
}
}
@Type1J
Copy link

Type1J commented Nov 29, 2016

I don't know if this is the cause of your problem (probably not), but it still needs to be addressed: Around line 254 or so you need a

        dragObject = NULL;

else the first mouse move might corrupt memory, and possibly cause your problem to occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment