Genta ImDemo - Lua
Genta ImDemo - Lua
-- It is suggested to look at the window created by this script while reading this.
function imDemo(deltaTime)
ImGui.Begin("ImLua")
-- This function is called for every frame. Use this function to create your
GUI.n
-- so you know which of these windows is currently being built.
-- If you additionally need the current width and height of the window, use
these functions:
local win_width = ImGui.GetWindowWidth()
local win_height = ImGui.GetWindowHeight()
-- These function return the window's width and height in boxels.
-- The easiest thing we can do in imgui is drawing text. The function is called
TextUnformatted because
-- it doesn't support C-style format strings. imgui has a function called
Text() that does support these
-- format strings, but it is not supported in lua.
ImGui.TextUnformatted("Hello, World! Imgui 1.82 demo for GENTA HAX")
ImGui.Text("Delta Time: "..deltaTime)
-- Since TextUnformatted does not support format strings, you can use lua to
create a string on the fly:
ImGui.TextUnformatted("Window size: " .. win_width .. ", " .. win_height)
-- Restore the previous style, this _must_ be called after each pushed
style:
ImGui.PopStyleColor()
-- This function must be called at the end of every tree node to tell imgui
that the code after
-- it is not part of the TreeNode anymore.
ImGui.TreePop()
end
if ImGui.TreeNode("Buttons") then
-- The following function creates a button:
if ImGui.Button("Push Me") then
-- The Button function creates a button with the given label. It
returns true in the
-- moment where the button is released, i.e. the if statement is used
to check whether
-- the action behind the button should now be executed. Let's make the
button toggle a variable:
makeRed = not makeRed
end
-- Use the variable to change the color of the text
if makeRed then
ImGui.PushStyleColor(ImGui.Col.Text, 0xFF0000FF)
else
ImGui.PushStyleColor(ImGui.Col.Text, 0xFF00FF00)
end
ImGui.TextUnformatted("Some Text")
ImGui.PopStyleColor()
-- If you want to trigger the action while the button is clicked and not
only when it is released:
ImGui.Button("Click and hold")
if ImGui.IsItemActive() then
makeRed = not makeRed
end
end
-- Keep the next button on the same line.
ImGui.SameLine()
-- Control position of next button
ImGui.SetCursorPosX(150)
end
ImGui.SameLine()
ImGui.SetCursorPosX(150 * 2)
end
ImGui.TreePop()
end
if ImGui.TreeNode("Checkboxes") then
-- The following function creates a checkbox:
local changed, newVal = ImGui.Checkbox("Make text red", makeRed)
-- The first parameter is the caption of the checkbox, it will be displayed
right of the checkbox.
-- The second parameter is the current boolean value.
-- The function returns two values: a bool to indicate whether the value
was changed and the new value.
if changed then
makeRed = newVal
end
if makeRed then
ImGui.PushStyleColor(ImGui.Col.Text, 0xFF0000FF)
else
ImGui.PushStyleColor(ImGui.Col.Text, 0xFF00FF00)
end
ImGui.TextUnformatted("Some Text")
ImGui.PopStyleColor()
ImGui.TreePop()
end
if ImGui.TreeNode("Sliders") then
-- The following function creates a slider:
local changed, newVal = ImGui.SliderFloat("Slider Caption", sliderVal, 0,
100000, "Value: %.2f")
-- The first parameter is the caption of the slider, it will be displayed
right of the slider.
-- The second parameter is the current position. Pass the variable that
should be changed by the slider.
-- The third and fourth parameter specify the range of the slider, i.e. the
min and max value.
-- The fourth parameter is a format string to show on the slider. Must
contain a variant of "%f" in the text.
-- The example format string rounds the value to two decimal places and
prefixes the value with "Value: " on the slider.
-- The function returns two values: a bool to indicate whether the value
was changed and the new value. You must copy
-- the new value into the slider variable if the value changed:
if changed then
sliderVal = newVal
end
-- For non-decimals:
local changed, newVal = ImGui.SliderInt("Int Slider", sliderVal, 0, 100000,
"Value: %.0f")
if changed then
sliderVal = newVal
end
-- For angles:
local changed, newVal = ImGui.SliderAngle("Angle Slider", angle, -180, 180,
"Value: %.0f")
if changed then
angle = newVal
end
ImGui.TreePop()
end
if ImGui.TreeNode("ComboBox") then
-- BeginCombo starts a combo box. The first parameter is the label, the
-- second parameter the text of the currently selected choice.
if ImGui.BeginCombo("Combo Box", choices[choice]) then
-- Loop over all choices
for i = 1, #choices do
-- The Selectable function adds a choice to a combobox.
-- The first parameter is the label, the second one
-- should be true if this choice is currently selected.
if ImGui.Selectable(choices[i], choice == i) then
-- Selectable returns true when a new choice was selected,
-- we should then copy the choice into our choice variable
choice = i
end
end
-- _Must_ be called if and only if BeginCombo returns true, i.e.
-- when the combobox is currently open
ImGui.EndCombo()
end
ImGui.TreePop()
end
if changed then
sliderVal = newVal
end
ImGui.TreePop()
end
if ImGui.TreeNode("Input") then
-- Allow the user to input text
local changed, newText = ImGui.InputText("One Line Text", text, 255)
-- Parameters: Label, current text, maximum number of allowed characters
if changed then
text = newText
end
if changed then
text2 = newText2
end
if changed then
text3 = newText3
end
ImGui.TreePop()
end
if ImGui.TreeNode("ListBox") then
local choices = {"Choice 1", "Choice 2", "Choice 3"}
-- BeginListBox starts a list box. The first parameter is the label, the
-- second parameter the text of the currently selected choice.
if ImGui.BeginListBox("List Box", choices[choice]) then
-- Loop over all choices
for i = 1, #choices do
-- The Selectable function adds a choice to a listbox.
-- The first parameter is the label, the second one
-- should be true if this choice is currently selected.
if ImGui.Selectable(choices[i], choice == i) then
-- Selectable returns true when a new choice was selected,
-- we should then copy the choice into our choice variable
choice = i
end
end
-- _Must_ be called if and only if BeginListBox returns true, i.e.
-- when the listbox is currently open
ImGui.EndListBox()
end
ImGui.TreePop()
end
if ImGui.TreeNode("Columns") then
ImGui.Columns(3)
ImGui.TextUnformatted("first column")
ImGui.NextColumn()
ImGui.TextUnformatted("second column")
ImGui.NextColumn()
ImGui.TextUnformatted("third column")
ImGui.NextColumn()
ImGui.TextUnformatted("first column second line")
ImGui.NextColumn()
ImGui.TextUnformatted("second column second line")
ImGui.NextColumn()
ImGui.TextUnformatted("third column second line")
ImGui.NextColumn()
ImGui.TextUnformatted("first column third line")
ImGui.Columns()
ImGui.TreePop()
end
if ImGui.TreeNode("Tables") then
-- Here we will showcase three different ways to output a table.
-- They are very simple variations of a same thing!
-- [Method 1] Using TableNextRow() to create a new row, and
TableSetColumnIndex() to select the column.
-- In many situations, this is the most flexible and easy to use pattern.
-- ImGui.TextUnformatted("Table 1")
-- ImGui.BeginTable("table1", 3)
-- for row = 0, 4, 1
-- do
-- ImGui.TableNextRow()
-- for column = 0, 3, 1
-- do
-- ImGui.TableSetColumnIndex(column)
-- ImGui.TextUnformatted("Row " .. row .. " Column " .. column)
-- end
-- end
-- ImGui.EndTable()
-- ImGui.TextUnformatted("")
-- ImGui.TextUnformatted("")
ImGui.TreePop()
end
if ImGui.TreeNode("Fonts") then
ImGui.PushStyleColor(ImGui.Col.Text, 0xFFA89300) -- Strong Artic Blue Text
ImGui.TextUnformatted("We cannot curently change the font we can change the
color")
ImGui.SetWindowFontScale(1.5)
ImGui.TextUnformatted("We can also change the scale")
ImGui.SetWindowFontScale(1.0)
ImGui.TextUnformatted("Because this is the font scale for the window we
need to make sure")
ImGui.TextUnformatted("we have changed it back to the default of 1.0.")
ImGui.PopStyleColor()
ImGui.TreePop()
end
if ImGui.TreeNode("Misc") then
-- Create a bullet style enumeration
ImGui.Bullet(); ImGui.TextUnformatted("Bullet")
ImGui.Bullet(); ImGui.TextUnformatted("Style")
ImGui.Bullet(); ImGui.TextUnformatted("Enumeration")
-- Note that caption strings must be unique inside a window! If you need to
have
-- multiple widgets with the same caption, append ## followed by a unique
id for an
-- invisible caption extension:
ImGui.Button("Button##1")
ImGui.Button("Button##2")
ImGui.TreePop()
end
ImGui.End()
end