YSTest: ReaderSetting.cpp 源文件

YSTest PreAlpha_b800_20170810 The YSLib Test Project

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

- 文件列表
- 文件成员

ReaderSetting.cpp
浏览该文件的文档.
1 /*
2 © 2012-2013, 2015-2016 FrankHB.
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 return YSLib::AsNode(name, to_string(value.GetR()) + ' '
84 + to_string(value.GetG()) + ' ' + to_string(value.GetB()));
85 }
86
88 Font
89 FetchFontSetting(const ValueNode::Container& con, const string& family,
90 const string& size)
91 {
92 if(const auto p = FetchDefaultFontCache().GetFontFamilyPtr(
93 FetchSetting<string>(con, family)))
94 return Font(*p, FetchSetting<int>(con, size));
95 return {};
96 }
97
98 } // unnamed namespace;
99
100 ReaderSetting::ReaderSetting()
101 : UpColor(240, 216, 192), DownColor(192, 216, 240), FontColor(),
102 Font(FetchDefaultTypeface().GetFontFamily(), 14), SmoothScroll(true),
103 ScrollDuration(1000), SmoothScrollDuration(80)
104 {}
105 ReaderSetting::ReaderSetting(const ValueNode::Container& con)
106 : UpColor(FetchSetting<Color>(con, "UpColor")), DownColor(
107 FetchSetting<Color>(con, "DownColor")), FontColor(FetchSetting<Color>(con,
108 "FontColor")), Font(FetchFontSetting(con, "FontFamily", "FontSize")),
109 SmoothScroll(FetchSetting<int>(con, "SmoothScroll") != 0),
110 ScrollDuration(FetchSetting<int>(con, "ScrollDuration")),
111 SmoothScrollDuration(FetchSetting<int>(con, "SmoothScrollDuration"))
112 {}
113
114 ReaderSetting::operator ValueNode::Container() const
115 {
116 return ValueNode::Container{ColorToNode("UpColor", UpColor),
117 ColorToNode("DownColor", DownColor),
118 ColorToNode("FontColor", FontColor),
119 AsNode("FontFamily", Font.GetFontFamily().GetFamilyName()),
120 StringifyToNode("FontSize", Font.GetSize()),
121 StringifyToNode("SmoothScroll", int(SmoothScroll)),
122 StringifyToNode("ScrollDuration", ScrollDuration.count()),
123 StringifyToNode("SmoothScrollDuration", SmoothScrollDuration.count())};
124 }
125
126 } // namespace YReader;
127
YSLib::Drawing::Font
字体：字模，包含字型、样式和大小。
Definition: Font.h:583
ystdex::to_string
std::string to_string(const std::thread::id &)
转换 thread::id 为字符串表示。
YSLib::FetchDefaultFontCache
YF_API Drawing::FontCache & FetchDefaultFontCache()
取默认字体缓存。 字体缓存初始化失败。
Definition: Initialization.cpp:530
YSLib::Drawing::r
The boundary is out of the const Graphics const Rect const Point const Point Color c const Rect const Rect & r
Definition: YDraw.h:174
Text
NPL::c_str
YF_API const char src c_str())) YF_API void InstallDirectory(const string &
安装目录：复制目录树。
Definition: Dependency.h:131
Color
Pixel Color
Definition: CharRenderer.cpp:58
YSLib::AccessNode
const ValueNode & AccessNode(const ValueNode &node, const _tKey &name)
Definition: ValueNode.h:866
YSLib::ValueNode::Container
ystdex::mapped_set< ValueNode, ystdex::less<>> Container
Definition: ValueNode.h:62
YSLib::AsNode
typename _tParams ValueNode AsNode(_tString &&str, _tParams &&...args)
Definition: ValueNode.h:1065
YSLib::Drawing::FetchDefaultTypeface
YF_API const Typeface & FetchDefaultTypeface()
取默认字型引用。
Definition: Font.cpp:381
YSLib::Drawing::g
The boundary is out of the const Graphics & g
Definition: YDraw.h:161
CHRLib::s
_tDst const _tSrc * s
Definition: CharacterProcessing.h:140
platform::Color
颜色。
Definition: Video.h:416
YSLib::StringifyToNode
ValueNode StringifyToNode(_tString &&str, _tParams &&...args)
取指定名称和转换为字符串的值类型节点。
Definition: ValueNode.h:1089
ReaderSetting.h
阅读器设置。
YReader
Definition: About.h:33
ystdex::mapped_set
允许指定不同于值类型的键的集合。
Definition: set.hpp:160
YSLib::UI::name
yconstfn const string & name
Definition: Loader.h:130
YReader::ReaderSetting::ReaderSetting
ReaderSetting()
Definition: ReaderSetting.cpp:100
ystdex::size
auto size(const _tRange &c) -> decltype(c.size())
类容器访问。
Definition: range.hpp:233
YSLib::IO::int
YF_API int
Definition: File.h:51
CHRLib::p
const auto p(d)