page.go 20 KB

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