A assets/icons/tools/ellipse.png => assets/icons/tools/ellipse.png +0 -0
M src/vector.nim => src/vector.nim +3 -0
@@ 41,6 41,9 @@ func `>`*(u, v: Vec): bool =
func `>=`*(u, v: Vec): bool =
u.x >= v.x and u.y >= v.y
+func abs*(u: Vec): Vec =
+ (abs(u.x), abs(u.y))
+
func square*(n: int32): Vec =
(n, n)
M src/vier.nim => src/vier.nim +48 -5
@@ 9,7 9,7 @@ import pkg/spryvm/spryvm
import pkg/spryvm/sprycore
import std/bitops
import std/cmdline
-import std/math
+import std/math except E
import std/os
import std/sequtils
import std/sets
@@ 29,8 29,9 @@ type
tFlood = "Flood"
tRect = "Rect"
tSegment = "Segment"
+ tEllipse = "Ellipse"
- RectMode = enum
+ ShapeMode = enum
rFilled
rOutline
@@ 40,8 41,8 @@ type
brushSize: int32 # ignored for now
of tFlood:
tolerance: int32
- of tRect:
- rectMode: RectMode # ignored for now
+ of tRect, tEllipse:
+ shapeMode: ShapeMode # ignored for now
of tSegment:
discard
@@ 114,7 115,8 @@ proc loadIcons() =
tBrush: loadTextureStatic("icons/tools/brush.png"),
tFlood: loadTextureStatic("icons/tools/flood.png"),
tRect: loadTextureStatic("icons/tools/rectangle.png"),
- tSegment: loadTextureStatic("icons/tools/segment.png")
+ tSegment: loadTextureStatic("icons/tools/segment.png"),
+ tEllipse: loadTextureStatic("icons/tools/ellipse.png")
]
proc loadTexture(picture: Picture) =
@@ 245,6 247,43 @@ proc lineSegment(start, `end`: Vec, includeStart: bool): seq[Vec] =
point.y += signY
result.add(point)
+proc ellipse(a, b: Vec): seq[Vec] =
+ # Algorithm taken from http://members.chello.at/easyfilter/bresenham.pdf
+ var
+ a = a
+ b = b
+ if a.x > b.x:
+ swap(a.x, b.x)
+ if a.y > b.y:
+ swap(a.y, b.y)
+ let
+ diameter = b - a
+ parity = diameter.y and 1
+ errorIncrementIncrement: Vec = (8 * diameter.y ^ 2, 8 * diameter.x ^ 2)
+ var
+ errorIncrement: Vec =
+ (4 * (1 - diameter.x) * diameter.y ^ 2, 4 * (1 + parity) * diameter.x ^ 2)
+ error = errorIncrement.x + errorIncrement.y + parity * diameter.x ^ 2
+ a.y += (diameter.y + 1) div 2
+ b.y = a.y - parity
+ while a.x <= b.x:
+ result.add([(a.x, a.y), (b.x, a.y), (a.x, b.y), (b.x, b.y)])
+ let doubleError = 2 * error
+ if doubleError <= errorIncrement.y:
+ a.y.inc()
+ b.y.dec()
+ errorIncrement.y += errorIncrementIncrement.y
+ error += errorIncrement.y
+ if doubleError >= errorIncrement.x or 2 * error > errorIncrement.y:
+ a.x.inc()
+ b.x.dec()
+ errorIncrement.x += errorIncrementIncrement.x
+ error += errorIncrement.x
+ while a.y - b.y <= diameter.y:
+ result.add([(a.x - 1, a.y), (b.x + 1, a.y), (a.x - 1, b.y), (b.x + 1, b.y)])
+ a.y.inc()
+ b.y.dec()
+
proc select(picture: Picture, target: Vec, tool: Tool) =
case tool.kind
of tBrush:
@@ 285,6 324,8 @@ proc select(picture: Picture, target: Vec, tool: Tool) =
(x, y)
of tSegment:
picture.selection = lineSegment(picture.anchor, target, includeStart = true)
+ of tEllipse:
+ picture.selection = ellipse(picture.anchor, target)
proc injectColor(picture: Picture, color: Color) =
## Changes all selected pixels to the given color.
@@ 662,6 703,8 @@ proc processKeyboard(app: App) =
)
picture.add(change)
picture.apply(change)
+ if isKeyPressed(E):
+ app.tool = Tool(kind: tEllipse)
if isKeyPressed(F):
app.tool = Tool(kind: tFlood)
if isKeyPressed(I):
A website/assets/icons/tools/ellipse.png => website/assets/icons/tools/ellipse.png +0 -0
M website/index.html => website/index.html +3 -2
@@ 29,7 29,7 @@ nimble <span class="token function">install</span>
<p>Vier has these modes:</p>
<dl><dt><img src="assets/icons/modes/inject.png" alt="Inject mode icon" /> Inject mode</dt><dd>In this mode, the user can move around the selected picture using <kbd>H</kbd><kbd>J</kbd><kbd>K</kbd><kbd>L</kbd> keys and draw with the currently selected tool. The color of the pixels being drawn is replaced by the selected color, so this can also work as an “eraser” if the selected color is transparent. If multiple pictures are opened, they can be switched between using <kbd>Ctrl</kbd>-<kbd>H</kbd> and <kbd>Ctrl</kbd>-<kbd>L</kbd>.</dd><dt><img src="assets/icons/modes/add.png" alt="Add mode icon" /> Add mode</dt><dd>In this mode, the user can move around the selected picture using <kbd>H</kbd><kbd>J</kbd><kbd>K</kbd><kbd>L</kbd> keys and draw with the currently selected tool. The selected color is added on the color of the pixels being drawn, which allows shading an area with a transparent color. If multiple pictures are opened, they can be switched between using <kbd>Ctrl</kbd>-<kbd>H</kbd> and <kbd>Ctrl</kbd>-<kbd>L</kbd>.</dd><dt><img src="assets/icons/modes/select.png" alt="Select mode icon" /> Select mode</dt><dd>In this mode, the user can move around the selected picture using <kbd>H</kbd><kbd>J</kbd><kbd>K</kbd><kbd>L</kbd> keys and select a part of the picture using the currently selected tool. The selection is currently useless; copy-paste functionality will be implemented later. If multiple pictures are opened, they can be switched between using <kbd>Ctrl</kbd>-<kbd>H</kbd> and <kbd>Ctrl</kbd>-<kbd>L</kbd>.</dd><dt><img src="assets/icons/modes/color.png" alt="Color mode icon" /> Color mode</dt><dd>In this mode, the user can move around a color palette (displayed at the top of the screen) to change the selected color (displayed in the upper left corner). If multiple palettes are available, they can be switched between using <kbd>Ctrl</kbd>-<kbd>H</kbd> and <kbd>Ctrl</kbd>-<kbd>L</kbd>.</dd><dt><img src="assets/icons/modes/command.png" alt="Command mode icon" /> Command mode</dt><dd>In this mode, the user can enter a command, displayed at the bottom of the screen, to perform complex actions such as opening a file. The commands are in the <a href="https://sprylang.se/">Spry</a> programming language, though this is not important for basic usage. The command is executed by pressing <kbd>⏎</kbd> or cancelled by pressing <kbd>Esc</kbd>.</dd></dl>
<p>The following tools are available:</p>
-<dl><dt><img src="assets/icons/tools/brush.png" alt="Brush icon" /> Brush</dt><dd>The brush tool selects pixels that it moves over.</dd><dt><img src="assets/icons/tools/segment.png" alt="Segment icon" /> Segment</dt><dd>The segment tool selects a line segment.</dd><dt><img src="assets/icons/tools/rectangle.png" alt="Rectangle icon" /> Rectangle</dt><dd>The rectangle tool selects an axis-aligned rectangle.</dd><dt><img src="assets/icons/tools/flood.png" alt="Flood icon" /> Flood</dt><dd>The flood tool selects a contiguous area of pixels with the same color.</dd></dl></section>
+<dl><dt><img src="assets/icons/tools/brush.png" alt="Brush icon" /> Brush</dt><dd>The brush tool selects pixels that it moves over.</dd><dt><img src="assets/icons/tools/segment.png" alt="Segment icon" /> Segment</dt><dd>The segment tool selects a line segment.</dd><dt><img src="assets/icons/tools/rectangle.png" alt="Rectangle icon" /> Rectangle</dt><dd>The rectangle tool selects an axis-aligned rectangle.</dd><dt><img src="assets/icons/tools/ellipse.png" alt="Ellipse icon" /> Rectangle</dt><dd>The rectangle tool selects the outline of an axis-aligned ellipse.</dd><dt><img src="assets/icons/tools/flood.png" alt="Flood icon" /> Flood</dt><dd>The flood tool selects a contiguous area of pixels with the same color.</dd></dl></section>
<section><h2 class="xd-section-heading">Keyboard mappings</h2><p>The key mappings are inspired by Vim. They are currently not customizable.</p>
<table><tr><th>Key</th><th>Alone</th><th>Shift</th><th>Control</th></tr>
<tr><td><kbd>⎵</kbd></td><td>hold to draw</td><td>hold to draw with secondary color</td></tr>
@@ 37,6 37,7 @@ nimble <span class="token function">install</span>
<tr><td><kbd>B</kbd></td><td>Tool ← Brush</td></tr>
<tr><td><kbd>C</kbd></td><td>Mode ← Color</td></tr>
<tr><td><kbd>D</kbd></td><td>delete pixel</td></tr>
+<tr><td><kbd>E</kbd></td><td>Tool ← Ellipse</td></tr>
<tr><td><kbd>F</kbd></td><td>Tool ← Flood</td></tr>
<tr><td><kbd>H</kbd></td><td>move left</td><td></td><td>previous picture/palette</td></tr>
<tr><td><kbd>I</kbd></td><td>Mode ← Inject</td></tr>
@@ 65,6 66,6 @@ Note that the mouse can also be used to draw and select colors. The right mouse
<section><h2 class="xd-section-heading">Commands</h2><dl><dt><code>e image.png</code></dt><dd>open a file for editing (the filename is a symbol)</dd><dt><code>edit "image.png"</code></dt><dd>open a file for editing (the filename is a string)</dd><dt><code>flip-x</code></dt><dd>flip the current picture horizontally</dd><dt><code>flip-y</code></dt><dd>flip the current picture vertically</dd><dt><code>new</code></dt><dd>create a new picture with a transparent background and the default size</dd><dt><code>resize 16 8</code></dt><dd>resize the current picture, anchoring at the northwest corner</dd><dt><code>resize-nn 16 8</code></dt><dd>resize the current picture using the nearest-neighbor algorithm</dd><dt><code>w image.png</code></dt><dd>save the current picture under the specified filename (the filename is a symbol)</dd><dt><code>write "image.png"</code></dt><dd>save the current picture under the specified filename (the filename is a string)</dd></dl>
<p>Note that currently, edits done via commands are not added to undo history and break it.</p></section>
<section><h2 class="xd-section-heading">Planned features</h2><p>Roughly in order of priority.</p>
-<ul><li>ellipse tool</li><li>upscaling (various algorithms)</li><li>better editing support and tab completion for the command line</li><li>mirrored drawing</li><li>color picker, palette editing</li><li>variations of tools (wide brush, fill with tolerance)</li><li>programmable configuration</li><li>gradients</li><li>animated sprite editing</li></ul></section>
+<ul><li>upscaling (various algorithms)</li><li>variants of tools</li><li>better editing support and tab completion for the command line</li><li>mirrored drawing</li><li>color picker, palette editing</li><li>variations of tools (wide brush, fill with tolerance)</li><li>programmable configuration</li><li>gradients</li><li>animated sprite editing</li></ul></section>
<section><h2 class="xd-section-heading">Name</h2><p>I partially created Vier as a semestral project for a Computer Graphics course at my university. The name of the lecturer can be loosely translated as “fear”, so I decided to name the project after a German word which is pronounced the same way. The word means “four”, which hints at the editor treating images as a square grid (with squares having four sides, obviously). I also came up with the backronym “Vim-Inspired Editor of Rasters”.</p></section>
</body></html>=
\ No newline at end of file
M website/index.xd => website/index.xd +3 -2
@@ 63,6 63,7 @@
; [link-image Brush icon; assets/icons/tools/brush.png] Brush; The brush tool selects pixels that it moves over.
; [link-image Segment icon; assets/icons/tools/segment.png] Segment; The segment tool selects a line segment.
; [link-image Rectangle icon; assets/icons/tools/rectangle.png] Rectangle; The rectangle tool selects an axis-aligned rectangle.
+ ; [link-image Ellipse icon; assets/icons/tools/ellipse.png] Ellipse; The ellipse tool selects the outline of an axis-aligned ellipse.
; [link-image Flood icon; assets/icons/tools/flood.png] Flood; The flood tool selects a contiguous area of pixels with the same color.
]
]
@@ 75,7 76,7 @@
[row [<kbd> B]; Tool ← Brush]
[row [<kbd> C]; Mode ← Color]
[row [<kbd> D]; delete pixel]
- [# row [<kbd> E]; Tool ← Ellipse]
+ [row [<kbd> E]; Tool ← Ellipse]
[row [<kbd> F]; Tool ← Flood]
[row [<kbd> H]; move left; [# move to left edge]; previous picture/palette]
[row [<kbd> I]; Mode ← Inject]
@@ 127,8 128,8 @@
[section Planned features;
[p Roughly in order of priority.]
[list
- ; ellipse tool
; upscaling (various algorithms)
+ ; variants of tools
; better editing support and tab completion for the command line
; mirrored drawing
; color picker, palette editing