| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- // 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/Container/ArrayPtr.h>
- #include <Atomic/UI/UI.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/Core/CoreEvents.h>
- #include <AtomicJS/Javascript/JSEvents.h>
- #include "UIResourceFrame.h"
- #include "../Editors/JSResourceEditor.h"
- #include "../Editors/SceneEditor3D/SceneEditor3D.h"
- #include "../Editors/ModelResourceEditor.h"
- #include "../Editors/TextResourceEditor.h"
- #include "../AEEvents.h"
- #include "../AEEditor.h"
- #include "UIIssuesWidget.h"
- #include "UIErrorsWidget.h"
- #include "UIConsoleWidget.h"
- #include "License/AELicenseSystem.h"
- #include "Modal/UIModalOps.h"
- #include "../Tools/External/AEExternalTooling.h"
- using namespace tb;
- namespace AtomicEditor
- {
- ResourceFrame::ResourceFrame(Context* context) :
- AEWidget(context),
- tabcontainer_(0),
- resourceLayout_(0)
- {
- UI* tbui = GetSubsystem<UI>();
- tbui->LoadResourceFile(delegate_, "AtomicEditor/editor/ui/resourceframe.tb.txt");
- tabcontainer_ = delegate_->GetWidgetByIDAndType<TBTabContainer>(TBIDC("tabcontainer"));
- assert(tabcontainer_);
- resourceLayout_ = delegate_->GetWidgetByIDAndType<TBLayout>(TBIDC("resourcelayout"));
- assert(resourceLayout_);
- delegate_->SetGravity(WIDGET_GRAVITY_ALL);
- issueswidget_ = new IssuesWidget(context_);
- errorswidget_ = new ErrorsWidget(context_);
- consolewidget_ = new ConsoleWidget(context_);
- SubscribeToEvent(E_FINDTEXT, HANDLER(ResourceFrame, HandleFindText));
- SubscribeToEvent(E_FINDTEXTCLOSE, HANDLER(ResourceFrame, HandleFindTextClose));
- SubscribeToEvent(E_EDITORPLAYSTARTED, HANDLER(ResourceFrame, HandlePlayStarted));
- SubscribeToEvent(E_EDITORPLAYSTOPPED, HANDLER(ResourceFrame, HandlePlayStopped));
- }
- ResourceFrame::~ResourceFrame()
- {
- }
- void ResourceFrame::HandleFindText(StringHash eventType, VariantMap& eventData)
- {
- using namespace FindText;
- if (!editors_.Size())
- return;
- int page = tabcontainer_->GetCurrentPage();
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (editorLookup_.Contains(widget))
- editorLookup_[widget]->FindText(eventData[P_TEXT].ToString(),
- (unsigned) eventData[P_FLAGS].GetInt());
- }
- void ResourceFrame::HandleFindTextClose(StringHash eventType, VariantMap& eventData)
- {
- if (!editors_.Size())
- return;
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (editorLookup_.Contains(widget))
- return editorLookup_[widget]->FindTextClose();
- }
- void ResourceFrame::EditResource(const String& fullpath)
- {
- if (editors_.Contains(fullpath))
- {
- NavigateToResource(fullpath);
- if (GetSubsystem<Editor>()->IsPlayingProject())
- {
- SendEvent(E_EDITORPLAYSTOP);
- }
- return;
- }
- delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
- String ext = GetExtension(fullpath);
- ResourceEditor* editor = NULL;
- if (ext == ".js")
- {
- JSResourceEditor* jse = new JSResourceEditor(context_, fullpath, tabcontainer_);
- editor = jse;
- }
- else if (ext == ".scene")
- {
- SceneEditor3D* sre = new SceneEditor3D(context_, fullpath, tabcontainer_);
- editor = sre;
- }
- else if (ext == ".xml" || ext == ".txt")
- {
- //SceneResourceEditor* sre = new SceneResourceEditor(context_, fullpath, tabcontainer_);
- TextResourceEditor* tre = new TextResourceEditor(context_, fullpath, tabcontainer_);
- editor = tre;
- }
- else if (ext == ".mdl")
- {
- //ModelResourceEditor* mre = new ModelResourceEditor(context_, fullpath, tabcontainer_);
- //editor = mre;
- }
- else if (ext == ".png" || ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".ogg")
- {
- FileSystem* fs = GetSubsystem<FileSystem>();
- fs->SystemOpen(fullpath);
- }
- else if (ext == ".tmx")
- {
- ExternalTooling* tooling = GetSubsystem<ExternalTooling>();
- tooling->LaunchOrOpen("AtomicTiled", fullpath);
- }
- if (editor)
- {
- // We have a new editor so send a stop playing if we are playing
- if (GetSubsystem<Editor>()->IsPlayingProject())
- {
- SendEvent(E_EDITORPLAYSTOP);
- }
- editors_[fullpath] = editor;
- editorLookup_[editor->GetRootContentWidget()] = editor;
- tabcontainer_->SetCurrentPage(tabcontainer_->GetNumPages()-1);
- editor->SetFocus();
- // BEGIN LICENSE MANAGEMENT
- LicenseSystem* licenseSystem = GetSubsystem<LicenseSystem>();
- if(licenseSystem->IsStandardLicense())
- {
- if (ext == ".scene" )
- {
- UIModalOps* ops = GetSubsystem<UIModalOps>();
- ops->ShowInfoModule3D();
- }
- }
- // END LICENSE MANAGEMENT
- }
- }
- void ResourceFrame::FocusCurrentTab()
- {
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (widget && editorLookup_.Contains(widget))
- return editorLookup_[widget]->SetFocus();
- }
- bool ResourceFrame::OnEvent(const TBWidgetEvent &ev)
- {
- if (ev.type == EVENT_TYPE_TAB_CHANGED && ev.target == tabcontainer_)
- {
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (widget)
- {
- if (editorLookup_.Contains(widget))
- {
- ResourceEditor* editor = editorLookup_[widget];
- if (editor != currentResourceEditor_)
- {
- VariantMap eventData;
- eventData[EditorResourceEditorChanged::P_RESOURCEEDITOR] = editor;
- SendEvent(E_EDITORRESOURCEEDITORCHANGED, eventData);
- currentResourceEditor_ = editor;
- }
- }
- }
- return true;
- }
- if (ev.type == EVENT_TYPE_KEY_DOWN || ev.type == EVENT_TYPE_SHORTCUT
- || ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_RIGHT_POINTER_UP)
- {
- if (!editors_.Size())
- return false;
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (editorLookup_.Contains(widget))
- return editorLookup_[widget]->OnEvent(ev);
- }
- return false;
- }
- bool ResourceFrame::IssuesWidgetVisible()
- {
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == issueswidget_->GetWidgetDelegate())
- break;
- }
- return child != NULL;
- }
- void ResourceFrame::ShowIssuesWidget(bool show)
- {
- if (show && ErrorsWidgetVisible())
- ShowErrorsWidget(false);
- if (show && ConsoleWidgetVisible())
- ShowConsoleWidget(false);
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == issueswidget_->GetWidgetDelegate())
- break;
- }
- if (show)
- {
- if (!child)
- {
- resourceLayout_->AddChild(issueswidget_->GetWidgetDelegate());
- }
- issueswidget_->UpdateIssues();
- }
- else
- {
- if (child)
- resourceLayout_->RemoveChild(child);
- }
- }
- bool ResourceFrame::ErrorsWidgetVisible()
- {
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == errorswidget_->GetWidgetDelegate())
- break;
- }
- return child != NULL;
- }
- void ResourceFrame::ShowErrorsWidget(bool show)
- {
- if (show && ConsoleWidgetVisible())
- ShowConsoleWidget(false);
- if (show && IssuesWidgetVisible())
- ShowIssuesWidget(false);
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == errorswidget_->GetWidgetDelegate())
- break;
- }
- if (show)
- {
- if (!child)
- {
- resourceLayout_->AddChild(errorswidget_->GetWidgetDelegate());
- }
- errorswidget_->UpdateErrors();
- }
- else
- {
- if (child)
- resourceLayout_->RemoveChild(child);
- }
- }
- bool ResourceFrame::ConsoleWidgetVisible()
- {
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == consolewidget_->GetWidgetDelegate())
- break;
- }
- return child != NULL;
- }
- void ResourceFrame::ShowConsoleWidget(bool show)
- {
- if (show && ErrorsWidgetVisible())
- ShowErrorsWidget(false);
- if (show && IssuesWidgetVisible())
- ShowIssuesWidget(false);
- TBWidget *child;
- for (child = resourceLayout_->GetFirstChild(); child; child = child->GetNext())
- {
- if (child == consolewidget_->GetWidgetDelegate())
- break;
- }
- if (show)
- {
- if (!child)
- {
- resourceLayout_->AddChild(consolewidget_->GetWidgetDelegate());
- }
- }
- else
- {
- if (child)
- resourceLayout_->RemoveChild(child);
- }
- }
- void ResourceFrame::SendCurrentEditorEvent(const TBWidgetEvent &ev)
- {
- TBWidget* widget = tabcontainer_->GetCurrentPageWidget();
- if (!widget)
- return;
- if (editorLookup_.Contains(widget))
- editorLookup_[widget]->OnEvent(ev);
- }
- void ResourceFrame::NavigateToResource(const String& fullpath, int lineNumber, int tokenPos)
- {
- if (!editors_.Contains(fullpath))
- return;
- ResourceEditor* editor = editors_[fullpath];
- TBWidget* root = tabcontainer_->GetContentRoot();
- int i = 0;
- for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext(), i++)
- {
- if (editorLookup_.Contains(child))
- {
- if (editorLookup_[child] == editor)
- {
- break;
- }
- }
- }
- if (i < tabcontainer_->GetNumPages())
- {
- tabcontainer_->SetCurrentPage(i);
- editor->SetFocus();
- // this cast could be better
- String ext = GetExtension(fullpath);
- if (ext == ".js" && lineNumber != -1)
- {
- JSResourceEditor* jse = (JSResourceEditor*) editor;
- jse->GotoLineNumber(lineNumber);
- }
- else if (ext == ".js" && tokenPos != -1)
- {
- JSResourceEditor* jse = (JSResourceEditor*) editor;
- jse->GotoTokenPos(tokenPos);
- }
- }
- }
- bool ResourceFrame::HasUnsavedModifications()
- {
- HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr;
- for (itr = editors_.Begin(); itr != editors_.End(); itr++)
- {
- if (itr->second_->HasUnsavedModifications())
- return true;
- }
- return false;
- }
- void ResourceFrame::CloseResourceEditor(ResourceEditor* editor, bool navigateToAvailableResource)
- {
- assert(editors_.Contains(editor->GetFullPath()));
- editors_.Erase(editor->GetFullPath());
- TBWidget* root = tabcontainer_->GetContentRoot();
- bool found = false;
- for (TBWidget *child = root->GetFirstChild(); child; child = child->GetNext())
- {
- if (editorLookup_.Contains(child))
- {
- if (editorLookup_[child] == editor)
- {
- found = true;
- root->RemoveChild(child);
- editorLookup_.Erase(child);
- break;
- }
- }
- }
- assert(found);
- tabcontainer_->SetCurrentPage(-1);
- if (navigateToAvailableResource)
- {
- if (editors_.Size())
- {
- HashMap<String, SharedPtr<ResourceEditor> >::ConstIterator itr = editors_.End();
- itr--;
- NavigateToResource(itr->second_->GetFullPath());
- }
- }
- }
- void ResourceFrame::HandlePlayStarted(StringHash eventType, VariantMap& eventData)
- {
- //delegate_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
- //delegate_->SetIgnoreInput(true);
- //delegate_->SetState(WIDGET_STATE_DISABLED, true);
- }
- void ResourceFrame::HandlePlayStopped(StringHash eventType, VariantMap& eventData)
- {
- //delegate_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
- //delegate_->SetIgnoreInput(false);
- //delegate_->SetState(WIDGET_STATE_DISABLED, false);
- }
- void ResourceFrame::CloseAllResourceEditors()
- {
- Vector<SharedPtr<ResourceEditor> > editors = editors_.Values();
- for (unsigned i = 0; i < editors.Size(); i++)
- editors[i]->Close(false);
- }
- }
|