OSDN Git Service

fix build system ofmoflib1 and saisei1
[moflib/moflib.git] / saisei-1.0 / src / script / MyEnvironment.cpp
1 #include "script/MyEnvironment.hpp"
2 #include <boost/tuple/tuple.hpp>
3 #include <boost/format.hpp>
4 #include <boost/lexical_cast.hpp>
5 #include <boost/algorithm/string.hpp>
6 #include <mof/script/ObjectData.hpp>
7 #include <memory>
8 #include <mof/streams.hpp>
9 #include "widget/createMenuView.hpp"
10 #include "widget/createFrame.hpp"
11 #include <mof/Font.hpp>
12 #include <mof/widgets.hpp>
13 #include <mof/utilities.hpp>
14 #include <configure.hpp>
15 #include <mof/utilities.hpp>
16 #include <mof/widget/GridLayout.hpp>
17 #include "sqlite_wrapper.hpp"
18 #include "resource.hpp"
19
20
21 namespace script
22 {
23 //{{{ Impl
24         struct MyEnvironment::Impl
25         {
26                 ::sqlite_wrapper sqlite_;
27
28                 Impl()
29                 {
30
31                 }
32         };
33 //}}}
34 //{{{ constructor
35         MyEnvironment::MyEnvironment(std::shared_ptr<mof::InputReceiver> input)
36                 : mof::script::Environment(input), impl_(new Impl())
37         {
38         }
39 //}}}
40 //{{{ destructor
41         MyEnvironment::~MyEnvironment()
42         {
43         }
44 //}}}
45 //{{{ create_message_data
46         std::unique_ptr<mof::script::MessageData> MyEnvironment::create_message_data
47         (
48                 const mof::tstring& title, const mof::script::GameData::entry_t& style
49         )
50         {
51                 using namespace mof::widget;
52                 using namespace boost;
53
54                 auto compiler = std::make_shared<TextCompiler>
55                         (
56                                 mof::Font(FONT_NAME_UME, FONT_SIZE_STANDARD),
57                                 [] (const mof::tstring& path) { return ::getTextureResourceManager(::SYSTEM)->getResource(path);}
58                         );
59  
60                 {
61                         // set behavior for page updated
62                         mof::KeyFrameAnimation<mof::Color4f>::KeyFrame keyFrames[] =
63                         {
64                                 mof::makeKeyFrame(0,  mof::Color4f(0, 1, 1, 1)),
65                                 mof::makeKeyFrame(15, mof::Color4f(0, 1, 1, 1)),
66                                 mof::makeKeyFrame(45, mof::Color4f(1, 1, 1, 1)),
67                         };
68                         compiler->setBehaviorOnColor
69                         (
70                                 TextCompiler::PAGE_OPEN,
71                                 mof::makeKeyFrameAnimationHandler(keyFrames[0], mof::lastOf(keyFrames)),
72                                 45
73                         );
74                 }
75
76                 {
77                         // set behavior for page updated
78                         compiler->setBehaviorOnColor
79                         (
80                                 TextCompiler::PAGE_CLOSE,
81                                 mof::makeKeyFrameAnimationHandler(0, mof::Color4f(1, 1, 1, 1), 15, mof::Color4f(0, 1, 1, 1)),
82                                 15
83                         );
84                 }
85
86                 std::unique_ptr<mof::script::MessageData> result(new mof::script::MessageData);
87
88                 {
89             result->message_ = std::make_shared<Message>(mof::Vector2D(550, 125), compiler);
90
91             result->frame_ = 
92                                 std::shared_ptr<Frame>
93                 (
94                                         createFrame
95                         (
96                                                 title,
97                         _T("image/frame0.png"),
98                         _T("image/frame3.png"),
99                                                 result->message_->getView()
100                         ).release()
101                                 );
102         }
103                 
104         
105                 return result;
106         
107         }
108 //}}}
109 //{{{ create_menu_data
110         std::unique_ptr<mof::script::MenuData> MyEnvironment::create_menu_data
111         (
112                 const mof::tstring& title,
113                 const std::vector<mof::tstring>& items,
114                 const mof::script::GameData::entry_t& style
115         )
116         {
117                 using namespace mof::widget;
118                 using namespace mof::script;
119                 using namespace boost;
120
121                 GridLayout::Direction direction = GridLayout::VERTICAL;
122                 std::vector<int> disable_items;
123                 
124                 // \83X\83^\83C\83\8b\82Ì\89ð\90Í
125                 foreach (const auto& p, style) {
126                         if (p.first == "direction") {
127                                 if (p.second == "vertical") direction = GridLayout::VERTICAL;
128                                 else if (p.second == "horizontal") direction = GridLayout::HORIZONTAL;
129                                 else throw std::invalid_argument("invalid direction value:" + p.second);
130                         }
131                         else if (p.first == "disable") {
132                                 // \96³\8cø\89»\82·\82é\8d\80\96Ú\82Ì\89ð\90Í
133                                 std::vector<mof::tstring> splited_list;
134                                 split(splited_list, p.second, is_any_of(","));
135                                 foreach (auto& item, splited_list) { 
136                                         disable_items.push_back(lexical_cast<int>(item));
137                                 }
138                         }
139                         else throw std::invalid_argument("invalid key:" + p.first);
140                 }
141
142                 std::unique_ptr<MenuData> result(new MenuData);
143
144                 {
145                         std::vector<MenuItem> menu_items;
146                         for (int i = 0; i < items.size(); ++i) {
147                                 bool disable = find(disable_items.begin(), disable_items.end(), i) != disable_items.end();// disable_items\82æ\82è\96³\8cø\82È\8d\80\96Ú\82©\94»\92f
148                                 menu_items.push_back(MenuItem(createMenuView(items[i], disable)));
149                         }
150             result->menu_ = std::make_shared<Menu>
151                                 (
152                                         menu_items.front(), menu_items.back(),
153                                         mof::makeFactoryMethod<GridLayout>(direction, 0)
154                                 );
155
156             result->frame_ = 
157                                 std::shared_ptr<Frame>
158                                 (
159                         createFrame
160                         (
161                         title ,
162                         _T("image/frame0.png") ,
163                         _T("image/frame3.png") ,
164                         result->menu_->getView()
165                         ).release()
166                                 );
167         }
168
169                 return result;
170         
171         }
172 //}}}
173 //{{{ get_game_data
174         mof::script::GameData::ptr MyEnvironment::get_game_data(const mof::tstring& resource_path)
175         {
176                 using namespace mof::script;
177                 using namespace boost;
178
179                 DEBUG_PRINT("load_game_data(" << resource_path << ")");
180
181                 if (resource_path == "gamedata.item_profile") {
182                         return impl_->sqlite_.query_item_profile();
183                 }
184                 else if (resource_path == "gamedata.relic_profile") {
185                         return impl_->sqlite_.query_relic_profile();
186                 }
187                 else if (resource_path == "gamedata.ideal_profile") {
188                         return impl_->sqlite_.query_ideal_profile();
189                 }
190                 else if (resource_path == "system.client_region") {
191                         mof::Rectangle<int> rect = mof::GraphicsDevice::getClientRegion();
192                         mof::script::GameData::ptr p = std::make_shared<mof::script::GameData>();
193                         mof::script::GameData::entry_t entry;
194                         entry["width"] = boost::lexical_cast<mof::tstring>(rect.getWidth());
195                         entry["height"] = boost::lexical_cast<mof::tstring>(rect.getHeight());
196                         p->data_.push_back(entry);
197                         return p;
198                 }
199                 else {
200                         DEBUG_PRINT("unknown resource path:" << resource_path);
201                         return std::make_shared<GameData>(); 
202                 }
203         }
204 //}}}   
205 }
206
207