menu.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  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 Menu struct {
  11. Panel // embedded panel
  12. styles *MenuStyles // pointer to current styles
  13. bar bool // true for menu bar
  14. items []*MenuItem // menu items
  15. active bool // menu active state
  16. autoOpen bool // open sub menus when mouse over if true
  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 parent 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. func NewMenuBar() *Menu {
  136. m := NewMenu()
  137. m.bar = true
  138. return m
  139. }
  140. // NewMenu creates and returns a pointer to a new empty menu
  141. func NewMenu() *Menu {
  142. m := new(Menu)
  143. m.Panel.Initialize(0, 0)
  144. m.styles = &StyleDefault.Menu
  145. m.items = make([]*MenuItem, 0)
  146. m.Panel.Subscribe(OnCursorEnter, m.onCursor)
  147. m.Panel.Subscribe(OnCursorLeave, m.onCursor)
  148. m.Panel.Subscribe(OnKeyDown, m.onKey)
  149. m.Panel.Subscribe(OnMouseOut, m.onMouse)
  150. m.update()
  151. return m
  152. }
  153. // AddOption creates and adds a new menu item to this menu with the
  154. // specified text and returns the pointer to the created menu item.
  155. func (m *Menu) AddOption(text string) *MenuItem {
  156. mi := newMenuItem(text, m.styles.Item)
  157. m.Panel.Add(mi)
  158. m.items = append(m.items, mi)
  159. mi.menu = m
  160. m.recalc()
  161. return mi
  162. }
  163. // AddSeparator creates and adds a new separator to the menu
  164. func (m *Menu) AddSeparator() *MenuItem {
  165. mi := newMenuItem("", m.styles.Item)
  166. m.Panel.Add(mi)
  167. m.items = append(m.items, mi)
  168. mi.menu = m
  169. m.recalc()
  170. return mi
  171. }
  172. // AddMenu creates and adds a new menu item to this menu with the
  173. // specified text and sub menu.
  174. // Returns the pointer to the created menu item.
  175. func (m *Menu) AddMenu(text string, subm *Menu) *MenuItem {
  176. mi := newMenuItem(text, m.styles.Item)
  177. m.Panel.Add(mi)
  178. m.items = append(m.items, mi)
  179. mi.submenu = subm
  180. mi.submenu.SetVisible(false)
  181. mi.submenu.SetBounded(false)
  182. mi.submenu.mitem = mi
  183. mi.submenu.autoOpen = true
  184. mi.menu = m
  185. mi.ricon = NewIconLabel(string(assets.ChevronRight))
  186. mi.Panel.Add(mi.ricon)
  187. mi.Panel.Add(mi.submenu)
  188. mi.update()
  189. m.recalc()
  190. return nil
  191. }
  192. // RemoveItem removes the specified menu item from this menu
  193. func (m *Menu) RemoveItem(mi *MenuItem) {
  194. }
  195. // onCursor process subscribed cursor events
  196. func (m *Menu) onCursor(evname string, ev interface{}) {
  197. if evname == OnCursorEnter {
  198. m.root.SetKeyFocus(m)
  199. m.active = true
  200. } else if evname == OnCursorLeave {
  201. m.active = false
  202. // If this is a sub menu and the parent menu item is not selected
  203. // hides this sub menu
  204. //if m.mitem != nil && !m.mitem.selected {
  205. // m.SetVisible(false)
  206. //}
  207. }
  208. m.root.StopPropagation(StopAll)
  209. }
  210. // onKey process subscribed key events
  211. func (m *Menu) onKey(evname string, ev interface{}) {
  212. sel := m.selectedPos()
  213. kev := ev.(*window.KeyEvent)
  214. switch kev.Keycode {
  215. // Select next enabled menu item
  216. case window.KeyDown:
  217. next := m.nextItem(sel)
  218. m.setSelectedPos(next)
  219. // Select previous enabled menu item
  220. case window.KeyUp:
  221. prev := m.prevItem(sel)
  222. m.setSelectedPos(prev)
  223. // Return to previous menu
  224. case window.KeyLeft:
  225. if m.mitem != nil {
  226. m.active = false
  227. m.mitem.menu.setSelectedItem(m.mitem)
  228. m.root.SetKeyFocus(m.mitem.menu)
  229. }
  230. // Enter into sub menu
  231. case window.KeyRight:
  232. if sel < 0 {
  233. return
  234. }
  235. mi := m.items[sel]
  236. if mi.submenu != nil {
  237. m.root.SetKeyFocus(mi.submenu)
  238. mi.submenu.setSelectedPos(0)
  239. }
  240. case window.KeyEnter:
  241. default:
  242. return
  243. }
  244. }
  245. // onMouse process subscribed mouse events for the menu
  246. func (m *Menu) onMouse(evname string, ev interface{}) {
  247. if evname == OnMouseOut {
  248. if m.bar {
  249. m.autoOpen = false
  250. m.setSelectedPos(-1)
  251. }
  252. }
  253. }
  254. // setSelectedPos sets the menu item at the specified position as selected
  255. // and all others as not selected.
  256. func (m *Menu) setSelectedPos(pos int) {
  257. for i := 0; i < len(m.items); i++ {
  258. mi := m.items[i]
  259. if i == pos {
  260. mi.selected = true
  261. } else {
  262. mi.selected = false
  263. }
  264. // If menu item has a sub menu, unselects the sub menu options recursively
  265. if mi.submenu != nil {
  266. mi.submenu.setSelectedPos(-1)
  267. }
  268. mi.update()
  269. }
  270. }
  271. // setSelectedItem sets the specified menu item as selected
  272. // and all others as not selected
  273. func (m *Menu) setSelectedItem(mitem *MenuItem) {
  274. for i := 0; i < len(m.items); i++ {
  275. mi := m.items[i]
  276. if mi == mitem {
  277. mi.selected = true
  278. } else {
  279. mi.selected = false
  280. }
  281. // If menu item has a sub menu, unselects the sub menu options recursively
  282. if mi.submenu != nil {
  283. mi.submenu.setSelectedItem(nil)
  284. }
  285. mi.update()
  286. }
  287. }
  288. // selectedPos returns the position of the current selected menu item
  289. // Returns -1 if no item selected
  290. func (m *Menu) selectedPos() int {
  291. for i := 0; i < len(m.items); i++ {
  292. mi := m.items[i]
  293. if mi.selected {
  294. return i
  295. }
  296. }
  297. return -1
  298. }
  299. // nextItem returns the position of the next enabled option from the
  300. // specified position
  301. func (m *Menu) nextItem(pos int) int {
  302. res := 0
  303. for i := pos + 1; i < len(m.items); i++ {
  304. mi := m.items[i]
  305. if mi.disabled || mi.label == nil {
  306. continue
  307. }
  308. res = i
  309. break
  310. }
  311. return res
  312. }
  313. // prevItem returns the position of previous enabled menu item from
  314. // the specified position
  315. func (m *Menu) prevItem(pos int) int {
  316. res := len(m.items) - 1
  317. for i := pos - 1; i >= 0 && i < len(m.items); i-- {
  318. mi := m.items[i]
  319. if mi.disabled || mi.label == nil {
  320. continue
  321. }
  322. res = i
  323. break
  324. }
  325. return res
  326. }
  327. // update updates the menu visual state
  328. func (m *Menu) update() {
  329. m.applyStyle(&m.styles.Body.Normal)
  330. }
  331. // applyStyle applies the specified menu body style
  332. func (m *Menu) applyStyle(mbs *MenuBodyStyle) {
  333. m.SetBordersFrom(&mbs.Border)
  334. m.SetBordersColor4(&mbs.BorderColor)
  335. m.SetPaddingsFrom(&mbs.Paddings)
  336. m.SetColor(&mbs.BgColor)
  337. }
  338. // recalc recalculates the positions of this menu internal items
  339. // and the content width and height of the menu
  340. func (m *Menu) recalc() {
  341. if m.bar {
  342. m.recalcBar()
  343. return
  344. }
  345. // Find the maximum icon and label widths
  346. minWidth := float32(0)
  347. iconWidth := float32(0)
  348. labelWidth := float32(0)
  349. shortcutWidth := float32(0)
  350. riconWidth := float32(0)
  351. for i := 0; i < len(m.items); i++ {
  352. mi := m.items[i]
  353. minWidth = mi.MinWidth()
  354. // Separator
  355. if mi.label == nil {
  356. continue
  357. }
  358. // Left icon width
  359. if mi.licon != nil && mi.licon.width > iconWidth {
  360. iconWidth = mi.licon.width
  361. }
  362. // Option label width
  363. if mi.label.width > labelWidth {
  364. labelWidth = mi.label.width
  365. }
  366. // Shortcut label width
  367. if mi.shortcut != nil && mi.shortcut.width > shortcutWidth {
  368. shortcutWidth = mi.shortcut.width
  369. }
  370. // Right icon (submenu indicator) width
  371. if mi.ricon != nil && mi.ricon.width > riconWidth {
  372. riconWidth = mi.ricon.width
  373. }
  374. }
  375. width := minWidth + iconWidth + labelWidth + shortcutWidth + riconWidth
  376. // Sets the position and width of the menu items
  377. // The height is defined by the menu item itself
  378. px := float32(0)
  379. py := float32(0)
  380. for i := 0; i < len(m.items); i++ {
  381. mi := m.items[i]
  382. mi.SetPosition(px, py)
  383. mh := mi.minHeight()
  384. py += mh
  385. mi.SetSize(width, mh)
  386. mi.recalc(iconWidth, labelWidth, shortcutWidth)
  387. }
  388. m.SetContentSize(width, py)
  389. }
  390. // recalcBar recalculates the positions of this MenuBar internal items
  391. // and the content width and height of the menu
  392. func (m *Menu) recalcBar() {
  393. height := float32(0)
  394. for i := 0; i < len(m.items); i++ {
  395. mi := m.items[i]
  396. if mi.minHeight() > height {
  397. height = mi.minHeight()
  398. }
  399. }
  400. px := float32(0)
  401. for i := 0; i < len(m.items); i++ {
  402. mi := m.items[i]
  403. mi.SetPosition(px, 0)
  404. width := float32(0)
  405. width = mi.minWidth()
  406. mi.SetSize(width, height)
  407. px += mi.Width()
  408. }
  409. m.SetContentSize(px, height)
  410. }
  411. // newMenuItem creates and returns a pointer to a new menu item
  412. // with the specified text.
  413. func newMenuItem(text string, styles *MenuItemStyles) *MenuItem {
  414. mi := new(MenuItem)
  415. mi.Panel.Initialize(0, 0)
  416. mi.styles = styles
  417. if text != "" {
  418. mi.label = NewLabel(text)
  419. mi.Panel.Add(mi.label)
  420. mi.Panel.Subscribe(OnCursorEnter, mi.onCursor)
  421. mi.Panel.Subscribe(OnCursorLeave, mi.onCursor)
  422. mi.Panel.Subscribe(OnMouseUp, mi.onMouse)
  423. mi.Panel.Subscribe(OnMouseDown, mi.onMouse)
  424. }
  425. mi.update()
  426. return mi
  427. }
  428. // SetIcon sets the left icon of this menu item
  429. // If an image was previously set it is replaced by this icon
  430. func (mi *MenuItem) SetIcon(icode int) *MenuItem {
  431. mi.licon = NewIconLabel(string(icode))
  432. mi.Panel.Add(mi.licon)
  433. mi.update()
  434. return mi
  435. }
  436. // SetImage sets the left image of this menu item
  437. // If an icon was previously set it is replaced by this image
  438. func (mi *MenuItem) SetImage(img *Image) {
  439. }
  440. // SetText sets the text of this menu item
  441. func (mi *MenuItem) SetText(text string) *MenuItem {
  442. if mi.label == nil {
  443. return mi
  444. }
  445. mi.label.SetText(text)
  446. mi.update()
  447. mi.menu.recalc()
  448. return mi
  449. }
  450. // SetShortcut sets the keyboard shortcut of this menu item
  451. func (mi *MenuItem) SetShortcut(mod window.ModifierKey, key window.Key) *MenuItem {
  452. if mapKeyText[key] == "" {
  453. panic("Invalid menu shortcut key")
  454. }
  455. mi.keyModifier = mod
  456. mi.keyCode = key
  457. text := ""
  458. if mi.keyModifier&window.ModShift != 0 {
  459. text = mapKeyModifier[window.ModShift]
  460. }
  461. if mi.keyModifier&window.ModControl != 0 {
  462. if text != "" {
  463. text += "+"
  464. }
  465. text += mapKeyModifier[window.ModControl]
  466. }
  467. if mi.keyModifier&window.ModAlt != 0 {
  468. if text != "" {
  469. text += "+"
  470. }
  471. text += mapKeyModifier[window.ModAlt]
  472. }
  473. if text != "" {
  474. text += "+"
  475. }
  476. text += mapKeyText[key]
  477. mi.shortcut = NewLabel(text)
  478. mi.Panel.Add(mi.shortcut)
  479. mi.update()
  480. mi.menu.recalc()
  481. return mi
  482. }
  483. // SetSubmenu sets an associated sub menu item for this menu item
  484. func (mi *MenuItem) SetSubmenu(smi *MenuItem) *MenuItem {
  485. return mi
  486. }
  487. // SetEnabled sets the enabled state of this menu item
  488. func (mi *MenuItem) SetEnabled(enabled bool) *MenuItem {
  489. mi.disabled = !enabled
  490. mi.update()
  491. return mi
  492. }
  493. // onCursor processes subscribed cursor events over the menu item
  494. func (mi *MenuItem) onCursor(evname string, ev interface{}) {
  495. switch evname {
  496. case OnCursorEnter:
  497. mi.menu.setSelectedItem(mi)
  498. case OnCursorLeave:
  499. //if mi.submenu != nil && mi.submenu.active {
  500. // return
  501. //}
  502. //mi.menu.setSelectedItem(nil)
  503. }
  504. }
  505. // onMouse processes subscribed mouse events over the menu item
  506. func (mi *MenuItem) onMouse(evname string, ev interface{}) {
  507. switch evname {
  508. case OnMouseDown:
  509. // MenuBar option
  510. if mi.menu.bar {
  511. mi.menu.autoOpen = !mi.menu.autoOpen
  512. if mi.submenu != nil && mi.submenu.Visible() {
  513. mi.submenu.SetVisible(false)
  514. return
  515. }
  516. mi.update()
  517. //if mi.submenu != nil {
  518. // if !mi.submenu.Visible() {
  519. // mi.submenu.SetVisible(true)
  520. // mi.submenu.SetPosition(0, mi.Height()-2)
  521. // } else {
  522. // mi.submenu.SetVisible(false)
  523. // }
  524. //} else {
  525. // // Dispatch on click
  526. //}
  527. } else {
  528. }
  529. case OnMouseUp:
  530. }
  531. }
  532. // update updates the menu item visual state
  533. func (mi *MenuItem) update() {
  534. // Separator
  535. if mi.label == nil {
  536. mi.applyStyle(&mi.styles.Separator)
  537. return
  538. }
  539. // Disabled item
  540. if mi.disabled {
  541. mi.applyStyle(&mi.styles.Disabled)
  542. return
  543. }
  544. // Selected item
  545. if mi.selected {
  546. mi.applyStyle(&mi.styles.Over)
  547. if mi.submenu != nil && mi.menu.autoOpen {
  548. mi.menu.SetTopChild(mi)
  549. mi.submenu.SetVisible(true)
  550. if mi.menu != nil && mi.menu.bar {
  551. mi.submenu.SetPosition(0, mi.Height()-2)
  552. } else {
  553. mi.submenu.SetPosition(mi.Width()-2, 0)
  554. }
  555. }
  556. return
  557. }
  558. // If this menu item has a sub menu and the sub menu is not active,
  559. // hides the sub menu
  560. if mi.submenu != nil {
  561. mi.submenu.SetVisible(false)
  562. }
  563. mi.applyStyle(&mi.styles.Normal)
  564. }
  565. // applyStyle applies the specified menu item style
  566. func (mi *MenuItem) applyStyle(mis *MenuItemStyle) {
  567. mi.SetBordersFrom(&mis.Border)
  568. mi.SetBordersColor4(&mis.BorderColor)
  569. mi.SetPaddingsFrom(&mis.Paddings)
  570. mi.SetColor(&mis.BgColor)
  571. if mi.licon != nil {
  572. mi.licon.SetPaddingsFrom(&mis.IconPaddings)
  573. }
  574. if mi.label != nil {
  575. mi.label.SetColor(&mis.FgColor)
  576. }
  577. if mi.shortcut != nil {
  578. mi.shortcut.SetPaddingsFrom(&mis.ShortcutPaddings)
  579. }
  580. if mi.ricon != nil {
  581. mi.ricon.SetPaddingsFrom(&mis.RiconPaddings)
  582. }
  583. }
  584. // recalc recalculates the positions of this menu item internal panels
  585. func (mi *MenuItem) recalc(iconWidth, labelWidth, shortcutWidth float32) {
  586. // Separator
  587. if mi.label == nil {
  588. return
  589. }
  590. if mi.licon != nil {
  591. py := (mi.label.height - mi.licon.height) / 2
  592. mi.licon.SetPosition(0, py)
  593. }
  594. mi.label.SetPosition(iconWidth, 0)
  595. if mi.shortcut != nil {
  596. mi.shortcut.SetPosition(iconWidth+labelWidth, 0)
  597. }
  598. if mi.ricon != nil {
  599. mi.ricon.SetPosition(iconWidth+labelWidth+shortcutWidth, 0)
  600. }
  601. }
  602. // minHeight returns the minimum height of this menu item
  603. func (mi *MenuItem) minHeight() float32 {
  604. mh := mi.MinHeight()
  605. if mi.label == nil {
  606. return mh + 1
  607. }
  608. mh += mi.label.height
  609. return mh
  610. }
  611. // minWidth returns the minimum width of this menu item
  612. func (mi *MenuItem) minWidth() float32 {
  613. mw := mi.MinWidth()
  614. if mi.label == nil {
  615. return mw + 1
  616. }
  617. mw += mi.label.width
  618. return mw
  619. }