From 982a7b42a2dcdbf0eda368abd691625b42a71ccc Mon Sep 17 00:00:00 2001 From: cuu Date: Thu, 11 Oct 2018 03:19:26 +0800 Subject: [PATCH] keyboard --- sysgo/UI/keyboard.go | 401 +++++++++++++++++++++++++++++++++++++++++- sysgo/UI/keys_def.go | 7 + sysgo/UI/text_item.go | 20 +++ 3 files changed, 422 insertions(+), 6 deletions(-) diff --git a/sysgo/UI/keyboard.go b/sysgo/UI/keyboard.go index ba5a4d7..ca3d561 100644 --- a/sysgo/UI/keyboard.go +++ b/sysgo/UI/keyboard.go @@ -5,18 +5,22 @@ import ( "github.com/cuu/gogame/draw" "github.com/cuu/gogame/surface" "github.com/cuu/gogame/color" + "github.com/cuu/gogame/event" + + + "github.com/cuu/LauncherGo/sysgo/easings" + ) //sysgo/UI/keyboard_keys.layout type KeyboardIcon struct { - IconItem - Color *color.Color - - Str string + TextItem // IconItem->TextItem->KeyboardIcon } func NewKeyboardIcon() *KeyboardIcon { p := &KeyboardIcon{} + p.Color = &color.Color{83,83,83,255}//SkinManager().GiveColor('Text') + p.MyType = ICON_TYPES["NAV"] return p @@ -44,12 +48,33 @@ func NewKeyboardSelector() * KeyboardSelector { } func (self *KeyboardSelector) Draw() { + sec_idx := self.Parent.SectionIndex + row_idx := self.Parent.RowIndex + idx := self.Parent.PsIndex + + x := self.Parent.SecsKeys[sec_idx][row_idx][idx].PosX + y := self.Parent.SecsKeys[sec_idx][row_idx][idx].PosY + w := self.Parent.SecsKeys[sec_idx][row_idx][idx].Width+6 + h := self.Parent.SecsKeys[sec_idx][row_idx][idx].Height+1 + + rect_ := draw.MidRect(x,y,w,h,self.Parent.Width,self.Parent.Height) + + if rect_.W <= 0 || rect_.H <= 0 { + return + } + color_ := &color.Color{126,206,244,255} + draw.AARoundRect(self.Parent.CanvasHWND,rect_,color_,3,0,color_) + } type Keyboard struct { Page + Secs map[int][][]string + + SecsKeys map[int][][]TextItemInterface + SectionNumbers int SectionIndex int Icons map[string]IconItemInterface @@ -69,6 +94,8 @@ type Keyboard struct { func NewKeyboard() *Keyboard { p := &Keyboard{} + p.EasingDur = 10 + p.SectionNumbers = 3 p.SectionIndex = 1 @@ -79,6 +106,12 @@ func NewKeyboard() *Keyboard { p.RowIndex = 0 p.FootMsg = [5]string{"Nav.","ABC","Done","Backspace","Enter"} + + p.Secs = make(map[int][][]string) + p.SecsKeys = make(map[int][][]TextItemInterface) + + p.KeyboardLayoutFile = "sysgo/UI/keyboard_keys.layout" + return p @@ -86,12 +119,368 @@ func NewKeyboard() *Keyboard { func (self *Keyboard) ReadLayoutFile( fname string) { - /* + LayoutIndex := 0 content ,err := ReadLines(fname) - */ + + Assert(err) + + var tmp [][]string + for i, v := range content { + content[i] = strings.TrimSpace(v) + + stmp := strings.Split(content[i], " ") + for j, u := range stmp { + stmp[j] = strings.TrimSpace(u) + } + + tmp = append(tmp, stmp) + } + + for _, v := range tmp { + if len(v) > 2 { + + if _, ok := self.Secs[LayoutIndex]; ok { + self.Secs[LayoutIndex] = append(self.Secs[LayoutIndex], v) + } else { + + self.Secs[LayoutIndex] = [][]string{} + self.Secs[LayoutIndex] = append(self.Secs[LayoutIndex], v) + + } + + } else { //empty [] + LayoutIndex += 1 + } + } +} + + +func (self *Keyboard) SetPassword(pwd string) { + pwd_seq_list := strings.SplitAfter(pwd,"") + + self.Textarea.ResetMyWords() + for _,v := range pwd_seq_list { + self.Textarea.AppendText(v) + } +} + + +func (self *Keyboard) Init() { + self.CanvasHWND = self.Screen.CanvasHWND + self.ReadLayoutFile(self.KeyboardLayoutFile) //assign to self.Secs + + self.SectionNumbers = len(self.Secs) + + self.PosX = self.Index * self.Screen.Width + self.Width = self.Screen.Width + self.Height = self.Screen.Height + + fontobj := Fonts["veramono24"] + + word_margin := 15 + + secs_zero := strings.Join(self.Secs[0][0],"") + fw,_:= font.Size(fontobj,secs_zero) + + start_x := (self.Width - fw - len(self.Secs[0][0])*word_margin)/2+word_margin/2 + start_y := 0 + +// cnt := 0 + + for i:=0; i < self.SectionNumbers; i++ { + self.SecsKeys[i] = [][]TextItemInterface{} + for j:=0; j < len(self.Secs[i]); j++ { + self.SecsKeys[i] = append(self.SecsKeys[i],[]TextItemInterface{}) + secs_ij := strings.Join(self.Secs[i][j],"") + fw,_ := font.Size(fontobj,secs_ij) + start_x = (self.Width-fw- len(self.Secs[i][j])*word_margin)/2+word_margin/2 + start_x = start_x + i*self.Width + + start_y = 84 * j * (word_margin+14) + + for idx,val := range self.Secs[i][j] { + ti := NewTextItem() + ti.FontObj = fontobj + ti.Parent = self + + if val == "_L" || val == "_R" { + it := NewKeyboardIcon() + it.ImgSurf = MyIconPool.GetImgSurf(val) + it.Parent = self + it.Str = val + it.Init(start_x+surface.GetWidth(it.ImgSurf)/2,start_y,surface.GetWidth(it.ImgSurf),surface.GetHeight(it.ImgSurf),0) + self.SecsKeys[i][j] = append(self.SecsKeys[i][j],it) + self.IconNumbers += 1 + start_x = start_x + surface.GetWidth(it.ImgSurf)+word_margin + }else { + + if val == "_S" { + val = "Space" + ti.FontObj = Fonts["veramono15"] + ti.Bold = true + } + + cur_alpha_w,cur_alpha_h := font.Size(ti.FontObj,val) + ti.Init(start_x + cur_alpha_w/2,start_y,cur_alpha_w,cur_alpha_h,0) + ti.Str = val + start_x = start_x + cur_alpha_w+word_margin // prepare for next alphabet + self.SecsKeys[i][j] = append(self.SecsKeys[i][j],ti) + } + } + } + } + + self.SectionIndex = 0 + + self.Textarea = NewTextarea() + + self.Textarea.PosX = 4 + self.Textarea.PosY = 4 + self.Textarea.Width = self.Width - 4*2 + self.Textarea.Height = 60 + + self.Textarea.CanvasHWND = self.CanvasHWND + self.Textarea.Init() + + ps := NewKeyboardSelector() + + ps.Parent = self + ps.Init(start_x,start_y,25,25,128) + self.Ps = ps + self.PsIndex = 0 + self.Ps.OnShow = true + +} + +func (self *Keyboard) SelectUpChar() { + sec_idx := self.SectionIndex + + self.RowIndex -=1 + if self.RowIndex < 0 { + self.RowIndex = len(self.SecsKeys[sec_idx])-1 + } + + if self.PsIndex >= len(self.SecsKeys[sec_idx][self.RowIndex]) { + self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex])-1 + } + + self.ClearCanvas() + self.Draw() + self.Screen.SwapAndShow() +} + +func (self *Keyboard) SelectDownChar() { + sec_idx := self.SectionIndex + + self.RowIndex += 1 + + if self.RowIndex >= len(self.SecsKeys[sec_idx]) { + self.RowIndex = 0 + } + + if self.PsIndex >=len(self.SecsKeys[sec_idx][self.RowIndex]) { + self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex])-1 + } + + self.ClearCanvas() + self.Draw() + self.Screen.SwapAndShow() +} + +func (self *Keyboard) SelectNextChar() { + + sec_idx := self.SectionIndex + row_idx := self.RowIndex + self.PsIndex+=1 + if self._PsIndex >= len(self.SecsKeys[sec_idx][row_idx]) { + self.PsIndex = 0 + self.RowIndex+=1 + if self.RowIndex >= len(self.SecsKeys[sec_idx]) { + self.RowIndex = 0 + } + + } + + self.ClearCanvas() + self.Draw() + self.Screen.SwapAndShow() } + +func (self *Keyboard) SelectPrevChar() { + + sec_idx := self.SectionIndex + self.PsIndex-=1 + if self.PsIndex < 0 { + self.RowIndex-=1 + if self.RowIndex <=0 { + self.RowIndex = len(self.SecsKeys[sec_idx])-1 + } + self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex]) -1 + } + + self.ClearCanvas() + self.Draw() + self.Screen.SwapAndShow() +} + +func (self *Keyboard) ClickOnChar() { + sec_idx := self.SectionIndex + alphabet := self.SecsKeys[sec_idx][self.RowIndex][self.PsIndex].Str + if alphabet == "Space"{ + alphabet = " " + } + + if alphabet == "_L" || alphabet == "_R" { + if alphabet == "_L" { + self.Textarea.SubTextIndex() + }else if alphabet == "_R"{ + self.Textarea.AddTextIndex() + } + }else { + self.Textarea.AppendText(alphabet) + } + + self.Textarea.Draw() + self.Screen.SwapAndShow() +} + +func (self *Keyboard) KeyboardShift() { + distance := self.Width //320 + current_time := float32(0.0) + start_posx := float32(0.0) + current_posx := start_posx + final_posx := float32(distance) +// posx_init := start + dur := self.EasingDur + last_posx := float32(0.0) + + var all_last_posx []int + + for i:=0;i= final_posx { + current_posx = final_posx + } + dx := current_posx - last_posx + all_last_posx = append(all_last_posx,int(dx)) + current_time+=1.0 + last_posx = current_posx + if current_posx >= final_posx { + break + } + } + + c := 0 + for _,v := range all_last_posx { + c+=v + } + if c < int(final_posx - start_posx) { + all_last_posx = append(all_last_posx, int( int(final_posx) - c )) + } + + for _,v := range all_last_posx { + for j:=0;j 0 { + self.LeftOrRight = 1 + self.ShiftKeyboardPage() + } + } + +} + +func (self *Keyboard) Draw() { + self.ClearCanvas() + self.Ps.Draw() + + for i:=0; i < self.SectionNumbers; i++ { + for _,j := range self.SecsKeys[i] { + for _,u := range j { + u.Draw() + } + } + } + + self.Textarea.Draw() +} diff --git a/sysgo/UI/keys_def.go b/sysgo/UI/keys_def.go index 5685a32..c9e29bd 100644 --- a/sysgo/UI/keys_def.go +++ b/sysgo/UI/keys_def.go @@ -27,6 +27,9 @@ func DefinePC() { PC["Space"] = "Space" PC["Enter"] = "Return" PC["Start"] = "S" + + PC["LK1"] = "H" + PC["LK5"] = "L" } func DefineGameShell() { @@ -45,6 +48,10 @@ func DefineGameShell() { GameShell["Space"] = "Space" GameShell["Enter"] = "K" GameShell["Start"] = "Return" + + GameShell["LK1"] = "H" + GameShell["LK5"] = "L" + } func init(){ diff --git a/sysgo/UI/text_item.go b/sysgo/UI/text_item.go index ada2090..dcefb3c 100644 --- a/sysgo/UI/text_item.go +++ b/sysgo/UI/text_item.go @@ -11,6 +11,14 @@ import ( ) +type TextItemInterface interface{ + GetBold() bool + SetBold(bold bool) + GetStr() string + Draw() +} + + type TextItem struct { IconItem Str string @@ -28,6 +36,18 @@ func NewTextItem() *TextItem { return p } +func (self *TextItem) GetBold() bool { + return self.Bold +} + +func (self *TextItem) SetBold(bold bool) { + self.Bold = bold +} + +func (self *TextItem) GetStr() string { + return self.Str +} + func (self *TextItem) Draw() { font.SetBold(self.FontObj,self.Bold)