page.go 20 KB

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