]> git.jsancho.org Git - guile-irrlicht.git/blob - src/gui-environment.cpp
df43fc63d5359ef1b4ca5aef614cf8fbc2886a9f
[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-skin", 1, 0, 0, irr_gui_getSkin);
58   }
59
60   DEFINE_WRAPPED_TYPE (irr::gui::IGUIEnvironment*, "gui-environment",
61                        init_gui_environment_type, gui_environment_p,
62                        wrap_gui_environment, unwrap_gui_environment);
63
64   SCM
65   irr_gui_addImage (SCM wrapped_gui_environment,
66                     SCM image,
67                     SCM position,
68                     SCM rest)
69   {
70     SCM use_alpha_channel = SCM_BOOL_T;
71     SCM parent = SCM_UNDEFINED;
72     SCM id = scm_from_int32 (-1);
73     SCM text = SCM_UNDEFINED;
74
75     scm_c_bind_keyword_arguments ("add-image!", rest, (scm_t_keyword_arguments_flags)0,
76                                   scm_from_utf8_keyword ("use_alpha_channel"), &use_alpha_channel,
77                                   scm_from_utf8_keyword ("parent"), &parent,
78                                   scm_from_utf8_keyword ("id"), &id,
79                                   scm_from_utf8_keyword ("text"), &text,
80                                   SCM_UNDEFINED);
81
82     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
83     irr::gui::IGUIImage* guiImage =
84       guienv->addImage (unwrap_texture (image),
85                         scm_to_position2d_s32 (position),
86                         scm_to_bool (use_alpha_channel),
87                         parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
88                         scm_to_int32 (id),
89                         text == SCM_UNDEFINED ? 0 : scm_to_wide_char_string (text));
90     return wrap_gui_image (guiImage);
91   }
92
93   SCM
94   irr_gui_addEditBox (SCM wrapped_gui_environment,
95                       SCM text,
96                       SCM rectangle,
97                       SCM rest)
98   {
99     SCM border = SCM_BOOL_T;
100     SCM parent = SCM_UNDEFINED;
101     SCM id = scm_from_int32 (-1);
102
103     scm_c_bind_keyword_arguments ("add-editbox!", rest, (scm_t_keyword_arguments_flags)0,
104                                   scm_from_utf8_keyword ("border"), &border,
105                                   scm_from_utf8_keyword ("parent"), &parent,
106                                   scm_from_utf8_keyword ("id"), &id,
107                                   SCM_UNDEFINED);
108
109     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
110     irr::gui::IGUIEditBox* editbox =
111       guienv->addEditBox (scm_to_wide_char_string (text),
112                           scm_to_rect_s32 (rectangle),
113                           scm_to_bool (border),
114                           parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
115                           scm_to_int32 (id));
116     return wrap_gui_editbox (editbox);
117   }
118
119   SCM
120   irr_gui_addListBox (SCM wrapped_gui_environment,
121                       SCM rectangle,
122                       SCM rest)
123   {
124     SCM parent = SCM_UNDEFINED;
125     SCM id = scm_from_int32 (-1);
126     SCM draw_background = SCM_BOOL_F;
127
128     scm_c_bind_keyword_arguments ("add-listbox!", rest, (scm_t_keyword_arguments_flags)0,
129                                   scm_from_utf8_keyword ("parent"), &parent,
130                                   scm_from_utf8_keyword ("id"), &id,
131                                   scm_from_utf8_keyword ("draw-background"), &draw_background,
132                                   SCM_UNDEFINED);
133
134     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
135     irr::gui::IGUIListBox* listbox =
136       guienv->addListBox (scm_to_rect_s32 (rectangle),
137                           parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
138                           scm_to_int32 (id),
139                           scm_to_bool (draw_background));
140     return wrap_gui_listbox (listbox);
141   }
142
143   SCM
144   irr_gui_addScrollBar (SCM wrapped_gui_environment,
145                         SCM horizontal,
146                         SCM rectangle,
147                         SCM rest)
148   {
149     SCM parent = SCM_UNDEFINED;
150     SCM id = scm_from_int32 (-1);
151
152     scm_c_bind_keyword_arguments ("add-scrollbar!", rest, (scm_t_keyword_arguments_flags)0,
153                                   scm_from_utf8_keyword ("parent"), &parent,
154                                   scm_from_utf8_keyword ("id"), &id,
155                                   SCM_UNDEFINED);
156
157     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
158     irr::gui::IGUIScrollBar* scrollbar =
159       guienv->addScrollBar (scm_to_bool (horizontal),
160                             scm_to_rect_s32 (rectangle),
161                             parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
162                             scm_to_int32 (id));
163     return wrap_gui_scrollbar (scrollbar);
164   }
165
166   SCM
167   irr_gui_addStaticText (SCM wrapped_gui_environment,
168                          SCM text,
169                          SCM rectangle,
170                          SCM rest)
171   {
172     SCM border = SCM_BOOL_F;
173     SCM word_wrap = SCM_BOOL_T;
174     SCM parent = SCM_UNDEFINED;
175     SCM id = scm_from_int32 (-1);
176     SCM fill_background = SCM_BOOL_F;
177
178     scm_c_bind_keyword_arguments ("add-static-text!", rest, (scm_t_keyword_arguments_flags)0,
179                                   scm_from_utf8_keyword ("border"), &border,
180                                   scm_from_utf8_keyword ("word-wrap"), &word_wrap,
181                                   scm_from_utf8_keyword ("parent"), &parent,
182                                   scm_from_utf8_keyword ("id"), &id,
183                                   scm_from_utf8_keyword ("fill-background"), &fill_background,
184                                   SCM_UNDEFINED);
185
186     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
187     irr::gui::IGUIStaticText* staticText =
188       guienv->addStaticText (scm_to_wide_char_string (text),
189                              scm_to_rect_s32 (rectangle),
190                              scm_to_bool (border),
191                              scm_to_bool (word_wrap),
192                              parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
193                              scm_to_int32 (id),
194                              scm_to_bool (fill_background));
195     return wrap_gui_static_text (staticText);
196   }
197
198   SCM
199   irr_gui_addWindow (SCM wrapped_gui_environment,
200                      SCM rectangle,
201                      SCM rest)
202   {
203     SCM modal = SCM_BOOL_F;
204     SCM text = SCM_UNDEFINED;
205     SCM parent = SCM_UNDEFINED;
206     SCM id = scm_from_int32 (-1);
207
208     scm_c_bind_keyword_arguments ("add-window!", rest, (scm_t_keyword_arguments_flags)0,
209                                   scm_from_utf8_keyword ("modal"), &modal,
210                                   scm_from_utf8_keyword ("text"), &text,
211                                   scm_from_utf8_keyword ("parent"), &parent,
212                                   scm_from_utf8_keyword ("id"), &id,
213                                   SCM_UNDEFINED);
214
215     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
216     irr::gui::IGUIWindow* window =
217       guienv->addWindow (scm_to_rect_s32 (rectangle),
218                          scm_to_bool (modal),
219                          text == SCM_UNDEFINED ? 0 : scm_to_wide_char_string (text),
220                          parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
221                          scm_to_int32 (id));
222     return wrap_gui_window (window);
223   }
224
225   SCM
226   irr_gui_getBuiltInFont (SCM wrapped_gui_environment)
227   {
228     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
229     irr::gui::IGUIFont* font = guienv->getBuiltInFont ();
230     return wrap_gui_font (font);
231   }
232
233   SCM
234   irr_gui_getSkin (SCM wrapped_gui_environment)
235   {
236     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
237     irr::gui::IGUISkin* skin = guienv->getSkin ();
238     return wrap_gui_skin (skin);
239   }
240
241   SCM
242   irr_gui_IGUIEnvironment_addButton (SCM wrapped_gui_environment,
243                                      SCM rectangle,
244                                      SCM rest)
245   {
246     SCM parent = SCM_UNDEFINED;
247     SCM id = scm_from_int32 (-1);
248     SCM text = SCM_UNDEFINED;
249     SCM tooltiptext = SCM_UNDEFINED;
250
251     scm_c_bind_keyword_arguments ("add-button!", rest, (scm_t_keyword_arguments_flags)0,
252                                   scm_from_utf8_keyword ("parent"), &parent,
253                                   scm_from_utf8_keyword ("id"), &id,
254                                   scm_from_utf8_keyword ("text"), &text,
255                                   scm_from_utf8_keyword ("tooltiptext"), &tooltiptext,
256                                   SCM_UNDEFINED);
257
258     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
259     irr::gui::IGUIButton* button =
260       guienv->addButton (scm_to_rect_s32 (rectangle),
261                          parent == SCM_UNDEFINED ? 0 : unwrap_gui_element (parent),
262                          scm_to_int32 (id),
263                          text == SCM_UNDEFINED ? 0 : scm_to_wide_char_string (text),
264                          tooltiptext == SCM_UNDEFINED ? 0 : scm_to_wide_char_string (tooltiptext));
265     return wrap_gui_button (button);
266   }
267
268   SCM
269   irr_gui_IGUIEnvironment_getFont (SCM wrapped_gui_environment,
270                                    SCM filename)
271   {
272     irr::gui::IGUIEnvironment* guienv = unwrap_gui_environment (wrapped_gui_environment);
273     irr::gui::IGUIFont* font = guienv->getFont (scm_to_utf8_stringn (filename, NULL));
274     return wrap_gui_font (font);
275   }
276
277 }