2019-11-10 08:51:02 -05:00
|
|
|
package d2scene
|
2019-10-26 00:26:48 -04:00
|
|
|
|
|
|
|
import (
|
2019-11-13 11:56:45 -05:00
|
|
|
"bufio"
|
2019-10-26 02:11:28 -04:00
|
|
|
"image/color"
|
2019-11-13 11:56:45 -05:00
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
2019-10-26 00:55:59 -04:00
|
|
|
"strings"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common/d2resource"
|
2019-11-10 08:51:02 -05:00
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2asset"
|
2019-11-10 03:36:53 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render"
|
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2corecommon/d2coreinterface"
|
2019-11-10 03:36:53 -05:00
|
|
|
|
2019-11-17 00:16:33 -05:00
|
|
|
"github.com/OpenDiablo2/D2Shared/d2common"
|
|
|
|
dh "github.com/OpenDiablo2/D2Shared/d2helper"
|
2019-12-21 20:53:18 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2audio"
|
2019-12-28 16:46:08 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2surface"
|
2019-11-10 08:51:02 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2ui"
|
2019-10-26 00:26:48 -04:00
|
|
|
)
|
|
|
|
|
2019-10-26 02:11:28 -04:00
|
|
|
type labelItem struct {
|
2019-11-10 12:28:41 -05:00
|
|
|
Label d2ui.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
IsHeading bool
|
|
|
|
Available bool
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:26:48 -04:00
|
|
|
// Credits represents the credits scene
|
|
|
|
type Credits struct {
|
2019-11-10 08:51:02 -05:00
|
|
|
uiManager *d2ui.Manager
|
2019-11-10 03:36:53 -05:00
|
|
|
soundManager *d2audio.Manager
|
2019-11-17 00:16:33 -05:00
|
|
|
sceneProvider d2coreinterface.SceneProvider
|
2019-12-21 20:53:18 -05:00
|
|
|
creditsBackground *d2render.Sprite
|
2019-11-10 12:28:41 -05:00
|
|
|
exitButton d2ui.Button
|
2019-10-26 02:11:28 -04:00
|
|
|
creditsText []string
|
|
|
|
labels []*labelItem
|
|
|
|
cycleTime float64
|
|
|
|
cyclesTillNextLine int
|
|
|
|
doneWithCredits bool
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateCredits creates an instance of the credits scene
|
2019-12-21 20:53:18 -05:00
|
|
|
func CreateCredits(sceneProvider d2coreinterface.SceneProvider, uiManager *d2ui.Manager, soundManager *d2audio.Manager) *Credits {
|
2019-10-26 00:26:48 -04:00
|
|
|
result := &Credits{
|
2019-10-26 02:11:28 -04:00
|
|
|
uiManager: uiManager,
|
|
|
|
soundManager: soundManager,
|
|
|
|
sceneProvider: sceneProvider,
|
|
|
|
labels: make([]*labelItem, 0),
|
|
|
|
cycleTime: 0,
|
|
|
|
doneWithCredits: false,
|
|
|
|
cyclesTillNextLine: 0,
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-11-13 11:56:45 -05:00
|
|
|
// Load is called to load the contributors data from file
|
|
|
|
// TODO: use markdown for file and convert it to the suitable format
|
|
|
|
func (v *Credits) LoadContributors() []string {
|
|
|
|
contributors := []string{}
|
|
|
|
file, err := os.Open(path.Join("./", "CONTRIBUTORS"))
|
|
|
|
if err != nil {
|
2019-11-14 23:55:51 -05:00
|
|
|
log.Print("CONTRIBUTORS file is missing")
|
2019-11-13 11:56:45 -05:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
contributors = append(contributors, strings.Trim(scanner.Text(), " "))
|
|
|
|
}
|
|
|
|
return contributors
|
|
|
|
}
|
|
|
|
|
2019-10-26 00:26:48 -04:00
|
|
|
// Load is called to load the resources for the credits scene
|
|
|
|
func (v *Credits) Load() []func() {
|
|
|
|
return []func(){
|
|
|
|
func() {
|
2019-12-21 20:53:18 -05:00
|
|
|
v.creditsBackground, _ = d2render.LoadSprite(d2resource.CreditsBackground, d2resource.PaletteSky)
|
|
|
|
v.creditsBackground.SetPosition(0, 0)
|
2019-10-26 00:55:59 -04:00
|
|
|
},
|
|
|
|
func() {
|
2019-12-21 20:53:18 -05:00
|
|
|
v.exitButton = d2ui.CreateButton(d2ui.ButtonTypeMedium, d2common.TranslateString("#970"))
|
|
|
|
v.exitButton.SetPosition(33, 543)
|
2019-10-26 00:55:59 -04:00
|
|
|
v.exitButton.OnActivated(func() { v.onExitButtonClicked() })
|
2019-11-10 12:28:41 -05:00
|
|
|
v.uiManager.AddWidget(&v.exitButton)
|
2019-10-26 00:55:59 -04:00
|
|
|
},
|
|
|
|
func() {
|
2019-12-24 01:48:45 -05:00
|
|
|
fileData, err := d2asset.LoadFile(d2resource.CreditsText)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
creditData, _ := dh.Utf16BytesToString(fileData[2:])
|
|
|
|
v.creditsText = strings.Split(creditData, "\r\n")
|
2019-10-26 10:34:55 -04:00
|
|
|
for i := range v.creditsText {
|
|
|
|
v.creditsText[i] = strings.Trim(v.creditsText[i], " ")
|
|
|
|
}
|
2019-11-13 11:56:45 -05:00
|
|
|
v.creditsText = append(v.LoadContributors(), v.creditsText...)
|
2019-10-26 00:26:48 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unload unloads the data for the credits scene
|
|
|
|
func (v *Credits) Unload() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Render renders the credits scene
|
2019-12-28 16:46:08 -05:00
|
|
|
func (v *Credits) Render(screen *d2surface.Surface) {
|
2019-12-21 20:53:18 -05:00
|
|
|
v.creditsBackground.RenderSegmented(screen, 4, 3, 0)
|
2019-10-26 02:11:28 -04:00
|
|
|
for _, label := range v.labels {
|
|
|
|
if label.Available {
|
|
|
|
continue
|
|
|
|
}
|
2019-12-21 20:53:18 -05:00
|
|
|
label.Label.Render(screen)
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
|
|
|
|
2019-11-01 14:12:23 -04:00
|
|
|
const secondsPerCycle = float64(0.02)
|
2019-10-26 02:11:28 -04:00
|
|
|
|
2019-10-26 00:26:48 -04:00
|
|
|
// Update runs the update logic on the credits scene
|
2019-10-26 02:11:28 -04:00
|
|
|
func (v *Credits) Update(tickTime float64) {
|
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--
|
|
|
|
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
|
|
|
|
}
|
|
|
|
if label.Label.Y-1 < -15 {
|
|
|
|
label.Available = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
label.Label.Y--
|
|
|
|
}
|
|
|
|
}
|
2019-10-26 00:26:48 -04:00
|
|
|
}
|
2019-10-26 00:55:59 -04:00
|
|
|
|
|
|
|
func (v *Credits) onExitButtonClicked() {
|
2019-12-21 20:53:18 -05:00
|
|
|
mainMenu := CreateMainMenu(v.sceneProvider, v.uiManager, v.soundManager)
|
2019-10-26 00:55:59 -04:00
|
|
|
mainMenu.ShowTrademarkScreen = false
|
|
|
|
v.sceneProvider.SetNextScene(mainMenu)
|
|
|
|
}
|
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:]
|
2019-11-15 19:47:58 -05:00
|
|
|
if len(text) == 0 && v.creditsText[0][0] != '*' {
|
|
|
|
v.cyclesTillNextLine = 19
|
|
|
|
return
|
|
|
|
} else if len(text) == 0 && v.creditsText[0][0] == '*' {
|
|
|
|
v.cyclesTillNextLine = 38
|
2019-10-26 02:11:28 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
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] == '*'
|
|
|
|
isNextSpace := len(v.creditsText) > 0 && len(v.creditsText[0]) == 0
|
2019-10-26 02:11:28 -04:00
|
|
|
var label = v.getNewFontLabel(isHeading)
|
|
|
|
if isHeading {
|
|
|
|
label.SetText(text[1:])
|
|
|
|
} else {
|
|
|
|
label.SetText(text)
|
|
|
|
}
|
2019-10-26 10:34:55 -04:00
|
|
|
width, _ := label.GetSize()
|
2019-10-26 02:11:28 -04:00
|
|
|
isDoubled := false
|
|
|
|
if !isHeading && !isNextHeading && !isNextSpace {
|
|
|
|
isDoubled = true
|
|
|
|
|
|
|
|
// Gotta go side by side
|
2019-12-21 20:53:18 -05:00
|
|
|
label.SetPosition(400-int(width), 605)
|
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:]
|
|
|
|
|
2019-10-26 10:34:55 -04:00
|
|
|
isNextHeading = 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)
|
|
|
|
|
2019-12-21 20:53:18 -05:00
|
|
|
label2.SetPosition(410, 605)
|
2019-10-26 02:11:28 -04:00
|
|
|
} else {
|
2019-12-21 20:53:18 -05:00
|
|
|
label.SetPosition(405-int(width/2), 605)
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if isHeading && isNextHeading {
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 38
|
2019-10-26 02:11:28 -04:00
|
|
|
} else if isNextHeading {
|
|
|
|
if isDoubled {
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 38
|
2019-10-26 02:11:28 -04:00
|
|
|
} else {
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 57
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
} else if isHeading {
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 38
|
2019-10-26 02:11:28 -04:00
|
|
|
} else {
|
2019-11-15 19:47:58 -05:00
|
|
|
v.cyclesTillNextLine = 19
|
2019-10-26 02:11:28 -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 {
|
|
|
|
label.Label.Color = color.RGBA{255, 88, 82, 255}
|
|
|
|
} else {
|
|
|
|
label.Label.Color = color.RGBA{198, 178, 150, 255}
|
|
|
|
}
|
2019-11-10 12:28:41 -05:00
|
|
|
return &label.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
newLabelItem := &labelItem{
|
|
|
|
Available: false,
|
|
|
|
IsHeading: isHeading,
|
2019-12-21 20:53:18 -05:00
|
|
|
Label: d2ui.CreateLabel(d2resource.FontFormal10, d2resource.PaletteSky),
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if isHeading {
|
|
|
|
newLabelItem.Label.Color = color.RGBA{255, 88, 82, 255}
|
|
|
|
} else {
|
|
|
|
newLabelItem.Label.Color = color.RGBA{198, 178, 150, 255}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
v.labels = append(v.labels, newLabelItem)
|
2019-11-10 12:28:41 -05:00
|
|
|
return &newLabelItem.Label
|
2019-10-26 02:11:28 -04:00
|
|
|
}
|