From 8a205de3ca38f6dfb29899abf8845a97d53e1142 Mon Sep 17 00:00:00 2001 From: cuu Date: Thu, 7 Feb 2019 19:38:11 +0800 Subject: [PATCH] add infinitely scroll up down in RomListPage --- main.go | 2 +- sysgo/UI/Emulator/emulator.go | 7 ++++ sysgo/UI/Emulator/fav_list_page.go | 22 +++++++++- sysgo/UI/Emulator/rom_list_page.go | 64 ++++++++++++++++++------------ sysgo/UI/main_screen.go | 7 +++- sysgo/UI/page.go | 3 +- 6 files changed, 76 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 02d3471..a6d901d 100644 --- a/main.go +++ b/main.go @@ -437,7 +437,7 @@ func run() int { } main_screen.KeyDown(ev) - + main_screen.LastKeyDown = everytime_keydown } } diff --git a/sysgo/UI/Emulator/emulator.go b/sysgo/UI/Emulator/emulator.go index 2b91acf..ab63d9e 100644 --- a/sysgo/UI/Emulator/emulator.go +++ b/sysgo/UI/Emulator/emulator.go @@ -31,11 +31,18 @@ type MyEmulator struct { // as leader of RomListPage and FavListPage, it's a Plu FavPage *FavListPage DeleteConfirmPage *UI.DeleteConfirmPage EmulatorConfig *ActionConfig + + SpeedMax int + SpeedTimeInter int + } func NewMyEmulator() *MyEmulator{ p := &MyEmulator{} + p.SpeedMax = 5 + p.SpeedTimeInter = 300 + return p } diff --git a/sysgo/UI/Emulator/fav_list_page.go b/sysgo/UI/Emulator/fav_list_page.go index 5998163..ec1db84 100644 --- a/sysgo/UI/Emulator/fav_list_page.go +++ b/sysgo/UI/Emulator/fav_list_page.go @@ -6,7 +6,7 @@ import ( "strings" "path/filepath" "errors" - + gotime "time" "github.com/veandco/go-sdl2/ttf" "github.com/cuu/gogame/event" @@ -53,6 +53,8 @@ func NewFavListPage() *FavListPage { p.BGwidth = 75 p.BGheight = 73 + p.ScrollStep = 1 + return p } @@ -434,6 +436,22 @@ func (self *FavListPage) OnLoadCb() { self.Screen.SwapAndShow() } +func (self *FavListPage) SpeedScroll(thekey string ) { + if self.Screen.LastKey == thekey { + self.ScrollStep += 1 + if self.ScrollStep >= self.Leader.SpeedMax { + self.ScrollStep = self.Leader.SpeedMax + } + } else { + self.ScrollStep = 1 + } + cur_time := gotime.Now() + + if cur_time.Sub(self.Screen.LastKeyDown) > gotime.Duration(self.Leader.SpeedTimeInter)*gotime.Millisecond { + self.ScrollStep = 1 + } +} + func (self *FavListPage) KeyDown(ev *event.Event) { if ev.Data["Key"] == UI.CurKeys["Menu"] || ev.Data["Key"] == UI.CurKeys["Left"] { @@ -443,12 +461,14 @@ func (self *FavListPage) KeyDown(ev *event.Event) { } if ev.Data["Key"] == UI.CurKeys["Up"]{ + self.SpeedScroll(ev.Data["Key"]) self.ScrollUp() self.Screen.Draw() self.Screen.SwapAndShow() } if ev.Data["Key"] == UI.CurKeys["Down"] { + self.SpeedScroll(ev.Data["Key"]) self.ScrollDown() self.Screen.Draw() self.Screen.SwapAndShow() diff --git a/sysgo/UI/Emulator/rom_list_page.go b/sysgo/UI/Emulator/rom_list_page.go index 830327c..7a846ec 100644 --- a/sysgo/UI/Emulator/rom_list_page.go +++ b/sysgo/UI/Emulator/rom_list_page.go @@ -8,7 +8,7 @@ import ( "path/filepath" "os/exec" "errors" - + gotime "time" "github.com/veandco/go-sdl2/ttf" "github.com/cuu/gogame/time" "github.com/cuu/gogame/color" @@ -54,6 +54,7 @@ func NewRomListPage() *RomListPage { p.BGwidth = 56 p.BGheight = 70 + p.ScrollStep = 1 return p } @@ -263,24 +264,27 @@ func (self *RomListPage) ScrollUp() { if len(self.MyList) == 0 { return } - - self.PsIndex -=1 + tmp := self.PsIndex + self.PsIndex -=self.ScrollStep + dy := 0 if self.PsIndex < 0 { - self.PsIndex = 0 + self.PsIndex = len(self.MyList) - 1 } + dy = tmp - self.PsIndex + cur_li := self.MyList[self.PsIndex] x,y := cur_li.Coord() _,h := cur_li.Size() - if y < 0 { + { for i,_ := range self.MyList{ x,y = self.MyList[i].Coord() _, h = self.MyList[i].Size() - self.MyList[i].NewCoord(x,y + h) + self.MyList[i].NewCoord(x,y + h*dy) } - self.Scrolled +=1 + self.Scrolled +=dy } } @@ -289,23 +293,26 @@ func (self *RomListPage) ScrollDown(){ if len(self.MyList) == 0 { return } - self.PsIndex +=1 + tmp := self.PsIndex + self.PsIndex +=self.ScrollStep if self.PsIndex >= len(self.MyList) { - self.PsIndex = len(self.MyList) - 1 + self.PsIndex = 0 } + dy := self.PsIndex - tmp + cur_li := self.MyList[self.PsIndex] x,y := cur_li.Coord() _,h := cur_li.Size() - if y+ h > self.Height { + { for i,_ := range self.MyList{ x,y = self.MyList[i].Coord() _, h = self.MyList[i].Size() - self.MyList[i].NewCoord(x,y - h) + self.MyList[i].NewCoord(x,y - h*dy) } - self.Scrolled -=1 + self.Scrolled -=dy } } @@ -428,19 +435,8 @@ func (self *RomListPage) ReScan() { self.SyncList(self.MyStack.Last()) } - - idx := self.PsIndex - - if idx > len(self.MyList) - 1 { - idx = len(self.MyList) - if idx > 0 { - idx -= 1 - }else if idx == 0 { - //nothing in MyList - } - } - - self.PsIndex = idx //sync PsIndex + self.PsIndex = 0 //sync PsIndex + self.Scrolled = 0 self.SyncScroll() } @@ -453,6 +449,22 @@ func (self *RomListPage) OnReturnBackCb() { } +func (self *RomListPage) SpeedScroll(thekey string ) { + if self.Screen.LastKey == thekey { + self.ScrollStep += 1 + if self.ScrollStep >= self.Leader.SpeedMax { + self.ScrollStep = self.Leader.SpeedMax + } + } else { + self.ScrollStep = 1 + } + cur_time := gotime.Now() + + if cur_time.Sub(self.Screen.LastKeyDown) > gotime.Duration(self.Leader.SpeedTimeInter)*gotime.Millisecond { + self.ScrollStep = 1 + } +} + func (self *RomListPage) KeyDown(ev *event.Event) { if ev.Data["Key"] == UI.CurKeys["Menu"]{ @@ -469,12 +481,14 @@ func (self *RomListPage) KeyDown(ev *event.Event) { } if ev.Data["Key"] == UI.CurKeys["Up"]{ + self.SpeedScroll(ev.Data["Key"]) self.ScrollUp() self.Screen.Draw() self.Screen.SwapAndShow() } if ev.Data["Key"] == UI.CurKeys["Down"] { + self.SpeedScroll(ev.Data["Key"]) self.ScrollDown() self.Screen.Draw() self.Screen.SwapAndShow() diff --git a/sysgo/UI/main_screen.go b/sysgo/UI/main_screen.go index 960625b..9d09c1a 100644 --- a/sysgo/UI/main_screen.go +++ b/sysgo/UI/main_screen.go @@ -7,7 +7,7 @@ import ( "log" //"encoding/json" "path/filepath" - + gotime "time" "github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/ttf" @@ -179,6 +179,9 @@ type MainScreen struct { Closed bool UIPluginList []*UIPlugin + + LastKey string + LastKeyDown gotime.Time } @@ -382,6 +385,8 @@ func (self *MainScreen) KeyDown(ev *event.Event) { } self.CurrentPage.KeyDown(ev) + self.LastKey = ev.Data["Key"] + } diff --git a/sysgo/UI/page.go b/sysgo/UI/page.go index fa12426..3beb35f 100644 --- a/sysgo/UI/page.go +++ b/sysgo/UI/page.go @@ -252,6 +252,7 @@ type Page struct { SelectedIconTopOffset int EasingDur int + ScrollStep int } func NewPage() *Page { @@ -261,7 +262,7 @@ func NewPage() *Page { p.EasingDur = 10 p.Align = ALIGN["SLeft"] - + p.ScrollStep = 1 p.FootMsg = [5]string{"Nav.","","","","Enter"} return p