gui

package module
v2.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 31, 2023 License: MIT Imports: 4 Imported by: 8

README

Warships-GUI

Warships-GUI provides an advanced graphical user interface for the Warships Online game.

This package is suggested for advanced-beginners. If you want a simpler take on GUI development, please check out the Warships-LightGUI package: http://github.com/grupawp/warships-lightgui

Installation

go get github.com/grupawp/warships-gui/v2

Quick Start

Create and display a single board:
package main

import (
	"context"

	gui "github.com/grupawp/warships-gui/v2"
)

func main() {
	ui := gui.NewGUI(true)

	board := gui.NewBoard(1, 1, nil)
	ui.Draw(board)

	states := [10][10]gui.State{}
	for i := range states {
		states[i] = [10]gui.State{}
	}
	board.SetStates(states)

	ctx := context.Background()
	ui.Start(ctx, nil)
}

Display text
package main

import (
	"context"

	gui "github.com/grupawp/warships-gui/v2"
)

func main() {
	ui := gui.NewGUI(true)
	txt := gui.NewText(1, 1, "Press Ctrl+C to exit", nil)
	ui.Draw(txt)
	ui.Start(context.TODO(), nil)
}
Listen for pressed coordinates
package main

import (
	"context"
	"fmt"

	gui "github.com/grupawp/warships-gui/v2"
)

func main() {
	ui := gui.NewGUI(true)

	txt := gui.NewText(1, 1, "Press on any coordinate to log it.", nil)
	ui.Draw(txt)
	ui.Draw(gui.NewText(1, 2, "Press Ctrl+C to exit", nil))

	board := gui.NewBoard(1, 4, nil)
	ui.Draw(board)

	go func() {
		for {
			char := board.Listen(context.TODO())
			txt.SetText(fmt.Sprintf("Coordinate: %s", char))
			ui.Log("Coordinate: %s", char) // logs are displayed after the game exits
		}
	}()

	ui.Start(context.TODO(), nil)
}

Documentation

https://pkg.go.dev/github.com/grupawp/warships-gui/v2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	White = Color{Red: 208, Green: 208, Blue: 208}
	Black = Color{Red: 21, Green: 21, Blue: 21}
	Blue  = Color{Red: 108, Green: 153, Blue: 187}
	Red   = Color{Red: 172, Green: 65, Blue: 66}
	Grey  = Color{Red: 105, Green: 105, Blue: 105}
	Green = Color{Red: 126, Green: 142, Blue: 0}
)

Functions

This section is empty.

Types

type Board

type Board struct {
	// contains filtered or unexported fields
}

Board represents a single board.

func NewBoard

func NewBoard(x, y int, cfg *BoardConfig) *Board

NewBoard returns a new Board struct. X and Y are the coordinates of the top left corner of the board. If no config is provided, default values are used.

func (*Board) Drawables

func (b *Board) Drawables() []tl.Drawable

func (*Board) ID

func (b *Board) ID() uuid.UUID

func (*Board) Listen

func (b *Board) Listen(ctx context.Context) string

Listen blocks until a field is clicked by the user and returns the field as a string containing coordinates. Use context to control cancelation and prevent listening indefinitely.

func (*Board) SetStates

func (b *Board) SetStates(states [10][10]State)

SetStates sets the states of the board. The states are represented as a 10x10 matrix, where the first index is the X coordinate and the second index is the Y coordinate. Example: states[0][0] is the state of the field A1.

type BoardConfig

type BoardConfig struct {
	RulerColor Color
	TextColor  Color
	EmptyColor Color
	HitColor   Color
	MissColor  Color
	ShipColor  Color
	EmptyChar  byte
	HitChar    byte
	MissChar   byte
	ShipChar   byte
}

BoardConfig holds configuration parameters for Board struct.

func NewBoardConfig

func NewBoardConfig() *BoardConfig

NewBoardConfig returns a new config with default values.

type Color

type Color struct {
	Red   uint8
	Green uint8
	Blue  uint8
}

Color represents an RGB color.

func NewColor

func NewColor(r, g, b uint8) Color

NewColor returns a new color. Parameters are red, green and blue values.

type Drawable

type Drawable interface {
	ID() uuid.UUID
	Drawables() []tl.Drawable
}

Drawable represents a collection of related objects that can be drawn on the screen.

type GUI

type GUI struct {
	// contains filtered or unexported fields
}

func NewGUI

func NewGUI(debug bool) *GUI

NewGUI returns a new GUI instance. If debug is true, the GUI will print logs to the terminal after exiting.

func (*GUI) Draw

func (g *GUI) Draw(d Drawable)

Draw draws the given Drawable on the screen.

func (*GUI) Log

func (g *GUI) Log(format string, a ...any)

Log takes a log string and additional parameters, which can be substituted into the string using standard fmt.Printf rules. If debug mode is on, the formatted log will be printed to the terminal when GUI exits.

func (*GUI) Remove

func (g *GUI) Remove(d Drawable)

Remove removes the given Drawable from the screen.

func (*GUI) Start

func (g *GUI) Start(ctx context.Context, endKey *tl.Key)

Start displays the GUI and blocks until endKey is pressed or context is done. If endKey is nil it defaults to Ctrl+C.

type State

type State string
const (
	Empty State = "Empty"
	Hit   State = "Hit"
	Miss  State = "Miss"
	Ship  State = "Ship"
)

type Text

type Text struct {
	// contains filtered or unexported fields
}

func NewText

func NewText(x, y int, text string, cfg *TextConfig) *Text

NewText returns a new Text struct. X and Y are the coordinates of the top left corner of the text. If no config is provided, default values are used.

func (*Text) Drawables

func (t *Text) Drawables() []tl.Drawable

func (*Text) ID

func (t *Text) ID() uuid.UUID

func (*Text) SetBgColor

func (t *Text) SetBgColor(c Color)

SetBgColor sets the background color of the Text struct.

func (*Text) SetFgColor

func (t *Text) SetFgColor(c Color)

SetFgColor sets the foreground color of the Text struct.

func (*Text) SetText

func (t *Text) SetText(text string)

SetText sets the text of the Text struct.

type TextConfig

type TextConfig struct {
	FgColor Color
	BgColor Color
}

TextConfig holds configuration parameters for Text struct.

func NewTextConfig

func NewTextConfig() *TextConfig

NewTextConfig returns a new config with default values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL