Skip to content

Commit

Permalink
2.0 Test 2 (1.3.0.173)
Browse files Browse the repository at this point in the history
Изменения:
— Реализованы списки персонажей (.tgc)
  • Loading branch information
AIexandrKotov committed Jun 19, 2021
1 parent f540bf5 commit 8563b70
Show file tree
Hide file tree
Showing 9 changed files with 539 additions and 24 deletions.
91 changes: 91 additions & 0 deletions Tropegen/CharacterFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tropegenbase.Characters;

namespace Tropegen
{
public class CharacterFile
{
public bool Canceled { get; set; }
public bool Edited { get; set; }
public string Path { get; set; }
public string Name { get; set; }
private List<Character> characterlist;
public List<Character> CharacterList
{
get
{
Edited = true;
return characterlist;
}
}

internal CharacterFile(string filename)
{
Path = filename;
Name = System.IO.Path.GetFileNameWithoutExtension(filename);
}

public CharacterFile(string filename, Character[] characters) : this(filename)
{
characterlist = characters.ToList();
}

public void Rename(string newname)
{
if (File.Exists(Path))
{
Load();
File.Delete(Path);
}
Path = Extended.GetPath() + newname + ".tgc";
Name = newname;
Save();
}

public void Remove()
{
if (File.Exists(Path)) File.Delete(Path);
}

public static CharacterFile New(string name)
{
var cf = new CharacterFile(Extended.GetPath() + name + ".tgc");
cf.characterlist = new List<Character>();
return cf;
}

public void Save()
{
Character.Save(Path, CharacterList);
}

public void Load()
{
Edited = false;
try
{
characterlist = Character.Load(Path).ToList();
}
catch
{
Canceled = true;
return;
}
}

public static List<CharacterFile> CharacterFiles()
{
var filenames = Directory.GetFiles(Extended.GetPath(), "*.tgc");
var ret = new List<CharacterFile>();
for (var i = 0; i < filenames.Length; i++) ret.Add(new CharacterFile(filenames[i]));
ret.ForEach(x => x.Load());
ret.RemoveAll(x => x.Canceled);
return ret;
}
}
}
139 changes: 139 additions & 0 deletions Tropegen/CharacterLists.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

107 changes: 107 additions & 0 deletions Tropegen/CharacterLists.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Tropegenbase.Data;

namespace Tropegen
{
public partial class CharacterLists : Form
{
public Extended Reference;
public CharacterLists(Extended extended)
{
InitializeComponent();
Reference = extended;
ListOfList.Items.AddRange(Reference.SavedFiles.Select(x => x.Name).ToArray());
ListOfList.Text = Reference.CurrentFile;
NewTextBox.Text = ListOfList.Text;
NewButton.Enabled = false;
RenameButton.Enabled = false;
RemoveButton.Enabled = ListOfList.Items.Count > 1;
Text = Lang.Get("CharacterLists");
DescriptionTextBox.Text = Lang.Get("CLDesc");
RemoveButton.Text = Lang.Get("Remove");
RenameButton.Text = Lang.Get("Rename");
NewButton.Text = Lang.Get("New");
}

private void NewTextBox_TextChanged(object sender, EventArgs e)
{
if (ListOfList.Items.Contains(NewTextBox.Text))
{
NewButton.Enabled = false;
RenameButton.Enabled = false;
}
else
{
NewButton.Enabled = true;
RenameButton.Enabled = true;
}
}

private void NewButton_Click(object sender, EventArgs e)
{
Reference.SavedFiles.Add(CharacterFile.New(NewTextBox.Text));
ListOfList.Text = Reference.SavedFiles[Reference.SavedFiles.Count - 1].Name;
Reference.SavedFiles.Find(x => Reference.CurrentFile == x.Name).Save();
var ind = Reference.SavedFiles.FindIndex(x => x.Name == ListOfList.Text);
Reference.CurrentSavedCharacters = Reference.SavedFiles[ind].CharacterList;
Reference.CurrentFile = Reference.SavedFiles[ind].Name;
UpdateListOfList();
if (ListOfList.Items.Count > 1) RemoveButton.Enabled = true;
ListOfList.Text = NewTextBox.Text;
NewButton.Enabled = false;
}

private bool indelete;

private void RemoveButton_Click(object sender, EventArgs e)
{
var ind = Reference.SavedFiles.FindIndex(x => x.Name == ListOfList.Text);
var name = Reference.SavedFiles[ind].Name;
indelete = true;
if (name == ListOfList.Text) ListOfList.Text = (string)ListOfList.Items[ListOfList.Items.Count - 1];
Reference.SavedFiles[ind].Remove();
Reference.SavedFiles.RemoveAt(ind);
UpdateListOfList();
if (ListOfList.Items.Count > 1) RemoveButton.Enabled = true;
else RemoveButton.Enabled = false;
ListOfList.SelectedIndex = ind > 0 ? ind - 1 : 0;
}

void UpdateListOfList()
{
ListOfList.Items.Clear();
ListOfList.Items.AddRange(Reference.SavedFiles.Select(x => x.Name).ToArray());
}

private void ListOfList_SelectedIndexChanged(object sender, EventArgs e)
{
if (!indelete)
Reference.SavedFiles.Find(x => Reference.CurrentFile == x.Name).Save();
else indelete = false;
var ind = Reference.SavedFiles.FindIndex(x => x.Name == ListOfList.Text);
Reference.CurrentSavedCharacters = Reference.SavedFiles[ind].CharacterList;
Reference.CurrentFile = Reference.SavedFiles[ind].Name;
NewTextBox.Text = Reference.CurrentFile;
}

private void RenameButton_Click(object sender, EventArgs e)
{
var ind = Reference.SavedFiles.FindIndex(x => x.Name == ListOfList.Text);
ListOfList.Items.Remove(ListOfList.Text);
Reference.SavedFiles[ind].Rename(NewTextBox.Text);
Reference.CurrentFile = NewTextBox.Text;
UpdateListOfList();
ListOfList.Text = NewTextBox.Text;
RenameButton.Enabled = false;
}
}
}
Loading

0 comments on commit 8563b70

Please sign in to comment.