diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..2a88165 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,18 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch file", + "type": "go", + "request": "launch", + "mode": "debug", + "program": "rhemawave.go", + "env": { + "DEBUGINFOD_URLS": "" + } + }, + ] +} \ No newline at end of file diff --git a/internal/page/audioDevices.go b/internal/page/audioDevices.go index 7b4afde..1fa117e 100644 --- a/internal/page/audioDevices.go +++ b/internal/page/audioDevices.go @@ -20,9 +20,8 @@ type AudioDevices struct { title StringItem prompt StringItem AudioStatusToggle StringItem - highlightIndex int - selectedIndex int verticalMargin int32 + ItemScale float32 CfgPtr *db.AppConfig } @@ -59,37 +58,26 @@ func (p *AudioDevices) Init(ctx *PageCtx) error { // Calculate scales titleScale := float32(64) / float32(baseFontSize) - itemScale := float32(32) / float32(baseFontSize) + p.ItemScale = float32(32) / float32(baseFontSize) // Create title - p.title, err = NewStringItem("Select an Audio Output Device", p.font, colors.White) + p.title, err = NewStringItem("Toggle Audio", p.font, colors.White) if err != nil { return fmt.Errorf("creating title: %w", err) } p.title.Scale(titleScale) // Create prompt - p.prompt, err = NewStringItem("Use Arrow Keys to select, Enter to confirm", p.font, colors.White) + p.prompt, err = NewStringItem("Switch On/Off with Enter. Setting saves immediately.", p.font, colors.White) if err != nil { return fmt.Errorf("creating prompt: %w", err) } - p.prompt.Scale(itemScale) + p.prompt.Scale(p.ItemScale) - on := p.Context.CfgPtr.AudioEnabled - - audioStatus := "Turn Audio On" - if on { - audioStatus = "Turn Audio Off" + if err = p.CreateAudioStatusItem(); err != nil { + logger.Error("failed to create AudioStatusItem in AudioDevices.Init()") } - audioStatusStringItem, err := NewStringItem(audioStatus, p.font, colors.White) - if err != nil { - return fmt.Errorf("failed to create Turn Audio Off string item: %w", err) - } - - // Scale the new option - audioStatusStringItem.Scale(itemScale) - p.layout() return nil } @@ -106,20 +94,40 @@ func (p *AudioDevices) CreateAudioStatusItem() error { if err != nil { return fmt.Errorf("failed to create '%s' string item: %v", statusString, err) } + fmt.Printf("AudioStatusToggle (%d)", audioStatusStringItem.ID) + audioStatusStringItem.Scale(p.ItemScale) p.AudioStatusToggle = audioStatusStringItem + + // Position the audio toggle + p.AudioStatusToggle.X = p.ScreenCenterX - p.AudioStatusToggle.W/2 // Horizontal Position + p.AudioStatusToggle.Y = p.ScreenCenterY + (p.AudioStatusToggle.H / 2) // Vertical Position return nil } func (p *AudioDevices) layout() { - // Position title at bottom + // Position title at the top p.title.X = p.ScreenCenterX - p.title.W/2 p.title.Y = p.ScreenHeight - p.title.H - p.verticalMargin*2 - // Position prompt above title + // Position prompt below the title p.prompt.X = p.ScreenCenterX - p.prompt.W/2 p.prompt.Y = p.title.Y - p.verticalMargin*2 - p.prompt.H } +func (p *AudioDevices) ToggleAudio() { + // Toggle current setting + p.Context.CfgPtr.AudioEnabled = !p.Context.CfgPtr.AudioEnabled + + // Update the database + p.Context.DbPtr.Save(p.Context.CfgPtr) + + // Update the configuration + p.Context.RefreshConfig() + + // Update AudioEnabled UI element + p.CreateAudioStatusItem() +} + func (p *AudioDevices) HandleEvent(event *sdl.Event) error { e, ok := (*event).(*sdl.KeyboardEvent) if !ok || e.Type != sdl.KEYDOWN { @@ -129,14 +137,7 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error { switch e.Keysym.Sym { case sdl.K_RETURN: - // Toggle AudioEnabled - p.Context.CfgPtr.AudioEnabled = !p.Context.CfgPtr.AudioEnabled - // Save the config to the database - p.Context.DbPtr.Save(p.Context.CfgPtr) - // Refresh the config throughout the application - p.Context.RefreshConfig() - // Update AudioEnabled UI element - + p.ToggleAudio() case sdl.K_BACKSPACE: p.Context.NavigateTo(SETTINGS) } @@ -145,19 +146,21 @@ func (p *AudioDevices) HandleEvent(event *sdl.Event) error { } func (p *AudioDevices) Render() error { - // Always render title and prompt if err := p.RenderTexture(p.textShader, p.title.ID, p.title.Position(), p.title.RawDimensions(), p.title.Color); err != nil { logger.Error("unable to render title texture: %v", err) return fmt.Errorf("unable to render title texture: %w", err) } + if err := p.RenderTexture(p.textShader, p.prompt.ID, p.prompt.Position(), p.prompt.RawDimensions(), p.prompt.Color); err != nil { logger.Error("unable to render prompt texture: %v", err) return fmt.Errorf("unable to render prompt texture: %w", err) } + if err := p.RenderTexture(p.textShader, p.AudioStatusToggle.ID, p.AudioStatusToggle.Position(), p.AudioStatusToggle.RawDimensions(), p.AudioStatusToggle.Color); err != nil { logger.Error("unable to render audioStatusToggle texture: %v", err) return fmt.Errorf("unable to render audioStatusToggle texture: %w", err) } + return nil }