AdjustSAutoLeftAlign keep going

This commit is contained in:
cuu
2018-06-12 21:49:54 +08:00
parent cd4c65ec8a
commit f8aaab26fa
6 changed files with 569 additions and 22 deletions

View File

@@ -9,6 +9,23 @@ import (
"github.com/cuu/gogame/utils"
)
type IconItemInterface interface {
Init(x,y,w,h,at int)
SetIndex(i int)
SetParent( p interface{} )
SetLabelColor(col *color.Color)
Coord() (int,int)
NewCoord(x,y int)
Size() (int,int)
AddLabel(text string, fontobj *ttf.Font)
AdjustLinkPage()
GetImageSurf() *sdl.Surface
SetImageSurf(newsurf *sdl.Surface)
CreateImageSurf()
ChangeImgSurfColor(col *color.Color)
Draw()
}
type IconItem struct {
PosX int
PosY int
@@ -41,7 +58,7 @@ func NewIconItem() *IconItem {
}
func (self *IconItem) Init(x,y,w,h,at) {
func (self *IconItem) Init(x,y,w,h,at int) {
self.PosX = x
self.PosY = y
self.Width = w
@@ -49,15 +66,31 @@ func (self *IconItem) Init(x,y,w,h,at) {
self.AnimationTime = at
}
func (self *IconItem) SetIndex(i int) {
self.Index = i
}
func (self *IconItem) SetParent(p interface{} ) {
self.Parent = p
}
func (self *IconItem) SetLabelColor(col *color.Color) {
self.Label.SetColor(col)
}
func (self *IconItem) Coord() (int,int) {
return self.PosX,self.PosY
}
func (self *IconItem) NewCoord(x,y int) {
self.PosX = x
self.PosY = y
}
func (self *IconItem) NewCoord(x,y int) {
return self.Width,self.Height
}
func (self *IconItem) AddLabel(text string, fontobj *ttf.Font) {
if self.Label == nil {
l:= NewLabel()
@@ -87,6 +120,16 @@ func (self *IconItem) AdjustLinkPage() {
}
}
func (self *IconItem) GetImageSurf() *sdl.Surface {
return self.ImgSurf
}
func (self *IconItem) SetImageSurf(newsurf *sdl.Surface) {
self.ImgSurf = newsurf
}
func (self *IconItem) CreateImageSurf() {
if self.ImgSurf == nil && self.ImageName != "" {
self.ImgSurf = image.Load(self.ImageName)

View File

@@ -0,0 +1,57 @@
package UI
import (
"io/ioutil"
"log"
"strings"
"github.com/veandco/go-sdl2/sdl"
"github.com/cuu/gogame/image"
)
type IconPool struct {
GameShellIconPath string
Icons map[string]*sdl.Surface
}
func NewIconPool() *IconPool {
i := &IconPool{}
i.GameShellIconPath = SkinMap("gameshell/icons/")
i.Icons = make( map[string]*sdl.Surface )
return i
}
func (self *IconPool) Init() {
files,err := ioutil.ReadDir(self.GameShellIconPath)
if err != nil {
log.Fatal(err)
return
}
for _,f := range files {
if f.IsDir() {
//pass
}else {
if strings.HasSuffix(f.Name(),".png") == true {
keyname := strings.Split(f.Name(),".")
if len(keyname) > 1 {
self.Icons[ keyname[0] ] = image.Load( self.GameShellIconPath+ "/"+f.Name() )
}
}
}
}
}
func (self *IconPool) GetImgSurf(keyname string) *sdl.Surface {
if val,ok := self.Icons[keyname]; ok {
return self.Icons[keyname]
} else {
return nil
}
}
var MyIconPool = NewIconPool()

View File

@@ -18,18 +18,36 @@ func NewMultiIconItem() *MultiIconItem {
m.IconIndex = 0
m.IconWidth = 18
m.IconHeight = 18
return m
}
func (m * MultiIconItem) CreateImageSurf() {
if m.ImgSurf == nil and m.ImageName != "" {
m.ImgSurf = image.Load(m.ImageName)
func (self * MultiIconItem) CreateImageSurf() {
if self.ImgSurf == nil && self.ImageName != "" {
self.ImgSurf = image.Load(self.ImageName)
}
}
func (m *MultiIconItem) Draw() {
func (self *MultiIconItem) Draw() {
parent_x,parent_y := self.Parent.Coord()
if self.Label != nil {
// lab_x,lab_y := self.Label.Coord()
lab_w,lab_h:= self.Label.Size()
if self.Align == ALIGN["VCenter"] {
self.Label.NewCoord( self.PosX - lab_w/2 + parent_x, self.PosY + self.Height/2+6 + parent_y)
}else if self.Align == ALIGN["HLeft"] {
self.Label.NewCoord( self.PosX + self.Width/2+3 + parent_x, self.PosY - lab_h/2 + parent_y )
}
self.Label.Draw()
}
if self.ImgSurf != nil {
portion := rect.Rect(0,self.IconIndex*self.IconHeight,self.IconWidth,self.IconHeight)
surface.Blit(self.Parent.GetCanvasHWND(),
self.ImgSurf,draw.MidRect(self.PosX + parent_x, self.PosY + parent_y,
self.Width,self.Height, Width, Height),&portion)
}
}

View File

@@ -5,6 +5,8 @@ import (
"github.com/veandco/go-sdl2/sdl"
"github.com/cuu/gogame/font"
)
type element struct {
@@ -45,6 +47,10 @@ func (stk *PageStack) Pop() interface{} {
return r
}
func (stk *PageStack) Length() int {
return stk.Size
}
func NewPageStack() *PageStack {
stk := new(PageStack)
stk.lock = &sync.Mutex{}
@@ -52,29 +58,67 @@ func NewPageStack() *PageStack {
}
type PageSelectorInterface interface {
Adjust(x,y,w,h,alpha int)
Draw()
}
type PageSelector struct {
PosX int
PosY int
Width int
Height int
Parent interface{} //
Parent PageInterface
Alpha int
OnShow bool
IconSurf *sdl.Surface
}
func (p *PageSelector) Adjust(x,y,w,h,alpha int) {
p.PosX = x
p.PosY = y
p.Width = w
p.Height = h
p.Alpha = alpha
func NewPageSelector() *PageSelector {
p := &PageSelector{}
return p
}
func (p *PageSelector) Draw() {
func (self *PageSelector) Init(x,y,w,h,alpha int) {
self.Adjust(x,y,w,h,alpha)
}
func (self *PageSelector) Adjust(x,y,w,h,alpha int) {
self.PosX = x
self.PosY = y
self.Width = w
self.Height = h
self.Alpha = alpha
}
func (self *PageSelector) Draw() {
canvas := self.Parent.GetCanvasHWND()
idx := self.Parent.GetPsIndex()
iconidx := self.Parent.GetIconIndex()
icons := self.Parent.GetIcons()
if idx < len(icons) {
icon_x ,_ := icons[idx].Coord()
_,icon_y := icons[iconidx].Coord()
parent_x,parent_y := self.Parent.Coord()
parent_w,parent_h := self.Parent.Size()
x := icon_x + parent_x
y := icon_y // only use current icon's PosY
rect_ = draw.MidRect(x,y, self.Width, self.Height, parent_w,parent_h)
if rect_.W <=0 || rect_.H <= 0 {
return
}
if self.IconSurf != nil {
surface.Blit(canvas,self.IconSurf, rect_,nil)
}
}
}
@@ -96,20 +140,21 @@ type PageInterface interface {
// GetIconIndex
// Coord
// Size
}
type Page struct {
PosX int
PosY int
Width int
Height int
Icons []interface{} // make first
Icons []IconItemInterface // slice ,use append
IconNumbers int
IconIndex int
PrevIconIndex int
Ps interface{}
Ps PageSelectorInterface
PsIndex int
Index int
@@ -120,7 +165,6 @@ type Page struct {
HWND *sdl.Surface
OnShow bool
Name string
Screen *MainScreen
@@ -136,14 +180,101 @@ func NewPage() *Page {
p.PageIconMargin = 20
p.SelectedIconTopOffset = 20
p.EasingDur = 30
p.Align = ALIGN["SLeft"]
p.FootMsg = [5]string{"Nav.","","","","Enter"}
return p
}
func (self *Page) AdjustHLeftAlign() {
self.PosX = self.Index*self.Screen.Width
self.Width = self.Screen.Width
self.Height = self.Screen.Height
cols := int(Width/IconWidth)
rows := int( self.IconNumbers * IconWidth) / self.Width + 1
cnt := 0
if rows < 1 {
rows = 1
}
for i := 0; i < rows; i++ {
for j := 0; j < cols; j++ {
start_x := IconWidth/2 + j*IconWidth
start_y := IconHeight/2 + i*IconHeight
icon := self.Icons[cnt]
icon.Adjust(start_x,start_y,IconWidth-4,IconHeight-4,0)
icon.SetIndex(cnt)
icon.SetParent(self)
if cnt >= self.IconNumbers -1 {
break
}
cnt += 1
}
}
ps := NewPageSelector()
ps.IconSurf = MyIconPool.GetImgSurf("blueselector")
ps.Parent = self
ps.Init(IconWidth/2,TitleBar_BarHeight+IconHeight/2, 92,92,128) //hard coded of the blueselector png size
self.Ps = ps
self.PsIndex = 0
self.OnShow = false
}
func (self *Page) AdjustSLeftAlign() { // ## adjust coordinator and append the PageSelector
self.PosX = self.Index * self.Screen.Width
self.Width = self.Screen.Width
self.Height = self.Screen.Height
start_x := (self.PageIconMargin + IconWidth + self.PageIconMargin ) / 2
start_y := self.Height/2
for i := 0; i < self.IconNumbers; i++ {
it := self.Icons[i]
it.SetParent(self)
it.SetIndex(i)
it.Adjust(start_x+i*self.PageIconMargin+i*IconWidth, start_y, IconWidth-6,IconHeight-6,0)
old_surf := it.GetImageSurf()
it_w,it_h := it.Size() //width height changed by Adjust above
it.SetImageSurf( transform.SmoothScale(old_surf,it_w,it_h) )
}
ps := NewPageSelector()
ps.IconSurf = MyIconPool.GetImageSurf("blueselector")
ps.Parent = self
ps.Init(start_x,start_y,92,92,128)
self.Ps = ps
self.PsIndex = 0
self.OnShow = false
if self.IconNumbers > 1 {
self.PsIndex = 1
self.IconIndex = self.PsIndex
self.PrevIconIndex = self.IconIndex
cur_icon_x,cur_icon_y := self.Icons[self.IconIndex].Coord()
self.Icons[self.IconIndex].NewCoord(cur_icon_x, cur_icon_y - self.SelectedIconTopOffset )
}
}
func (self *Page) AdjustSAutoLeftAlign() { // ## adjust coordinator and append the PageSelector
self.PosX = self.Index * self.Screen.Width
self.Width = self.Screen.Width
self.Height = self.Screen.Height
start_x := (self.PageIconMargin + IconWidth + self.PageIconMargin ) / 2
start_y := self.Height/2
}