ResourceEditor.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. TBButton *btn = new TBButton();
  40. msg_win->AddButton("dont_save", false, msg_win->GetWidgetByIDAndType<TBLayout>(5));
  41. msg_win->AddButton("TBMessageWindow.cancel", false);
  42. msg_win->AddButton("save", true);
  43. return false;
  44. }
  45. else
  46. {
  47. editor_->Close(container_->GetNumPages()>1);
  48. return true;
  49. }
  50. }
  51. bool OnEvent(const TBWidgetEvent &ev)
  52. {
  53. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  54. {
  55. if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
  56. {
  57. if (ev.ref_id == TBIDC("dont_save"))
  58. {
  59. container_->OnEvent(ev);
  60. editor_->Close(container_->GetNumPages()>1);
  61. }
  62. else if (ev.ref_id == TBIDC("cancel"))
  63. {
  64. editor_->SendEvent(E_EDITORRESOURCECLOSECANCELED);
  65. SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  66. }
  67. else if (ev.ref_id == TBIDC("save"))
  68. {
  69. editor_->Save();
  70. container_->OnEvent(ev);
  71. editor_->Close(container_->GetNumPages()>1);
  72. }
  73. return true;
  74. }
  75. if (ev.target->GetID() == TBIDC("tabclose"))
  76. {
  77. if (RequestClose())
  78. {
  79. container_->OnEvent(ev);
  80. return true;
  81. }
  82. }
  83. else
  84. {
  85. TBWidgetEvent nevent = ev;
  86. nevent.target = this;
  87. container_->OnEvent(nevent);
  88. }
  89. }
  90. return false;
  91. }
  92. };
  93. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  94. Object(context), fullpath_(fullpath), container_(container),
  95. editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
  96. {
  97. String filename = GetFileNameAndExtension(fullpath_);
  98. editorTabLayout_ = new EditorTabLayout();
  99. editorTabLayout_->SetID(TBIDC("tab"));
  100. button_ = new UIButton(context_);
  101. button_->SetText(filename.CString());
  102. button_->SetSqueezable(true);
  103. button_->SetSkinBg("TBButton.flat");
  104. button_->SetValue(1);
  105. editorTabLayout_->AddChild(button_->GetInternalWidget());
  106. TBButton* closebutton = new TBButton();
  107. editorTabLayout_->AddChild(closebutton);
  108. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  109. closebutton->SetIsFocusable(false);
  110. closebutton->SetID(TBIDC("tabclose"));
  111. editorTabLayout_->editor_ = this;
  112. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  113. editorTabLayout_->close_ = closebutton;
  114. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  115. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  116. rootContentWidget_ = new UIWidget(context_);
  117. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  118. container_->GetContentRoot()->AddChild(rootContentWidget_);
  119. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  120. }
  121. ResourceEditor::~ResourceEditor()
  122. {
  123. }
  124. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  125. {
  126. /*
  127. using namespace FileChanged;
  128. const String& fileName = eventData[P_FILENAME].GetString();
  129. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  130. if (fullpath_ == fileName)
  131. {
  132. FileSystem* fs = GetSubsystem<FileSystem>();
  133. if (!fs->FileExists(fullpath_))
  134. Close();
  135. }
  136. */
  137. }
  138. void ResourceEditor::RequestClose()
  139. {
  140. editorTabLayout_->RequestClose();
  141. }
  142. void ResourceEditor::Close(bool navigateToAvailableResource)
  143. {
  144. // keep us alive through the close
  145. SharedPtr<ResourceEditor> keepalive(this);
  146. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  147. VariantMap data;
  148. data[EditorResourceClose::P_EDITOR] = this;
  149. data[EditorResourceClose::P_NAVIGATE] = navigateToAvailableResource;
  150. SendEvent(E_EDITORRESOURCECLOSE, data);
  151. }
  152. void ResourceEditor::InvokeShortcut(const String& shortcut)
  153. {
  154. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  155. ev.ref_id = TBIDC(shortcut.CString());
  156. OnEvent(ev);
  157. }
  158. void ResourceEditor::SetModified(bool modified)
  159. {
  160. modified_ = modified;
  161. if (modified)
  162. {
  163. String filename = GetFileNameAndExtension(fullpath_);
  164. filename += "*";
  165. button_->SetText(filename.CString());
  166. }
  167. else
  168. {
  169. String filename = GetFileNameAndExtension(fullpath_);
  170. button_->SetText(filename.CString());
  171. }
  172. }
  173. }