| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293 |
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // Please see LICENSE.md in repository root for license information
- // https://github.com/AtomicGameEngine/AtomicGameEngine
- #include "AtomicEditor.h"
- #include <Atomic/Core/Context.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include "../AEEvents.h"
- #include "../AEEditor.h"
- #include "../UI/UIMainFrame.h"
- #include "../UI/UIResourceFrame.h"
- #include "../UI/UIProjectFrame.h"
- #include "../Project/AEProject.h"
- #include "AEResourceOps.h"
- namespace AtomicEditor
- {
- /// Construct.
- ResourceOps::ResourceOps(Context* context) :
- Object(context)
- {
- context->RegisterSubsystem(this);
- }
- /// Destruct.
- ResourceOps::~ResourceOps()
- {
- }
- bool ResourceOps::CheckCreateComponent(const String& resourcePath, const String& resourceName, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- Project* project = editor->GetProject();
- String fullpath = resourcePath + resourceName;
- if (!resourceName.EndsWith(".js"))
- fullpath += ".js";
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (fs->FileExists(fullpath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("The component:\n\n%s\n\nalready exists", fullpath.CString());
- editor->PostModalError("Create Component Error", errorMsg);
- }
- return false;
- }
- if (!project->IsComponentsDirOrFile(resourcePath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Components must reside in or in a subfolder of the Components folder");
- editor->PostModalError("Create Component Error", errorMsg);
- }
- return false;
- }
- return true;
- }
- bool ResourceOps::CheckCreateScript(const String& resourcePath, const String& resourceName, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- Project* project = editor->GetProject();
- String fullpath = resourcePath + resourceName;
- if (!resourceName.EndsWith(".js"))
- fullpath += ".js";
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (fs->FileExists(fullpath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("The script:\n\n%s\n\nalready exists", fullpath.CString());
- editor->PostModalError("Create Script Error", errorMsg);
- }
- return false;
- }
- if (!project->IsScriptsDirOrFile(resourcePath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Scripts must reside in or in a subfolder of the Scripts folder");
- editor->PostModalError("Create Script Error", errorMsg);
- }
- return false;
- }
- return true;
- }
- bool ResourceOps::CopyFile(File* srcFile, const String& destFileName)
- {
- SharedPtr<File> destFile(new File(context_, destFileName, FILE_WRITE));
- if (!destFile->IsOpen())
- return false;
- unsigned fileSize = srcFile->GetSize();
- SharedArrayPtr<unsigned char> buffer(new unsigned char[fileSize]);
- unsigned bytesRead = srcFile->Read(buffer.Get(), fileSize);
- unsigned bytesWritten = destFile->Write(buffer.Get(), fileSize);
- return bytesRead == fileSize && bytesWritten == fileSize;
- }
- void ResourceOps::HandleResourceDelete(const String& resourcePath, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (fs->DirExists(resourcePath))
- {
- if (reportError)
- editor->PostModalError("Delete Resource Error", "Deleting Folders not currently implemented");
- return;
- }
- else if (fs->FileExists(resourcePath))
- {
- if (!fs->Delete(resourcePath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Unable to delete:\n\n %s", resourcePath.CString());
- editor->PostModalError("Delete Resource Error", errorMsg);
- }
- return;
- }
- GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
- return;
- }
- else
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Unable to find:\n\n %s", resourcePath.CString());
- editor->PostModalError("Delete Resource Error", errorMsg);
- }
- return;
- }
- }
- void ResourceOps::HandleNewFolder(const String& resourcePath, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- FileSystem* fs = GetSubsystem<FileSystem>();
- if (fs->DirExists(resourcePath) || fs->FileExists(resourcePath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Already exists:\n\n %s", resourcePath.CString());
- editor->PostModalError("New Folder Error", errorMsg);
- }
- return;
- }
- if (!fs->CreateDir(resourcePath))
- {
- if (reportError)
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Could not create:\n\n %s", resourcePath.CString());
- editor->PostModalError("New Folder Error", errorMsg);
- }
- return;
- }
- // file watcher doesn't currently handle subdir
- GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
- }
- void ResourceOps::HandleCreateComponent(const String& resourcePath, const String& resourceName,
- bool navigateToResource, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- if (!CheckCreateComponent(resourcePath, resourceName, reportError))
- return;
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_component.js");
- if (srcFile.Null() || !srcFile->IsOpen())
- {
- editor->PostModalError("Create Component Error", "Could not open component template");
- return;
- }
- String fullpath = resourcePath + resourceName;
- if (!resourceName.EndsWith(".js"))
- fullpath += ".js";
- if (!CopyFile(srcFile, fullpath))
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
- "AtomicEditor/template_component.js", fullpath.CString());
- editor->PostModalError("Create Component Error", errorMsg);
- return;
- }
- if (navigateToResource)
- {
- ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
- rframe->EditResource(fullpath);
- }
- GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
- }
- void ResourceOps::HandleCreateScript(const String& resourcePath, const String& resourceName,
- bool navigateToResource, bool reportError)
- {
- Editor* editor = GetSubsystem<Editor>();
- if (!CheckCreateScript(resourcePath, resourceName, reportError))
- return;
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- SharedPtr<File> srcFile = cache->GetFile("AtomicEditor/templates/template_script.js");
- if (srcFile.Null() || !srcFile->IsOpen())
- {
- editor->PostModalError("Create Script Error", "Could not open script template");
- return;
- }
- String fullpath = resourcePath + resourceName;
- if (!resourceName.EndsWith(".js"))
- fullpath += ".js";
- if (!CopyFile(srcFile, fullpath))
- {
- String errorMsg;
- errorMsg.AppendWithFormat("Error copying template:\n\n%s\n\nto:\n\n%s",
- "AtomicEditor/template_script.js", fullpath.CString());
- editor->PostModalError("Create Script Error", errorMsg);
- return;
- }
- if (navigateToResource)
- {
- ResourceFrame* rframe = GetSubsystem<MainFrame>()->GetResourceFrame();
- rframe->EditResource(fullpath);
- }
- GetSubsystem<MainFrame>()->GetProjectFrame()->Refresh();
- }
- }
|