]> git.jsancho.org Git - guile-irrlicht.git/blob - src/gui-environment.cpp
add-window!
[guile-irrlicht.git] / src / gui-environment.cpp
1 /* guile-irrlicht --- GNU Guile bindings for Irrlicht Engine
2
3    Copyright (C) 2020 Javier Sancho <jsf@jsancho.org>
4
5    This file is part of guile-irrlicht.
6
7    guile-irrlicht is free software; you can redistribute it and/or modify
8    it under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 3 of the
10    License, or (at your option) any later version.
11
12    guile-irrlicht is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with guile-irrlicht. If not, see
19    <http://www.gnu.org/licenses/>.
20 */
21
22 #include <irrlicht/irrlicht.h>
23 #include <libguile.h>
24
25 #include "device.h"
26 #include "gsubr.h"
27 #include "gui-button.h"
28 #include "gui-editbox.h"
29 #include "gui-element.h"
30 #include "gui-environment.h"
31 #include "gui-font.h"
32 #include "gui-image.h"
33 #include "gui-listbox.h"
34 #include "gui-scrollbar.h"
35 #include "gui-skin.h"
36 #include "gui-static-text.h"
37 #include "gui-window.h"
38 #include "position2d.h"
39 #include "rect.h"
40 #include "texture.h"
41 #include "wchar.h"
42 #include "wrapped.h"
43
44 extern "C" {
45
46   void
47   init_gui_environment (void)
48   {
49     init_gui_environment_type ();
50     DEFINE_GSUBR ("add-image!", 3, 0, 1, irr_gui_addImage);
51     DEFINE_GSUBR ("add-editbox!", 3, 0, 1, irr_gui_addEditBox);
52     DEFINE_GSUBR ("add-listbox!", 2, 0, 1, irr_gui_addListBox);
53     DEFINE_GSUBR ("add-scrollbar!", 3, 0, 1, irr_gui_addScrollBar);
54     DEFINE_GSUBR ("add-static-text!", 3, 0, 1, irr_gui_addStaticText);
55     DEFINE_GSUBR ("add-window!", 2, 0, 1, irr_gui_addWindow);
56     DEFINE_GSUBR ("get-built-in-font", 1, 0, 0, irr_gui_getBuiltInFont);
57     DEFINE_GSUBR ("get-gui-environment", 1, 0, 0, irr_getGUIEnvironment);
58     DEFINE_GSUBR ("get-skin", 1, 0, 0, irr_gui_getSkin);
59   }
60
61   DEFINE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, "gui-environment",
62                        init_gui_environment_type, gui_environment_p,
63                        wrap_gui_environment, unwrap_gui_environment);
64
65   SCM
66   irr_getGUIEnvironment (SCM wrapped_obj)
67   {
68     irr::gui::IGUIEnvironment* gui_environment;
69     if (device_p (wrapped_obj))
70       {
71         gui_environment = unwrap_device (wrapped_obj)->getGUIEnvironment ();
72       }
73     else
74       {
75         scm_error (scm_arg_type_key, NULL, "Cannot get GUI environment from object: ~S",
76                    scm_list_1 (wrapped_obj), scm_list_1 (wrapped_obj));
77       }
78     return wrap_gui_environment (gui_environment);
79   }
80
81   SCM
82   irr_gui_addImage (SCM wrapped_gui_environment,
83                     SCM image,
84                     SCM position,
85                     SCM rest)
86   {
87     SCM use_alpha_channel = SCM_BOOL_T;
88     SCM parent = SCM_BOOL_F;
89     SCM id = scm_from_int32 (-1);
90     SCM text = SCM_BOOL_F;
91
92     scm_c_bind_keyword_arguments ("add-image!", rest, (scm_t_keyword_arguments_flags)0,
93                                   scm_from_utf8_keyword ("use_alpha_channel"), &use_alpha_channel,
94                                   scm_from_utf8_keyword ("parent"), &parent,
95                                   scm_from_utf8_keyword ("id"), &id,
96                                   scm_from_utf8_keyword ("text"), &text,
97                                   SCM_UNDEFINED);
98
99     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
100     irr::gui::IGUIImage* guiImage =
101       guienv->addImage (unwrap_texture (image),
102                         scm_to_position2d_s32 (position),
103                         scm_to_bool (use_alpha_channel),
104                         scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
105                         scm_to_int32 (id),
106                         scm_is_false (text) ? 0 : scm_to_wide_char_string (text));
107     return wrap_gui_image (guiImage);
108   }
109
110   SCM
111   irr_gui_addEditBox (SCM wrapped_gui_environment,
112                       SCM text,
113                       SCM rectangle,
114                       SCM rest)
115   {
116     SCM border = SCM_BOOL_T;
117     SCM parent = SCM_BOOL_F;
118     SCM id = scm_from_int32 (-1);
119
120     scm_c_bind_keyword_arguments ("add-editbox!", rest, (scm_t_keyword_arguments_flags)0,
121                                   scm_from_utf8_keyword ("border"), &border,
122                                   scm_from_utf8_keyword ("parent"), &parent,
123                                   scm_from_utf8_keyword ("id"), &id,
124                                   SCM_UNDEFINED);
125
126     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
127     irr::gui::IGUIEditBox* editbox =
128       guienv->addEditBox (scm_to_wide_char_string (text),
129                           scm_to_rect_s32 (rectangle),
130                           scm_to_bool (border),
131                           scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
132                           scm_to_int32 (id));
133     return wrap_gui_editbox (editbox);
134   }
135
136   SCM
137   irr_gui_addListBox (SCM wrapped_gui_environment,
138                       SCM rectangle,
139                       SCM rest)
140   {
141     SCM parent = SCM_BOOL_F;
142     SCM id = scm_from_int32 (-1);
143     SCM draw_background = SCM_BOOL_F;
144
145     scm_c_bind_keyword_arguments ("add-listbox!", rest, (scm_t_keyword_arguments_flags)0,
146                                   scm_from_utf8_keyword ("parent"), &parent,
147                                   scm_from_utf8_keyword ("id"), &id,
148                                   scm_from_utf8_keyword ("draw-background"), &draw_background,
149                                   SCM_UNDEFINED);
150
151     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
152     irr::gui::IGUIListBox* listbox =
153       guienv->addListBox (scm_to_rect_s32 (rectangle),
154                           scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
155                           scm_to_int32 (id),
156                           scm_to_bool (draw_background));
157     return wrap_gui_listbox (listbox);
158   }
159
160   SCM
161   irr_gui_addScrollBar (SCM wrapped_gui_environment,
162                         SCM horizontal,
163                         SCM rectangle,
164                         SCM rest)
165   {
166     SCM parent = SCM_BOOL_F;
167     SCM id = scm_from_int32 (-1);
168
169     scm_c_bind_keyword_arguments ("add-scrollbar!", rest, (scm_t_keyword_arguments_flags)0,
170                                   scm_from_utf8_keyword ("parent"), &parent,
171                                   scm_from_utf8_keyword ("id"), &id,
172                                   SCM_UNDEFINED);
173
174     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
175     irr::gui::IGUIScrollBar* scrollbar =
176       guienv->addScrollBar (scm_to_bool (horizontal),
177                             scm_to_rect_s32 (rectangle),
178                             scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
179                             scm_to_int32 (id));
180     return wrap_gui_scrollbar (scrollbar);
181   }
182
183   SCM
184   irr_gui_addStaticText (SCM wrapped_gui_environment,
185                          SCM text,
186                          SCM rectangle,
187                          SCM rest)
188   {
189     SCM border = SCM_BOOL_F;
190     SCM word_wrap = SCM_BOOL_T;
191     SCM parent = SCM_BOOL_F;
192     SCM id = scm_from_int32 (-1);
193     SCM fill_background = SCM_BOOL_F;
194
195     scm_c_bind_keyword_arguments ("add-static-text!", rest, (scm_t_keyword_arguments_flags)0,
196                                   scm_from_utf8_keyword ("border"), &border,
197                                   scm_from_utf8_keyword ("word-wrap"), &word_wrap,
198                                   scm_from_utf8_keyword ("parent"), &parent,
199                                   scm_from_utf8_keyword ("id"), &id,
200                                   scm_from_utf8_keyword ("fill-background"), &fill_background,
201                                   SCM_UNDEFINED);
202
203     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
204     irr::gui::IGUIStaticText* staticText =
205       guienv->addStaticText (scm_to_wide_char_string (text),
206                              scm_to_rect_s32 (rectangle),
207                              scm_to_bool (border),
208                              scm_to_bool (word_wrap),
209                              scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
210                              scm_to_int32 (id),
211                              scm_to_bool (fill_background));
212     return wrap_gui_static_text (staticText);
213   }
214
215   SCM
216   irr_gui_addWindow (SCM wrapped_gui_environment,
217                      SCM rectangle,
218                      SCM rest)
219   {
220     SCM modal = SCM_BOOL_F;
221     SCM text = SCM_BOOL_F;
222     SCM parent = SCM_BOOL_F;
223     SCM id = scm_from_int32 (-1);
224
225     scm_c_bind_keyword_arguments ("add-window!", rest, (scm_t_keyword_arguments_flags)0,
226                                   scm_from_utf8_keyword ("modal"), &modal,
227                                   scm_from_utf8_keyword ("text"), &text,
228                                   scm_from_utf8_keyword ("parent"), &parent,
229                                   scm_from_utf8_keyword ("id"), &id,
230                                   SCM_UNDEFINED);
231
232     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
233     irr::gui::IGUIWindow* window =
234       guienv->addWindow (scm_to_rect_s32 (rectangle),
235                          scm_to_bool (modal),
236                          scm_is_false (text) ? 0 : scm_to_wide_char_string (text),
237                          scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
238                          scm_to_int32 (id));
239     return wrap_gui_window (window);
240   }
241
242   SCM
243   irr_gui_getBuiltInFont (SCM wrapped_gui_environment)
244   {
245     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
246     irr::gui::IGUIFont* font = guienv->getBuiltInFont ();
247     return wrap_gui_font (font);
248   }
249
250   SCM
251   irr_gui_getSkin (SCM wrapped_gui_environment)
252   {
253     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
254     irr::gui::IGUISkin* skin = guienv->getSkin ();
255     return wrap_gui_skin (skin);
256   }
257
258   SCM
259   irr_gui_IGUIEnvironment_addButton (SCM wrapped_gui_environment,
260                                      SCM rectangle,
261                                      SCM rest)
262   {
263     SCM parent = SCM_BOOL_F;
264     SCM id = scm_from_int32 (-1);
265     SCM text = SCM_BOOL_F;
266     SCM tooltiptext = SCM_BOOL_F;
267
268     scm_c_bind_keyword_arguments ("add-button!", rest, (scm_t_keyword_arguments_flags)0,
269                                   scm_from_utf8_keyword ("parent"), &parent,
270                                   scm_from_utf8_keyword ("id"), &id,
271                                   scm_from_utf8_keyword ("text"), &text,
272                                   scm_from_utf8_keyword ("tooltiptext"), &tooltiptext,
273                                   SCM_UNDEFINED);
274
275     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
276     irr::gui::IGUIButton* button =
277       guienv->addButton (scm_to_rect_s32 (rectangle),
278                          scm_is_false (parent) ? 0 : unwrap_gui_element (parent),
279                          scm_to_int32 (id),
280                          scm_is_false (text) ? 0 : scm_to_wide_char_string (text),
281                          scm_is_false (tooltiptext) ? 0 : scm_to_wide_char_string (tooltiptext));
282     return wrap_gui_button (button);
283   }
284
285   SCM
286   irr_gui_IGUIEnvironment_getFont (SCM wrapped_gui_environment,
287                                    SCM filename)
288   {
289     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
290     irr::gui::IGUIFont* font = guienv->getFont (scm_to_utf8_stringn (filename, NULL));
291     return wrap_gui_font (font);
292   }
293
294 }