ResourceEditor.cpp 5.8 KB

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