Explorar o código

add infinitely scroll up down in RomListPage

cuu %!s(int64=5) %!d(string=hai) anos
pai
achega
8a205de3ca

+ 1 - 1
main.go

@@ -437,7 +437,7 @@ func run() int {
       }
                   
 			main_screen.KeyDown(ev)
-			
+			main_screen.LastKeyDown = everytime_keydown
 		}
 	}
 

+ 7 - 0
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
 }
 

+ 21 - 1
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()

+ 39 - 25
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()

+ 6 - 1
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"]
+  
 }
 
 

+ 2 - 1
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