SDL

Форк
0
/
testhittesting.c 
168 строк · 5.1 Кб
1
/*
2
  Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
3

4
  This software is provided 'as-is', without any express or implied
5
  warranty.  In no event will the authors be held liable for any damages
6
  arising from the use of this software.
7

8
  Permission is granted to anyone to use this software for any purpose,
9
  including commercial applications, and to alter it and redistribute it
10
  freely.
11
*/
12
#include <SDL3/SDL.h>
13
#include <SDL3/SDL_main.h>
14
#include <SDL3/SDL_test.h>
15

16
#define RESIZE_BORDER 20
17

18
static const SDL_Rect drag_areas[] = {
19
    { 20, 20, 100, 100 },
20
    { 200, 70, 100, 100 },
21
    { 400, 90, 100, 100 }
22
};
23
static const SDL_FRect render_areas[] = {
24
    { 20.0f, 20.0f, 100.0f, 100.0f },
25
    { 200.0f, 70.0f, 100.0f, 100.0f },
26
    { 400.0f, 90.0f, 100.0f, 100.0f }
27
};
28

29
static const SDL_Rect *areas = drag_areas;
30
static int numareas = SDL_arraysize(drag_areas);
31

32
static SDL_HitTestResult SDLCALL
33
hitTest(SDL_Window *window, const SDL_Point *pt, void *data)
34
{
35
    int i;
36
    int w, h;
37

38
    for (i = 0; i < numareas; i++) {
39
        if (SDL_PointInRect(pt, &areas[i])) {
40
            SDL_Log("HIT-TEST: DRAGGABLE\n");
41
            return SDL_HITTEST_DRAGGABLE;
42
        }
43
    }
44

45
    SDL_GetWindowSize(window, &w, &h);
46

47
#define REPORT_RESIZE_HIT(name)                  \
48
    {                                            \
49
        SDL_Log("HIT-TEST: RESIZE_" #name "\n"); \
50
        return SDL_HITTEST_RESIZE_##name;        \
51
    }
52

53
    if (pt->x < RESIZE_BORDER && pt->y < RESIZE_BORDER) {
54
        REPORT_RESIZE_HIT(TOPLEFT);
55
    } else if (pt->x > RESIZE_BORDER && pt->x < w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
56
        REPORT_RESIZE_HIT(TOP);
57
    } else if (pt->x > w - RESIZE_BORDER && pt->y < RESIZE_BORDER) {
58
        REPORT_RESIZE_HIT(TOPRIGHT);
59
    } else if (pt->x > w - RESIZE_BORDER && pt->y > RESIZE_BORDER && pt->y < h - RESIZE_BORDER) {
60
        REPORT_RESIZE_HIT(RIGHT);
61
    } else if (pt->x > w - RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
62
        REPORT_RESIZE_HIT(BOTTOMRIGHT);
63
    } else if (pt->x < w - RESIZE_BORDER && pt->x > RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
64
        REPORT_RESIZE_HIT(BOTTOM);
65
    } else if (pt->x < RESIZE_BORDER && pt->y > h - RESIZE_BORDER) {
66
        REPORT_RESIZE_HIT(BOTTOMLEFT);
67
    } else if (pt->x < RESIZE_BORDER && pt->y < h - RESIZE_BORDER && pt->y > RESIZE_BORDER) {
68
        REPORT_RESIZE_HIT(LEFT);
69
    }
70

71
    SDL_Log("HIT-TEST: NORMAL\n");
72
    return SDL_HITTEST_NORMAL;
73
}
74

75
int main(int argc, char **argv)
76
{
77
    int i;
78
    int done = 0;
79
    SDLTest_CommonState *state;
80

81
    /* Initialize test framework */
82
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
83
    if (!state) {
84
        return 1;
85
    }
86

87
    state->window_flags = SDL_WINDOW_BORDERLESS | SDL_WINDOW_RESIZABLE;
88

89
    /* Enable standard application logging */
90
    SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
91

92
    /* Parse commandline */
93
    if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
94
        return 1;
95
    }
96

97
    if (!SDLTest_CommonInit(state)) {
98
        return 2;
99
    }
100

101
    for (i = 0; i < state->num_windows; i++) {
102
        if (SDL_SetWindowHitTest(state->windows[i], hitTest, NULL) < 0) {
103
            SDL_Log("Enabling hit-testing failed for window %d: %s", i, SDL_GetError());
104
            SDL_Quit();
105
            return 1;
106
        }
107
    }
108

109
    while (!done) {
110
        SDL_Event e;
111
        int nothing_to_do = 1;
112

113
        for (i = 0; i < state->num_windows; ++i) {
114
            SDL_SetRenderDrawColor(state->renderers[i], 0, 0, 127, 255);
115
            SDL_RenderClear(state->renderers[i]);
116
            SDL_SetRenderDrawColor(state->renderers[i], 255, 0, 0, 255);
117
            SDLTest_DrawString(state->renderers[i], (float)state->window_w / 2 - 80.0f, 10.0f, "Drag the red boxes");
118
            SDL_RenderFillRects(state->renderers[i], render_areas, SDL_arraysize(render_areas));
119
            SDL_RenderPresent(state->renderers[i]);
120
        }
121

122
        while (SDL_PollEvent(&e)) {
123
            SDLTest_CommonEvent(state, &e, &done);
124
            nothing_to_do = 0;
125

126
            switch (e.type) {
127
            case SDL_EVENT_MOUSE_BUTTON_DOWN:
128
                SDL_Log("button down!\n");
129
                break;
130

131
            case SDL_EVENT_MOUSE_BUTTON_UP:
132
                SDL_Log("button up!\n");
133
                break;
134

135
            case SDL_EVENT_WINDOW_MOVED:
136
                SDL_Log("Window event moved to (%d, %d)!\n", (int)e.window.data1, (int)e.window.data2);
137
                break;
138

139
            case SDL_EVENT_KEY_DOWN:
140
                if (e.key.key == SDLK_ESCAPE) {
141
                    done = 1;
142
                } else if (e.key.key == SDLK_X) {
143
                    if (!areas) {
144
                        areas = drag_areas;
145
                        numareas = SDL_arraysize(drag_areas);
146
                    } else {
147
                        areas = NULL;
148
                        numareas = 0;
149
                    }
150
                }
151
                break;
152

153
            case SDL_EVENT_QUIT:
154
                done = 1;
155
                break;
156
            default:
157
                break;
158
            }
159
        }
160

161
        if (nothing_to_do) {
162
            SDL_Delay(50);
163
        }
164
    }
165

166
    SDLTest_CommonQuit(state);
167
    return 0;
168
}
169

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.