ResourceEditor.cpp 3.5 KB

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