瀏覽代碼

gui menu dev

leonsal 8 年之前
父節點
當前提交
a2a0f317fe
共有 3 個文件被更改,包括 55 次插入12 次删除
  1. 45 8
      gui/menu.go
  2. 8 0
      gui/panel.go
  3. 2 4
      gui/root.go

+ 45 - 8
gui/menu.go

@@ -228,15 +228,16 @@ func (m *Menu) onCursor(evname string, ev interface{}) {
 // onKey process subscribed key events
 func (m *Menu) onKey(evname string, ev interface{}) {
 
+	log.Error("Menu onKey:%s", evname)
 	sel := m.selectedPos()
-	if sel < 0 {
-		return
-	}
-	mi := m.items[sel]
 	kev := ev.(*window.KeyEvent)
 	switch kev.Keycode {
 	// Select next enabled menu item
 	case window.KeyDown:
+		if sel < 0 {
+			return
+		}
+		mi := m.items[sel]
 		// Select next enabled menu item
 		if m.bar {
 			// If selected item is not a sub menu, ignore
@@ -255,6 +256,9 @@ func (m *Menu) onKey(evname string, ev interface{}) {
 		m.setSelectedPos(next)
 	// Up -> Previous item for vertical menus
 	case window.KeyUp:
+		if sel < 0 {
+			return
+		}
 		if m.bar {
 			return
 		}
@@ -262,6 +266,9 @@ func (m *Menu) onKey(evname string, ev interface{}) {
 		m.setSelectedPos(prev)
 	// Left -> Previous menu item for menu bar
 	case window.KeyLeft:
+		if sel < 0 {
+			return
+		}
 		// For menu bar, select previous menu item
 		if m.bar {
 			prev := m.prevItem(sel)
@@ -283,6 +290,9 @@ func (m *Menu) onKey(evname string, ev interface{}) {
 
 	// Right -> Next menu bar item || Next sub menu
 	case window.KeyRight:
+		if sel < 0 {
+			return
+		}
 		mi := m.items[sel]
 		// For menu bar, select next menu item
 		if m.bar {
@@ -305,13 +315,32 @@ func (m *Menu) onKey(evname string, ev interface{}) {
 		}
 	// Enter -> Select menu option
 	case window.KeyEnter:
+		if sel < 0 {
+			return
+		}
+		mi := m.items[sel]
 		mi.activate()
 	// Check for menu items shortcuts
 	default:
-		found := mi.rootMenu().checkKey(kev)
-		if found != nil {
+		var root *Menu
+		if sel < 0 {
+			root = m
+		} else {
+			mi := m.items[sel]
+			root = mi.rootMenu()
+		}
+		found := root.checkKey(kev)
+		if found == nil {
+			return
+		}
+		if found.submenu == nil {
 			found.activate()
+			return
+		}
+		if found.menu.bar {
+			found.menu.autoOpen = true
 		}
+		found.menu.setSelectedItem(found)
 	}
 }
 
@@ -571,13 +600,20 @@ func (mi *MenuItem) SetText(text string) *MenuItem {
 }
 
 // SetShortcut sets the keyboard shortcut of this menu item
-func (mi *MenuItem) SetShortcut(mod window.ModifierKey, key window.Key) *MenuItem {
+func (mi *MenuItem) SetShortcut(mods window.ModifierKey, key window.Key) *MenuItem {
 
 	if mapKeyText[key] == "" {
 		panic("Invalid menu shortcut key")
 	}
-	mi.keyMods = mod
+	mi.keyMods = mods
 	mi.keyCode = key
+
+	// If parent menu is a menu bar, nothing more to do
+	if mi.menu.bar {
+		return mi
+	}
+
+	// Builds shortcut text
 	text := ""
 	if mi.keyMods&window.ModShift != 0 {
 		text = mapKeyModifier[window.ModShift]
@@ -599,6 +635,7 @@ func (mi *MenuItem) SetShortcut(mod window.ModifierKey, key window.Key) *MenuIte
 	}
 	text += mapKeyText[key]
 
+	// Creates and adds shortcut label
 	mi.shortcut = NewLabel(text)
 	mi.Panel.Add(mi.shortcut)
 	mi.update()

+ 8 - 0
gui/panel.go

@@ -180,6 +180,7 @@ func (pan *Panel) GetPanel() *Panel {
 }
 
 // SetRoot satisfies the IPanel interface
+// Sets the pointer to the root panel for this panel and all its children
 func (p *Panel) SetRoot(root *Root) {
 
 	p.root = root
@@ -220,6 +221,12 @@ func (p *Panel) Material() *material.Material {
 	return p.mat
 }
 
+// Root returns the pointer for this panel root panel
+func (p *Panel) Root() *Root {
+
+	return p.root
+}
+
 // SetTopChild sets the Z coordinate of the specified panel to
 // be on top of all other children of this panel.
 // The function does not check if the specified panel is a
@@ -478,6 +485,7 @@ func (p *Panel) Add(ichild IPanel) *Panel {
 	p.Node.Add(ichild)
 	node := ichild.GetPanel()
 	node.SetParent(p)
+	ichild.SetRoot(p.root)
 	if p.layout != nil {
 		p.layout.Recalc(p)
 	}

+ 2 - 4
gui/root.go

@@ -35,6 +35,7 @@ func NewRoot(gs *gls.GLS, win window.IWindow) *Root {
 	r := new(Root)
 	r.gs = gs
 	r.win = win
+	r.root = r
 	r.Panel.Initialize(0, 0)
 	r.TimerManager.Initialize()
 	// for optimization, sets this root panel as not renderable as in most cases
@@ -66,7 +67,7 @@ func (r *Root) Add(ipan IPanel) {
 
 	r.Panel.Add(ipan)
 	ipan.GetNode().SetParent(r)
-	log.Error("Root Add:%p", ipan)
+	ipan.SetRoot(r)
 }
 
 // SetKeyFocus sets the panel which will receive all keyboard events
@@ -174,7 +175,6 @@ func (r *Root) onKey(evname string, ev interface{}) {
 	}
 	// Dispatch window.KeyEvent to focused panel subscribers
 	r.stopPropagation = 0
-	r.keyFocus.SetRoot(r)
 	r.keyFocus.GetPanel().Dispatch(evname, ev)
 	// If requested, stop propagation of event outside the root gui
 	if (r.stopPropagation & Stop3D) != 0 {
@@ -242,7 +242,6 @@ func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
 		if found {
 			r.targets = append(r.targets, ipan)
 		} else {
-			ipan.SetRoot(r)
 			// If OnCursorEnter previously sent, sends OnCursorLeave with a nil event
 			if pan.cursorEnter {
 				pan.Dispatch(OnCursorLeave, nil)
@@ -287,7 +286,6 @@ func (r *Root) sendPanels(x, y float32, evname string, ev interface{}) {
 
 	// Send events to panels
 	for _, ipan := range r.targets {
-		ipan.SetRoot(r)
 		pan := ipan.GetPanel()
 		// Cursor position event
 		if evname == OnCursor {