Skip to content

Instantly share code, notes, and snippets.

@eliasdaler
Created June 8, 2019 07:42
Show Gist options
  • Save eliasdaler/c7e56e1a85da931321b6bd4cac66d562 to your computer and use it in GitHub Desktop.
Save eliasdaler/c7e56e1a85da931321b6bd4cac66d562 to your computer and use it in GitHub Desktop.
void LevelEditor::update(math::Time dt)
{
camera.update(dt);
{
auto& rs = engine_system.getGame().getSystem<RenderingSystem>();
rs.updateView("levelEditor", camera.getView());
}
currentState->update(dt);
ImGui::Begin("TileMap Editor");
ImGui::Text("Level: %s", level.getName().c_str());
if (!levelNames.empty()) {
ImGui::SameLine();
if (ImGui::SmallButton("Change...")) {
ImGui::OpenPopup("Change level");
}
}
if (ImGui::Button("Create level...")) {
ImGui::OpenPopup("Create level");
}
if (ImGui::BeginPopupModal("Create level", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
bool levelNameError = false;
if (newLevelName.empty()) {
levelNameError = true;
}
if (levelNameError) {
ImGui::PushStyleColor(ImGuiCol_FrameBg, sf::Color(255, 0, 0, 138));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, sf::Color(255, 0, 0, 102));
}
ImGui::InputText("Name", &newLevelName);
if (levelNameError) {
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}
if (!levelNameError) {
if (ImGui::Button("OK")) {
createLevel(newLevelName);
newLevelName.clear();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
}
if (ImGui::Button("Cancel")) {
newLevelName.clear();
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if (ImGui::BeginPopupModal("Change level", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
// level combo box
if (ImGui::BeginCombo("Level", levelNames.at(changeLevelComboBoxIndex).c_str())) {
int i = 0;
for (const auto& levelName : levelNames) {
bool isSelected = (changeLevelComboBoxIndex == i);
if (ImGui::Selectable(levelName.c_str(), isSelected)) {
changeLevelComboBoxIndex = i;
changeLevelSceneIds = util::getSceneNames(levelName.c_str());
changeLevelSceneComboBoxIndex = 0;
}
if (isSelected) {
ImGui::SetItemDefaultFocus();
}
++i;
}
ImGui::EndCombo();
}
// scene names combo box
if (ImGui::BeginCombo("Scene",
changeLevelSceneIds.at(changeLevelSceneComboBoxIndex).c_str())) {
int i = 0;
for (const auto& sceneName : changeLevelSceneIds) {
bool isSelected = (changeLevelSceneComboBoxIndex == i);
if (ImGui::Selectable(sceneName.c_str(), isSelected)) {
changeLevelSceneComboBoxIndex = i;
}
if (isSelected) {
ImGui::SetItemDefaultFocus();
}
++i;
}
ImGui::EndCombo();
}
if (ImGui::Button("OK")) {
ImGui::CloseCurrentPopup();
auto& newLevelName = levelNames.at(changeLevelComboBoxIndex);
auto& newSceneName = changeLevelSceneIds.at(changeLevelSceneComboBoxIndex);
auto& scriptManager = engine_system.get<LuaScriptManager>();
auto& lua = scriptManager.getState();
if (!undoStack.empty()) {
saveCurrentLevel(); // TODO: make it possible not to do it?
undoStack.clear();
redoStack.clear();
}
sol::protected_function changeLevelFunc = lua["changeLevel"];
scriptManager.callFunc(changeLevelFunc, newLevelName, newSceneName);
}
ImGui::SameLine();
if (ImGui::Button("Cancel")) {
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
if (ImGui::Combo("##State names", &currentStateId, stateNames)) {
setCurrentState(stateTypes[currentStateId]);
}
if (ImGui::Combo("Scene", &currentSceneComboBoxIndex, sceneIds)) {
setCurrentScene(sceneIds[currentSceneComboBoxIndex]);
}
if (ImGui::Button("Create scene...")) {
ImGui::OpenPopup("Create scene");
}
if (ImGui::BeginPopupModal("Create scene", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
bool sceneNameError = false;
if (newSceneName.empty() || level.sceneExists(newSceneName)) {
sceneNameError = true;
}
if (sceneNameError) {
ImGui::PushStyleColor(ImGuiCol_FrameBg, sf::Color(255, 0, 0, 138));
ImGui::PushStyleColor(ImGuiCol_FrameBgHovered, sf::Color(255, 0, 0, 102));
}
ImGui::InputText("Name", &newSceneName);
if (sceneNameError) {
ImGui::PopStyleColor();
ImGui::PopStyleColor();
}
if (!sceneNameError) {
if (ImGui::Button("OK")) {
createScene(newSceneName);
setCurrentScene(newSceneName);
newSceneName.clear();
ImGui::CloseCurrentPopup();
}
ImGui::SameLine();
}
if (ImGui::Button("Cancel")) {
newSceneName.clear();
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
}
ImGui::Checkbox("Show grid", &showGrid);
if (ImGui::Button("Save")) {
saveCurrentLevel();
}
// RENDERING SETTINGS
if (ImGui::TreeNode("Rendering settings")) {
bool settingsChanged = false;
if (ImGui::Checkbox("Draw objects", &renderingSettings.drawObjects)) {
settingsChanged = true;
}
if (ImGui::Checkbox("Lighting", &renderingSettings.lightingEnabled)) {
settingsChanged = true;
}
if (settingsChanged) {
auto& rs = engine_system.getGame().getSystem<RenderingSystem>();
rs.setSettings(renderingSettings);
}
ImGui::TreePop();
}
ImGui::Separator();
currentState->updateGui();
ImGui::End();
// History window
ImGui::Begin("History");
for (const auto& command : undoStack) {
ImGui::PushID(command.get());
ImGui::Text("%s", command->getName());
ImGui::PopID();
}
if (!redoStack.empty()) {
ImGui::Separator();
}
for (const auto& command : redoStack) {
ImGui::PushID(command.get());
ImGui::Text("%s", command->getName());
ImGui::PopID();
}
if (scrollHistoryToBottom) {
ImGui::SetScrollHere(1.f);
scrollHistoryToBottom = false;
}
ImGui::End();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment