1 // Aseprite
2 // Copyright (C) 2001-2018  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/app.h"
12 #include "app/commands/command.h"
13 #include "app/context.h"
14 #include "app/context_access.h"
15 #include "app/doc.h"
16 #include "app/find_widget.h"
17 #include "app/load_widget.h"
18 #include "app/modules/editors.h"
19 #include "app/pref/preferences.h"
20 #include "app/ui/status_bar.h"
21 #include "app/ui_context.h"
22 #include "doc/document.h"
23 #include "doc/mask.h"
24 #include "ui/window.h"
25 
26 #include "grid_settings.xml.h"
27 
28 namespace app {
29 
30 using namespace ui;
31 using namespace gfx;
32 
33 class SnapToGridCommand : public Command {
34 public:
SnapToGridCommand()35   SnapToGridCommand()
36     : Command(CommandId::SnapToGrid(), CmdUIOnlyFlag) {
37   }
38 
clone() const39   Command* clone() const override { return new SnapToGridCommand(*this); }
40 
41 protected:
onChecked(Context * ctx)42   bool onChecked(Context* ctx) override {
43     DocumentPreferences& docPref = Preferences::instance().document(ctx->activeDocument());
44     return docPref.grid.snap();
45   }
46 
onExecute(Context * ctx)47   void onExecute(Context* ctx) override {
48     DocumentPreferences& docPref = Preferences::instance().document(ctx->activeDocument());
49     bool newValue = !docPref.grid.snap();
50     docPref.grid.snap(newValue);
51 
52     StatusBar::instance()->showSnapToGridWarning(newValue);
53   }
54 };
55 
56 class SelectionAsGridCommand : public Command {
57 public:
SelectionAsGridCommand()58   SelectionAsGridCommand()
59     : Command(CommandId::SelectionAsGrid(), CmdUIOnlyFlag) {
60   }
61 
clone() const62   Command* clone() const override { return new SelectionAsGridCommand(*this); }
63 
64 protected:
onEnabled(Context * ctx)65   bool onEnabled(Context* ctx) override {
66     return (ctx->activeDocument() &&
67             ctx->activeDocument()->isMaskVisible());
68   }
69 
onExecute(Context * ctx)70   void onExecute(Context* ctx) override {
71     const ContextReader reader(ctx);
72     const Doc* document = reader.document();
73     const Mask* mask(document->mask());
74     DocumentPreferences& docPref =
75       Preferences::instance().document(ctx->activeDocument());
76 
77     docPref.grid.bounds(mask->bounds());
78 
79     // Make grid visible
80     if (!docPref.show.grid())
81       docPref.show.grid(true);
82   }
83 };
84 
85 class GridSettingsCommand : public Command {
86 public:
87   GridSettingsCommand();
clone() const88   Command* clone() const override { return new GridSettingsCommand(*this); }
89 
90 protected:
91   bool onEnabled(Context* context) override;
92   void onExecute(Context* context) override;
93 };
94 
GridSettingsCommand()95 GridSettingsCommand::GridSettingsCommand()
96   : Command(CommandId::GridSettings(), CmdUIOnlyFlag)
97 {
98 }
99 
onEnabled(Context * context)100 bool GridSettingsCommand::onEnabled(Context* context)
101 {
102   return true;
103 }
104 
onExecute(Context * context)105 void GridSettingsCommand::onExecute(Context* context)
106 {
107   gen::GridSettings window;
108 
109   DocumentPreferences& docPref = Preferences::instance().document(context->activeDocument());
110   Rect bounds = docPref.grid.bounds();
111 
112   window.gridX()->setTextf("%d", bounds.x);
113   window.gridY()->setTextf("%d", bounds.y);
114   window.gridW()->setTextf("%d", bounds.w);
115   window.gridH()->setTextf("%d", bounds.h);
116   window.openWindowInForeground();
117 
118   if (window.closer() == window.ok()) {
119     bounds.x = window.gridX()->textInt();
120     bounds.y = window.gridY()->textInt();
121     bounds.w = window.gridW()->textInt();
122     bounds.h = window.gridH()->textInt();
123     bounds.w = MAX(bounds.w, 1);
124     bounds.h = MAX(bounds.h, 1);
125 
126     docPref.grid.bounds(bounds);
127 
128     // Make grid visible
129     if (!docPref.show.grid())
130       docPref.show.grid(true);
131   }
132 }
133 
createSnapToGridCommand()134 Command* CommandFactory::createSnapToGridCommand()
135 {
136   return new SnapToGridCommand;
137 }
138 
createGridSettingsCommand()139 Command* CommandFactory::createGridSettingsCommand()
140 {
141   return new GridSettingsCommand;
142 }
143 
createSelectionAsGridCommand()144 Command* CommandFactory::createSelectionAsGridCommand()
145 {
146   return new SelectionAsGridCommand;
147 }
148 
149 } // namespace app
150