src/video/cocoa/SDL_cocoashape.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:33:28 -0800
changeset 10737 3406a0f8b041
parent 10176 3115d38204aa
child 11144 cccd37a9c28b
permissions -rw-r--r--
Updated copyright for 2017
eligottlieb@4809
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
eligottlieb@4809
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
eligottlieb@4809
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
eligottlieb@4809
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
eligottlieb@4809
    20
*/
eligottlieb@4809
    21
icculus@8093
    22
#include "../../SDL_internal.h"
slouken@6044
    23
slouken@6044
    24
#if SDL_VIDEO_DRIVER_COCOA
slouken@6044
    25
eligottlieb@4811
    26
#include "SDL_cocoavideo.h"
eligottlieb@4809
    27
#include "SDL_shape.h"
eligottlieb@4810
    28
#include "SDL_cocoashape.h"
aschiffler@4907
    29
#include "../SDL_sysvideo.h"
icculus@6281
    30
#include "SDL_assert.h"
eligottlieb@4809
    31
eligottlieb@4838
    32
SDL_WindowShaper*
slouken@8986
    33
Cocoa_CreateShaper(SDL_Window* window)
slouken@8986
    34
{
egottlieb@4859
    35
    SDL_WindowData* windata = (SDL_WindowData*)window->driverdata;
egottlieb@4859
    36
    [windata->nswindow setOpaque:NO];
jorgen@7507
    37
slime73@10176
    38
    [windata->nswindow setStyleMask:NSBorderlessWindowMask];
jorgen@7507
    39
egottlieb@4859
    40
    SDL_WindowShaper* result = result = malloc(sizeof(SDL_WindowShaper));
eligottlieb@4838
    41
    result->window = window;
eligottlieb@4838
    42
    result->mode.mode = ShapeModeDefault;
eligottlieb@4838
    43
    result->mode.parameters.binarizationCutoff = 1;
egottlieb@4859
    44
    result->userx = result->usery = 0;
eligottlieb@4838
    45
    window->shaper = result;
slouken@7191
    46
eligottlieb@4838
    47
    SDL_ShapeData* data = malloc(sizeof(SDL_ShapeData));
eligottlieb@4838
    48
    result->driverdata = data;
egottlieb@4859
    49
    data->context = [windata->nswindow graphicsContext];
egottlieb@4859
    50
    data->saved = SDL_FALSE;
egottlieb@4859
    51
    data->shape = NULL;
slouken@7191
    52
eligottlieb@4838
    53
    int resized_properly = Cocoa_ResizeWindowShape(window);
icculus@6281
    54
    SDL_assert(resized_properly == 0);
eligottlieb@4838
    55
    return result;
eligottlieb@4810
    56
}
eligottlieb@4810
    57
egottlieb@4859
    58
typedef struct {
egottlieb@4859
    59
    NSView* view;
egottlieb@4859
    60
    NSBezierPath* path;
egottlieb@4860
    61
    SDL_Window* window;
egottlieb@4859
    62
} SDL_CocoaClosure;
egottlieb@4859
    63
egottlieb@4859
    64
void
slouken@8986
    65
ConvertRects(SDL_ShapeTree* tree, void* closure)
slouken@8986
    66
{
egottlieb@4859
    67
    SDL_CocoaClosure* data = (SDL_CocoaClosure*)closure;
egottlieb@4859
    68
    if(tree->kind == OpaqueShape) {
egottlieb@4860
    69
        NSRect rect = NSMakeRect(tree->data.shape.x,data->window->h - tree->data.shape.y,tree->data.shape.w,tree->data.shape.h);
egottlieb@4859
    70
        [data->path appendBezierPathWithRect:[data->view convertRect:rect toView:nil]];
egottlieb@4859
    71
    }
egottlieb@4859
    72
}
egottlieb@4859
    73
eligottlieb@4838
    74
int
slouken@8986
    75
Cocoa_SetWindowShape(SDL_WindowShaper *shaper, SDL_Surface *shape, SDL_WindowShapeMode *shape_mode)
slime73@9587
    76
{ @autoreleasepool
slouken@8986
    77
{
egottlieb@4859
    78
    SDL_ShapeData* data = (SDL_ShapeData*)shaper->driverdata;
slouken@7191
    79
    SDL_WindowData* windata = (SDL_WindowData*)shaper->window->driverdata;
slouken@7191
    80
    SDL_CocoaClosure closure;
egottlieb@4859
    81
    if(data->saved == SDL_TRUE) {
eligottlieb@4838
    82
        [data->context restoreGraphicsState];
egottlieb@4859
    83
        data->saved = SDL_FALSE;
eligottlieb@4838
    84
    }
slouken@7191
    85
slouken@7771
    86
    /*[data->context saveGraphicsState];*/
slouken@7771
    87
    /*data->saved = SDL_TRUE;*/
slouken@7191
    88
    [NSGraphicsContext setCurrentContext:data->context];
slouken@7191
    89
eligottlieb@4838
    90
    [[NSColor clearColor] set];
egottlieb@4859
    91
    NSRectFill([[windata->nswindow contentView] frame]);
egottlieb@4859
    92
    data->shape = SDL_CalculateShapeTree(*shape_mode,shape);
slouken@7191
    93
slouken@6848
    94
    closure.view = [windata->nswindow contentView];
icculus@8647
    95
    closure.path = [NSBezierPath bezierPath];
slouken@7191
    96
    closure.window = shaper->window;
slouken@6848
    97
    SDL_TraverseShapeTree(data->shape,&ConvertRects,&closure);
slouken@6848
    98
    [closure.path addClip];
slouken@6044
    99
slouken@6044
   100
    return 0;
slime73@9587
   101
}}
egottlieb@4829
   102
eligottlieb@4838
   103
int
slouken@8986
   104
Cocoa_ResizeWindowShape(SDL_Window *window)
slouken@8986
   105
{
eligottlieb@4838
   106
    SDL_ShapeData* data = window->shaper->driverdata;
icculus@6281
   107
    SDL_assert(data != NULL);
eligottlieb@4838
   108
    return 0;
egottlieb@4829
   109
}
slouken@6044
   110
slouken@6044
   111
#endif /* SDL_VIDEO_DRIVER_COCOA */
slouken@6044
   112
slouken@6044
   113
/* vi: set ts=4 sw=4 expandtab: */