ResourceEditor.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  2. // Please see LICENSE.md in repository root for license information
  3. // https://github.com/AtomicGameEngine/AtomicGameEngine
  4. #include <TurboBadger/tb_tab_container.h>
  5. #include "AtomicEditor.h"
  6. #include <Atomic/IO/FileSystem.h>
  7. #include <Atomic/Resource/ResourceEvents.h>
  8. #include "ResourceEditor.h"
  9. //#include "../UI/UIMainFrame.h"
  10. //#include "../UI/UIResourceFrame.h"
  11. namespace AtomicEditor
  12. {
  13. class EditorTabLayout: public TBLayout
  14. {
  15. public:
  16. ResourceEditor* editor_;
  17. TBButton* button_;
  18. TBButton* close_;
  19. TBTabContainer* container_;
  20. void SetValue(int value)
  21. {
  22. button_->SetValue(value);
  23. }
  24. bool OnEvent(const TBWidgetEvent &ev)
  25. {
  26. if (ev.type == EVENT_TYPE_CLICK || ev.type == EVENT_TYPE_POINTER_DOWN)
  27. {
  28. if (ev.target->GetID() == TBIDC("tabclose"))
  29. {
  30. container_->OnEvent(ev);
  31. editor_->Close();
  32. return true;
  33. }
  34. else
  35. {
  36. TBWidgetEvent nevent = ev;
  37. nevent.target = this;
  38. container_->OnEvent(nevent);
  39. }
  40. }
  41. return false;
  42. }
  43. };
  44. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  45. Object(context), fullpath_(fullpath), container_(container),
  46. editorTabLayout_(0), rootContentWidget_(0), button_(0)
  47. {
  48. String filename = GetFileNameAndExtension(fullpath_);
  49. editorTabLayout_ = new EditorTabLayout();
  50. editorTabLayout_->SetID(TBIDC("tab"));
  51. button_ = new UIButton(context_);
  52. button_->SetText(filename.CString());
  53. button_->SetSqueezable(true);
  54. button_->SetSkinBg("TBButton.flat");
  55. button_->SetValue(1);
  56. editorTabLayout_->AddChild(button_->GetInternalWidget());
  57. TBButton* closebutton = new TBButton();
  58. editorTabLayout_->AddChild(closebutton);
  59. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  60. closebutton->SetIsFocusable(false);
  61. closebutton->SetID(TBIDC("tabclose"));
  62. editorTabLayout_->editor_ = this;
  63. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  64. editorTabLayout_->close_ = closebutton;
  65. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  66. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  67. rootContentWidget_ = new UIWidget(context_);
  68. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  69. container_->GetContentRoot()->AddChild(rootContentWidget_);
  70. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  71. }
  72. ResourceEditor::~ResourceEditor()
  73. {
  74. }
  75. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  76. {
  77. using namespace FileChanged;
  78. const String& fileName = eventData[P_FILENAME].GetString();
  79. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  80. if (fullpath_ == fileName)
  81. {
  82. FileSystem* fs = GetSubsystem<FileSystem>();
  83. if (!fs->FileExists(fullpath_))
  84. Close();
  85. }
  86. }
  87. void ResourceEditor::Close(bool navigateToAvailableResource)
  88. {
  89. // keep us alive through the close
  90. SharedPtr<ResourceEditor> keepalive(this);
  91. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  92. VariantMap data;
  93. data["Editor"] = this;
  94. data["NavigateToAvailableResource"] = navigateToAvailableResource;
  95. SendEvent("CloseResourceEditor", data);
  96. //MainFrame* frame = GetSubsystem<MainFrame>();
  97. //ResourceFrame* rframe = frame->GetResourceFrame();
  98. //rframe->CloseResourceEditor(this, navigateToAvailabeResource);
  99. }
  100. }