ResourceEditor.cpp 4.9 KB

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