Skip to content

Commit

Permalink
Break some loops, more ImGui binding
Browse files Browse the repository at this point in the history
  • Loading branch information
chreden committed Mar 10, 2023
1 parent 81d6793 commit 39d626a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion trview.app/Elements/ILevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace trview
virtual trlevel::LevelVersion version() const = 0;
Event<std::weak_ptr<IItem>> on_item_selected;
// Event raised when the level needs to change the selected room.
Event<uint16_t> on_room_selected;
Event<uint32_t> on_room_selected;
// Event raised when the level needs to change the alternate mode.
Event<bool> on_alternate_mode_selected;
/// Event raised when the level needs to change the alternate group mode.
Expand Down
45 changes: 26 additions & 19 deletions trview.app/Elements/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,33 +188,40 @@ namespace trview

void Level::set_selected_room(uint16_t index)
{
_selected_room = index;
regenerate_neighbours();

// If the user has selected a room that is or has an alternate mode, raise the event that the
// alternate mode needs to change so that the correct rooms can be rendered.
const auto& room = *_rooms[index];
if (is_alternate_mismatch(room))
if (_selected_room != index)
{
if (version() >= trlevel::LevelVersion::Tomb4)
{
on_alternate_group_selected(room.alternate_group(), !is_alternate_group_set(room.alternate_group()));
}
else
_selected_room = index;
regenerate_neighbours();

// If the user has selected a room that is or has an alternate mode, raise the event that the
// alternate mode needs to change so that the correct rooms can be rendered.
const auto& room = *_rooms[index];
if (is_alternate_mismatch(room))
{
on_alternate_mode_selected(!_alternate_mode);
if (version() >= trlevel::LevelVersion::Tomb4)
{
on_alternate_group_selected(room.alternate_group(), !is_alternate_group_set(room.alternate_group()));
}
else
{
on_alternate_mode_selected(!_alternate_mode);
}
}
}

on_level_changed();
on_room_selected(index);
on_level_changed();
on_room_selected(index);
}
}

void Level::set_selected_item(uint32_t index)
{
_selected_item = _entities[index];
on_level_changed();
on_item_selected(_selected_item);
const auto selected_item = _entities[index];
if (_selected_item.lock() != selected_item)
{
_selected_item = selected_item;
on_level_changed();
on_item_selected(_selected_item);
}
}

void Level::set_neighbour_depth(uint32_t depth)
Expand Down
6 changes: 3 additions & 3 deletions trview.app/Windows/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,15 +583,15 @@ namespace trview
_token_store += _level->on_alternate_mode_selected += [&](bool enabled) { set_alternate_mode(enabled); };
_token_store += _level->on_alternate_group_selected += [&](uint16_t group, bool enabled) { set_alternate_group(group, enabled); };
_token_store += _level->on_level_changed += [&]() { _scene_changed = true; };
_token_store += _level->on_room_selected += [&](uint16_t room) { select_room(room); };
_token_store += _level->on_item_selected += [&](auto&& item) { select_item(item); };
_level->on_room_selected += on_room_selected;
_level->on_item_selected += on_item_selected;

_level->set_show_triggers(_ui->toggle(Options::triggers));
_level->set_show_geometry(_ui->toggle(Options::geometry));
_level->set_show_water(_ui->toggle(Options::water));
_level->set_show_wireframe(_ui->toggle(Options::wireframe));
_level->set_show_bounding_boxes(_ui->toggle(Options::show_bounding_boxes));
_level->set_show_lights(_ui->toggle(Options::lights));
_level->set_show_lights(_ui->toggle( Options::lights));
_level->set_show_items(_ui->toggle(Options::items));
_level->set_show_rooms(_ui->toggle(Options::rooms));
_level->set_show_camera_sinks(_ui->toggle(Options::camera_sinks));
Expand Down
36 changes: 33 additions & 3 deletions trview.lua.imgui/trview.lua.imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,26 @@ namespace trview
return 2;
}

int finish(lua_State* L)
int begin_child(lua_State* L)
{
const auto name = get_string(L, 1, "name");
bool result = ImGui::BeginChild(name.c_str());
lua_pushboolean(L, result);
return 1;
}

int end(lua_State* L)
{
ImGui::End();
return 0;
}

int end_child(lua_State* L)
{
ImGui::EndChild();
return 0;
}

int button(lua_State* L)
{
const auto label = get_string(L, 1, "label");
Expand Down Expand Up @@ -117,7 +131,7 @@ namespace trview
{
auto id = get_string(L, 1, "id");
auto column = get_integer(L, 1, "column");
bool result = ImGui::BeginTable(id.c_str(), column);
bool result = ImGui::BeginTable(id.c_str(), column, ImGuiTableFlags_ScrollY);
lua_pushboolean(L, result);
return 1;
}
Expand Down Expand Up @@ -154,6 +168,15 @@ namespace trview
ImGui::TableSetupScrollFreeze(cols, rows);
return 0;
}

int selectable(lua_State* L)
{
const auto label = get_string(L, 1, "label");
auto selected = get_bool(L, 1, "selected");
bool result = ImGui::Selectable(label.c_str(), &selected, ImGuiSelectableFlags_SpanAllColumns);
lua_pushboolean(L, result);
return 1;
}
}

void imgui_register(lua_State* L)
Expand All @@ -162,8 +185,12 @@ namespace trview
// Windows
lua_pushcfunction(L, begin);
lua_setfield(L, -2, "Begin");
lua_pushcfunction(L, finish);
lua_pushcfunction(L, begin_child);
lua_setfield(L, -2, "BeginChild");
lua_pushcfunction(L, end);
lua_setfield(L, -2, "End");
lua_pushcfunction(L, end_child);
lua_setfield(L, -2, "EndChild");
// Buttons
lua_pushcfunction(L, button);
lua_setfield(L, -2, "Button");
Expand All @@ -186,6 +213,9 @@ namespace trview
lua_setfield(L, -2, "TableHeadersRow");
lua_pushcfunction(L, table_setup_scroll_freeze);
lua_setfield(L, -2, "TableSetupScrollFreeze");
// Selectable
lua_pushcfunction(L, selectable);
lua_setfield(L, -2, "Selectable");

lua_setglobal(L, "ImGui");
}
Expand Down

0 comments on commit 39d626a

Please sign in to comment.