Skip to content

Commit af52d4e

Browse files
author
s.kushnirenko
committed
js: fix: refactor r_vec2i function to handle different types of input
1 parent c43f673 commit af52d4e

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/js/js_game.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,22 @@ vec2i archive::r_size2i(pcstr name, pcstr w, pcstr h) {
153153

154154
vec2i archive::r_vec2i(pcstr name, pcstr x, pcstr y) {
155155
vec2i result(0, 0);
156-
r_section(name, [&] (archive arch) {
157-
result.x = arch.r_int(x);
158-
result.y = arch.r_int(y);
159-
});
156+
js_getproperty(vm, -1, name);
157+
if (js_isobject(vm, -1)) {
158+
if (js_isarray(vm, -1)) {
159+
int length = js_getlength(vm, -1);
160+
if (length > 0) {
161+
js_getindex(vm, -1, 0); result.x = !js_isundefined(vm, -1) ? js_tointeger(vm, -1) : 0; js_pop(vm, 1);
162+
if (length > 1) {
163+
js_getindex(vm, -1, 1); result.y = !js_isundefined(vm, -1) ? js_tointeger(vm, -1) : 0; js_pop(vm, 1);
164+
}
165+
}
166+
} else {
167+
js_getproperty(vm, -1, x); result.x = !js_isundefined(vm, -1) ? js_tointeger(vm, -1) : 0; js_pop(vm, 1);
168+
js_getproperty(vm, -1, y); result.y = !js_isundefined(vm, -1) ? js_tointeger(vm, -1) : 0; js_pop(vm, 1);
169+
}
170+
}
171+
js_pop(vm, 1);
172+
160173
return result;
161174
}

src/js/js_game.h

+5-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ struct archive {
3232

3333
for (int i = 0; i < length; ++i) {
3434
js_getindex(vm, -1, i);
35-
int v = js_tointeger(vm, -1);
35+
float v = js_isnumber(vm, -1) ? (float)js_tonumber(vm, -1) : 0.f;
3636
result.push_back((T)v);
3737
js_pop(vm, 1);
3838
}
39-
js_pop(vm, 1);
4039
}
40+
js_pop(vm, 1);
4141
return result;
4242
}
4343

@@ -103,18 +103,17 @@ struct g_archive : public archive {
103103
template<typename T>
104104
inline void r_array(pcstr name, T read_func) {
105105
js_getglobal(vm, name);
106-
if (r_array_impl(read_func)) {
107-
js_pop(vm, 1);
108-
}
106+
r_array_impl(read_func);
107+
js_pop(vm, 1);
109108
}
110109

111110
template<typename T>
112111
inline void r_section(pcstr name, T read_func) {
113112
js_getglobal(vm, name);
114113
if (js_isobject(vm, -1)) {
115114
read_func(vm);
116-
js_pop(vm, 1);
117115
}
116+
js_pop(vm, 1);
118117
}
119118
};
120119
extern g_archive g_config_arch;

src/scripts/ui.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ top_menu_bar = {
1313
}
1414

1515
main_menu_window = {
16-
button_pos : {x: 192, y: 125},
16+
button_pos : [192, 125],
1717
button_size : {w: 256, h: 25},
1818
button_offset : 40,
1919

@@ -28,7 +28,7 @@ main_menu_window = {
2828

2929
advisor_rating_window = {
3030
ui : {
31-
outer_panel : { type : "outer_panel", pos : {x:0, y:0}, size : {w:40, h:27} },
31+
outer_panel : { type : "outer_panel", pos: [0, 0], size : {w:40, h:27} },
3232
advisor_icon : { type : "image", image : IMG_ADVISOR_RATING_ICON, pos : {x: 10, y: 10} },
3333
header_label : { type : "label", font : FONT_LARGE_BLACK_ON_LIGHT, text : {group: 53, id: 0}, pos : {x: 60, y:17} },
3434
population_label : { type : "label", text : "", pos : {x: 300, y:20} },
@@ -39,11 +39,11 @@ advisor_rating_window = {
3939

4040
mission_briefing_window = {
4141
ui : {
42-
outer_panel : { type : "outer_panel", pos: {x:16, y:32}, size : {w:38, h:27} },
42+
outer_panel : { type : "outer_panel", pos: [16, 32], size : {w:38, h:27} },
4343
title : { type : "text", pos : {x:32, y:48}, font : FONT_LARGE_BLACK_ON_LIGHT },
4444
subtitle : { type : "text", pos : {x: 32, y:78}, font : FONT_NORMAL_BLACK_ON_LIGHT },
45-
objectives_label : { type : "label", text : {group:62, id:10}, pos : {x:48, y:104}, font : FONT_NORMAL_WHITE_ON_DARK },
4645
objectives_panel : { type : "inner_panel", pos : {x:32, y:96}, size: {w:33, h:6} },
46+
objectives_label : { type : "label", text : {group:62, id:10}, pos : {x:48, y:104}, font : FONT_NORMAL_WHITE_ON_DARK },
4747
tocity_label : { type : "label", text : {group:62, id:7}, pos : {x:416, y:433}, font : FONT_NORMAL_BLACK_ON_LIGHT },
4848
goal_0 : { type : "label", pos : {x:32 + 16, y:90 + 32}, body : {w:15, h:1}, font : FONT_NORMAL_YELLOW, enabled: false },
4949
goal_1 : { type : "label", pos : {x:288 + 16, y:90 + 32}, body : {w:15, h:1}, font : FONT_NORMAL_YELLOW, enabled: false },

0 commit comments

Comments
 (0)