2.2.57 • Published 3 days ago

@thi.ng/imgui v2.2.57

Weekly downloads
89
License
Apache-2.0
Repository
github
Last release
3 days ago

imgui

npm version npm downloads Twitter Follow

This project is part of the @thi.ng/umbrella monorepo.

About

Immediate mode GUI with flexible state handling & data only shape output.

screenshot

Currently still somewhat bare-bones, but already usable & customizable immediate mode GUI implementation, primarily for @thi.ng/hdom-canvas / @thi.ng/hiccup-canvas and @thi.ng/webgl, however with no direct dependency on either and only outputting data structures.

IMGUI components are largely ephemeral and expressed as simple functions, producing a visual representation of a user state value. IMGUIs are reconstructed from scratch each frame and don't exist otherwise (apart from some rudimentary input state & config). If a component's function isn't called again, it won't exist in the next frame shown to the user. Components only return a new value if an user interaction produced a change. Additionally, each component produces a number of shapes & text labels, all of which are collected internally and are, from the user's POV, a mere side effect. At the end of the update cycle IMGUI produces a tree of @thi.ng/hiccup-canvas compatible elements, which can be easily converted into other formats (incl. SVG).

Note: The WebGL conversion still in the early stages and not yet published, pending ongoing development in other packages...

Current features

  • No direct user state mutation (unlike most other IMGUI impls)
  • Flexible & nestable grid layout with support for cell-spans
  • Theme stack for scoped theme switches / overrides
  • Stack for scoped disabled GUI elements & to create modals
  • Hashing & caching of component local state & draws shapes / resources
  • Hover-based mouse cursor overrides
  • Hover tooltips
  • Re-usable hover & activation behaviors (for creating new components)
  • Fully keyboard controllable & Tab-focus switching / highlighting
  • All built-in components based on @thi.ng/geom shape primitives

Available components / widgets

The above screenshot shows most of the currently available components:

  • Push button (horizontal / vertical)
  • Icon button (w/ opt text label)
  • 2x dial types & dial groups (h / v)
  • Dropdown
  • Radial menu
  • Radio button group (h / v)
  • Slider & slider groups (h / v)
  • Text input (single line, filtered input)
  • Text label
  • Toggle button
  • XY pad

All components are:

  • Skinnable (via theme)
  • Keyboard controllable (incl. focus switching)
  • Support tooltips

State handling

All built-in components only return a result value if the component was interacted with and would result in a state change (i.e. a slider has been dragged or button pressed). So, unlike the traditional IMGUI pattern (esp. in languages with pointer support), none of the components here directly manipulate user state and this task is left entirely to the user. This results in somewhat slightly more verbose code, but offers complete freedom WRT how user state is & can be organized. Also, things like undo / redo become easier to handle this way.

// example state (see @thi.ng/atom)
const STATE = new History(new Atom({ foo: true }));

...
// get atom snapshot
const curr = STATE.deref();

// toggle component will only return result if user clicked it
let res = toggle(gui, layout, "foo", curr.foo, false, curr.foo ? "ON" : "OFF");
// conditional immutable update (w/ automatic undo snapshot)
res !== undefined && STATE.resetIn("foo", res);

Layout support

Most component functions exist in two versions: Using a @thi.ng/layout-compatible grid layout manager or not (e.g. dial vs. dialRaw). The latter versions are more "low-level" & verbose to use, but offer complete layout freedom and are re-used by other component types.

The components in this package not needing a layout manager are only expecting a ILayout or IGridLayout interface, allowing for custom implementations. Furthermore / alternatively, the @thi.ng/layout package also defines a LayoutBox interface, which can be passed instead and too is the type ILayout implementations are expected to produce when allocating space for a component.

The GridLayout class supports infinite nesting and column/row-based space allocation, based on an initial configuration and supporting multiple column/row spans.

screenshot

The code producing this structure:

// create a single column layout @ position 10,10 / 200px wide
// the last values are row height and cell spacing
const layout = gridLayout(10, 10, 200, 1, 16, 4);

// get next layout box (1st row)
// usually you don't need to call .next() manually, but merely pass
// the layout instance to a component...
layout.next();
// { x: 10, y: 10, w: 200, h: 16, cw: 200, ch: 16, gap: 4 }

// 2nd row
layout.next();
// { x: 10, y: 30, w: 200, h: 16, cw: 200, ch: 16, gap: 4 }

// create nested 2-column layout (3rd row)
const twoCols = layout.nest(2);

twoCols.next();
// { x: 10, y: 50, w: 98, h: 16, cw: 98, ch: 16, gap: 4 }

twoCols.next();
// { x: 112, y: 50, w: 98, h: 16, cw: 98, ch: 16, gap: 4 }

// now nest 3-columns in the 1st column of twoCols
// (i.e. now each column is 1/6th of the main layout's width)
const inner = twoCols.nest(3);

// allocate with col/rowspan, here 1 column x 4 rows
inner.next([1, 4])
// { x: 10, y: 70, w: 30, h: 76, cw: 30, ch: 16, gap: 4 }
inner.next([1, 4])
// { x: 44, y: 70, w: 30, h: 76, cw: 30, ch: 16, gap: 4 }
inner.next([1, 4])
// { x: 78, y: 70, w: 30, h: 76, cw: 30, ch: 16, gap: 4 }

// back to twoCols (2nd column)
twoCols.next([1, 2]);
// { x: 112, y: 70, w: 98, h: 36, cw: 98, ch: 16, gap: 4 }

Key controls

The entire UI is fully keyboard controllable, built-in behaviors:

KeysScopeDescription
Tab / Shift+TabGlobalSwitch focus
Enter / SpaceGlobalActivate focused button
Up / Down or drag mouseSlider, Dial, XYAdjust value
Shift+Up/DownSlider, Dial, XYAdjust value (5x step)
Left/RightRadial menuNavigate menu CW/CCW
Left/RightTextfieldMove cursor to prev/next word
Left/RightXYAdjust X value
Alt+Left/RightTextfieldMove cursor to prev/next word

More complex behaviors can be achieved in user land. E.g. in the demo, holding down Alt whilst adjusting a slider or dial group will set all values uniformly...

Current limitations

Some of the most obvious missing features:

  • variable width font support (currently monospace only)
  • more granular theme options
  • theme-aware layouting (font size, padding etc.)
  • image / texture support (Tex ID abstraction)
  • windows / element containers
  • menu / tree components
  • scrolling / clipping
  • drag & drop

Status

STABLE - used in production

Search or submit any issues for this package

Related packages

Installation

yarn add @thi.ng/imgui

ES module import:

<script type="module" src="https://cdn.skypack.dev/@thi.ng/imgui"></script>

Skypack documentation

For Node.js REPL:

# with flag only for < v16
node --experimental-repl-await

> const imgui = await import("@thi.ng/imgui");

Package sizes (gzipped, pre-treeshake): ESM: 6.90 KB

Dependencies

Usage examples

Several demos in this repo's /examples directory are using this package.

A selection:

ScreenshotDescriptionLive demoSource
Interactive inverse FFT toy synthDemoSource
Canvas based Immediate Mode GUI componentsDemoSource
Minimal IMGUI usage exampleDemoSource

API

Generated API docs

TODO

Authors

Karsten Schmidt

If this project contributes to an academic publication, please cite it as:

@misc{thing-imgui,
  title = "@thi.ng/imgui",
  author = "Karsten Schmidt",
  note = "https://thi.ng/imgui",
  year = 2019
}

License

© 2019 - 2021 Karsten Schmidt // Apache Software License 2.0

2.2.57

3 days ago

2.2.56

5 days ago

2.2.55

8 days ago

2.2.54

17 days ago

2.2.53

20 days ago

2.2.52

30 days ago

2.2.51

1 month ago

2.2.50

1 month ago

2.2.49

1 month ago

2.2.48

1 month ago

2.2.47

1 month ago

2.2.46

1 month ago

2.2.45

2 months ago

2.2.44

2 months ago

2.2.43

2 months ago

2.2.42

2 months ago

2.2.41

2 months ago

2.2.40

2 months ago

2.2.39

2 months ago

2.2.37

2 months ago

2.2.38

2 months ago

2.2.35

2 months ago

2.2.36

2 months ago

2.2.33

2 months ago

2.2.34

2 months ago

2.2.32

2 months ago

2.2.31

3 months ago

2.2.30

3 months ago

2.2.28

3 months ago

2.2.29

3 months ago

2.2.27

3 months ago

2.2.25

3 months ago

2.2.24

3 months ago

2.2.22

4 months ago

2.2.23

4 months ago

2.2.21

4 months ago

2.2.17

4 months ago

2.2.18

4 months ago

2.2.16

4 months ago

2.2.19

4 months ago

2.2.20

4 months ago

2.2.15

5 months ago

2.2.14

5 months ago

2.2.13

5 months ago

2.1.87

7 months ago

2.1.88

7 months ago

2.1.85

7 months ago

2.1.86

7 months ago

2.1.83

8 months ago

2.1.84

7 months ago

2.1.81

8 months ago

2.1.82

8 months ago

2.1.80

8 months ago

2.2.1

6 months ago

2.2.0

6 months ago

2.2.3

6 months ago

2.2.2

6 months ago

2.2.5

6 months ago

2.2.4

6 months ago

2.2.7

6 months ago

2.2.6

6 months ago

2.2.9

6 months ago

2.2.11

5 months ago

2.2.12

5 months ago

2.2.10

6 months ago

2.1.69

9 months ago

2.1.68

10 months ago

2.1.78

8 months ago

2.1.79

8 months ago

2.1.76

8 months ago

2.1.77

8 months ago

2.1.74

9 months ago

2.1.75

9 months ago

2.1.72

9 months ago

2.1.73

9 months ago

2.1.70

9 months ago

2.1.67

11 months ago

2.1.66

11 months ago

2.1.65

12 months ago

2.1.63

1 year ago

2.1.64

1 year ago

2.1.58

1 year ago

2.1.59

1 year ago

2.1.56

1 year ago

2.1.57

1 year ago

2.1.54

1 year ago

2.1.55

1 year ago

2.1.61

1 year ago

2.1.62

1 year ago

2.1.60

1 year ago

2.1.52

1 year ago

2.1.53

1 year ago

2.1.51

1 year ago

2.1.50

1 year ago

2.1.49

1 year ago

2.1.47

1 year ago

2.1.45

1 year ago

2.1.46

1 year ago

2.1.43

1 year ago

2.1.44

1 year ago

2.1.41

1 year ago

2.1.42

1 year ago

2.1.38

1 year ago

2.1.39

1 year ago

2.1.36

1 year ago

2.1.37

1 year ago

2.1.34

2 years ago

2.1.35

2 years ago

2.1.32

2 years ago

2.1.33

2 years ago

2.1.30

2 years ago

2.1.31

2 years ago

2.1.40

1 year ago

2.1.28

2 years ago

2.1.29

2 years ago

2.1.27

2 years ago

2.1.16

2 years ago

2.1.17

2 years ago

2.1.15

2 years ago

2.1.18

2 years ago

2.1.19

2 years ago

2.1.25

2 years ago

2.1.26

2 years ago

2.1.23

2 years ago

2.1.24

2 years ago

2.1.21

2 years ago

2.1.22

2 years ago

2.1.20

2 years ago

2.1.14

2 years ago

2.1.12

2 years ago

2.1.13

2 years ago

2.1.10

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.8

2 years ago

2.1.2

2 years ago

2.1.1

2 years ago

2.1.4

2 years ago

2.1.3

2 years ago

2.1.6

2 years ago

2.1.5

2 years ago

2.1.7

2 years ago

2.0.10

2 years ago

2.1.0

2 years ago

2.0.9

2 years ago

2.0.8

2 years ago

2.0.5

3 years ago

2.0.4

3 years ago

2.0.7

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

0.2.76

3 years ago

0.2.74

3 years ago

0.2.75

3 years ago

0.2.73

3 years ago

0.2.72

3 years ago

0.2.71

3 years ago

0.2.70

3 years ago

0.2.69

3 years ago

0.2.68

3 years ago

0.2.67

3 years ago

0.2.66

3 years ago

0.2.65

3 years ago

0.2.64

3 years ago

0.2.63

3 years ago

0.2.62

3 years ago

0.2.61

3 years ago

0.2.60

3 years ago

0.2.59

3 years ago

0.2.55

3 years ago

0.2.54

3 years ago

0.2.53

3 years ago

0.2.52

3 years ago

0.2.51

3 years ago

0.2.50

3 years ago

0.2.49

3 years ago

0.2.48

3 years ago

0.2.47

3 years ago

0.2.46

3 years ago

0.2.45

3 years ago

0.2.44

3 years ago

0.2.43

3 years ago

0.2.42

3 years ago

0.2.41

4 years ago

0.2.40

4 years ago

0.2.39

4 years ago

0.2.38

4 years ago

0.2.37

4 years ago

0.2.36

4 years ago

0.2.35

4 years ago

0.2.34

4 years ago

0.2.33

4 years ago

0.2.32

4 years ago

0.2.30

4 years ago

0.2.31

4 years ago

0.2.29

4 years ago

0.2.28

4 years ago

0.2.27

4 years ago

0.2.26

4 years ago

0.2.25

4 years ago

0.2.24

4 years ago

0.2.23

4 years ago

0.2.22

4 years ago

0.2.21

4 years ago

0.2.20

4 years ago

0.2.19

4 years ago

0.2.18

4 years ago

0.2.17

4 years ago

0.2.16

4 years ago

0.2.15

4 years ago

0.2.14

4 years ago

0.2.13

4 years ago

0.2.12

4 years ago

0.2.11

4 years ago

0.2.10

4 years ago

0.2.9

4 years ago

0.2.8

4 years ago

0.2.7

4 years ago

0.2.6

4 years ago

0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

4 years ago

0.1.7

4 years ago

0.1.6

4 years ago

0.1.5

4 years ago

0.1.4

4 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago