ResourceEditor.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 "AtomicEditor.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, TBTabContainer* container):
  44. Object(context), fullpath_(fullpath), container_(container),
  45. layout_(0), button_(0)
  46. {
  47. String filename = GetFileNameAndExtension(fullpath_);
  48. layout_ = new EditorTabLayout();
  49. layout_->SetID(TBIDC("tab"));
  50. button_ = new TBButton();
  51. button_->SetText(filename.CString());
  52. button_->SetSqueezable(true);
  53. button_->SetSkinBg(TBIDC("TBButton.flat"));
  54. button_->SetValue(1);
  55. layout_->AddChild(button_);
  56. TBButton* closebutton = new TBButton();
  57. layout_->AddChild(closebutton);
  58. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  59. closebutton->SetIsFocusable(false);
  60. closebutton->SetID(TBIDC("tabclose"));
  61. layout_->editor_ = this;
  62. layout_->button_ = button_;
  63. layout_->close_ = closebutton;
  64. layout_->container_ = container;
  65. container_->GetTabLayout()->AddChild(layout_);
  66. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  67. }
  68. ResourceEditor::~ResourceEditor()
  69. {
  70. }
  71. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  72. {
  73. using namespace FileChanged;
  74. const String& fileName = eventData[P_FILENAME].GetString();
  75. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  76. if (fullpath_ == fileName)
  77. {
  78. FileSystem* fs = GetSubsystem<FileSystem>();
  79. if (!fs->FileExists(fullpath_))
  80. Close();
  81. }
  82. }
  83. void ResourceEditor::Close(bool navigateToAvailabeResource)
  84. {
  85. // keep us alive through the close
  86. SharedPtr<ResourceEditor> keepalive(this);
  87. container_->GetTabLayout()->RemoveChild(layout_);
  88. MainFrame* frame = GetSubsystem<MainFrame>();
  89. ResourceFrame* rframe = frame->GetResourceFrame();
  90. rframe->CloseResourceEditor(this, navigateToAvailabeResource);
  91. }
  92. }