ResourceEditor.cpp 6.2 KB

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