aboutsummaryrefslogtreecommitdiff
path: root/src/App.js
blob: 68473cec216aa712d03f28362feea64f5859d912 (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
import React, { Component } from 'react'
import './App.css'
import { rgb, xy, findContiguousPixels, getPixel } from './Utils'
import { setPixel, clear, noop, save, load } from './Actions'
import Palette from './Palette'
import PaintArea from './PaintArea'
import Toolkit from './Toolkit'
import ColorIndicator from './ColorIndicator'
import ConnectedIndicator from './ConnectedIndicator'
import LoadDialog from './LoadDialog'
import SaveDialog from './SaveDialog'

const tools = [
  { 
    name: "paint", 
    icon: "fas fa-pencil-alt", 
    action: function (x, y) {
      let { r, g, b } = rgb(this.state.selectedColor)
      setPixel(this._websocket, x, y, r, g, b)    
    }
  },
  { 
    name: "fill", 
    icon: "fab fa-bitbucket", 
    action: function (x, y) {
      let pixelsToColor = findContiguousPixels(x, y, this.state.pixels)
      pixelsToColor.forEach((coord) => {
        let px = { ...xy(coord), ...rgb(this.state.selectedColor)}
        setPixel(this._websocket, px.x, px.y, px.r, px.g, px.b)
      })
    }
  },
  { 
    name: "erase", 
    icon: "fas fa-eraser", 
    action: function (x, y) {
      setPixel(this._websocket, x, y, 0, 0, 0)    
    },
  },
  { 
    name: "pick", 
    icon: "fas fa-eye-dropper", 
    action: function (x, y) {
      let color = getPixel(x, y, this.state.pixels)
      this.setState({ selectedColor: color })
    },
  },
  // { 
  //   name: "lighten", 
  //   icon: "far fa-sun"
  // },
  // { 
  //   name: "darken", 
  //   icon: "fas fa-sun"
  // },
  { 
    name: "save", 
    icon: "fas fa-save", 
    onSelect: function () {
      this.setState({ showingSave: true })
    }
  },
  { 
    name: "load", 
    icon: "fas fa-save", 
    onSelect: function () {
      this.setState({ showingLoad: true })
    }
  },
  { 
    name: "trash", 
    icon: "fas fa-trash", 
    onSelect: function () {
      clear(this._websocket)
    }
  },
]

class App extends Component {
  constructor(props) {
    super(props)

    this.state = {
      connected: false,
      // Data from server
      pixels: [],
      saves: [],
      // Local data
      selectedColor: [0, 0, 0],
      selectedTool: tools[0],
      showingSave: false,
      showingLoad: false,
    }
    this._applyTool = this._applyTool.bind(this)
    this._selectTool = this._selectTool.bind(this)
    this._connectWebsocket = this._connectWebsocket.bind(this)
    this._onMessage = this._onMessage.bind(this)
    this._onOpen = this._onOpen.bind(this)
    this._onClose = this._onClose.bind(this)
    this._onError = this._onError.bind(this)
    this._loadDrawing = this._loadDrawing.bind(this)
    this._saveDrawing = this._saveDrawing.bind(this)
    this._connectWebsocket()
  }

  _onMessage({data}) {
    let state = JSON.parse(data)
    this.setState({
      ...state // Includes pixels and saves
    })
  }

  _onOpen() {
    this.setState({connected: true})
    noop(this._websocket)
  }

  _onClose() {
    this.setState({connected: false})
    this._connectWebsocket()
  }

  _onError() {
    this.setState({connected: false})
    this._connectWebsocket()
  }

  _connectWebsocket() {
    this._websocket = new WebSocket('ws://' + window.location.hostname + ':3001/ws')
    this._websocket.onmessage = this._onMessage
    this._websocket.onopen = this._onOpen
    this._websocket.onclose = this._onClose
    this._websocket.onerror = this._onError
  }

  _applyTool(x, y) {
    let tool = this.state.selectedTool
    if (!tool) {
      return
    }
    let action = tool.action
    if (!action) {
      return 
    }
    action.bind(this)(x, y)
  }

  _selectTool(tool) {
    let selectAction = tool.onSelect
    if (selectAction) {
      selectAction.bind(this)()
    } else {
      this.setState({selectedTool: tool})
    }
  }

  _loadDrawing(name) {
    load(this._websocket, name)
    this.setState({showingLoad: false})
  }

  _saveDrawing(name) {
    save(this._websocket, name)
    this.setState({showingSave: false})
  }

  render() {
    return (
      <div className="App">
        <ConnectedIndicator connected={this.state.connected}/>
        <Toolkit
          tools={tools}
          selectedTool={this.state.selectedTool}
          onSelectTool={this._selectTool}/>
        <PaintArea 
          data={this.state.pixels}
          onTool={this._applyTool}/>
        <Palette 
          selectedColor={this.state.selectedColor}
          onSelectColor={(color) => this.setState({selectedColor: color})} />
        <ColorIndicator color={this.state.selectedColor}/>
        <div>
        {
          this.state.showingLoad 
          && <LoadDialog 
            saves={this.state.saves}
            onLoad={(drawing) => this._loadDrawing(drawing)}
            onClose={() => this.setState({showingLoad: false})}/>
        }
        </div>
        <div>
        {
          this.state.showingSave
          && <SaveDialog 
            saves={this.state.saves}
            onSave={(name) => this._saveDrawing(name)}
            onClose={() => this.setState({showingSave: false})}/>
        }
        </div>

      </div>
    );
  }
}

export default App;