> For the complete documentation index, see [llms.txt](https://docs.anvil.team/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.anvil.team/namespaces/imgui.md).

# ImGui

{% hint style="info" %}
Not all ImGui methods or enums are available.\
If you're in need of a specific method, you can ask on our Discord and we will try\
to add support for it as soon as possible.
{% endhint %}

### <mark style="color:green;">`ImGui.Begin(id)`</mark>

* <mark style="color:purple;">`id`</mark> - The id of the window

Begins a new ImGui Window with the given id.

**Example:**

```lua
function render()
	ImGui.Begin("mymenu")
        ImGui.Text("some text")
        ImGui.End()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.Begin(id, windowFlags)`</mark>

* <mark style="color:purple;">`id`</mark> - The id of the window
* <mark style="color:purple;">`windowFlags`</mark> - The [flags](/enums/imguiwindowflags.md) for the window

Begins a new ImGui Window with the given id and additional window flags. \
For a list of available windowflags, [check here](/enums/imguiwindowflags.md).

**Example:**

```lua
function render()
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.End()`</mark>

Ends a ImGui Window.

**Example:**

```lua
function render()
	ImGui.Begin("mymenu")
        ImGui.Text("some text")
        ImGui.End()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PushStyleColor(colorIndex, color)`</mark>

* <mark style="color:purple;">`colorIndex`</mark> - The color index as a [ImGuiCol](/enums/imguicol.md)
* <mark style="color:purple;">`color`</mark> - The color as a [Vec4](/types/vec4.md)

Pushes a style color identified by the colorIndex. For available indices [check here](/enums/imguicol.md).\
All following rendered elements targeted by the index will have this color applied to them.

{% hint style="danger" %}
For each time you push a style color, you WILL have to use [ImGui.PopStyleColor](#imgui.pushstylecolor-colorindex-color-1) after you're doing using the given color for elements.
{% endhint %}

**Example:**

```lua
function render()
        ImGui.PushStyleColor(ImGuiCol.WindowBg, Vec4:new(255, 0, 0, 255))
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
        ImGui.PopStyleColor()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PopStyleColor(count)`</mark>

* <mark style="color:purple;">`count`</mark> - How many style colors to pop of the color stack

Pops a number of style colors of the color stack.&#x20;

**Example:**

```lua
function render()
        ImGui.PushStyleColor(ImGuiCol.WindowBg, Vec4:new(255, 0, 0, 255))
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
        ImGui.PopStyleColor()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PushStyleVarFloat(varIndex, floatValue)`</mark>

* <mark style="color:purple;">`varIndex`</mark> - The variable index as a [ImGuiStyleVar](/enums/imguistylevar.md)
* <mark style="color:purple;">`floatValue`</mark> - The value to set the var to

Pushes a style var identified by the varIndex. For available indices [check here](/enums/imguistylevar.md).\
All following rendered elements targeted by the index will have this style variable applied to them.

{% hint style="danger" %}
For each time you push a style variable, you WILL have to use <mark style="color:green;">ImGui.PopStyleVar</mark> after you're doing using the given style variable for elements.
{% endhint %}

**Example:**

```lua
function render()
        ImGui.PushStyleVarFloat(ImGuiStyleVar.WindowRounding, 10.0)
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
        ImGui.PopStyleVar()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PushStyleVarVec2(varIndex, vec2Value)`</mark>

* <mark style="color:purple;">`varIndex`</mark> - The variable index as a [ImGuiStyleVar](/enums/imguistylevar.md)
* <mark style="color:purple;">`vec2Value`</mark> - The [Vec2](/types/vec2.md) value to set the var to

Pushes a style var identified by the varIndex. For available indices [check here](/enums/imguistylevar.md).\
All following rendered elements targeted by the index will have this style variable applied to them.

{% hint style="danger" %}
For each time you push a style variable, you WILL have to use <mark style="color:green;">ImGui.PopStyleVar</mark> after you're doing using the given style variable for elements.
{% endhint %}

**Example:**

```lua
function render()
        ImGui.PushStyleVarVec2(ImGuiStyleVar.WindowPadding, Vec2:new(16, 16))
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
        ImGui.PopStyleVar()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PopStyleVar(count)`</mark>

* <mark style="color:purple;">`count`</mark> - How many style variables to pop of the style variable stack

Pops a number of style variable of the style variable stack.&#x20;

**Example:**

```lua
function render()
        ImGui.PushStyleVarVec2(ImGuiStyleVar.WindowPadding, Vec2:new(16, 16))
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("some text")
        ImGui.End()
        ImGui.PopStyleVar()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PushFont(name, size)`</mark>

* <mark style="color:purple;">`name`</mark> - The name of the font
* <mark style="color:purple;">`size`</mark> - The size in pixels of the font

**Available Fonts:**

* Verdana
* VerdanaBold
* Impact
* Beyblade
* Dameron
* RobotoMono-Light

**Available Font sizes:**

* <mark style="color:purple;">12, 14, 16, 18, 24, 32, 36, 48, 60</mark>

Pushes a font. All following elements containing text will have this font applied until it is popped\
with ImGui.PopFont

{% hint style="warning" %}
Make sure to use <mark style="color:green;">ImGui.PopFont</mark> after you're done using the font. \
**For&#x20;**<mark style="color:yellow;">**one**</mark>**&#x20;PushFont you want&#x20;**<mark style="color:yellow;">**one**</mark>**&#x20;PopFont.**
{% endhint %}

**Example:**

```lua
function render()
        ImGui.PushFont("VerdanaBold", 32)
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("This is written in Verdana Bold.")
        ImGui.End()
        ImGui.PopFont()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.PopFont()`</mark>

Pops a font.&#x20;

**Example:**

```lua
function render()
         ImGui.PushFont("VerdanaBold", 32)
	ImGui.Begin("mymenu", ImGuiWindowFlags.NoTitleBar)
        ImGui.Text("This is written in Verdana Bold.")
        ImGui.End()
        ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.GetTexture(name)`</mark>

* <mark style="color:purple;">`name`</mark> - The name of the texture.

Returns a pointer to a IDirect3DTexture9. This can be directly used wherever the ImGui lua bindings expect a texture. For example ImGui.Image

#### Custom Textures

Custom Textures can be loaded the following way:

1. Locate the directory C:\Users\\`your user`\Anvil
2. Create a new directory called <mark style="color:green;">texture\_assets</mark>
3. You can store any images of format `.png`, `.jpg` or `.jpeg` in this folder
4. The name of each file will be the key which you pass to <mark style="color:blue;">ImGui.GetTexture</mark>

**Custom Textures Example Scenario**

Suppose you put a file called <mark style="color:yellow;">icon.png</mark> into the <mark style="color:green;">texture\_assets</mark> directory.\
To get access to this image as a texture that you can pass to other ImGui functions, do the following:

```lua
ImGui.GetTexture("icon")
```

**Full Example:**

```lua
function render()
	ImGui.Begin("mymenu")
        ImGui.Image(ImGui.GetTexture("icon"))
        ImGui.End()
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.BeginMenuBar()`</mark>

Begin a menu bar.

{% hint style="danger" %}
You will have to pass the <mark style="color:yellow;">ImGuiWindowFlags.MenuBar</mark> flag to the <mark style="color:green;">ImGui.Begin</mark>&#x20;

in order to see the menubar!
{% endhint %}

### <mark style="color:green;">`ImGui.EndMenuBar()`</mark>

Ends a menu bar.

### <mark style="color:green;">`ImGui.BeginMenu(menuName)`</mark>

* <mark style="color:purple;">`menuName`</mark> - The name of the menu.

Begin a menu.

### <mark style="color:green;">`ImGui.EndMenu()`</mark>

End a menu bar.

### <mark style="color:green;">`ImGui.MenuItem(label, shortCut, selected)`</mark>

* <mark style="color:purple;">`label`</mark> - The label of the menu item
* <mark style="color:purple;">`shortCut`</mark> - Shortcut.
* <mark style="color:purple;">`selected`</mark> - If the menu item is selected.

**Example:**

```lua
local mOpen = false
local mSave = false

local function render()

	ImGui.PushStyleColor(ImGuiCol.WindowBg, Vec4:new(0, 0, 0, 1))
	ImGui.Begin("lua window")
	if (ImGui.BeginMenuBar()) then
		if (ImGui.BeginMenu("File")) then
			mOpen = ImGui.MenuItem("Open", "", mOpen)
        		mSave = ImGui.MenuItem("Save", "", mSave)
			ImGui.EndMenu()
		end
		ImGui.EndMenuBar()
	end

	ImGui.End()
	ImGui.PopStyleColor(1)

end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.SameLine(offsetFromStart, spacing)`</mark>

Use this to have the next ImGui Element on the same "line" meaning vertical position.

* <mark style="color:purple;">`offsetFromStart`</mark> - align to specified x position
* <mark style="color:purple;">`spacing`</mark> - horizontal spacing amount in pixels

**Example:**

```lua
function render()
         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
         ImGui.Text("Hello WOrld.")
         ImGui.SameLine(0, 16)
         ImGui.Text("I am written on the same line.")
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.Image(texture, size)`</mark>

Renders a texture with the given size.

* <mark style="color:purple;">`texture`</mark> - The texture to use. Use [<mark style="color:green;">ImGui.GetTexture</mark>](#imgui.gettexture-name) to obtain a texture.
* <mark style="color:purple;">`size`</mark> - The size of the image as a [Vec2](/types/vec2.md)

**Example:**

```lua
function render()
         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
         ImGui.Image(ImGui.GetTexture("superman"), Vec2:new(128, 128))
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.Text(text)`</mark>

Renders the given text.

* <mark style="color:purple;">`text`</mark> - The text to render

**Example:**

```lua
function render()
         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
         ImGui.Text(string.format("Hello my name is %s", "Jeff"))
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.calcTextSize(text)`</mark>

Returns the size of the given text in the context of the currently used font as a Vec2.

* <mark style="color:purple;">`text`</mark> - The text&#x20;

**Example:**

```lua
function render()
         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
	 local text = string.format("Hello my name is %s", "Jeff")
	 local textSize = ImGui.calcTextSize(text)
         ImGui.Text(string.format("The text has a size of %s", textSize:toString()))
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
```

### <mark style="color:green;">`ImGui.Checkbox(text, state)`</mark>

Renders a checkbox with the given text as label.\
Returns a tuple of two boolean values.

The first boolean indicates if the checkbox was clicked this frame.\
The second boolean indicates the current state of the checkbox.

* <mark style="color:purple;">`text`</mark> - The label
* <mark style="color:purple;">`state`</mark> - The current state of the checkbox

**Example:**

<pre class="language-lua"><code class="lang-lua"><strong>local useBhop = false;
</strong><strong>local useBhopChanged = false;
</strong><strong>function render()
</strong>         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
         useBhopChanged, useBhop = ImGui.Checkbox("Enable bhop", useBhop)
         if (useBhopChanged) then
                  print(string.format("Bhop checkbox changed state and is now: %s", tostring(useBhop)))
         end
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
</code></pre>

### <mark style="color:green;">`ImGui.Button(text, [size])`</mark>

Renders a checkbox with the given text as label.\
Returns a tuple of two boolean values.

The first boolean indicates if the checkbox was clicked this frame.\
The second boolean indicates the current state of the checkbox.

* <mark style="color:purple;">`label`</mark> - The label
* <mark style="color:purple;">`size`</mark> - The size of the button. <mark style="color:yellow;">Optional.</mark>

**Example:**

<pre class="language-lua"><code class="lang-lua"><strong>function render()
</strong>         ImGui.PushFont("VerdanaBold", 16)
	 ImGui.Begin("mymenu")
         if (ImGui.Button("Do something")) then
                  print("Button was clicked")
                  --Do some code...
         end
         if (ImGui.Button("Big button", Vec2:new(300, 300))) then
                  print("Big Button was clicked")
         end
         ImGui.End()
         ImGui.PopFont() --This is important here
end

engine.addEventListener("render", render)
</code></pre>

### <mark style="color:green;">`ImGui.GetRandomColor([alphaFloat])`</mark>

Returns a random color.

* <mark style="color:purple;">`alphaFloat`</mark> - The alpha value of the color, expects 0 - 1. <mark style="color:yellow;">Optional.</mark>

**Example:**

```lua
local color = ImGui.GetRandomColor() -- Alpha 1.0
local transparentColor = ImGui.GetRandomColor(0.5) --Transparent 

function render()
	draw.circleFilled(Vec2:new(200,300), 60, color, 20)
	draw.circleFilled(Vec2:new(200,400), 60, transparentColor, 20)
end

engine.addEventListener("render", render)
```
