ResourceEditor.cpp 5.6 KB

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