Skip to content

Commit b5f0202

Browse files
author
s.kushnirenko
committed
ui: use js config for rating window
1 parent 37d4317 commit b5f0202

File tree

11 files changed

+93
-38
lines changed

11 files changed

+93
-38
lines changed

src/graphics/elements/button.h

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
void button_none(int param1, int param2);
88

99
void button_border_draw(int x, int y, int width_pixels, int height_pixels, bool has_focus);
10+
inline void button_border_draw(vec2i pos, vec2i size, bool has_focus) { button_border_draw(pos.x, pos.y, size.x, size.y, has_focus); }
1011

1112
template<class T>
1213
bool is_button_hover(const T &button, vec2i context) {

src/graphics/elements/panel.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "core/vec2i.h"
44
#include "game/resource.h"
5+
#include "city/constants.h"
56
#include "ui.h"
67

78
#define DEFAULT_BLOCK_SIZE 16
@@ -22,4 +23,5 @@ void large_label_draw(int x, int y, int width_blocks, int type);
2223
namespace ui {
2324
void panel(vec2i pos, vec2i size, UiFlags_ flags);
2425
void icon(vec2i pos, e_resource img);
26+
void icon(vec2i pos, e_advisor advisor);
2527
}

src/graphics/elements/ui.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ void ui::icon(vec2i pos, e_resource img) {
128128
ImageDraw::img_generic(ctx, image_id_resource_icon(RESOURCE_DEBEN), offset.x + pos.x, offset.y + pos.y);
129129
}
130130

131+
void ui::icon(vec2i pos, e_advisor adv) {
132+
const vec2i offset = g_state.offset();
133+
painter ctx = game.painter();
134+
ImageDraw::img_generic(ctx, image_group(IMG_ADVISOR_ICONS) + (adv - 1), offset.x + pos.x, offset.y + pos.y);
135+
}
136+
137+
131138
arrow_button &ui::arw_button(vec2i pos, bool up, bool tiny) {
132139
const vec2i offset = g_state.offset();
133140

src/graphics/image_desc.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include <cstdint>
44

5-
enum e_image_id : uint16_t {
5+
enum e_image_id {
66
#define REGISTER_IMG(a, b) a = b,
77
#include "graphics/indexes.h"
88
IMG_SIZE

src/graphics/indexes.h

+2
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,5 @@ REGISTER_IMG(IMG_SMALL_MASTABA_BRICKS, 127)
128128
REGISTER_IMG(IMG_BUILDING_FIREHOUSE, 128)
129129
REGISTER_IMG(IMG_BUILDING_APOTHECARY, 128)
130130
REGISTER_IMG(IMG_PERSONAL_MANSION, 129)
131+
REGISTER_IMG(IMG_ADVISOR_RATING_ICON, 130)
132+
REGISTER_IMG(IMG_ADVISOR_ICONS, 131)

src/js/js_game.h

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <vector>
99

10+
enum e_image_id;
11+
1012
struct archive {
1113
js_State *vm;
1214
inline archive(js_State *_vm) : vm(_vm) {}
@@ -43,13 +45,19 @@ struct archive {
4345
return result;
4446
}
4547

48+
inline e_image_id r_image(pcstr name) { return (e_image_id)r_int(name); }
49+
4650
inline bool r_bool(pcstr name) {
4751
js_getproperty(vm, -1, name);
4852
bool result = js_isundefined(vm, -1) ? 0 : js_toboolean(vm, -1);
4953
js_pop(vm, 1);
5054
return result;
5155
}
5256

57+
inline vec2i r_size2i(pcstr name, pcstr w = "w", pcstr h = "h") {
58+
return r_vec2i(name, w, h);
59+
}
60+
5361
inline vec2i r_vec2i(pcstr name, pcstr x = "x", pcstr y = "y") {
5462
vec2i result(0, 0);
5563
r_section(name, [&] (archive arch) {

src/scripts/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ var building_scribal_school_info = {}
2929
var empire_window = {}
3030
var images_remap = []
3131
var imagepaks = []
32+
var advisor_rating_window = {}

src/scripts/images.js

+2
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ images = [
149149
{img: IMG_BUILDING_FIREHOUSE, pack:PACK_GENERAL, id:78},
150150
{img: IMG_BUILDING_APOTHECARY, pack:PACK_GENERAL, id:68},
151151
{img: IMG_PERSONAL_MANSION, pack:PACK_GENERAL, id:85},
152+
{img: IMG_ADVISOR_RATING_ICON, pack:PACK_GENERAL, id:128, offset:3},
153+
{img: IMG_ADVISOR_ICONS, pack:PACK_GENERAL, id:128, offset:0},
152154
]
153155

154156
cart_offsets = [

src/scripts/ui.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ top_menu_bar = {
1313
}
1414

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

2020
buttons : [
@@ -26,6 +26,13 @@ main_menu_window = {
2626
]
2727
}
2828

29+
advisor_rating_window = {
30+
outer_panel_pos : {x:0, y:0},
31+
outer_panel_size : {w:40, h:27},
32+
advisor_icon_image : IMG_ADVISOR_RATING_ICON,
33+
advisor_icon_pos : {x: 10, y: 10},
34+
}
35+
2936
empire_window = {
3037
trade_column_spacing : 106,
3138
trade_row_spacing : 20,

src/window/advisor/advisor_ratings.cpp

+58-33
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,31 @@
1313
#include "scenario/property.h"
1414
#include "game/game.h"
1515

16-
#define ADVISOR_HEIGHT 27
16+
#include "js/js_game.h"
17+
18+
ANK_REGISTER_CONFIG_ITERATOR(config_load_advisor_rating);
1719

1820
static void button_rating(int rating, int param2);
1921

22+
struct advisor_rating_window {
23+
vec2i outer_panel_pos;
24+
vec2i outer_panel_size;
25+
e_image_id advisor_icon_image;
26+
vec2i advisor_icon_pos;
27+
};
28+
29+
advisor_rating_window g_advisor_rating_window;
30+
31+
void config_load_advisor_rating() {
32+
g_config_arch.r_section("advisor_rating_window", [] (archive arch) {
33+
auto &w = g_advisor_rating_window;
34+
w.outer_panel_pos = arch.r_vec2i("outer_panel_pos");
35+
w.outer_panel_size = arch.r_size2i("outer_panel_size");
36+
w.advisor_icon_image = arch.r_image("advisor_icon_image");
37+
w.advisor_icon_pos = arch.r_vec2i("advisor_icon_pos");
38+
});
39+
}
40+
2041
static generic_button rating_buttons[] = {
2142
{80, 276, 120, 60, button_rating, button_none, SELECTED_RATING_CULTURE, 0},
2243
{200, 276, 120, 60, button_rating, button_none, SELECTED_RATING_PROSPERITY, 0},
@@ -42,6 +63,7 @@ static void draw_rating_column(int x_offset, int y_offset, int value, int has_re
4263
ImageDraw::img_generic(ctx, image_base, x_offset - 4, y);
4364
for (int i = 0; i < 2 * value_to_draw; i++)
4465
ImageDraw::img_generic(ctx, image_base + 1, x_offset + 11, --y);
66+
4567
if (has_reached)
4668
ImageDraw::img_generic(ctx, image_base + 2, x_offset - 6, y - 50);
4769
}
@@ -59,9 +81,16 @@ static void draw_rating(int id, int value, int open_play, int goal) {
5981
}
6082

6183
static int draw_background() {
84+
return g_advisor_rating_window.outer_panel_size.y;
85+
}
86+
87+
static void draw_foreground() {
6288
painter ctx = game.painter();
63-
outer_panel_draw(vec2i{0, 0}, 40, ADVISOR_HEIGHT);
64-
ImageDraw::img_generic(ctx, image_id_from_group(GROUP_ADVISOR_ICONS) + 3, 10, 10);
89+
90+
auto &w = g_advisor_rating_window;
91+
ui::panel(w.outer_panel_pos, w.outer_panel_size, UiFlags_PanelOuter);
92+
ui::icon(w.advisor_icon_pos, ADVISOR_RATINGS);
93+
6594
int width = lang_text_draw(53, 0, 60, 12, FONT_LARGE_BLACK_ON_LIGHT);
6695
if (!winning_population() || scenario_is_open_play()) {
6796
lang_text_draw(53, 7, 80 + width, 17, FONT_NORMAL_BLACK_ON_LIGHT);
@@ -86,42 +115,38 @@ static int draw_background() {
86115
inner_panel_draw(box_x, box_y, 35, 5);
87116
switch (city_rating_selected()) {
88117
case SELECTED_RATING_CULTURE:
89-
lang_text_draw(53, 1, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
90-
if (city_rating_culture() <= 90) {
91-
lang_text_draw_multiline(53, 9 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
92-
} else
93-
lang_text_draw_multiline(53, 50, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
94-
break;
118+
lang_text_draw(53, 1, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
119+
if (city_rating_culture() <= 90) {
120+
lang_text_draw_multiline(53, 9 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
121+
} else
122+
lang_text_draw_multiline(53, 50, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
123+
break;
95124
case SELECTED_RATING_PROSPERITY:
96-
lang_text_draw(53, 2, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
97-
if (city_rating_prosperity() <= 90) {
98-
lang_text_draw_multiline(
99-
53, 16 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
100-
} else
101-
lang_text_draw_multiline(53, 51, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
102-
break;
125+
lang_text_draw(53, 2, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
126+
if (city_rating_prosperity() <= 90) {
127+
lang_text_draw_multiline(53, 16 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
128+
} else
129+
lang_text_draw_multiline(53, 51, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
130+
break;
103131
case SELECTED_RATING_MONUMENT:
104-
lang_text_draw(53, 3, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
105-
if (city_rating_monument() <= 90) {
106-
lang_text_draw_multiline(53, 41 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
107-
} else
108-
lang_text_draw_multiline(53, 52, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
109-
break;
132+
lang_text_draw(53, 3, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
133+
if (city_rating_monument() <= 90) {
134+
lang_text_draw_multiline(53, 41 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
135+
} else
136+
lang_text_draw_multiline(53, 52, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
137+
break;
110138
case SELECTED_RATING_KINGDOM:
111-
lang_text_draw(53, 4, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
112-
if (city_rating_kingdom() <= 90) {
113-
lang_text_draw_multiline(53, 27 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
114-
} else
115-
lang_text_draw_multiline(53, 53, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
116-
break;
139+
lang_text_draw(53, 4, box_x + 8, box_y + 4, FONT_NORMAL_WHITE_ON_DARK);
140+
if (city_rating_kingdom() <= 90) {
141+
lang_text_draw_multiline(53, 27 + city_rating_selected_explanation(), vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
142+
} else
143+
lang_text_draw_multiline(53, 53, vec2i{box_x + 8, box_y + 22}, box_w, FONT_NORMAL_WHITE_ON_DARK);
144+
break;
117145
default:
118-
lang_text_draw_centered(53, 8, box_x + 8, 380, box_w, FONT_NORMAL_WHITE_ON_DARK);
119-
break;
146+
lang_text_draw_centered(53, 8, box_x + 8, 380, box_w, FONT_NORMAL_WHITE_ON_DARK);
147+
break;
120148
}
121149

122-
return ADVISOR_HEIGHT;
123-
}
124-
static void draw_foreground(void) {
125150
button_border_draw(rating_buttons[0].x, rating_buttons[0].y, rating_buttons[0].width, rating_buttons[0].height, focus_button_id == SELECTED_RATING_CULTURE);
126151
button_border_draw(rating_buttons[1].x, rating_buttons[1].y, rating_buttons[1].width, rating_buttons[1].height, focus_button_id == SELECTED_RATING_PROSPERITY);
127152
button_border_draw(rating_buttons[2].x, rating_buttons[2].y, rating_buttons[2].width, rating_buttons[2].height, focus_button_id == SELECTED_RATING_MONUMENT);

src/window/main_menu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ main_menu_data_t g_main_menu_data;
5656
void config_load_main_menu() {
5757
g_config_arch.r_section("main_menu_window", [] (archive arch) {
5858
auto &data = g_main_menu_data;
59-
data.button_pos = arch.r_vec2i("pos");
60-
data.button_size = arch.r_vec2i("btn", "w", "h");
59+
data.button_pos = arch.r_vec2i("button_pos");
60+
data.button_size = arch.r_size2i("button_size");
6161
data.button_offset = arch.r_int("button_offset");
6262

6363
data.buttons_text.clear();

0 commit comments

Comments
 (0)