-
Notifications
You must be signed in to change notification settings - Fork 37
/
Bookshelf.cs
98 lines (83 loc) · 2.48 KB
/
Bookshelf.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using System;
using System.Collections.Generic;
using System.Linq;
using Entoarox.CustomBooks.Menu;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace Entoarox.CustomBooks
{
internal class Bookshelf
{
/*********
** Accessors
*********/
public Dictionary<string, Book> Books = new Dictionary<string, Book>();
/*********
** Public methods
*********/
public string CreateBook()
{
string id = Guid.NewGuid().ToString();
this.Books.Add(id, new Book(id));
return id;
}
public class Book
{
/*********
** Fields
*********/
private readonly string Id;
/*********
** Accessors
*********/
public List<Page> Pages;
public string Name;
public Color Color;
public enum PageType
{
Title,
Text,
Image
}
/*********
** Public methods
*********/
public Book(string id)
{
this.Id = id;
this.Name = "(unnamed book)";
this.Pages = new List<Page>();
this.Color = new Color(139, 69, 19);
}
public Book()
{
}
public List<Menu.Page> GetPages()
{
return this.Pages.Select(a => a.Deserialize(this.Id)).ToList();
}
public void SetPages(List<Menu.Page> pages)
{
this.Pages = pages.Select(a => a.Serialize()).ToList();
}
public class Page
{
public PageType Type;
public string Content;
public Menu.Page Deserialize(string book)
{
switch (this.Type)
{
case PageType.Title:
return new TitlePage(JsonConvert.DeserializeObject<string[]>(this.Content));
case PageType.Text:
return new TextPage(this.Content);
case PageType.Image:
return new ImagePage(book, this.Content);
}
throw new InvalidOperationException("Unknown page type, cannot deserialize!");
}
}
}
}
}