From d4b9be001c916c88f59f8ff6320bdb4e88bfb187 Mon Sep 17 00:00:00 2001 From: Semphriss <66701383+Semphriss@users.noreply.github.com> Date: Thu, 11 Nov 2021 17:16:49 -0500 Subject: [PATCH] Editor saves configuration to file (w/o lighting) (#1871) Co-authored-by: Semphris See #1871 for details as to why the lighting is not saved. --- src/editor/overlay_widget.cpp | 44 +++++++++++-------------- src/editor/overlay_widget.hpp | 9 +---- src/object/background.cpp | 3 +- src/object/gradient.cpp | 3 +- src/supertux/command_line_arguments.cpp | 1 - src/supertux/gameconfig.cpp | 38 +++++++++++++++++++-- src/supertux/gameconfig.hpp | 7 ++++ src/supertux/menu/editor_menu.cpp | 14 ++++---- 8 files changed, 73 insertions(+), 46 deletions(-) diff --git a/src/editor/overlay_widget.cpp b/src/editor/overlay_widget.cpp index b83c9c27008..e9b79f9e77c 100644 --- a/src/editor/overlay_widget.cpp +++ b/src/editor/overlay_widget.cpp @@ -51,14 +51,8 @@ namespace { } // namespace -bool EditorOverlayWidget::render_background = true; -bool EditorOverlayWidget::render_grid = true; -bool EditorOverlayWidget::snap_to_grid = true; -bool EditorOverlayWidget::autotile_mode = false; -bool EditorOverlayWidget::autotile_help = true; bool EditorOverlayWidget::action_pressed = false; bool EditorOverlayWidget::alt_pressed = false; -int EditorOverlayWidget::selected_snap_grid_size = 3; EditorOverlayWidget::EditorOverlayWidget(Editor& editor) : m_editor(editor), @@ -257,7 +251,7 @@ EditorOverlayWidget::put_tile() uint32_t tile = tiles->pos(static_cast(add_tile.x), static_cast(add_tile.y)); auto tilemap = m_editor.get_selected_tilemap(); - if (autotile_mode && ((tilemap && tilemap->get_autotileset(tile)) || tile == 0)) { + if (g_config->editor_autotile_mode && ((tilemap && tilemap->get_autotileset(tile)) || tile == 0)) { if (tile == 0) { tilemap->autotile_erase(m_hovered_tile + add_tile, m_hovered_corner + add_tile); } else if (tilemap->get_autotileset(tile)->is_corner()) { @@ -310,7 +304,7 @@ EditorOverlayWidget::draw_rectangle() for (int x = static_cast(dr.get_left()); x <= static_cast(dr.get_right()); x++, x_++) { int y_ = sgn_y ? 0 : static_cast(-dr.get_height()); for (int y = static_cast(dr.get_top()); y <= static_cast(dr.get_bottom()); y++, y_++) { - if (autotile_mode) { + if (g_config->editor_autotile_mode) { input_autotile( Vector(static_cast(x), static_cast(y)), m_editor.get_tiles()->pos(x_, y_) ); } else { input_tile( Vector(static_cast(x), static_cast(y)), m_editor.get_tiles()->pos(x_, y_) ); @@ -324,7 +318,7 @@ EditorOverlayWidget::check_tiles_for_fill(uint32_t replace_tile, uint32_t target_tile, uint32_t third_tile) const { - if (autotile_mode) { + if (g_config->editor_autotile_mode) { return m_editor.get_tileset()->get_autotileset_from_tile(replace_tile) == m_editor.get_tileset()->get_autotileset_from_tile(target_tile) && m_editor.get_tileset()->get_autotileset_from_tile(replace_tile) @@ -426,7 +420,7 @@ EditorOverlayWidget::fill() } // Autotile happens after directional detection (because of borders; see snow tileset) - if (autotile_mode) { + if (g_config->editor_autotile_mode) { input_autotile(pos, tiles->pos(static_cast(tpos.x), static_cast(tpos.y))); } @@ -604,8 +598,8 @@ EditorOverlayWidget::move_object() return; } Vector new_pos = m_sector_pos - m_obj_mouse_desync; - if (snap_to_grid) { - auto& snap_grid_size = snap_grid_sizes[selected_snap_grid_size]; + if (g_config->editor_snap_to_grid) { + auto& snap_grid_size = snap_grid_sizes[g_config->editor_selected_snap_grid_size]; new_pos = glm::floor(new_pos / static_cast(snap_grid_size)) * static_cast(snap_grid_size); auto pm = dynamic_cast(m_dragged_object); @@ -713,9 +707,9 @@ EditorOverlayWidget::put_object() else { auto target_pos = m_sector_pos; - if (snap_to_grid) + if (g_config->editor_snap_to_grid) { - auto& snap_grid_size = snap_grid_sizes[selected_snap_grid_size]; + auto& snap_grid_size = snap_grid_sizes[g_config->editor_selected_snap_grid_size]; target_pos = glm::floor(m_sector_pos / static_cast(snap_grid_size)) * static_cast(snap_grid_size); } @@ -727,7 +721,7 @@ EditorOverlayWidget::put_object() if (!dynamic_cast(object.get())) { m_editor.add_layer(object.get()); } - } else if (!snap_to_grid) { + } else if (!g_config->editor_snap_to_grid) { auto bbox = mo->get_bbox(); mo->move_to(mo->get_pos() - Vector(bbox.get_width() / 2, bbox.get_height() / 2)); } @@ -977,10 +971,10 @@ EditorOverlayWidget::on_key_up(const SDL_KeyboardEvent& key) auto sym = key.keysym.sym; if (sym == SDLK_LSHIFT || sym == SDLK_RSHIFT) { - snap_to_grid = !snap_to_grid; + g_config->editor_snap_to_grid = !g_config->editor_snap_to_grid; } if (sym == SDLK_LCTRL || sym == SDLK_RCTRL) { - autotile_mode = !autotile_mode; + g_config->editor_autotile_mode = !g_config->editor_autotile_mode; action_pressed = false; // Hovered objects depend on which keys are pressed hover_object(); @@ -996,13 +990,13 @@ EditorOverlayWidget::on_key_down(const SDL_KeyboardEvent& key) { auto sym = key.keysym.sym; if (sym == SDLK_F8) { - render_grid = !render_grid; + g_config->editor_render_grid = !g_config->editor_render_grid; } if (sym == SDLK_F7 || sym == SDLK_LSHIFT || sym == SDLK_RSHIFT) { - snap_to_grid = !snap_to_grid; + g_config->editor_snap_to_grid = !g_config->editor_snap_to_grid; } if (sym == SDLK_F5 || ((sym == SDLK_LCTRL || sym == SDLK_RCTRL) && !action_pressed)) { - autotile_mode = !autotile_mode; + g_config->editor_autotile_mode = !g_config->editor_autotile_mode; action_pressed = true; // Hovered objects depend on which keys are pressed hover_object(); @@ -1249,10 +1243,10 @@ EditorOverlayWidget::draw(DrawingContext& context) draw_rectangle_preview(context); draw_path(context); - if (render_grid) { + if (g_config->editor_render_grid) { draw_tile_grid(context, 32, true); draw_tilemap_border(context); - auto snap_grid_size = snap_grid_sizes[selected_snap_grid_size]; + auto snap_grid_size = snap_grid_sizes[g_config->editor_selected_snap_grid_size]; if (snap_grid_size != 32) { draw_tile_grid(context, snap_grid_size, false); } @@ -1296,15 +1290,15 @@ EditorOverlayWidget::draw(DrawingContext& context) } - if (autotile_help) { + if (g_config->editor_autotile_help) { if (m_editor.get_tileset()->get_autotileset_from_tile(m_editor.get_tiles()->pos(0, 0)) != nullptr) { - if (autotile_mode) { + if (g_config->editor_autotile_mode) { context.color().draw_text(Resources::normal_font, _("Autotile mode is on"), Vector(144, 16), ALIGN_LEFT, LAYER_OBJECTS+1, EditorOverlayWidget::text_autotile_active_color); } else { context.color().draw_text(Resources::normal_font, _("Hold Ctrl to enable autotile"), Vector(144, 16), ALIGN_LEFT, LAYER_OBJECTS+1, EditorOverlayWidget::text_autotile_available_color); } - } else if (autotile_mode) { + } else if (g_config->editor_autotile_mode) { if (m_editor.get_tiles()->pos(0, 0) == 0) { context.color().draw_text(Resources::normal_font, _("Autotile erasing mode is on"), Vector(144, 16), ALIGN_LEFT, LAYER_OBJECTS+1, EditorOverlayWidget::text_autotile_active_color); } else { diff --git a/src/editor/overlay_widget.hpp b/src/editor/overlay_widget.hpp index 7f8e5e64028..5ea6378d1c6 100644 --- a/src/editor/overlay_widget.hpp +++ b/src/editor/overlay_widget.hpp @@ -40,14 +40,6 @@ class Tip; class EditorOverlayWidget final : public Widget { public: - static bool render_background; - static bool render_grid; - static bool snap_to_grid; - static bool autotile_mode; - static bool autotile_help; - static bool alt_pressed; - static int selected_snap_grid_size; - static Color text_autotile_available_color; static Color text_autotile_active_color; static Color text_autotile_error_color; @@ -74,6 +66,7 @@ class EditorOverlayWidget final : public Widget private: static bool action_pressed; + static bool alt_pressed; private: void input_tile(const Vector& pos, uint32_t tile); diff --git a/src/object/background.cpp b/src/object/background.cpp index d256e0c55ea..816b6985b06 100644 --- a/src/object/background.cpp +++ b/src/object/background.cpp @@ -20,6 +20,7 @@ #include "editor/editor.hpp" #include "supertux/d_scope.hpp" +#include "supertux/gameconfig.hpp" #include "supertux/globals.hpp" #include "util/reader.hpp" #include "util/reader_mapping.hpp" @@ -372,7 +373,7 @@ Background::draw_image(DrawingContext& context, const Vector& pos_) void Background::draw(DrawingContext& context) { - if (Editor::is_active() && !EditorOverlayWidget::render_background) + if (Editor::is_active() && !g_config->editor_render_background) return; if (m_image.get() == nullptr) diff --git a/src/object/gradient.cpp b/src/object/gradient.cpp index 8b3b81d0c83..9c812d54e6f 100644 --- a/src/object/gradient.cpp +++ b/src/object/gradient.cpp @@ -18,6 +18,7 @@ #include "editor/editor.hpp" #include "object/camera.hpp" +#include "supertux/gameconfig.hpp" #include "supertux/level.hpp" #include "supertux/sector.hpp" #include "util/reader.hpp" @@ -236,7 +237,7 @@ Gradient::set_direction(const GradientDirection& direction) void Gradient::draw(DrawingContext& context) { - if (Editor::is_active() && !EditorOverlayWidget::render_background) + if (Editor::is_active() && !g_config->editor_render_background) return; Rectf gradient_region; diff --git a/src/supertux/command_line_arguments.cpp b/src/supertux/command_line_arguments.cpp index 9a217e5d3a6..c4989711510 100644 --- a/src/supertux/command_line_arguments.cpp +++ b/src/supertux/command_line_arguments.cpp @@ -307,7 +307,6 @@ CommandLineArguments::parse_args(int argc, char** argv) else if (arg == "--developer") { developer_mode = true; - EditorOverlayWidget::autotile_help = !developer_mode; } else if (arg == "--christmas") { diff --git a/src/supertux/gameconfig.cpp b/src/supertux/gameconfig.cpp index 3496b56ca75..4dfbf239e26 100644 --- a/src/supertux/gameconfig.cpp +++ b/src/supertux/gameconfig.cpp @@ -73,6 +73,13 @@ Config::Config() : enable_discord(false), #endif hide_editor_levelnames(false), + editor_selected_snap_grid_size(3), + editor_render_grid(true), + editor_snap_to_grid(true), + editor_render_background(true), + editor_render_lighting(false), + editor_autotile_mode(false), + editor_autotile_help(true), editor_autosave_frequency(5), repository_url() { @@ -113,9 +120,23 @@ Config::load() #endif } + // Compatibility; will be overwritten by the "editor" category config_mapping.get("editor_autosave_frequency", editor_autosave_frequency); - EditorOverlayWidget::autotile_help = !developer_mode; + editor_autotile_help = !developer_mode; + + boost::optional editor_mapping; + if (config_mapping.get("editor", editor_mapping)) + { + editor_mapping->get("autosave_frequency", editor_autosave_frequency); + editor_mapping->get("autotile_help", editor_autotile_help); + editor_mapping->get("autotile_mode", editor_autotile_mode); + editor_mapping->get("render_background", editor_render_background); + editor_mapping->get("render_grid", editor_render_grid); + editor_mapping->get("render_lighting", editor_render_lighting); + editor_mapping->get("selected_snap_grid_size", editor_selected_snap_grid_size); + editor_mapping->get("snap_to_grid", editor_snap_to_grid); + } else { log_warning << "!!!!" << std::endl; } if (is_christmas()) { if (!config_mapping.get("christmas", christmas_mode)) @@ -245,8 +266,6 @@ Config::save() } writer.end_list("integrations"); - writer.write("editor_autosave_frequency", editor_autosave_frequency); - if (is_christmas()) { writer.write("christmas", christmas_mode); } @@ -319,6 +338,19 @@ Config::save() } writer.end_list("addons"); + writer.start_list("editor"); + { + writer.write("autosave_frequency", editor_autosave_frequency); + writer.write("autotile_help", editor_autotile_help); + writer.write("autotile_mode", editor_autotile_mode); + writer.write("render_background", editor_render_background); + writer.write("render_grid", editor_render_grid); + writer.write("render_lighting", editor_render_lighting); + writer.write("selected_snap_grid_size", editor_selected_snap_grid_size); + writer.write("snap_to_grid", editor_snap_to_grid); + } + writer.end_list("editor"); + writer.end_list("supertux-config"); } diff --git a/src/supertux/gameconfig.hpp b/src/supertux/gameconfig.hpp index db7e210e0d1..8f3e48f4705 100644 --- a/src/supertux/gameconfig.hpp +++ b/src/supertux/gameconfig.hpp @@ -113,6 +113,13 @@ class Config final #endif bool hide_editor_levelnames; + int editor_selected_snap_grid_size; + bool editor_render_grid; + bool editor_snap_to_grid; + bool editor_render_background; + bool editor_render_lighting; + bool editor_autotile_mode; + bool editor_autotile_help; int editor_autosave_frequency; std::string repository_url; diff --git a/src/supertux/menu/editor_menu.cpp b/src/supertux/menu/editor_menu.cpp index c18e3d0f06c..af2f938fc0a 100644 --- a/src/supertux/menu/editor_menu.cpp +++ b/src/supertux/menu/editor_menu.cpp @@ -62,13 +62,13 @@ EditorMenu::EditorMenu() add_hl(); - add_string_select(-1, _("Grid Size"), &EditorOverlayWidget::selected_snap_grid_size, snap_grid_sizes); - add_toggle(-1, _("Show Grid"), &EditorOverlayWidget::render_grid); - add_toggle(-1, _("Grid Snapping"), &EditorOverlayWidget::snap_to_grid); - add_toggle(-1, _("Render Background"), &EditorOverlayWidget::render_background); - add_toggle(-1, _("Render Light"), &Compositor::s_render_lighting); - add_toggle(-1, _("Autotile Mode"), &EditorOverlayWidget::autotile_mode); - add_toggle(-1, _("Enable Autotile Help"), &EditorOverlayWidget::autotile_help); + add_string_select(-1, _("Grid Size"), &(g_config->editor_selected_snap_grid_size), snap_grid_sizes); + add_toggle(-1, _("Show Grid"), &(g_config->editor_render_grid)); + add_toggle(-1, _("Grid Snapping"), &(g_config->editor_snap_to_grid)); + add_toggle(-1, _("Render Background"), &(g_config->editor_render_background)); + add_toggle(-1, _("Render Light"), &(Compositor::s_render_lighting)); + add_toggle(-1, _("Autotile Mode"), &(g_config->editor_autotile_mode)); + add_toggle(-1, _("Enable Autotile Help"), &(g_config->editor_autotile_help)); add_intfield(_("Autosave Frequency"), &(g_config->editor_autosave_frequency)); add_submenu(worldmap ? _("Worldmap Settings") : _("Level Settings"),