-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.go
528 lines (470 loc) · 13.4 KB
/
view.go
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package image
import (
"bytes"
"compress/zlib"
"encoding/gob"
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"math"
"os"
"path/filepath"
"strings"
"github.com/go-gl/gl/v2.1/gl"
"github.com/gregjohnson2017/tabula-editor/pkg/comms"
"github.com/gregjohnson2017/tabula-editor/pkg/config"
"github.com/gregjohnson2017/tabula-editor/pkg/log"
"github.com/gregjohnson2017/tabula-editor/pkg/shaders"
"github.com/gregjohnson2017/tabula-editor/pkg/ui"
"github.com/gregjohnson2017/tabula-editor/pkg/util"
"github.com/kroppt/gfx"
"github.com/veandco/go-sdl2/sdl"
)
var _ ui.Component = ui.Component(&View{})
// View defines an interactable image viewing pane
type View struct {
cfg *config.Config
area sdl.Rect
canvas sdl.Rect
view sdl.FRect
mousePix sdl.Point
mult int32
activeTool Tool
layers []*Layer
selLayer *Layer
canvasLayer *Layer
dragLoc sdl.Point
panLoc sdl.Point
dragging bool
panning bool
bbComms chan<- comms.Image
toolComms <-chan Tool
checkerProg gfx.Program
program gfx.Program
projName string
}
func (iv *View) AddLayer(tex gfx.Texture) {
iv.layers = append(iv.layers, NewLayer(sdl.Point{X: 0, Y: 0}, tex))
}
// NewView returns a pointer to a new View struct that implements ui.Component
func NewView(area sdl.Rect, bbComms chan<- comms.Image, toolComms <-chan Tool, cfg *config.Config) (*View, error) {
var iv = &View{}
iv.cfg = cfg
iv.area = area
iv.view = ui.RectToFRect(area)
iv.bbComms = bbComms
iv.toolComms = toolComms
iv.mult = 0
iv.canvas = sdl.Rect{
X: -50,
Y: -50,
W: 100,
H: 100,
}
var data = make([]byte, iv.canvas.W*iv.canvas.H*4)
canvasTex, err := gfx.NewTexture(iv.canvas.W, iv.canvas.H, data, gl.RGBA, 4, 4)
if err != nil {
return nil, err
}
canvasTex.SetParameter(gl.TEXTURE_MIN_FILTER, gl.LINEAR_MIPMAP_NEAREST)
canvasTex.SetParameter(gl.TEXTURE_MAG_FILTER, gl.NEAREST)
iv.canvasLayer = NewLayer(sdl.Point{X: iv.canvas.X, Y: iv.canvas.Y}, canvasTex)
iv.layers = append(iv.layers, iv.canvasLayer)
v1, err := gfx.NewShader(shaders.VertexShaderSource, gl.VERTEX_SHADER)
if err != nil {
return nil, err
}
f1, err := gfx.NewShader(shaders.CheckerShaderFragment, gl.FRAGMENT_SHADER)
if err != nil {
return nil, err
}
if iv.checkerProg, err = gfx.NewProgram(v1, f1); err != nil {
return nil, err
}
f2, err := gfx.NewShader(shaders.FragmentShaderSource, gl.FRAGMENT_SHADER)
if err != nil {
return nil, err
}
if iv.program, err = gfx.NewProgram(v1, f2); err != nil {
return nil, err
}
err = iv.checkerProg.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
err = iv.program.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
iv.activeTool = &EmptyTool{}
iv.CenterCanvas()
iv.projName = "New Project"
return iv, nil
}
// Destroy frees all assets acquired by the ui.Component
func (iv *View) Destroy() {
iv.checkerProg.Destroy()
iv.program.Destroy()
for _, layer := range iv.layers {
layer.Destroy()
}
}
// InBoundary returns whether a point is in this ui.Component's bounds
func (iv *View) InBoundary(pt sdl.Point) bool {
return ui.InBounds(iv.area, pt)
}
// Render draws the ui.Component
func (iv *View) Render() {
sw := util.Start()
go func() {
iv.bbComms <- comms.Image{FileName: iv.projName, MousePix: iv.mousePix, Mult: iv.mult}
}()
// TODO selection outline
// gl viewport 0, 0 is bottom left
gl.Viewport(iv.area.X, iv.cfg.BottomBarHeight, iv.area.W, iv.area.H)
iv.program.Bind()
for _, layer := range iv.layers {
if layer == iv.canvasLayer {
iv.checkerProg.Bind()
iv.canvasLayer.Render(iv.view)
iv.program.Bind()
} else {
layer.Render(iv.view)
}
}
iv.program.Unbind()
select {
case tool := <-iv.toolComms:
log.Debugln("image.View switching tool to", tool.String())
iv.activeTool = tool
default:
}
sw.StopRecordAverage(iv.String() + ".Render")
}
// RenderCanvas draws what is on the canvas or area, whichever is larger
func (iv *View) RenderCanvas() {
sw := util.Start()
err := iv.program.UploadUniform("area", float32(iv.canvas.W), float32(iv.canvas.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
// gl viewport 0, 0 is bottom left
gl.Viewport(0, 0, iv.canvas.W, iv.canvas.H)
iv.program.Bind()
for _, layer := range iv.layers {
layer.Render(ui.RectToFRect(iv.canvas))
}
iv.program.Unbind()
iv.updateView()
sw.Stop("RenderCanvas")
}
const maxZoom = 8
// updateView updates the view rectangle according to the zoom multiplier,
// while maintaining the current pan
func (iv *View) updateView() {
frac := float32(math.Pow(2, float64(-iv.mult)))
newView := sdl.FRect{}
newView.W = float32(iv.area.W) * frac
newView.H = float32(iv.area.H) * frac
newView.X = (iv.view.W-newView.W)/2 + iv.view.X
newView.Y = (iv.view.H-newView.H)/2 + iv.view.Y
iv.view = newView
err := iv.checkerProg.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
err = iv.program.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
}
// CenterCanvas updates the view so the canvas is in the center of the window
func (iv *View) CenterCanvas() {
iv.view = sdl.FRect{
X: float32(iv.canvas.X) - (float32(iv.area.W)/2 - float32(iv.canvas.W)/2),
Y: float32(iv.canvas.Y) - (float32(iv.area.H)/2 - float32(iv.canvas.H)/2),
W: float32(iv.area.W),
H: float32(iv.area.H),
}
iv.updateView()
err := iv.checkerProg.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
err = iv.program.UploadUniform("area", float32(iv.view.W), float32(iv.view.H))
if err != nil {
log.Warnf("failed to upload uniform \"%v\": %v", "area", err)
}
}
// setPixel sets the currently hovered texel of the selected layer
// to the specified color
func (iv *View) setPixel(p sdl.Point, col color.RGBA) error {
if iv.selLayer != nil {
p.X -= iv.selLayer.area.X
p.Y -= iv.selLayer.area.Y
pt := gfx.Point{X: p.X, Y: p.Y}
bs := []byte{col.R, col.G, col.B, col.A}
return iv.selLayer.texture.SetPixel(pt, bs, true)
}
return nil
}
// x and y is in the SDL window coordinate space.
func (iv *View) updateMousePos(x, y int32) {
iv.mousePix = iv.getMousePix(x, y)
}
// x and y is in the SDL window coordinate space.
func (iv *View) getMousePix(x, y int32) sdl.Point {
return sdl.Point{
X: int32(math.Floor(float64(iv.view.X + float32(x)*iv.view.W/float32(iv.area.W)))),
Y: int32(math.Floor(float64(iv.view.Y + float32(y)*iv.view.H/float32(iv.area.H)))),
}
}
// OnEnter is called when the cursor enters the ui.Component's region
func (iv *View) OnEnter() {}
// OnLeave is called when the cursor leaves the ui.Component's region
func (iv *View) OnLeave() {
iv.dragging = false
}
// OnClick is called when the user clicks within the ui.Component's region
func (iv *View) OnClick(evt *sdl.MouseButtonEvent) bool {
iv.updateMousePos(evt.X, evt.Y)
iv.activeTool.OnClick(evt, iv)
iv.selectLayer()
if evt.Button == sdl.BUTTON_RIGHT {
if evt.State == sdl.PRESSED {
if iv.selLayer == nil {
// no layer was clicked on
return true
}
iv.dragging = true
} else if evt.State == sdl.RELEASED {
iv.dragging = false
}
iv.dragLoc.X = evt.X
iv.dragLoc.Y = evt.Y
} else if evt.Button == sdl.BUTTON_MIDDLE {
if evt.State == sdl.PRESSED {
iv.panning = true
} else if evt.State == sdl.RELEASED {
iv.panning = false
}
iv.panLoc.X = evt.X
iv.panLoc.Y = evt.Y
}
return true
}
// OnMotion is called when the cursor moves within the ui.Component's region
func (iv *View) OnMotion(evt *sdl.MouseMotionEvent) bool {
if !iv.dragging && !iv.panning {
iv.updateMousePos(evt.X, evt.Y)
iv.activeTool.OnMotion(evt, iv)
if iv.selLayer == nil {
return true
}
return ui.InBounds(iv.selLayer.area, sdl.Point{X: evt.X, Y: evt.Y})
}
if evt.State == sdl.ButtonRMask() {
// do not allow the canvas to be dragged
if iv.selLayer == nil || iv.selLayer == iv.canvasLayer {
return true
}
newImgPix := iv.getMousePix(evt.X, evt.Y)
oldImgPix := iv.getMousePix(iv.dragLoc.X, iv.dragLoc.Y)
diff := sdl.Point{
X: newImgPix.X - oldImgPix.X,
Y: newImgPix.Y - oldImgPix.Y,
}
iv.selLayer.area.X += diff.X
iv.selLayer.area.Y += diff.Y
iv.dragLoc.X = evt.X
iv.dragLoc.Y = evt.Y
} else if evt.State == sdl.ButtonMMask() {
if iv.panning {
iv.view.X += float32(iv.panLoc.X-evt.X) * float32(iv.view.W) / float32(iv.area.W)
iv.view.Y += float32(iv.panLoc.Y-evt.Y) * float32(iv.view.W) / float32(iv.area.W)
iv.panLoc.X = evt.X
iv.panLoc.Y = evt.Y
}
}
return true
}
// OnScroll is called when the user scrolls within the ui.Component's region
func (iv *View) OnScroll(evt *sdl.MouseWheelEvent) bool {
if iv.dragging {
return true
}
if evt.Y > 0 {
iv.mult++
if iv.mult > maxZoom {
iv.mult = maxZoom
}
iv.updateView()
} else if evt.Y < 0 {
iv.mult--
iv.updateView()
}
return true
}
// selectLayer sets the currently selected layer to nil, and sets the layer
// that the mouse is currently hovering over, if any.
func (iv *View) selectLayer() {
iv.selLayer = nil
for i := len(iv.layers) - 1; i >= 0; i-- {
layer := iv.layers[i]
if ui.InBounds(layer.area, iv.mousePix) {
iv.selLayer = layer
return
}
}
}
// ErrCoordOutOfRange indicates that given coordinates are out of range
const ErrCoordOutOfRange log.ConstErr = "coordinates out of range"
// SelectPixel adds the given x, y pixel to the
func (iv *View) SelectPixel(p sdl.Point) error {
if iv.selLayer == nil {
return nil
}
if !ui.InBounds(iv.selLayer.area, p) {
return nil
}
// TODO
return nil
}
// OnResize is called when the user resizes the window
func (iv *View) OnResize(x, y int32) {
iv.area.W += x
iv.area.H += y
iv.updateView()
}
// String returns the name of the component type
func (iv *View) String() string {
return "image.View"
}
// ErrWriteFormat indicates that an unsupported image format was trying to be written to
const ErrWriteFormat log.ConstErr = "unsupported image format"
// WriteToFile uses an OpenGL Frame Buffer Object to render the data in the canvas
// to a texture, and then write the data in that texture to the specified file
func (iv *View) WriteToFile(fileName string) error {
sw := util.Start()
// TODO after canvas figured out
w, h := iv.canvas.W, iv.canvas.H
fb, err := gfx.NewFrameBuffer(iv.canvas.W, iv.canvas.H)
if err != nil {
return err
}
fb.Bind()
iv.RenderCanvas()
fb.Unbind()
data := fb.GetTexture().GetData()
img := image.NewNRGBA(image.Rect(0, 0, int(w), int(h)))
// flip resulting data vertically
for j := 0; j < int(h)/2; j++ {
for i := 0; i < int(w)*4; i++ {
a := j*int(w)*4 + i
b := (int(h)-j-1)*int(w)*4 + i
data[a], data[b] = data[b], data[a]
}
}
copy(img.Pix, data)
out, err := os.Create(fileName)
if err != nil {
return err
}
switch ext := filepath.Ext(fileName); ext {
case ".png":
err = png.Encode(out, img)
if err != nil {
return err
}
case ".jpg", ".jpeg", ".jpe", ".jfif":
// TODO add dialog to choose quality
var opt jpeg.Options
opt.Quality = 100
err = jpeg.Encode(out, img, &opt)
if err != nil {
return err
}
default:
return fmt.Errorf("writing to file extension %v: %w", ext, ErrWriteFormat)
}
sw.Stop("WriteToFile")
return nil
}
type Project struct {
ProjName string
Mult int32
Canvas sdl.Rect
View sdl.FRect
Layers []*Layer
}
const ErrInvalidFormat log.ConstErr = "invalid project file (not .tabula)"
// SaveProject saves the relevant project data at the specified file location
// in a compressed format. The fileName must end with '.tabula'
func (iv *View) SaveProject(fileName string) error {
sw := util.Start()
var ext string
if ext = filepath.Ext(fileName); ext != ".tabula" {
return fmt.Errorf("%w: %v", ErrInvalidFormat, fileName)
}
out, err := os.Create(fileName)
if err != nil {
return err
}
defer out.Close()
proj := Project{
ProjName: strings.TrimSuffix(filepath.Base(fileName), ext),
Mult: iv.mult,
Canvas: iv.canvas,
View: iv.view,
Layers: iv.layers,
}
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
if err = enc.Encode(proj); err != nil {
return err
}
zw := zlib.NewWriter(out)
if _, err = zw.Write(buf.Bytes()); err != nil {
return err
}
defer zw.Close()
iv.projName = proj.ProjName
sw.Stop("SaveProject")
return nil
}
// LoadProject loads the project data at the specified file location,
// decompresses and decodes the data and populates the relevant fields in
// the image view. The fileName must end with '.tabula'
func (iv *View) LoadProject(fileName string) error {
sw := util.Start()
var err error
var in *os.File
if in, err = os.Open(fileName); err != nil {
return err
}
defer in.Close()
if ext := filepath.Ext(fileName); ext != ".tabula" {
return fmt.Errorf("%w: %v", ErrInvalidFormat, fileName)
}
zr, err := zlib.NewReader(in)
if err != nil {
return fmt.Errorf("zlib reader error: %w", err)
}
defer zr.Close()
var proj Project
dec := gob.NewDecoder(zr)
if err = dec.Decode(&proj); err != nil {
return fmt.Errorf("gob decoder error: %w", err)
}
iv.layers = proj.Layers
iv.canvasLayer = proj.Layers[0]
iv.mult = proj.Mult
iv.view = proj.View
iv.canvas = proj.Canvas
iv.projName = proj.ProjName
iv.updateView()
sw.Stop("LoadProject")
return nil
}