Skip to content

Commit

Permalink
added new method set_window_icon for macos, windows, html5 platform
Browse files Browse the repository at this point in the history
  • Loading branch information
AGulev committed Feb 28, 2018
1 parent d7865a6 commit 89e9b9a
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ builtins
.internal
.clang_complete
.vscode/
*.txt
8 changes: 8 additions & 0 deletions defos/src/defos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions defos/src/defos_html5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions defos/src/defos_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion defos/src/defos_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
10 changes: 10 additions & 0 deletions defos/src/defos_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
160 changes: 160 additions & 0 deletions example/example.gui
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 11 additions & 0 deletions example/example.gui_script
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions game.project
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Binary file added icons/js-web/html5.ico
Binary file not shown.
Binary file added icons/x86-osx/mac.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/x86-win32/win.ico
Binary file not shown.
Binary file added icons/x86_64-osx/mac.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added icons/x86_64-win32/win.ico
Binary file not shown.

0 comments on commit 89e9b9a

Please sign in to comment.