Skip to content

Commit

Permalink
Add ImGui input controls for plugins (#1182)
Browse files Browse the repository at this point in the history
Add bindings for ImGui input functions. This will at least let plugins do some basic input instead of checkboxes and buttons.
Rearrange files for the imgui project to `inc` and `src` - will probably do this for the rest of the projects at some point too.
#1129
  • Loading branch information
chreden authored Sep 24, 2023
1 parent 4803978 commit e44b785
Show file tree
Hide file tree
Showing 13 changed files with 654 additions and 112 deletions.
194 changes: 186 additions & 8 deletions doc/lua/imgui.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,20 @@ The ImGui library allows plugins to render UI elements.
| Button |`boolean result`|`{ string label, ImGuiButtonFlags flags }`||
| Checkbox | `boolean result, boolean checked` | `{ string label, boolean checked }` | Make a checkbox. The checked parameter indicates whether the checkbox is checked and the new state is in the second return value. |
| End ||| End rendering a window |
| EndChild ||||
| EndChild ||| End a child window |
| EndCombo ||||
| EndTable ||||
| SameLine ||||
| InputDouble | `boolean result, number value` | `{ string label, number value, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputFloat | `boolean result, number value` | `{ string label, number value, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputFloat2 | `boolean result, number value1, number value2` | `{ string label, number value1, number value2, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputFloat3 | `boolean result, number value1, number value2, number value3` | `{ string label, number value1, number value2, number value3, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputFloat4 | `boolean result, number value1, number value2, number value3, number value4` | `{ string label, number value1, number value2, number value3, number value4, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputInt | `boolean result, number value` | `{ string label, number value, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputInt2 | `boolean result, number value1, number value2` | `{ string label, number value1, number value2 [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputInt3 | `boolean result, number value1, number value2, number value3` | `{ string label, number value1, number value2, number value3, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputInt4 | `boolean result, number value1, number value2, number value3, number value4` | `{ string label, number value1, number value2, number value3, number value4, [optional] number step, [optional] number step_fast, [optional] string format, [optional] InputTextFlags flags }` ||
| InputText | `boolean result, string value` | `{ string label, string value, [optional] InputTextFlags flags }` ||
| SameLine ||| Put the next element on the same line. |
| Selectable |`boolean result`|`{ string label, boolean selected, ImGuiSelectableFlags flags }`||
| TableHeadersRow ||||
| TableNextColumn |`boolean result`|||
Expand All @@ -27,14 +37,182 @@ The ImGui library allows plugins to render UI elements.

# Enumerations

## ImGuiWindowFlags
## WindowFlags

## ImGuiComboFlags
| Name |
| ---- |
| None |
| NoTitleBar |
| NoResize |
| NoMove |
| NoScrollbar |
| NoScrollWithMouse |
| NoCollapse |
| AlwaysAutoResize |
| NoBackground |
| NoSavedSettings |
| NoMouseInputs |
| MenuBar |
| HorizontalScrollbar |
| NoFocusOnAppearing |
| NoBringToFrontOnFocus |
| AlwaysVerticalScrollbar |
| AlwaysHorizontalScrollbar |
| AlwaysUseWindowPadding |
| NoNavInputs |
| NoNavFocus |
| UnsavedDocument |
| NoDocking |

## ImGuiSelectableFlags

## ImGuiTableColumnFlags
## ComboFlags

## ImGuiTableFlags
| Name |
| ---- |
| None |
| PopupAlignLeft |
| HeightSmall |
| HeightRegular |
| HeightLarge |
| HeightLargest |
| NoArrowButton |
| NoPreview |

## ImGuiButtonFlags
## SelectableFlags

| Name |
| ---- |
| None |
| DontClosePopups |
| SpanAllColumns |
| AllowDoubleClick |
| Disabled |
| AllowItemOverlap |
| NoHoldingActiveID |
| SelectOnNav |
| SelectOnClick |
| SelectOnRelease |
| SpanAvailWidth |
| DrawHoveredWhenHeld |
| SetNavIdOnHover |
| NoPadWithHalfSpacing |

## TableColumnFlags

| Name |
| ---- |
| None |
| Disabled |
| DefaultHide |
| DefaultSort |
| WidthStretch |
| WidthFixed |
| NoResize |
| NoReorder |
| NoHide |
| NoClip |
| NoSort |
| NoSortAscending |
| NoSortDescending |
| NoHeaderLabel |
| NoHeaderWidth |
| PreferSortAscending |
| PreferSortDescending |
| IndentEnable |
| IndentDisable |
| IsEnabled |
| IsVisible |
| IsSorted |
| IsHovered |

## TableFlags

| Name | Notes |
| ---- | ----- |
| None ||
| Resizable ||
| Reorderable ||
| Hideable ||
| Sortable | Not yet implemented |
| NoSavedSettings ||
| ContextMenuInBody ||
| RowBg ||
| BordersInnerH ||
| BordersOuterH ||
| BordersInnerV ||
| BordersOuterV ||
| BordersH ||
| BordersV ||
| BordersInner ||
| BordersOuter ||
| Borders ||
| NoBordersInBody ||
| NoBordersInBodyUntilResize ||
| SizingFixedFit ||
| SizingFixedSame ||
| SizingStretchProp ||
| SizingStretchSame ||
| NoHostExtendX ||
| NoHostExtendY ||
| NoKeepColumnsVisible ||
| PreciseWidths ||
| NoClip ||
| PadOuterX ||
| NoPadOuterX ||
| NoPadInnerX ||
| ScrollX ||
| ScrollY ||
| SortMulti | Not yet implemented |
| SortTristate | Not yet implemented |

## InputTextFlags

| Name | Notes |
| ---- | ----- |
| None ||
| CharsDecimal ||
| CharsHexadecimal ||
| CharsUppercase ||
| CharsNoBlank ||
| AutoSelectAll ||
| EnterReturnsTrue ||
| CallbackCompletion | Not yet implemented |
| CallbackHistory | Not yet implemented |
| CallbackAlways | Not yet implemented |
| CallbackCharFilter | Not yet implemented |
| AllowTabInput ||
| CtrlEnterForNewLine ||
| NoHorizontalScroll ||
| AlwaysOverwrite ||
| ReadOnly ||
| Password ||
| NoUndoRedo ||
| CharsScientific ||
| CallbackResize | Not yet implemented |
| CallbackEdit | Not yet implemented |

## ButtonFlags

| Name |
| ---- |
| None |
| MouseButtonLeft |
| MouseButtonRight |
| MouseButtonMiddle |
| PressedOnClick |
| PressedOnClickRelease |
| PressedOnClickReleaseAnywhere |
| PressedOnRelease |
| PressedOnDoubleClick |
| PressedOnDragDropHold |
| Repeat |
| FlattenChildren |
| AllowItemOverlap |
| DontClosePopups |
| AlignTextBaseLine |
| NoKeyModifiers |
| NoHoldingActiveId |
| NoNavFocus |
| NoHoveredOnFocus |
| PressedOnMask_ |
| PressedOnDefault_ |
2 changes: 1 addition & 1 deletion trview.app/Lua/Lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <trview.common/Strings.h>
#include "trview/trview.h"
#include <external/lua/src/lualib.h>
#include <trview.lua.imgui/trview.lua.imgui.h>
#include <trview.lua.imgui/inc/trview.lua.imgui.h>
#include "Route/Lua_Waypoint.h"

namespace trview
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions trview.lua.imgui/pch.h → trview.lua.imgui/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,11 @@
// add headers that you want to pre-compile here
#include "framework.h"

#include <external/lua/src/lua.h>
#include <external/lua/src/lauxlib.h>

#include <external/imgui/imgui.h>
#include <external/imgui/imgui_internal.h>
#include <external/imgui/misc/cpp/imgui_stdlib.h>

#endif //PCH_H
133 changes: 133 additions & 0 deletions trview.lua.imgui/src/tables.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
#include "pch.h"
#include "tables.h"

namespace trview
{
namespace lua
{
double get_double(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
luaL_checktype(L, -1, LUA_TNUMBER);
double value = lua_tonumber(L, -1);
lua_pop(L, 1);
return value;
}

float get_float(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
luaL_checktype(L, -1, LUA_TNUMBER);
float value = lua_tonumber(L, -1);
lua_pop(L, 1);
return value;
}

int get_integer(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
luaL_checktype(L, -1, LUA_TNUMBER);
int value = lua_tointeger(L, -1);
lua_pop(L, 1);
return value;
}

std::optional<double> get_optional_double(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
return std::nullopt;
}
luaL_checktype(L, -1, LUA_TNUMBER);
double value = lua_tonumber(L, -1);
lua_pop(L, 1);
return value;
}

std::optional<float> get_optional_float(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
return std::nullopt;
}
luaL_checktype(L, -1, LUA_TNUMBER);
float value = lua_tonumber(L, -1);
lua_pop(L, 1);
return value;
}

std::optional<int> get_optional_integer(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
return std::nullopt;
}
luaL_checktype(L, -1, LUA_TNUMBER);
int value = lua_tointeger(L, -1);
lua_pop(L, 1);
return value;
}

std::optional<std::string> get_optional_string(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
if (lua_isnil(L, -1))
{
lua_pop(L, 1);
return std::nullopt;
}
luaL_checktype(L, -1, LUA_TSTRING);
std::string value = lua_tostring(L, -1);
lua_pop(L, 1);
return value;
}

std::string get_string(lua_State* L, int index, const std::string& name)
{
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, name.c_str());
luaL_checktype(L, -1, LUA_TSTRING);
std::string value = lua_tostring(L, -1);
lua_pop(L, 1);
return value;
}

void set_enum(lua_State* L, const std::string& name, int index, const std::vector<EnumValue>& values)
{
if (index < 0)
{
index += lua_gettop(L) + 1;
}

lua_newtable(L);
for (const auto& v : values)
{
set_integer(L, -1, v.name.c_str(), v.value);
}
lua_setfield(L, index, name.c_str());
}

void set_integer(lua_State* L, int index, const std::string& name, int value)
{
if (index < 0)
{
index += lua_gettop(L) + 1;
}
lua_pushinteger(L, value);
lua_setfield(L, index, name.c_str());
}
}
}
31 changes: 31 additions & 0 deletions trview.lua.imgui/src/tables.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <optional>
#include <string>
#include <vector>

struct lua_State;

namespace trview
{
namespace lua
{
double get_double(lua_State* L, int index, const std::string& name);
float get_float(lua_State* L, int index, const std::string& name);
int get_integer(lua_State* L, int index, const std::string& name);
std::optional<double> get_optional_double(lua_State* L, int index, const std::string& name);
std::optional<float> get_optional_float(lua_State* L, int index, const std::string& name);
std::optional<int> get_optional_integer(lua_State* L, int index, const std::string& name);
std::optional<std::string> get_optional_string(lua_State* L, int index, const std::string& name);
std::string get_string(lua_State* L, int index, const std::string& name);

struct EnumValue
{
std::string name;
int value;
};

void set_enum(lua_State* L, const std::string& name, int index, const std::vector<EnumValue>& values);
void set_integer(lua_State* L, int index, const std::string& name, int value);
}
}
Loading

0 comments on commit e44b785

Please sign in to comment.