YSTest  PreAlpha_b400_20130424
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 YSL_BEGIN_NAMESPACE(YReader)
31 
32 using namespace Text;
33 using std::chrono::milliseconds;
34 
36 namespace
37 {
38 
40 
41 template<typename _type>
42 _type
43 FetchSetting(const ValueNode::Container&, const string&);
44 
45 template<>
46 inline string
47 FetchSetting<string>(const ValueNode::Container& con, const string& name)
48 {
49  return Access<string>(AccessNode(con, name));
50 }
51 
52 template<>
53 int
54 FetchSetting<int>(const ValueNode::Container& con, const string& name)
55 {
56  return std::stoi(FetchSetting<string>(con, name));
57 }
58 
59 template<>
60 Color
61 FetchSetting<Color>(const ValueNode::Container& con, const string& name)
62 {
63  const auto s(FetchSetting<string>(con, name).c_str());
64  unsigned r, g, b;
65 
66  if(std::sscanf(s, "%u%u%u", &r, &g, &b) != 3)
67  throw std::invalid_argument("Color components are not enough.");
68 #if 0
69  if(r < 0x100 && g < 0x100 && b < 0x100)
70  return Color(r, g, b);
71  throw std::invalid_argument("Invalid color components found.");
72 #endif
73  return Color(min<Color::MonoType>(r, 0xFF), min<Color::MonoType>(g, 0xFF),
74  min<Color::MonoType>(b, 0xFF));
75 }
77 
80 ColorToNode(const string& name, const Color& value)
81 {
82  using ystdex::to_string;
83 
84  return YSLib::MakeNode(name, to_string(value.GetR()) + ' '
85  + to_string(value.GetG()) + ' ' + to_string(value.GetB()));
86 }
87 
89 Font
90 FetchFontSetting(const ValueNode::Container& con, const string& family,
91  const string& size)
92 {
93  if(const auto p = FetchDefaultFontCache().GetFontFamilyPtr(
94  FetchSetting<string>(con, family)))
95  return Font(*p, FetchSetting<int>(con, size));
96  return Font();
97 }
98 
99 } // unnamed namespace;
100 
102  : UpColor(240, 216, 192), DownColor(192, 216, 240), FontColor(),
103  Font(FetchDefaultTypeface().GetFontFamily(), 14), SmoothScroll(true),
104  ScrollDuration(1000), SmoothScrollDuration(80)
105 {}
107  : UpColor(FetchSetting<Color>(con, "UpColor")), DownColor(
108  FetchSetting<Color>(con, "DownColor")), FontColor(FetchSetting<Color>(con,
109  "FontColor")), Font(FetchFontSetting(con, "FontFamily", "FontSize")),
110  SmoothScroll(FetchSetting<int>(con, "SmoothScroll") != 0),
111  ScrollDuration(FetchSetting<int>(con, "ScrollDuration")),
112  SmoothScrollDuration(FetchSetting<int>(con, "SmoothScrollDuration"))
113 {}
114 
115 ReaderSetting::operator ValueNode::Container() const
116 {
117  return ValueNode::Container{ColorToNode("UpColor", UpColor),
118  ColorToNode("DownColor", DownColor),
119  ColorToNode("FontColor", FontColor),
120  MakeNode("FontFamily", Font.GetFontFamily().GetFamilyName()),
121  StringifyToNode("FontSize", Font.GetSize()),
122  StringifyToNode("SmoothScroll", int(SmoothScroll)),
123  StringifyToNode("ScrollDuration", ScrollDuration.count()),
124  StringifyToNode("SmoothScrollDuration", SmoothScrollDuration.count())};
125 }
126 
127 YSL_END_NAMESPACE(YReader)
128