ResourceEditor.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // LICENSE: Atomic Game Engine Editor and Tools EULA
  4. // Please see LICENSE_ATOMIC_EDITOR_AND_TOOLS.md in repository root for
  5. // license information: https://github.com/AtomicGameEngine/AtomicGameEngine
  6. //
  7. #include <TurboBadger/tb_tab_container.h>
  8. #include <Atomic/IO/FileSystem.h>
  9. #include <Atomic/Resource/ResourceEvents.h>
  10. #include <TurboBadger/tb_message_window.h>
  11. #include "ResourceEditorEvents.h"
  12. #include "ResourceEditor.h"
  13. //#include "../UI/UIMainFrame.h"
  14. //#include "../UI/UIResourceFrame.h"
  15. namespace AtomicEditor
  16. {
  17. class EditorTabLayout: public TBLayout
  18. {
  19. public:
  20. ResourceEditor* editor_;
  21. TBButton* button_;
  22. TBButton* close_;
  23. TBTabContainer* container_;
  24. void SetValue(int value)
  25. {
  26. button_->SetValue(value);
  27. }
  28. bool RequestClose()
  29. {
  30. if (editor_->HasUnsavedModifications())
  31. {
  32. TBMessageWindow *msg_win = new TBMessageWindow(this, TBIDC("unsaved_modifications_dialog"));
  33. TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
  34. settings.dimmer = true;
  35. settings.styling = true;
  36. msg_win->Show("Unsaved Modifications", "There are unsaved modifications.\nDo you wish to discard them and close?", &settings, 640, 360);
  37. return false;
  38. }
  39. else
  40. {
  41. return true;
  42. }
  43. }
  44. bool OnEvent(const TBWidgetEvent &ev)
  45. {
  46. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  47. {
  48. if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
  49. {
  50. if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
  51. {
  52. SharedPtr<ResourceEditor> keepalive(editor_);
  53. container_->OnEvent(ev);
  54. editor_->Close();
  55. VariantMap editorClosedEvent;
  56. editorClosedEvent[ResourceEditorClosed::P_RESOURCEEDITOR] = editor_;
  57. editor_->SendEvent(E_RESOURCEEDITORCLOSED, editorClosedEvent);
  58. }
  59. else
  60. {
  61. SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  62. }
  63. return true;
  64. }
  65. if (ev.target->GetID() == TBIDC("tabclose"))
  66. {
  67. if (RequestClose())
  68. {
  69. container_->OnEvent(ev);
  70. editor_->Close();
  71. return true;
  72. }
  73. }
  74. else
  75. {
  76. TBWidgetEvent nevent = ev;
  77. nevent.target = this;
  78. container_->OnEvent(nevent);
  79. }
  80. }
  81. return false;
  82. }
  83. };
  84. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  85. Object(context), fullpath_(fullpath), container_(container),
  86. editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
  87. {
  88. String filename = GetFileNameAndExtension(fullpath_);
  89. editorTabLayout_ = new EditorTabLayout();
  90. editorTabLayout_->SetID(TBIDC("tab"));
  91. button_ = new UIButton(context_);
  92. button_->SetText(filename.CString());
  93. button_->SetSqueezable(true);
  94. button_->SetSkinBg("TBButton.flat");
  95. button_->SetValue(1);
  96. editorTabLayout_->AddChild(button_->GetInternalWidget());
  97. TBButton* closebutton = new TBButton();
  98. editorTabLayout_->AddChild(closebutton);
  99. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  100. closebutton->SetIsFocusable(false);
  101. closebutton->SetID(TBIDC("tabclose"));
  102. editorTabLayout_->editor_ = this;
  103. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  104. editorTabLayout_->close_ = closebutton;
  105. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  106. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  107. rootContentWidget_ = new UIWidget(context_);
  108. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  109. container_->GetContentRoot()->AddChild(rootContentWidget_);
  110. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  111. }
  112. ResourceEditor::~ResourceEditor()
  113. {
  114. }
  115. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  116. {
  117. /*
  118. using namespace FileChanged;
  119. const String& fileName = eventData[P_FILENAME].GetString();
  120. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  121. if (fullpath_ == fileName)
  122. {
  123. FileSystem* fs = GetSubsystem<FileSystem>();
  124. if (!fs->FileExists(fullpath_))
  125. Close();
  126. }
  127. */
  128. }
  129. void ResourceEditor::RequestClose()
  130. {
  131. editorTabLayout_->RequestClose();
  132. }
  133. void ResourceEditor::Close(bool navigateToAvailableResource)
  134. {
  135. // keep us alive through the close
  136. SharedPtr<ResourceEditor> keepalive(this);
  137. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  138. VariantMap data;
  139. data["Editor"] = this;
  140. data["NavigateToAvailableResource"] = navigateToAvailableResource;
  141. SendEvent("EditorCloseResource", data);
  142. }
  143. void ResourceEditor::InvokeShortcut(const String& shortcut)
  144. {
  145. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  146. ev.ref_id = TBIDC(shortcut.CString());
  147. OnEvent(ev);
  148. }
  149. void ResourceEditor::SetModified(bool modified)
  150. {
  151. modified_ = modified;
  152. if (modified)
  153. {
  154. String filename = GetFileNameAndExtension(fullpath_);
  155. filename += "*";
  156. button_->SetText(filename.CString());
  157. }
  158. else
  159. {
  160. String filename = GetFileNameAndExtension(fullpath_);
  161. button_->SetText(filename.CString());
  162. }
  163. }
  164. }