Skip to content

Commit aafe973

Browse files
committed
add view windows to column and grid menus
1 parent 037ffe4 commit aafe973

File tree

10 files changed

+91
-14
lines changed

10 files changed

+91
-14
lines changed

current-scripts/Demos/useful-scripts/objects/obj_column_menu/Create_0.gml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ event_inherited();
33
items = ds_list_create();
44
num_items = 0;
55
pos = 0;
6+
view_area = new Vector2(0, 0);
67

78
column_menu_functions();

current-scripts/Demos/useful-scripts/objects/obj_column_menu/Draw_0.gml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
event_inherited();
22
var _x = x + cursor_padding;
33

4-
for (var i=0; i<num_items; i++) {
4+
for (var i=view_area.x; i<=view_area.y; i++) {
55
var _item = items[| i];
66
var _type = _item.types[| ds_list_size(_item.types)-1];
7-
8-
var _y = y + (item_height + line_spacing) * i;
9-
7+
8+
var _y = y + (item_height + line_spacing) * (i - view_area.x);
9+
1010
switch (_type) {
1111
case "item":
1212
case "selectable":
@@ -27,5 +27,5 @@ for (var i=0; i<num_items; i++) {
2727
}
2828

2929
if (enabled) {
30-
draw_sprite(cursor_spr, 0, x, y + (item_height + line_spacing) * pos + item_height / 2);
30+
draw_sprite(cursor_spr, 0, x, y + (item_height + line_spacing) * (pos - view_area.x) + item_height / 2);
3131
}

current-scripts/Demos/useful-scripts/objects/obj_column_menu/Step_1.gml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ control_state.poll_input();
1010

1111
if (control_state.pressed_state[MENU_CONTROLS.UP]) {
1212
pos = wrap(pos-1, 0, num_items);
13+
self.column_menu_update_view();
1314
audio_play_sound(cursor_move_sfx, 1, false);
1415
}
1516

1617
if (control_state.pressed_state[MENU_CONTROLS.DOWN]) {
1718
pos = wrap(pos+1, 0, num_items);
19+
self.column_menu_update_view();
1820
audio_play_sound(cursor_move_sfx, 1, false);
1921
}
2022

current-scripts/Demos/useful-scripts/objects/obj_column_menu/obj_column_menu.yy

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
event_inherited();
22

33
items = ds_grid_create(1, 1);
4-
pos = { x: 0, y: 0 };
4+
pos = new Vector2(0, 0);
5+
view_area = new Rectangle(0, 0, 0, 0);
56

67
grid_menu_functions();

current-scripts/Demos/useful-scripts/objects/obj_grid_menu/Draw_0.gml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
event_inherited();
22

3-
for (var i=0; i<ds_grid_width(items); i++) {
4-
var _x = x + cursor_padding * (i+1) + column_width * i;
3+
for (var i=view_area.left; i<=view_area.right; i++) {
4+
var _x = x + cursor_padding * (i-view_area.left+1) + column_width * i;
55

6-
for (var j=0; j<ds_grid_height(items); j++) {
6+
for (var j=view_area.top; j<=view_area.bottom; j++) {
77
var _item = items[# i, j];
88
if (!is_struct(_item)) continue;
9-
9+
1010
var _type = _item.types[| ds_list_size(_item.types)-1];
11-
var _y = y + (item_height + line_spacing) * j;
11+
var _y = y + (item_height + line_spacing) * (j-view_area.top);
1212

1313
switch (_type) {
1414
case "item":
@@ -32,7 +32,7 @@ for (var i=0; i<ds_grid_width(items); i++) {
3232

3333
if (enabled) {
3434
draw_sprite(cursor_spr, 0,
35-
x + (cursor_padding + column_width) * pos.x,
36-
y + (item_height + line_spacing) * pos.y + item_height / 2
35+
x + (cursor_padding + column_width) * (pos.x - view_area.left),
36+
y + (item_height + line_spacing) * (pos.y - view_area.top) + item_height / 2
3737
);
3838
}

current-scripts/Demos/useful-scripts/objects/obj_grid_menu/Step_1.gml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ if (control_state.pressed_state[MENU_CONTROLS.UP]) {
1818
_item = items[# pos.x, pos.y];
1919
} until (is_struct(_item) || _cur_pos == pos.y)
2020

21+
self.grid_menu_update_view();
22+
2123
audio_play_sound(cursor_move_sfx, 1, false);
2224
}
2325

@@ -29,6 +31,8 @@ if (control_state.pressed_state[MENU_CONTROLS.DOWN]) {
2931
pos.y = wrap(pos.y+1, 0, ds_grid_height(items));
3032
_item = items[# pos.x, pos.y];
3133
} until (is_struct(_item) || _cur_pos == pos.y)
34+
35+
self.grid_menu_update_view();
3236

3337
audio_play_sound(cursor_move_sfx, 1, false);
3438
}
@@ -48,6 +52,8 @@ if (control_state.pressed_state[MENU_CONTROLS.LEFT]) {
4852
pos.x = wrap(pos.x-1, 0, ds_grid_width(items));
4953
_item = items[# pos.x, pos.y];
5054
} until (is_struct(_item) || _cur_pos == pos.x)
55+
56+
self.grid_menu_update_view();
5157

5258
audio_play_sound(cursor_move_sfx, 1, false);
5359
}

current-scripts/Demos/useful-scripts/objects/obj_grid_menu/obj_grid_menu.yy

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

current-scripts/Demos/useful-scripts/scripts/column_menu_functions/column_menu_functions.gml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/// @func column_menu_init(config)
22
/// @param config
3+
// - {real} view_height
34
// - {font} font
45
// - {sprite} cursor_spr
56
// - {sound} cursor_move_sfx
@@ -8,6 +9,7 @@
89
function column_menu_init(_config) {
910
self.menu_base_init(_config.font, _config.cursor_spr);
1011

12+
view_height = _config.view_height;
1113
menu_font = _config.font;
1214
cursor_spr = _config.cursor_spr;
1315
cursor_padding = sprite_get_width(cursor_spr) + 16;
@@ -16,6 +18,26 @@ function column_menu_init(_config) {
1618
cursor_confirm_sfx = _config.cursor_confirm_sfx;
1719
}
1820

21+
/// @func column_menu_update_view()
22+
function column_menu_update_view() {
23+
if (view_height > 0) {
24+
if (pos < view_area.x) {
25+
view_area.x = pos;
26+
view_area.y = pos + view_height - 1;
27+
} else if (pos > view_area.y) {
28+
view_area.y = pos;
29+
view_area.x = pos - (view_height - 1);
30+
}
31+
}
32+
}
33+
34+
/// @func column_menu_update_view_area()
35+
function column_menu_update_view_area() {
36+
view_area.y = view_height < 1
37+
? num_items - 1
38+
: min(num_items, pos + view_height - 1);
39+
}
40+
1941
/// @func column_menu_get_item_by_index(menu, index)
2042
/// @param {obj_column_menu} menu
2143
/// @param {number} index
@@ -45,6 +67,7 @@ function column_menu_add_selectable(_config) {
4567
ds_list_add(items, _new);
4668
num_items++;
4769
_new.parent_menu = self;
70+
column_menu_update_view_area();
4871
return _new;
4972
}
5073

@@ -64,6 +87,7 @@ function column_menu_add_spinner(_config) {
6487
ds_list_add(items, _new);
6588
num_items++;
6689
_new.parent_menu = self;
90+
column_menu_update_view_height();
6791
return _new;
6892
}
6993

@@ -79,5 +103,6 @@ function column_menu_add_key_config(_config) {
79103
ds_list_add(items, _new);
80104
num_items++;
81105
_new.parent_menu = self;
106+
column_menu_update_view_height();
82107
return _new;
83108
}

current-scripts/Demos/useful-scripts/scripts/grid_menu_functions/grid_menu_functions.gml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
/// @param config
33
// - {real} width
44
// - {real} height
5+
// - {real} view_width
6+
// - {real} view_height
57
// - {number} column_width
68
// - {font} font
79
// - {sprite} cursor_spr
@@ -12,6 +14,8 @@ function grid_menu_init(_config) {
1214
self.menu_base_init(_config.font, _config.cursor_spr);
1315

1416
column_width = _config.column_width;
17+
view_width = _config.view_width;
18+
view_height = _config.view_height;
1519
menu_font = _config.font;
1620
cursor_spr = _config.cursor_spr;
1721
cursor_padding = sprite_get_width(cursor_spr) + 16;
@@ -20,6 +24,40 @@ function grid_menu_init(_config) {
2024
cursor_confirm_sfx = _config.cursor_confirm_sfx;
2125

2226
ds_grid_resize(items, _config.width, _config.height);
27+
28+
view_area.left = 0;
29+
view_area.right = view_width < 1
30+
? _config.width - 1
31+
: _config.view_width - 1;
32+
33+
34+
view_area.top = 0;
35+
view_area.bottom = view_height < 1
36+
? _config.height - 1
37+
: _config.view_height - 1;
38+
}
39+
40+
/// @func grid_menu_update_view()
41+
function grid_menu_update_view() {
42+
if (view_height > 0) {
43+
if (pos.y < view_area.top) {
44+
view_area.top = pos.y;
45+
view_area.bottom = pos.y + view_height - 1;
46+
} else if (pos.y > view_area.bottom) {
47+
view_area.bottom = pos.y;
48+
view_area.top = pos.y - (view_height - 1);
49+
}
50+
}
51+
52+
if (view_width > 0) {
53+
if (pos.x < view_area.left) {
54+
view_area.left = pos.x;
55+
view_area.right = pos.x + view_height - 1;
56+
} else if (pos.x > view_area.right) {
57+
view_area.right = pos.x;
58+
view_area.left = pos.x - (view_height - 1);
59+
}
60+
}
2361
}
2462

2563
/// @func grid_menu_get_item_by_index(menu, x, y)

0 commit comments

Comments
 (0)