page.go 22 KB

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