Skip to content

Commit

Permalink
Merge branch 'master' into 311_dt1-widget-dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
gravestench authored Jul 8, 2021
2 parents 734cfb7 + 531c8d6 commit 2efe48d
Show file tree
Hide file tree
Showing 28 changed files with 567 additions and 1,197 deletions.
70 changes: 13 additions & 57 deletions hswidget/animdatawidget/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ package animdatawidget

import (
"fmt"
"log"
"sort"

"github.com/ianling/giu"

"github.com/OpenDiablo2/OpenDiablo2/d2common/d2datautils"

"github.com/OpenDiablo2/HellSpawner/hsassets"
)

Expand All @@ -20,71 +17,30 @@ const (
)

type widgetState struct {
mode widgetMode
Mode widgetMode
mapKeys []string
mapIndex int32
recordIdx int32
MapIndex int32
RecordIdx int32
deleteIcon *giu.Texture
addEntryState
}

// Dispose clears widget's state
func (ws *widgetState) Dispose() {
ws.mode = widgetModeList
ws.mapKeys = make([]string, 0)
ws.mapIndex = 0
ws.recordIdx = 0
ws.addEntryState.Dispose()
ws.deleteIcon = nil
func (s *widgetState) Dispose() {
s.Mode = widgetModeList
s.mapKeys = make([]string, 0)
s.MapIndex = 0
s.RecordIdx = 0
s.addEntryState.Dispose()
s.deleteIcon = nil
}

type addEntryState struct {
name string
}

func (aes *addEntryState) Dispose() {
aes.name = ""
Name string
}

// Encode encodes state into byte slice to save it
func (ws *widgetState) Encode() []byte {
sw := d2datautils.CreateStreamWriter()

sw.PushInt32(int32(ws.mode))
sw.PushInt32(ws.mapIndex)
sw.PushInt32(ws.recordIdx)

return sw.GetBytes()
}

// Decode decodes byte slice into widget state
func (ws *widgetState) Decode(data []byte) {
sr := d2datautils.CreateStreamReader(data)

mode, err := sr.ReadInt32()
if err != nil {
log.Print(err)

return
}

mapIndex, err := sr.ReadInt32()
if err != nil {
log.Print(err)

return
}

recordIdx, err := sr.ReadInt32()
if err != nil {
log.Print(err)

return
}

ws.mode = widgetMode(mode)
ws.mapIndex = mapIndex
ws.recordIdx = recordIdx
func (s *addEntryState) Dispose() {
s.Name = ""
}

func (p *widget) getStateID() string {
Expand Down
72 changes: 38 additions & 34 deletions hswidget/animdatawidget/widget.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package animdatawidget

import (
"encoding/json"
"fmt"
"log"
"strconv"
Expand Down Expand Up @@ -37,7 +38,10 @@ func Create(textureLoader hscommon.TextureLoader, state []byte, id string, d2 *d

if state != nil && giu.Context.GetState(result.getStateID()) == nil {
s := result.getState()
s.Decode(state)
if err := json.Unmarshal(state, s); err != nil {
log.Printf("error decoding animation data widget state: %v", err)
}

result.setState(s)
}

Expand All @@ -48,7 +52,7 @@ func Create(textureLoader hscommon.TextureLoader, state []byte, id string, d2 *d
func (p *widget) Build() {
state := p.getState()

switch state.mode {
switch state.Mode {
case widgetModeList:
p.buildAnimationsList()
case widgetModeViewRecord:
Expand All @@ -61,9 +65,9 @@ func (p *widget) buildAnimationsList() {

keys := make([]string, 0)

if state.name != "" {
if state.Name != "" {
for _, key := range state.mapKeys {
if strings.Contains(key, state.name) {
if strings.Contains(key, state.Name) {
keys = append(keys, key)
}
}
Expand All @@ -87,8 +91,8 @@ func (p *widget) buildAnimationsList() {
},
),
giu.Selectable(name).OnClick(func() {
state.mapIndex = int32(currentIdx)
state.mode = widgetModeViewRecord
state.MapIndex = int32(currentIdx)
state.Mode = widgetModeViewRecord
}),
)
}
Expand All @@ -115,9 +119,9 @@ func (p *widget) buildAnimationsList() {
func (p *widget) buildViewRecordLayout() {
state := p.getState()

name := state.mapKeys[state.mapIndex]
name := state.mapKeys[state.MapIndex]
records := p.d2.GetRecords(name)
record := records[state.recordIdx]
record := records[state.RecordIdx]

max := len(records) - 1

Expand All @@ -127,26 +131,26 @@ func (p *widget) buildViewRecordLayout() {
giu.Layout{
giu.Row(
giu.ArrowButton("##"+p.id+"previousAnimation", giu.DirectionLeft).OnClick(func() {
state.recordIdx = 0
state.RecordIdx = 0

if state.mapIndex > 0 {
state.mapIndex--
if state.MapIndex > 0 {
state.MapIndex--
}
}),
giu.Label(fmt.Sprintf("Animation name: %s", name)),
giu.ArrowButton("##"+p.id+"nextAnimation", giu.DirectionRight).OnClick(func() {
state.recordIdx = 0
state.RecordIdx = 0

if int(state.mapIndex) < len(state.mapKeys)-1 {
state.mapIndex++
if int(state.MapIndex) < len(state.mapKeys)-1 {
state.MapIndex++
}
}),
),
giu.Separator(),
giu.Custom(func() {
if max > 0 {
giu.Layout{
giu.SliderInt("record##"+p.id, &state.recordIdx, 0, int32(max)),
giu.SliderInt("record##"+p.id, &state.RecordIdx, 0, int32(max)),
giu.Separator(),
}.Build()
}
Expand All @@ -167,31 +171,31 @@ func (p *widget) buildViewRecordLayout() {
giu.Label(fmt.Sprintf("Frame duration: %v (miliseconds)", record.FrameDurationMS())),
giu.Separator(),
giu.Button("Back to entry preview##"+p.id+"backToRecordSelection").Size(actionBtnW, actionBtnH).OnClick(func() {
state.mode = widgetModeList
state.Mode = widgetModeList
}),
giu.Button("Add record##"+p.id+"addRecordBtn").Size(actionBtnW, actionBtnH).OnClick(func() {
p.d2.PushRecord(name)

// no -1, because current records hasn't new field yet
state.recordIdx = int32(len(records))
state.RecordIdx = int32(len(records))
}),
giu.Button("Delete record##"+p.id+"deleteRecordBtn").Size(actionBtnW, actionBtnH).OnClick(func() {
if len(records) == 1 {
state.recordIdx = 0
state.mode = widgetModeList
state.RecordIdx = 0
state.Mode = widgetModeList
p.deleteEntry(name)

return
}
if state.recordIdx == int32(len(records)-1) {
if state.recordIdx > 0 {
state.recordIdx--
if state.RecordIdx == int32(len(records)-1) {
if state.RecordIdx > 0 {
state.RecordIdx--
} else {
state.mode = widgetModeList
state.Mode = widgetModeList
}
}

err := p.d2.DeleteRecord(name, int(state.recordIdx))
err := p.d2.DeleteRecord(name, int(state.RecordIdx))
if err != nil {
log.Print(err)
}
Expand All @@ -204,17 +208,17 @@ func (p *widget) makeSearchLayout() giu.Layout {

return giu.Layout{
giu.Label("Search or type new entry name:"),
giu.InputText("##"+p.id+"newEntryName", &state.name).Size(listW).OnChange(func() {
giu.InputText("##"+p.id+"newEntryName", &state.Name).Size(listW).OnChange(func() {
// formatting
state.name = strings.ToUpper(state.name)
state.name = strings.ReplaceAll(state.name, " ", "")
state.Name = strings.ToUpper(state.Name)
state.Name = strings.ReplaceAll(state.Name, " ", "")
}),
giu.Custom(func() {
if state.name == "" {
if state.Name == "" {
return
}

found := (len(p.d2.GetRecords(state.name)) > 0)
found := (len(p.d2.GetRecords(state.Name)) > 0)
if found {
giu.Row(
giu.Button("View##"+p.id+"addEntryViewEntry").Size(saveCancelButtonW, saveCancelButtonH).OnClick(func() {
Expand All @@ -227,12 +231,12 @@ func (p *widget) makeSearchLayout() giu.Layout {

giu.Row(
giu.Button("Add##"+p.id+"addEntry").Size(saveCancelButtonW, saveCancelButtonH).OnClick(func() {
err := p.d2.AddEntry(state.name)
err := p.d2.AddEntry(state.Name)
if err != nil {
log.Print(err)
}

p.d2.PushRecord(state.name)
p.d2.PushRecord(state.Name)
p.reloadMapKeys()
p.viewRecord()
}),
Expand All @@ -245,12 +249,12 @@ func (p *widget) viewRecord() {
state := p.getState()

for n, i := range state.mapKeys {
if i == state.name {
state.mapIndex = int32(n)
if i == state.Name {
state.MapIndex = int32(n)
}
}

state.mode = widgetModeViewRecord
state.Mode = widgetModeViewRecord
}

func (p *widget) deleteEntry(name string) {
Expand Down
Loading

0 comments on commit 2efe48d

Please sign in to comment.