page.go 20 KB

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