-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubwindow.cpp
More file actions
133 lines (110 loc) · 4.02 KB
/
subwindow.cpp
File metadata and controls
133 lines (110 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "subwindow.h"
#include "draw.h" // For render_target_height, set_shader(), ...
#include "events.h" // For KSTATE_*.
Subwindow_State *begin_subwindow(Rect r, Subwindow_Theme *theme, i64 identifier, Source_Location loc)
{
auto hash = ui_get_hash(loc, identifier);
auto state = find_or_create_state(&table_subwindow, hash);
defer { stop_using_state(&state->widget); };
if (!theme) theme = &default_overall_theme.subwindow_theme;
state->theme = *theme; // Make a copy for the state because who knows, you maybe changing stuff around after calling begin_subwindow.
state->hash = hash; // What for ??? nocheckin
state->full_rect = r;
if (state->num_begins == 0)
{
state->num_begins += 1;
}
else if (state->num_begins == 1)
{
logprint("subwindow", "begin_subwindow was called too many times on this state, without calling end_subwindow first!\n");
}
else
{
logprint("subwindow", "the 'state' argument passed into begin_subwindow is corrupted!\n");
return state;
}
if (state->dragging)
{
if (ui_active_widget != &state->widget)
{
state->dragging = Subwindow_State::NOTHING;
}
if (!(mouse_button_left_state & KSTATE_DOWN))
{
state->dragging = Subwindow_State::NOTHING;
}
if (!state->dragging)
{
active_widget_remove(&state->widget);
}
}
if (state->dragging)
{
auto dx = mouse_x_float - state->dragging_last_x;
auto dy = mouse_y_float - state->dragging_last_y;
if (dx || dy)
{
if (state->dragging == Subwindow_State::TITLE)
{
state->full_rect.x += dx;
state->full_rect.y += dy;
}
else if (state->dragging == Subwindow_State::TO_RESIZE)
{
assert(0); // @Incomplete:
}
state->dragging_last_x = mouse_x_float;
state->dragging_last_y = mouse_y_float;
}
}
auto title_bar_height = floorf(.5f + theme->title_bar_height * render_target_height);
auto [title_bar_rect, content_area] = cut_top(state->full_rect, title_bar_height);
state->title_bar_rect = title_bar_rect;
state->content_area = content_area;
// Draw the background quad.
set_shader(shader_argb_no_texture);
rounded_rectangle(content_area, theme->content_area_shape, theme->content_area_background_color);
push_scissor(content_area);
return state;
}
Rect end_subwindow(Subwindow_State *state, String title_bar_string)
{
if (state->num_begins == 0)
{
logprint("subwindow", "end_subwindow was called too many times on this state, or begin was never called.\n");
return {};
}
else if (state->num_begins == 1)
{
state->num_begins -= 1;
}
else
{
logprint("subwindow", "the 'state' argument passed into end_subwindow is corrupted!!!!!\n");
return {};
}
auto theme = &state->theme;
pop_scissor();
if (state->dragging)
{
if (ui_active_widget != &state->widget) state->dragging = Subwindow_State::NOTHING;
if (!state->dragging) active_widget_remove(&state->widget);
}
auto draw_title_bar = true; // @Incomplete: @Theme: Control whether title bar draws with a style flag.
if (draw_title_bar)
{
auto sub_hash = ui_get_hash(Source_Location::current(), 0);
auto title_bar_hash = combine_hashes(state->hash, sub_hash);
auto [pressed, title_bar_state] = button(state->title_bar_rect, title_bar_string, &theme->title_bar_theme, NULL, title_bar_hash);
if (pressed)
{
state->dragging = Subwindow_State::TITLE;
state->dragging_last_x = mouse_x_float;
state->dragging_last_y = mouse_y_float;
// Since it is a click, make the subwindow active.
active_widget_add(&state->widget);
}
}
// occlusion_declare(state->full_rect, state);
return state->full_rect;
}