go_dreamfactory/cmd/v2/ui/views/oldtimes.go
2023-04-25 18:27:51 +08:00

195 lines
4.4 KiB
Go

package formview
import (
"fmt"
"go_dreamfactory/cmd/v2/lib/common"
"go_dreamfactory/cmd/v2/model"
"go_dreamfactory/cmd/v2/service"
"go_dreamfactory/cmd/v2/service/observer"
"go_dreamfactory/comm"
"go_dreamfactory/pb"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"
)
type OldtimesView struct {
BaseformView
chapterItemList *common.ItemList //章节列表
levelItemList *common.ItemList //关卡列表
flag bool
right *fyne.Container
}
func (this *OldtimesView) CreateView(t *model.TestCase) fyne.CanvasObject {
this.chapterItemList = common.NewItemList()
this.chapterItemList.ItemList = this.chapterItemList.CreateList()
this.levelItemList = common.NewItemList()
this.levelItemList.ItemList = this.levelItemList.CreateList()
this.right = container.NewGridWithColumns(2,
this.chapterItemList.ItemList,
this.levelItemList.ItemList)
this.chapterItemList.ItemList.OnSelected = func(id widget.ListItemID) {
item := this.chapterItemList.CachedList.Items[id]
this.chapterItemList.SelItemId = item.Id
this.levelItemList.Reset()
if chapter, ok := item.Data.(*pb.Chapter); ok {
for _, ll := range chapter.Levels {
item := common.Item{
Id: cast.ToString(ll.Lid),
Text: fmt.Sprintf("%v", ll.Lid),
}
this.levelItemList.AddItem(item)
}
}
}
// this.levelItemList.ItemList.OnSelected = func(id widget.ListItemID) {
// item := this.levelItemList.CachedList.Items[id]
// this.levelItemList.SelItemId = item.Id
// }
loadChapters := func() {
this.chapterItemList.Reset()
if err := service.GetPttService().SendToClient(
t.MainType,
"getall",
&pb.OldtimesGetallReq{},
); err != nil {
logrus.Error(err)
return
}
}
loadChapters()
levelIdEntry := widget.NewEntry()
form := widget.NewForm(
widget.NewFormItem("关卡", levelIdEntry),
)
enterBtn := widget.NewButton("进入", func() {
defer levelIdEntry.SetText("")
chapterId := cast.ToInt32(this.chapterItemList.SelItemId)
form.OnSubmit = func() {
levelId := cast.ToInt32(levelIdEntry.Text)
if chapterId == 0 || levelId == 0 {
common.ShowTip("没有正确选择章节或关卡")
return
}
if err := service.GetPttService().SendToClient(
t.MainType,
"enter",
&pb.OldtimesEnterReq{
ChapterId: chapterId,
LevelId: levelId,
},
); err != nil {
logrus.Error(err)
return
}
}
win := dialog.NewCustom("升级", "关闭", form, this.w)
win.Resize(fyne.NewSize(600, 300))
win.Show()
})
finishBtn := widget.NewButton("闯关", func() {
defer levelIdEntry.SetText("")
chapterId := cast.ToInt32(this.chapterItemList.SelItemId)
form.OnSubmit = func() {
levelId := cast.ToInt32(levelIdEntry.Text)
if chapterId == 0 || levelId == 0 {
common.ShowTip("没有正确选择章节或关卡")
return
}
if err := service.GetPttService().SendToClient(
t.MainType,
"finish",
&pb.OldtimesFinishReq{
ChapterId: chapterId,
LevelId: levelId,
},
); err != nil {
logrus.Error(err)
return
}
}
win := dialog.NewCustom("升级", "关闭", form, this.w)
win.Resize(fyne.NewSize(600, 300))
win.Show()
})
//领取章节奖励
receiveBtn := widget.NewButton("章节奖励", func() {
chapterId := cast.ToInt32(this.chapterItemList.SelItemId)
if err := service.GetPttService().SendToClient(
t.MainType,
"receive",
&pb.OldtimesReceiveReq{
ChapterId: chapterId,
},
); err != nil {
logrus.Error(err)
return
}
})
topBtns := container.NewHBox(enterBtn, finishBtn, receiveBtn)
c := container.NewBorder(topBtns, nil, nil, nil,
this.right,
)
this.chapterList()
return c
}
func (this *OldtimesView) chapterList() {
if this.flag {
return
}
this.obs.AddListener(observer.EVENT_REQ_RSP, observer.Listener{
OnNotify: func(d interface{}, args ...interface{}) {
data := d.(*pb.UserMessage)
if data.MainType == string(comm.ModuleOldtimes) &&
data.SubType == "getall" {
rsp := &pb.OldtimesGetallResp{}
if !comm.ProtoUnmarshal(data, rsp) {
logrus.Error("unmarshal err")
return
}
if rsp.Data == nil {
return
}
for _, v := range rsp.Data.Chapters {
item := common.Item{
Id: cast.ToString(v.Cid),
Text: fmt.Sprintf("%v", v.Cid),
Data: v,
}
this.chapterItemList.AddItem(item)
}
}
},
})
this.flag = true
}