2015-02-12 23:16:25 +08:00
|
|
|
// Aseprite
|
2019-12-19 05:00:11 +08:00
|
|
|
// Copyright (C) 2018-2019 Igara Studio S.A.
|
2018-03-07 06:21:09 +08:00
|
|
|
// Copyright (C) 2001-2018 David Capello
|
2015-02-12 23:16:25 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2014-10-29 22:58:03 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "app/ui/keyboard_shortcuts.h"
|
|
|
|
|
|
|
|
#include "app/app.h"
|
|
|
|
#include "app/app_menus.h"
|
|
|
|
#include "app/commands/command.h"
|
|
|
|
#include "app/commands/commands.h"
|
2018-07-24 02:15:04 +08:00
|
|
|
#include "app/commands/commands.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "app/commands/params.h"
|
2018-07-07 22:54:44 +08:00
|
|
|
#include "app/doc.h"
|
2019-12-19 05:00:11 +08:00
|
|
|
#include "app/tools/active_tool.h"
|
2014-10-29 22:58:03 +08:00
|
|
|
#include "app/tools/ink.h"
|
|
|
|
#include "app/tools/tool.h"
|
|
|
|
#include "app/tools/tool_box.h"
|
|
|
|
#include "app/ui_context.h"
|
|
|
|
#include "app/xml_document.h"
|
|
|
|
#include "app/xml_exception.h"
|
|
|
|
#include "ui/accelerator.h"
|
|
|
|
#include "ui/message.h"
|
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
|
|
|
#include <vector>
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
#define XML_KEYBOARD_FILE_VERSION "1"
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char* name;
|
|
|
|
const char* userfriendly;
|
|
|
|
app::KeyAction action;
|
|
|
|
} actions[] = {
|
|
|
|
{ "CopySelection" , "Copy Selection" , app::KeyAction::CopySelection },
|
|
|
|
{ "SnapToGrid" , "Snap To Grid" , app::KeyAction::SnapToGrid },
|
|
|
|
{ "AngleSnap" , "Angle Snap" , app::KeyAction::AngleSnap },
|
|
|
|
{ "MaintainAspectRatio" , "Maintain Aspect Ratio", app::KeyAction::MaintainAspectRatio },
|
2018-03-07 06:21:09 +08:00
|
|
|
{ "ScaleFromCenter" , "Scale From Center" , app::KeyAction::ScaleFromCenter },
|
2014-10-29 22:58:03 +08:00
|
|
|
{ "LockAxis" , "Lock Axis" , app::KeyAction::LockAxis },
|
|
|
|
{ "AddSelection" , "Add Selection" , app::KeyAction::AddSelection },
|
|
|
|
{ "SubtractSelection" , "Subtract Selection" , app::KeyAction::SubtractSelection },
|
2018-10-27 01:04:08 +08:00
|
|
|
{ "IntersectSelection" , "Intersect Selection" , app::KeyAction::IntersectSelection },
|
2016-04-05 05:46:48 +08:00
|
|
|
{ "AutoSelectLayer" , "Auto Select Layer" , app::KeyAction::AutoSelectLayer },
|
2015-09-16 23:19:10 +08:00
|
|
|
{ "StraightLineFromLastPoint", "Straight Line from Last Point", app::KeyAction::StraightLineFromLastPoint },
|
2017-06-23 04:47:56 +08:00
|
|
|
{ "AngleSnapFromLastPoint", "Angle Snap from Last Point", app::KeyAction::AngleSnapFromLastPoint },
|
2018-03-07 06:21:09 +08:00
|
|
|
{ "MoveOrigin" , "Move Origin" , app::KeyAction::MoveOrigin },
|
2016-04-05 05:46:48 +08:00
|
|
|
{ "SquareAspect" , "Square Aspect" , app::KeyAction::SquareAspect },
|
|
|
|
{ "DrawFromCenter" , "Draw From Center" , app::KeyAction::DrawFromCenter },
|
2018-03-07 06:21:09 +08:00
|
|
|
{ "RotateShape" , "Rotate Shape" , app::KeyAction::RotateShape },
|
2014-11-09 08:09:29 +08:00
|
|
|
{ "LeftMouseButton" , "Trigger Left Mouse Button" , app::KeyAction::LeftMouseButton },
|
|
|
|
{ "RightMouseButton" , "Trigger Right Mouse Button" , app::KeyAction::RightMouseButton },
|
2014-10-29 22:58:03 +08:00
|
|
|
{ NULL , NULL , app::KeyAction::None }
|
|
|
|
};
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
static struct {
|
|
|
|
const char* name;
|
|
|
|
const char* userfriendly;
|
|
|
|
app::WheelAction action;
|
|
|
|
} wheel_actions[] = {
|
|
|
|
{ "Zoom" , "Zoom" , app::WheelAction::Zoom },
|
|
|
|
{ "VScroll" , "Scroll: Vertically" , app::WheelAction::VScroll },
|
|
|
|
{ "HScroll" , "Scroll: Horizontally" , app::WheelAction::HScroll },
|
|
|
|
{ "FgColor" , "Color: Foreground Palette Entry" , app::WheelAction::FgColor },
|
|
|
|
{ "BgColor" , "Color: Background Palette Entry" , app::WheelAction::BgColor },
|
|
|
|
{ "Frame" , "Change Frame" , app::WheelAction::Frame },
|
|
|
|
{ "BrushSize" , "Change Brush Size" , app::WheelAction::BrushSize },
|
|
|
|
{ "BrushAngle" , "Change Brush Angle" , app::WheelAction::BrushAngle },
|
|
|
|
{ "ToolSameGroup" , "Change Tool (same group)" , app::WheelAction::ToolSameGroup },
|
|
|
|
{ "ToolOtherGroup" , "Change Tool" , app::WheelAction::ToolOtherGroup },
|
|
|
|
{ "Layer" , "Change Layer" , app::WheelAction::Layer },
|
|
|
|
{ "InkOpacity" , "Change Ink Opacity" , app::WheelAction::InkOpacity },
|
|
|
|
{ "LayerOpacity" , "Change Layer Opacity" , app::WheelAction::LayerOpacity },
|
|
|
|
{ "CelOpacity" , "Change Cel Opacity" , app::WheelAction::CelOpacity },
|
|
|
|
{ "Alpha" , "Color: Alpha" , app::WheelAction::Alpha },
|
|
|
|
{ "HslHue" , "Color: HSL Hue" , app::WheelAction::HslHue },
|
|
|
|
{ "HslSaturation", "Color: HSL Saturation" , app::WheelAction::HslSaturation },
|
|
|
|
{ "HslLightness" , "Color: HSL Lightness" , app::WheelAction::HslLightness },
|
|
|
|
{ "HsvHue" , "Color: HSV Hue" , app::WheelAction::HsvHue },
|
|
|
|
{ "HsvSaturation", "Color: HSV Saturation" , app::WheelAction::HsvSaturation },
|
|
|
|
{ "HsvValue" , "Color: HSV Value" , app::WheelAction::HsvValue },
|
|
|
|
{ nullptr , nullptr , app::WheelAction::None }
|
|
|
|
};
|
|
|
|
|
|
|
|
const char* get_shortcut(TiXmlElement* elem) {
|
2014-10-29 22:58:03 +08:00
|
|
|
const char* shortcut = NULL;
|
|
|
|
|
2015-02-12 23:46:56 +08:00
|
|
|
#ifdef _WIN32
|
2014-10-29 22:58:03 +08:00
|
|
|
if (!shortcut) shortcut = elem->Attribute("win");
|
2015-02-12 23:46:56 +08:00
|
|
|
#elif defined __APPLE__
|
2014-10-29 22:58:03 +08:00
|
|
|
if (!shortcut) shortcut = elem->Attribute("mac");
|
2015-02-12 23:46:56 +08:00
|
|
|
#else
|
2014-10-29 22:58:03 +08:00
|
|
|
if (!shortcut) shortcut = elem->Attribute("linux");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!shortcut)
|
|
|
|
shortcut = elem->Attribute("shortcut");
|
|
|
|
|
|
|
|
return shortcut;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
std::string get_user_friendly_string_for_keyaction(app::KeyAction action) {
|
2014-10-29 22:58:03 +08:00
|
|
|
for (int c=0; actions[c].name; ++c) {
|
|
|
|
if (action == actions[c].action)
|
|
|
|
return actions[c].userfriendly;
|
|
|
|
}
|
2018-07-20 10:05:14 +08:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string get_user_friendly_string_for_wheelaction(app::WheelAction wheelAction) {
|
|
|
|
for (int c=0; wheel_actions[c].name; ++c) {
|
|
|
|
if (wheelAction == wheel_actions[c].action)
|
|
|
|
return wheel_actions[c].userfriendly;
|
|
|
|
}
|
|
|
|
return std::string();
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
2015-02-12 23:16:25 +08:00
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
namespace base {
|
|
|
|
|
|
|
|
template<> app::KeyAction convert_to(const std::string& from) {
|
|
|
|
app::KeyAction action = app::KeyAction::None;
|
|
|
|
for (int c=0; actions[c].name; ++c) {
|
|
|
|
if (from == actions[c].name)
|
|
|
|
return actions[c].action;
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> std::string convert_to(const app::KeyAction& from) {
|
|
|
|
for (int c=0; actions[c].name; ++c) {
|
|
|
|
if (from == actions[c].action)
|
|
|
|
return actions[c].name;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
2015-02-12 23:16:25 +08:00
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
template<> app::WheelAction convert_to(const std::string& from) {
|
|
|
|
app::WheelAction action = app::WheelAction::None;
|
|
|
|
for (int c=0; wheel_actions[c].name; ++c) {
|
|
|
|
if (from == wheel_actions[c].name)
|
|
|
|
return wheel_actions[c].action;
|
|
|
|
}
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<> std::string convert_to(const app::WheelAction& from) {
|
|
|
|
for (int c=0; wheel_actions[c].name; ++c) {
|
|
|
|
if (from == wheel_actions[c].action)
|
|
|
|
return wheel_actions[c].name;
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
} // namespace base
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
|
|
using namespace ui;
|
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Key
|
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
Key::Key(Command* command, const Params& params, KeyContext keyContext)
|
2014-10-29 22:58:03 +08:00
|
|
|
: m_type(KeyType::Command)
|
|
|
|
, m_useUsers(false)
|
|
|
|
, m_keycontext(keyContext)
|
|
|
|
, m_command(command)
|
2015-03-12 02:40:22 +08:00
|
|
|
, m_params(params)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Key::Key(KeyType type, tools::Tool* tool)
|
|
|
|
: m_type(type)
|
|
|
|
, m_useUsers(false)
|
|
|
|
, m_keycontext(KeyContext::Any)
|
|
|
|
, m_tool(tool)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Key::Key(KeyAction action)
|
|
|
|
: m_type(KeyType::Action)
|
|
|
|
, m_useUsers(false)
|
|
|
|
, m_keycontext(KeyContext::Any)
|
|
|
|
, m_action(action)
|
|
|
|
{
|
2014-11-16 05:31:12 +08:00
|
|
|
switch (action) {
|
|
|
|
case KeyAction::None:
|
|
|
|
m_keycontext = KeyContext::Any;
|
|
|
|
break;
|
|
|
|
case KeyAction::CopySelection:
|
|
|
|
case KeyAction::SnapToGrid:
|
2016-04-05 05:46:48 +08:00
|
|
|
case KeyAction::LockAxis:
|
2015-09-12 07:04:02 +08:00
|
|
|
m_keycontext = KeyContext::TranslatingSelection;
|
2014-11-16 05:31:12 +08:00
|
|
|
break;
|
|
|
|
case KeyAction::AngleSnap:
|
2015-09-12 07:04:02 +08:00
|
|
|
m_keycontext = KeyContext::RotatingSelection;
|
2014-11-16 05:31:12 +08:00
|
|
|
break;
|
|
|
|
case KeyAction::MaintainAspectRatio:
|
2016-04-08 22:55:40 +08:00
|
|
|
case KeyAction::ScaleFromCenter:
|
2015-09-12 07:04:02 +08:00
|
|
|
m_keycontext = KeyContext::ScalingSelection;
|
2014-11-16 05:31:12 +08:00
|
|
|
break;
|
|
|
|
case KeyAction::AddSelection:
|
|
|
|
case KeyAction::SubtractSelection:
|
2018-10-27 01:04:08 +08:00
|
|
|
case KeyAction::IntersectSelection:
|
2015-09-12 07:04:02 +08:00
|
|
|
m_keycontext = KeyContext::SelectionTool;
|
2014-11-16 05:31:12 +08:00
|
|
|
break;
|
2014-11-17 05:33:31 +08:00
|
|
|
case KeyAction::AutoSelectLayer:
|
|
|
|
m_keycontext = KeyContext::MoveTool;
|
|
|
|
break;
|
2015-09-16 23:19:10 +08:00
|
|
|
case KeyAction::StraightLineFromLastPoint:
|
2017-06-23 04:47:56 +08:00
|
|
|
case KeyAction::AngleSnapFromLastPoint:
|
2015-09-16 23:19:10 +08:00
|
|
|
m_keycontext = KeyContext::FreehandTool;
|
|
|
|
break;
|
2016-04-05 05:46:48 +08:00
|
|
|
case KeyAction::MoveOrigin:
|
|
|
|
case KeyAction::SquareAspect:
|
|
|
|
case KeyAction::DrawFromCenter:
|
2018-03-07 06:21:09 +08:00
|
|
|
case KeyAction::RotateShape:
|
2016-04-05 05:46:48 +08:00
|
|
|
m_keycontext = KeyContext::ShapeTool;
|
|
|
|
break;
|
2014-11-16 05:31:12 +08:00
|
|
|
case KeyAction::LeftMouseButton:
|
|
|
|
m_keycontext = KeyContext::Any;
|
|
|
|
break;
|
|
|
|
case KeyAction::RightMouseButton:
|
|
|
|
m_keycontext = KeyContext::Any;
|
|
|
|
break;
|
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
Key::Key(WheelAction wheelAction)
|
|
|
|
: m_type(KeyType::WheelAction)
|
|
|
|
, m_useUsers(false)
|
|
|
|
, m_keycontext(KeyContext::MouseWheel)
|
|
|
|
, m_action(KeyAction::None)
|
|
|
|
, m_wheelAction(wheelAction)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
void Key::add(const ui::Accelerator& accel,
|
|
|
|
const KeySource source,
|
|
|
|
KeyboardShortcuts& globalKeys)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
|
|
|
Accelerators* accels = &m_accels;
|
|
|
|
|
|
|
|
if (source == KeySource::UserDefined) {
|
|
|
|
if (!m_useUsers) {
|
|
|
|
m_useUsers = true;
|
|
|
|
m_users = m_accels;
|
|
|
|
}
|
|
|
|
accels = &m_users;
|
2014-11-16 05:31:12 +08:00
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
// Remove the accelerator from other commands
|
|
|
|
if (source == KeySource::UserDefined) {
|
2018-07-24 02:15:04 +08:00
|
|
|
globalKeys.disableAccel(accel, m_keycontext, this);
|
2014-11-16 05:31:12 +08:00
|
|
|
m_userRemoved.remove(accel);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
// Add the accelerator
|
|
|
|
accels->add(accel);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2018-07-25 00:45:12 +08:00
|
|
|
const ui::Accelerator* Key::isPressed(const Message* msg,
|
|
|
|
KeyboardShortcuts& globalKeys) const
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-20 10:05:14 +08:00
|
|
|
if (auto keyMsg = dynamic_cast<const KeyMessage*>(msg)) {
|
|
|
|
for (const Accelerator& accel : accels()) {
|
|
|
|
if (accel.isPressed(keyMsg->modifiers(),
|
|
|
|
keyMsg->scancode(),
|
|
|
|
keyMsg->unicodeChar()) &&
|
|
|
|
(m_keycontext == KeyContext::Any ||
|
2018-07-24 02:15:04 +08:00
|
|
|
m_keycontext == globalKeys.getCurrentKeyContext())) {
|
2018-07-25 00:45:12 +08:00
|
|
|
return &accel;
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (auto mouseMsg = dynamic_cast<const MouseMessage*>(msg)) {
|
|
|
|
for (const Accelerator& accel : accels()) {
|
|
|
|
if ((accel.modifiers() == mouseMsg->modifiers()) &&
|
|
|
|
(m_keycontext == KeyContext::Any ||
|
|
|
|
// TODO we could have multiple mouse wheel key-context,
|
|
|
|
// like "sprite editor" context, or "timeline" context,
|
|
|
|
// etc.
|
|
|
|
m_keycontext == KeyContext::MouseWheel)) {
|
2018-07-25 00:45:12 +08:00
|
|
|
return &accel;
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
2018-07-25 00:45:12 +08:00
|
|
|
return nullptr;
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
2015-05-21 02:02:31 +08:00
|
|
|
bool Key::isPressed() const
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
|
|
|
for (const Accelerator& accel : this->accels()) {
|
2015-05-21 02:02:31 +08:00
|
|
|
if (accel.isPressed())
|
2014-10-29 22:58:03 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-08-26 00:29:19 +08:00
|
|
|
bool Key::isLooselyPressed() const
|
|
|
|
{
|
|
|
|
for (const Accelerator& accel : this->accels()) {
|
|
|
|
if (accel.isLooselyPressed())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
bool Key::hasAccel(const ui::Accelerator& accel) const
|
|
|
|
{
|
2014-11-16 05:31:12 +08:00
|
|
|
return accels().has(accel);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Key::disableAccel(const ui::Accelerator& accel)
|
|
|
|
{
|
|
|
|
if (!m_useUsers) {
|
|
|
|
m_useUsers = true;
|
|
|
|
m_users = m_accels;
|
|
|
|
}
|
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
m_users.remove(accel);
|
|
|
|
|
|
|
|
if (m_accels.has(accel))
|
|
|
|
m_userRemoved.add(accel);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Key::reset()
|
|
|
|
{
|
|
|
|
m_users.clear();
|
2014-11-16 05:31:12 +08:00
|
|
|
m_userRemoved.clear();
|
2014-10-29 22:58:03 +08:00
|
|
|
m_useUsers = false;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
void Key::copyOriginalToUser()
|
|
|
|
{
|
|
|
|
m_users = m_accels;
|
|
|
|
m_userRemoved.clear();
|
|
|
|
m_useUsers = true;
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
std::string Key::triggerString() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
|
|
|
case KeyType::Command:
|
2015-03-12 02:40:22 +08:00
|
|
|
m_command->loadParams(m_params);
|
2014-10-29 22:58:03 +08:00
|
|
|
return m_command->friendlyName();
|
|
|
|
case KeyType::Tool:
|
|
|
|
case KeyType::Quicktool: {
|
|
|
|
std::string text = m_tool->getText();
|
|
|
|
if (m_type == KeyType::Quicktool)
|
|
|
|
text += " (quick)";
|
|
|
|
return text;
|
|
|
|
}
|
|
|
|
case KeyType::Action:
|
|
|
|
return get_user_friendly_string_for_keyaction(m_action);
|
2018-07-20 10:05:14 +08:00
|
|
|
case KeyType::WheelAction:
|
|
|
|
return get_user_friendly_string_for_wheelaction(m_wheelAction);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
return "Unknown";
|
|
|
|
}
|
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// KeyboardShortcuts
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
KeyboardShortcuts* KeyboardShortcuts::instance()
|
|
|
|
{
|
|
|
|
static KeyboardShortcuts* singleton = NULL;
|
|
|
|
if (!singleton)
|
|
|
|
singleton = new KeyboardShortcuts();
|
|
|
|
return singleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardShortcuts::KeyboardShortcuts()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyboardShortcuts::~KeyboardShortcuts()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
void KeyboardShortcuts::setKeys(const KeyboardShortcuts& keys,
|
|
|
|
const bool cloneKeys)
|
|
|
|
{
|
|
|
|
if (cloneKeys) {
|
|
|
|
for (const KeyPtr& key : keys)
|
|
|
|
m_keys.push_back(std::make_shared<Key>(*key));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_keys = keys.m_keys;
|
|
|
|
}
|
|
|
|
UserChange();
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
void KeyboardShortcuts::clear()
|
|
|
|
{
|
|
|
|
m_keys.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::importFile(TiXmlElement* rootElement, KeySource source)
|
|
|
|
{
|
|
|
|
// <keyboard><commands><key>
|
|
|
|
TiXmlHandle handle(rootElement);
|
|
|
|
TiXmlElement* xmlKey = handle
|
|
|
|
.FirstChild("commands")
|
|
|
|
.FirstChild("key").ToElement();
|
|
|
|
while (xmlKey) {
|
|
|
|
const char* command_name = xmlKey->Attribute("command");
|
|
|
|
const char* command_key = get_shortcut(xmlKey);
|
2014-11-16 05:31:12 +08:00
|
|
|
bool removed = bool_attr_is_true(xmlKey, "removed");
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2015-11-19 03:15:25 +08:00
|
|
|
if (command_name) {
|
2017-11-30 22:57:18 +08:00
|
|
|
Command* command = Commands::instance()->byId(command_name);
|
2014-10-29 22:58:03 +08:00
|
|
|
if (command) {
|
|
|
|
// Read context
|
|
|
|
KeyContext keycontext = KeyContext::Any;
|
|
|
|
const char* keycontextstr = xmlKey->Attribute("context");
|
|
|
|
if (keycontextstr) {
|
|
|
|
if (strcmp(keycontextstr, "Selection") == 0)
|
2015-09-12 07:04:02 +08:00
|
|
|
keycontext = KeyContext::SelectionTool;
|
2014-10-29 22:58:03 +08:00
|
|
|
else if (strcmp(keycontextstr, "Normal") == 0)
|
|
|
|
keycontext = KeyContext::Normal;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read params
|
|
|
|
Params params;
|
|
|
|
|
|
|
|
TiXmlElement* xmlParam = xmlKey->FirstChildElement("param");
|
|
|
|
while (xmlParam) {
|
|
|
|
const char* param_name = xmlParam->Attribute("name");
|
|
|
|
const char* param_value = xmlParam->Attribute("value");
|
|
|
|
|
|
|
|
if (param_name && param_value)
|
|
|
|
params.set(param_name, param_value);
|
|
|
|
|
|
|
|
xmlParam = xmlParam->NextSiblingElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the keyboard shortcut to the command
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = this->command(command_name, params, keycontext);
|
2015-11-19 03:15:25 +08:00
|
|
|
if (key && command_key) {
|
2014-11-16 05:31:12 +08:00
|
|
|
Accelerator accel(command_key);
|
|
|
|
|
|
|
|
if (!removed) {
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(accel, source, *this);
|
2014-11-16 05:31:12 +08:00
|
|
|
|
2015-11-30 20:34:14 +08:00
|
|
|
// Add the shortcut to the menuitems with this command
|
|
|
|
// (this is only visual, the
|
|
|
|
// "CustomizedGuiManager::onProcessMessage" is the only
|
|
|
|
// one that process keyboard shortcuts)
|
2014-11-16 05:31:12 +08:00
|
|
|
if (key->accels().size() == 1) {
|
|
|
|
AppMenus::instance()->applyShortcutToMenuitemsWithCommand(
|
2015-03-12 02:40:22 +08:00
|
|
|
command, params, key);
|
2014-11-16 05:31:12 +08:00
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
2014-11-16 05:31:12 +08:00
|
|
|
else
|
|
|
|
key->disableAccel(accel);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
xmlKey = xmlKey->NextSiblingElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load keyboard shortcuts for tools
|
2018-07-20 10:05:14 +08:00
|
|
|
// <keyboard><tools><key>
|
2014-10-29 22:58:03 +08:00
|
|
|
xmlKey = handle
|
|
|
|
.FirstChild("tools")
|
|
|
|
.FirstChild("key").ToElement();
|
|
|
|
while (xmlKey) {
|
|
|
|
const char* tool_id = xmlKey->Attribute("tool");
|
|
|
|
const char* tool_key = get_shortcut(xmlKey);
|
2014-11-16 05:31:12 +08:00
|
|
|
bool removed = bool_attr_is_true(xmlKey, "removed");
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2015-12-27 01:52:29 +08:00
|
|
|
if (tool_id) {
|
2016-04-23 00:19:06 +08:00
|
|
|
tools::Tool* tool = App::instance()->toolBox()->getToolById(tool_id);
|
2014-10-29 22:58:03 +08:00
|
|
|
if (tool) {
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = this->tool(tool);
|
2015-12-27 01:52:29 +08:00
|
|
|
if (key && tool_key) {
|
2016-10-27 23:25:33 +08:00
|
|
|
LOG(VERBOSE) << "KEYS: Shortcut for tool " << tool_id << ": " << tool_key << "\n";
|
2014-11-16 05:31:12 +08:00
|
|
|
Accelerator accel(tool_key);
|
|
|
|
|
|
|
|
if (!removed)
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(accel, source, *this);
|
2014-11-16 05:31:12 +08:00
|
|
|
else
|
|
|
|
key->disableAccel(accel);
|
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlKey = xmlKey->NextSiblingElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load keyboard shortcuts for quicktools
|
2018-07-20 10:05:14 +08:00
|
|
|
// <keyboard><quicktools><key>
|
2014-10-29 22:58:03 +08:00
|
|
|
xmlKey = handle
|
|
|
|
.FirstChild("quicktools")
|
|
|
|
.FirstChild("key").ToElement();
|
|
|
|
while (xmlKey) {
|
|
|
|
const char* tool_id = xmlKey->Attribute("tool");
|
|
|
|
const char* tool_key = get_shortcut(xmlKey);
|
2014-11-16 05:31:12 +08:00
|
|
|
bool removed = bool_attr_is_true(xmlKey, "removed");
|
|
|
|
|
2015-12-27 01:52:29 +08:00
|
|
|
if (tool_id) {
|
2016-04-23 00:19:06 +08:00
|
|
|
tools::Tool* tool = App::instance()->toolBox()->getToolById(tool_id);
|
2014-10-29 22:58:03 +08:00
|
|
|
if (tool) {
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = this->quicktool(tool);
|
2015-12-27 01:52:29 +08:00
|
|
|
if (key && tool_key) {
|
2016-10-27 23:25:33 +08:00
|
|
|
LOG(VERBOSE) << "KEYS: Shortcut for quicktool " << tool_id << ": " << tool_key << "\n";
|
2014-11-16 05:31:12 +08:00
|
|
|
Accelerator accel(tool_key);
|
|
|
|
|
|
|
|
if (!removed)
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(accel, source, *this);
|
2014-11-16 05:31:12 +08:00
|
|
|
else
|
|
|
|
key->disableAccel(accel);
|
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlKey = xmlKey->NextSiblingElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load special keyboard shortcuts for sprite editor customization
|
2018-07-20 10:05:14 +08:00
|
|
|
// <keyboard><actions><key>
|
2014-10-29 22:58:03 +08:00
|
|
|
xmlKey = handle
|
|
|
|
.FirstChild("actions")
|
|
|
|
.FirstChild("key").ToElement();
|
|
|
|
while (xmlKey) {
|
2018-07-20 10:05:14 +08:00
|
|
|
const char* action_id = xmlKey->Attribute("action");
|
|
|
|
const char* action_key = get_shortcut(xmlKey);
|
2014-11-16 05:31:12 +08:00
|
|
|
bool removed = bool_attr_is_true(xmlKey, "removed");
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
if (action_id) {
|
|
|
|
KeyAction action = base::convert_to<KeyAction, std::string>(action_id);
|
2014-10-29 22:58:03 +08:00
|
|
|
if (action != KeyAction::None) {
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = this->action(action);
|
2018-07-20 10:05:14 +08:00
|
|
|
if (key && action_key) {
|
|
|
|
LOG(VERBOSE) << "KEYS: Shortcut for action " << action_id
|
|
|
|
<< ": " << action_key << "\n";
|
|
|
|
Accelerator accel(action_key);
|
|
|
|
|
|
|
|
if (!removed)
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(accel, source, *this);
|
2018-07-20 10:05:14 +08:00
|
|
|
else
|
|
|
|
key->disableAccel(accel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlKey = xmlKey->NextSiblingElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load special keyboard shortcuts for mouse wheel customization
|
|
|
|
// <keyboard><wheel><key>
|
|
|
|
xmlKey = handle
|
|
|
|
.FirstChild("wheel")
|
|
|
|
.FirstChild("key").ToElement();
|
|
|
|
while (xmlKey) {
|
|
|
|
const char* action_id = xmlKey->Attribute("action");
|
|
|
|
const char* action_key = get_shortcut(xmlKey);
|
|
|
|
bool removed = bool_attr_is_true(xmlKey, "removed");
|
|
|
|
|
|
|
|
if (action_id) {
|
|
|
|
WheelAction action = base::convert_to<WheelAction, std::string>(action_id);
|
|
|
|
if (action != WheelAction::None) {
|
|
|
|
KeyPtr key = this->wheelAction(action);
|
|
|
|
if (key && action_key) {
|
|
|
|
LOG(VERBOSE) << "KEYS: Shortcut for wheel action " << action_id
|
|
|
|
<< ": " << action_key << "\n";
|
|
|
|
Accelerator accel(action_key);
|
2014-11-16 05:31:12 +08:00
|
|
|
|
|
|
|
if (!removed)
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(accel, source, *this);
|
2014-11-16 05:31:12 +08:00
|
|
|
else
|
|
|
|
key->disableAccel(accel);
|
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
xmlKey = xmlKey->NextSiblingElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::importFile(const std::string& filename, KeySource source)
|
|
|
|
{
|
|
|
|
XmlDocumentRef doc = app::open_xml(filename);
|
2015-04-03 07:42:43 +08:00
|
|
|
TiXmlHandle handle(doc.get());
|
2014-10-29 22:58:03 +08:00
|
|
|
TiXmlElement* xmlKey = handle.FirstChild("keyboard").ToElement();
|
|
|
|
|
|
|
|
importFile(xmlKey, source);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::exportFile(const std::string& filename)
|
|
|
|
{
|
|
|
|
XmlDocumentRef doc(new TiXmlDocument());
|
|
|
|
|
|
|
|
TiXmlElement keyboard("keyboard");
|
|
|
|
TiXmlElement commands("commands");
|
|
|
|
TiXmlElement tools("tools");
|
|
|
|
TiXmlElement quicktools("quicktools");
|
|
|
|
TiXmlElement actions("actions");
|
2018-07-20 10:05:14 +08:00
|
|
|
TiXmlElement wheel("wheel");
|
2014-10-29 22:58:03 +08:00
|
|
|
|
|
|
|
keyboard.SetAttribute("version", XML_KEYBOARD_FILE_VERSION);
|
|
|
|
|
|
|
|
exportKeys(commands, KeyType::Command);
|
|
|
|
exportKeys(tools, KeyType::Tool);
|
|
|
|
exportKeys(quicktools, KeyType::Quicktool);
|
|
|
|
exportKeys(actions, KeyType::Action);
|
2018-07-20 10:05:14 +08:00
|
|
|
exportKeys(wheel, KeyType::WheelAction);
|
2015-02-12 23:16:25 +08:00
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
keyboard.InsertEndChild(commands);
|
|
|
|
keyboard.InsertEndChild(tools);
|
|
|
|
keyboard.InsertEndChild(quicktools);
|
|
|
|
keyboard.InsertEndChild(actions);
|
2018-07-20 10:05:14 +08:00
|
|
|
keyboard.InsertEndChild(wheel);
|
2015-12-22 21:24:25 +08:00
|
|
|
|
|
|
|
TiXmlDeclaration declaration("1.0", "utf-8", "");
|
|
|
|
doc->InsertEndChild(declaration);
|
2014-10-29 22:58:03 +08:00
|
|
|
doc->InsertEndChild(keyboard);
|
|
|
|
save_xml(doc, filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::exportKeys(TiXmlElement& parent, KeyType type)
|
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2014-10-29 22:58:03 +08:00
|
|
|
// Save only user defined accelerators.
|
2014-11-16 05:31:12 +08:00
|
|
|
if (key->type() != type)
|
2014-10-29 22:58:03 +08:00
|
|
|
continue;
|
|
|
|
|
2014-11-16 05:31:12 +08:00
|
|
|
for (const ui::Accelerator& accel : key->userRemovedAccels())
|
2018-07-18 10:53:08 +08:00
|
|
|
exportAccel(parent, key.get(), accel, true);
|
2014-11-16 05:31:12 +08:00
|
|
|
|
|
|
|
for (const ui::Accelerator& accel : key->userAccels())
|
2018-07-18 10:53:08 +08:00
|
|
|
exportAccel(parent, key.get(), accel, false);
|
2014-11-16 05:31:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
void KeyboardShortcuts::exportAccel(TiXmlElement& parent, const Key* key, const ui::Accelerator& accel, bool removed)
|
2014-11-16 05:31:12 +08:00
|
|
|
{
|
|
|
|
TiXmlElement elem("key");
|
|
|
|
|
|
|
|
switch (key->type()) {
|
|
|
|
|
|
|
|
case KeyType::Command: {
|
2015-05-02 06:41:18 +08:00
|
|
|
elem.SetAttribute("command", key->command()->id().c_str());
|
2014-11-16 05:31:12 +08:00
|
|
|
|
2017-10-24 00:03:18 +08:00
|
|
|
if (key->keycontext() != KeyContext::Any)
|
|
|
|
elem.SetAttribute(
|
|
|
|
"context", convertKeyContextToString(key->keycontext()).c_str());
|
2014-11-16 05:31:12 +08:00
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
for (const auto& param : key->params()) {
|
|
|
|
if (param.second.empty())
|
|
|
|
continue;
|
2014-11-16 05:31:12 +08:00
|
|
|
|
2015-03-12 02:40:22 +08:00
|
|
|
TiXmlElement paramElem("param");
|
|
|
|
paramElem.SetAttribute("name", param.first.c_str());
|
|
|
|
paramElem.SetAttribute("value", param.second.c_str());
|
|
|
|
elem.InsertEndChild(paramElem);
|
2014-11-16 05:31:12 +08:00
|
|
|
}
|
|
|
|
break;
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
2014-11-16 05:31:12 +08:00
|
|
|
|
|
|
|
case KeyType::Tool:
|
|
|
|
case KeyType::Quicktool:
|
|
|
|
elem.SetAttribute("tool", key->tool()->getId().c_str());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case KeyType::Action:
|
|
|
|
elem.SetAttribute("action",
|
|
|
|
base::convert_to<std::string>(key->action()).c_str());
|
|
|
|
break;
|
2018-07-20 10:05:14 +08:00
|
|
|
|
|
|
|
case KeyType::WheelAction:
|
|
|
|
elem.SetAttribute("action",
|
|
|
|
base::convert_to<std::string>(key->wheelAction()).c_str());
|
|
|
|
break;
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
2014-11-16 05:31:12 +08:00
|
|
|
|
|
|
|
elem.SetAttribute("shortcut", accel.toString().c_str());
|
|
|
|
|
|
|
|
if (removed)
|
|
|
|
elem.SetAttribute("removed", "true");
|
|
|
|
|
|
|
|
parent.InsertEndChild(elem);
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::reset()
|
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys)
|
2014-10-29 22:58:03 +08:00
|
|
|
key->reset();
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr KeyboardShortcuts::command(const char* commandName, const Params& params, KeyContext keyContext)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2017-11-30 22:57:18 +08:00
|
|
|
Command* command = Commands::instance()->byId(commandName);
|
2014-10-29 22:58:03 +08:00
|
|
|
if (!command)
|
2018-07-18 10:53:08 +08:00
|
|
|
return nullptr;
|
2014-10-29 22:58:03 +08:00
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2014-10-29 22:58:03 +08:00
|
|
|
if (key->type() == KeyType::Command &&
|
|
|
|
key->keycontext() == keyContext &&
|
|
|
|
key->command() == command &&
|
2015-03-12 02:40:22 +08:00
|
|
|
key->params() == params) {
|
2014-10-29 22:58:03 +08:00
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = std::make_shared<Key>(command, params, keyContext);
|
2014-10-29 22:58:03 +08:00
|
|
|
m_keys.push_back(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr KeyboardShortcuts::tool(tools::Tool* tool)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2014-10-29 22:58:03 +08:00
|
|
|
if (key->type() == KeyType::Tool &&
|
|
|
|
key->tool() == tool) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = std::make_shared<Key>(KeyType::Tool, tool);
|
2014-10-29 22:58:03 +08:00
|
|
|
m_keys.push_back(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr KeyboardShortcuts::quicktool(tools::Tool* tool)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2014-10-29 22:58:03 +08:00
|
|
|
if (key->type() == KeyType::Quicktool &&
|
|
|
|
key->tool() == tool) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = std::make_shared<Key>(KeyType::Quicktool, tool);
|
2014-10-29 22:58:03 +08:00
|
|
|
m_keys.push_back(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr KeyboardShortcuts::action(KeyAction action)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2014-10-29 22:58:03 +08:00
|
|
|
if (key->type() == KeyType::Action &&
|
|
|
|
key->action() == action) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = std::make_shared<Key>(action);
|
2014-10-29 22:58:03 +08:00
|
|
|
m_keys.push_back(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
KeyPtr KeyboardShortcuts::wheelAction(WheelAction wheelAction)
|
|
|
|
{
|
|
|
|
for (KeyPtr& key : m_keys) {
|
|
|
|
if (key->type() == KeyType::WheelAction &&
|
|
|
|
key->wheelAction() == wheelAction) {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyPtr key = std::make_shared<Key>(wheelAction);
|
|
|
|
m_keys.push_back(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2017-12-06 21:22:32 +08:00
|
|
|
void KeyboardShortcuts::disableAccel(const ui::Accelerator& accel,
|
|
|
|
const KeyContext keyContext,
|
|
|
|
const Key* newKey)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2017-12-06 21:22:32 +08:00
|
|
|
if (key->keycontext() == keyContext &&
|
|
|
|
key->hasAccel(accel) &&
|
|
|
|
// Tools can contain the same keyboard shortcut
|
|
|
|
(key->type() != KeyType::Tool ||
|
|
|
|
newKey == nullptr ||
|
|
|
|
newKey->type() != KeyType::Tool)) {
|
2014-10-29 22:58:03 +08:00
|
|
|
key->disableAccel(accel);
|
2017-12-06 21:22:32 +08:00
|
|
|
}
|
2014-10-29 22:58:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyContext KeyboardShortcuts::getCurrentKeyContext()
|
|
|
|
{
|
2018-07-07 22:54:44 +08:00
|
|
|
Doc* doc = UIContext::instance()->activeDocument();
|
2014-10-29 22:58:03 +08:00
|
|
|
if (doc &&
|
|
|
|
doc->isMaskVisible() &&
|
2019-12-19 05:00:11 +08:00
|
|
|
// The active key context will be the selectedTool() (in the
|
|
|
|
// toolbox) instead of the activeTool() (which depends on the
|
|
|
|
// quick tool shortcuts).
|
|
|
|
//
|
|
|
|
// E.g. If we have the rectangular marquee tool selected
|
|
|
|
// (selectedTool()) are going to press keys like alt+left or
|
|
|
|
// alt+right to move the selection edge in the selection
|
|
|
|
// context, the alt key switches the activeTool() to the
|
|
|
|
// eyedropper, but we want to use alt+left and alt+right in the
|
|
|
|
// original context (the selection tool).
|
|
|
|
App::instance()->activeToolManager()
|
|
|
|
->selectedTool()->getInk(0)->isSelection())
|
2015-09-12 07:04:02 +08:00
|
|
|
return KeyContext::SelectionTool;
|
2014-10-29 22:58:03 +08:00
|
|
|
else
|
|
|
|
return KeyContext::Normal;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
bool KeyboardShortcuts::getCommandFromKeyMessage(const Message* msg, Command** command, Params* params)
|
2014-10-29 22:58:03 +08:00
|
|
|
{
|
2018-07-18 10:53:08 +08:00
|
|
|
for (KeyPtr& key : m_keys) {
|
2018-07-24 02:15:04 +08:00
|
|
|
if (key->type() == KeyType::Command &&
|
|
|
|
key->isPressed(msg, *this)) {
|
2014-10-29 22:58:03 +08:00
|
|
|
if (command) *command = key->command();
|
|
|
|
if (params) *params = key->params();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
tools::Tool* KeyboardShortcuts::getCurrentQuicktool(tools::Tool* currentTool)
|
|
|
|
{
|
|
|
|
if (currentTool && currentTool->getInk(0)->isSelection()) {
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = action(KeyAction::CopySelection);
|
2015-05-21 02:02:31 +08:00
|
|
|
if (key && key->isPressed())
|
2014-10-29 22:58:03 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-04-23 00:19:06 +08:00
|
|
|
tools::ToolBox* toolbox = App::instance()->toolBox();
|
2014-10-29 22:58:03 +08:00
|
|
|
|
|
|
|
// Iterate over all tools
|
|
|
|
for (tools::Tool* tool : *toolbox) {
|
2018-07-18 10:53:08 +08:00
|
|
|
KeyPtr key = quicktool(tool);
|
2014-10-29 22:58:03 +08:00
|
|
|
|
|
|
|
// Collect all tools with the pressed keyboard-shortcut
|
2015-05-21 02:02:31 +08:00
|
|
|
if (key && key->isPressed()) {
|
2014-10-29 22:58:03 +08:00
|
|
|
return tool;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-08-26 00:29:19 +08:00
|
|
|
KeyAction KeyboardShortcuts::getCurrentActionModifiers(KeyContext context)
|
|
|
|
{
|
|
|
|
KeyAction flags = KeyAction::None;
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
for (const KeyPtr& key : m_keys) {
|
2015-08-26 00:29:19 +08:00
|
|
|
if (key->type() == KeyType::Action &&
|
|
|
|
key->keycontext() == context &&
|
|
|
|
key->isLooselyPressed()) {
|
|
|
|
flags = static_cast<KeyAction>(int(flags) | int(key->action()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
2018-07-20 10:05:14 +08:00
|
|
|
WheelAction KeyboardShortcuts::getWheelActionFromMouseMessage(const KeyContext context,
|
|
|
|
const ui::Message* msg)
|
|
|
|
{
|
2018-07-25 00:45:12 +08:00
|
|
|
WheelAction wheelAction = WheelAction::None;
|
|
|
|
const ui::Accelerator* bestAccel = nullptr;
|
|
|
|
KeyPtr bestKey;
|
2018-07-20 10:05:14 +08:00
|
|
|
for (const KeyPtr& key : m_keys) {
|
|
|
|
if (key->type() == KeyType::WheelAction &&
|
2018-07-25 00:45:12 +08:00
|
|
|
key->keycontext() == context) {
|
|
|
|
const ui::Accelerator* accel = key->isPressed(msg, *this);
|
|
|
|
if ((accel) &&
|
|
|
|
(!bestAccel || bestAccel->modifiers() < accel->modifiers())) {
|
|
|
|
bestAccel = accel;
|
|
|
|
wheelAction = key->wheelAction();
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
2018-07-25 00:45:12 +08:00
|
|
|
return wheelAction;
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyboardShortcuts::hasMouseWheelCustomization() const
|
|
|
|
{
|
|
|
|
for (const KeyPtr& key : m_keys) {
|
|
|
|
if (key->type() == KeyType::WheelAction &&
|
|
|
|
!key->userAccels().empty())
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
void KeyboardShortcuts::clearMouseWheelKeys()
|
2018-07-20 10:05:14 +08:00
|
|
|
{
|
2018-07-24 02:15:04 +08:00
|
|
|
for (auto it=m_keys.begin(); it!=m_keys.end(); ) {
|
|
|
|
if ((*it)->type() == KeyType::WheelAction)
|
|
|
|
it = m_keys.erase(it);
|
|
|
|
else
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
2018-07-20 10:05:14 +08:00
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
void KeyboardShortcuts::addMissingMouseWheelKeys()
|
|
|
|
{
|
|
|
|
for (int wheelAction=int(WheelAction::First);
|
|
|
|
wheelAction<=int(WheelAction::Last); ++wheelAction) {
|
|
|
|
auto it = std::find_if(
|
|
|
|
m_keys.begin(), m_keys.end(),
|
|
|
|
[wheelAction](const KeyPtr& key) -> bool {
|
|
|
|
return key->wheelAction() == (WheelAction)wheelAction;
|
|
|
|
});
|
|
|
|
if (it == m_keys.end()) {
|
|
|
|
KeyPtr key = std::make_shared<Key>((WheelAction)wheelAction);
|
|
|
|
m_keys.push_back(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::setDefaultMouseWheelKeys(const bool zoomWithWheel)
|
|
|
|
{
|
|
|
|
clearMouseWheelKeys();
|
|
|
|
|
|
|
|
KeyPtr key;
|
2018-07-20 10:05:14 +08:00
|
|
|
key = std::make_shared<Key>(WheelAction::Zoom);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator(zoomWithWheel ? kKeyNoneModifier:
|
|
|
|
kKeyCtrlModifier, kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-20 10:05:14 +08:00
|
|
|
|
|
|
|
if (!zoomWithWheel) {
|
|
|
|
key = std::make_shared<Key>(WheelAction::VScroll);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator(kKeyNoneModifier, kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
key = std::make_shared<Key>(WheelAction::HScroll);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator(kKeyShiftModifier, kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-20 10:05:14 +08:00
|
|
|
|
|
|
|
key = std::make_shared<Key>(WheelAction::FgColor);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator(kKeyAltModifier, kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-20 10:05:14 +08:00
|
|
|
|
|
|
|
key = std::make_shared<Key>(WheelAction::BgColor);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator((KeyModifiers)(kKeyAltModifier | kKeyShiftModifier), kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-20 10:05:14 +08:00
|
|
|
|
|
|
|
if (zoomWithWheel) {
|
2018-07-25 00:32:36 +08:00
|
|
|
key = std::make_shared<Key>(WheelAction::BrushSize);
|
2018-07-24 02:15:04 +08:00
|
|
|
key->add(Accelerator(kKeyCtrlModifier, kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-25 00:32:36 +08:00
|
|
|
|
|
|
|
key = std::make_shared<Key>(WheelAction::Frame);
|
|
|
|
key->add(Accelerator((KeyModifiers)(kKeyCtrlModifier | kKeyShiftModifier), kKeyNil, 0),
|
|
|
|
KeySource::Original, *this);
|
|
|
|
m_keys.push_back(key);
|
2018-07-24 02:15:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardShortcuts::addMissingKeysForCommands()
|
|
|
|
{
|
|
|
|
std::set<std::string> commandsAlreadyAdded;
|
|
|
|
for (const KeyPtr& key : m_keys) {
|
|
|
|
if (key->type() != KeyType::Command)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (key->params().empty())
|
|
|
|
commandsAlreadyAdded.insert(key->command()->id());
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2018-07-24 02:15:04 +08:00
|
|
|
std::vector<std::string> ids;
|
|
|
|
Commands* commands = Commands::instance();
|
|
|
|
commands->getAllIds(ids);
|
|
|
|
|
|
|
|
for (const std::string& id : ids) {
|
|
|
|
Command* command = commands->byId(id.c_str());
|
|
|
|
|
|
|
|
// Don't add commands that need params (they will be added to
|
|
|
|
// the list using the list of keyboard shortcuts from gui.xml).
|
|
|
|
if (command->needsParams())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
auto it = commandsAlreadyAdded.find(command->id());
|
|
|
|
if (it != commandsAlreadyAdded.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Create the new Key element in KeyboardShortcuts for this
|
|
|
|
// command without params.
|
|
|
|
this->command(command->id().c_str());
|
|
|
|
}
|
2018-07-20 10:05:14 +08:00
|
|
|
}
|
|
|
|
|
2018-07-18 10:53:08 +08:00
|
|
|
std::string key_tooltip(const char* str, const app::Key* key)
|
2017-06-23 20:02:59 +08:00
|
|
|
{
|
|
|
|
std::string res;
|
|
|
|
if (str)
|
|
|
|
res += str;
|
|
|
|
if (key && !key->accels().empty()) {
|
|
|
|
res += " (";
|
|
|
|
res += key->accels().front().toString();
|
|
|
|
res += ")";
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2017-10-24 00:03:18 +08:00
|
|
|
std::string convertKeyContextToString(KeyContext keyContext)
|
|
|
|
{
|
|
|
|
switch (keyContext) {
|
|
|
|
case KeyContext::Any:
|
|
|
|
return std::string();
|
|
|
|
case KeyContext::Normal:
|
|
|
|
return "Normal";
|
|
|
|
case KeyContext::SelectionTool:
|
|
|
|
return "Selection";
|
|
|
|
case KeyContext::TranslatingSelection:
|
|
|
|
return "TranslatingSelection";
|
|
|
|
case KeyContext::ScalingSelection:
|
|
|
|
return "ScalingSelection";
|
|
|
|
case KeyContext::RotatingSelection:
|
|
|
|
return "RotatingSelection";
|
|
|
|
case KeyContext::MoveTool:
|
|
|
|
return "MoveTool";
|
|
|
|
case KeyContext::FreehandTool:
|
|
|
|
return "FreehandTool";
|
|
|
|
case KeyContext::ShapeTool:
|
|
|
|
return "ShapeTool";
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string convertKeyContextToUserFriendlyString(KeyContext keyContext)
|
|
|
|
{
|
|
|
|
switch (keyContext) {
|
|
|
|
case KeyContext::Any:
|
|
|
|
return std::string();
|
|
|
|
case KeyContext::Normal:
|
|
|
|
return "Normal";
|
|
|
|
case KeyContext::SelectionTool:
|
|
|
|
return "Selection";
|
|
|
|
case KeyContext::TranslatingSelection:
|
|
|
|
return "Translating Selection";
|
|
|
|
case KeyContext::ScalingSelection:
|
|
|
|
return "Scaling Selection";
|
|
|
|
case KeyContext::RotatingSelection:
|
|
|
|
return "Rotating Selection";
|
|
|
|
case KeyContext::MoveTool:
|
|
|
|
return "Move Tool";
|
|
|
|
case KeyContext::FreehandTool:
|
|
|
|
return "Freehand Tool";
|
|
|
|
case KeyContext::ShapeTool:
|
|
|
|
return "Shape Tool";
|
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2014-10-29 22:58:03 +08:00
|
|
|
} // namespace app
|