ResourceEditor.cpp 5.5 KB

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