diff --git a/.gitignore b/.gitignore index a3072b5..2f2226a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ builtins .internal .clang_complete .vscode/ +*.txt diff --git a/defos/src/defos.cpp b/defos/src/defos.cpp index a77fb7e..12749fb 100644 --- a/defos/src/defos.cpp +++ b/defos/src/defos.cpp @@ -337,6 +337,13 @@ static int on_cursor_lock_disabled(lua_State *L) return 0; } +static int set_window_icon(lua_State *L) +{ + const char *icon_path = luaL_checkstring(L, 1); + defos_set_window_icon(icon_path); + return 0; +} + void defos_emit_event(DefosEvent event) { LuaCallbackInfo *mscb = &defos_event_handlers[event]; @@ -401,6 +408,7 @@ static const luaL_reg Module_methods[] = {"get_view_size", get_view_size}, {"set_cursor", set_cursor}, {"reset_cursor", reset_cursor}, + {"set_window_icon", set_window_icon}, {0, 0}}; static void LuaInit(lua_State *L) diff --git a/defos/src/defos_html5.cpp b/defos/src/defos_html5.cpp index 44ef03d..2619b2b 100644 --- a/defos/src/defos_html5.cpp +++ b/defos/src/defos_html5.cpp @@ -144,6 +144,25 @@ void defos_set_window_title(const char* title_lua) { EM_ASM_({document.title = UTF8ToString($0)}, title_lua); } +void defos_set_window_icon(const char *icon_path) +{ + EM_ASM_({ + document.head || (document.head = document.getElementsByTagName('head')[0]); + function changeFavicon(src) { + var link = document.createElement('link'); + var oldLink = document.getElementById('dynamic-favicon'); + link.id = 'dynamic-favicon'; + link.rel = 'shortcut icon'; + link.href = src; + if (oldLink) { + document.head.removeChild(oldLink); + } + document.head.appendChild(link); + } + changeFavicon(UTF8ToString($0)); + }, icon_path); +} + void defos_set_window_size(float x, float y, float w, float h) { defos_set_view_size(x, y, w, h); } diff --git a/defos/src/defos_mac.mm b/defos/src/defos_mac.mm index eb5f005..e6e221f 100644 --- a/defos/src/defos_mac.mm +++ b/defos/src/defos_mac.mm @@ -110,6 +110,16 @@ void defos_set_window_size(float x, float y, float w, float h) { [window setFrame:NSMakeRect(x, win_y, w , h) display:YES]; } +void defos_set_window_icon(const char *icon_path) +{ + NSString *bundlePath = [[NSBundle mainBundle] resourcePath]; + NSString *secondParentPath = [[bundlePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent]; + NSString *path = [secondParentPath stringByAppendingPathComponent:[NSString stringWithUTF8String:icon_path]]; + NSImage* image = [[NSImage alloc] initWithContentsOfFile: path]; + [window setRepresentedURL:[NSURL URLWithString:path]]; + [[window standardWindowButton:NSWindowDocumentIconButton] setImage:image]; +} + void defos_set_view_size(float x, float y, float w, float h) { if (isnan(x)) { NSRect frame = window.screen.frame; diff --git a/defos/src/defos_private.h b/defos/src/defos_private.h index a902274..47da7a8 100644 --- a/defos/src/defos_private.h +++ b/defos/src/defos_private.h @@ -50,6 +50,8 @@ extern bool defos_is_fullscreen(); extern bool defos_is_maximized(); extern void defos_set_window_title(const char* title_lua); +extern void defos_set_window_icon(const char *icon_path); +extern void defos_set_window_icon_mac(dmBuffer::HBuffer buffer); extern void defos_set_window_size(float x, float y, float w, float h); extern WinRect defos_get_window_size(); @@ -75,4 +77,4 @@ extern void defos_set_custom_cursor_html5(const char *url); extern void defos_set_custom_cursor_win(const char *filename); extern void defos_set_custom_cursor_mac(dmBuffer::HBuffer buffer, float hotSpotX, float hotSpotY); extern void defos_set_cursor(DefosCursor cursor); -extern void defos_reset_cursor(); +extern void defos_reset_cursor(); \ No newline at end of file diff --git a/defos/src/defos_win.cpp b/defos/src/defos_win.cpp index a7e755f..4597cb4 100644 --- a/defos/src/defos_win.cpp +++ b/defos/src/defos_win.cpp @@ -206,6 +206,16 @@ void defos_set_window_title(const char *title_lua) SetWindowTextW(dmGraphics::GetNativeWindowsHWND(), CA2W(title_lua)); } +void defos_set_window_icon(const char *icon_path) +{ + HANDLE icon = LoadImage(NULL, icon_path, IMAGE_ICON, 32, 32, LR_LOADFROMFILE); + if (icon) + { + HWND window = dmGraphics::GetNativeWindowsHWND(); + SendMessage(window, (UINT)WM_SETICON, ICON_BIG, (LPARAM)icon); + } +} + WinRect defos_get_window_size() { HWND window = dmGraphics::GetNativeWindowsHWND(); diff --git a/example/example.gui b/example/example.gui index 220debf..c9f0e4a 100644 --- a/example/example.gui +++ b/example/example.gui @@ -2668,6 +2668,166 @@ nodes { text_leading: 1.0 text_tracking: 0.0 } +nodes { + position { + x: 861.0 + y: 389.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEMPLATE + id: "set_icon" + layer: "" + inherit_alpha: true + alpha: 1.0 + template: "/dirtylarry/button.gui" + template_node_child: false +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 300.0 + y: 88.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "button/button_normal" + id: "set_icon/larrybutton" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + parent: "set_icon" + layer: "" + inherit_alpha: true + slice9 { + x: 32.0 + y: 32.0 + z: 32.0 + w: 32.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: true + size_mode: SIZE_MODE_MANUAL +} +nodes { + position { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 250.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_TEXT + blend_mode: BLEND_MODE_ALPHA + text: "Set Icon" + font: "larryfont" + id: "set_icon/larrylabel" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + outline { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + shadow { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + adjust_mode: ADJUST_MODE_FIT + line_break: true + parent: "set_icon/larrybutton" + layer: "" + inherit_alpha: true + alpha: 1.0 + outline_alpha: 1.0 + shadow_alpha: 1.0 + overridden_fields: 4 + overridden_fields: 8 + overridden_fields: 18 + template_node_child: true + text_leading: 1.0 + text_tracking: 0.0 +} material: "/builtins/materials/gui.material" adjust_reference: ADJUST_REFERENCE_LEGACY max_nodes: 512 diff --git a/example/example.gui_script b/example/example.gui_script index 6741156..8a01863 100644 --- a/example/example.gui_script +++ b/example/example.gui_script @@ -4,6 +4,13 @@ local cursor_url = require("example.html5_cursor") local system_name = sys.get_sys_info().system_name +--pls add the files using bundle_resources parameter in game.project https://www.defold.com/manuals/project-settings/ +local ICON_NAMES = { + ["Windows"] = "win.ico", + ["HTML5"] = "html5.ico", + ["Darwin"] = "/mac.png" +} + function window_callback(self, event, data) if event == window.WINDOW_EVENT_FOCUS_LOST then -- though after lost focus cursor clipping will restore, we should restore it @@ -204,6 +211,10 @@ function on_input(self, action_id, action) end end) + dirtylarry:button("set_icon", action_id, action, function() + defos.set_window_icon(ICON_NAMES[system_name]) + end) + dirtylarry:button("change_cursor", action_id, action, function() self.current_cursor = self.current_cursor + 1 diff --git a/game.project b/game.project index 36be68e..57620fb 100644 --- a/game.project +++ b/game.project @@ -3,6 +3,8 @@ title = DefOS version = 1.0.1 dependencies = https://github.com/andsve/dirtylarry/archive/master.zip custom_resources = resources/ +bundle_resources = /icons +write_log = 0 [bootstrap] main_collection = /example/example.collectionc diff --git a/icons/js-web/html5.ico b/icons/js-web/html5.ico new file mode 100644 index 0000000..b260328 Binary files /dev/null and b/icons/js-web/html5.ico differ diff --git a/icons/x86-osx/mac.png b/icons/x86-osx/mac.png new file mode 100644 index 0000000..d285f2d Binary files /dev/null and b/icons/x86-osx/mac.png differ diff --git a/icons/x86-win32/win.ico b/icons/x86-win32/win.ico new file mode 100644 index 0000000..73a3363 Binary files /dev/null and b/icons/x86-win32/win.ico differ diff --git a/icons/x86_64-osx/mac.png b/icons/x86_64-osx/mac.png new file mode 100644 index 0000000..d285f2d Binary files /dev/null and b/icons/x86_64-osx/mac.png differ diff --git a/icons/x86_64-win32/win.ico b/icons/x86_64-win32/win.ico new file mode 100644 index 0000000..73a3363 Binary files /dev/null and b/icons/x86_64-win32/win.ico differ