From 5f452d388240187b485f9f1acb286ffa27adcf01 Mon Sep 17 00:00:00 2001 From: KheirFerrum <102964889+KheirFerrum@users.noreply.github.com> Date: Sat, 21 Sep 2024 12:43:42 +0100 Subject: [PATCH] Stop preview from modifying character directly Init now takes avatar as a const value so that the preview only creates a preview rather than modifies the existing character. --- src/character_preview.cpp | 44 +++++++++++++-------------------------- src/character_preview.h | 8 +++---- src/newcharacter.cpp | 9 +------- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/src/character_preview.cpp b/src/character_preview.cpp index 34adb0c4dda9..e76d12706044 100644 --- a/src/character_preview.cpp +++ b/src/character_preview.cpp @@ -1,10 +1,11 @@ #if defined(TILES) +#include "avatar.h" +#include "character.h" #include "character_preview.h" #include "bionics.h" #include "magic.h" #include "messages.h" #include "type_id.h" -#include "character.h" #include "profession.h" #include "sdltiles.h" #include "output.h" @@ -22,25 +23,18 @@ auto termy_to_pixel_value() -> int return projected_window_height() / TERMY; } -void character_preview_window::init( Character *character ) +void character_preview_window::init( const avatar &u ) { - this->character = character; + preview = std::make_unique( u ); // Setting bionics - for( const bionic_id &bio : character->prof->CBMs() ) { - character->add_bionic( bio ); - // Saving possible spells to cancell them later - for( const std::pair &spell_pair : bio->learned_spells ) { - const spell_id learned_spell = spell_pair.first; - if( learned_spell->spell_class != trait_id( "NONE" ) ) { - spells.push_back( learned_spell->spell_class ); - } - } + for( const bionic_id &bio : preview->prof->CBMs() ) { + preview->add_bionic( bio ); } // Collecting profession clothes - std::vector> prof_items = character->prof->items( character->male, - character->get_mutations() ); + std::vector> prof_items = preview->prof->items( preview->male, + preview->get_mutations() ); for( detached_ptr &it : prof_items ) { if( it->is_armor() ) { clothes.push_back( std::move( it ) ); @@ -130,10 +124,10 @@ void character_preview_window::zoom_out() void character_preview_window::toggle_clothes() { if( !show_clothes ) { - character->worn.clear(); + preview->worn.clear(); } else { for( detached_ptr &it : clothes ) { - character->wear_item( item::spawn( *std::move( it ) ), false ); + preview->wear_item( item::spawn( *std::move( it ) ), false ); } } show_clothes = !show_clothes; @@ -153,23 +147,13 @@ void character_preview_window::display() const // Drawing character itself const point pos = calc_character_pos(); - tilecontext->display_character( *character, pos ); + tilecontext->display_character( *preview, pos ); } -void character_preview_window::clear() const +void character_preview_window::clear() { - character->worn.clear(); - character->clear_bionics(); - character->set_max_power_level( 0_kJ ); - character->set_power_level( character->get_max_power_level() ); - character->magic = pimpl(); - for( const trait_id &spell : spells ) { - if( character->has_trait( spell ) ) { - character->remove_mutation( spell ); - } - } - character->clear_morale(); - Messages::clear_messages(); + preview.reset( nullptr ); + clothes.clear(); tilecontext->set_draw_scale( DEFAULT_TILESET_ZOOM ); } diff --git a/src/character_preview.h b/src/character_preview.h index 7ef348d01adb..7a2ecdaa0163 100644 --- a/src/character_preview.h +++ b/src/character_preview.h @@ -7,7 +7,7 @@ #include "type_id.h" class item; -class Character; +class avatar; /** Gets size of single width terminal unit size value in pixels **/ auto termx_to_pixel_value() -> int; @@ -34,7 +34,7 @@ struct character_preview_window { catacurses::window w_preview; - void init( Character *character ); + void init( const avatar &u ); /** Window preparations before displaying. Sets desirable position. Could also be usefull for ui-rescale **/ void prepare( int nlines, int ncols, const Orientation *orientation, int hide_below_ncols ); void zoom_in(); @@ -42,7 +42,7 @@ struct character_preview_window { void toggle_clothes(); void display() const; /** Use it as you done with preview **/ - void clear() const; + void clear(); auto clothes_showing() const -> bool; private: @@ -56,7 +56,7 @@ struct character_preview_window { int hide_below_ncols = 0; int ncols_width = 0; int nlines_width = 0; - Character *character = nullptr; + std::unique_ptr preview; std::vector> clothes; std::vector spells; bool show_clothes = true; diff --git a/src/newcharacter.cpp b/src/newcharacter.cpp index 404599efde33..561f107f1f88 100644 --- a/src/newcharacter.cpp +++ b/src/newcharacter.cpp @@ -1108,7 +1108,7 @@ tab_direction set_traits( avatar &u, points_left &points ) #if defined(TILES) character_preview_window character_preview; - character_preview.init( &u ); + character_preview.init( u ); const bool use_character_preview = get_option( "USE_CHARACTER_PREVIEW" ) && get_option( "USE_TILES" ); #endif @@ -1369,13 +1369,6 @@ tab_direction set_traits( avatar &u, points_left &points ) //inc_type is either -1 or 1, so we can just multiply by it to invert if( inc_type != 0 ) { u.toggle_trait( cur_trait ); -#if defined(TILES) - // If character had trait - it's now removed. Trait could blocked some clothes, need to retoggle - if( has_trait && character_preview.clothes_showing() ) { - character_preview.toggle_clothes(); - character_preview.toggle_clothes(); - } -#endif points.trait_points -= mdata.points * inc_type; if( iCurWorkingPage == 0 ) { num_good += mdata.points * inc_type;