<commit_before>/* OpenHoW
 * Copyright (C) 2017-2019 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../engine.h"
#include "../game/GameManager.h"

#include "map_config_editor.h"

MapConfigEditor::MapConfigEditor() {
  map_ = GameManager::GetInstance()->GetCurrentMap();
  if (map_ == nullptr) {
    throw std::runtime_error("Attempted to create config editor without a valid map loaded!\n");
  }

  manifest_ = map_->GetManifest();
  backup_ = *manifest_;

  strncpy(name_buffer, manifest_->name.c_str(), sizeof(name_buffer));
  strncpy(author_buffer, manifest_->author.c_str(), sizeof(author_buffer));
}

MapConfigEditor::~MapConfigEditor() {
  RestoreManifest();
}

void MapConfigEditor::DisplayTemperature() {
  enum {
    TEMP_NORMAL, TEMP_HOT, TEMP_COLD, MAX_TEMP
  };
  static const char *temperatures[MAX_TEMP] = {"Normal", "Hot", "Cold"};
  static int temperature_index = -1;
  if (temperature_index == -1) {
    if (pl_strcasecmp(manifest_->temperature.c_str(), "hot") == 0) {
      temperature_index = TEMP_HOT;
    } else if (pl_strcasecmp(manifest_->temperature.c_str(), "cold") == 0) {
      temperature_index = TEMP_COLD;
    } else {
      temperature_index = TEMP_NORMAL;
    }
  }

  if (ImGui::BeginCombo("Temperature", temperatures[temperature_index])) {
    for (int i = 0; i < MAX_TEMP; ++i) {
      if (ImGui::Selectable(temperatures[i], (temperature_index == i))) {
        ImGui::SetItemDefaultFocus();
        temperature_index = i;
        manifest_->temperature = u_stringtolower(temperatures[temperature_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::DisplayWeather() {
  enum {
    WEATHER_CLEAR, WEATHER_RAIN, WEATHER_SNOW, MAX_WEATHER
  };
  static const char *weather_labels[MAX_WEATHER] = {"Clear", "Rain", "Snow"};
  static int weather_index = -1;
  if (weather_index == -1) {
    if (pl_strcasecmp(manifest_->weather.c_str(), "rain") == 0) {
      weather_index = WEATHER_RAIN;
    } else if (pl_strcasecmp(manifest_->weather.c_str(), "snow") == 0) {
      weather_index = WEATHER_SNOW;
    } else {
      weather_index = WEATHER_CLEAR;
    }
  }

  if (ImGui::BeginCombo("Weather", weather_labels[weather_index])) {
    for (int i = 0; i < MAX_WEATHER; ++i) {
      if (ImGui::Selectable(weather_labels[i], (weather_index == i))) {
        ImGui::SetItemDefaultFocus();
        weather_index = i;
        manifest_->weather = u_stringtolower(weather_labels[weather_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::DisplayTime() {
  enum {
    TIME_DAY, TIME_NIGHT, MAX_TIME
  };
  static const char *times[MAX_TIME] = {"Day", "Night"};
  static int time_index = -1;
  if (time_index == -1) {
    if (pl_strcasecmp(manifest_->time.c_str(), "day") == 0) {
      time_index = TIME_DAY;
    } else {
      time_index = TIME_NIGHT;
    }
  }

  if (ImGui::BeginCombo("Time", times[time_index])) {
    for (int i = 0; i < MAX_TIME; ++i) {
      if (ImGui::Selectable(times[i], (time_index == i))) {
        ImGui::SetItemDefaultFocus();
        time_index = i;
        manifest_->time = u_stringtolower(times[time_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::Display() {
  ImGui::SetNextWindowSize(ImVec2(310, 512), ImGuiCond_Once);
  ImGui::Begin(dname("Map Config Editor"), &status_, ED_DEFAULT_WINDOW_FLAGS);
  ImGui::InputText("Name", name_buffer, sizeof(name_buffer));
  ImGui::InputText("Author", author_buffer, sizeof(author_buffer));

  // todo: mode selection - need to query wherever this ends up being implemented...

  ImGui::Separator();

  DisplayTemperature();
  DisplayWeather();
  DisplayTime();

  ImGui::Separator();

  ImGui::Text("Sky Settings");

  float rgb[3];
  rgb[0] = plByteToFloat(manifest_->sky_colour_top.r);
  rgb[1] = plByteToFloat(manifest_->sky_colour_top.g);
  rgb[2] = plByteToFloat(manifest_->sky_colour_top.b);
  if (ImGui::ColorEdit3("Top Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sky_colour_top.r = plFloatToByte(rgb[0]);
    manifest_->sky_colour_top.g = plFloatToByte(rgb[1]);
    manifest_->sky_colour_top.b = plFloatToByte(rgb[2]);

    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  rgb[0] = plByteToFloat(manifest_->sky_colour_bottom.r);
  rgb[1] = plByteToFloat(manifest_->sky_colour_bottom.g);
  rgb[2] = plByteToFloat(manifest_->sky_colour_bottom.b);
  if (ImGui::ColorEdit3("Bottom Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sky_colour_bottom.r = plFloatToByte(rgb[0]);
    manifest_->sky_colour_bottom.g = plFloatToByte(rgb[1]);
    manifest_->sky_colour_bottom.b = plFloatToByte(rgb[2]);

    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  ImGui::Separator();

  ImGui::Text("Lighting Settings");

  ImGui::SliderAngle("Sun Pitch", &manifest_->sun_pitch, 0, 90, nullptr);
  ImGui::SliderAngle("Sun Yaw", &manifest_->sun_yaw, 0, 360, nullptr);

  rgb[0] = plByteToFloat(manifest_->sun_colour.r);
  rgb[1] = plByteToFloat(manifest_->sun_colour.g);
  rgb[2] = plByteToFloat(manifest_->sun_colour.b);
  if (ImGui::ColorEdit3("Sun Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sun_colour.r = plFloatToByte(rgb[0]);
    manifest_->sun_colour.g = plFloatToByte(rgb[1]);
    manifest_->sun_colour.b = plFloatToByte(rgb[2]);
  }

  rgb[0] = plByteToFloat(manifest_->ambient_colour.r);
  rgb[1] = plByteToFloat(manifest_->ambient_colour.g);
  rgb[2] = plByteToFloat(manifest_->ambient_colour.b);
  if (ImGui::ColorEdit3("Ambient Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->ambient_colour.r = plFloatToByte(rgb[0]);
    manifest_->ambient_colour.g = plFloatToByte(rgb[1]);
    manifest_->ambient_colour.b = plFloatToByte(rgb[2]);

    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  ImGui::Separator();

  // Fog<commit_msg>reintroduced changes to map config editor<commit_after>/* OpenHoW
 * Copyright (C) 2017-2019 <NAME> <<EMAIL>>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "../engine.h"
#include "../game/GameManager.h"

#include "map_config_editor.h"

MapConfigEditor::MapConfigEditor() {
  map_ = GameManager::GetInstance()->GetCurrentMap();
  if (map_ == nullptr) {
    throw std::runtime_error("Attempted to create config editor without a valid map loaded!\n");
  }

  manifest_ = map_->GetManifest();
  backup_ = *manifest_;

  strncpy(name_buffer, manifest_->name.c_str(), sizeof(name_buffer));
  strncpy(author_buffer, manifest_->author.c_str(), sizeof(author_buffer));
}

MapConfigEditor::~MapConfigEditor() {
  RestoreManifest();
}

void MapConfigEditor::DisplayTemperature() {
  enum {
    TEMP_NORMAL, TEMP_HOT, TEMP_COLD, MAX_TEMP
  };
  static const char *temperatures[MAX_TEMP] = {"Normal", "Hot", "Cold"};
  static int temperature_index = -1;
  if (temperature_index == -1) {
    if (pl_strcasecmp(manifest_->temperature.c_str(), "hot") == 0) {
      temperature_index = TEMP_HOT;
    } else if (pl_strcasecmp(manifest_->temperature.c_str(), "cold") == 0) {
      temperature_index = TEMP_COLD;
    } else {
      temperature_index = TEMP_NORMAL;
    }
  }

  if (ImGui::BeginCombo("Temperature", temperatures[temperature_index])) {
    for (int i = 0; i < MAX_TEMP; ++i) {
      if (ImGui::Selectable(temperatures[i], (temperature_index == i))) {
        ImGui::SetItemDefaultFocus();
        temperature_index = i;
        manifest_->temperature = u_stringtolower(temperatures[temperature_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::DisplayWeather() {
  enum {
    WEATHER_CLEAR, WEATHER_RAIN, WEATHER_SNOW, MAX_WEATHER
  };
  static const char *weather_labels[MAX_WEATHER] = {"Clear", "Rain", "Snow"};
  static int weather_index = -1;
  if (weather_index == -1) {
    if (pl_strcasecmp(manifest_->weather.c_str(), "rain") == 0) {
      weather_index = WEATHER_RAIN;
    } else if (pl_strcasecmp(manifest_->weather.c_str(), "snow") == 0) {
      weather_index = WEATHER_SNOW;
    } else {
      weather_index = WEATHER_CLEAR;
    }
  }

  if (ImGui::BeginCombo("Weather", weather_labels[weather_index])) {
    for (int i = 0; i < MAX_WEATHER; ++i) {
      if (ImGui::Selectable(weather_labels[i], (weather_index == i))) {
        ImGui::SetItemDefaultFocus();
        weather_index = i;
        manifest_->weather = u_stringtolower(weather_labels[weather_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::DisplayTime() {
  enum {
    TIME_DAY, TIME_NIGHT, MAX_TIME
  };
  static const char *times[MAX_TIME] = {"Day", "Night"};
  static int time_index = -1;
  if (time_index == -1) {
    if (pl_strcasecmp(manifest_->time.c_str(), "day") == 0) {
      time_index = TIME_DAY;
    } else {
      time_index = TIME_NIGHT;
    }
  }

  if (ImGui::BeginCombo("Time", times[time_index])) {
    for (int i = 0; i < MAX_TIME; ++i) {
      if (ImGui::Selectable(times[i], (time_index == i))) {
        ImGui::SetItemDefaultFocus();
        time_index = i;
        manifest_->time = u_stringtolower(times[time_index]);
      }
    }

    ImGui::EndCombo();
  }
}

void MapConfigEditor::Display() {
  ImGui::SetNextWindowSize(ImVec2(310, 512), ImGuiCond_Once);
  ImGui::Begin(dname("Map Config Editor"), &status_, ED_DEFAULT_WINDOW_FLAGS);
  ImGui::InputText("Name", name_buffer, sizeof(name_buffer));
  ImGui::InputText("Author", author_buffer, sizeof(author_buffer));

  // todo: mode selection - need to query wherever this ends up being implemented...

  ImGui::Separator();

  DisplayTemperature();
  DisplayWeather();
  DisplayTime();

  ImGui::Separator();

  ImGui::Text("Sky Settings");

  float rgb[3];
  rgb[0] = plByteToFloat(manifest_->sky_colour_top.r);
  rgb[1] = plByteToFloat(manifest_->sky_colour_top.g);
  rgb[2] = plByteToFloat(manifest_->sky_colour_top.b);
  if (ImGui::ColorEdit3("Top Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sky_colour_top.r = plFloatToByte(rgb[0]);
    manifest_->sky_colour_top.g = plFloatToByte(rgb[1]);
    manifest_->sky_colour_top.b = plFloatToByte(rgb[2]);
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  rgb[0] = plByteToFloat(manifest_->sky_colour_bottom.r);
  rgb[1] = plByteToFloat(manifest_->sky_colour_bottom.g);
  rgb[2] = plByteToFloat(manifest_->sky_colour_bottom.b);
  if (ImGui::ColorEdit3("Bottom Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sky_colour_bottom.r = plFloatToByte(rgb[0]);
    manifest_->sky_colour_bottom.g = plFloatToByte(rgb[1]);
    manifest_->sky_colour_bottom.b = plFloatToByte(rgb[2]);
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  ImGui::Separator();

  ImGui::Text("Lighting Settings");

  if(ImGui::SliderAngle("Sun Pitch", &manifest_->sun_pitch, 0, 90, nullptr)) {
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }
  if(ImGui::SliderAngle("Sun Yaw", &manifest_->sun_yaw, 0, 360, nullptr)) {
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  rgb[0] = plByteToFloat(manifest_->sun_colour.r);
  rgb[1] = plByteToFloat(manifest_->sun_colour.g);
  rgb[2] = plByteToFloat(manifest_->sun_colour.b);
  if (ImGui::ColorEdit3("Sun Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->sun_colour.r = plFloatToByte(rgb[0]);
    manifest_->sun_colour.g = plFloatToByte(rgb[1]);
    manifest_->sun_colour.b = plFloatToByte(rgb[2]);
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  rgb[0] = plByteToFloat(manifest_->ambient_colour.r);
  rgb[1] = plByteToFloat(manifest_->ambient_colour.g);
  rgb[2] = plByteToFloat(manifest_->ambient_colour.b);
  if (ImGui::ColorEdit3("Ambient Colour", rgb, ImGuiColorEditFlags_InputRGB)) {
    manifest_->ambient_colour.r = plFloatToByte(rgb[0]);
    manifest_->ambient_colour.g = plFloatToByte(rgb[1]);
    manifest_->ambient_colour.b = plFloatToByte(rgb[2]);
    map_->ApplySkyColours(manifest_->sky_colour_bottom, manifest_->sky_colour_top);
  }

  ImGui::Separator();

  // Fog
{'fraction_non_alphanumeric': 0.14602268459400586, 'fraction_numerical': 0.00991512055885225, 'mean_word_length': 4.461033634126333, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 114, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7473265', 'n_tokens_mistral': 4739, 'n_tokens_neox': 4492, 'n_words': 872}