2020-06-13 20:36:20 -04:00
|
|
|
package d2gamescreen
|
2019-10-26 00:26:48 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-13 11:56:45 -05:00
|
|
|
"bufio"
|
2020-07-02 13:55:43 -04:00
|
|
|
"fmt"
|
2019-11-13 11:56:45 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-10-26 00:55:59 -04:00
|
|
|
"strings"
|
|
|
|
|
2020-07-11 11:24:04 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-08 21:02:37 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2resource"
|
2020-09-12 16:25:09 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2util"
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2asset"
|
2020-06-13 20:36:20 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2screen"
|
2020-01-31 23:18:11 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2core/d2ui"
|
2019-10-26 00:26:48 -04:00
|
|
|
)
|
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
const (
|
2020-08-06 10:30:23 -04:00
|
|
|
creditsX, creditsY = 0, 0
|
2020-07-18 09:54:10 -04:00
|
|
|
charSelExitBtnX, charSelExitBtnY = 33, 543
|
|
|
|
)
|
|
|
|
|
|
|
|
const secondsPerCycle float64 = 0.02
|
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
type labelItem struct {
|
2020-08-06 10:30:23 -04:00
|
|
|
Label *d2ui.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
IsHeading bool
|
|
|
|
Available bool
|
|
|
|
}
|
|
|
|
|
2020-06-13 20:36:20 -04:00
|
|
|
// Credits represents the credits screen
|
2019-10-26 00:26:48 -04:00
|
|
|
type Credits struct {
|
2020-02-01 18:55:56 -05:00
|
|
|
creditsBackground *d2ui.Sprite
|
2020-08-06 10:30:23 -04:00
|
|
|
exitButton *d2ui.Button
|
2019-10-26 02:11:28 -04:00
|
|
|
creditsText []string
|
|
|
|
labels []*labelItem
|
|
|
|
cycleTime float64
|
|
|
|
cyclesTillNextLine int
|
|
|
|
doneWithCredits bool
|
2020-07-14 13:11:23 -04:00
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
asset *d2asset.AssetManager
|
2020-07-14 13:11:23 -04:00
|
|
|
renderer d2interface.Renderer
|
2020-10-07 21:20:05 -04:00
|
|
|
navigator d2interface.Navigator
|
2020-08-06 10:30:23 -04:00
|
|
|
uiManager *d2ui.UIManager
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2020-06-13 20:36:20 -04:00
|
|
|
// CreateCredits creates an instance of the credits screen
|
2020-10-07 21:20:05 -04:00
|
|
|
func CreateCredits(navigator d2interface.Navigator, asset *d2asset.AssetManager, renderer d2interface.Renderer,
|
2020-09-12 16:51:30 -04:00
|
|
|
ui *d2ui.UIManager) *Credits {
|
2019-10-26 00:26:48 -04:00
|
|
|
result := &Credits{
|
2020-09-12 16:51:30 -04:00
|
|
|
asset: asset,
|
2019-10-26 02:11:28 -04:00
|
|
|
labels: make([]*labelItem, 0),
|
|
|
|
cycleTime: 0,
|
|
|
|
doneWithCredits: false,
|
|
|
|
cyclesTillNextLine: 0,
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer: renderer,
|
2020-07-14 13:11:23 -04:00
|
|
|
navigator: navigator,
|
2020-08-06 10:30:23 -04:00
|
|
|
uiManager: ui,
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 00:26:48 -04:00
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
// LoadContributors loads the contributors data from file
|
2019-11-13 11:56:45 -05:00
|
|
|
func (v *Credits) LoadContributors() []string {
|
|
|
|
file, err := os.Open(path.Join("./", "CONTRIBUTORS"))
|
2020-02-01 21:51:49 -05:00
|
|
|
if err != nil || file == nil {
|
2019-11-14 23:55:51 -05:00
|
|
|
log.Print("CONTRIBUTORS file is missing")
|
2020-02-08 21:02:37 -05:00
|
|
|
return []string{"MISSING CONTRIBUTOR FILES!"}
|
2019-11-13 11:56:45 -05:00
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err = file.Close(); err != nil {
|
|
|
|
fmt.Printf("an error occurred while closing file: %s, err: %q\n", file.Name(), err)
|
|
|
|
}
|
|
|
|
}()
|
2019-11-13 11:56:45 -05:00
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
2020-07-02 13:55:43 -04:00
|
|
|
|
|
|
|
var contributors []string
|
2019-11-13 11:56:45 -05:00
|
|
|
for scanner.Scan() {
|
|
|
|
contributors = append(contributors, strings.Trim(scanner.Text(), " "))
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-11-13 11:56:45 -05:00
|
|
|
return contributors
|
|
|
|
}
|
|
|
|
|
2020-06-24 18:46:03 -04:00
|
|
|
// OnLoad is called to load the resources for the credits screen
|
|
|
|
func (v *Credits) OnLoad(loading d2screen.LoadingState) {
|
2020-09-23 13:30:54 -04:00
|
|
|
var err error
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
v.creditsBackground, err = v.uiManager.NewSprite(d2resource.CreditsBackground, d2resource.PaletteSky)
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
2020-10-22 01:12:06 -04:00
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
v.creditsBackground.SetPosition(creditsX, creditsY)
|
|
|
|
loading.Progress(twentyPercent)
|
2020-02-08 21:02:37 -05:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
v.exitButton = v.uiManager.NewButton(d2ui.ButtonTypeMedium, "EXIT")
|
2020-07-18 09:54:10 -04:00
|
|
|
v.exitButton.SetPosition(charSelExitBtnX, charSelExitBtnY)
|
2020-02-08 21:02:37 -05:00
|
|
|
v.exitButton.OnActivated(func() { v.onExitButtonClicked() })
|
2020-07-18 09:54:10 -04:00
|
|
|
loading.Progress(fourtyPercent)
|
2020-02-08 21:02:37 -05:00
|
|
|
|
2020-09-12 16:51:30 -04:00
|
|
|
fileData, err := v.asset.LoadFile(d2resource.CreditsText)
|
2020-02-08 21:02:37 -05:00
|
|
|
if err != nil {
|
2020-06-24 18:46:03 -04:00
|
|
|
loading.Error(err)
|
|
|
|
return
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
loading.Progress(sixtyPercent)
|
2020-06-24 18:46:03 -04:00
|
|
|
|
2020-09-23 13:30:54 -04:00
|
|
|
creditData, err := d2util.Utf16BytesToString(fileData[2:])
|
|
|
|
if err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
v.creditsText = strings.Split(creditData, "\r\n")
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
for i := range v.creditsText {
|
|
|
|
v.creditsText[i] = strings.Trim(v.creditsText[i], " ")
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
loading.Progress(eightyPercent)
|
2020-06-24 18:46:03 -04:00
|
|
|
|
2020-02-08 21:02:37 -05:00
|
|
|
v.creditsText = append(v.LoadContributors(), v.creditsText...)
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2020-06-13 20:36:20 -04:00
|
|
|
// Render renders the credits screen
|
2020-06-29 00:41:58 -04:00
|
|
|
func (v *Credits) Render(screen d2interface.Surface) error {
|
2020-07-02 13:55:43 -04:00
|
|
|
err := v.creditsBackground.RenderSegmented(screen, 4, 3, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
for _, label := range v.labels {
|
|
|
|
if label.Available {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
label.Label.Render(screen)
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
|
|
|
|
return nil
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
// Advance runs the update logic on the credits screen
|
2020-02-08 21:02:37 -05:00
|
|
|
func (v *Credits) Advance(tickTime float64) error {
|
2019-10-26 10:34:55 -04:00
|
|
|
v.cycleTime += tickTime
|
2019-10-26 02:11:28 -04:00
|
|
|
for v.cycleTime >= secondsPerCycle {
|
|
|
|
v.cycleTime -= secondsPerCycle
|
|
|
|
v.cyclesTillNextLine--
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
if !v.doneWithCredits && v.cyclesTillNextLine <= 0 {
|
|
|
|
v.addNextItem()
|
|
|
|
}
|
2019-10-26 00:26:48 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
for _, label := range v.labels {
|
|
|
|
if label.Available {
|
|
|
|
continue
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
if label.Label.Y-1 < -15 {
|
|
|
|
label.Available = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
label.Label.Y--
|
|
|
|
}
|
|
|
|
}
|
2020-02-08 21:02:37 -05:00
|
|
|
|
|
|
|
return nil
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
2019-10-26 00:55:59 -04:00
|
|
|
|
|
|
|
func (v *Credits) onExitButtonClicked() {
|
2020-07-14 13:11:23 -04:00
|
|
|
v.navigator.ToMainMenu()
|
2019-10-26 00:55:59 -04:00
|
|
|
}
|
2019-10-26 02:11:28 -04:00
|
|
|
|
|
|
|
func (v *Credits) addNextItem() {
|
|
|
|
if len(v.creditsText) == 0 {
|
|
|
|
v.doneWithCredits = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-26 10:34:55 -04:00
|
|
|
text := v.creditsText[0]
|
2019-10-26 02:11:28 -04:00
|
|
|
v.creditsText = v.creditsText[1:]
|
2020-07-02 13:55:43 -04:00
|
|
|
|
|
|
|
if text == "" {
|
|
|
|
if v.creditsText[0][0] == '*' {
|
|
|
|
v.cyclesTillNextLine = 38
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 19
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
return
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
isHeading := text[0] == '*'
|
2019-10-26 10:34:55 -04:00
|
|
|
isNextHeading := len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
|
2020-07-02 13:55:43 -04:00
|
|
|
isNextSpace := len(v.creditsText) > 0 && v.creditsText[0] == ""
|
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
var label = v.getNewFontLabel(isHeading)
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
if isHeading {
|
|
|
|
label.SetText(text[1:])
|
|
|
|
} else {
|
|
|
|
label.SetText(text)
|
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
|
|
|
isDoubled, isNextHeading := v.setItemLabelPosition(label, isHeading, isNextHeading, isNextSpace)
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isHeading && isNextHeading:
|
|
|
|
v.cyclesTillNextLine = 38
|
|
|
|
case isNextHeading:
|
|
|
|
if isDoubled {
|
|
|
|
v.cyclesTillNextLine = 38
|
|
|
|
} else {
|
|
|
|
v.cyclesTillNextLine = 57
|
|
|
|
}
|
|
|
|
case isHeading:
|
|
|
|
v.cyclesTillNextLine = 38
|
|
|
|
default:
|
|
|
|
v.cyclesTillNextLine = 19
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
const (
|
|
|
|
itemLabelY = 605
|
|
|
|
itemLabelX = 400
|
|
|
|
itemLabel2offsetX = 10
|
|
|
|
halfItemLabel2offsetX = itemLabel2offsetX / 2
|
2020-07-18 09:54:10 -04:00
|
|
|
)
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
func (v *Credits) setItemLabelPosition(label *d2ui.Label, isHeading, isNextHeading, isNextSpace bool) (isDoubled, nextHeading bool) {
|
2019-10-26 10:34:55 -04:00
|
|
|
width, _ := label.GetSize()
|
2020-07-18 09:54:10 -04:00
|
|
|
half := 2
|
2020-08-06 10:30:23 -04:00
|
|
|
halfWidth := width / half
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
if !isHeading && !isNextHeading && !isNextSpace {
|
|
|
|
isDoubled = true
|
|
|
|
// Gotta go side by side
|
2020-07-18 09:54:10 -04:00
|
|
|
label.SetPosition(itemLabelX-width, itemLabelY)
|
2019-10-26 02:11:28 -04:00
|
|
|
|
2019-10-26 10:34:55 -04:00
|
|
|
text2 := v.creditsText[0]
|
2019-10-26 02:11:28 -04:00
|
|
|
v.creditsText = v.creditsText[1:]
|
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
nextHeading = len(v.creditsText) > 0 && len(v.creditsText[0]) > 0 && v.creditsText[0][0] == '*'
|
2019-10-26 02:11:28 -04:00
|
|
|
label2 := v.getNewFontLabel(isHeading)
|
|
|
|
label2.SetText(text2)
|
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
label2.SetPosition(itemLabelX+itemLabel2offsetX, itemLabelY)
|
2019-10-26 02:11:28 -04:00
|
|
|
|
2020-07-02 13:55:43 -04:00
|
|
|
return isDoubled, nextHeading
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
label.SetPosition(itemLabelX+halfItemLabel2offsetX-halfWidth, itemLabelY)
|
2020-07-02 13:55:43 -04:00
|
|
|
|
|
|
|
return isDoubled, isNextHeading
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
|
2020-07-18 09:54:10 -04:00
|
|
|
const (
|
|
|
|
lightRed = 0xff5852ff
|
2020-08-06 10:30:23 -04:00
|
|
|
beige = 0xc6b296ff
|
2020-07-18 09:54:10 -04:00
|
|
|
)
|
|
|
|
|
2019-11-10 08:51:02 -05:00
|
|
|
func (v *Credits) getNewFontLabel(isHeading bool) *d2ui.Label {
|
2019-10-26 02:11:28 -04:00
|
|
|
for _, label := range v.labels {
|
|
|
|
if label.Available {
|
|
|
|
label.Available = false
|
2019-10-26 10:34:55 -04:00
|
|
|
if isHeading {
|
2020-08-02 21:26:07 -04:00
|
|
|
label.Label.Color[0] = rgbaColor(lightRed)
|
2019-10-26 10:34:55 -04:00
|
|
|
} else {
|
2020-08-02 21:26:07 -04:00
|
|
|
label.Label.Color[0] = rgbaColor(beige)
|
2019-10-26 10:34:55 -04:00
|
|
|
}
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
return label.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newLabelItem := &labelItem{
|
|
|
|
Available: false,
|
|
|
|
IsHeading: isHeading,
|
2020-08-06 10:30:23 -04:00
|
|
|
Label: v.uiManager.NewLabel(d2resource.FontFormal10, d2resource.PaletteSky),
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if isHeading {
|
2020-08-02 21:26:07 -04:00
|
|
|
newLabelItem.Label.Color[0] = rgbaColor(lightRed)
|
2019-10-26 02:11:28 -04:00
|
|
|
} else {
|
2020-08-02 21:26:07 -04:00
|
|
|
newLabelItem.Label.Color[0] = rgbaColor(beige)
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
v.labels = append(v.labels, newLabelItem)
|
2020-07-02 13:55:43 -04:00
|
|
|
|
2020-08-06 10:30:23 -04:00
|
|
|
return newLabelItem.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|