add smooth feature

This commit is contained in:
abakkk 2019-03-05 21:08:43 +01:00
parent 7f9a3195b4
commit c59f499b4c
6 changed files with 66 additions and 3 deletions

32
draw.js
View File

@ -60,6 +60,7 @@ var DrawingArea = new Lang.Class({
this.connect('style-changed', this._onStyleChanged.bind(this));
this.connect('repaint', this._repaint.bind(this));
this.settings = Convenience.getSettings();
this.emitter = new DrawingAreaEmitter();
this.helper = helper;
@ -217,6 +218,8 @@ var DrawingArea = new Lang.Class({
this._stopDrawing();
});
this.smoothedStroke = this.settings.get_boolean('smoothed-stroke');
this.currentElement = new DrawingElement ({
color: this.currentColor,
line: { lineWidth: this.currentLineWidth, lineJoin: this.currentLineJoin, lineCap: this.currentLineCap },
@ -269,7 +272,7 @@ var DrawingArea = new Lang.Class({
if (!this.currentElement)
return;
if (this.currentElement.shape == Shapes.NONE)
this.currentElement.points.push([x, y]);
this.currentElement.addPoint(x, y, this.smoothedStroke);
else
this.currentElement.points[1] = [x, y];
this._redisplay();
@ -336,6 +339,13 @@ var DrawingArea = new Lang.Class({
this._redisplay();
},
smoothLastElement: function() {
if (this.elements.length > 0 && this.elements[this.elements.length - 1].shape == Shapes.NONE) {
this.elements[this.elements.length - 1].smoothAll();
this._redisplay();
}
},
toggleBackground: function() {
this.hasBackground = !this.hasBackground;
this.get_parent().set_background_color(this.hasBackground ? this.activeBackgroundColor : null);
@ -598,7 +608,25 @@ var DrawingElement = new Lang.Class({
}
return row;
}
},
addPoint: function(x, y, smoothedStroke) {
this.points.push([x, y]);
if (smoothedStroke)
this.smooth(this.points.length - 1);
},
smooth: function(i) {
if (i < 2)
return;
this.points[i-1] = [(this.points[i-2][0] + this.points[i][0]) / 2, (this.points[i-2][1] + this.points[i][1]) / 2];
},
smoothAll: function() {
for (let i = 0; i < this.points.length; i++) {
this.smooth(i);
}
},
});
var HELPER_ANIMATION_TIME = 0.25;

View File

@ -117,6 +117,7 @@ var AreaManager = new Lang.Class({
'undo': this.activeArea.undo.bind(this.activeArea),
'redo': this.activeArea.redo.bind(this.activeArea),
'delete-last-element': this.activeArea.deleteLastElement.bind(this.activeArea),
'smooth-last-element': this.activeArea.smoothLastElement.bind(this.activeArea),
'save-as-svg': this.activeArea.save.bind(this.activeArea),
'toggle-background': this.activeArea.toggleBackground.bind(this.activeArea),
'increment-line-width': () => this.activeArea.incrementLineWidth(1),

View File

@ -70,6 +70,9 @@ msgstr ""
msgid "Erase last brushstroke"
msgstr ""
msgid "Smooth last brushstroke"
msgstr ""
msgid "Increment line width"
msgstr ""
@ -186,6 +189,12 @@ msgstr ""
msgid "(in drawing mode)"
msgstr ""
msgid "Smooth stroke during the drawing process"
msgstr ""
msgid "You can smooth the stroke afterward\nSee"
msgstr ""
msgid "Change the style"
msgstr ""

View File

@ -41,6 +41,7 @@ var INTERNAL_KEYBINDINGS = {
'undo': "Undo last brushstroke",
'redo': "Redo last brushstroke",
'delete-last-element' : "Erase last brushstroke",
'smooth-last-element': "Smooth last brushstroke",
'-separator-1': '',
'increment-line-width': "Increment line width",
'decrement-line-width': "Decrement line width",
@ -148,6 +149,20 @@ const PrefsPage = new GObject.Class({
listBox.add(new Gtk.Box({ margin_top: MARGIN, margin_left: MARGIN, margin_right: MARGIN }));
let smoothBox = new Gtk.Box({ margin: MARGIN });
let smoothLabelBox = new Gtk.Box({ orientation: Gtk.Orientation.VERTICAL });
let smoothLabel1 = new Gtk.Label({label: _("Smooth stroke during the drawing process")});
let smoothLabel2 = new Gtk.Label({ use_markup: true, halign: 1, label: "<small>" + _("You can smooth the stroke afterward\nSee") + " \"" + _("Smooth last brushstroke") + "\"</small>" });
smoothLabel1.set_halign(1);
smoothLabel2.get_style_context().add_class("dim-label");
smoothLabelBox.pack_start(smoothLabel1, true, true, 0);
smoothLabelBox.pack_start(smoothLabel2, true, true, 0);
let smoothSwitch = new Gtk.Switch({valign: 3});
this.settings.bind("smoothed-stroke", smoothSwitch, "active", 0);
smoothBox.pack_start(smoothLabelBox, true, true, 4);
smoothBox.pack_start(smoothSwitch, false, false, 4);
listBox.add(smoothBox);
let internalKeybindingsWidget = new KeybindingsWidget(INTERNAL_KEYBINDINGS, this.settings);
internalKeybindingsWidget.margin = MARGIN;
listBox.add(internalKeybindingsWidget);

Binary file not shown.

View File

@ -1,6 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<schemalist gettext-domain="gnome-shell-extensions">
<schema path="/org/gnome/shell/extensions/draw-on-your-screen/" id="org.gnome.shell.extensions.draw-on-your-screen">
<key type="b" name="smoothed-stroke">
<default>false</default>
<summary>smoothed stroke</summary>
<description>smoothed stroke</description>
</key>
<key type="as" name="toggle-drawing">
<default>["&lt;Alt&gt;&lt;Super&gt;d"]</default>
<summary>toggle drawing</summary>
@ -26,6 +31,11 @@
<summary>delete last element</summary>
<description>delete last element</description>
</key>
<key type="as" name="smooth-last-element">
<default>["&lt;Primary&gt;equal"]</default>
<summary>smooth last brushstroke</summary>
<description>smooth last brushstroke</description>
</key>
<key type="as" name="toggle-background">
<default>["&lt;Primary&gt;b"]</default>
<summary>toggle background</summary>
@ -57,7 +67,7 @@
<description>select text</description>
</key>
<key type="as" name="select-none-shape">
<default>["&lt;Primary&gt;u"]</default>
<default>["&lt;Primary&gt;p"]</default>
<summary>unselect shape (free drawing)</summary>
<description>unselect shape (free drawing)</description>
</key>