From a409bc62004a79cdcfc61023395b498c3e3a221c Mon Sep 17 00:00:00 2001 From: cuu Date: Sat, 6 Oct 2018 21:48:08 +0800 Subject: [PATCH] keyboard --- sysgo/UI/keyboard.go | 53 ++++++++++++++++- sysgo/UI/text_item.go | 48 +++++++++++++++ sysgo/UI/textarea.go | 135 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 229 insertions(+), 7 deletions(-) create mode 100644 sysgo/UI/text_item.go diff --git a/sysgo/UI/keyboard.go b/sysgo/UI/keyboard.go index 8c88f81..ba5a4d7 100644 --- a/sysgo/UI/keyboard.go +++ b/sysgo/UI/keyboard.go @@ -1,6 +1,9 @@ package UI import ( + + "github.com/cuu/gogame/draw" + "github.com/cuu/gogame/surface" "github.com/cuu/gogame/color" ) //sysgo/UI/keyboard_keys.layout @@ -19,6 +22,15 @@ func NewKeyboardIcon() *KeyboardIcon { return p } +func (self *KeyboardIcon) Draw() { + + rect_ := draw.MidRect(self.PosX,self.PosY,self.Width,self.Height,Width,Height) + + surface.Blit(self.Parent.GetCanvasHWND(),self.ImgSurf,rect_,nil) + +} + + type KeyboardSelector struct { PageSelector Parent *Keyboard @@ -35,14 +47,51 @@ func (self *KeyboardSelector) Draw() { } -type Keyboard { +type Keyboard struct { Page SectionNumbers int SectionIndex int - Icons map[string]UI.IconItemInterface + Icons map[string]IconItemInterface KeyboardLayoutFile string ///sysgo/UI/keyboard_keys.layout + LeftOrRight int + + RowIndex int + + Textarea *Textarea + Selector *KeyboardSelector + + +} + +func NewKeyboard() *Keyboard { + p := &Keyboard{} + + p.SectionNumbers = 3 + p.SectionIndex = 1 + + p.Icons = make( map[string]IconItemInterface ) + + p.LeftOrRight = 1 + + p.RowIndex = 0 + + p.FootMsg = [5]string{"Nav.","ABC","Done","Backspace","Enter"} + + return p + +} + +func (self *Keyboard) ReadLayoutFile( fname string) { + + /* + LayoutIndex := 0 + + content ,err := ReadLines(fname) + */ + + } diff --git a/sysgo/UI/text_item.go b/sysgo/UI/text_item.go new file mode 100644 index 0000000..ada2090 --- /dev/null +++ b/sysgo/UI/text_item.go @@ -0,0 +1,48 @@ +package UI + +import ( + + "github.com/veandco/go-sdl2/ttf" + + "github.com/cuu/gogame/surface" + "github.com/cuu/gogame/color" + "github.com/cuu/gogame/font" + "github.com/cuu/gogame/draw" + +) + +type TextItem struct { + IconItem + Str string + Color *color.Color + FontObj *ttf.Font + Bold bool +} + +func NewTextItem() *TextItem { + p := &TextItem{} + p.Color = &color.Color{83,83,83,255} + p.MyType = ICON_TYPES["LETTER"] + p.Bold = false + + return p +} + +func (self *TextItem) Draw() { + font.SetBold(self.FontObj,self.Bold) + + my_text := font.Render(self.FontObj,self.Str,true,self.Color,nil) + + if surface.GetWidth(my_text) != self.Width { + self.Width = surface.GetWidth(my_text) + } + + if surface.GetHeight(my_text) != self.Height { + self.Height = surface.GetHeight(my_text) + } + + + rect_ := draw.MidRect(self.PosX,self.PosY,self.Width,self.Height,Width,Height) + surface.Blit(self.Parent.GetCanvasHWND(),my_text,rect_,nil) + +} diff --git a/sysgo/UI/textarea.go b/sysgo/UI/textarea.go index 639afec..704d732 100644 --- a/sysgo/UI/textarea.go +++ b/sysgo/UI/textarea.go @@ -1,10 +1,17 @@ package UI import ( + "fmt" + "strings" + "github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/ttf" - + + "github.com/cuu/gogame/surface" "github.com/cuu/gogame/color" + "github.com/cuu/gogame/draw" + "github.com/cuu/gogame/font" + "github.com/cuu/gogame/rect" ) type Textarea struct { @@ -79,7 +86,6 @@ func (self *Textarea) AppendAndBlitText(alphabet string) { if self.TextFull == false { if self.TextIndex <= len(self.MyWords) { - m = append(m[:idx], append([]string{"U"}, m[idx:]...)...) self.MyWords = append(self.MyWords[:self.TextIndex], append([]string{alphabet},self.MyWords[self.TextIndex:]...)...) @@ -96,12 +102,131 @@ func (self *Textarea) AppendAndBlitText(alphabet string) { func (self *Textarea) BuildBlitText() { blit_rows := make([][]string,0) - w := 0 - xmargin := 5 + w := 0 +// xmargin := 5 endmargin :=15 - x := self.PosX + xmargin linenumber := 0 cursor_row := 0 + for i,v := range self.MyWords { + t := font.Render(self.FontObj,v,true,&color.Color{8,135,174,255},nil) + t_width := surface.GetWidth(t) + w+=t_width + + if linenumber < len(blit_rows) { + blit_rows[linenumber] = append(blit_rows[linenumber],v) + }else { + blit_rows = append(blit_rows,[]string{v}) + } + + if i == self.TextIndex - 1 { + cursor_row = linenumber + } + + if w + t_width >= self.Width - endmargin { + w = 0 + linenumber += 1 + blit_rows = append(blit_rows,[]string{}) + } + } + + if len(blit_rows) == 1 { + self.BlitWords = blit_rows[0] + self.BlitIndex = self.TextIndex + }else if len(blit_rows) == 2 || cursor_row < 2 { + self.BlitWords = append(blit_rows[0], blit_rows[1]...) + self.BlitIndex = self.TextIndex + + }else { + self.BlitWords = append(blit_rows[cursor_row-1], blit_rows[cursor_row]...) + self.BlitIndex = self.TextIndex + + for i,v := range blit_rows { + if i == cursor_row - 1 { + break + } + self.BlitIndex -= len(v) + } + } + +} + +func (self *Textarea) BlitText() { + //blit every single word into surface and calc the width ,check multi line + self.BuildBlitText() + + w := 0 + xmargin := 5 + endmargin := 15 + + x := self.PosX + xmargin + y := self.PosY + + linenumber := 0 + + if len(self.MyWords) > self.TextLimit { + self.TextFull = true + }else { + self.TextFull = false + } + + for _,v := range self.BlitWords { + t := font.Render(self.FontObj,v,true,&color.Color{8,135,174,255},nil) + w += surface.GetWidth(t) + + if w >= self.Width - endmargin && linenumber == 0 { + linenumber += 1 + x = self.PosX + xmargin + y = self.PosY + surface.GetHeight(t) * linenumber + w = 0 + } + + rect_ := rect.Rect(x,y,0,0) + surface.Blit(self.CanvasHWND,t,&rect_,nil) + x += surface.GetWidth(t) + + } +} + +func (self *Textarea) Cursor() { + w := 0 + xmargin := 5 + endmargin := 15 + x := self.PosX+xmargin + y := self.PosY + linenumber := 0 + + for _,v := range self.BlitWords[:self.BlitIndex] { + t := font.Render(self.FontObj,v,true,&color.Color{8,135,174,255},nil) + w += surface.GetWidth(t) + + if w >= self.Width - endmargin && linenumber == 0 { + x = self.PosX + xmargin + y = self.PosY + surface.GetHeight(t) + w = 0 + linenumber +=1 + } + + if w >= self.Width - endmargin*3 && linenumber > 0 { + x += surface.GetWidth(t) + break + } + x += surface.GetWidth(t) + } + + c_t := font.Render(self.FontObj,"_",true,&color.Color{0,0,0,255},nil) + rect_ := rect.Rect(x+1,y-2,0,0) + surface.Blit(self.CanvasHWND,c_t,&rect_,nil) +} + +func (self *Textarea) Draw() { + + rect_:= rect.Rect(self.PosX,self.PosY,self.Width,self.Height) + + draw.AARoundRect(self.CanvasHWND,&rect_,self.BackgroundColor,4,0,self.BackgroundColor) + + self.BlitText() + self.Cursor() + }