Expose API to control surfaces z-order #73

Merged
MartinFouilleul merged 3 commits from surface_order into main 2023-08-28 15:47:20 +00:00
6 changed files with 173 additions and 78 deletions
Showing only changes of commit b5034d3c81 - Show all commits

View File

@ -65,6 +65,22 @@ int main()
} }
break; break;
case OC_EVENT_KEYBOARD_KEY:
{
if(event->key.action == OC_KEY_PRESS)
{
if(event->key.code == OC_KEY_UP)
{
oc_surface_bring_to_front(surface2);
}
else if(event->key.code == OC_KEY_DOWN)
{
oc_surface_send_to_back(surface2);
}
}
}
break;
default: default:
break; break;
} }
@ -89,7 +105,7 @@ int main()
oc_clear(); oc_clear();
oc_set_color_rgba(0, 0, 1, 1); oc_set_color_rgba(0, 0, 1, 1);
oc_rectangle_fill(300, 300, 300, 200); oc_rectangle_fill(200, 200, 300, 200);
oc_render(surface2, canvas2); oc_render(surface2, canvas2);
//*/ //*/

View File

@ -32,6 +32,8 @@ typedef struct oc_osx_window_data
NSView* nsView; NSView* nsView;
NSObject* nsWindowDelegate; NSObject* nsWindowDelegate;
oc_list layers;
} oc_osx_window_data; } oc_osx_window_data;
#define OC_PLATFORM_WINDOW_DATA oc_osx_window_data osx; #define OC_PLATFORM_WINDOW_DATA oc_osx_window_data osx;
@ -78,6 +80,9 @@ typedef uint32_t CAContextID;
typedef struct oc_layer typedef struct oc_layer
{ {
oc_window window;
oc_list_elt listElt;
CALayer* caLayer; CALayer* caLayer;
CAContext* caContext; CAContext* caContext;
} oc_layer; } oc_layer;

View File

@ -1311,10 +1311,10 @@ oc_window oc_window_create(oc_rect contentRect, oc_str8 title, oc_window_style s
oc_log_error("Could not allocate window data\n"); oc_log_error("Could not allocate window data\n");
return ((oc_window){ 0 }); return ((oc_window){ 0 });
} }
window->style = style; window->style = style;
window->shouldClose = false; window->shouldClose = false;
window->hidden = true; window->hidden = true;
window->osx.layers = (oc_list){ 0 };
u32 styleMask = oc_osx_get_window_style_mask(style); u32 styleMask = oc_osx_get_window_style_mask(style);
@ -1735,10 +1735,51 @@ void oc_osx_surface_set_hidden(oc_surface_data* surface, bool hidden)
} }
} }
void oc_osx_update_layers(oc_window_data* window)
{
@autoreleasepool
{
int z = 0;
oc_list_for(&window->osx.layers, layer, oc_layer, listElt)
{
layer->caLayer.zPosition = (CGFloat)z;
z++;
}
}
}
void oc_osx_surface_bring_to_front(oc_surface_data* surface)
{
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_list_push_back(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
}
void oc_osx_surface_send_to_back(oc_surface_data* surface)
{
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_list_push(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
}
void oc_surface_cleanup(oc_surface_data* surface) void oc_surface_cleanup(oc_surface_data* surface)
{ {
@autoreleasepool @autoreleasepool
{ {
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
[surface->layer.caLayer release]; [surface->layer.caLayer release];
} }
} }
@ -1747,12 +1788,17 @@ void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window
{ {
@autoreleasepool @autoreleasepool
{ {
surface->layer.window = oc_window_handle_from_ptr(window);
surface->nativeLayer = oc_osx_surface_native_layer; surface->nativeLayer = oc_osx_surface_native_layer;
surface->contentsScaling = oc_osx_surface_contents_scaling; surface->contentsScaling = oc_osx_surface_contents_scaling;
surface->getSize = oc_osx_surface_get_size; surface->getSize = oc_osx_surface_get_size;
surface->getHidden = oc_osx_surface_get_hidden; surface->getHidden = oc_osx_surface_get_hidden;
surface->setHidden = oc_osx_surface_set_hidden; surface->setHidden = oc_osx_surface_set_hidden;
surface->bringToFront = oc_osx_surface_bring_to_front;
surface->sendToBack = oc_osx_surface_send_to_back;
surface->layer.caLayer = [[CALayer alloc] init]; surface->layer.caLayer = [[CALayer alloc] init];
[surface->layer.caLayer retain]; [surface->layer.caLayer retain];
@ -1760,10 +1806,12 @@ void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window
CGSize size = frame.size; CGSize size = frame.size;
surface->layer.caLayer.frame = (CGRect){ { 0, 0 }, size }; surface->layer.caLayer.frame = (CGRect){ { 0, 0 }, size };
surface->layer.caLayer.contentsScale = window->osx.nsView.layer.contentsScale; surface->layer.caLayer.contentsScale = window->osx.nsView.layer.contentsScale;
surface->layer.caLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable; surface->layer.caLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
[window->osx.nsView.layer addSublayer:surface->layer.caLayer]; [window->osx.nsView.layer addSublayer:surface->layer.caLayer];
oc_list_push_back(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
} }
} }

View File

@ -115,6 +115,9 @@ ORCA_API oc_vec2 oc_surface_contents_scaling(oc_surface surface);
ORCA_API bool oc_surface_get_hidden(oc_surface surface); ORCA_API bool oc_surface_get_hidden(oc_surface surface);
ORCA_API void oc_surface_set_hidden(oc_surface surface, bool hidden); ORCA_API void oc_surface_set_hidden(oc_surface surface, bool hidden);
ORCA_API void oc_surface_bring_to_front(oc_surface surface);
ORCA_API void oc_surface_send_to_back(oc_surface surface);
//NOTE(martin): surface sharing //NOTE(martin): surface sharing
typedef u64 oc_surface_id; typedef u64 oc_surface_id;

View File

@ -372,6 +372,24 @@ void oc_surface_render_commands(oc_surface surface,
} }
} }
void oc_surface_bring_to_front(oc_surface handle)
{
oc_surface_data* surface = oc_surface_data_from_handle(handle);
if(surface && surface->bringToFront)
{
surface->bringToFront(surface);
}
}
void oc_surface_send_to_back(oc_surface handle)
{
oc_surface_data* surface = oc_surface_data_from_handle(handle);
if(surface && surface->sendToBack)
{
surface->sendToBack(surface);
}
}
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): images //NOTE(martin): images
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------

View File

@ -12,8 +12,7 @@
#include "graphics_common.h" #include "graphics_common.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C" {
{
#endif #endif
//--------------------------------------------------------------- //---------------------------------------------------------------
@ -35,6 +34,9 @@ extern "C"
typedef oc_surface_id (*oc_surface_remote_id_proc)(oc_surface_data* surface); typedef oc_surface_id (*oc_surface_remote_id_proc)(oc_surface_data* surface);
typedef void (*oc_surface_host_connect_proc)(oc_surface_data* surface, oc_surface_id remoteId); typedef void (*oc_surface_host_connect_proc)(oc_surface_data* surface, oc_surface_id remoteId);
typedef void (*oc_surface_bring_to_front_proc)(oc_surface_data* surface);
typedef void (*oc_surface_send_to_back_proc)(oc_surface_data* surface);
typedef struct oc_surface_data typedef struct oc_surface_data
{ {
oc_surface_api api; oc_surface_api api;
@ -53,6 +55,9 @@ extern "C"
oc_surface_remote_id_proc remoteID; oc_surface_remote_id_proc remoteID;
oc_surface_host_connect_proc hostConnect; oc_surface_host_connect_proc hostConnect;
oc_surface_bring_to_front_proc bringToFront;
oc_surface_send_to_back_proc sendToBack;
oc_canvas_backend* backend; oc_canvas_backend* backend;
} oc_surface_data; } oc_surface_data;