Skip to content

Commit

Permalink
Editor saves configuration to file (w/o lighting) (#1871)
Browse files Browse the repository at this point in the history
Co-authored-by: Semphris <semphris@protonmail.com>
See #1871 for details as to why the lighting is not saved.
  • Loading branch information
Semphriss authored Nov 11, 2021
1 parent 566b4cb commit d4b9be0
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 46 deletions.
44 changes: 19 additions & 25 deletions src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -257,7 +251,7 @@ EditorOverlayWidget::put_tile()
uint32_t tile = tiles->pos(static_cast<int>(add_tile.x), static_cast<int>(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()) {
Expand Down Expand Up @@ -310,7 +304,7 @@ EditorOverlayWidget::draw_rectangle()
for (int x = static_cast<int>(dr.get_left()); x <= static_cast<int>(dr.get_right()); x++, x_++) {
int y_ = sgn_y ? 0 : static_cast<int>(-dr.get_height());
for (int y = static_cast<int>(dr.get_top()); y <= static_cast<int>(dr.get_bottom()); y++, y_++) {
if (autotile_mode) {
if (g_config->editor_autotile_mode) {
input_autotile( Vector(static_cast<float>(x), static_cast<float>(y)), m_editor.get_tiles()->pos(x_, y_) );
} else {
input_tile( Vector(static_cast<float>(x), static_cast<float>(y)), m_editor.get_tiles()->pos(x_, y_) );
Expand All @@ -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)
Expand Down Expand Up @@ -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<int>(tpos.x), static_cast<int>(tpos.y)));
}

Expand Down Expand Up @@ -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<float>(snap_grid_size)) * static_cast<float>(snap_grid_size);

auto pm = dynamic_cast<MarkerObject*>(m_dragged_object);
Expand Down Expand Up @@ -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<float>(snap_grid_size)) * static_cast<float>(snap_grid_size);
}

Expand All @@ -727,7 +721,7 @@ EditorOverlayWidget::put_object()
if (!dynamic_cast<PathGameObject*>(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));
}
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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 {
Expand Down
9 changes: 1 addition & 8 deletions src/editor/overlay_widget.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/object/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/object/gradient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/supertux/command_line_arguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down
38 changes: 35 additions & 3 deletions src/supertux/gameconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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<ReaderMapping> 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))
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
}

Expand Down
7 changes: 7 additions & 0 deletions src/supertux/gameconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions src/supertux/menu/editor_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down

0 comments on commit d4b9be0

Please sign in to comment.