<commit_before>package glmenu

import (
	"errors"
	"fmt"
	"github.com/4ydx/gltext"
	"github.com/go-gl/glfw/v3.1/glfw"
	"github.com/go-gl/mathgl/mgl32"
)

type MenuManager struct {
	Font       *gltext.Font
	StartKey   glfw.Key // they key that, when pressed, will display the StartMenu
	StartMenu  string   // the name passed to each NewMenu call
	Menus      map[string]*Menu
	IsResolved bool
}

// Finalize connects menus together and performs final formatting steps
// this must be run after all menus are prepared
func (mm *MenuManager) Finalize() error {
	if mm.IsResolved {
		return errors.New("Menus have already been resolved")
	}
	for _, menu := range mm.Menus {
		menu.Finalize()
		for _, label := range menu.Labels {
			if label.Config.Action == GOTO_MENU {
				gotoMenu, ok := mm.Menus[label.Config.Goto]
				if ok {
					func(from *Menu, to *Menu, l *Label) {
						l.onRelease = func(xPos, yPos float64, button MouseClick, inBox bool) {
							if inBox {
								from.Hide()
								to.Show()
							}
						}
					}(menu, gotoMenu, label)
				}
			}
		}
	}
	mm.IsResolved = true
	return nil
}

func (mm *MenuManager) IsVisible() bool {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			return true
		}
	}
	return false
}

// Clicked resolves menus that have been clicked
func (mm *MenuManager) MouseClick(xPos, yPos float64, button MouseClick) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseClick(xPos, yPos, button)
		}
	}
}

func (mm *MenuManager) MouseRelease(xPos, yPos float64, button MouseClick) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseRelease(xPos, yPos, button)
		}
	}
}

func (mm *MenuManager) MouseHover(xPos, yPos float64) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseHover(xPos, yPos)
		}
	}
}

func (mm *MenuManager) KeyRelease(key glfw.Key, withShift bool) {
	for _, menu := range mm.Menus {
		if menu.KeyRelease(key, withShift) {
			return
		}
	}
}

func (mm *MenuManager) Draw() bool {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			if menu.OnComplete != nil {
				menu.OnComplete()
			}
			return menu.Draw()
		}
	}
	return false
}

func (mm *MenuManager) Release() {
	for _, menu := range mm.Menus {
		menu.Release()
	}
}

func (mm *MenuManager) NewMenu(window *glfw.Window, name string, menuDefaults MenuDefaults, offsetBy mgl32.Vec2) (*Menu, error) {
	m, err := NewMenu(window, mm.Font, menuDefaults, offsetBy)
	if err != nil {
		return nil, err
	}
	m.MenuManager = mm

	if _, ok := mm.Menus[name]; ok {
		return nil, errors.New(fmt.Sprintf("The named menu %s already exists.", name))
	}
	mm.Menus[name] = m
	return m, nil
}

func (mm *MenuManager) Show(name string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	m.Show()
	return nil
}

func (mm *MenuManager) Toggle(name string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	m.Toggle()
	return nil
}

func (mm *MenuManager) SetText(name string, index int, text string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	for i, l := range m.Labels {
		if i == index {
			l.Text.SetString(text)
		}
	}
	return nil
}

// NewMenuManager handles a tree of menus that interact with one another
func NewMenuManager(font *gltext.Font, startKey glfw.Key, startMenu string) *MenuManager {
	mm := &MenuManager{Font: font, StartKey: startKey, StartMenu: startMenu}
	mm.Menus = make(map[string]*Menu)
	return mm
}
<commit_msg>Hide all menus under the manager.<commit_after>package glmenu

import (
	"errors"
	"fmt"
	"github.com/4ydx/gltext"
	"github.com/go-gl/glfw/v3.1/glfw"
	"github.com/go-gl/mathgl/mgl32"
)

type MenuManager struct {
	Font       *gltext.Font
	StartKey   glfw.Key // they key that, when pressed, will display the StartMenu
	StartMenu  string   // the name passed to each NewMenu call
	Menus      map[string]*Menu
	IsResolved bool
}

// Finalize connects menus together and performs final formatting steps
// this must be run after all menus are prepared
func (mm *MenuManager) Finalize() error {
	if mm.IsResolved {
		return errors.New("Menus have already been resolved")
	}
	for _, menu := range mm.Menus {
		menu.Finalize()
		for _, label := range menu.Labels {
			if label.Config.Action == GOTO_MENU {
				gotoMenu, ok := mm.Menus[label.Config.Goto]
				if ok {
					func(from *Menu, to *Menu, l *Label) {
						l.onRelease = func(xPos, yPos float64, button MouseClick, inBox bool) {
							if inBox {
								from.Hide()
								to.Show()
							}
						}
					}(menu, gotoMenu, label)
				}
			}
		}
	}
	mm.IsResolved = true
	return nil
}

func (mm *MenuManager) IsVisible() bool {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			return true
		}
	}
	return false
}

// Clicked resolves menus that have been clicked
func (mm *MenuManager) MouseClick(xPos, yPos float64, button MouseClick) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseClick(xPos, yPos, button)
		}
	}
}

func (mm *MenuManager) MouseRelease(xPos, yPos float64, button MouseClick) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseRelease(xPos, yPos, button)
		}
	}
}

func (mm *MenuManager) MouseHover(xPos, yPos float64) {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			menu.MouseHover(xPos, yPos)
		}
	}
}

func (mm *MenuManager) KeyRelease(key glfw.Key, withShift bool) {
	for _, menu := range mm.Menus {
		if menu.KeyRelease(key, withShift) {
			return
		}
	}
}

func (mm *MenuManager) Draw() bool {
	for _, menu := range mm.Menus {
		if menu.IsVisible {
			if menu.OnComplete != nil {
				menu.OnComplete()
			}
			return menu.Draw()
		}
	}
	return false
}

func (mm *MenuManager) Release() {
	for _, menu := range mm.Menus {
		menu.Release()
	}
}

func (mm *MenuManager) NewMenu(window *glfw.Window, name string, menuDefaults MenuDefaults, offsetBy mgl32.Vec2) (*Menu, error) {
	m, err := NewMenu(window, mm.Font, menuDefaults, offsetBy)
	if err != nil {
		return nil, err
	}
	m.MenuManager = mm

	if _, ok := mm.Menus[name]; ok {
		return nil, errors.New(fmt.Sprintf("The named menu %s already exists.", name))
	}
	mm.Menus[name] = m
	return m, nil
}

func (mm *MenuManager) Hide() {
	for _, m := range mm.Menus {
		m.Hide()
	}
}

func (mm *MenuManager) Show(name string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	m.Show()
	return nil
}

func (mm *MenuManager) Toggle(name string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	m.Toggle()
	return nil
}

func (mm *MenuManager) SetText(name string, index int, text string) error {
	m, ok := mm.Menus[name]
	if !ok {
		return errors.New(fmt.Sprintf("The named menu '%s' doesn't exists.", name))
	}
	for i, l := range m.Labels {
		if i == index {
			l.Text.SetString(text)
		}
	}
	return nil
}

// NewMenuManager handles a tree of menus that interact with one another
func NewMenuManager(font *gltext.Font, startKey glfw.Key, startMenu string) *MenuManager {
	mm := &MenuManager{Font: font, StartKey: startKey, StartMenu: startMenu}
	mm.Menus = make(map[string]*Menu)
	return mm
}

{'fraction_non_alphanumeric': 0.13154672833628078, 'fraction_numerical': 0.004353149231397089, 'mean_word_length': 3.312023460410557, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1625910', 'n_tokens_mistral': 2912, 'n_tokens_neox': 2662, 'n_words': 766}