ResourceEditor.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "ResourceEditor.h"
  12. //#include "../UI/UIMainFrame.h"
  13. //#include "../UI/UIResourceFrame.h"
  14. namespace AtomicEditor
  15. {
  16. class EditorTabLayout: public TBLayout
  17. {
  18. public:
  19. ResourceEditor* editor_;
  20. TBButton* button_;
  21. TBButton* close_;
  22. TBTabContainer* container_;
  23. void SetValue(int value)
  24. {
  25. button_->SetValue(value);
  26. }
  27. bool RequestClose()
  28. {
  29. if (editor_->HasUnsavedModifications())
  30. {
  31. TBMessageWindow *msg_win = new TBMessageWindow(this, TBIDC("unsaved_modifications_dialog"));
  32. TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
  33. settings.dimmer = true;
  34. settings.styling = true;
  35. msg_win->Show("Unsaved Modifications", "There are unsaved modifications.\nDo you wish to discard them and close?", &settings, 640, 360);
  36. return false;
  37. }
  38. else
  39. {
  40. editor_->Close();
  41. return true;
  42. }
  43. }
  44. bool OnEvent(const TBWidgetEvent &ev)
  45. {
  46. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  47. {
  48. if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
  49. {
  50. if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
  51. {
  52. container_->OnEvent(ev);
  53. editor_->Close();
  54. }
  55. else
  56. {
  57. editor_->SendEvent("EditorResourceCloseCanceled");
  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. }
  139. void ResourceEditor::InvokeShortcut(const String& shortcut)
  140. {
  141. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  142. ev.ref_id = TBIDC(shortcut.CString());
  143. OnEvent(ev);
  144. }
  145. void ResourceEditor::SetModified(bool modified)
  146. {
  147. modified_ = modified;
  148. if (modified)
  149. {
  150. String filename = GetFileNameAndExtension(fullpath_);
  151. filename += "*";
  152. button_->SetText(filename.CString());
  153. }
  154. else
  155. {
  156. String filename = GetFileNameAndExtension(fullpath_);
  157. button_->SetText(filename.CString());
  158. }
  159. }
  160. }