OSDN Git Service

release part9
[moflib/moflib.git] / saisei-1.0 / bin / Debug / script / moflib.lua
1 require('script/strict')
2 --\83\86\81[\83e\83B\83\8a\83e\83B\8aÖ\90\94
3 --{{{ is_empty
4 function is_empty(str)
5         if str == nil or str == '' then return true
6         else return false end
7 end
8 --}}}
9 --{{{ split
10 function split(str, delim)
11         if string.find(str, delim) == nil then
12                 return {str}
13         end
14
15         local result = {}
16         local pat = '(.-)' .. delim .. '()'
17         local last_pos
18         for part, pos in string.gfind(str, pat) do
19                 table.insert(result, part)
20                 last_pos = pos
21         end
22         table.insert(result, string.sub(str, last_pos))
23         return unpack(result)
24 end
25 --}}}
26 --{{{ join
27 function join(tbl, sep)
28         local ret
29         for n, v in pairs(tbl) do
30                 local seg = tostring(v)
31                 if ret == nil then
32                         ret = seg
33                 else
34                         ret = ret .. sep .. seg
35                 end
36         end
37         return ret
38 end
39 --}}}
40 --{{{ find
41 function find(tbl, elem)
42         if tbl == nil then return false end
43
44         for n, v in pairs(tbl) do
45                 if v == elem then return true end
46         end
47         return false
48 end
49 --}}}
50 --{{{ layout
51 function layout(left_margin, width, right_margin, top_margin, height, bottom_margin)
52         -- TODO get from app
53         local data = load_game_data('system.client_region')
54         local screen_width = data[1]['width'] 
55         local screen_height = data[1]['height']
56
57         local x, y --results
58
59         -- TODO parameter check. use error function
60         if left_margin ~= nil and right_margin ~= nil then
61                 -- \8d\89E\82Ì\83}\81[\83W\83\93\82©\82ç\92\86\90S\88Ê\92u\82ð\8b\81\82ß\82é
62                 local center_from_left = left_margin + (1.0 - left_margin - right_margin) / 2
63                 x = center_from_left * screen_width - width / 2
64         end
65         
66         if top_margin ~= nil and bottom_margin ~= nil then
67                 -- \8fã\89º\82Ì\83}\81[\83W\83\93\82©\82ç\92\86\90S\88Ê\92u\82ð\8b\81\82ß\82é
68                 local center_from_top = top_margin + (1.0 - top_margin - bottom_margin) / 2
69                 y = center_from_top * screen_height - height / 2
70         end
71
72
73
74         return x, y;
75 end
76 --}}}
77 --moflib API
78 --{{{ waitFrame
79 function wait_frame(frame)
80         waitFrameImpl(frame)
81         coroutine.yield()
82 end
83
84 --\82±\82±\82Ì\83L\81[\92è\90\94\82ÍInputReceiver\82Æ\93¯\8aú\82µ\82Ä\82¢\82È\82¯\82ê\82Î\82È\82ç\82È\82¢
85 KEY_UP    = 1
86 KEY_DOWN  = 2
87 KEY_LEFT  = 3
88 KEY_RIGHT  = 4
89 KEY_A      = 5
90 KEY_B      = 6
91 KEY_C      = 7
92 KEY_D      = 8
93 KEY_E      = 9
94 KEY_F      = 10
95 KEY_G      = 11
96 KEY_H      = 12
97 KEY_I      = 13
98 KEY_J      = 14
99 KEY_K      = 15
100 KEY_L      = 16
101 KEY_M      = 17
102 KEY_N      = 18
103 KEY_O      = 19
104 KEY_P      = 20
105 KEY_Q      = 21
106 KEY_R      = 22
107 KEY_S      = 23
108 KEY_T      = 24
109 KEY_U      = 25
110 KEY_V      = 26
111 KEY_W      = 27
112 KEY_X      = 28
113 KEY_Y      = 29
114 KEY_Z      = 30
115 KEY_ESCAPE = 31 
116 KEY_ANY    = 32
117 --}}}
118 --{{{ sound
119 function sound(filepath)
120         local id = soundCreateImpl(filepath)
121         soundPlayImpl(id)
122         --disposeImpl(id, 'sound')
123 end
124 --}}}
125 --{{{ load_game_data
126 function load_game_data(resource_path)
127         return loadGameDataImpl(resource_path)
128 end
129 --}}}
130 --{{{ wait_for_key
131 function wait_for_key(key)
132         waitForKeyImpl(key)
133         coroutine.yield()
134 end
135 --}}}
136 --{{{ get_last_key
137 function get_last_key()
138         return getLastKeyImpl()
139 end
140 --}}}
141 --{{{ get_key
142 function get_key()
143         wait_for_key(KEY_ANY)
144         return get_last_key()
145 end
146 --}}}
147 --{{{ message
148 function message(title, texts, style, initializer)
149         local obj = Message:new(title)
150         if initializer ~= nil then initializer(obj) end
151         local frame = obj:show()
152         --wait_frame(frame)
153         for i = 1 , #texts , 1  
154         do
155                 local frame = obj:next(texts[i])
156                 wait_frame(frame)
157                 wait_for_key(KEY_Z)
158         end 
159         local frame = obj:hide()
160         wait_frame(frame)
161         obj:dispose()
162 end
163 --{{{ new
164 Message = {}
165 function Message:new(title, style)
166         if style == nil then style = '' end
167         local id = messageNewImpl(title, style)
168         local obj = {}
169         obj.id_ = id
170         setmetatable(obj, {__index = Message})
171         return obj
172 end
173 --}}}
174 --{{{ show
175 function Message:show()
176         return showImpl(self.id_, 'message')
177 end
178 --}}}
179 --{{{ hide
180 function Message:hide()
181         return hideImpl(self.id_, 'message')
182 end
183 --}}}
184 --{{{ set_tween
185 function Message:set_tween(target, stream, period)
186         if period == nil then period = 0 end
187         objectSetBehaviorImpl(self.id_, 'message.' .. target, stream, period)
188 end
189 --}}}
190 --{{{ get_property
191 function Message:get_properties()
192         return objectGetPropertiesImpl(self.id_, 'message')
193 end
194 --}}}
195 --{{{ next
196 function Message:next(text)
197         print_debug(text)
198         return messageNextImpl(self.id_, text)
199 end
200 --}}}
201 --{{{ dispose
202 function Message:dispose(text)
203         return disposeImpl(self.id_, 'message')
204 end
205 --}}}
206 --}}}
207 --{{{ question
208 function question(title, items, style, disables, initializer)
209         if disables ~= nil then 
210                 -- disable\82ª\8ew\92è\82³\82ê\82Ä\82¢\82½\82çstyle\82É\92Ç\89Á
211                 if not is_empty(style) then style = style .. ';' end
212                 style = 'disable=' .. join(disables, ',')
213         end
214         local obj = Question:new(title, items, style)
215         if initializer ~= nil then initializer(obj) end
216         local frame = obj:show()
217         wait_frame(frame)
218         while true do
219                 local key = get_key()
220                 if key == KEY_UP then obj:move_cursor_up()
221                 elseif key == KEY_DOWN then obj:move_cursor_down()
222                 elseif key == KEY_LEFT then obj:move_cursor_left()
223                 elseif key == KEY_RIGHT then obj:move_cursor_right()
224                 elseif key == KEY_X then 
225                         sound("sound/cancel.wav")-- \83L\83\83\83\93\83Z\83\8b\89¹
226                         frame = obj:hide()
227                         wait_frame(frame)
228                         obj:dispose()
229                         return 0 --\83L\83\83\83\93\83Z\83\8b
230                 elseif key == KEY_Z then 
231                         local index = obj:get_current()
232                         if not find(disables, index) then -- disable\82È\82ç\96³\8e\8b\82·\82é
233                                 sound("sound/decide.wav")-- \8c\88\92è\89¹
234                                 frame = obj:select()
235                                 wait_frame(frame)
236                                 frame = obj:hide()
237                                 wait_frame(frame)
238                                 obj:dispose()
239                                 return index + 1 --0 \82Í\83L\83\83\83\93\83Z\83\8b\8e\9e\82Ì\93®\82«
240                         end
241                 end     
242         end
243 end
244 --{{{ new
245 Question = {}
246 function Question:new(title, items, style)
247         if style == nil then style = '' end
248         local id = menuNewImpl(title, items, style)
249         local obj = {}
250         obj.id_ = id
251         setmetatable(obj, {__index = Question})
252         return obj
253 end
254 --}}}
255 --{{{ show
256 function Question:show()
257         return showImpl(self.id_, 'menu')
258 end
259 --}}}
260 --{{{ hide
261 function Question:hide()
262         return hideImpl(self.id_, 'menu')
263 end
264 --}}}
265 --{{{ set_tween
266 function Question:set_tween(target, stream, period)
267         if period == nil then period = 0 end
268         objectSetBehaviorImpl(self.id_, 'menu.' .. target, stream, period)
269 end
270 --}}}
271 --{{{ get_property
272 function Question:get_properties()
273         return objectGetPropertiesImpl(self.id_, 'menu')
274 end
275 --}}}
276 --{{{ move_cursor
277 MOVE_UP    = 0
278 MOVE_DOWN  = 1
279 MOVE_LEFT  = 2
280 MOVE_RIGHT = 3
281
282 function Question:move_cursor_up()
283         return menuMoveCursorImpl(self.id_, MOVE_UP)
284 end
285
286 function Question:move_cursor_down()
287         return menuMoveCursorImpl(self.id_, MOVE_DOWN)
288 end
289
290 function Question:move_cursor_left()
291         return menuMoveCursorImpl(self.id_, MOVE_LEFT)
292 end
293
294 function Question:move_cursor_right()
295         return menuMoveCursorImpl(self.id_, MOVE_RIGHT)
296 end
297 --}}}
298 --{{{ get_currnet
299 function Question:get_current()
300         return menuGetCurrentImpl(self.id_)
301 end
302 --}}}
303 --{{{ select
304 function Question:select()
305         return menuSelectImpl(self.id_)
306 end
307 --}}}
308 --{{{ dispose
309 function Question:dispose()
310         return disposeImpl(self.id_, 'menu')
311 end
312 --}}}
313 --}}}
314 --{{{ print_debug
315 function print_debug(obj, spacer)
316         if (type(obj) ~= 'table') then
317                 printDebugImpl(tostring(obj))
318                 return
319         end
320
321         if spacer == nil then spacer = '' end
322
323         for key, value in pairs(obj) do  
324                 if (type(value) ~= 'table') then
325                         printDebugImpl(spacer .. tostring(key) .. ' => ' .. tostring(value))
326                 else
327                         printDebugImpl(spacer .. tostring(key) .. ' => ' )
328                         print_debug(value, spacer .. '  ')
329                 end
330         end
331 end
332 --}}}
333 --{{{ picture
334 --{{{ new
335 Picture = {}
336 function Picture:new(path)
337         local id = pictureCreateImpl(path)
338         local obj = {}
339         obj.id_ = id
340         setmetatable(obj, {__index = Picture})
341         return obj
342 end
343 --}}}
344 --{{{ set_tween
345 function Picture:set_tween(target, stream)
346         objectSetBehaviorImpl(self.id_, 'picture.' .. target, stream, 0)
347 end
348 --}}}
349 --{{{ dispose
350 function Picture:dispose()
351         return disposeImpl(self.id_, 'picture')
352 end
353 --}}}
354 --}}}
355 --{{{ particlegen
356 --{{{ new
357 Particlegen = {}
358 function Particlegen:new()
359         local id = particlegenCreateImpl()
360         local obj = {}
361         obj.id_ = id
362         setmetatable(obj, {__index = Particlegen})
363         return obj
364 end
365 --}}}
366 --{{{ set_tween
367 function Particlegen:set_tween(target, stream)
368         objectSetBehaviorImpl(self.id_, 'particlegen.' .. target, stream, 0)
369 end
370 --}}}
371 --{{{ dispose
372 function Particlegen:dispose()
373         return disposeImpl(self.id_, 'particlegen')
374 end
375 --}}}
376 --}}}