Skip to content

Commit 8cf8bbf

Browse files
committed
Merge pull request #58 from STRd6/brush-options
brush options
2 parents 244276b + 92a2ef4 commit 8cf8bbf

File tree

11 files changed

+163
-45
lines changed

11 files changed

+163
-45
lines changed

actions.coffee.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Actions
188188
image: blob
189189
state: JSON.stringify(editor.saveState())
190190
, "*"
191-
191+
192192
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAC4klEQVQ4T32T70tTURjHv8fpppuaQkuhlgU2f4wCs6b4QpxLod9BJSaYEOS7+gOiF/VCYvjKepf0IsFfU6wxUSNFiALJ9NWi7AelbmbX2qZzv9zdvT3nSOAMei6Xe++55/mc7/N9zmGgGBsb06Wnp19QVfVaMpkspaEjynZ4aOwLPZ8kEomppqamJJ+/Mxgll2s0mv6CgoJjhYWFMBgM0Ov1oESsr68jFAphcXERkiS9prFmgvhSABMTE9NlZWV1JpMJjLHdC4hvWZbh8XiwsLDQ09zc3JYCGB8fl2w2m1Gr1f4XEAgEMDk5udbS0rJvdwkCEAwGkZmZCZ1Oh4yMDFFCJBKB3++H1+tFcXExpqam1lpbW1MBo6OjUn19vTEcDot6Y7GYSOayuQfxeBxkMMxms1DQ1taWCnC73QLAJ/JknsgTHjz3I0cHRLZk5GdrsSJFwdKAbL0GisoQ2Iji5exSFXO5XJLdbjdyudFoVAC4H/cHf+KsrQSXjmfDPePF+eoDKQY/nV7D9NtvYCMjI1JDQ4Nxc3NT1MwB3Ic7vT9grynFjbo83H40h4e3KgUgJgNbtBsej/nw/vMy2PDwsNTY2ChM5ADaSAJwb+gXTlWVoKU2F4yuNOqwSgBFUalbgGPoO+Y/EMDpdAoAd5sDaNchKysLDlcAJyyH4PsdEslyUoFCN4dwk/mLb2UFbGBgQLJarUYKrK6uCh84oOOZHxXlJjKLNNNsWU4KOFegqAp9J6i9BOjt7T1DP5wWi8VQVFQk5PMdeb1zHvaTJbhSmwVZ2SIItYAvzBRkpmvR2beEWc8nKo6iu7v7MLXuLoEu07nYw89Cn6cQp6uO4mJtAt2z7dhrOMidwFp4Ge3WLnT1xzE9924bsDMcDkcOlVD8Klg5f/NcORor/JgJDCJPu1+ICMYkVOdfRUdPEi9m5v4F/IVVtE+8MZv0NXm6fJKcS2UkwMgDppIXLIKPS18hbSTwB3tLeq03+hLeAAAAAElFTkSuQmCC"
193193

194194

brushes.coffee.md

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,62 @@
11
Brushes
22
=======
33

4+
require "./util"
5+
46
A brush takes a point and returns a list of points.
57

8+
tiny = """
9+
1
10+
"""
11+
12+
small = """
13+
010
14+
111
15+
010
16+
"""
17+
18+
big = """
19+
01110
20+
11111
21+
11111
22+
11111
23+
01110
24+
"""
25+
26+
bigger = """
27+
0011100
28+
0111110
29+
1111111
30+
1111111
31+
1111111
32+
0111110
33+
0011100
34+
"""
35+
36+
sizes = [tiny, small, big, bigger].map (data) ->
37+
rows = data.split('\n')
38+
39+
offset = Math.floor rows[0].length / 2
40+
41+
points = rows.reduce (array, row, y) ->
42+
y -= offset
43+
row.split('').forEach (c, x) ->
44+
x -= offset
45+
if c is '1'
46+
array.push Point(x, y)
47+
48+
return array
49+
, []
50+
51+
(point) ->
52+
points.map (brushPoint) ->
53+
brushPoint.add(point)
54+
55+
# TODO: use these arrays as sizes
56+
657
module.exports =
7-
pencil: (point) ->
8-
[point]
9-
10-
brush: ({x, y}) ->
11-
[
12-
Point x, y - 1
13-
Point x - 1, y
14-
Point x, y
15-
Point x + 1, y
16-
Point x, y + 1
17-
]
58+
pencil: sizes[0]
59+
60+
brush: sizes[1]
61+
62+
sizes: sizes

editor.coffee.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,10 @@ Editor
466466
467467
initialState = self.getSnapshot()
468468
469+
document.addEventListener "mousedown", (e) ->
470+
return if e.path.some (node) ->
471+
node.classList?.contains "panel"
472+
473+
self.detailTool null
474+
469475
return self

presenters/tool.coffee

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Observable = require "observable"
2+
3+
module.exports = (editor, tool) ->
4+
self =
5+
activate: ->
6+
editor.activeTool(tool)
7+
activeClass: ->
8+
"active" if tool is editor.activeTool()
9+
detail: ->
10+
editor.detailTool(tool)
11+
detailClass: ->
12+
"panelOpen" if tool is editor.detailTool()
13+
14+
style: ->
15+
"background-image: url(#{tool.iconUrl})"
16+
17+
title: ->
18+
tool.hotkeys
19+
20+
settings: tool.settings

shims.coffee

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
# Event#path polyfill
3+
# NOTE: In Chrome this includes `window` but here we only get up to `document`
4+
unless "path" of Event.prototype
5+
Object.defineProperty Event.prototype, "path",
6+
get: ->
7+
path = []
8+
node = @target
9+
while node
10+
path.push node
11+
node = node.parentNode
12+
13+
return path

style.styl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,28 @@ body
125125
border-left: 1px solid rgba(255, 255, 255, 0.5)
126126
cursor: pointer
127127

128+
& > .panel
129+
background-color: #eee
130+
border-bottom: 1px solid rgba(0, 0, 0, 0.5)
131+
border-right: 1px solid rgba(0, 0, 0, 0.5)
132+
border-top: 1px solid rgba(255, 255, 255, 0.5)
133+
display: none
134+
margin-top: -1px
135+
margin-left: 35px
136+
padding: 3px
137+
padding-bottom: 4px
138+
position: absolute
139+
z-index: 10
140+
141+
&.panelOpen
142+
border-right: 1px solid #eee
143+
144+
&:empty
145+
border-right: 1px solid rgba(0, 0, 0, 0.5)
146+
147+
& > .panel
148+
display: inline-block
149+
128150
&:last-child
129151
border-bottom-right-radius: 2px
130152

templates/editor.haml.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Editor template
66
- editor = this
77
- Symmetry = require "../symmetry"
88
- Palette = require "./palette"
9+
- Tool = require "./tool"
10+
- ToolPresenter = require "../presenters/tool"
911

1012
.editor
1113

@@ -14,9 +16,7 @@ The toolbar holds our tools.
1416
.toolbar
1517
.tools
1618
- @tools.each (tool) ->
17-
- activeClass = -> "active" if tool is activeTool()
18-
- activate = -> activeTool(tool)
19-
.tool(style="background-image: url(#{tool.iconUrl})" title=tool.hotkeys class=activeClass click=activate)
19+
= Tool ToolPresenter(editor, tool)
2020
%h2 Symmetry
2121
.tools
2222
- symmetryMode = @symmetryMode

templates/tool.jadelet

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- ToolPanel = require "./tool_panel"
2+
3+
.tool(style=@style title=@title class=@activeClass class=@detailClass click=@activate dblclick=@detail)
4+
- if @settings
5+
= ToolPanel(@settings)

templates/tool_panel.jadelet

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.panel
2+
- if size = @size
3+
input(type=size.type min=size.min max=size.max value=size.value)

tools.coffee.md

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ Tools
44
Brushes = require "./brushes"
55
{circle, line, rect, rectOutline, endDeltoid} = require "./util"
66
7-
line2 = (start, end, fn) ->
8-
fn start
9-
line start, end, fn
10-
117
neighbors = (point) ->
128
[
139
{x: point.x, y: point.y-1}
@@ -38,45 +34,50 @@ Tools
3834
editor.restore()
3935
fn(editor, editor.canvas, start, end)
4036
41-
brushTool = (brushName, hotkey, offsetX, offsetY, icon, options) ->
37+
sizedTool = (hotkey, offsetX, offsetY, icon, options) ->
4238
previousPosition = null
43-
brush = Brushes[brushName]
4439
4540
OP = (out) ->
4641
(p) ->
4742
out(p, options)
4843
4944
paint = (out) ->
50-
(x, y) ->
51-
brush({x, y}).forEach OP out
45+
(p) ->
46+
brush = Brushes.sizes[self.settings.size.value()]
5247
53-
hotkeys: hotkey
54-
iconUrl: icon
55-
iconOffset:
56-
x: offsetX
57-
y: offsetY
58-
touch: ({position, editor})->
59-
paint(editor.draw) position.x, position.y
60-
previousPosition = position
61-
move: ({editor, position})->
62-
line previousPosition, position, paint(editor.draw)
63-
previousPosition = position
64-
release: ->
65-
previousPosition = null
48+
brush(p).forEach OP out
49+
50+
self =
51+
hotkeys: hotkey
52+
iconUrl: icon
53+
iconOffset:
54+
x: offsetX
55+
y: offsetY
56+
touch: ({position, editor})->
57+
paint(editor.draw) position
58+
previousPosition = position
59+
move: ({editor, position})->
60+
line previousPosition, position, paint(editor.draw)
61+
previousPosition = position
62+
release: ->
63+
previousPosition = null
64+
settings:
65+
size:
66+
type: 'range'
67+
min: 0
68+
max: 3
69+
value: Observable 0
6670
6771
Default tools.
6872

6973
TOOLS =
7074
7175
Draw a line when moving while touching.
7276

73-
pencil: brushTool "pencil", "p", 4, 14,
77+
pencil: sizedTool "p", 4, 14,
7478
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAA5klEQVQ4T5VTuw2DMBB9LmkZg54ZGCDpHYkJYBBYATcUSKnSwAy0iDFoKR0fDgiMDc5JLvy59969OzPchzSesP3+sLFgySoMweMYou/xmWe81VKx5d0CyCQBoghoGgiV/JombwDNzjkwjsAw/A8gswwgBWm6VPdU7L4laPa6BsrSyX6oxTBQ7munO1v9LgCv2ldCWxcWgDV4EDjZbQq0dDKv65ytuxokKdtWO08AagkhTr2/BiD2otBv8hyMurCbPHNaTQ8OBjJScZFs9eChTKMwB8byT5ajkwIC8E22AvyY7j7ZJugLVIZ5EV8R1SQAAAAASUVORK5CYII="
7579
76-
brush: brushTool "brush", "b", 4, 14,
77-
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAAKBJREFUeJytkrsRgzAQRFeME6UXXwVUogKoRB2JmAagEEqBcB0ge/Dw0cm2ZpTd7tuTFqg/zBcA0NSKkwg6719G1WJSlUnkI4XZgCGQql+tQKoCbYt+WWrB2SDGA92aYKMD/6dbEjCJAPP8A73wbe5OnAuDYV1LsyfkEMgYi4W5ciW56Zxzt/THBR2YJmAcbXn34s77d+dh6Ps+2tlw8eGedfBU8rnbDOMAAAAASUVORK5CYII="
78-
79-
eraser: brushTool "pencil", "e", 4, 11,
80+
eraser: sizedTool "e", 4, 11,
8081
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAAIdJREFUeJzNUsERwCAIw15n031wDt0Hl0s/9VoF9NnmZzRBCERfI2zusdOtDABmopRGVoRCrdviADNMiADM6L873Mql2NYiw3E2WItzVi2dSuw8JBHNvQyegcU4vmjNFesWZrHFTSlYQ/RhRDgatKZFnXPy7zMIoVaYa3fH5i3PTHira4r/gQv1W1E4p9FksQAAAABJRU5ErkJggg==",
8182
color: "transparent"
8283
@@ -194,14 +195,13 @@ Shapes
194195
circle start, end, (x, y) ->
195196
editor.draw({x, y})
196197
197-
line2: shapeTool "l", 0, 0,
198+
line: shapeTool "l", 0, 0,
198199
"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAV0lEQVQ4T6XSyQ0AIAgEQOm/aIWHxoNzJTG+GASk9hnE+Z2P3FDMRBjZK0PI/fQyovVeQqzhpRFv+ikkWl+IRID8DRfJAC6SBUykAqhIFXgQBDgQFFjIAMAADxGQlO+iAAAAAElFTkSuQmCC"
199200
(editor, canvas, start, end) ->
200201
color = editor.activeColor()
201202
202203
# Have to draw our own lines if we want them crisp ;_;
203-
line start, end, (x, y) ->
204-
editor.draw {x, y}
204+
line start, end, editor.draw
205205
206206
module.exports = (I={}, self=Core(I)) ->
207207
self.extend
@@ -213,6 +213,7 @@ Shapes
213213
self.tools.push tool
214214
215215
activeTool: Observable()
216+
detailTool: Observable()
216217
previousTool: Observable()
217218
218219
tools: Observable []

util.coffee.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Util
22
====
33

44
require "extensions"
5+
require "./shims"
56
67
global.Bindable = require "bindable"
78
global.Matrix = require "matrix"
@@ -77,7 +78,9 @@ Call an iterator for each integer point on a line between two integer points.
7778
err += dx
7879
y0 += sy
7980
80-
iterator x0, y0
81+
iterator
82+
x: x0
83+
y: y0
8184
8285
rect: (start, end, iterator) ->
8386
[start.y..end.y].forEach (y) ->

0 commit comments

Comments
 (0)