<commit_before>{ This file is a part of Map editor for VCMI project

  Copyright (C) 2015-2017 <NAME> <EMAIL>

  This source 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 2 of the License, or (at your option) any later
  version.

  This code 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.

  A copy of the GNU General Public License is available on the World Wide Web at
  <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit hero_skills_frame;

{$I compilersetup.inc}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, ActnList, Buttons, base_options_frame, gui_helpers, Map, editor_classes,
  base_info, object_options, editor_types, lists_manager;

type

  { THeroSkillsFrame }

  THeroSkillsFrame = class(TBaseOptionsFrame)
    act: TActionList;
    actAdd: TAction;
    actDelete: TAction;
    ButtonAdd: TSpeedButton;
    ButtonRemove: TSpeedButton;
    cbCustomise: TCheckBox;
    iml: TImageList;
    MasterySelector: TComboBox;
    SkillsEdit: TListBox;
    SkillSelector: TComboBox;
    procedure actAddExecute(Sender: TObject);
    procedure actAddUpdate(Sender: TObject);
    procedure actDeleteExecute(Sender: TObject);
    procedure actDeleteUpdate(Sender: TObject);
    procedure cbCustomiseChange(Sender: TObject);
    procedure SkillsEditSelectionChange(Sender: TObject; User: boolean);
  private
    FObject: THeroSecondarySkills;
    FCustomSkills, FDefaultSkills: THeroSecondarySkills;

    procedure SaveSkills;
    procedure LoadSkills(ASrc:THeroSecondarySkills);

    procedure LoadDefaultSkills(AHeroId: AnsiString);

    function FormatInfo(AInfo: THeroSecondarySkill): TLocalizedString;
  protected
    procedure Load; override;
    procedure UpdateControls(); override;
    procedure ApplyDefaults; override;
    procedure ReloadDefaults; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure Commit; override;

    procedure VisitNormalHero(AOptions: TNormalHeroOptions);override;
    procedure VisitRandomHero(AOptions: TRandomHeroOptions);override;
    procedure VisitPrison(AOptions: TPrisonOptions);override;

    procedure VisitHeroDefinition(AOptions: THeroDefinition); override;
  end;

implementation

{$R *.lfm}

{ THeroSkillsFrame }

procedure THeroSkillsFrame.cbCustomiseChange(Sender: TObject);
begin
  UpdateControls();

  if cbCustomise.Checked then
  begin
    LoadSkills(FCustomSkills);
  end
  else
  begin
    SkillSelector.ItemIndex:=-1;
    MasterySelector.ItemIndex:=-1;

    SaveSkills;
    LoadSkills(FDefaultSkills);
  end;
end;

procedure THeroSkillsFrame.SkillsEditSelectionChange(Sender: TObject; User: boolean);
var
  idx: Integer;
  o: THeroSecondarySkill;
begin
  if not cbCustomise.Checked then
    exit;

  idx := SkillsEdit.ItemIndex;

  if idx < 0 then
  begin
    SkillSelector.ItemIndex:=-1;
    MasterySelector.ItemIndex:=-1;

    exit;
  end;

  o := SkillsEdit.Items.Objects[idx] as THeroSecondarySkill;

  SkillSelector.SetValue(ListsManager.SkillInfos, o.Identifier);
  MasterySelector.ItemIndex := Integer(o.Level) - 1;
end;

procedure THeroSkillsFrame.actAddUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=cbCustomise.Checked and (SkillSelector.ItemIndex>=0) and (MasterySelector.ItemIndex>=0);
end;

procedure THeroSkillsFrame.actAddExecute(Sender: TObject);
var
  i, matched: Integer;

  skill_info: TBaseInfo;
  mastery: TSkillLevel;

  o: THeroSecondarySkill;
begin
  matched := -1;

  skill_info := SkillSelector.SelectedInfo;

  if not Assigned(skill_info) then
    exit;

  if MasterySelector.ItemIndex < 0 then
    Exit;
  mastery:=TSkillLevel(MasterySelector.ItemIndex+1);

  for i := 0 to SkillsEdit.Items.Count - 1 do
  begin
    o := SkillsEdit.Items.Objects[i] as THeroSecondarySkill;

    if o.Identifier = skill_info.Identifier then
    begin
      matched:=i;
      break;
    end;
  end;

  if matched < 0 then
  begin
    o := THeroSecondarySkill.Create(nil);
    o.Identifier:=skill_info.Identifier;
    o.Level:=mastery;
    SkillsEdit.AddItem(FormatInfo(o), o);
    FFreeList.Add(o);
  end
  else
  begin
    //update
    o := SkillsEdit.Items.Objects[matched] as THeroSecondarySkill;
    o.Level:=mastery;
    SkillsEdit.Items[matched] := FormatInfo(o);
    SkillsEdit.Items.Objects[matched] := o;
  end;
end;

procedure THeroSkillsFrame.actDeleteExecute(Sender: TObject);
var
  idx: Integer;
begin
  idx := SkillsEdit.ItemIndex;

  if idx >= 0 then
    SkillsEdit.Items.Delete(idx);
end;

procedure THeroSkillsFrame.actDeleteUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=cbCustomise.Checked and (SkillsEdit.ItemIndex>=0);
end;

procedure THeroSkillsFrame.SaveSkills;
var
  i: Integer;
  src, dst: THeroSecondarySkill;
begin
  FCustomSkills.Clear;
<commit_msg>Fixed updating hero secondary skill<commit_after>{ This file is a part of Map editor for VCMI project

  Copyright (C) 2015-2017 <NAME> <EMAIL>

  This source 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 2 of the License, or (at your option) any later
  version.

  This code 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.

  A copy of the GNU General Public License is available on the World Wide Web at
  <http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
  Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit hero_skills_frame;

{$I compilersetup.inc}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, ActnList, Buttons, base_options_frame, gui_helpers, Map, editor_classes,
  base_info, object_options, editor_types, lists_manager;

type

  { THeroSkillsFrame }

  THeroSkillsFrame = class(TBaseOptionsFrame)
    act: TActionList;
    actAdd: TAction;
    actDelete: TAction;
    ButtonAdd: TSpeedButton;
    ButtonRemove: TSpeedButton;
    cbCustomise: TCheckBox;
    iml: TImageList;
    MasterySelector: TComboBox;
    SkillsEdit: TListBox;
    SkillSelector: TComboBox;
    procedure actAddExecute(Sender: TObject);
    procedure actAddUpdate(Sender: TObject);
    procedure actDeleteExecute(Sender: TObject);
    procedure actDeleteUpdate(Sender: TObject);
    procedure cbCustomiseChange(Sender: TObject);
    procedure SkillsEditSelectionChange(Sender: TObject; User: boolean);
  private
    FObject: THeroSecondarySkills;
    FCustomSkills, FDefaultSkills: THeroSecondarySkills;

    procedure SaveSkills;
    procedure LoadSkills(ASrc:THeroSecondarySkills);

    procedure LoadDefaultSkills(AHeroId: AnsiString);

    function FormatInfo(AInfo: THeroSecondarySkill): TLocalizedString;
  protected
    procedure Load; override;
    procedure UpdateControls(); override;
    procedure ApplyDefaults; override;
    procedure ReloadDefaults; override;
  public
    constructor Create(TheOwner: TComponent); override;
    destructor Destroy; override;
    procedure Commit; override;

    procedure VisitNormalHero(AOptions: TNormalHeroOptions);override;
    procedure VisitRandomHero(AOptions: TRandomHeroOptions);override;
    procedure VisitPrison(AOptions: TPrisonOptions);override;

    procedure VisitHeroDefinition(AOptions: THeroDefinition); override;
  end;

implementation

{$R *.lfm}

{ THeroSkillsFrame }

procedure THeroSkillsFrame.cbCustomiseChange(Sender: TObject);
begin
  UpdateControls();

  if cbCustomise.Checked then
  begin
    LoadSkills(FCustomSkills);
  end
  else
  begin
    SkillSelector.ItemIndex:=-1;
    MasterySelector.ItemIndex:=-1;

    SaveSkills;
    LoadSkills(FDefaultSkills);
  end;
end;

procedure THeroSkillsFrame.SkillsEditSelectionChange(Sender: TObject; User: boolean);
var
  idx: Integer;
  o: THeroSecondarySkill;
begin
  if not cbCustomise.Checked then
    exit;

  idx := SkillsEdit.ItemIndex;

  if idx < 0 then
  begin
    SkillSelector.ItemIndex:=-1;
    MasterySelector.ItemIndex:=-1;

    exit;
  end;

  o := SkillsEdit.Items.Objects[idx] as THeroSecondarySkill;

  SkillSelector.SetValue(ListsManager.SkillInfos, o.Identifier);
  MasterySelector.ItemIndex := Integer(o.Level) - 1;
end;

procedure THeroSkillsFrame.actAddUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=cbCustomise.Checked and (SkillSelector.ItemIndex>=0) and (MasterySelector.ItemIndex>=0);
end;

procedure THeroSkillsFrame.actAddExecute(Sender: TObject);
var
  i, matched: Integer;

  skill_info: TBaseInfo;
  mastery: TSkillLevel;

  o: THeroSecondarySkill;
begin
  matched := -1;

  skill_info := SkillSelector.SelectedInfo;

  if not Assigned(skill_info) then
    exit;

  if MasterySelector.ItemIndex < 0 then
    Exit;
  mastery:=TSkillLevel(MasterySelector.ItemIndex+1);

  for i := 0 to SkillsEdit.Items.Count - 1 do
  begin
    o := SkillsEdit.Items.Objects[i] as THeroSecondarySkill;

    if o.Identifier = skill_info.Identifier then
    begin
      matched:=i;
      break;
    end;
  end;

  if matched < 0 then
  begin
    o := THeroSecondarySkill.Create(nil);
    o.Identifier:=skill_info.Identifier;
    o.Level:=mastery;
    SkillsEdit.AddItem(FormatInfo(o), o);
    FFreeList.Add(o);
  end
  else
  begin
    //update
    o := SkillsEdit.Items.Objects[matched] as THeroSecondarySkill;
    o.Level:=mastery;
    SkillsEdit.Items.Delete(matched);
    SkillsEdit.AddItem(FormatInfo(o), o);
  end;
end;

procedure THeroSkillsFrame.actDeleteExecute(Sender: TObject);
var
  idx: Integer;
begin
  idx := SkillsEdit.ItemIndex;

  if idx >= 0 then
    SkillsEdit.Items.Delete(idx);
end;

procedure THeroSkillsFrame.actDeleteUpdate(Sender: TObject);
begin
  (Sender as TAction).Enabled:=cbCustomise.Checked and (SkillsEdit.ItemIndex>=0);
end;

procedure THeroSkillsFrame.SaveSkills;
var
  i: Integer;
  src, dst: THeroSecondarySkill;
begin
  FCustomSkills.Clear;

{'fraction_non_alphanumeric': 0.08497043677283998, 'fraction_numerical': 0.007438489414457372, 'mean_word_length': 4.4336787564766835, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '6583446', 'n_tokens_mistral': 3420, 'n_tokens_neox': 3237, 'n_words': 889}