|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | +"embed" |
| 5 | +"fmt" |
| 6 | +"image/color" |
| 7 | +"log" |
| 8 | +"time" |
| 9 | + |
| 10 | +"github.com/hajimehoshi/ebiten/v2" |
| 11 | +"github.com/hajimehoshi/ebiten/v2/audio" |
| 12 | +"github.com/hajimehoshi/ebiten/v2/ebitenutil" |
| 13 | +"github.com/hajimehoshi/ebiten/v2/inpututil" |
| 14 | +"github.com/hajimehoshi/ebiten/v2/text" |
| 15 | +"golang.org/x/image/font" |
| 16 | +"golang.org/x/image/font/opentype" |
| 17 | +) |
| 18 | + |
| 19 | +// Controller or System |
| 20 | +type Controller interface { |
| 21 | +Update(duration time.Duration) |
| 22 | +Draw(screen *ebiten.Image) |
| 23 | +} |
| 24 | + |
| 25 | +type Component interface { |
| 26 | +Update(duration time.Duration) |
| 27 | +} |
| 28 | + |
| 29 | +type Screen struct { |
| 30 | +time time.Time |
| 31 | +startTime time.Time |
| 32 | + |
| 33 | +controllers []Controller |
| 34 | + |
| 35 | +config *Config |
| 36 | + |
| 37 | +audioPlayer *audio.Player |
| 38 | +font font.Face |
| 39 | +} |
| 40 | + |
| 41 | +var ( |
| 42 | +//go:embed files |
| 43 | +fs embed.FS |
| 44 | +) |
| 45 | + |
| 46 | +func (s *Screen) Update() error { |
| 47 | +if time.Now().After(s.time) { |
| 48 | +s.time = s.time.Add(s.config.Interval) |
| 49 | +} |
| 50 | + |
| 51 | +duration := time.Now().Sub(s.time).Round(time.Second).Abs() |
| 52 | +for _, controller := range s.controllers { |
| 53 | +controller.Update(duration) |
| 54 | +} |
| 55 | + |
| 56 | +if inpututil.IsKeyJustPressed(ebiten.KeyBackspace) { |
| 57 | +s.time = time.Now() |
| 58 | +s.startTime = s.time |
| 59 | +} |
| 60 | + |
| 61 | +if isPressed(ebiten.KeyLeft) { |
| 62 | +s.time = s.time.Add(-time.Second) |
| 63 | +} |
| 64 | +if isPressed(ebiten.KeyRight) { |
| 65 | +s.time = s.time.Add(time.Second) |
| 66 | +} |
| 67 | +return nil |
| 68 | +} |
| 69 | + |
| 70 | +func isPressed(key ebiten.Key) bool { |
| 71 | +if ebiten.IsKeyPressed(ebiten.KeyShift) { |
| 72 | +return ebiten.IsKeyPressed(key) |
| 73 | +} |
| 74 | +return inpututil.IsKeyJustPressed(key) |
| 75 | +} |
| 76 | + |
| 77 | +func (s *Screen) Draw(screen *ebiten.Image) { |
| 78 | +text.Draw(screen, fmt.Sprintf("Time left: %s", FormatTime(s.time)), s.font, 5, 25, color.White) |
| 79 | +ebitenutil.DebugPrintAt(screen, "Backspace: Reset time", 5, 45) |
| 80 | +ebitenutil.DebugPrintAt(screen, "<-: Remove one second", 5, 60) |
| 81 | +ebitenutil.DebugPrintAt(screen, "->: Add one second", 5, 75) |
| 82 | +ebitenutil.DebugPrintAt(screen, "^: Increase volume", 5, 90) |
| 83 | +ebitenutil.DebugPrintAt(screen, "v: Lower volume", 5, 105) |
| 84 | +ebitenutil.DebugPrintAt(screen, "M: Mute", 5, 120) |
| 85 | +ebitenutil.DebugPrintAt(screen, "Shift: Increase action", 5, 135) |
| 86 | +ebitenutil.DebugPrintAt(screen, fmt.Sprintf("Total Time: %s", FormatTime(s.startTime)), 5, 150) |
| 87 | + |
| 88 | +for _, controller := range s.controllers { |
| 89 | +controller.Draw(screen) |
| 90 | +} |
| 91 | + |
| 92 | +} |
| 93 | + |
| 94 | +func FormatTime(t time.Time) string { |
| 95 | +seconds := time.Now().Sub(t).Round(time.Second).Abs() |
| 96 | + |
| 97 | +minute := seconds / time.Minute |
| 98 | +seconds -= minute * time.Minute |
| 99 | +seconds = seconds / time.Second |
| 100 | + |
| 101 | +return fmt.Sprintf("%02d:%02d", minute, seconds) |
| 102 | +} |
| 103 | + |
| 104 | +func (s *Screen) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { |
| 105 | +return outsideWidth, outsideHeight |
| 106 | +} |
| 107 | + |
| 108 | +func main() { |
| 109 | +config := &Config{ |
| 110 | +FirstAlarm: MustParseDuration(FirstAlarm), |
| 111 | +SecondAlarm: MustParseDuration(SecondAlarm), |
| 112 | +Interval: MustParseDuration(Interval), |
| 113 | +Volume: 0.1, |
| 114 | +} |
| 115 | + |
| 116 | +err := config.Validate() |
| 117 | +if err != nil { |
| 118 | +log.Fatalf("failed to validate config: %s", err) |
| 119 | +} |
| 120 | + |
| 121 | +font, err := loadTextFont() |
| 122 | +if err != nil { |
| 123 | +log.Fatalf("failed to load font: %s", err) |
| 124 | +} |
| 125 | + |
| 126 | +firstAlert, err := NewAlert(config.FirstAlarm, "files/quack.mp3") |
| 127 | +if err != nil { |
| 128 | +log.Fatalf("failed to load alert: %s", err) |
| 129 | +} |
| 130 | + |
| 131 | +secondAlert, err := NewAlert(config.SecondAlarm, "files/goat.mp3") |
| 132 | +if err != nil { |
| 133 | +log.Fatalf("failed to load alert: %s", err) |
| 134 | +} |
| 135 | + |
| 136 | +sounds := NewSoundController(config.Volume) |
| 137 | +sounds.Add(firstAlert) |
| 138 | +sounds.Add(secondAlert) |
| 139 | + |
| 140 | +s := &Screen{ |
| 141 | +config: config, |
| 142 | +time: time.Now().Add(config.Interval), |
| 143 | +startTime: time.Now(), |
| 144 | + |
| 145 | +controllers: []Controller{sounds}, |
| 146 | +font: font, |
| 147 | +} |
| 148 | + |
| 149 | +ebiten.SetWindowSize(200, 180) |
| 150 | +ebiten.SetWindowTitle(WindowTitle) |
| 151 | +ebiten.SetWindowFloating(true) |
| 152 | + |
| 153 | +if err := ebiten.RunGame(s); err != nil { |
| 154 | +log.Fatal(err) |
| 155 | +} |
| 156 | + |
| 157 | +} |
| 158 | + |
| 159 | +func loadTextFont() (font.Face, error) { |
| 160 | +ttfFile, err := fs.ReadFile("files/tahoma.ttf") |
| 161 | +if err != nil { |
| 162 | +return nil, fmt.Errorf("failed to open file: %w", err) |
| 163 | +} |
| 164 | +tt, err := opentype.Parse(ttfFile) |
| 165 | +if err != nil { |
| 166 | +return nil, fmt.Errorf("failed to parse font: %w", err) |
| 167 | +} |
| 168 | + |
| 169 | +tahoma, err := opentype.NewFace(tt, &opentype.FaceOptions{ |
| 170 | +Size: 26, |
| 171 | +DPI: 72, |
| 172 | +Hinting: font.HintingVertical, |
| 173 | +}) |
| 174 | +if err != nil { |
| 175 | +return nil, fmt.Errorf("failed to create new font face: %w", err) |
| 176 | +} |
| 177 | +// Adjust the line height. |
| 178 | +//mplusBigFont = text.FaceWithLineHeight(mplusBigFont, 54) |
| 179 | + |
| 180 | +return tahoma, nil |
| 181 | +} |
0 commit comments