2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
|
|
|
// Copyright (C) 2001-2015 David Capello
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License version 2 as
|
|
|
|
// published by the Free Software Foundation.
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2009-06-01 10:59:15 +08:00
|
|
|
#include "config.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/app.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/document.h"
|
2014-11-07 08:04:32 +08:00
|
|
|
#include "app/document_location.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/modules/editors.h"
|
2014-12-28 22:06:11 +08:00
|
|
|
#include "app/pref/preferences.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/settings/ui_settings_impl.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/color_bar.h"
|
|
|
|
#include "app/ui/document_view.h"
|
|
|
|
#include "app/ui/editor/editor.h"
|
|
|
|
#include "app/ui/main_window.h"
|
2015-02-10 20:38:07 +08:00
|
|
|
#include "app/ui/preview_editor.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/tabs.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/ui/timeline.h"
|
2013-08-06 08:20:19 +08:00
|
|
|
#include "app/ui/workspace.h"
|
2013-11-11 05:27:11 +08:00
|
|
|
#include "app/ui_context.h"
|
2011-03-23 08:11:25 +08:00
|
|
|
#include "base/mutex.h"
|
2014-10-21 09:21:31 +08:00
|
|
|
#include "doc/sprite.h"
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
namespace app {
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
UIContext* UIContext::m_instance = NULL;
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2009-10-09 09:34:06 +08:00
|
|
|
UIContext::UIContext()
|
- All tools stuff refactored in various files/components.
- Added classes: IToolLoop, Tool, ToolGroup, ToolInk, ToolController, ToolPointShape, ToolIntertwine, ToolBox, etc.
- Added ToolLoopManager.
- Removed old src/modules/tools.cpp.
- Added ISettings and UISettingsImpl, adding the tools settings (onion skinning, grid, tiled mode, etc.).
- Added App::PenSizeBeforeChange, PenSizeAfterChange, CurrentToolChange signals.
- Renamed Context::get_bg/fg_color to getBg/FgColor.
- Refactored Brush class to Pen and added PenType.
- Renamed tiled_t to TiledMode.
- get_config_rect now uses the new Rect class imported from Vaca instead of old jrect.
- Added default_skin.xml to load tool icons.
- Added pen preview in Editor::cursor stuff.
- Added Editor::decorators.
Note: This big patch is from some time ago. I did my best to pre-commit other small changes before this big one.
2010-03-08 03:47:45 +08:00
|
|
|
: Context(new UISettingsImpl)
|
2014-08-25 19:27:42 +08:00
|
|
|
, m_lastSelectedView(NULL)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2014-12-28 22:06:11 +08:00
|
|
|
documents().addObserver(&App::instance()->preferences());
|
2014-09-09 10:08:06 +08:00
|
|
|
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(m_instance == NULL);
|
2009-10-09 09:34:06 +08:00
|
|
|
m_instance = this;
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2009-10-09 09:34:06 +08:00
|
|
|
UIContext::~UIContext()
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2010-08-04 10:33:44 +08:00
|
|
|
ASSERT(m_instance == this);
|
2009-10-09 09:34:06 +08:00
|
|
|
m_instance = NULL;
|
2014-07-29 11:53:24 +08:00
|
|
|
|
2014-12-28 22:06:11 +08:00
|
|
|
documents().removeObserver(&App::instance()->preferences());
|
2014-09-09 10:08:06 +08:00
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
// The context must be empty at this point. (It's to check if the UI
|
|
|
|
// is working correctly, i.e. closing all files when the user can
|
|
|
|
// take any action about it.)
|
|
|
|
ASSERT(documents().empty());
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2014-11-07 05:40:29 +08:00
|
|
|
bool UIContext::isUiAvailable() const
|
|
|
|
{
|
|
|
|
return App::instance()->isGui();
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
DocumentView* UIContext::activeView() const
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2014-11-07 05:40:29 +08:00
|
|
|
if (!isUiAvailable())
|
|
|
|
return NULL;
|
|
|
|
|
2013-03-28 08:19:35 +08:00
|
|
|
Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
|
2014-07-29 11:53:24 +08:00
|
|
|
WorkspaceView* view = workspace->activeView();
|
2013-03-28 08:19:35 +08:00
|
|
|
if (DocumentView* docView = dynamic_cast<DocumentView*>(view))
|
|
|
|
return docView;
|
|
|
|
else
|
|
|
|
return NULL;
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:20:19 +08:00
|
|
|
void UIContext::setActiveView(DocumentView* docView)
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2014-08-25 19:27:42 +08:00
|
|
|
if (m_lastSelectedView == docView) // Do nothing case
|
|
|
|
return;
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
setActiveDocument(docView ? docView->getDocument(): NULL);
|
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
MainWindow* mainWin = App::instance()->getMainWindow();
|
|
|
|
if (docView) {
|
|
|
|
mainWin->getTabsBar()->selectTab(docView);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
if (mainWin->getWorkspace()->activeView() != docView)
|
|
|
|
mainWin->getWorkspace()->setActiveView(docView);
|
2013-03-28 08:19:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
current_editor = (docView ? docView->getEditor(): NULL);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
if (current_editor)
|
|
|
|
current_editor->requestFocus();
|
|
|
|
|
2015-02-10 20:38:07 +08:00
|
|
|
mainWin->getPreviewEditor()->updateUsingEditor(current_editor);
|
|
|
|
mainWin->getTimeline()->updateUsingEditor(current_editor);
|
2013-02-21 06:54:00 +08:00
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
// Change the image-type of color bar.
|
|
|
|
ColorBar::instance()->setPixelFormat(app_get_current_pixel_format());
|
|
|
|
|
2013-02-21 06:54:00 +08:00
|
|
|
// Restore the palette of the selected document.
|
2013-03-12 07:29:45 +08:00
|
|
|
app_refresh_screen();
|
|
|
|
|
|
|
|
// Change the main frame title.
|
2014-09-03 11:39:15 +08:00
|
|
|
App::instance()->updateDisplayTitleBar();
|
2014-08-25 19:27:42 +08:00
|
|
|
|
|
|
|
m_lastSelectedView = docView;
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t UIContext::countViewsOf(Document* document) const
|
|
|
|
{
|
2013-03-28 08:19:35 +08:00
|
|
|
Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
|
2013-01-21 05:40:37 +08:00
|
|
|
size_t counter = 0;
|
2013-03-28 08:19:35 +08:00
|
|
|
|
2014-12-01 08:06:29 +08:00
|
|
|
for (auto view : *workspace) {
|
2013-03-28 08:19:35 +08:00
|
|
|
if (DocumentView* docView = dynamic_cast<DocumentView*>(view)) {
|
|
|
|
if (docView->getDocument() == document) {
|
|
|
|
++counter;
|
|
|
|
}
|
|
|
|
}
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
2013-03-28 08:19:35 +08:00
|
|
|
|
2013-01-21 05:40:37 +08:00
|
|
|
return counter;
|
|
|
|
}
|
|
|
|
|
2014-12-01 08:06:29 +08:00
|
|
|
DocumentView* UIContext::getFirstDocumentView(Document* document) const
|
|
|
|
{
|
|
|
|
Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
|
|
|
|
|
|
|
|
for (auto view : *workspace) {
|
|
|
|
if (DocumentView* docView = dynamic_cast<DocumentView*>(view)) {
|
|
|
|
if (docView->getDocument() == document) {
|
|
|
|
return docView;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
Editor* UIContext::activeEditor()
|
2013-01-21 05:40:37 +08:00
|
|
|
{
|
2014-07-29 11:53:24 +08:00
|
|
|
DocumentView* view = activeView();
|
|
|
|
if (view)
|
|
|
|
return view->getEditor();
|
2013-01-21 05:40:37 +08:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
void UIContext::onAddDocument(doc::Document* doc)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2014-07-29 11:53:24 +08:00
|
|
|
Context::onAddDocument(doc);
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-12-09 07:19:32 +08:00
|
|
|
// We don't create views in batch mode.
|
|
|
|
if (!App::instance()->isGui())
|
|
|
|
return;
|
|
|
|
|
2013-01-21 05:40:37 +08:00
|
|
|
// Add a new view for this document
|
2014-07-29 11:53:24 +08:00
|
|
|
DocumentView* view = new DocumentView(static_cast<app::Document*>(doc), DocumentView::Normal);
|
2013-01-21 05:40:37 +08:00
|
|
|
|
|
|
|
// Add a tab with the new view for the document
|
|
|
|
App::instance()->getMainWindow()->getWorkspace()->addView(view);
|
|
|
|
|
|
|
|
setActiveView(view);
|
|
|
|
view->getEditor()->setDefaultScroll();
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2014-07-29 11:53:24 +08:00
|
|
|
void UIContext::onRemoveDocument(doc::Document* doc)
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2014-07-29 11:53:24 +08:00
|
|
|
Context::onRemoveDocument(doc);
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2014-11-07 08:04:32 +08:00
|
|
|
// We don't destroy views in batch mode.
|
|
|
|
if (!isUiAvailable())
|
|
|
|
return;
|
|
|
|
|
2013-03-28 08:19:35 +08:00
|
|
|
Workspace* workspace = App::instance()->getMainWindow()->getWorkspace();
|
|
|
|
DocumentViews docViews;
|
2013-01-21 05:40:37 +08:00
|
|
|
|
2013-03-28 08:19:35 +08:00
|
|
|
// Collect all views related to the document.
|
|
|
|
for (Workspace::iterator it=workspace->begin(); it != workspace->end(); ++it) {
|
|
|
|
WorkspaceView* view = *it;
|
|
|
|
if (DocumentView* docView = dynamic_cast<DocumentView*>(view)) {
|
2014-07-29 11:53:24 +08:00
|
|
|
if (docView->getDocument() == doc) {
|
2013-03-28 08:19:35 +08:00
|
|
|
docViews.push_back(docView);
|
|
|
|
}
|
2013-01-21 05:40:37 +08:00
|
|
|
}
|
|
|
|
}
|
2009-06-01 10:59:15 +08:00
|
|
|
|
2013-03-28 08:19:35 +08:00
|
|
|
for (DocumentViews::iterator it=docViews.begin(); it != docViews.end(); ++it) {
|
|
|
|
DocumentView* docView = *it;
|
|
|
|
workspace->removeView(docView);
|
|
|
|
delete docView;
|
|
|
|
}
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
|
|
|
|
2013-03-12 07:29:45 +08:00
|
|
|
void UIContext::onGetActiveLocation(DocumentLocation* location) const
|
2009-06-01 10:59:15 +08:00
|
|
|
{
|
2014-07-29 11:53:24 +08:00
|
|
|
DocumentView* view = activeView();
|
2014-11-07 08:04:32 +08:00
|
|
|
if (view) {
|
2014-07-29 11:53:24 +08:00
|
|
|
view->getDocumentLocation(location);
|
2014-11-07 08:04:32 +08:00
|
|
|
}
|
|
|
|
// Default/dummy location (maybe for batch/command line mode)
|
|
|
|
else if (Document* doc = activeDocument()) {
|
|
|
|
location->document(doc);
|
|
|
|
location->sprite(doc->sprite());
|
|
|
|
location->layer(doc->sprite()->indexToLayer(LayerIndex(0)));
|
2014-12-29 07:39:11 +08:00
|
|
|
location->frame(0);
|
2014-11-07 08:04:32 +08:00
|
|
|
}
|
2009-06-01 10:59:15 +08:00
|
|
|
}
|
2013-08-06 08:20:19 +08:00
|
|
|
|
|
|
|
} // namespace app
|