page.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. package UI
  2. import (
  3. "fmt"
  4. // "math"
  5. //"reflect"
  6. "sync"
  7. "github.com/veandco/go-sdl2/sdl"
  8. "github.com/cuu/gogame/draw"
  9. "github.com/cuu/gogame/surface"
  10. // "github.com/cuu/gogame/rect"
  11. // "github.com/cuu/gogame/font"
  12. "github.com/cuu/gogame/event"
  13. "github.com/clockworkpi/LauncherGoDev/sysgo/easings"
  14. "github.com/cuu/gogame/transform"
  15. )
  16. type element struct {
  17. data interface{}
  18. next *element
  19. }
  20. type PageStack struct {
  21. lock *sync.Mutex
  22. head *element
  23. Size int
  24. }
  25. func (stk *PageStack) Push(data interface{}) {
  26. stk.lock.Lock()
  27. element := new(element)
  28. element.data = data
  29. temp := stk.head
  30. element.next = temp
  31. stk.head = element
  32. stk.Size++
  33. stk.lock.Unlock()
  34. }
  35. func (stk *PageStack) Pop() interface{} {
  36. if stk.head == nil {
  37. return nil
  38. }
  39. stk.lock.Lock()
  40. r := stk.head.data
  41. stk.head = stk.head.next
  42. stk.Size--
  43. stk.lock.Unlock()
  44. return r
  45. }
  46. func (stk *PageStack) Length() int {
  47. return stk.Size
  48. }
  49. func NewPageStack() *PageStack {
  50. stk := new(PageStack)
  51. stk.lock = &sync.Mutex{}
  52. return stk
  53. }
  54. type PageSelectorInterface interface {
  55. Init(x, y, w, h, alpha int)
  56. Adjust(x, y, w, h, alpha int)
  57. GetOnShow() bool
  58. SetOnShow(onshow bool)
  59. Coord() (int, int)
  60. NewCoord(x, y int)
  61. Size() (int, int)
  62. NewSize(w, h int)
  63. Draw()
  64. }
  65. type PageSelector struct {
  66. Widget
  67. Alpha int
  68. OnShow bool
  69. IconSurf *sdl.Surface
  70. Parent PageInterface
  71. }
  72. func NewPageSelector() *PageSelector {
  73. p := &PageSelector{}
  74. p.OnShow = true
  75. return p
  76. }
  77. func (self *PageSelector) Init(x, y, w, h, alpha int) {
  78. self.Adjust(x, y, w, h, alpha)
  79. }
  80. func (self *PageSelector) Adjust(x, y, w, h, alpha int) {
  81. self.PosX = x
  82. self.PosY = y
  83. self.Width = w
  84. self.Height = h
  85. self.Alpha = alpha
  86. }
  87. func (self *PageSelector) GetOnShow() bool {
  88. return self.OnShow
  89. }
  90. func (self *PageSelector) SetOnShow(onshow bool) {
  91. self.OnShow = onshow
  92. }
  93. func (self *PageSelector) Draw() {
  94. canvas := self.Parent.GetCanvasHWND()
  95. idx := self.Parent.GetPsIndex()
  96. iconidx := self.Parent.GetIconIndex()
  97. icons := self.Parent.GetIcons()
  98. if idx < len(icons) {
  99. icon_x, _ := icons[idx].Coord()
  100. _, icon_y := icons[iconidx].Coord()
  101. parent_x, _ := self.Parent.Coord()
  102. parent_w, parent_h := self.Parent.Size()
  103. x := icon_x + parent_x
  104. y := icon_y // only use current icon's PosY
  105. rect_ := draw.MidRect(x, y, self.Width, self.Height, parent_w, parent_h)
  106. if rect_.W <= 0 || rect_.H <= 0 {
  107. return
  108. }
  109. if self.IconSurf != nil {
  110. surface.Blit(canvas, self.IconSurf, rect_, nil)
  111. }
  112. }
  113. }
  114. type PageInterface interface {
  115. // ## shared functions ##
  116. Adjust()
  117. Init()
  118. GetScreen() *MainScreen
  119. GetIcons() []IconItemInterface
  120. SetScreen(main_screen *MainScreen)
  121. SetFootMsg(footmsg [5]string)
  122. GetCanvasHWND() *sdl.Surface
  123. SetCanvasHWND(canvas *sdl.Surface)
  124. GetHWND() *sdl.Surface
  125. SetHWND(h *sdl.Surface)
  126. AdjustHLeftAlign()
  127. AdjustSAutoLeftAlign()
  128. SetPsIndex(idx int)
  129. GetPsIndex() int
  130. SetIndex(idx int)
  131. GetAlign() int
  132. SetAlign(al int)
  133. ScrollUp()
  134. ScrollDown()
  135. SetIconIndex(idx int)
  136. GetIconIndex() int
  137. Coord() (int, int)
  138. NewCoord(x, y int)
  139. Size() (int, int)
  140. NewSize(w, h int)
  141. UpdateIconNumbers()
  142. GetIconNumbers() int
  143. SetOnShow(on_show bool)
  144. GetOnShow() bool
  145. AppendIcon(it interface{})
  146. ClearIcons()
  147. DrawIcons()
  148. GetMyList() []ListItemInterface
  149. GetName() string
  150. SetName(n string)
  151. GetFootMsg() [5]string
  152. KeyDown(ev *event.Event)
  153. ReturnToUpLevelPage()
  154. OnLoadCb()
  155. OnReturnBackCb()
  156. OnKbdReturnBackCb()
  157. OnExitCb()
  158. // IconClick()
  159. ResetPageSelector()
  160. DrawPageSelector()
  161. ClearCanvas()
  162. Draw()
  163. }
  164. type Page struct {
  165. Widget
  166. Icons []IconItemInterface // slice ,use append
  167. IconNumbers int
  168. IconIndex int
  169. PrevIconIndex int
  170. Ps PageSelectorInterface
  171. PsIndex int
  172. Index int
  173. Align int
  174. CanvasHWND *sdl.Surface
  175. HWND *sdl.Surface
  176. MyList []ListItemInterface
  177. OnShow bool
  178. Name string
  179. Screen *MainScreen
  180. PageIconMargin int // default 20
  181. FootMsg [5]string
  182. SelectedIconTopOffset int
  183. EasingDur int
  184. ScrollStep int
  185. }
  186. func NewPage() *Page {
  187. p := &Page{}
  188. p.PageIconMargin = 20
  189. p.SelectedIconTopOffset = 20
  190. p.EasingDur = 1
  191. p.Align = ALIGN["SLeft"]
  192. p.ScrollStep = 1
  193. p.FootMsg = [5]string{"Nav.", "", "", "", "Enter"}
  194. return p
  195. }
  196. func (self *Page) GetScreen() *MainScreen {
  197. return self.Screen
  198. }
  199. func (self *Page) SetScreen(main_screen *MainScreen) {
  200. self.Screen = main_screen
  201. }
  202. func (self *Page) AdjustHLeftAlign() {
  203. self.PosX = self.Index * self.Screen.Width
  204. self.Width = self.Screen.Width
  205. self.Height = self.Screen.Height
  206. cols := int(Width / IconWidth)
  207. rows := int(self.IconNumbers*IconWidth)/self.Width + 1
  208. cnt := 0
  209. if rows < 1 {
  210. rows = 1
  211. }
  212. for i := 0; i < rows; i++ {
  213. for j := 0; j < cols; j++ {
  214. start_x := IconWidth/2 + j*IconWidth
  215. start_y := IconHeight/2 + i*IconHeight
  216. icon := self.Icons[cnt]
  217. icon.Adjust(start_x, start_y, IconWidth-4, IconHeight-4, 0)
  218. icon.SetIndex(cnt)
  219. icon.SetParent(self)
  220. if cnt >= self.IconNumbers-1 {
  221. break
  222. }
  223. cnt += 1
  224. }
  225. }
  226. ps := NewPageSelector()
  227. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  228. ps.Parent = self
  229. ps.Init(IconWidth/2, TitleBar_BarHeight+IconHeight/2, 92, 92, 128) //hard coded of the blueselector png size
  230. self.Ps = ps
  231. self.PsIndex = 0
  232. self.OnShow = false
  233. }
  234. func (self *Page) AdjustSLeftAlign() { // ## adjust coordinator and append the PageSelector
  235. self.PosX = self.Index * self.Screen.Width
  236. self.Width = self.Screen.Width
  237. self.Height = self.Screen.Height
  238. start_x := (self.PageIconMargin + IconWidth + self.PageIconMargin) / 2
  239. start_y := self.Height / 2
  240. for i := 0; i < self.IconNumbers; i++ {
  241. it := self.Icons[i]
  242. it.SetParent(self)
  243. it.SetIndex(i)
  244. it.Adjust(start_x+i*self.PageIconMargin+i*IconWidth, start_y, IconWidth-6, IconHeight-6, 0)
  245. old_surf := it.GetImgSurf()
  246. it_w, it_h := it.Size() //width height changed by Adjust above
  247. it.SetImgSurf(transform.SmoothScale(old_surf, it_w, it_h))
  248. }
  249. ps := NewPageSelector()
  250. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  251. ps.Parent = self
  252. ps.Init(start_x, start_y, 92, 92, 128)
  253. self.Ps = ps
  254. self.PsIndex = 0
  255. self.OnShow = false
  256. if self.IconNumbers > 1 {
  257. self.PsIndex = 1
  258. self.IconIndex = self.PsIndex
  259. self.PrevIconIndex = self.IconIndex
  260. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  261. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-self.SelectedIconTopOffset)
  262. }
  263. }
  264. func (self *Page) AdjustSAutoLeftAlign() { // ## adjust coordinator and append the PageSelector
  265. self.PosX = self.Index * self.Screen.Width
  266. self.Width = self.Screen.Width
  267. self.Height = self.Screen.Height
  268. start_x := (self.PageIconMargin + IconWidth + self.PageIconMargin) / 2
  269. start_y := self.Height / 2
  270. if self.IconNumbers == 1 {
  271. start_x = self.Width / 2
  272. start_y = self.Height / 2
  273. it := self.Icons[0]
  274. it.SetParent(self)
  275. it.SetIndex(0)
  276. it.Adjust(start_x, start_y, IconWidth, IconHeight, 0)
  277. /*
  278. old_surf := it.GetImgSurf()
  279. it_w,it_h := it.Size()
  280. it.SetImgSurf( transform.SmoothScale(old_surf, it_w,it_h))
  281. */
  282. } else if self.IconNumbers == 2 {
  283. start_x = (self.Width-self.PageIconMargin-self.IconNumbers*IconWidth)/2 + IconWidth/2
  284. start_y = self.Height / 2
  285. for i := 0; i < self.IconNumbers; i++ {
  286. it := self.Icons[i]
  287. it.SetParent(self)
  288. it.SetIndex(i)
  289. it.Adjust(start_x+i*self.PageIconMargin+i*IconWidth, start_y, IconWidth, IconHeight, 0)
  290. /*
  291. old_surf := it.GetImgSurf()
  292. it_w,it_h := it.Size()
  293. it.SetImgSurf( transform.SmoothScale( old_surf, it_w,it_h))
  294. */
  295. }
  296. } else if self.IconNumbers > 2 {
  297. for i := 0; i < self.IconNumbers; i++ {
  298. it := self.Icons[i]
  299. it.SetParent(self)
  300. it.SetIndex(i)
  301. it.Adjust(start_x+i*self.PageIconMargin+i*IconWidth, start_y, IconWidth, IconHeight, 0)
  302. /*
  303. old_surf := it.GetImgSurf()
  304. it_w,it_h := it.Size()
  305. it.SetImgSurf( transform.SmoothScale( old_surf, it_w,it_h))
  306. */
  307. }
  308. }
  309. ps := NewPageSelector()
  310. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  311. ps.Parent = self
  312. ps.Init(start_x, start_y, 92, 92, 128)
  313. self.Ps = ps
  314. self.PsIndex = 0
  315. self.OnShow = false
  316. if self.IconNumbers > 1 {
  317. self.PsIndex = 1
  318. self.IconIndex = self.PsIndex
  319. self.PrevIconIndex = self.IconIndex
  320. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  321. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-self.SelectedIconTopOffset)
  322. }
  323. }
  324. func (self *Page) InitLeftAlign() {
  325. self.PosX = self.Index * self.Screen.Width
  326. self.Width = self.Screen.Width
  327. self.Height = self.Screen.Height
  328. cols := int(self.Width / IconWidth)
  329. rows := int((self.IconNumbers*IconWidth)/self.Width + 1)
  330. if rows < 1 {
  331. rows = 1
  332. }
  333. cnt := 0
  334. start_x := 0
  335. start_y := 0
  336. for i := 0; i < rows; i++ {
  337. for j := 0; j < cols; j++ {
  338. start_x = IconWidth/2 + j*IconWidth
  339. start_y = TitleBar_BarHeight + IconHeight/2 + i*IconHeight
  340. icon := NewIconItem()
  341. icon.Init(start_x, start_y, IconWidth-4, IconHeight-4, 0)
  342. icon.SetIndex(cnt)
  343. icon.SetParent(self)
  344. self.Icons = append(self.Icons, icon)
  345. if cnt >= (self.IconNumbers - 1) {
  346. break
  347. }
  348. cnt += 1
  349. }
  350. }
  351. ps := NewPageSelector()
  352. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  353. ps.Parent = self
  354. ps.Init(IconWidth/2, IconHeight/2, 92, 92, 128)
  355. self.Ps = ps
  356. self.PsIndex = 0
  357. self.OnShow = false
  358. }
  359. func (self *Page) Adjust() { // default init way,
  360. self.PosX = self.Index * self.Screen.Width
  361. self.Width = self.Screen.Width
  362. self.Height = self.Screen.Height
  363. start_x := 0
  364. start_y := 0
  365. if self.Align == ALIGN["HLeft"] {
  366. start_x = (self.Width-self.IconNumbers*IconWidth)/2 + IconWidth/2
  367. start_y = self.Height / 2
  368. for i := 0; i < self.IconNumbers; i++ {
  369. self.Icons[i].SetParent(self)
  370. self.Icons[i].SetIndex(i)
  371. self.Icons[i].Adjust(start_x+i*IconWidth, start_y, IconWidth, IconHeight, 0)
  372. }
  373. ps := NewPageSelector()
  374. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  375. ps.Parent = self
  376. ps.Init(start_x, start_y, 92, 92, 128)
  377. self.Ps = ps
  378. self.PsIndex = 0
  379. self.OnShow = false
  380. } else if self.Align == ALIGN["SLeft"] {
  381. start_x = (self.PageIconMargin + IconWidth + self.PageIconMargin) / 2
  382. start_y = self.Height / 2
  383. for i := 0; i < self.IconNumbers; i++ {
  384. it := self.Icons[i]
  385. it.SetParent(self)
  386. it.SetIndex(i)
  387. it.Adjust(start_x+i*self.PageIconMargin+i*IconWidth, start_y, IconWidth, IconHeight, 0)
  388. }
  389. ps := NewPageSelector()
  390. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  391. ps.Parent = self
  392. ps.Init(start_x, start_y-self.SelectedIconTopOffset, 92, 92, 128)
  393. self.Ps = ps
  394. self.PsIndex = 0
  395. self.OnShow = false
  396. if self.IconNumbers > 1 {
  397. self.PsIndex = 1
  398. self.IconIndex = self.PsIndex
  399. self.PrevIconIndex = self.IconIndex
  400. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  401. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-self.SelectedIconTopOffset)
  402. }
  403. }
  404. }
  405. func (self *Page) GetOnShow() bool {
  406. return self.OnShow
  407. }
  408. func (self *Page) SetOnShow(on_show bool) {
  409. self.OnShow = on_show
  410. }
  411. func (self *Page) UpdateIconNumbers() {
  412. self.IconNumbers = len(self.Icons)
  413. }
  414. func (self *Page) GetIconNumbers() int {
  415. return self.IconNumbers
  416. }
  417. func (self *Page) Init() {
  418. if self.Screen != nil {
  419. if self.Screen.CanvasHWND != nil && self.CanvasHWND == nil {
  420. self.CanvasHWND = self.Screen.CanvasHWND
  421. }
  422. }
  423. self.PosX = self.Index * self.Screen.Width
  424. self.Width = self.Screen.Width
  425. self.Height = self.Screen.Height
  426. start_x := (self.Width-self.IconNumbers*IconWidth)/2 + IconWidth/2
  427. start_y := self.Height / 2
  428. for i := 0; i < self.IconNumbers; i++ {
  429. it := NewIconItem()
  430. it.SetParent(self)
  431. it.SetIndex(i)
  432. it.Init(start_x+i*IconWidth, start_y, IconWidth, IconHeight, 0)
  433. self.Icons = append(self.Icons, it)
  434. }
  435. if self.IconNumbers > 0 {
  436. ps := NewPageSelector()
  437. ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
  438. ps.Parent = self
  439. ps.Init(start_x, start_y, IconWidth+4, IconHeight+4, 128)
  440. self.Ps = ps
  441. self.PsIndex = 0
  442. self.OnShow = false
  443. }
  444. }
  445. func (self *Page) IconStepMoveData(icon_eh, cuts int) []int { // no Sine,No curve,plain movement steps data
  446. var all_pieces []int
  447. piece := float32(icon_eh / cuts)
  448. c := float32(0.0)
  449. prev := float32(0.0)
  450. for i := 0; i < cuts; i++ {
  451. c += piece
  452. dx := c - prev
  453. if dx < 0.5 {
  454. dx = float32(1.0)
  455. }
  456. dx += 0.9
  457. all_pieces = append(all_pieces, int(dx))
  458. if c >= float32(icon_eh) {
  459. break
  460. }
  461. }
  462. c = 0.0
  463. bidx := 0
  464. for _, v := range all_pieces {
  465. c += float32(v)
  466. bidx += 1
  467. if c >= float32(icon_eh) {
  468. break
  469. }
  470. }
  471. all_pieces = all_pieces[0:bidx]
  472. if len(all_pieces) < cuts {
  473. dff := cuts - len(all_pieces)
  474. var diffa []int
  475. for i := 0; i < dff; i++ {
  476. diffa = append(diffa, 0)
  477. }
  478. all_pieces = append(all_pieces, diffa...)
  479. }
  480. return all_pieces
  481. }
  482. func (self *Page) EasingData(start, distance int) []int {
  483. current_time := float32(0.0)
  484. start_posx := float32(0.0)
  485. current_posx := start_posx
  486. final_posx := float32(distance)
  487. // posx_init := start
  488. dur := self.EasingDur
  489. last_posx := float32(0.0)
  490. var all_last_posx []int
  491. for i := 0; i < distance*dur; i++ {
  492. current_posx = float32(easings.SineIn(float32(current_time), float32(start_posx), float32(final_posx-start_posx), float32(dur)))
  493. if current_posx >= final_posx {
  494. current_posx = final_posx
  495. }
  496. dx := current_posx - last_posx
  497. all_last_posx = append(all_last_posx, int(dx))
  498. current_time += 1.0
  499. last_posx = current_posx
  500. if current_posx >= final_posx {
  501. break
  502. }
  503. }
  504. c := 0
  505. for _, v := range all_last_posx {
  506. c += v
  507. }
  508. if c < int(final_posx-start_posx) {
  509. all_last_posx = append(all_last_posx, int(int(final_posx)-c))
  510. }
  511. return all_last_posx
  512. }
  513. func (self *Page) IconSmoothUp(icon_ew int) {
  514. data := self.EasingData(self.PosX, icon_ew)
  515. data2 := self.IconStepMoveData(self.SelectedIconTopOffset, len(data))
  516. for i, _ := range data {
  517. self.ClearCanvas()
  518. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  519. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-data2[i])
  520. prev_icon_x, prev_icon_y := self.Icons[self.PrevIconIndex].Coord()
  521. if prev_icon_y < self.Height/2 {
  522. self.Icons[self.PrevIconIndex].NewCoord(prev_icon_x, prev_icon_y+data2[i])
  523. self.DrawIcons()
  524. self.Screen.SwapAndShow()
  525. }
  526. }
  527. }
  528. func (self *Page) IconsEasingLeft(icon_ew int) {
  529. data := self.EasingData(self.PosX, icon_ew)
  530. data2 := self.IconStepMoveData(self.SelectedIconTopOffset, len(data))
  531. for i, v := range data {
  532. self.ClearCanvas()
  533. self.PosX -= v
  534. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  535. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-data2[i])
  536. prev_icon_x, prev_icon_y := self.Icons[self.PrevIconIndex].Coord()
  537. if prev_icon_y < self.Height/2 {
  538. self.Icons[self.PrevIconIndex].NewCoord(prev_icon_x, prev_icon_y+data2[i])
  539. }
  540. self.DrawIcons()
  541. self.Screen.SwapAndShow()
  542. }
  543. }
  544. func (self *Page) IconsEasingRight(icon_ew int) {
  545. data := self.EasingData(self.PosX, icon_ew)
  546. data2 := self.IconStepMoveData(self.SelectedIconTopOffset, len(data))
  547. for i, v := range data {
  548. self.ClearCanvas()
  549. self.PosX += v
  550. cur_icon_x, cur_icon_y := self.Icons[self.IconIndex].Coord()
  551. self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y-data2[i])
  552. prev_icon_x, prev_icon_y := self.Icons[self.PrevIconIndex].Coord()
  553. if prev_icon_y < self.Height/2 {
  554. self.Icons[self.PrevIconIndex].NewCoord(prev_icon_x, prev_icon_y+data2[i])
  555. }
  556. self.DrawIcons()
  557. self.Screen.SwapAndShow()
  558. }
  559. }
  560. func (self *Page) EasingLeft(ew int) {
  561. data := self.EasingData(self.PosX, ew)
  562. for _, i := range data {
  563. self.PosX -= i
  564. self.Draw()
  565. self.Screen.SwapAndShow()
  566. }
  567. }
  568. func (self *Page) EasingRight(ew int) {
  569. data := self.EasingData(self.PosX, ew)
  570. for _, i := range data {
  571. self.PosX += i
  572. self.Draw()
  573. self.Screen.SwapAndShow()
  574. }
  575. }
  576. func (self *Page) MoveLeft(ew int) {
  577. self.PosX -= ew
  578. }
  579. func (self *Page) MoveRight(ew int) {
  580. self.PosX += ew
  581. }
  582. func (self *Page) ResetPageSelector() {
  583. self.PsIndex = 0
  584. self.IconIndex = 0
  585. self.Ps.SetOnShow(true)
  586. }
  587. func (self *Page) DrawPageSelector() {
  588. if self.Ps.GetOnShow() == true {
  589. // fmt.Println("DrawPageSelector")
  590. self.Ps.Draw()
  591. }
  592. }
  593. func (self *Page) MoveIconIndexPrev() bool {
  594. self.IconIndex -= 1
  595. if self.IconIndex < 0 {
  596. self.IconIndex = 0
  597. self.PrevIconIndex = self.IconIndex
  598. return false
  599. }
  600. self.PrevIconIndex = self.IconIndex + 1
  601. return true
  602. }
  603. func (self *Page) MoveIconIndexNext() bool {
  604. self.IconIndex += 1
  605. if self.IconIndex > (self.IconNumbers - 1) {
  606. self.IconIndex = self.IconNumbers - 1
  607. self.PrevIconIndex = self.IconIndex
  608. return false
  609. }
  610. self.PrevIconIndex = self.IconIndex - 1
  611. return true
  612. }
  613. func (self *Page) IconClick() {
  614. if self.IconIndex > (len(self.Icons) - 1) {
  615. return
  616. }
  617. cur_icon := self.Icons[self.IconIndex]
  618. if self.Ps.GetOnShow() == false {
  619. return
  620. }
  621. if cur_icon.GetMyType() == ICON_TYPES["EXE"] {
  622. fmt.Printf("IconClick: %s %d", cur_icon.GetCmdPath(), cur_icon.GetIndex())
  623. self.Screen.RunEXE(cur_icon.GetCmdPath())
  624. return
  625. }
  626. if cur_icon.GetMyType() == ICON_TYPES["DIR"] {
  627. child_page := cur_icon.GetLinkPage()
  628. if child_page != nil {
  629. self.Screen.PushPage(child_page)
  630. child_page.Draw()
  631. }
  632. return
  633. }
  634. if cur_icon.GetMyType() == ICON_TYPES["FUNC"] || cur_icon.GetMyType() == ICON_TYPES["Emulator"] {
  635. invoker := cur_icon.GetCmdInvoke()
  636. if invoker != nil {
  637. invoker.Run(self.Screen)
  638. }
  639. return
  640. }
  641. }
  642. func (self *Page) ReturnToUpLevelPage() {
  643. pop_page := self.Screen.MyPageStack.Pop()
  644. if pop_page != nil {
  645. page_ := pop_page.(PageInterface)
  646. page_.Draw()
  647. self.Screen.CurrentPage = page_
  648. self.Screen.CurrentPage.OnReturnBackCb()
  649. } else {
  650. if self.Screen.MyPageStack.Length() == 0 {
  651. if len(self.Screen.Pages) > 0 {
  652. if self.Screen.PageIndex < len(self.Screen.Pages) {
  653. self.Screen.CurrentPage = self.Screen.Pages[self.Screen.PageIndex]
  654. self.Screen.CurrentPage.Draw()
  655. fmt.Println("OnTopLevel", self.Screen.PageIndex)
  656. }
  657. }
  658. }
  659. }
  660. }
  661. func (self *Page) ClearCanvas() {
  662. surface.Fill(self.CanvasHWND, self.Screen.SkinManager.GiveColor("White"))
  663. }
  664. func (self *Page) AppendIcon(it interface{}) {
  665. self.Icons = append(self.Icons, it.(IconItemInterface))
  666. }
  667. func (self *Page) GetIcons() []IconItemInterface {
  668. return self.Icons
  669. }
  670. func (self *Page) ClearIcons() {
  671. for i := 0; i < self.IconNumbers; i++ {
  672. self.Icons[i].Clear()
  673. }
  674. }
  675. func (self *Page) DrawIcons() {
  676. for i := 0; i < self.IconNumbers; i++ {
  677. self.Icons[i].Draw()
  678. }
  679. }
  680. func (self *Page) GetMyList() []ListItemInterface {
  681. return self.MyList
  682. }
  683. func (self *Page) KeyDown(ev *event.Event) {
  684. if ev.Data["Key"] == CurKeys["A"] {
  685. if self.FootMsg[3] == "Back" {
  686. self.ReturnToUpLevelPage()
  687. self.Screen.Draw()
  688. self.Screen.SwapAndShow()
  689. return
  690. }
  691. }
  692. if ev.Data["Key"] == CurKeys["Menu"] {
  693. self.ReturnToUpLevelPage()
  694. self.Screen.Draw()
  695. self.Screen.SwapAndShow()
  696. }
  697. if ev.Data["Key"] == CurKeys["Right"] {
  698. if self.MoveIconIndexNext() == true {
  699. if self.IconIndex == (self.IconNumbers-1) || self.PrevIconIndex == 0 {
  700. self.IconSmoothUp(IconWidth + self.PageIconMargin)
  701. } else {
  702. self.IconsEasingLeft(IconWidth + self.PageIconMargin)
  703. }
  704. self.PsIndex = self.IconIndex
  705. self.Screen.Draw()
  706. self.Screen.SwapAndShow()
  707. }
  708. }
  709. if ev.Data["Key"] == CurKeys["Left"] {
  710. if self.MoveIconIndexPrev() == true {
  711. if self.IconIndex == 0 || self.PrevIconIndex == (self.IconNumbers-1) {
  712. self.IconSmoothUp(IconWidth + self.PageIconMargin)
  713. } else {
  714. self.IconsEasingRight(IconWidth + self.PageIconMargin)
  715. }
  716. self.PsIndex = self.IconIndex
  717. self.Screen.Draw()
  718. self.Screen.SwapAndShow()
  719. }
  720. }
  721. if ev.Data["Key"] == CurKeys["Enter"] {
  722. self.IconClick()
  723. self.Screen.Draw()
  724. self.Screen.SwapAndShow()
  725. }
  726. }
  727. func (self *Page) OnLoadCb() {
  728. }
  729. func (self *Page) OnKbdReturnBackCb() {
  730. }
  731. func (self *Page) OnReturnBackCb() {
  732. }
  733. func (self *Page) OnExitCb() {
  734. }
  735. func (self *Page) Draw() {
  736. self.ClearCanvas()
  737. self.DrawIcons()
  738. self.DrawPageSelector()
  739. }
  740. func (self *Page) GetFootMsg() [5]string {
  741. return self.FootMsg
  742. }
  743. func (self *Page) SetFootMsg(footmsg [5]string) {
  744. self.FootMsg = footmsg
  745. }
  746. func (self *Page) GetCanvasHWND() *sdl.Surface {
  747. return self.CanvasHWND
  748. }
  749. func (self *Page) SetCanvasHWND(canvas *sdl.Surface) {
  750. self.CanvasHWND = canvas
  751. }
  752. func (self *Page) GetHWND() *sdl.Surface {
  753. return self.HWND
  754. }
  755. func (self *Page) SetHWND(h *sdl.Surface) {
  756. self.HWND = h
  757. }
  758. func (self *Page) SetPsIndex(idx int) {
  759. self.PsIndex = idx
  760. }
  761. func (self *Page) GetPsIndex() int {
  762. return self.PsIndex
  763. }
  764. func (self *Page) SetIconIndex(idx int) {
  765. self.IconIndex = idx
  766. }
  767. func (self *Page) GetIconIndex() int {
  768. return self.IconIndex
  769. }
  770. func (self *Page) GetName() string {
  771. return self.Name
  772. }
  773. func (self *Page) SetName(n string) {
  774. self.Name = n
  775. }
  776. func (self *Page) SetIndex(idx int) {
  777. self.Index = idx
  778. }
  779. func (self *Page) SetAlign(al int) {
  780. inthere := false
  781. for _, v := range ALIGN {
  782. if v == al {
  783. inthere = true
  784. break
  785. }
  786. }
  787. if inthere {
  788. self.Align = al
  789. }
  790. }
  791. func (self *Page) GetAlign() int {
  792. return self.Align
  793. }
  794. func (self *Page) ScrollUp() {
  795. if len(self.MyList) == 0 {
  796. return
  797. }
  798. self.PsIndex -= 1
  799. if self.PsIndex < 0 {
  800. self.PsIndex = 0
  801. }
  802. cur_li := self.MyList[self.PsIndex]
  803. x, y := cur_li.Coord()
  804. _, h := cur_li.Size()
  805. if y < 0 {
  806. for i, _ := range self.MyList {
  807. x, y = self.MyList[i].Coord()
  808. _, h = self.MyList[i].Size()
  809. self.MyList[i].NewCoord(x, y+h)
  810. }
  811. //self.Scrolled +=1
  812. }
  813. }
  814. func (self *Page) ScrollDown() {
  815. if len(self.MyList) == 0 {
  816. return
  817. }
  818. self.PsIndex += 1
  819. if self.PsIndex >= len(self.MyList) {
  820. self.PsIndex = len(self.MyList) - 1
  821. }
  822. cur_li := self.MyList[self.PsIndex]
  823. x, y := cur_li.Coord()
  824. _, h := cur_li.Size()
  825. if y+h > self.Height {
  826. for i, _ := range self.MyList {
  827. x, y = self.MyList[i].Coord()
  828. _, h = self.MyList[i].Size()
  829. self.MyList[i].NewCoord(x, y-h)
  830. }
  831. // self.Scrolled -=1
  832. }
  833. }
  834. func (self *Page) FastScrollUp(step int) {
  835. if len(self.MyList) == 0 {
  836. return
  837. }
  838. if step < 1 {
  839. step = 1
  840. }
  841. tmp := self.PsIndex
  842. self.PsIndex -= step
  843. if self.PsIndex < 0 {
  844. self.PsIndex = 0
  845. }
  846. dy := tmp - self.PsIndex
  847. cur_li := self.MyList[self.PsIndex]
  848. x, y := cur_li.Coord()
  849. _, h := cur_li.Size()
  850. if y < 0 {
  851. for i, _ := range self.MyList {
  852. x, y = self.MyList[i].Coord()
  853. _, h = self.MyList[i].Size()
  854. self.MyList[i].NewCoord(x, y+h*dy)
  855. }
  856. //self.Scrolled +=1
  857. }
  858. }
  859. func (self *Page) FastScrollDown(step int) {
  860. if len(self.MyList) == 0 {
  861. return
  862. }
  863. if step < 1 {
  864. step = 1
  865. }
  866. tmp := self.PsIndex
  867. self.PsIndex += step
  868. if self.PsIndex >= len(self.MyList) {
  869. self.PsIndex = len(self.MyList) - 1
  870. }
  871. dy := self.PsIndex - tmp
  872. cur_li := self.MyList[self.PsIndex]
  873. x, y := cur_li.Coord()
  874. _, h := cur_li.Size()
  875. if y+h > self.Height {
  876. for i, _ := range self.MyList {
  877. x, y = self.MyList[i].Coord()
  878. _, h = self.MyList[i].Size()
  879. self.MyList[i].NewCoord(x, y-h*dy)
  880. }
  881. // self.Scrolled -=1
  882. }
  883. }