aboutsummaryrefslogtreecommitdiffhomepage
path: root/desktop/sources/scripts/terminal.js
blob: ccabebc04df492977194fbb8a3df90b45fc99706 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
'use strict'

function Terminal (pico) {
  const Cursor = require('./cursor')
  const QQQ = require('./qqq')

  this.qqq = new QQQ(this)
  this.cursor = new Cursor(this)
  this.controller = new Controller()
  this.theme = new Theme(this.theme = new Theme({ background: '#111111', f_high: '#ffffff', f_med: '#333333', f_low: '#000000', f_inv: '#000000', b_high: '#ffb545', b_med: '#72dec2', b_low: '#444444', b_inv: '#ffffff' }))

  this.pico = pico
  this.el = document.createElement('canvas')
  this.isPaused = false
  this.tile = { w: 12, h: 20 }
  this.debug = 'hello.'

  this.timer = null
  this.bpm = 120

  this.install = function (host) {
    this.resize()
    host.appendChild(this.el)
    this.theme.install(host)
  }

  this.start = function () {
    this.pico.terminal = this
    this.pico.start()
    this.theme.start()
    this.qqq.start()

    this.update()
    this.setSpeed(120)
  }

  this.setSpeed = function (bpm) {
    this.bpm = clamp(bpm, 60, 300)
    this.log(`Changed speed to ${this.bpm}.`)
    const ms = (60000 / bpm) / 4
    clearInterval(this.timer)
    this.timer = setInterval(() => { this.run() }, ms)
  }

  this.run = function () {
    if (this.isPaused) { return }

    this.qqq.clear()

    this.pico.run()
    this.qqq.run()
    this.update()
  }

  this.pause = function () {
    this.isPaused = !this.isPaused
    this.log(this.isPaused ? 'Paused' : 'Unpaused')
  }

  this.load = function (path) {
    const terminal = this
    var fs = require('fs')
    fs.readFile(path, 'utf8', function (err, data) {
      if (err) throw err
      const w = data.split('\n')[0].length
      const h = data.split('\n').length
      terminal.log(`Loaded: ${path} ${w}x${h}`)
      pico.load(w, h, data)
      terminal.resize()
      terminal.update()
    })
  }

  this.log = function (msg) {
    console.info(msg)
    this.debug = msg
    this.update()
  }

  this.update = function () {
    this.clear()
    this.draw_program()
    this.draw_output(2)
    this.draw_debug(1)
    this.draw_inspector(0)
  }

  this.new = function () {
    pico.clear()
  }

  this.draw_program = function () {
    const ports = this.find_ports()
    const terminal = this

    let y = 0
    while (y < pico.h) {
      let x = 0
      while (x < pico.w) {
        const styles = {
          isSelection: terminal.isSelection(x, y),
          isCursor: terminal.isCursor(x, y),
          is_port: ports[`${x}:${y}`]
        }
        this.draw_sprite(x, y, pico.glyphAt(x, y), styles)
        x += 1
      }
      y += 1
    }
  }

  this.draw_output = function (offset) {
    const s = pico.r.replace(/\./g, ' ').trim()

    let x = 0
    while (x < s.length) {
      const c = s.substr(x, 1)
      this.draw_sprite(x, pico.h + offset, c)
      x += 1
    }
  }

  this.draw_debug = function (offset) {
    const s = this.debug.trim()
    let x = 0
    while (x < s.length) {
      const c = s.substr(x, 1)
      this.draw_sprite(x, pico.h + offset, c)
      x += 1
    }
  }

  this.draw_inspector = function (offset) {
    const s = this.cursor.inspect()
    let x = 0
    while (x < s.length) {
      const c = s.substr(x, 1)
      this.draw_sprite(x, pico.h + offset, c)
      x += 1
    }
  }

  this.isCursor = function (x, y) {
    return x === this.cursor.x && y === this.cursor.y
  }

  this.isSelection = function (x, y) {
    if (x >= this.cursor.x && x < this.cursor.x + this.cursor.w && y >= this.cursor.y && y < this.cursor.y + this.cursor.h) {
      return true
    }
    return false
  }

  this.find_ports = function () {
    const h = {}
    const fns = pico.findFns()
    for (const id in fns) {
      const g = fns[id]
      if (pico.isLocked(g.x, g.y)) { continue }
      for (const id in g.ports) {
        const port = g.ports[id]
        h[`${g.x + port.x}:${g.y + port.y}`] = port.output ? 2 : port.bang ? 1 : 3
      }
    }

    return h
  }

  this.context = function () {
    return this.el.getContext('2d')
  }

  this.clear = function () {
    const ctx = this.context()
    ctx.clearRect(0, 0, this.size.width, this.size.height)
  }

  this.draw_sprite = function (x, y, g, styles = { isCursor: false, isSelection: false, is_port: false }) {
    const ctx = this.context()

    ctx.textBaseline = 'bottom'
    ctx.textAlign = 'center'
    ctx.font = `${this.tile.h * 0.75}px input_mono_regular`

    if (styles.isSelection) {
      ctx.fillStyle = this.theme.active.b_inv
      ctx.fillRect(x * this.tile.w, (y) * this.tile.h, this.tile.w, this.tile.h)
      ctx.fillStyle = this.theme.active.f_inv
    } else if (styles.is_port) {
      if (styles.is_port == 2) {
        ctx.fillStyle = this.theme.active.b_high
        ctx.fillRect(x * this.tile.w, (y) * this.tile.h, this.tile.w, this.tile.h)
        ctx.fillStyle = this.theme.active.f_low
      } else if (styles.is_port == 1) {
        ctx.fillStyle = this.theme.active.b_med
        ctx.fillRect(x * this.tile.w, (y) * this.tile.h, this.tile.w, this.tile.h)
        ctx.fillStyle = this.theme.active.f_med
      } else if (styles.is_port == 3) {
        ctx.fillStyle = this.theme.active.b_low
        ctx.fillRect(x * this.tile.w, (y) * this.tile.h, this.tile.w, this.tile.h)
        ctx.fillStyle = this.theme.active.f_high
      }
    } else {
      ctx.fillStyle = 'white'
    }
    ctx.fillText(styles.isCursor && g == '.' ? (!pico.isPaused ? '@' : '~') : g.toUpperCase(), (x + 0.5) * this.tile.w, (y + 1) * this.tile.h)
  }

  this.resize = function () {
    this.size = { width: this.tile.w * pico.w, height: this.tile.h * pico.h + (this.tile.h * 3), ratio: 0.75 }
    this.el.width = this.size.width
    this.el.height = this.size.height + this.tile.h
    this.el.style.width = (this.size.width * this.size.ratio) + 'px'
    this.el.style.height = (this.size.height * this.size.ratio) + 'px'

    let { remote } = require('electron')
    let win = remote.getCurrentWindow()

    win.setSize((this.size.width * this.size.ratio) + 60, (this.size.height * this.size.ratio) + 30, true)
  }

  window.onresize = (event) => {
    const marginTop = (window.innerHeight - (this.size.height * this.size.ratio)) / 2
    this.el.style.marginTop = (marginTop - 20) + 'px'
  }

  function clamp (v, min, max) { return v < min ? min : v > max ? max : v }
}

module.exports = Terminal