Replace interactive_move_layer() with MovingCelState.

This commit is contained in:
David Capello 2011-10-29 16:25:47 -03:00
parent daf16f6069
commit 48000d6f48
9 changed files with 177 additions and 120 deletions

View File

@ -311,6 +311,7 @@ add_library(aseprite-library
widgets/editor/editor_listeners.cpp
widgets/editor/editor_view.cpp
widgets/editor/keys.cpp
widgets/editor/moving_cel_state.cpp
widgets/editor/moving_pixels_state.cpp
widgets/editor/pixels_movement.cpp
widgets/editor/scrolling_state.cpp

View File

@ -24,6 +24,7 @@
#include "base/unique_ptr.h"
#include "commands/command.h"
#include "console.h"
#include "document.h"
#include "gui/gui.h"
#include "ini_file.h"
#include "modules/editors.h"
@ -34,7 +35,6 @@
#include "raster/palette.h"
#include "raster/sprite.h"
#include "ui_context.h"
#include "util/misc.h"
#include "widgets/color_bar.h"
#include <allegro/config.h>

View File

@ -22,6 +22,7 @@
#include "base/unique_ptr.h"
#include "commands/command.h"
#include "document.h"
#include "file/file.h"
#include "gui/system.h"
#include "raster/raster.h"

View File

@ -20,6 +20,7 @@
#include "app.h"
#include "console.h"
#include "document.h"
#include "document_wrappers.h"
#include "gui/gui.h"
#include "modules/editors.h"
@ -45,6 +46,7 @@
#include "util/clipboard.h"
#include "util/misc.h"
#include "widgets/color_bar.h"
#include "widgets/editor/editor.h"
#include "widgets/statebar.h"
#include <allegro.h>

View File

@ -32,8 +32,6 @@
#include "modules/gui.h"
#include "modules/palettes.h"
#include "raster/raster.h"
#include "undo/undo_history.h"
#include "undoers/set_cel_position.h"
#include "util/misc.h"
#include "widgets/editor/editor.h"
#include "widgets/statebar.h"
@ -81,103 +79,3 @@ Image* NewImageFromMask(const Document* srcDocument)
return dst;
}
// Gives to the user the possibility to move the sprite's layer in the
// current editor, returns true if the position was changed.
int interactive_move_layer(int mode, bool use_undo, int (*callback)())
{
Editor* editor = current_editor;
Document* document = editor->getDocument();
undo::UndoHistory* undo = document->getUndoHistory();
Sprite* sprite = document->getSprite();
ASSERT(sprite->getCurrentLayer()->is_image());
LayerImage* layer = static_cast<LayerImage*>(sprite->getCurrentLayer());
Cel *cel = layer->getCel(sprite->getCurrentFrame());
int start_x, new_x;
int start_y, new_y;
int start_b;
int ret;
int update = false;
int quiet_clock = -1;
int first_time = true;
int begin_x;
int begin_y;
if (!cel)
return false;
begin_x = cel->getX();
begin_y = cel->getY();
editor->hideDrawingCursor();
jmouse_set_cursor(JI_CURSOR_MOVE);
editor->editor_click_start(mode, &start_x, &start_y, &start_b);
do {
if (update) {
cel->setPosition(begin_x - start_x + new_x,
begin_y - start_y + new_y);
// Update layer-bounds.
editor->invalidate();
// Update status bar.
app_get_statusbar()->setStatusText
(0,
"Pos %3d %3d Offset %3d %3d",
(int)cel->getX(),
(int)cel->getY(),
(int)(cel->getX() - begin_x),
(int)(cel->getY() - begin_y));
/* update clock */
quiet_clock = ji_clock;
first_time = false;
}
/* call the user's routine */
if (callback)
(*callback)();
/* redraw dirty widgets */
jwidget_flush_redraw(ji_get_default_manager());
jmanager_dispatch_messages(ji_get_default_manager());
gui_feedback();
} while (editor->editor_click(&new_x, &new_y, &update, NULL));
new_x = cel->getX();
new_y = cel->getY();
cel->setPosition(begin_x, begin_y);
/* the position was changed */
if (!editor->editor_click_cancel()) {
if (use_undo && undo->isEnabled()) {
undo->setLabel("Cel Movement");
undo->setModification(undo::ModifyDocument);
undo->pushUndoer(new undoers::SetCelPosition(undo->getObjects(), cel));
}
cel->setPosition(new_x, new_y);
ret = true;
}
/* the position wasn't changed */
else {
ret = false;
}
/* redraw the sprite in all editors */
update_screen_for_document(document);
/* restore the cursor */
editor->showDrawingCursor();
editor->editor_click_done();
return ret;
}

View File

@ -19,15 +19,10 @@
#ifndef UTIL_MISC_H_INCLUDED
#define UTIL_MISC_H_INCLUDED
#include "app/color.h"
#include "widgets/editor/editor.h" // For movement modes
class Image;
class Document;
Image* NewImageFromMask(const Document* srcDocument);
int interactive_move_layer(int mode, bool use_undo, int (*callback)());
#endif

View File

@ -0,0 +1,117 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2011 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
#include "widgets/editor/moving_cel_state.h"
#include "app.h"
#include "gui/message.h"
#include "raster/cel.h"
#include "raster/layer.h"
#include "raster/sprite.h"
#include "undo/undo_history.h"
#include "undoers/set_cel_position.h"
#include "widgets/editor/editor.h"
#include "widgets/statebar.h"
MovingCelState::MovingCelState(Editor* editor, Message* msg)
: m_canceled(false)
{
Document* document = editor->getDocument();
Sprite* sprite = editor->getSprite();
ASSERT(sprite->getCurrentLayer()->is_image());
LayerImage* layer = static_cast<LayerImage*>(sprite->getCurrentLayer());
m_cel = layer->getCel(sprite->getCurrentFrame());
m_celStartX = m_cel->getX();
m_celStartY = m_cel->getY();
editor->screenToEditor(msg->mouse.x, msg->mouse.y, &m_mouseStartX, &m_mouseStartY);
editor->captureMouse();
}
MovingCelState::~MovingCelState()
{
}
bool MovingCelState::onMouseUp(Editor* editor, Message* msg)
{
// Here we put back the cel into its original coordinate (so we can
// add an undoer before).
if (m_celStartX != m_cel->getX() ||
m_celStartY != m_cel->getY()) {
// Hold the new cel's position
int newX = m_cel->getX();
int newY = m_cel->getY();
// Put the cel in the original position.
m_cel->setPosition(m_celStartX, m_celStartY);
// If the user didn't cancel the operation...
if (!m_canceled) {
Document* document = editor->getDocument();
undo::UndoHistory* undoHistory = document->getUndoHistory();
// Add an undoer so we can go back to the current position (the original one).
if (undoHistory->isEnabled()) {
undoHistory->setLabel("Cel Movement");
undoHistory->setModification(undo::ModifyDocument);
undoHistory->pushUndoer(new undoers::SetCelPosition(undoHistory->getObjects(), m_cel));
}
// And now we move the cel to the new position.
m_cel->setPosition(newX, newY);
}
}
editor->setState(editor->getDefaultState());
editor->releaseMouse();
return true;
}
bool MovingCelState::onMouseMove(Editor* editor, Message* msg)
{
int newMouseX, newMouseY;
editor->screenToEditor(msg->mouse.x, msg->mouse.y, &newMouseX, &newMouseY);
m_cel->setPosition(m_celStartX - m_mouseStartX + newMouseX,
m_celStartY - m_mouseStartY + newMouseY);
// Redraw the new cel position.
editor->invalidate();
// Use StandbyState implementation
return StandbyState::onMouseMove(editor, msg);
}
bool MovingCelState::onUpdateStatusBar(Editor* editor)
{
app_get_statusbar()->setStatusText
(0,
"Pos %3d %3d Offset %3d %3d",
(int)m_cel->getX(),
(int)m_cel->getY(),
(int)(m_cel->getX() - m_celStartX),
(int)(m_cel->getY() - m_celStartY));
return true;
}

View File

@ -0,0 +1,47 @@
/* ASE - Allegro Sprite Editor
* Copyright (C) 2001-2011 David Capello
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#ifndef WIDGETS_EDITOR_MOVING_CEL_STATE_H_INCLUDED
#define WIDGETS_EDITOR_MOVING_CEL_STATE_H_INCLUDED
#include "base/compiler_specific.h"
#include "widgets/editor/standby_state.h"
class Cel;
class Editor;
class MovingCelState : public StandbyState
{
public:
MovingCelState(Editor* editor, Message* msg);
virtual ~MovingCelState();
virtual bool onMouseUp(Editor* editor, Message* msg) OVERRIDE;
virtual bool onMouseMove(Editor* editor, Message* msg) OVERRIDE;
virtual bool onUpdateStatusBar(Editor* editor) OVERRIDE;
private:
Cel* m_cel;
int m_celStartX;
int m_celStartY;
int m_mouseStartX;
int m_mouseStartY;
bool m_canceled;
};
#endif // WIDGETS_EDITOR_MOVING_CEL_STATE_H_INCLUDED

View File

@ -36,10 +36,10 @@
#include "tools/ink.h"
#include "tools/tool.h"
#include "ui_context.h"
#include "util/misc.h"
#include "widgets/color_bar.h"
#include "widgets/editor/drawing_state.h"
#include "widgets/editor/editor.h"
#include "widgets/editor/moving_cel_state.h"
#include "widgets/editor/moving_pixels_state.h"
#include "widgets/editor/scrolling_state.h"
#include "widgets/editor/tool_loop_impl.h"
@ -75,6 +75,7 @@ bool StandbyState::onMouseDown(Editor* editor, Message* msg)
UIContext* context = UIContext::instance();
tools::Tool* current_tool = editor->getCurrentEditorTool();
tools::Ink* clicked_ink = current_tool->getInk(msg->mouse.right ? 1: 0);
Sprite* sprite = editor->getSprite();
// Each time an editor is clicked the current editor and the active
@ -83,15 +84,14 @@ bool StandbyState::onMouseDown(Editor* editor, Message* msg)
context->setActiveDocument(editor->getDocument());
// Start scroll loop
if (msg->mouse.middle ||
current_tool->getInk(msg->mouse.right ? 1: 0)->isScrollMovement()) {
if (msg->mouse.middle || clicked_ink->isScrollMovement()) {
editor->setState(EditorStatePtr(new ScrollingState()));
editor->captureMouse();
return true;
}
// Move frames position
if (current_tool->getInk(msg->mouse.right ? 1: 0)->isCelMovement()) {
// Move cel X,Y coordinates
if (clicked_ink->isCelMovement()) {
if ((sprite->getCurrentLayer()) &&
(sprite->getCurrentLayer()->getType() == GFXOBJ_LAYER_IMAGE)) {
// TODO you can move the `Background' with tiled mode
@ -104,12 +104,8 @@ bool StandbyState::onMouseDown(Editor* editor, Message* msg)
Alert::show(PACKAGE "<<The layer movement is locked.||&Close");
}
else {
bool click2 = get_config_bool("Options", "MoveClick2", FALSE);
// TODO replace "interactive_move_layer" with a new EditorState
interactive_move_layer(click2 ? Editor::MODE_CLICKANDCLICK:
Editor::MODE_CLICKANDRELEASE,
TRUE, NULL);
// Change to MovingCelState
editor->setState(EditorStatePtr(new MovingCelState(editor, msg)));
}
}
}
@ -130,7 +126,7 @@ bool StandbyState::onMouseDown(Editor* editor, Message* msg)
}
}
// Call the eyedropper command
else if (current_tool->getInk(msg->mouse.right ? 1: 0)->isEyedropper()) {
else if (clicked_ink->isEyedropper()) {
Command* eyedropper_cmd =
CommandsModule::instance()->getCommandByName(CommandId::Eyedropper);