Add ZoomCommand so zoom keys are configured in gui.xml file

This commit is contained in:
David Capello 2014-08-13 00:22:29 -03:00
parent 155b770acd
commit 6db36fe30b
10 changed files with 154 additions and 82 deletions

View File

@ -132,6 +132,20 @@
<param name="change" value="decrement-size" />
</key>
<!-- Zoom -->
<key command="Zoom" shortcut="1"><param name="percentage" value="100" /></key>
<key command="Zoom" shortcut="2"><param name="percentage" value="200" /></key>
<key command="Zoom" shortcut="3"><param name="percentage" value="400" /></key>
<key command="Zoom" shortcut="4"><param name="percentage" value="800" /></key>
<key command="Zoom" shortcut="5"><param name="percentage" value="1600" /></key>
<key command="Zoom" shortcut="6"><param name="percentage" value="3200" /></key>
<key command="Zoom" shortcut="Ctrl+Plus Pad" mac="Cmd+Plus Pad">
<param name="action" value="in" />
</key>
<key command="Zoom" shortcut="Ctrl+Minus Pad" mac="Cmd+Minus Pad">
<param name="action" value="out" />
</key>
<!-- Scroll with arrows -->
<key command="Scroll" shortcut="Ctrl+Left" mac="Cmd+Left">
<param name="direction" value="left" />

View File

@ -84,6 +84,7 @@ add_library(app-lib
commands/cmd_switch_colors.cpp
commands/cmd_timeline.cpp
commands/cmd_undo.cpp
commands/cmd_zoom.cpp
commands/command.cpp
commands/commands.cpp
commands/filters/cmd_color_curve.cpp
@ -169,7 +170,6 @@ add_library(app-lib
ui/editor/editor_observers.cpp
ui/editor/editor_states_history.cpp
ui/editor/editor_view.cpp
ui/editor/keys.cpp
ui/editor/moving_cel_state.cpp
ui/editor/moving_pixels_state.cpp
ui/editor/pixels_movement.cpp

View File

@ -0,0 +1,106 @@
/* Aseprite
* Copyright (C) 2001-2014 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "app/commands/command.h"
#include "app/commands/params.h"
#include "app/modules/editors.h"
#include "app/ui/editor/editor.h"
namespace app {
class ZoomCommand : public Command {
public:
enum Action { In, Out, Set };
ZoomCommand();
Command* clone() const OVERRIDE { return new ZoomCommand(*this); }
protected:
void onLoadParams(Params* params);
bool onEnabled(Context* context);
void onExecute(Context* context);
private:
Action m_action;
int m_percentage;
};
ZoomCommand::ZoomCommand()
: Command("Zoom",
"Zoom",
CmdUIOnlyFlag)
{
}
void ZoomCommand::onLoadParams(Params* params)
{
std::string action = params->get("action");
if (action == "in") m_action = In;
else if (action == "out") m_action = Out;
else if (action == "set") m_action = Set;
std::string percentage = params->get("percentage");
if (!percentage.empty()) {
m_percentage = std::strtol(percentage.c_str(), NULL, 10);
m_action = Set;
}
}
bool ZoomCommand::onEnabled(Context* context)
{
return current_editor != NULL;
}
void ZoomCommand::onExecute(Context* context)
{
int zoom = current_editor->zoom();
switch (m_action) {
case In:
if (zoom < 5)
++zoom;
break;
case Out:
if (zoom > 0)
--zoom;
break;
case Set:
switch (m_percentage) {
case 3200: zoom = 5; break;
case 1600: zoom = 4; break;
case 800: zoom = 3; break;
case 400: zoom = 2; break;
case 200: zoom = 1; break;
default: zoom = 0; break;
}
break;
}
current_editor->setEditorZoom(zoom);
}
Command* CommandFactory::createZoomCommand()
{
return new ZoomCommand;
}
} // namespace app

View File

@ -109,3 +109,4 @@ FOR_EACH_COMMAND(SpriteSize)
FOR_EACH_COMMAND(SwitchColors)
FOR_EACH_COMMAND(Timeline)
FOR_EACH_COMMAND(Undo)
FOR_EACH_COMMAND(Zoom)

View File

@ -22,11 +22,16 @@
#include "app/ui/editor/drawing_state.h"
#include "app/commands/command.h"
#include "app/commands/commands.h"
#include "app/commands/params.h"
#include "app/modules/gui.h"
#include "app/tools/ink.h"
#include "app/tools/tool.h"
#include "app/tools/tool_loop.h"
#include "app/tools/tool_loop_manager.h"
#include "app/ui/editor/editor.h"
#include "app/ui_context.h"
#include "ui/message.h"
#include "ui/system.h"
@ -160,8 +165,14 @@ bool DrawingState::onSetCursor(Editor* editor)
bool DrawingState::onKeyDown(Editor* editor, KeyMessage* msg)
{
if (editor->processKeysToSetZoom(msg))
return true;
Command* command = NULL;
Params* params = NULL;
if (get_command_from_key_message(msg, &command, &params)) {
// We accept zoom commands.
if (strcmp(command->short_name(), CommandId::Zoom) == 0) {
UIContext::instance()->executeCommand(command, params);
}
}
// When we are drawing, we "eat" all pressed keys.
return true;

View File

@ -322,6 +322,13 @@ void Editor::setEditorScroll(int x, int y, int use_refresh_region)
drawBrushPreview(m_cursor_screen_x, m_cursor_screen_y);
}
void Editor::setEditorZoom(int zoom)
{
setZoomAndCenterInMouse(zoom,
jmouse_x(0), jmouse_y(0),
Editor::kCofiguredZoomBehavior);
}
void Editor::updateEditor()
{
View::getView(this)->updateView();

View File

@ -129,6 +129,7 @@ namespace app {
void setDefaultScroll();
void setEditorScroll(int x, int y, int use_refresh_region);
void setEditorZoom(int zoom);
// Updates the Editor's view.
void updateEditor();
@ -178,8 +179,6 @@ namespace app {
void setZoomAndCenterInMouse(int zoom, int mouse_x, int mouse_y, ZoomBehavior zoomBehavior);
bool processKeysToSetZoom(ui::KeyMessage* msg);
void pasteImage(const Image* image, int x, int y);
void startSelectionTransformation(const gfx::Point& move);

View File

@ -1,74 +0,0 @@
/* Aseprite
* Copyright (C) 2001-2013 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
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <allegro/keyboard.h>
#include "app/app.h"
#include "app/color.h"
#include "app/ui/color_bar.h"
#include "app/ui/editor/editor.h"
#include "app/modules/gui.h"
#include "raster/image.h"
#include "raster/sprite.h"
#include "app/settings/settings.h"
#include "ui/message.h"
#include "ui/system.h"
#include "ui/view.h"
#include "ui/widget.h"
#include "app/ui_context.h"
namespace app {
using namespace ui;
bool Editor::processKeysToSetZoom(KeyMessage* msg)
{
if ((m_sprite) &&
(hasMouse()) &&
(msg->keyModifiers() == kKeyNoneModifier)) {
View* view = View::getView(this);
gfx::Rect vp = view->getViewportBounds();
int x = 0;
int y = 0;
int zoom = -1;
switch (msg->scancode()) { // TODO make these keys configurable
case kKey1: zoom = 0; break;
case kKey2: zoom = 1; break;
case kKey3: zoom = 2; break;
case kKey4: zoom = 3; break;
case kKey5: zoom = 4; break;
case kKey6: zoom = 5; break;
}
// Change zoom
if (zoom >= 0) {
setZoomAndCenterInMouse(zoom, jmouse_x(0), jmouse_y(0),
Editor::kCofiguredZoomBehavior);
return true;
}
}
return false;
}
} // namespace app

View File

@ -326,10 +326,15 @@ bool MovingPixelsState::onKeyDown(Editor* editor, KeyMessage* msg)
Command* command = NULL;
Params* params = NULL;
if (get_command_from_key_message(msg, &command, &params)) {
// We accept zoom commands.
if (strcmp(command->short_name(), CommandId::Zoom) == 0) {
UIContext::instance()->executeCommand(command, params);
return true;
}
// Intercept the "Cut" or "Copy" command to handle them locally
// with the current m_pixelsMovement data.
if (strcmp(command->short_name(), CommandId::Cut) == 0 ||
strcmp(command->short_name(), CommandId::Copy) == 0) {
else if (strcmp(command->short_name(), CommandId::Cut) == 0 ||
strcmp(command->short_name(), CommandId::Copy) == 0) {
// Copy the floating image to the clipboard.
{
Document* document = editor->document();
@ -405,6 +410,9 @@ void MovingPixelsState::onBeforeCommandExecution(Command* command)
if (moveMaskCmd->getTarget() == MoveMaskCommand::Content)
return;
}
else if (strcmp(command->short_name(), CommandId::Zoom) == 0) {
return;
}
if (m_pixelsMovement)
dropPixels(m_currentEditor);

View File

@ -466,7 +466,7 @@ bool StandbyState::onSetCursor(Editor* editor)
bool StandbyState::onKeyDown(Editor* editor, KeyMessage* msg)
{
return editor->processKeysToSetZoom(msg);
return false;
}
bool StandbyState::onKeyUp(Editor* editor, KeyMessage* msg)