mirror of
https://github.com/aseprite/aseprite.git
synced 2025-03-29 19:20:09 +00:00
Use ContextObserver to update the StatusBar
Instead of calling StatusBar::updateUsingEditor(Editor*), now the StatusBar is a ContextObserver that observe changes in the active doc::Site. The Editor notifies a ActiveSiteChange event when the active frame/layer is changed. Fix #657
This commit is contained in:
parent
c3d7a96a87
commit
a489efdf03
@ -267,19 +267,28 @@ void Editor::setLayer(const Layer* layer)
|
||||
m_layer = const_cast<Layer*>(layer);
|
||||
m_observers.notifyAfterLayerChanged(this);
|
||||
|
||||
// The active layer has changed.
|
||||
if (isActive())
|
||||
UIContext::instance()->notifyActiveSiteChanged();
|
||||
|
||||
updateStatusBar();
|
||||
}
|
||||
|
||||
void Editor::setFrame(frame_t frame)
|
||||
{
|
||||
if (m_frame != frame) {
|
||||
m_observers.notifyBeforeFrameChanged(this);
|
||||
m_frame = frame;
|
||||
m_observers.notifyAfterFrameChanged(this);
|
||||
if (m_frame == frame)
|
||||
return;
|
||||
|
||||
invalidate();
|
||||
updateStatusBar();
|
||||
}
|
||||
m_observers.notifyBeforeFrameChanged(this);
|
||||
m_frame = frame;
|
||||
m_observers.notifyAfterFrameChanged(this);
|
||||
|
||||
// The active frame has changed.
|
||||
if (isActive())
|
||||
UIContext::instance()->notifyActiveSiteChanged();
|
||||
|
||||
invalidate();
|
||||
updateStatusBar();
|
||||
}
|
||||
|
||||
void Editor::getSite(Site* site) const
|
||||
|
@ -197,10 +197,13 @@ StatusBar::StatusBar()
|
||||
tooltipManager->addTooltipFor(m_slider, "Cel Opacity", JI_BOTTOM);
|
||||
|
||||
App::instance()->CurrentToolChange.connect(&StatusBar::onCurrentToolChange, this);
|
||||
UIContext::instance()->addObserver(this);
|
||||
}
|
||||
|
||||
StatusBar::~StatusBar()
|
||||
{
|
||||
UIContext::instance()->removeObserver(this);
|
||||
|
||||
delete m_tipwindow; // widget
|
||||
delete m_commandsBox;
|
||||
}
|
||||
@ -223,9 +226,6 @@ void StatusBar::clearText()
|
||||
|
||||
bool StatusBar::setStatusText(int msecs, const char *format, ...)
|
||||
{
|
||||
// TODO this call should be in an observer of the "current frame" property changes.
|
||||
updateCurrentFrame(current_editor);
|
||||
|
||||
if ((ui::clock() > m_timeout) || (msecs > 0)) {
|
||||
char buf[256]; // TODO warning buffer overflow
|
||||
va_list ap;
|
||||
@ -408,12 +408,6 @@ void StatusBar::onPaint(ui::PaintEvent& ev)
|
||||
}
|
||||
}
|
||||
|
||||
void StatusBar::updateUsingEditor(Editor* editor)
|
||||
{
|
||||
updateFromDocument(editor);
|
||||
updateCurrentFrame(editor);
|
||||
}
|
||||
|
||||
bool StatusBar::CustomizedTipWindow::onProcessMessage(Message* msg)
|
||||
{
|
||||
switch (msg->type()) {
|
||||
@ -452,50 +446,40 @@ void StatusBar::onCelOpacityChange()
|
||||
}
|
||||
}
|
||||
|
||||
void StatusBar::updateFromDocument(Editor* editor)
|
||||
void StatusBar::onActiveSiteChange(const doc::Site& site)
|
||||
{
|
||||
try {
|
||||
if (editor && editor->document()) {
|
||||
const DocumentReader reader(editor->document(), 100);
|
||||
m_hasDoc = true;
|
||||
m_commandsBox->setVisible(true);
|
||||
if (site.document() && site.sprite()) {
|
||||
m_hasDoc = true;
|
||||
m_commandsBox->setVisible(true);
|
||||
|
||||
// Cel opacity
|
||||
const Cel* cel;
|
||||
if (editor->sprite()->supportAlpha() &&
|
||||
editor->layer() &&
|
||||
editor->layer()->isImage() &&
|
||||
!editor->layer()->isBackground() &&
|
||||
(cel = editor->layer()->cel(editor->frame()))) {
|
||||
m_slider->setValue(MID(0, cel->opacity(), 255));
|
||||
m_slider->setEnabled(true);
|
||||
}
|
||||
else {
|
||||
m_slider->setValue(255);
|
||||
m_slider->setEnabled(false);
|
||||
}
|
||||
// Current frame
|
||||
m_currentFrame->setTextf("%d", site.frame()+1);
|
||||
|
||||
// Cel opacity
|
||||
const Cel* cel;
|
||||
if (site.sprite()->supportAlpha() &&
|
||||
site.layer() &&
|
||||
site.layer()->isImage() &&
|
||||
!site.layer()->isBackground() &&
|
||||
(cel = site.layer()->cel(site.frame()))) {
|
||||
m_slider->setValue(MID(0, cel->opacity(), 255));
|
||||
m_slider->setEnabled(true);
|
||||
}
|
||||
else {
|
||||
m_hasDoc = false;
|
||||
m_commandsBox->setVisible(false);
|
||||
m_slider->setValue(255);
|
||||
m_slider->setEnabled(false);
|
||||
}
|
||||
}
|
||||
catch (const LockedDocumentException&) {
|
||||
m_slider->setEnabled(false);
|
||||
else {
|
||||
m_hasDoc = false;
|
||||
m_commandsBox->setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
void StatusBar::updateCurrentFrame(Editor* editor)
|
||||
{
|
||||
if (editor && editor->sprite())
|
||||
m_currentFrame->setTextf("%d", editor->frame()+1);
|
||||
}
|
||||
|
||||
void StatusBar::newFrame()
|
||||
{
|
||||
Command* cmd = CommandsModule::instance()->getCommandByName(CommandId::NewFrame);
|
||||
UIContext::instance()->executeCommand(cmd);
|
||||
updateCurrentFrame(current_editor);
|
||||
}
|
||||
|
||||
} // namespace app
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
#include "app/color.h"
|
||||
#include "base/observers.h"
|
||||
#include "doc/context_observer.h"
|
||||
#include "doc/layer_index.h"
|
||||
#include "ui/base.h"
|
||||
#include "ui/widget.h"
|
||||
@ -36,7 +37,8 @@ namespace app {
|
||||
class Tool;
|
||||
}
|
||||
|
||||
class StatusBar : public ui::Widget {
|
||||
class StatusBar : public ui::Widget
|
||||
, public doc::ContextObserver {
|
||||
static StatusBar* m_instance;
|
||||
public:
|
||||
static StatusBar* instance() { return m_instance; }
|
||||
@ -51,18 +53,17 @@ namespace app {
|
||||
void showColor(int msecs, const char* text, const Color& color, int alpha);
|
||||
void showTool(int msecs, tools::Tool* tool);
|
||||
|
||||
void updateUsingEditor(Editor* editor);
|
||||
|
||||
protected:
|
||||
void onResize(ui::ResizeEvent& ev) override;
|
||||
void onPreferredSize(ui::PreferredSizeEvent& ev) override;
|
||||
void onPaint(ui::PaintEvent& ev) override;
|
||||
|
||||
// ContextObserver impl
|
||||
void onActiveSiteChange(const doc::Site& site) override;
|
||||
|
||||
private:
|
||||
void onCurrentToolChange();
|
||||
void onCelOpacityChange();
|
||||
void updateFromDocument(Editor* editor);
|
||||
void updateCurrentFrame(Editor* editor);
|
||||
void newFrame();
|
||||
|
||||
enum State { SHOW_TEXT, SHOW_COLOR, SHOW_TOOL };
|
||||
|
@ -97,7 +97,6 @@ void UIContext::setActiveView(DocumentView* docView)
|
||||
|
||||
mainWin->getPreviewEditor()->updateUsingEditor(current_editor);
|
||||
mainWin->getTimeline()->updateUsingEditor(current_editor);
|
||||
StatusBar::instance()->updateUsingEditor(current_editor);
|
||||
|
||||
// Change the image-type of color bar.
|
||||
ColorBar::instance()->setPixelFormat(app_get_current_pixel_format());
|
||||
|
Loading…
x
Reference in New Issue
Block a user