egui

package
v2.0.0-dev0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2026 License: BSD-3-Clause Imports: 32 Imported by: 72

README

Docs: GoDoc

egui handles all the GUI elements for a typical simulation, reducing boilerplate code in models.

The ra25 example has a fully updated implementation of this new GUI infrastructure.

Examples

Here's the start of the main ConfigGUI method:

// ConfigGUI configures the Cogent Core GUI interface for this simulation.
func (ss *Sim) ConfigGUI() *core.Window {
	title := "Leabra Random Associator"
	ss.GUI.MakeWindow(ss, "ra25", title, `This demonstrates a basic Leabra model. See <a href="https://github.com/emer/emergent">emergent on GitHub</a>.</p>`)
	ss.GUI.CycleUpdateInterval = 10
	ss.GUI.NetView.SetNet(ss.Net)

    // optionally reconfigure the netview:
	ss.GUI.NetView.Scene().Camera.Pose.Pos.Set(0, 1, 2.75) 
	ss.GUI.NetView.Scene().Camera.LookAt(math32.Vec3(0, 0, 0), math32.Vec3(0, 1, 0)) 
	ss.GUI.AddPlots(title, &ss.Logs) // automatically adds all configured plots

Toolbar Items

The ToolbarItem class provides toolbar configuration options, taking the place of core.ActOpts from existing code that operates directly at the GoGi level. The main differences are

  • The standard UpdateFunc options of either making the action active or inactive while the sim is running are now handled using Active: equi.ActiveStopped or egui.ActiveRunning or egui.ActiveAlways

  • The action function is just a simple Func: func() { with no args -- use context capture of closures to access any relevant state.

  • Use ss.GUI.UpdateWindow() inside any action function instead of vp.SetNeedsFullRender()

Here is a typical item:

    ss.GUI.AddToolbarItem(egui.ToolbarItem{Label: "Init", Icon: "update",
        Tooltip: "Initialize everything including network weights, and start over.  Also applies current params.",
        Active:  egui.ActiveStopped,
        Func: func() {
            ss.Init()
            ss.GUI.UpdateWindow()
        },
    })

For actions that take any significant amount of time, call the function in a separate routine using go, and use the GUI based variables:

    ss.GUI.AddToolbarItem(egui.ToolbarItem{Label: "Train",
        Icon:    "run",
        Tooltip: "Starts the network training, picking up from wherever it may have left off.  If not stopped, training will complete the specified number of Runs through the full number of Epochs of training, with testing automatically occuring at the specified interval.",
        Active:  egui.ActiveStopped,
        Func: func() {
            if !ss.GUI.IsRunning {
                ss.GUI.IsRunning = true
                ss.GUI.ToolBar.UpdateActions()
                go ss.Train()
            }
        },
    })

Here's an ActiveRunning case:

    ss.GUI.AddToolbarItem(egui.ToolbarItem{Label: "Stop",
        Icon:    "stop",
        Tooltip: "Interrupts running.  Hitting Train again will pick back up where it left off.",
        Active:  egui.ActiveRunning,
        Func: func() {
            ss.Stop()
        },
    })

Spike Rasters

	stb := ss.GUI.TabView.AddNewTab(core.KiT_Layout, "Spike Rasters").(*core.Layout)
	stb.Lay = core.LayoutVert
	stb.SetStretchMax()
	for _, lnm := range ss.Stats.Rasters {
		sr := ss.Stats.F32Tensor("Raster_" + lnm)
		ss.GUI.ConfigRasterGrid(stb, lnm, sr)
	}

Tensor Grid (e.g., of an Image)

	tg := ss.GUI.TabView.AddNewTab(tensorcore.KiT_TensorGrid, "Image").(*tensorcore.TensorGrid)
	tg.SetStretchMax()
	ss.GUI.SetGrid("Image", tg)
	tg.SetTensor(&ss.TrainEnv.Img.Tsr)

Activation-based Receptive Fields

	ss.GUI.AddActRFGridTabs(&ss.Stats.ActRFs)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Embed

func Embed[S, C any](parent tree.Node) *S

Embed runs a sim with the default config, embedding it under the given parent node. It returns the resulting sim. *S must implement Sim[C] (interface Sim parameterized by config type C).

See also Run and RunSim.

func Run

func Run[S, C any]()

Run runs a sim of the given type S with config type C. *S must implement Sim[C] (interface Sim parameterized by config type C), and *C must implement Config.

This is a high-level helper function designed to be called as one-liner from the main() function of the sim's command subdirectory with package main. This subdirectory has the same name as the sim name itself, ex: sims/ra25 has the package with the sim logic, and sims/ra25/ra25 has the compilable main().

Run uses the config type C to make a new Config object and set its default values with [Config.Defaults].

func RunSim

func RunSim[S, C any](cfg *C) error

RunSim runs a sim with the given config. *S must implement Sim[C] (interface Sim parameterized by config type C).

Unlike Run, this does not handle command-line config parsing. End users should typically use Run, which uses RunSim under the hood.

func ScriptFieldWidget

func ScriptFieldWidget(field string) core.Value

ScriptFieldWidget is a core FieldWidget function to use a text Editor for the Params Script (or any other field named Script).

Types

type BaseConfig

type BaseConfig struct {

	// Name is the short name of the sim.
	Name string `display:"-"`

	// Title is the longer title of the sim.
	Title string `display:"-"`

	// URL is a link to the online README or other documentation for this sim.
	URL string `display:"-"`

	// Doc is brief documentation of the sim.
	Doc string `display:"-"`

	// Includes has a list of additional config files to include.
	// After configuration, it contains list of include files added.
	Includes []string

	// GUI indicates to open the GUI. Otherwise it runs automatically and quits,
	// saving results to log files.
	GUI bool `default:"true"`

	// Debug indicates to report debugging information.
	Debug bool

	// GPU indicates to use the GPU for computation. This is on by default, except
	// on web, where it is currently off by default.
	GPU bool
}

BaseConfig contains the basic configuration parameters common to all sims.

func (*BaseConfig) AsBaseConfig

func (bc *BaseConfig) AsBaseConfig() *BaseConfig

func (*BaseConfig) BaseDefaults

func (bc *BaseConfig) BaseDefaults()

BaseDefaults sets default values not specified by struct tags. It is called automatically by NewConfig.

func (*BaseConfig) IncludesPtr

func (bc *BaseConfig) IncludesPtr() *[]string

type Config

type Config interface {

	// AsBaseConfig returns the embedded [BaseConfig].
	AsBaseConfig() *BaseConfig

	// Defaults sets default values for config fields.
	// Helper functions such as [Run], [Embed], and [NewConfig] already set defaults
	// based on struct tags, so you only need to set non-tag-based defaults here.
	Defaults()
}

Config is an interface implemented by all Sim config types. To implement Config, you must embed BaseConfig. You must implement [Config.Defaults] yourself.

func NewConfig

func NewConfig[C any]() (*C, Config)

NewConfig makes a new Config of type *C with defaults set.

type GUI

type GUI struct {
	lab.Browser

	// CycleUpdateInterval is number of cycles between updates of cycle-level plots.
	CycleUpdateInterval int

	// Active is true if the GUI is configured and running
	Active bool `display:"-"`

	// NetViews are the created netviews.
	NetViews []*netview.NetView

	// SimForm displays the Sim object fields in the left panel.
	SimForm *core.Form `display:"-"`

	// Body is the entire content of the sim window.
	Body *core.Body `display:"-"`

	// Readme is the sim readme frame
	Readme *core.Frame `display:"-"`

	// OnStop is called when running is stopped through the GUI,
	// via the Stopped method. It should update the network view for example.
	OnStop func(mode, level enums.Enum)

	// StopLevel is the enum to use when the stop button is pressed.
	StopLevel enums.Enum
	// contains filtered or unexported fields
}

GUI manages all standard elements of a simulation Graphical User Interface

func NewGUIBody

func NewGUIBody(b tree.Node, sim any, fsroot fs.FS, appname, title, about string) *GUI

NewGUIBody returns a new GUI, with an initialized Body by calling [gui.MakeBody].

func (*GUI) AddLooperCtrl

func (gui *GUI) AddLooperCtrl(p *tree.Plan, loops *looper.Stacks, prefix ...string)

AddLooperCtrl adds toolbar control for looper.Stacks with Init, Run, Step controls, with selector for which stack is being controlled. A prefix can optionally be provided if multiple loops are used.

func (*GUI) AddNetView

func (gui *GUI) AddNetView(tabName string) *netview.NetView

AddNetView adds NetView in tab with given name

func (*GUI) AddToolbarItem

func (gui *GUI) AddToolbarItem(p *tree.Plan, item ToolbarItem)

AddToolbarItem adds a toolbar item but also checks when it be active in the UI

func (*GUI) FinalizeGUI

func (gui *GUI) FinalizeGUI(closePrompt bool)

FinalizeGUI wraps the end functionality of the GUI

func (*GUI) GoUpdateWindow

func (gui *GUI) GoUpdateWindow()

GoUpdateWindow triggers an update on window body, for calling from a separate goroutine.

func (*GUI) IsRunning

func (gui *GUI) IsRunning() bool

IsRunning returns the state of the isRunning flag, under a mutex.

func (*GUI) MakeBody

func (gui *GUI) MakeBody(b tree.Node, sim any, fsroot fs.FS, appname, title, about string, readme ...embed.FS)

MakeBody initializes default Body with a top-level core.Splits containing a core.Form editor of the given sim object, and a filetree for the data filesystem rooted at fsroot, and with given app name, title, and about information. The first arg is an optional existing core.Body to make into: if nil then a new body is made first. It takes an optional fs with a README.md file.

func (*GUI) NetView

func (gui *GUI) NetView() *netview.NetView

NetView returns the first created netview, or nil if none.

func (*GUI) SetStopNow

func (gui *GUI) SetStopNow()

SetStopNow sets the stopNow flag to true, under a mutex.

func (*GUI) StartRun

func (gui *GUI) StartRun()

StartRun should be called whenever a process starts running. It sets stopNow = false and isRunning = true under a mutex.

func (*GUI) StopNow

func (gui *GUI) StopNow() bool

StopNow returns the state of the stopNow flag, under a mutex.

func (*GUI) Stopped

func (gui *GUI) Stopped(mode, level enums.Enum)

Stopped is called when a run method stops running, from a separate goroutine (do not call from main event loop). Turns off the isRunning flag, calls OnStop with the given arguments, and calls GoUpdateWindow to update window state.

func (*GUI) UpdateWindow

func (gui *GUI) UpdateWindow()

UpdateWindow triggers an update on window body, to be called from within the normal event processing loop. See GoUpdateWindow for version to call from separate goroutine.

type Sim

type Sim[C any] interface {

	// SetConfig sets the sim config.
	SetConfig(cfg *C)

	ConfigSim()
	Init()
	ConfigGUI(b tree.Node)

	// Body returns the [core.Body] used by the sim.
	Body() *core.Body

	RunNoGUI()
}

Sim is an interface implemented by all sim types. It is parameterized by the config type C. *C must implement Config.

See Run, RunSim, and Embed.

type ToolGhosting

type ToolGhosting int32 //enums:enum

ToolGhosting the mode enum

const (
	ActiveStopped ToolGhosting = iota

	ActiveRunning

	ActiveAlways
)

The evaluation modes for when a tool bar can be clicked

const ToolGhostingN ToolGhosting = 3

ToolGhostingN is the highest valid value for type ToolGhosting, plus one.

func ToolGhostingValues

func ToolGhostingValues() []ToolGhosting

ToolGhostingValues returns all possible values for the type ToolGhosting.

func (ToolGhosting) Desc

func (i ToolGhosting) Desc() string

Desc returns the description of the ToolGhosting value.

func (ToolGhosting) Int64

func (i ToolGhosting) Int64() int64

Int64 returns the ToolGhosting value as an int64.

func (ToolGhosting) MarshalText

func (i ToolGhosting) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (*ToolGhosting) SetInt64

func (i *ToolGhosting) SetInt64(in int64)

SetInt64 sets the ToolGhosting value from an int64.

func (*ToolGhosting) SetString

func (i *ToolGhosting) SetString(s string) error

SetString sets the ToolGhosting value from its string representation, and returns an error if the string is invalid.

func (ToolGhosting) String

func (i ToolGhosting) String() string

String returns the string representation of this ToolGhosting value.

func (*ToolGhosting) UnmarshalText

func (i *ToolGhosting) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (ToolGhosting) Values

func (i ToolGhosting) Values() []enums.Enum

Values returns all possible values for the type ToolGhosting.

type ToolbarItem

type ToolbarItem struct {
	Label   string
	Icon    icons.Icon
	Tooltip string
	Active  ToolGhosting
	Func    func()
}

ToolbarItem holds the configuration values for a toolbar item

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL