ResourceEditor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "ResourceEditor.h"
  11. //#include "../UI/UIMainFrame.h"
  12. //#include "../UI/UIResourceFrame.h"
  13. namespace AtomicEditor
  14. {
  15. class EditorTabLayout: public TBLayout
  16. {
  17. public:
  18. ResourceEditor* editor_;
  19. TBButton* button_;
  20. TBButton* close_;
  21. TBTabContainer* container_;
  22. void SetValue(int value)
  23. {
  24. button_->SetValue(value);
  25. }
  26. bool OnEvent(const TBWidgetEvent &ev)
  27. {
  28. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  29. {
  30. if (ev.target->GetID() == TBIDC("tabclose"))
  31. {
  32. container_->OnEvent(ev);
  33. editor_->Close();
  34. return true;
  35. }
  36. else
  37. {
  38. TBWidgetEvent nevent = ev;
  39. nevent.target = this;
  40. container_->OnEvent(nevent);
  41. }
  42. }
  43. return false;
  44. }
  45. };
  46. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  47. Object(context), fullpath_(fullpath), container_(container),
  48. editorTabLayout_(0), rootContentWidget_(0), button_(0)
  49. {
  50. String filename = GetFileNameAndExtension(fullpath_);
  51. editorTabLayout_ = new EditorTabLayout();
  52. editorTabLayout_->SetID(TBIDC("tab"));
  53. button_ = new UIButton(context_);
  54. button_->SetText(filename.CString());
  55. button_->SetSqueezable(true);
  56. button_->SetSkinBg("TBButton.flat");
  57. button_->SetValue(1);
  58. editorTabLayout_->AddChild(button_->GetInternalWidget());
  59. TBButton* closebutton = new TBButton();
  60. editorTabLayout_->AddChild(closebutton);
  61. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  62. closebutton->SetIsFocusable(false);
  63. closebutton->SetID(TBIDC("tabclose"));
  64. editorTabLayout_->editor_ = this;
  65. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  66. editorTabLayout_->close_ = closebutton;
  67. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  68. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  69. rootContentWidget_ = new UIWidget(context_);
  70. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  71. container_->GetContentRoot()->AddChild(rootContentWidget_);
  72. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  73. }
  74. ResourceEditor::~ResourceEditor()
  75. {
  76. }
  77. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  78. {
  79. /*
  80. using namespace FileChanged;
  81. const String& fileName = eventData[P_FILENAME].GetString();
  82. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  83. if (fullpath_ == fileName)
  84. {
  85. FileSystem* fs = GetSubsystem<FileSystem>();
  86. if (!fs->FileExists(fullpath_))
  87. Close();
  88. }
  89. */
  90. }
  91. void ResourceEditor::Close(bool navigateToAvailableResource)
  92. {
  93. // keep us alive through the close
  94. SharedPtr<ResourceEditor> keepalive(this);
  95. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  96. VariantMap data;
  97. data["Editor"] = this;
  98. data["NavigateToAvailableResource"] = navigateToAvailableResource;
  99. SendEvent("EditorCloseResource", data);
  100. }
  101. void ResourceEditor::InvokeShortcut(const String& shortcut)
  102. {
  103. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  104. ev.ref_id = TBIDC(shortcut.CString());
  105. OnEvent(ev);
  106. }
  107. }