| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
- // Please see LICENSE.md in repository root for license information
- // https://github.com/AtomicGameEngine/AtomicGameEngine
- #include "AtomicEditor.h"
- #include <Atomic/Container/ArrayPtr.h>
- #include <Atomic/UI/TBUI.h>
- #include <Atomic/IO/Log.h>
- #include <Atomic/IO/File.h>
- #include <Atomic/IO/FileSystem.h>
- #include <Atomic/Resource/ResourceCache.h>
- #include <Atomic/Core/CoreEvents.h>
- #include "../AEEvents.h"
- #include "../UI/UIFindTextWidget.h"
- #include "TextResourceEditor.h"
- #include <TurboBadger/tb_message_window.h>
- #include <TurboBadger/tb_editfield.h>
- #include <TurboBadger/tb_style_edit.h>
- #include <TurboBadger/tb_style_edit_content.h>
- using namespace tb;
- namespace AtomicEditor
- {
- TextResourceEditor ::TextResourceEditor(Context* context, const String &fullpath, TBTabContainer *container) :
- ResourceEditor(context, fullpath, container),
- styleEdit_(0),
- editField_(0),
- modified_(false),
- currentFindPos_(-1)
- {
- TBLayout* layout = new TBLayout();
- layout->SetLayoutDistribution(LAYOUT_DISTRIBUTION_GRAVITY);
- layout->SetSize(container_->GetRect().w, container_->GetRect().h);
- layout->SetGravity(WIDGET_GRAVITY_ALL);
- TBContainer* c = new TBContainer();
- c->SetGravity(WIDGET_GRAVITY_ALL);
- TBEditField* text = editField_ = new TBEditField();
- text->SetMultiline(true);
- text->SetWrapping(true);
- text->SetGravity(WIDGET_GRAVITY_ALL);
- text->SetStyling(true);
- text->SetSkinBg(TBIDC("TextCode"));
- TBFontDescription fd;
- fd.SetID(TBIDC("Monaco"));
- fd.SetSize(12);
- text->SetFontDescription(fd);
- SharedPtr<File> jsFile(GetSubsystem<ResourceCache>()->GetFile(fullpath));
- assert(jsFile);
- String source;
- jsFile->ReadText(source);
- text->SetText(source.CString());
- c->AddChild(text);
- layout->AddChild(c);
- layout->SetSpacing(0);
- container_->GetContentRoot()->AddChild(layout);
- TBStyleEdit* sedit = text->GetStyleEdit();
- sedit->text_change_listener = this;
- TBTextTheme* theme = new TBTextTheme();
- for (unsigned i = 0; i < TB_MAX_TEXT_THEME_COLORS; i++)
- theme->themeColors[i] = TBColor(255, 255, 255);
- sedit->SetTextTheme(theme);
- styleEdit_ = sedit;
- }
- TextResourceEditor::~TextResourceEditor()
- {
- }
- bool TextResourceEditor::OnEvent(const TBWidgetEvent &ev)
- {
- if (ev.type == EVENT_TYPE_KEY_DOWN)
- {
- if (ev.special_key == TB_KEY_ESC)
- {
- SendEvent(E_FINDTEXTCLOSE);
- }
- }
- if (ev.type == EVENT_TYPE_SHORTCUT)
- {
- if (ev.ref_id == TBIDC("close"))
- {
- if (modified_)
- {
- TBMessageWindow *msg_win = new TBMessageWindow(container_, TBIDC("unsaved_jsmodifications_dialog"));
- TBMessageWindowSettings settings(TB_MSG_OK_CANCEL, TBID(uint32(0)));
- settings.dimmer = true;
- settings.styling = true;
- msg_win->Show("Unsaved Modifications", "There are unsaved modications.\nDo you wish to discard them and close?", &settings, 640, 360);
- }
- else
- {
- Close();
- }
- }
- if (ev.ref_id == TBIDC("save") && modified_)
- {
- TBStr text;
- styleEdit_->GetText(text);
- File file(context_, fullpath_, FILE_WRITE);
- file.Write((void*) text.CStr(), text.Length());
- file.Close();
- ResourceCache* cache = GetSubsystem<ResourceCache>();
- //SharedPtr<File> jsFile (GetSubsystem<ResourceCache>()->GetFile<File>(fullpath_));
- //cache->ReloadResource(jsFile);
- String filename = GetFileNameAndExtension(fullpath_);
- button_->SetText(filename.CString());
- modified_ = false;
- SendEvent(E_JAVASCRIPTSAVED);
- return true;
- }
- else if (ev.ref_id == TBIDC("find"))
- {
- using namespace FindTextOpen;
- SendEvent(E_FINDTEXTOPEN);
- }
- else if (ev.ref_id == TBIDC("findnext") || ev.ref_id == TBIDC("findprev"))
- {
- String text;
- FindTextWidget* finder = GetSubsystem<FindTextWidget>();
- finder->GetFindText(text);
- // TODO: get flags from finder
- unsigned flags = FINDTEXT_FLAG_NONE;
- if (ev.ref_id == TBIDC("findnext"))
- flags |= FINDTEXT_FLAG_NEXT;
- else if (ev.ref_id == TBIDC("findprev"))
- flags |= FINDTEXT_FLAG_PREV;
- flags |= FINDTEXT_FLAG_WRAP;
- finder->Find(text, flags);
- }
- }
- if (ev.type == EVENT_TYPE_CLICK)
- {
- if (ev.target->GetID() == TBIDC("unsaved_jsmodifications_dialog"))
- {
- if (ev.ref_id == TBIDC("TBMessageWindow.ok"))
- {
- Close();
- }
- else
- {
- SetFocus();
- }
- return true;
- }
- }
- return false;
- }
- void TextResourceEditor::FindTextClose()
- {
- editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
- styleEdit_->selection.SelectNothing();
- }
- void TextResourceEditor::OnChange(TBStyleEdit* styleEdit)
- {
- modified_ = true;
- String filename = GetFileNameAndExtension(fullpath_);
- filename += "*";
- button_->SetText(filename.CString());
- }
- bool TextResourceEditor::FindText(const String& findText, unsigned flags)
- {
- // TODO: this should be shared with the JS resource editor
- unsigned findLength = findText.Length();
- if (!findLength)
- return true;
- TBStr _source;
- styleEdit_->GetText(_source);
- String source = _source.CStr();
- unsigned pos = String::NPOS;
- int startPos = currentFindPos_;
- if (currentFindPos_ == -1)
- startPos = styleEdit_->caret.GetGlobalOfs();
- else
- {
- if (flags & FINDTEXT_FLAG_NEXT)
- startPos += findLength;
- }
- if (flags & FINDTEXT_FLAG_PREV)
- {
- String pretext = source.Substring(0, startPos);
- pos = pretext.FindLast(findText, String::NPOS, flags & FINDTEXT_FLAG_CASESENSITIVE ? true : false);
- }
- else
- {
- pos = source.Find(findText, startPos, flags & FINDTEXT_FLAG_CASESENSITIVE ? true : false);
- }
- if (pos == String::NPOS)
- {
- if (flags & FINDTEXT_FLAG_WRAP)
- {
- if (flags & FINDTEXT_FLAG_PREV)
- {
- pos = source.FindLast(findText, String::NPOS, flags & FINDTEXT_FLAG_CASESENSITIVE ? true : false);
- }
- else
- {
- pos = source.Find(findText, 0, flags & FINDTEXT_FLAG_CASESENSITIVE ? true : false);
- }
- }
- if (pos == String::NPOS)
- {
- styleEdit_->selection.SelectNothing();
- return true;
- }
- }
- currentFindPos_ = pos;
- styleEdit_->caret.SetGlobalOfs((int) pos + findLength);
- int height = styleEdit_->layout_height;
- int newy = styleEdit_->caret.y - height/2;
- styleEdit_->SetScrollPos(styleEdit_->scroll_x, newy);
- styleEdit_->selection.Select(pos, pos + findLength);
- return true;
- }
- void TextResourceEditor::SetFocus()
- {
- editField_->SetFocus(WIDGET_FOCUS_REASON_UNKNOWN);
- }
- bool TextResourceEditor::HasUnsavedModifications()
- {
- return modified_;
- }
- }
|