|
| 1 | +// Copyright 2025 Jacek Olszak |
| 2 | +// This code is licensed under MIT license (see LICENSE for details) |
| 3 | + |
| 4 | +package internal |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/elgopher/pi" |
| 8 | + "github.com/elgopher/pi/pidebug" |
| 9 | + "github.com/elgopher/pi/pigui" |
| 10 | +) |
| 11 | + |
| 12 | +func attachToolbar(parent *pigui.Element) *pigui.Element { |
| 13 | + toolbar := pigui.Attach(parent, 0, pi.Screen().H()-9, pi.Screen().W(), 9) |
| 14 | + toolbar.OnDraw = func(event pigui.DrawEvent) { |
| 15 | + prev := pi.SetColor(*bgColor) |
| 16 | + defer pi.SetColor(prev) |
| 17 | + pi.RectFill(0, 0, toolbar.W, toolbar.H) |
| 18 | + } |
| 19 | + |
| 20 | + // attachIconButton(toolbar, icons.AlignTop, 0) // icon hidden until implemented |
| 21 | + // attachIconButton(toolbar, icons.Screen, 8) // icon hidden for now because screen inspector is the only tab |
| 22 | + // attachIconButton(toolbar, icons.Palette, 16) // icon hidden until implemented |
| 23 | + // attachIconButton(toolbar, icons.Variables, 24) // icon hidden until implemented |
| 24 | + // attachIconButton(toolbar, icons.Paint, 32) // icon hidden until implemented |
| 25 | + |
| 26 | + snap := attachIconButton(toolbar, icons.Snap, pi.Screen().W()-34) |
| 27 | + snap.OnTapped = func(event pigui.Event) { |
| 28 | + captureSnapshot() |
| 29 | + } |
| 30 | + |
| 31 | + prev := attachIconButton(toolbar, icons.Prev, pi.Screen().W()-24) |
| 32 | + prev.OnTapped = func(event pigui.Event) { |
| 33 | + showPrevSnapshot() |
| 34 | + } |
| 35 | + prev.OnUpdate = func(pigui.UpdateEvent) { |
| 36 | + if theScreenRecorder.HasPrev() { |
| 37 | + prev.Icon = icons.Prev |
| 38 | + } else { |
| 39 | + prev.Icon = pi.Sprite{} |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + playPause := attachIconButton(toolbar, icons.Pause, pi.Screen().W()-19) |
| 44 | + playPause.OnTapped = func(pigui.Event) { |
| 45 | + pauseOrResume() |
| 46 | + } |
| 47 | + playPause.OnUpdate = func(pigui.UpdateEvent) { |
| 48 | + if pidebug.Paused() { |
| 49 | + playPause.Icon = icons.Pause |
| 50 | + } else { |
| 51 | + playPause.Icon = icons.Play |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + next := attachIconButton(toolbar, icons.Next, pi.Screen().W()-14) |
| 56 | + next.OnTapped = func(event pigui.Event) { |
| 57 | + showNextSnapshot() |
| 58 | + } |
| 59 | + |
| 60 | + exit := attachIconButton(toolbar, icons.Exit, pi.Screen().W()-8) |
| 61 | + exit.OnTapped = func(event pigui.Event) { |
| 62 | + exitConsoleMode() |
| 63 | + } |
| 64 | + |
| 65 | + return toolbar |
| 66 | +} |
| 67 | + |
| 68 | +type IconButton struct { |
| 69 | + *pigui.Element |
| 70 | + |
| 71 | + Icon pi.Sprite |
| 72 | +} |
| 73 | + |
| 74 | +func attachIconButton(parent *pigui.Element, icon pi.Sprite, x int) *IconButton { |
| 75 | + btn := pigui.Attach(parent, x, 0, icon.W, icon.H+1) |
| 76 | + iconBtn := &IconButton{Icon: icon, Element: btn} |
| 77 | + btn.OnDraw = func(event pigui.DrawEvent) { |
| 78 | + y := 0 |
| 79 | + if event.Pressed { |
| 80 | + y = 1 |
| 81 | + } |
| 82 | + pi.Spr(iconBtn.Icon, 0, y) |
| 83 | + } |
| 84 | + return iconBtn |
| 85 | +} |
0 commit comments