summaryrefslogtreecommitdiff
path: root/js/ui/brush.js
blob: 90422b7b69f8ab6e77968b9386c37daf9ef07d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var brush = (function(){
  
  var brush = new Matrix (5, 5, function(x,y){
    var lex = new Lex (x,y)
    lex.build()
    return lex
  })

  brush.modified = false
  
  brush.mask = blit.circle
  
  brush.generate = function(){
    brush.fill(brush)
    brush.mask(brush)
  }

  brush.bind = function(){

    var last_point = [0,0]
    var dragging = false
    var erasing = false

    brush.forEach(function(lex, x, y){

      if (lex.bound) return
      lex.bound = true
      
      var point = [x,y]
      lex.span.addEventListener('contextmenu', function(e){
        e.preventDefault()
      })
      lex.span.addEventListener('mousedown', function(e){
        e.preventDefault()
        current_canvas = brush
        brush.modified = true
        dragging = true
        erasing = (e.which == "3" || e.ctrlKey)
        if (erasing) {
          lex.clear()
        }
        else {
          fillColor = brush.bg
          lex.fill(brush)
        }
        brush.focus(x, y)
        // lex.focus()
      })
      lex.span.addEventListener('mousemove', function(e){
        e.preventDefault()
        if (! dragging) {
          return
        }
        erasing = (e.which == "3" || e.ctrlKey)
        if (erasing) {
          lex.clear()
        }
        else {
          lex.fill(brush)
        }
        brush.focus(x, y)
        //lex.focus()
      })
    })
    window.addEventListener('mouseup', function(e){
      dragging = erasing = false
    })
  }

  brush.resize = function(w, h, min, max){
    this.w = clamp(w, this.min, this.max)
    this.h = clamp(h, this.min, this.max)
    // brush.__proto__.resize.call(brush, w, h)
    // this.w = w
    // this.h = h
    this.rebuild()
    //brush.__proto__.rebuild.call(brush, w, h)
    controls.brush_w.char = ""+this.w
    controls.brush_w.build()
    controls.brush_h.char = ""+this.h
    controls.brush_h.build()
  }
  brush.size_add = function(w, h){
    brush.resize(brush.w + w, brush.h + h)
  }

  brush.expand = function(i){
    brush.size_add(i, i)
    // var w = this.w = clamp(this.w+i, 1, 9), h = this.h = clamp(this.h+i, 1, 9)
    // controls.width.char = ""+w
    // controls.width.build()
    // controls.height.char = ""+h
    // controls.height.build()
    // this.rebuild()
  }
  brush.contract = function(i){
    brush.size_add(-i, -i)
    // this.expand(-i)
  }
  
  brush.load = function(lex){
    brush.char = lex.char
    brush.fg = lex.fg
    brush.bg = lex.bg
    brush.opacity = 1
  }

  brush.min = 1
  brush.max = 100

  brush.char = " "
  brush.fg = 0
  brush.bg = 1
  brush.opacity = 1
  
  brush.draw_fg = true
  brush.draw_bg = true
  brush.draw_char = true
  
  return brush

})()