From c59f499b4cc22dcd02d338d3b742fdde9328ffd0 Mon Sep 17 00:00:00 2001 From: abakkk Date: Tue, 5 Mar 2019 21:08:43 +0100 Subject: [PATCH] add smooth feature --- draw.js | 32 ++++++++++++++++-- extension.js | 1 + locale/draw-on-your-screen.pot | 9 +++++ prefs.js | 15 ++++++++ schemas/gschemas.compiled | Bin 2744 -> 2884 bytes ...extensions.draw-on-your-screen.gschema.xml | 12 ++++++- 6 files changed, 66 insertions(+), 3 deletions(-) diff --git a/draw.js b/draw.js index f3581cf..7f33741 100644 --- a/draw.js +++ b/draw.js @@ -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; diff --git a/extension.js b/extension.js index b95021f..2712137 100644 --- a/extension.js +++ b/extension.js @@ -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), diff --git a/locale/draw-on-your-screen.pot b/locale/draw-on-your-screen.pot index 401e176..1f7af00 100644 --- a/locale/draw-on-your-screen.pot +++ b/locale/draw-on-your-screen.pot @@ -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 "" diff --git a/prefs.js b/prefs.js index e459618..21b40ca 100644 --- a/prefs.js +++ b/prefs.js @@ -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: "" + _("You can smooth the stroke afterward\nSee") + " \"" + _("Smooth last brushstroke") + "\"" }); + 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); diff --git a/schemas/gschemas.compiled b/schemas/gschemas.compiled index 0d32dc8523dc8cafa2fb12a52351259c8577b946..1300e090abc1e5c980c9380c79ad189490eee477 100644 GIT binary patch literal 2884 zcmZ`*U1%It6rN~eV`9?S)|j+WW37@1vzz~HAdN<2TdReHPzgSmOm^<>PBS~Rote#F zYX~YR_=EI8L$O#9qz{GUL5mMnQv?Y@p?%TjErJggYRQ8yK4|^E**iP4NgVk24&R(P z=iHw;_a1#sa}3S$6!|oO#|n1KeNrm}mv78>lld;D98=x{e_O676^|&&Jv@i2AtXF%gZxe&B$_<6zc|Rp+IyiVz$btg zX8*iFopufM3GgEDqSR?q{}{XiZ2t282>od@{wnw;FeLqH(|-fZNqb4^w5ivD+kn+K z7pl2_v@4(|z*E2x=}(*fv*0D5Zo~PPd1c{x`szz%i-QrmpP590M;) zoi_D0@G!7^;=)V@Sa0&b)u$nUJsngyA{TBE(5d1&1`TyJjpT*`jN5-F{KW)~t1YQO%PFzV* zr_K6T!8ZZ#%=IDaw5k6A{u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+r%z4)BA91}CoP%sHOsW6=4JJ~ zL=3Ny3D?Z&?$W4*$I_vDoxYRFSTYDa5fY5B$njq^HS42&703e^?K#waAGMvAKjl* z_e~ooUrwTmVD1a1;b*m+<5t!)p=adGsXP`fwhCccWpx>Af^_%aMsj6V>?4^)dEtcV|s;s;Cd>KPllmqm1}9$nR|0)qE^L7JItvBiha zYoW@#hGKcJPD9bSL#1)qSK9&hRl;44*)m{gMNK07@rpcM~^@|5H$dMKKr;K|GRx)M-`*~slu%9>M0*dg6$f6 z@rT28K-_Tjui?@>4Fvl*ZgkJrAh}VQUW^4krp%1h_HYnYUdJ)R!1~0tZ}3pkdd{?> z{gW<#9{6T5^EiM?XJS~dRB;8{a4=TrFDu`b<^IT2)=c}O%Xk8;pxU~{j-4o9aPEvb z3pUyi|Cq?@K3=sXKJ1dghg~xGuuDcCcF8Iqc1fJ@>P?*0Q*#;DLET)Uu + + false + smoothed stroke + smoothed stroke + ["<Alt><Super>d"] toggle drawing @@ -26,6 +31,11 @@ delete last element delete last element + + ["<Primary>equal"] + smooth last brushstroke + smooth last brushstroke + ["<Primary>b"] toggle background @@ -57,7 +67,7 @@ select text - ["<Primary>u"] + ["<Primary>p"] unselect shape (free drawing) unselect shape (free drawing)