menu.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gui
  5. import (
  6. "github.com/g3n/engine/gui/assets"
  7. "github.com/g3n/engine/math32"
  8. "github.com/g3n/engine/window"
  9. )
  10. type MenuBar struct {
  11. }
  12. type Menu struct {
  13. Panel // embedded panel
  14. styles *MenuStyles // pointer to current styles
  15. items []*MenuItem // menu items
  16. active bool // menu active state
  17. mitem *MenuItem // parent menu item for sub menu
  18. }
  19. // MenuBodyStyle describes the style of the menu body
  20. type MenuBodyStyle struct {
  21. Border BorderSizes
  22. Paddings BorderSizes
  23. BorderColor math32.Color4
  24. BgColor math32.Color
  25. FgColor math32.Color
  26. }
  27. // MenuBodyStyles describes all styles of the menu body
  28. type MenuBodyStyles struct {
  29. Normal MenuBodyStyle
  30. Over MenuBodyStyle
  31. Focus MenuBodyStyle
  32. Disabled MenuBodyStyle
  33. }
  34. // MenuStyles describes all styles of the menu body and menu item
  35. type MenuStyles struct {
  36. Body *MenuBodyStyles // Menu body styles
  37. Item *MenuItemStyles // Menu item styles
  38. }
  39. // MenuItem is an option of a Menu
  40. type MenuItem struct {
  41. Panel // embedded panel
  42. styles *MenuItemStyles // pointer to current styles
  43. menu *Menu // pointer to container menu
  44. licon *Label // optional left icon label
  45. label *Label // optional text label (nil for separators)
  46. shortcut *Label // optional shorcut text label
  47. ricon *Label // optional right internal icon label for submenu
  48. icode int // icon code (if icon is set)
  49. submenu *Menu // pointer to optional associated sub menu
  50. keyModifier window.ModifierKey // shortcut key modifier
  51. keyCode window.Key // shortcut key code
  52. disabled bool // item disabled state
  53. selected bool // selection state
  54. }
  55. // MenuItemStyle describes the style of a menu item
  56. type MenuItemStyle struct {
  57. Border BorderSizes
  58. Paddings BorderSizes
  59. BorderColor math32.Color4
  60. BgColor math32.Color
  61. FgColor math32.Color
  62. IconPaddings BorderSizes
  63. ShortcutPaddings BorderSizes
  64. RiconPaddings BorderSizes
  65. }
  66. // MenuItemStyles describes all the menu item styles
  67. type MenuItemStyles struct {
  68. Normal MenuItemStyle
  69. Over MenuItemStyle
  70. Disabled MenuItemStyle
  71. Separator MenuItemStyle
  72. }
  73. var mapKeyModifier = map[window.ModifierKey]string{
  74. window.ModShift: "Shift",
  75. window.ModControl: "Ctrl",
  76. window.ModAlt: "Alt",
  77. }
  78. var mapKeyText = map[window.Key]string{
  79. window.KeyApostrophe: "'",
  80. window.KeyComma: ",",
  81. window.KeyMinus: "-",
  82. window.KeyPeriod: ".",
  83. window.KeySlash: "/",
  84. window.Key0: "0",
  85. window.Key1: "1",
  86. window.Key2: "2",
  87. window.Key3: "3",
  88. window.Key4: "4",
  89. window.Key5: "5",
  90. window.Key6: "6",
  91. window.Key7: "7",
  92. window.Key8: "8",
  93. window.Key9: "9",
  94. window.KeySemicolon: ";",
  95. window.KeyEqual: "=",
  96. window.KeyA: "A",
  97. window.KeyB: "B",
  98. window.KeyC: "C",
  99. window.KeyD: "D",
  100. window.KeyE: "E",
  101. window.KeyF: "F",
  102. window.KeyG: "G",
  103. window.KeyH: "H",
  104. window.KeyI: "I",
  105. window.KeyJ: "J",
  106. window.KeyK: "K",
  107. window.KeyL: "L",
  108. window.KeyM: "M",
  109. window.KeyN: "N",
  110. window.KeyO: "O",
  111. window.KeyP: "P",
  112. window.KeyQ: "Q",
  113. window.KeyR: "R",
  114. window.KeyS: "S",
  115. window.KeyT: "T",
  116. window.KeyU: "U",
  117. window.KeyV: "V",
  118. window.KeyW: "W",
  119. window.KeyX: "X",
  120. window.KeyY: "Y",
  121. window.KeyZ: "Z",
  122. window.KeyF1: "F1",
  123. window.KeyF2: "F2",
  124. window.KeyF3: "F3",
  125. window.KeyF4: "F4",
  126. window.KeyF5: "F5",
  127. window.KeyF6: "F6",
  128. window.KeyF7: "F7",
  129. window.KeyF8: "F8",
  130. window.KeyF9: "F9",
  131. window.KeyF10: "F10",
  132. window.KeyF11: "F11",
  133. window.KeyF12: "F12",
  134. }
  135. // NewMenu creates and returns a pointer to a new empty menu
  136. func NewMenu() *Menu {
  137. m := new(Menu)
  138. m.Panel.Initialize(0, 0)
  139. m.styles = &StyleDefault.Menu
  140. m.items = make([]*MenuItem, 0)
  141. m.Panel.Subscribe(OnCursorEnter, m.onCursor)
  142. m.Panel.Subscribe(OnCursorLeave, m.onCursor)
  143. m.Panel.Subscribe(OnKeyDown, m.onKey)
  144. m.update()
  145. return m
  146. }
  147. // AddOption creates and adds a new menu item to this menu with the
  148. // specified text and returns the pointer to the created menu item.
  149. func (m *Menu) AddOption(text string) *MenuItem {
  150. mi := newMenuItem(text, m.styles.Item)
  151. m.Panel.Add(mi)
  152. m.items = append(m.items, mi)
  153. mi.menu = m
  154. m.recalc()
  155. return mi
  156. }
  157. // AddSeparator creates and adds a new separator to the menu
  158. func (m *Menu) AddSeparator() *MenuItem {
  159. mi := newMenuItem("", m.styles.Item)
  160. m.Panel.Add(mi)
  161. m.items = append(m.items, mi)
  162. mi.menu = m
  163. m.recalc()
  164. return mi
  165. }
  166. // AddMenu creates and adds a new menu item to this menu with the
  167. // specified text and sub menu.
  168. // Returns the pointer to the created menu item.
  169. func (m *Menu) AddMenu(text string, subm *Menu) *MenuItem {
  170. mi := newMenuItem(text, m.styles.Item)
  171. m.Panel.Add(mi)
  172. m.items = append(m.items, mi)
  173. mi.submenu = subm
  174. mi.submenu.SetVisible(false)
  175. mi.submenu.SetBounded(false)
  176. mi.submenu.mitem = mi
  177. mi.menu = m
  178. mi.ricon = NewIconLabel(string(assets.ChevronRight))
  179. mi.Panel.Add(mi.ricon)
  180. mi.Panel.Add(mi.submenu)
  181. mi.update()
  182. m.recalc()
  183. return nil
  184. }
  185. // RemoveItem removes the specified menu item from this menu
  186. func (m *Menu) RemoveItem(mi *MenuItem) {
  187. }
  188. // onCursor process subscribed cursor events
  189. func (m *Menu) onCursor(evname string, ev interface{}) {
  190. //log.Error("evname:%s / %v", evname, ev)
  191. if evname == OnCursorEnter {
  192. m.root.SetKeyFocus(m)
  193. m.active = true
  194. } else if evname == OnCursorLeave {
  195. m.active = false
  196. // If this is a sub menu and the parent menu item is not selected
  197. // hides this sub menu
  198. if m.mitem != nil && !m.mitem.selected {
  199. m.SetVisible(false)
  200. }
  201. }
  202. m.root.StopPropagation(StopAll)
  203. }
  204. // onKey process subscribed key events
  205. func (m *Menu) onKey(evname string, ev interface{}) {
  206. sel := m.selectedPos()
  207. kev := ev.(*window.KeyEvent)
  208. switch kev.Keycode {
  209. // Select next enabled menu item
  210. case window.KeyDown:
  211. next := m.nextItem(sel)
  212. m.setSelectedPos(next)
  213. // Select previous enabled menu item
  214. case window.KeyUp:
  215. prev := m.prevItem(sel)
  216. m.setSelectedPos(prev)
  217. // Return to previous menu
  218. case window.KeyLeft:
  219. if m.mitem != nil {
  220. m.active = false
  221. m.mitem.menu.setSelectedItem(m.mitem)
  222. m.root.SetKeyFocus(m.mitem.menu)
  223. }
  224. // Enter into sub menu
  225. case window.KeyRight:
  226. if sel < 0 {
  227. return
  228. }
  229. mi := m.items[sel]
  230. if mi.submenu != nil {
  231. m.root.SetKeyFocus(mi.submenu)
  232. mi.submenu.setSelectedPos(0)
  233. }
  234. case window.KeyEnter:
  235. default:
  236. return
  237. }
  238. }
  239. // setSelectedPos sets the menu item at the specified position as selected
  240. // and all others as not selected.
  241. func (m *Menu) setSelectedPos(pos int) {
  242. for i := 0; i < len(m.items); i++ {
  243. mi := m.items[i]
  244. if i == pos {
  245. mi.selected = true
  246. } else {
  247. mi.selected = false
  248. }
  249. // If menu item has a sub menu, unselects the sub menu options recursively
  250. if mi.submenu != nil {
  251. mi.submenu.setSelectedPos(-1)
  252. }
  253. mi.update()
  254. }
  255. }
  256. // setSelectedItem sets the specified menu item as selected
  257. // and all others as not selected
  258. func (m *Menu) setSelectedItem(mitem *MenuItem) {
  259. for i := 0; i < len(m.items); i++ {
  260. mi := m.items[i]
  261. if mi == mitem {
  262. mi.selected = true
  263. } else {
  264. mi.selected = false
  265. }
  266. // If menu item has a sub menu, unselects the sub menu options recursively
  267. if mi.submenu != nil {
  268. mi.submenu.setSelectedItem(nil)
  269. }
  270. mi.update()
  271. }
  272. }
  273. // selectedPos returns the position of the current selected menu item
  274. // Returns -1 if no item selected
  275. func (m *Menu) selectedPos() int {
  276. for i := 0; i < len(m.items); i++ {
  277. mi := m.items[i]
  278. if mi.selected {
  279. return i
  280. }
  281. }
  282. return -1
  283. }
  284. // nextItem returns the position of the next enabled option from the
  285. // specified position
  286. func (m *Menu) nextItem(pos int) int {
  287. res := 0
  288. for i := pos + 1; i < len(m.items); i++ {
  289. mi := m.items[i]
  290. if mi.disabled || mi.label == nil {
  291. continue
  292. }
  293. res = i
  294. break
  295. }
  296. return res
  297. }
  298. // prevItem returns the position of previous enabled menu item from
  299. // the specified position
  300. func (m *Menu) prevItem(pos int) int {
  301. res := len(m.items) - 1
  302. for i := pos - 1; i >= 0 && i < len(m.items); i-- {
  303. mi := m.items[i]
  304. if mi.disabled || mi.label == nil {
  305. continue
  306. }
  307. res = i
  308. break
  309. }
  310. return res
  311. }
  312. // update updates the menu visual state
  313. func (m *Menu) update() {
  314. m.applyStyle(&m.styles.Body.Normal)
  315. }
  316. // applyStyle applies the specified menu body style
  317. func (m *Menu) applyStyle(mbs *MenuBodyStyle) {
  318. m.SetBordersFrom(&mbs.Border)
  319. m.SetBordersColor4(&mbs.BorderColor)
  320. m.SetPaddingsFrom(&mbs.Paddings)
  321. m.SetColor(&mbs.BgColor)
  322. }
  323. // recalc recalculates the positions of this menu internal items
  324. // and the content width and height of the menu
  325. func (m *Menu) recalc() {
  326. // Find the maximum icon and label widths
  327. minWidth := float32(0)
  328. iconWidth := float32(0)
  329. labelWidth := float32(0)
  330. shortcutWidth := float32(0)
  331. riconWidth := float32(0)
  332. for i := 0; i < len(m.items); i++ {
  333. mi := m.items[i]
  334. minWidth = mi.MinWidth()
  335. // Separator
  336. if mi.label == nil {
  337. continue
  338. }
  339. // Left icon width
  340. if mi.licon != nil && mi.licon.width > iconWidth {
  341. iconWidth = mi.licon.width
  342. }
  343. // Option label width
  344. if mi.label.width > labelWidth {
  345. labelWidth = mi.label.width
  346. }
  347. // Shortcut label width
  348. if mi.shortcut != nil && mi.shortcut.width > shortcutWidth {
  349. shortcutWidth = mi.shortcut.width
  350. }
  351. // Right icon (submenu indicator) width
  352. if mi.ricon != nil && mi.ricon.width > riconWidth {
  353. riconWidth = mi.ricon.width
  354. }
  355. }
  356. width := minWidth + iconWidth + labelWidth + shortcutWidth + riconWidth
  357. // Sets the position and width of the menu items
  358. // The height is defined by the menu item itself
  359. px := float32(0)
  360. py := float32(0)
  361. for i := 0; i < len(m.items); i++ {
  362. mi := m.items[i]
  363. mi.SetPosition(px, py)
  364. mh := mi.minHeight()
  365. py += mh
  366. mi.SetSize(width, mh)
  367. mi.recalc(iconWidth, labelWidth, shortcutWidth)
  368. }
  369. m.SetContentSize(width, py)
  370. }
  371. // newMenuItem creates and returns a pointer to a new menu item
  372. // with the specified text.
  373. func newMenuItem(text string, styles *MenuItemStyles) *MenuItem {
  374. mi := new(MenuItem)
  375. mi.Panel.Initialize(0, 0)
  376. mi.styles = styles
  377. if text != "" {
  378. mi.label = NewLabel(text)
  379. mi.Panel.Add(mi.label)
  380. mi.Panel.Subscribe(OnCursorEnter, mi.onCursor)
  381. mi.Panel.Subscribe(OnCursorLeave, mi.onCursor)
  382. }
  383. mi.update()
  384. return mi
  385. }
  386. // SetIcon sets the left icon of this menu item
  387. // If an image was previously set it is replaced by this icon
  388. func (mi *MenuItem) SetIcon(icode int) *MenuItem {
  389. mi.licon = NewIconLabel(string(icode))
  390. mi.Panel.Add(mi.licon)
  391. mi.update()
  392. return mi
  393. }
  394. // SetImage sets the left image of this menu item
  395. // If an icon was previously set it is replaced by this image
  396. func (mi *MenuItem) SetImage(img *Image) *MenuItem {
  397. return mi
  398. }
  399. // SetText sets the text of this menu item
  400. func (mi *MenuItem) SetText(text string) *MenuItem {
  401. return mi
  402. }
  403. // SetShortcut sets the keyboard shortcut of this menu item
  404. func (mi *MenuItem) SetShortcut(mod window.ModifierKey, key window.Key) *MenuItem {
  405. if mapKeyText[key] == "" {
  406. panic("Invalid menu shortcut key")
  407. }
  408. mi.keyModifier = mod
  409. mi.keyCode = key
  410. text := ""
  411. if mi.keyModifier&window.ModShift != 0 {
  412. text = mapKeyModifier[window.ModShift]
  413. }
  414. if mi.keyModifier&window.ModControl != 0 {
  415. if text != "" {
  416. text += "+"
  417. }
  418. text += mapKeyModifier[window.ModControl]
  419. }
  420. if mi.keyModifier&window.ModAlt != 0 {
  421. if text != "" {
  422. text += "+"
  423. }
  424. text += mapKeyModifier[window.ModAlt]
  425. }
  426. if text != "" {
  427. text += "+"
  428. }
  429. text += mapKeyText[key]
  430. mi.shortcut = NewLabel(text)
  431. mi.Panel.Add(mi.shortcut)
  432. mi.update()
  433. mi.menu.recalc()
  434. return mi
  435. }
  436. // SetSubmenu sets an associated sub menu item for this menu item
  437. func (mi *MenuItem) SetSubmenu(smi *MenuItem) *MenuItem {
  438. return mi
  439. }
  440. // SetEnabled sets the enabled state of this menu item
  441. func (mi *MenuItem) SetEnabled(enabled bool) *MenuItem {
  442. mi.disabled = !enabled
  443. mi.update()
  444. return mi
  445. }
  446. func (mi *MenuItem) onCursor(evname string, ev interface{}) {
  447. switch evname {
  448. case OnCursorEnter:
  449. mi.menu.setSelectedItem(mi)
  450. case OnCursorLeave:
  451. if mi.submenu != nil && mi.submenu.active {
  452. return
  453. }
  454. mi.menu.setSelectedItem(nil)
  455. }
  456. }
  457. // update updates the menu item visual state
  458. func (mi *MenuItem) update() {
  459. // Separator
  460. if mi.label == nil {
  461. mi.applyStyle(&mi.styles.Separator)
  462. return
  463. }
  464. if mi.disabled {
  465. mi.applyStyle(&mi.styles.Disabled)
  466. return
  467. }
  468. if mi.selected {
  469. mi.applyStyle(&mi.styles.Over)
  470. if mi.submenu != nil {
  471. mi.menu.SetTopChild(mi)
  472. mi.submenu.SetVisible(true)
  473. mi.submenu.SetPosition(mi.Width()-4, 0)
  474. }
  475. return
  476. }
  477. // If this menu item has a sub menu and the sub menu is not active,
  478. // hides the sub menu
  479. if mi.submenu != nil && !mi.submenu.active {
  480. mi.submenu.SetVisible(false)
  481. }
  482. mi.applyStyle(&mi.styles.Normal)
  483. }
  484. // applyStyle applies the specified menu item style
  485. func (mi *MenuItem) applyStyle(mis *MenuItemStyle) {
  486. mi.SetBordersFrom(&mis.Border)
  487. mi.SetBordersColor4(&mis.BorderColor)
  488. mi.SetPaddingsFrom(&mis.Paddings)
  489. mi.SetColor(&mis.BgColor)
  490. if mi.licon != nil {
  491. mi.licon.SetPaddingsFrom(&mis.IconPaddings)
  492. }
  493. if mi.label != nil {
  494. mi.label.SetColor(&mis.FgColor)
  495. }
  496. if mi.shortcut != nil {
  497. mi.shortcut.SetPaddingsFrom(&mis.ShortcutPaddings)
  498. }
  499. if mi.ricon != nil {
  500. mi.ricon.SetPaddingsFrom(&mis.RiconPaddings)
  501. }
  502. }
  503. // recalc recalculates the positions of this menu item internal panels
  504. func (mi *MenuItem) recalc(iconWidth, labelWidth, shortcutWidth float32) {
  505. // Separator
  506. if mi.label == nil {
  507. return
  508. }
  509. if mi.licon != nil {
  510. py := (mi.label.height - mi.licon.height) / 2
  511. mi.licon.SetPosition(0, py)
  512. }
  513. mi.label.SetPosition(iconWidth, 0)
  514. if mi.shortcut != nil {
  515. mi.shortcut.SetPosition(iconWidth+labelWidth, 0)
  516. }
  517. if mi.ricon != nil {
  518. mi.ricon.SetPosition(iconWidth+labelWidth+shortcutWidth, 0)
  519. }
  520. }
  521. // minHeight returns the minimum height of this menu item
  522. func (mi *MenuItem) minHeight() float32 {
  523. mh := mi.MinHeight()
  524. if mi.label == nil {
  525. return mh + 1
  526. }
  527. mh += mi.label.height
  528. return mh
  529. }