ResourceEditor.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. editor_->Close();
  42. return true;
  43. }
  44. }
  45. bool OnEvent(const TBWidgetEvent &ev)
  46. {
  47. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  48. {
  49. if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
  50. {
  51. if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
  52. {
  53. container_->OnEvent(ev);
  54. editor_->Close();
  55. }
  56. else
  57. {
  58. SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  59. }
  60. return true;
  61. }
  62. if (ev.target->GetID() == TBIDC("tabclose"))
  63. {
  64. if (RequestClose())
  65. {
  66. container_->OnEvent(ev);
  67. return true;
  68. }
  69. }
  70. else
  71. {
  72. TBWidgetEvent nevent = ev;
  73. nevent.target = this;
  74. container_->OnEvent(nevent);
  75. }
  76. }
  77. return false;
  78. }
  79. };
  80. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  81. Object(context), fullpath_(fullpath), container_(container),
  82. editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
  83. {
  84. String filename = GetFileNameAndExtension(fullpath_);
  85. editorTabLayout_ = new EditorTabLayout();
  86. editorTabLayout_->SetID(TBIDC("tab"));
  87. button_ = new UIButton(context_);
  88. button_->SetText(filename.CString());
  89. button_->SetSqueezable(true);
  90. button_->SetSkinBg("TBButton.flat");
  91. button_->SetValue(1);
  92. editorTabLayout_->AddChild(button_->GetInternalWidget());
  93. TBButton* closebutton = new TBButton();
  94. editorTabLayout_->AddChild(closebutton);
  95. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  96. closebutton->SetIsFocusable(false);
  97. closebutton->SetID(TBIDC("tabclose"));
  98. editorTabLayout_->editor_ = this;
  99. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  100. editorTabLayout_->close_ = closebutton;
  101. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  102. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  103. rootContentWidget_ = new UIWidget(context_);
  104. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  105. container_->GetContentRoot()->AddChild(rootContentWidget_);
  106. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  107. }
  108. ResourceEditor::~ResourceEditor()
  109. {
  110. }
  111. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  112. {
  113. /*
  114. using namespace FileChanged;
  115. const String& fileName = eventData[P_FILENAME].GetString();
  116. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  117. if (fullpath_ == fileName)
  118. {
  119. FileSystem* fs = GetSubsystem<FileSystem>();
  120. if (!fs->FileExists(fullpath_))
  121. Close();
  122. }
  123. */
  124. }
  125. void ResourceEditor::RequestClose()
  126. {
  127. editorTabLayout_->RequestClose();
  128. }
  129. void ResourceEditor::Close(bool navigateToAvailableResource)
  130. {
  131. // keep us alive through the close
  132. SharedPtr<ResourceEditor> keepalive(this);
  133. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  134. VariantMap data;
  135. data["Editor"] = this;
  136. data["NavigateToAvailableResource"] = navigateToAvailableResource;
  137. SendEvent("EditorCloseResource", data);
  138. VariantMap editorClosedEvent;
  139. editorClosedEvent[ResourceEditorClosed::P_RESOURCEEDITOR] = this;
  140. SendEvent(E_RESOURCEEDITORCLOSED, editorClosedEvent);
  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. }