ResourceEditor.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // Copyright (c) 2014-2016 THUNDERBEAST GAMES LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <TurboBadger/tb_tab_container.h>
  23. #include <Atomic/Core/StringUtils.h>
  24. #include <Atomic/IO/FileSystem.h>
  25. #include <Atomic/Resource/ResourceEvents.h>
  26. #include <TurboBadger/tb_message_window.h>
  27. #include "ResourceEditorEvents.h"
  28. #include "ResourceEditor.h"
  29. //#include "../UI/UIMainFrame.h"
  30. //#include "../UI/UIResourceFrame.h"
  31. namespace AtomicEditor
  32. {
  33. class EditorTabLayout: public TBLayout
  34. {
  35. public:
  36. ResourceEditor* editor_;
  37. TBButton* button_;
  38. TBButton* close_;
  39. TBTabContainer* container_;
  40. void SetValue(int value)
  41. {
  42. button_->SetValue(value);
  43. }
  44. bool RequestClose()
  45. {
  46. if (editor_->HasUnsavedModifications())
  47. {
  48. TBMessageWindow *msg_win = new TBMessageWindow(this, TBIDC("unsaved_modifications_dialog"));
  49. TBMessageWindowSettings settings(TB_MSG_NONE, TBID(uint32(0)));
  50. settings.dimmer = true;
  51. settings.styling = true;
  52. String windowString = Atomic::ToString("%s has unsaved modifications.\nDo you wish to discard them and close?", GetFileNameAndExtension(editor_->GetFullPath()).CString());
  53. msg_win->Show("Unsaved Modifications", windowString.CString(), &settings, 640, 360);
  54. msg_win->AddButtonLeft("dont_save", false);
  55. msg_win->AddButton("TBMessageWindow.cancel", false);
  56. msg_win->AddButton("save", true);
  57. return false;
  58. }
  59. else
  60. {
  61. editor_->Close(container_->GetNumPages()>1);
  62. return true;
  63. }
  64. }
  65. bool OnEvent(const TBWidgetEvent &ev)
  66. {
  67. if (ev.type == EVENT_TYPE_CLICK)
  68. {
  69. if (ev.target->GetID() == TBIDC("unsaved_modifications_dialog"))
  70. {
  71. if (ev.ref_id == TBIDC("dont_save"))
  72. {
  73. container_->OnEvent(ev);
  74. editor_->Close(container_->GetNumPages()>1);
  75. }
  76. else if (ev.ref_id == TBIDC("TBMessageWindow.cancel"))
  77. {
  78. editor_->SendEvent(E_EDITORRESOURCECLOSECANCELED);
  79. SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
  80. }
  81. else if (ev.ref_id == TBIDC("save"))
  82. {
  83. editor_->Save();
  84. container_->OnEvent(ev);
  85. editor_->Close(container_->GetNumPages()>1);
  86. }
  87. return true;
  88. }
  89. else if (ev.target->GetID() == TBIDC("tabclose"))
  90. {
  91. if (RequestClose())
  92. {
  93. container_->OnEvent(ev);
  94. }
  95. return true;
  96. }
  97. else
  98. {
  99. TBWidgetEvent nevent = ev;
  100. nevent.target = this;
  101. return container_->OnEvent(nevent);
  102. }
  103. }
  104. return false;
  105. }
  106. };
  107. ResourceEditor::ResourceEditor(Context* context, const String& fullpath, UITabContainer *container):
  108. Object(context), fullpath_(fullpath), container_(container),
  109. editorTabLayout_(0), rootContentWidget_(0), button_(0), modified_(false)
  110. {
  111. String filename = GetFileNameAndExtension(fullpath_);
  112. editorTabLayout_ = new EditorTabLayout();
  113. editorTabLayout_->SetID(TBIDC("tab"));
  114. button_ = new UIButton(context_);
  115. button_->SetText(filename.CString());
  116. button_->SetSqueezable(true);
  117. button_->SetSkinBg("TBButton.flat");
  118. button_->SetValue(1);
  119. editorTabLayout_->AddChild(button_->GetInternalWidget());
  120. TBButton* closebutton = new TBButton();
  121. editorTabLayout_->AddChild(closebutton);
  122. closebutton->SetSkinBg(TBIDC("TBWindow.close"));
  123. closebutton->SetIsFocusable(false);
  124. closebutton->SetID(TBIDC("tabclose"));
  125. editorTabLayout_->editor_ = this;
  126. editorTabLayout_->button_ = (TBButton*) button_->GetInternalWidget();
  127. editorTabLayout_->close_ = closebutton;
  128. editorTabLayout_->container_ = (TBTabContainer*) container->GetInternalWidget();
  129. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->AddChild(editorTabLayout_);
  130. rootContentWidget_ = new UIWidget(context_);
  131. rootContentWidget_->SetGravity(UI_GRAVITY_ALL);
  132. container_->GetContentRoot()->AddChild(rootContentWidget_);
  133. SubscribeToEvent(E_FILECHANGED, HANDLER(ResourceEditor, HandleFileChanged));
  134. SubscribeToEvent(E_RENAMERESOURCENOTIFICATION, HANDLER(ResourceEditor, HandleRenameResourceNotification));
  135. }
  136. ResourceEditor::~ResourceEditor()
  137. {
  138. }
  139. void ResourceEditor::HandleFileChanged(StringHash eventType, VariantMap& eventData)
  140. {
  141. /*
  142. using namespace FileChanged;
  143. const String& fileName = eventData[P_FILENAME].GetString();
  144. const String& resourceName = eventData[P_RESOURCENAME].GetString();
  145. if (fullpath_ == fileName)
  146. {
  147. FileSystem* fs = GetSubsystem<FileSystem>();
  148. if (!fs->FileExists(fullpath_))
  149. Close();
  150. }
  151. */
  152. }
  153. void ResourceEditor::HandleRenameResourceNotification(StringHash eventType, VariantMap& eventData)
  154. {
  155. using namespace RenameResourceNotification;
  156. const String& newPath = eventData[P_NEWRESOURCEPATH].GetString();
  157. const String& path = eventData[P_RESOURCEPATH].GetString();
  158. if (fullpath_.Compare(path) == 0) {
  159. fullpath_ = newPath;
  160. SetModified(modified_);
  161. }
  162. }
  163. void ResourceEditor::RequestClose()
  164. {
  165. editorTabLayout_->RequestClose();
  166. }
  167. void ResourceEditor::Close(bool navigateToAvailableResource)
  168. {
  169. // keep us alive through the close
  170. SharedPtr<ResourceEditor> keepalive(this);
  171. ((TBTabContainer*)container_->GetInternalWidget())->GetTabLayout()->RemoveChild(editorTabLayout_);
  172. VariantMap data;
  173. data[EditorResourceClose::P_EDITOR] = this;
  174. data[EditorResourceClose::P_NAVIGATE] = navigateToAvailableResource;
  175. SendEvent(E_EDITORRESOURCECLOSE, data);
  176. }
  177. void ResourceEditor::InvokeShortcut(const String& shortcut)
  178. {
  179. TBWidgetEvent ev(EVENT_TYPE_SHORTCUT);
  180. ev.ref_id = TBIDC(shortcut.CString());
  181. OnEvent(ev);
  182. }
  183. void ResourceEditor::SetModified(bool modified)
  184. {
  185. modified_ = modified;
  186. if (modified)
  187. {
  188. String filename = GetFileNameAndExtension(fullpath_);
  189. filename += "*";
  190. button_->SetText(filename.CString());
  191. }
  192. else
  193. {
  194. String filename = GetFileNameAndExtension(fullpath_);
  195. button_->SetText(filename.CString());
  196. }
  197. }
  198. void ResourceEditor::Delete() {}
  199. }