keyboard.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. package UI
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/cuu/gogame/font"
  6. "github.com/cuu/gogame/draw"
  7. "github.com/cuu/gogame/surface"
  8. "github.com/cuu/gogame/color"
  9. "github.com/cuu/gogame/event"
  10. "github.com/cuu/LauncherGoDev/sysgo/easings"
  11. )
  12. //sysgo/UI/keyboard_keys.layout
  13. type KeyboardIcon struct {
  14. TextItem // IconItem->TextItem->KeyboardIcon
  15. }
  16. func NewKeyboardIcon() *KeyboardIcon {
  17. p := &KeyboardIcon{}
  18. p.Color = &color.Color{83,83,83,255}//SkinManager().GiveColor('Text')
  19. p.MyType = ICON_TYPES["NAV"]
  20. return p
  21. }
  22. func (self *KeyboardIcon) Draw() {
  23. rect_ := draw.MidRect(self.PosX,self.PosY,self.Width,self.Height,Width,Height)
  24. surface.Blit(self.Parent.GetCanvasHWND(),self.ImgSurf,rect_,nil)
  25. }
  26. type KeyboardSelector struct {
  27. PageSelector
  28. Parent *Keyboard
  29. }
  30. func NewKeyboardSelector() * KeyboardSelector {
  31. p := &KeyboardSelector{}
  32. return p
  33. }
  34. func (self *KeyboardSelector) Draw() {
  35. sec_idx := self.Parent.SectionIndex
  36. row_idx := self.Parent.RowIndex
  37. idx := self.Parent.PsIndex
  38. x, y := self.Parent.SecsKeys[sec_idx][row_idx][idx].Coord()
  39. w, h := self.Parent.SecsKeys[sec_idx][row_idx][idx].Size()
  40. rect_ := draw.MidRect(x,y,w+6,h+1,self.Parent.Width,self.Parent.Height)
  41. if rect_.W <= 0 || rect_.H <= 0 {
  42. return
  43. }
  44. color_ := &color.Color{126,206,244,255}
  45. draw.AARoundRect(self.Parent.CanvasHWND,rect_,color_,3,0,color_)
  46. }
  47. type Keyboard struct {
  48. Page
  49. Secs map[int][][]string
  50. SecsKeys map[int][][]TextItemInterface
  51. SectionNumbers int
  52. SectionIndex int
  53. Icons map[string]IconItemInterface
  54. KeyboardLayoutFile string ///sysgo/UI/keyboard_keys.layout
  55. LeftOrRight int
  56. RowIndex int
  57. Textarea *Textarea
  58. Selector *KeyboardSelector
  59. }
  60. func NewKeyboard() *Keyboard {
  61. p := &Keyboard{}
  62. p.PageIconMargin = 20
  63. p.SelectedIconTopOffset = 20
  64. p.Align = ALIGN["SLeft"]
  65. p.EasingDur = 10
  66. p.SectionNumbers = 3
  67. p.SectionIndex = 1
  68. p.Icons = make( map[string]IconItemInterface )
  69. p.LeftOrRight = 1
  70. p.RowIndex = 0
  71. p.FootMsg = [5]string{"Nav.","ABC","Done","Backspace","Enter"}
  72. p.Secs = make(map[int][][]string)
  73. p.SecsKeys = make(map[int][][]TextItemInterface)
  74. p.KeyboardLayoutFile = "sysgo/UI/keyboard_keys.layout"
  75. return p
  76. }
  77. func (self *Keyboard) ReadLayoutFile( fname string) {
  78. LayoutIndex := 0
  79. content ,err := ReadLines(fname)
  80. Assert(err)
  81. var tmp [][]string
  82. for i, v := range content {
  83. content[i] = strings.TrimSpace(v)
  84. stmp := strings.Split(content[i], " ")
  85. for j, u := range stmp {
  86. stmp[j] = strings.TrimSpace(u)
  87. }
  88. tmp = append(tmp, stmp)
  89. }
  90. for _, v := range tmp {
  91. if len(v) > 2 {
  92. if _, ok := self.Secs[LayoutIndex]; ok {
  93. self.Secs[LayoutIndex] = append(self.Secs[LayoutIndex], v)
  94. } else {
  95. self.Secs[LayoutIndex] = [][]string{}
  96. self.Secs[LayoutIndex] = append(self.Secs[LayoutIndex], v)
  97. }
  98. } else { //empty []
  99. LayoutIndex += 1
  100. }
  101. }
  102. }
  103. func (self *Keyboard) SetPassword(pwd string) {
  104. pwd_seq_list := strings.SplitAfter(pwd,"")
  105. self.Textarea.ResetMyWords()
  106. for _,v := range pwd_seq_list {
  107. self.Textarea.AppendText(v)
  108. }
  109. }
  110. func (self *Keyboard) Init() {
  111. self.CanvasHWND = self.Screen.CanvasHWND
  112. self.ReadLayoutFile(self.KeyboardLayoutFile) //assign to self.Secs
  113. self.SectionNumbers = len(self.Secs)
  114. self.PosX = self.Index * self.Screen.Width
  115. self.Width = self.Screen.Width
  116. self.Height = self.Screen.Height
  117. fontobj := Fonts["veramono24"]
  118. word_margin := 15
  119. secs_zero := strings.Join(self.Secs[0][0],"")
  120. fw,_:= font.Size(fontobj,secs_zero)
  121. start_x := (self.Width - fw - len(self.Secs[0][0])*word_margin)/2+word_margin/2
  122. start_y := 0
  123. // cnt := 0
  124. for i:=0; i < self.SectionNumbers; i++ {
  125. self.SecsKeys[i] = [][]TextItemInterface{}
  126. for j:=0; j < len(self.Secs[i]); j++ {
  127. self.SecsKeys[i] = append(self.SecsKeys[i],[]TextItemInterface{})
  128. secs_ij := strings.Join(self.Secs[i][j],"")
  129. fw,_ := font.Size(fontobj,secs_ij)
  130. start_x = (self.Width-fw- len(self.Secs[i][j])*word_margin)/2+word_margin/2
  131. start_x = start_x + i*self.Width
  132. start_y = 84 + j * (word_margin+14)
  133. for _,val := range self.Secs[i][j] {
  134. ti := NewTextItem()
  135. ti.FontObj = fontobj
  136. ti.Parent = self
  137. if val == "_L" || val == "_R" {
  138. it := NewKeyboardIcon()
  139. it.ImgSurf = MyIconPool.GetImgSurf(val)
  140. it.Parent = self
  141. it.Str = val
  142. it.Init(start_x+surface.GetWidth(it.ImgSurf)/2,start_y,surface.GetWidth(it.ImgSurf),surface.GetHeight(it.ImgSurf),0)
  143. self.SecsKeys[i][j] = append(self.SecsKeys[i][j],it)
  144. self.IconNumbers += 1
  145. start_x = start_x + surface.GetWidth(it.ImgSurf)+word_margin
  146. }else {
  147. if val == "_S" {
  148. val = "Space"
  149. ti.FontObj = Fonts["veramono15"]
  150. ti.Bold = true
  151. }
  152. cur_alpha_w,cur_alpha_h := font.Size(ti.FontObj,val)
  153. ti.Init(start_x + cur_alpha_w/2,start_y,cur_alpha_w,cur_alpha_h,0)
  154. ti.Str = val
  155. start_x = start_x + cur_alpha_w+word_margin // prepare for next alphabet
  156. self.SecsKeys[i][j] = append(self.SecsKeys[i][j],ti)
  157. }
  158. }
  159. }
  160. }
  161. self.SectionIndex = 0
  162. self.Textarea = NewTextarea()
  163. self.Textarea.PosX = 4
  164. self.Textarea.PosY = 4
  165. self.Textarea.Width = self.Width - 4*2
  166. self.Textarea.Height = 60
  167. self.Textarea.CanvasHWND = self.CanvasHWND
  168. self.Textarea.Init()
  169. ps := NewKeyboardSelector()
  170. ps.Parent = self
  171. ps.Init(start_x,start_y,25,25,128)
  172. ps.OnShow = true
  173. self.Ps = ps
  174. self.PsIndex = 0
  175. }
  176. func (self *Keyboard) SelectUpChar() {
  177. sec_idx := self.SectionIndex
  178. self.RowIndex -=1
  179. if self.RowIndex < 0 {
  180. self.RowIndex = len(self.SecsKeys[sec_idx])-1
  181. }
  182. if self.PsIndex >= len(self.SecsKeys[sec_idx][self.RowIndex]) {
  183. self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex])-1
  184. }
  185. self.ClearCanvas()
  186. self.Draw()
  187. self.Screen.SwapAndShow()
  188. }
  189. func (self *Keyboard) SelectDownChar() {
  190. sec_idx := self.SectionIndex
  191. self.RowIndex += 1
  192. if self.RowIndex >= len(self.SecsKeys[sec_idx]) {
  193. self.RowIndex = 0
  194. }
  195. if self.PsIndex >=len(self.SecsKeys[sec_idx][self.RowIndex]) {
  196. self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex])-1
  197. }
  198. self.ClearCanvas()
  199. self.Draw()
  200. self.Screen.SwapAndShow()
  201. }
  202. func (self *Keyboard) SelectNextChar() {
  203. sec_idx := self.SectionIndex
  204. row_idx := self.RowIndex
  205. self.PsIndex+=1
  206. if self.PsIndex >= len(self.SecsKeys[sec_idx][row_idx]) {
  207. self.PsIndex = 0
  208. self.RowIndex+=1
  209. if self.RowIndex >= len(self.SecsKeys[sec_idx]) {
  210. self.RowIndex = 0
  211. }
  212. }
  213. self.ClearCanvas()
  214. self.Draw()
  215. self.Screen.SwapAndShow()
  216. }
  217. func (self *Keyboard) SelectPrevChar() {
  218. sec_idx := self.SectionIndex
  219. self.PsIndex-=1
  220. if self.PsIndex < 0 {
  221. self.RowIndex-=1
  222. if self.RowIndex <=0 {
  223. self.RowIndex = len(self.SecsKeys[sec_idx])-1
  224. }
  225. self.PsIndex = len(self.SecsKeys[sec_idx][self.RowIndex]) -1
  226. }
  227. self.ClearCanvas()
  228. self.Draw()
  229. self.Screen.SwapAndShow()
  230. }
  231. func (self *Keyboard) ClickOnChar() {
  232. sec_idx := self.SectionIndex
  233. alphabet := self.SecsKeys[sec_idx][self.RowIndex][self.PsIndex].GetStr()
  234. if alphabet == "Space"{
  235. alphabet = " "
  236. }
  237. if alphabet == "_L" || alphabet == "_R" {
  238. if alphabet == "_L" {
  239. self.Textarea.SubTextIndex()
  240. }else if alphabet == "_R"{
  241. self.Textarea.AddTextIndex()
  242. }
  243. }else {
  244. self.Textarea.AppendText(alphabet)
  245. }
  246. self.Textarea.Draw()
  247. self.Screen.SwapAndShow()
  248. }
  249. func (self *Keyboard) KeyboardShift() {
  250. distance := self.Width //320
  251. current_time := float32(0.0)
  252. start_posx := float32(0.0)
  253. current_posx := start_posx
  254. final_posx := float32(distance)
  255. // posx_init := start
  256. dur := self.EasingDur
  257. last_posx := float32(0.0)
  258. var all_last_posx []int
  259. for i:=0;i<distance*dur;i++ {
  260. current_posx = float32(easings.SineIn(float32(current_time), float32(start_posx), float32(final_posx-start_posx),float32(dur)))
  261. if current_posx >= final_posx {
  262. current_posx = final_posx
  263. }
  264. dx := current_posx - last_posx
  265. all_last_posx = append(all_last_posx,int(dx))
  266. current_time+=1.0
  267. last_posx = current_posx
  268. if current_posx >= final_posx {
  269. break
  270. }
  271. }
  272. c := 0
  273. for _,v := range all_last_posx {
  274. c+=v
  275. }
  276. if c < int(final_posx - start_posx) {
  277. all_last_posx = append(all_last_posx, int( int(final_posx) - c ))
  278. }
  279. for _,v := range all_last_posx {
  280. for j:=0;j<self.SectionNumbers;j++ {
  281. for _,u := range self.SecsKeys[j] {
  282. for _,x := range u {
  283. x_,y_ := x.Coord()
  284. x.NewCoord(x_+self.LeftOrRight*v,y_)
  285. }
  286. }
  287. }
  288. self.ResetPageSelector()
  289. self.ClearCanvas()
  290. self.Draw()
  291. self.Screen.SwapAndShow()
  292. }
  293. }
  294. func (self *Keyboard) ShiftKeyboardPage() {
  295. self.KeyboardShift()
  296. self.SectionIndex -= self.LeftOrRight
  297. self.Draw()
  298. self.Screen.SwapAndShow()
  299. }
  300. func (self *Keyboard) KeyDown( ev *event.Event) {
  301. if ev.Data["Key"] == CurKeys["Up"] {
  302. self.SelectUpChar()
  303. return
  304. }
  305. if ev.Data["Key"] == CurKeys["Down"] {
  306. self.SelectDownChar()
  307. return
  308. }
  309. if ev.Data["Key"] == CurKeys["Right"] {
  310. self.SelectNextChar()
  311. return
  312. }
  313. if ev.Data["Key"] == CurKeys["Left"] {
  314. self.SelectPrevChar()
  315. return
  316. }
  317. if ev.Data["Key"] == CurKeys["B"] || ev.Data["Key"] == CurKeys["Enter"] {
  318. self.ClickOnChar()
  319. return
  320. }
  321. if ev.Data["Key"] == CurKeys["X"] {
  322. if self.SectionIndex <= 0 {
  323. self.LeftOrRight = -1
  324. }
  325. if self.SectionIndex >= (self.SectionNumbers - 1) {
  326. self.LeftOrRight = 1
  327. }
  328. self.ShiftKeyboardPage()
  329. }
  330. if ev.Data["Key"] == CurKeys["Menu"] {
  331. self.ReturnToUpLevelPage()
  332. self.Screen.Draw()
  333. self.Screen.SwapAndShow()
  334. }
  335. if ev.Data["Key"] == CurKeys["Y"] { // done
  336. fmt.Println(strings.Join(self.Textarea.MyWords,""))
  337. self.ReturnToUpLevelPage()
  338. self.Screen.SwapAndShow()
  339. //Uplevel/Parent page invoke OnReturnBackCb,eg: ConfigWireless
  340. }
  341. if ev.Data["Key"] == CurKeys["A"] {
  342. self.Textarea.RemoveFromLastText()
  343. self.Textarea.Draw()
  344. self.Screen.SwapAndShow()
  345. }
  346. if ev.Data["Key"] == CurKeys["LK1"] {
  347. if self.SectionIndex < self.SectionNumbers -1 {
  348. self.LeftOrRight = -1
  349. self.ShiftKeyboardPage()
  350. }
  351. }
  352. if ev.Data["Key"] == CurKeys["LK5"] {
  353. if self.SectionIndex > 0 {
  354. self.LeftOrRight = 1
  355. self.ShiftKeyboardPage()
  356. }
  357. }
  358. }
  359. func (self *Keyboard) Draw() {
  360. self.ClearCanvas()
  361. self.Ps.Draw()
  362. for i:=0; i < self.SectionNumbers; i++ {
  363. for _,j := range self.SecsKeys[i] {
  364. for _,u := range j {
  365. u.Draw()
  366. }
  367. }
  368. }
  369. self.Textarea.Draw()
  370. }