Skip to content

Commit

Permalink
Stop preview from modifying character directly
Browse files Browse the repository at this point in the history
Init now takes avatar as a const value so that the preview only creates a preview rather than modifies the existing character.
  • Loading branch information
KheirFerrum committed Sep 21, 2024
1 parent 6653fcc commit 5f452d3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 42 deletions.
44 changes: 14 additions & 30 deletions src/character_preview.cpp
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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<avatar>( 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<const spell_id, int> &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<detached_ptr<item>> prof_items = character->prof->items( character->male,
character->get_mutations() );
std::vector<detached_ptr<item>> prof_items = preview->prof->items( preview->male,
preview->get_mutations() );
for( detached_ptr<item> &it : prof_items ) {
if( it->is_armor() ) {
clothes.push_back( std::move( it ) );
Expand Down Expand Up @@ -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<item> &it : clothes ) {
character->wear_item( item::spawn( *std::move( it ) ), false );
preview->wear_item( item::spawn( *std::move( it ) ), false );
}
}
show_clothes = !show_clothes;
Expand All @@ -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<known_magic>();
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 );
}

Expand Down
8 changes: 4 additions & 4 deletions src/character_preview.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,15 +34,15 @@ 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();
void zoom_out();
void toggle_clothes();
void display() const;
/** Use it as you done with preview **/
void clear() const;
void clear();
auto clothes_showing() const -> bool;

private:
Expand All @@ -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<avatar> preview;
std::vector<detached_ptr<item>> clothes;
std::vector<trait_id> spells;
bool show_clothes = true;
Expand Down
9 changes: 1 addition & 8 deletions src/newcharacter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>( "USE_CHARACTER_PREVIEW" ) &&
get_option<bool>( "USE_TILES" );
#endif
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5f452d3

Please sign in to comment.