| 1 | // Aseprite |
|---|---|
| 2 | // Copyright (C) 2001-2017 David Capello |
| 3 | // |
| 4 | // This program is distributed under the terms of |
| 5 | // the End-User License Agreement for Aseprite. |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | #include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include "app/commands/command.h" |
| 12 | #include "app/commands/params.h" |
| 13 | #include "app/i18n/strings.h" |
| 14 | #include "app/ui/color_bar.h" |
| 15 | #include "fmt/format.h" |
| 16 | |
| 17 | namespace app { |
| 18 | |
| 19 | class PaletteEditorCommand : public Command { |
| 20 | public: |
| 21 | PaletteEditorCommand(); |
| 22 | |
| 23 | protected: |
| 24 | void onLoadParams(const Params& params) override; |
| 25 | bool onChecked(Context* context) override; |
| 26 | void onExecute(Context* context) override; |
| 27 | std::string onGetFriendlyName() const override; |
| 28 | |
| 29 | private: |
| 30 | bool m_edit; |
| 31 | bool m_popup; |
| 32 | bool m_background; |
| 33 | }; |
| 34 | |
| 35 | PaletteEditorCommand::PaletteEditorCommand() |
| 36 | : Command(CommandId::PaletteEditor(), CmdRecordableFlag) |
| 37 | { |
| 38 | m_edit = true; |
| 39 | m_popup = false; |
| 40 | m_background = false; |
| 41 | } |
| 42 | |
| 43 | void PaletteEditorCommand::onLoadParams(const Params& params) |
| 44 | { |
| 45 | m_edit = |
| 46 | (params.empty() || |
| 47 | params.get("edit") == "switch"|| |
| 48 | params.get_as<bool>("switch")); // "switch" for backward compatibility |
| 49 | m_popup = (!params.get("popup").empty()); |
| 50 | m_background = (params.get("popup") == "background"); |
| 51 | } |
| 52 | |
| 53 | bool PaletteEditorCommand::onChecked(Context* context) |
| 54 | { |
| 55 | return ColorBar::instance()->inEditMode(); |
| 56 | } |
| 57 | |
| 58 | void PaletteEditorCommand::onExecute(Context* context) |
| 59 | { |
| 60 | auto colorBar = ColorBar::instance(); |
| 61 | bool editMode = colorBar->inEditMode(); |
| 62 | ColorButton* button = |
| 63 | (m_background ? |
| 64 | colorBar->bgColorButton(): |
| 65 | colorBar->fgColorButton()); |
| 66 | |
| 67 | // Switch edit mode |
| 68 | if (m_edit && !m_popup) { |
| 69 | colorBar->setEditMode(!editMode); |
| 70 | } |
| 71 | // Switch popup |
| 72 | else if (!m_edit && m_popup) { |
| 73 | if (button->isPopupVisible()) |
| 74 | button->closePopup(); |
| 75 | else |
| 76 | button->openPopup(true); |
| 77 | } |
| 78 | // Switch both (the popup visibility is used to switch both states) |
| 79 | else if (m_edit && m_popup) { |
| 80 | if (button->isPopupVisible()) { |
| 81 | colorBar->setEditMode(false); |
| 82 | button->closePopup(); |
| 83 | } |
| 84 | else { |
| 85 | colorBar->setEditMode(true); |
| 86 | button->openPopup(true); |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | std::string PaletteEditorCommand::onGetFriendlyName() const |
| 92 | { |
| 93 | std::string edit, plus, popup; |
| 94 | if (m_edit) { |
| 95 | edit = Strings::commands_PaletteEditor_Edit(); |
| 96 | } |
| 97 | if (m_edit && m_popup) { |
| 98 | plus = Strings::commands_PaletteEditor_And(); |
| 99 | } |
| 100 | if (m_popup) { |
| 101 | if (m_background) |
| 102 | popup = Strings::commands_PaletteEditor_BgPopup(); |
| 103 | else |
| 104 | popup = Strings::commands_PaletteEditor_FgPopup(); |
| 105 | } |
| 106 | return fmt::format(getBaseFriendlyName(), edit, plus, popup); |
| 107 | } |
| 108 | |
| 109 | Command* CommandFactory::createPaletteEditorCommand() |
| 110 | { |
| 111 | return new PaletteEditorCommand; |
| 112 | } |
| 113 | |
| 114 | } // namespace app |
| 115 |