diff options
Diffstat (limited to 'platform/javascript')
| -rw-r--r-- | platform/javascript/export/export.cpp | 72 | ||||
| -rw-r--r-- | platform/javascript/os_javascript.cpp | 68 | ||||
| -rw-r--r-- | platform/javascript/os_javascript.h | 8 |
3 files changed, 130 insertions, 18 deletions
diff --git a/platform/javascript/export/export.cpp b/platform/javascript/export/export.cpp index c769d2082..e055aeea5 100644 --- a/platform/javascript/export/export.cpp +++ b/platform/javascript/export/export.cpp @@ -50,6 +50,7 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform { PACK_MULTIPLE_FILES }; + void _fix_html(Vector<uint8_t>& p_html, const String& p_name, bool p_debug); PackMode pack_mode; @@ -58,6 +59,12 @@ class EditorExportPlatformJavaScript : public EditorExportPlatform { int max_memory; int version_code; + String html_title; + String html_head_include; + String html_font_family; + String html_style_include; + bool html_controls_enabled; + Ref<ImageTexture> logo; protected: @@ -101,6 +108,16 @@ bool EditorExportPlatformJavaScript::_set(const StringName& p_name, const Varian show_run=p_value; else if (n=="options/memory_size") max_memory=p_value; + else if (n=="html/title") + html_title=p_value; + else if (n=="html/head_include") + html_head_include=p_value; + else if (n=="html/font_family") + html_font_family=p_value; + else if (n=="html/style_include") + html_style_include=p_value; + else if (n=="html/controls_enabled") + html_controls_enabled=p_value; else return false; @@ -119,6 +136,16 @@ bool EditorExportPlatformJavaScript::_get(const StringName& p_name,Variant &r_re r_ret=show_run; else if (n=="options/memory_size") r_ret=max_memory; + else if (n=="html/title") + r_ret=html_title; + else if (n=="html/head_include") + r_ret=html_head_include; + else if (n=="html/font_family") + r_ret=html_font_family; + else if (n=="html/style_include") + r_ret=html_style_include; + else if (n=="html/controls_enabled") + r_ret=html_controls_enabled; else return false; @@ -130,39 +157,47 @@ void EditorExportPlatformJavaScript::_get_property_list( List<PropertyInfo> *p_l p_list->push_back( PropertyInfo( Variant::STRING, "custom_package/release", PROPERTY_HINT_GLOBAL_FILE,"zip")); p_list->push_back( PropertyInfo( Variant::INT, "options/memory_size",PROPERTY_HINT_ENUM,"32mb,64mb,128mb,256mb,512mb,1024mb")); p_list->push_back( PropertyInfo( Variant::BOOL, "browser/enable_run")); + p_list->push_back( PropertyInfo( Variant::STRING, "html/title")); + p_list->push_back( PropertyInfo( Variant::STRING, "html/head_include",PROPERTY_HINT_MULTILINE_TEXT)); + p_list->push_back( PropertyInfo( Variant::STRING, "html/font_family")); + p_list->push_back( PropertyInfo( Variant::STRING, "html/style_include",PROPERTY_HINT_MULTILINE_TEXT)); + p_list->push_back( PropertyInfo( Variant::BOOL, "html/controls_enabled")); + //p_list->push_back( PropertyInfo( Variant::INT, "resources/pack_mode", PROPERTY_HINT_ENUM,"Copy,Single Exec.,Pack (.pck),Bundles (Optical)")); } -static void _fix_html(Vector<uint8_t>& html,const String& name,int max_memory) { +void EditorExportPlatformJavaScript::_fix_html(Vector<uint8_t>& p_html, const String& p_name, bool p_debug) { String str; String strnew; - str.parse_utf8((const char*)html.ptr(),html.size()); + str.parse_utf8((const char*)p_html.ptr(),p_html.size()); Vector<String> lines=str.split("\n"); for(int i=0;i<lines.size();i++) { - if (lines[i].find("$GODOTTMEM")!=-1) { - - strnew+=lines[i].replace("$GODOTTMEM",itos(max_memory*1024*1024))+"\n"; - } else if (lines[i].find("$GODOTFS")!=-1) { - strnew+=lines[i].replace("$GODOTFS",name+"fs.js")+"\n"; - } else if (lines[i].find("$GODOTMEM")!=-1) { - strnew+=lines[i].replace("$GODOTMEM",name+".mem")+"\n"; - } else if (lines[i].find("$GODOTJS")!=-1) { - strnew+=lines[i].replace("$GODOTJS",name+".js")+"\n"; - } else { - strnew+=lines[i]+"\n"; - } + String current_line = lines[i]; + current_line = current_line.replace("$GODOT_TMEM",itos((1<<(max_memory+5))*1024*1024)); + current_line = current_line.replace("$GODOT_FS",p_name+"fs.js"); + current_line = current_line.replace("$GODOT_MEM",p_name+".mem"); + current_line = current_line.replace("$GODOT_JS",p_name+".js"); + current_line = current_line.replace("$GODOT_CANVAS_WIDTH",Globals::get_singleton()->get("display/width")); + current_line = current_line.replace("$GODOT_CANVAS_HEIGHT",Globals::get_singleton()->get("display/height")); + current_line = current_line.replace("$GODOT_HEAD_TITLE",!html_title.empty()?html_title:(String) Globals::get_singleton()->get("application/name")); + current_line = current_line.replace("$GODOT_HEAD_INCLUDE",html_head_include); + current_line = current_line.replace("$GODOT_STYLE_FONT_FAMILY",html_font_family); + current_line = current_line.replace("$GODOT_STYLE_INCLUDE",html_style_include); + current_line = current_line.replace("$GODOT_CONTROLS_ENABLED",html_controls_enabled?"true":"false"); + current_line = current_line.replace("$GODOT_DEBUG_ENABLED",p_debug?"true":"false"); + strnew += current_line+"\n"; } CharString cs = strnew.utf8(); - html.resize(cs.size()); + p_html.resize(cs.size()); for(int i=9;i<cs.size();i++) { - html[i]=cs[i]; + p_html[i]=cs[i]; } } @@ -274,7 +309,7 @@ Error EditorExportPlatformJavaScript::export_project(const String& p_path, bool if (file=="godot.html") { - _fix_html(data,p_path.get_file().basename(),1<<(max_memory+5)); + _fix_html(data,p_path.get_file().basename(), p_debug); file=p_path.get_file(); } if (file=="godotfs.js") { @@ -335,6 +370,9 @@ EditorExportPlatformJavaScript::EditorExportPlatformJavaScript() { logo = Ref<ImageTexture>( memnew( ImageTexture )); logo->create_from_image(img); max_memory=3; + html_title=""; + html_font_family="arial,sans-serif"; + html_controls_enabled=true; pack_mode=PACK_SINGLE_FILE; } diff --git a/platform/javascript/os_javascript.cpp b/platform/javascript/os_javascript.cpp index 7bb47881a..ffbcd9824 100644 --- a/platform/javascript/os_javascript.cpp +++ b/platform/javascript/os_javascript.cpp @@ -76,6 +76,14 @@ void OS_JavaScript::set_opengl_extensions(const char* p_gl_extensions) { gl_extensions=p_gl_extensions; } +static EM_BOOL joy_callback_func(int p_type, const EmscriptenGamepadEvent *p_event, void *p_user) { + OS_JavaScript *os = (OS_JavaScript*) OS::get_singleton(); + if (os) { + return os->joy_connection_changed(p_type, p_event); + } + return false; +} + void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int p_audio_driver) { print_line("Init OS"); @@ -142,6 +150,8 @@ void OS_JavaScript::initialize(const VideoMode& p_desired,int p_video_driver,int input = memnew( InputDefault ); + emscripten_set_gamepadconnected_callback(NULL, true, &joy_callback_func); + emscripten_set_gamepaddisconnected_callback(NULL, true, &joy_callback_func); } void OS_JavaScript::set_main_loop( MainLoop * p_main_loop ) { @@ -296,7 +306,7 @@ bool OS_JavaScript::main_loop_iterate() { } - + process_joysticks(); return Main::iteration(); } @@ -605,6 +615,62 @@ void OS_JavaScript::_close_notification_funcs(const String& p_file,int p_flags) } } +void OS_JavaScript::process_joysticks() { + + int joy_count = emscripten_get_num_gamepads(); + for (int i = 0; i < joy_count; i++) { + EmscriptenGamepadEvent state; + emscripten_get_gamepad_status(i, &state); + if (state.connected) { + + int num_buttons = MIN(state.numButtons, 16); + int num_axes = MIN(state.numAxes, 8); + for (int j = 0; j < num_buttons; j++) { + + float value = state.analogButton[j]; + if (String(state.mapping) == "standard" && (j == 6 || j == 7)) { + InputDefault::JoyAxis jx; + jx.min = 0; + jx.value = value; + last_id = input->joy_axis(last_id, i, j, jx); + } + else { + last_id = input->joy_button(last_id, i, j, value); + } + } + for (int j = 0; j < num_axes; j++) { + + InputDefault::JoyAxis jx; + jx.min = -1; + jx.value = state.axis[j]; + last_id = input->joy_axis(last_id, i, j, jx); + } + } + } +} + +bool OS_JavaScript::joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event) { + if (p_type == EMSCRIPTEN_EVENT_GAMEPADCONNECTED) { + + String guid = ""; + if (String(p_event->mapping) == "standard") + guid = "Default HTML5 Gamepad"; + input->joy_connection_changed(p_event->index, true, String(p_event->id), guid); + } + else { + input->joy_connection_changed(p_event->index, false, ""); + } + return true; +} + +bool OS_JavaScript::is_joy_known(int p_device) { + return input->is_joy_mapped(p_device); +} + +String OS_JavaScript::get_joy_guid(int p_device) const { + return input->get_joy_guid_remapped(p_device); +} + OS_JavaScript::OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func) { diff --git a/platform/javascript/os_javascript.h b/platform/javascript/os_javascript.h index 61eecd8f3..ec3a6cf91 100644 --- a/platform/javascript/os_javascript.h +++ b/platform/javascript/os_javascript.h @@ -41,6 +41,7 @@ #include "audio_server_javascript.h" #include "audio_driver_javascript.h" #include "main/input_default.h" +#include "emscripten/html5.h" typedef void (*GFXInitFunc)(void *ud,bool gl2,int w, int h, bool fs); typedef int (*OpenURIFunc)(const String&); @@ -90,6 +91,8 @@ private: static void _close_notification_funcs(const String& p_file,int p_flags); + void process_joysticks(); + public: // functions used by main to initialize/deintialize the OS @@ -163,6 +166,11 @@ public: void process_accelerometer(const Vector3& p_accelerometer); void process_touch(int p_what,int p_pointer, const Vector<TouchPos>& p_points); void push_input(const InputEvent& p_ev); + + virtual bool is_joy_known(int p_device); + virtual String get_joy_guid(int p_device) const; + bool joy_connection_changed(int p_type, const EmscriptenGamepadEvent *p_event); + OS_JavaScript(GFXInitFunc p_gfx_init_func,void*p_gfx_init_ud, OpenURIFunc p_open_uri_func, GetDataDirFunc p_get_data_dir_func,GetLocaleFunc p_get_locale_func); ~OS_JavaScript(); |
