YSTest: ReaderSetting.cpp 源文件

YSTest PreAlpha_b500_20140530 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

全部 类 命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义 组 页
ReaderSetting.cpp
浏览该文件的文档.
1 /*
2 Copyright by FrankHB 2012 - 2013.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #include "ReaderSetting.h"
29
30 namespace YReader
31 {
32
33 using namespace Text;
34 using std::chrono::milliseconds;
35
37 namespace
38 {
39
41
42 template<typename _type>
43 _type
44 FetchSetting(const ValueNode::Container&, const string&);
45
46 template<>
47 inline string
48 FetchSetting<string>(const ValueNode::Container& con, const string& name)
49 {
50 return Access<string>(AccessNode(con, name));
51 }
52
53 template<>
54 int
55 FetchSetting<int>(const ValueNode::Container& con, const string& name)
56 {
57 return std::stoi(FetchSetting<string>(con, name));
58 }
59
60 template<>
61 Color
62 FetchSetting<Color>(const ValueNode::Container& con, const string& name)
63 {
64 const auto s(FetchSetting<string>(con, name).c_str());
65 unsigned r, g, b;
66
67 if(std::sscanf(s, "%u%u%u", &r, &g, &b) != 3)
68 throw std::invalid_argument("Color components are not enough.");
69 #if 0
70 if(r < 0x100 && g < 0x100 && b < 0x100)
71 return Color(r, g, b);
72 throw std::invalid_argument("Invalid color components found.");
73 #endif
74 return Color(min<MonoType>(r, 0xFF), min<MonoType>(g, 0xFF),
75 min<MonoType>(b, 0xFF));
76 }
78
80 ValueNode
81 ColorToNode(const string& name, const Color& value)
82 {
83 using ystdex::to_string;
84
85 return YSLib::MakeNode(name, to_string(value.GetR()) + ' '
86 + to_string(value.GetG()) + ' ' + to_string(value.GetB()));
87 }
88
90 Font
91 FetchFontSetting(const ValueNode::Container& con, const string& family,
92 const string& size)
93 {
94 if(const auto p = FetchDefaultFontCache().GetFontFamilyPtr(
95 FetchSetting<string>(con, family)))
96 return Font(*p, FetchSetting<int>(con, size));
97 return Font();
98 }
99
100 } // unnamed namespace;
101
102 ReaderSetting::ReaderSetting()
103 : UpColor(240, 216, 192), DownColor(192, 216, 240), FontColor(),
104 Font(FetchDefaultTypeface().GetFontFamily(), 14), SmoothScroll(true),
105 ScrollDuration(1000), SmoothScrollDuration(80)
106 {}
107 ReaderSetting::ReaderSetting(const ValueNode::Container& con)
108 : UpColor(FetchSetting<Color>(con, "UpColor")), DownColor(
109 FetchSetting<Color>(con, "DownColor")), FontColor(FetchSetting<Color>(con,
110 "FontColor")), Font(FetchFontSetting(con, "FontFamily", "FontSize")),
111 SmoothScroll(FetchSetting<int>(con, "SmoothScroll") != 0),
112 ScrollDuration(FetchSetting<int>(con, "ScrollDuration")),
113 SmoothScrollDuration(FetchSetting<int>(con, "SmoothScrollDuration"))
114 {}
115
116 ReaderSetting::operator ValueNode::Container() const
117 {
118 return ValueNode::Container{ColorToNode("UpColor", UpColor),
119 ColorToNode("DownColor", DownColor),
120 ColorToNode("FontColor", FontColor),
121 MakeNode("FontFamily", Font.GetFontFamily().GetFamilyName()),
122 StringifyToNode("FontSize", Font.GetSize()),
123 StringifyToNode("SmoothScroll", int(SmoothScroll)),
124 StringifyToNode("ScrollDuration", ScrollDuration.count()),
125 StringifyToNode("SmoothScrollDuration", SmoothScrollDuration.count())};
126 }
127
128 } // namespace YReader;
129
YSLib::ValueNode::Container
set< ValueNode > Container
Definition: ValueNode.h:48
YSLib::true
bool return true
Definition: DSMain.cpp:177
ReaderSetting.h
阅读器设置。
YReader::ReaderSetting::ReaderSetting
ReaderSetting()
Definition: ReaderSetting.cpp:102
YSLib::AccessNode
YF_API const ValueNode & AccessNode(const ValueNode::Container *, const string &)
访问容器中的节点。
Definition: ValueNode.cpp:90
YSLib::StringifyToNode
ValueNode StringifyToNode(_tString &&name, _tParams &&...args)
取指定名称和转换为字符串的值类型节点。
Definition: ValueNode.h:337
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:110
YSLib::FetchDefaultFontCache
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。
Definition: Initialization.cpp:394
YSLib::Drawing::FetchDefaultTypeface
YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:367
ystdex::to_string
std::string to_string(unsigned char val)
转换为字符串。
Definition: string.hpp:353
platform_ex::Windows::s
return s
Definition: MinGW32.cpp:49
platform::Consoles::Color
Color
控制台颜色枚举。
Definition: Video.h:458
YSLib::Drawing::r
bounds & r
Definition: ydraw.h:220
YSLib::Drawing::g
c yconstfn g
Definition: ystyle.h:104
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:546
platform::Color
颜色。
Definition: Video.h:339
YSLib::ValueNode::MakeNode
ValueNode MakeNode(_tString &&name, _tParams &&...args)
取指定名称和退化参数的值类型节点。
Definition: ValueNode.h:325