keyboard.go 10 KB

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