github.com/df-mc/dragonfly@v0.9.13/server/player/title/title.go (about)

     1  package title
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"time"
     7  )
     8  
     9  // Title represents a title that may be sent to the player. The title will show up as large text in the middle
    10  // of the screen, with optional subtitle and action text.
    11  type Title struct {
    12  	text, subtitle, actionText                string
    13  	fadeInDuration, fadeOutDuration, duration time.Duration
    14  }
    15  
    16  // New returns a new title using the text passed. The text is formatted according to the formatting rules of
    17  // fmt.Sprintln, but with no newline at the end.
    18  // The title has default durations set, which will generally suffice.
    19  func New(text ...any) Title {
    20  	return Title{
    21  		text:            format(text),
    22  		fadeInDuration:  time.Second / 20,
    23  		fadeOutDuration: time.Second / 20,
    24  		duration:        time.Second * 2,
    25  	}
    26  }
    27  
    28  // Text returns the text of the title, as passed to New when created.
    29  func (title Title) Text() string {
    30  	return title.text
    31  }
    32  
    33  // WithSubtitle sets the subtitle of the title. The text passed will be formatted according to the formatting
    34  // rules of fmt.Sprintln, but without the newline.
    35  // The subtitle is shown under the title in a somewhat smaller font.
    36  // The new Title with the subtitle is returned.
    37  func (title Title) WithSubtitle(text ...any) Title {
    38  	title.subtitle = format(text)
    39  	return title
    40  }
    41  
    42  // Subtitle returns the subtitle of the title, as passed to SetSubtitle. Subtitle returns an empty string if
    43  // no subtitle was previously set.
    44  func (title Title) Subtitle() string {
    45  	return title.subtitle
    46  }
    47  
    48  // WithActionText sets the action text of the title. This text is roughly the same as sending a tip/popup, but
    49  // will synchronise with the title.
    50  // SetActionText will format the text passed using the formatting rules of fmt.Sprintln, but without newline.
    51  // The new Title with the action text is returned.
    52  func (title Title) WithActionText(text ...any) Title {
    53  	title.actionText = format(text)
    54  	return title
    55  }
    56  
    57  // ActionText returns the action text added to the title. This text is roughly the same as sending a tip, but
    58  // will synchronise with the title. By default, the action text is empty.
    59  func (title Title) ActionText() string {
    60  	return title.actionText
    61  }
    62  
    63  // Duration returns the duration that the title will be visible for, without fading in or out. By default,
    64  // this is two seconds.
    65  func (title Title) Duration() time.Duration {
    66  	return title.duration
    67  }
    68  
    69  // WithDuration sets the duration that the title will be visible for without fading in or fading out.
    70  // The new Title with the duration is returned.
    71  func (title Title) WithDuration(d time.Duration) Title {
    72  	title.duration = d
    73  	return title
    74  }
    75  
    76  // WithFadeInDuration sets the duration that the title takes to fade in on the screen.
    77  // The new Title with the fade-in duration is returned.
    78  func (title Title) WithFadeInDuration(d time.Duration) Title {
    79  	title.fadeInDuration = d
    80  	return title
    81  }
    82  
    83  // FadeInDuration returns the duration that the fade-in of the title takes. By default, this is a quarter of
    84  // a second.
    85  func (title Title) FadeInDuration() time.Duration {
    86  	return title.fadeInDuration
    87  }
    88  
    89  // WithFadeOutDuration sets the duration that the title takes to fade out of the screen.
    90  // The new Title with the fade-out duration is returned.
    91  func (title Title) WithFadeOutDuration(d time.Duration) Title {
    92  	title.fadeOutDuration = d
    93  	return title
    94  }
    95  
    96  // FadeOutDuration returns the duration that the fade-out of the title takes.By default, this is a quarter of
    97  // a second.
    98  func (title Title) FadeOutDuration() time.Duration {
    99  	return title.fadeOutDuration
   100  }
   101  
   102  // format is a utility function to format a list of values to have spaces between them, but no newline at the
   103  // end.
   104  func format(a []any) string {
   105  	return strings.TrimSuffix(strings.TrimSuffix(fmt.Sprintln(a...), "\n"), "\n")
   106  }