ResourceEditor.cpp 5.3 KB

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