Skip to content

Commit db86e86

Browse files
committed
ui: trade route draw options moved to config
1 parent d3552d4 commit db86e86

File tree

6 files changed

+55
-94
lines changed

6 files changed

+55
-94
lines changed

src/graphics/elements/ui.cpp

+19-9
Original file line numberDiff line numberDiff line change
@@ -461,24 +461,34 @@ int ui::label_percent(int amount, vec2i pos, e_font font) {
461461
return text_draw_percentage(amount, offset.x + pos.x, offset.y + pos.y, font);
462462
}
463463

464-
int ui::label_colored(textid tx, vec2i pos, e_font font, color color) {
465-
return lang_text_draw_colored(tx.group, tx.id, pos.x, pos.y, font, color);
464+
int ui::label_colored(textid tx, vec2i pos, e_font font, color color, int box_width) {
465+
if (box_width > 0) {
466+
lang_text_draw_centered_colored(tx.group, tx.id, pos.x, pos.y, box_width, font, color);
467+
return box_width;
468+
} else {
469+
return lang_text_draw_colored(tx.group, tx.id, pos.x, pos.y, font, color);
470+
}
466471
}
467472

468-
int ui::label_colored(pcstr tx, vec2i pos, e_font font, color color) {
469-
return lang_text_draw_colored(tx, pos.x, pos.y, font, color);
473+
int ui::label_colored(pcstr tx, vec2i pos, e_font font, color color, int box_width) {
474+
if (box_width > 0) {
475+
text_draw_centered((const uint8_t*)tx, pos.x, pos.y, box_width, font, color);
476+
return box_width;
477+
} else {
478+
return lang_text_draw_colored(tx, pos.x, pos.y, font, color);
479+
}
470480
}
471481

472-
void ui::eimage(e_image_id group, vec2i pos, int img) {
482+
const image_t *ui::eimage(int imgid, vec2i pos) {
473483
painter ctx = game.painter();
474484
const vec2i offset = g_state.offset();
475-
ImageDraw::img_generic(ctx, image_group(group) + img, pos + offset);
485+
return ImageDraw::img_generic(ctx, imgid, pos + offset);
476486
}
477487

478-
void ui::eimage(image_desc imgd, vec2i pos) {
488+
const image_t *ui::eimage(image_desc imgd, vec2i pos) {
479489
painter ctx = game.painter();
480490
const vec2i offset = g_state.offset();
481-
ImageDraw::img_generic(ctx, image_group(imgd), pos + offset);
491+
return ImageDraw::img_generic(ctx, image_group(imgd), pos + offset);
482492
}
483493

484494
void ui::panel(vec2i pos, vec2i size, UiFlags flags) {
@@ -649,7 +659,7 @@ void ui::widget::line(bool hline, vec2i npos, int size) {
649659

650660
void ui::eimg::draw() {
651661
if (img > 0) {
652-
ui::eimage(img, pos);
662+
//ui::eimage(img, pos);
653663
} else if (isometric) {
654664
painter ctx = game.painter();
655665
const vec2i offset = g_state.offset();

src/graphics/elements/ui.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ int label(int group, int number, vec2i pos, e_font font = FONT_NORMAL_BLACK_ON_L
6161
int label(pcstr, vec2i pos, e_font font = FONT_NORMAL_BLACK_ON_LIGHT, UiFlags flags = UiFlags_None, int box_width = 0);
6262
int label_amount(int group, int number, int amount, vec2i pos, e_font font = FONT_NORMAL_BLACK_ON_LIGHT, pcstr postfix = "");
6363
int label_percent(int amount, vec2i pos, e_font font = FONT_NORMAL_BLACK_ON_LIGHT);
64-
int label_colored(textid tx, vec2i pos, e_font font, color color);
65-
int label_colored(pcstr tx, vec2i pos, e_font font, color color);
66-
void eimage(e_image_id img, vec2i pos, int offset = 0);
67-
void eimage(image_desc img, vec2i pos);
64+
int label_colored(textid tx, vec2i pos, e_font font, color color, int box_width = 0);
65+
int label_colored(pcstr tx, vec2i pos, e_font font, color color, int box_width = 0);
66+
const image_t *eimage(int imgid, vec2i pos);
67+
const image_t *eimage(image_desc img, vec2i pos);
6868
void panel(vec2i pos, vec2i size, UiFlags flags);
6969
void icon(vec2i pos, e_resource img, UiFlags flags = UiFlags_None);
7070
void icon(vec2i pos, e_advisor advisor);

src/graphics/indexes.h

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ REGISTER_IMG(IMG_CART_COPPER, 52)
99
REGISTER_IMG(IMG_CART_BARLEY, 73)
1010
REGISTER_IMG(IMG_TOP_MENU_BACKGROUND, 75)
1111
REGISTER_IMG(IMG_RESOURCE_PAPYRUS, 77)
12-
REGISTER_IMG(IMG_EMPIRE_ROUTE_HL_CLOSED, 78)
13-
REGISTER_IMG(IMG_EMPIRE_ROUTE_OPEN, 79)
14-
REGISTER_IMG(IMG_EMPIRE_ROUTE_HL_OPEN, 80)
1512
REGISTER_IMG(IMG_MINIMAP_BRIGHT_TEAL, 82)
1613
REGISTER_IMG(IMG_MINIMAP_DARK_RED, 85)
1714
REGISTER_IMG(IMG_MINIMAP_PURPLE, 86)

src/scripts/images.js

-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ images = [
1515

1616
{img: IMG_TOP_MENU_BACKGROUND, pack:PACK_GENERAL, id:121, offset:8},
1717
{img: IMG_RESOURCE_PAPYRUS, pack:PACK_GENERAL, id:207, offset:0},
18-
{img: IMG_EMPIRE_ROUTE_HL_CLOSED, pack:PACK_GENERAL, id:149, offset:211},
19-
{img: IMG_EMPIRE_ROUTE_OPEN, pack:PACK_GENERAL, id:149, offset:201},
20-
{img: IMG_EMPIRE_ROUTE_HL_OPEN, pack:PACK_GENERAL, id:149, offset:186},
2118
{img: IMG_MINIMAP_BRIGHT_TEAL, pack:PACK_GENERAL, id:149, offset:170},
2219
{img: IMG_MINIMAP_DARK_RED, pack:PACK_GENERAL, id:149, offset:165},
2320
{img: IMG_MINIMAP_PURPLE, pack:PACK_GENERAL, id:149, offset:175},

src/scripts/ui.js

+3
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,9 @@ empire_window = {
14111411
vertical_bar : {pack:PACK_GENERAL, id:172, offset:0},
14121412
cross_bar : {pack:PACK_GENERAL, id:172, offset:2},
14131413
trade_amount : {pack:PACK_GENERAL, id:171},
1414+
closed_trade_route_hl : {pack:PACK_GENERAL, id:149, offset:211},
1415+
open_trade_route : {pack:PACK_GENERAL, id:149, offset:201},
1416+
open_trade_route_hl : {pack:PACK_GENERAL, id:149, offset:186},
14141417

14151418
ui : {
14161419
background : dummy({size:[sw(0), sh(0)]}),

src/window/window_empire.cpp

+29-75
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,8 @@ struct empire_window : public autoconfig_window_t<empire_window> {
6565
int selected_city = 1;
6666
vec2i min_pos, max_pos;
6767
vec2i draw_offset;
68-
int focus_button_id;
6968
int is_scrolling;
7069
int finished_scroll;
71-
int focus_resource;
7270
int trade_column_spacing;
7371
int trade_row_spacing;
7472
int info_y_traded;
@@ -81,7 +79,8 @@ struct empire_window : public autoconfig_window_t<empire_window> {
8179
int trade_button_offset_y;
8280
vec2i start_pos, finish_pos;
8381
image_desc image, bottom_image, horizontal_bar,
84-
vertical_bar, cross_bar, trade_amount;
82+
vertical_bar, cross_bar, trade_amount,
83+
closed_trade_route_hl, open_trade_route, open_trade_route_hl;
8584
//svector<object_trade_info, 16> buying_goods;
8685
//svector<object_trade_info, 16> selling_goods;
8786

@@ -113,6 +112,9 @@ struct empire_window : public autoconfig_window_t<empire_window> {
113112
arch.r_desc("vertical_bar", vertical_bar);
114113
arch.r_desc("cross_bar", cross_bar);
115114
arch.r_desc("trade_amount", trade_amount);
115+
arch.r_desc("closed_trade_route_hl", closed_trade_route_hl);
116+
arch.r_desc("open_trade_route", open_trade_route);
117+
arch.r_desc("open_trade_route_hl", open_trade_route_hl);
116118

117119
init();
118120
}
@@ -129,6 +131,7 @@ struct empire_window : public autoconfig_window_t<empire_window> {
129131
void determine_selected_object(const mouse *m);
130132
void draw_city_info(const empire_object *object);
131133
void draw_trade_resource(e_resource resource, int trade_now, int trade_max, vec2i offset, e_font font);
134+
void draw_trade_route(int route_id, e_empire_route_state effect);
132135
};
133136

134137
empire_window g_empire_window;
@@ -137,16 +140,14 @@ void empire_window::init() {
137140
selected_button = 0;
138141
int selected_object = g_empire_map.selected_object();
139142
selected_city = selected_object ? g_empire.get_city_for_object(selected_object - 1) : 0;
140-
focus_button_id = 0;
141143

142144
ui["city_want_sell_items"].ondraw([this] (ui::element *e) { draw_city_want_sell(e); });
143145
ui["city_want_buy_items"].ondraw([this] (ui::element *e) { draw_city_want_buy(e); });
144146
ui["city_sell_items"].ondraw([this] (ui::element *e) { draw_city_selling(e); });
145147
ui["city_buy_items"].ondraw([this] (ui::element *e) { draw_city_buy(e); });
146148
}
147149

148-
static void draw_trade_route(int route_id, e_empire_route_state effect) {
149-
auto &data = g_empire_window;
150+
void empire_window::draw_trade_route(int route_id, e_empire_route_state effect) {
150151
painter ctx = game.painter();
151152

152153
map_route_object* obj = empire_get_route_object(route_id);
@@ -162,21 +163,21 @@ static void draw_trade_route(int route_id, e_empire_route_state effect) {
162163
//image_id = image_id_from_group(GROUP_MINIMAP_BUILDING) + 211;
163164
break;
164165
case ROUTE_CLOSED_SELECTED: // highlighted, closed
165-
image_id = image_group(IMG_EMPIRE_ROUTE_HL_CLOSED);
166+
image_id = image_group(closed_trade_route_hl);
166167
break;
167168
case ROUTE_OPEN: // open
168-
image_id = image_group(IMG_EMPIRE_ROUTE_OPEN);
169+
image_id = image_group(open_trade_route);
169170
break;
170171
case ROUTE_OPEN_SELECTED: // highlighted, open
171-
image_id = image_group(IMG_EMPIRE_ROUTE_HL_OPEN);
172+
image_id = image_group(open_trade_route_hl);
172173
break;
173174
}
174175

175176
for (int i = 0; i < obj->num_points; i++) {
176177
const auto &route_point = obj->points[i];
177178

178179
// first corner in pair
179-
ImageDraw::img_generic(ctx, image_id, data.draw_offset + route_point.p);
180+
ImageDraw::img_generic(ctx, image_id, draw_offset + route_point.p);
180181

181182
// draw lines connecting the turns
182183
if (i < obj->num_points - 1) {
@@ -189,25 +190,15 @@ static void draw_trade_route(int route_id, e_empire_route_state effect) {
189190

190191
float progress = 1.0;
191192
while (progress < len) {
192-
vec2i disp = data.draw_offset + route_point.p + vec2i{(int)(scaled_x * progress), (int)(scaled_y * progress)};
193+
vec2i disp = draw_offset + route_point.p + vec2i{(int)(scaled_x * progress), (int)(scaled_y * progress)};
193194
ImageDraw::img_generic(ctx, image_id, disp);
194195
progress += 1.0f;
195196
}
196197
}
197198
}
198199
}
199200

200-
static int column_idx(int index) {
201-
return index % 2;
202-
}
203-
204-
static int row_idx(int index) {
205-
return index / 2;
206-
}
207-
208201
void empire_window::draw_trade_resource(e_resource resource, int trade_now, int trade_max, vec2i offset, e_font font) {
209-
painter ctx = game.painter();
210-
int img_id = image_id_resource_icon(resource);
211202
ui.icon(offset + vec2i{ 1, 1 }, resource, UiFlags_Outline);
212203
ui.button("", offset - vec2i{ 2, 2 }, vec2i{ 105, 24 }, fonts_vec{}, UiFlags_NoBody)
213204
.tooltip({23, resource})
@@ -224,20 +215,9 @@ void empire_window::draw_trade_resource(e_resource resource, int trade_now, int
224215
ui.label(text.c_str(), offset + vec2i{ 40, 0 }, font);
225216

226217
switch (trade_max) {
227-
case 1500:
228-
case 15:
229-
ImageDraw::img_generic(ctx, image_group(trade_amount), offset + vec2i{21, -1});
230-
break;
231-
232-
case 2500:
233-
case 25:
234-
ImageDraw::img_generic(ctx, image_group(trade_amount + 1), offset + vec2i{17, -1});
235-
break;
236-
237-
case 4000:
238-
case 40:
239-
ImageDraw::img_generic(ctx, image_group(trade_amount + 2), offset + vec2i{13, -1});
240-
break;
218+
case 1500: case 15: ui.image(trade_amount, offset + vec2i{21, -1}); break;
219+
case 2500: case 25: ui.image(trade_amount + 1, offset + vec2i{17, -1}); break;
220+
case 4000: case 40: ui.image(trade_amount + 2, offset + vec2i{13, -1}); break;
241221
}
242222
}
243223

@@ -294,9 +274,6 @@ void empire_window::draw_city_buy(ui::element *e) {
294274
if (!empire_object_city_buys_resource(object->id, resource))
295275
continue;
296276

297-
int column_offset = trade_column_spacing * column_idx(index) + 200;
298-
int row_offset = trade_row_spacing * row_idx(index) + 20;
299-
300277
const auto &trade_route = city->get_route();
301278
int trade_max = trade_route.limit(resource);
302279
int trade_now = std::min(trade_max, trade_route.traded(resource));
@@ -327,9 +304,6 @@ void empire_window::draw_city_selling(ui::element *e) {
327304
continue;
328305
}
329306

330-
int column_offset = trade_column_spacing * column_idx(index) - 150;
331-
int row_offset = trade_row_spacing * row_idx(index) + 20;
332-
333307
const auto &trade_route = city->get_route();
334308
int trade_max = trade_route.limit(resource);
335309
int trade_now = std::min(trade_max, trade_route.traded(resource));
@@ -437,6 +411,7 @@ void empire_window::determine_selected_object(const mouse *m) {
437411
finished_scroll = 0;
438412
return;
439413
}
414+
440415
g_empire_map.select_object(vec2i{ m->x, m->y } - min_pos - vec2i{ 16, 16 });
441416
window_invalidate();
442417
}
@@ -469,17 +444,13 @@ int empire_window::ui_handle_mouse(const mouse *m) {
469444
}
470445
}
471446

472-
focus_button_id = 0;
473-
focus_resource = 0;
474-
int button_id;
475-
476447
determine_selected_object(m);
448+
477449
int selected_object = g_empire_map.selected_object();
478450
if (selected_object) {
479451
const empire_object *obj = empire_object_get(selected_object - 1);
480452
if (obj->type == EMPIRE_OBJECT_CITY) {
481453
selected_city = g_empire.get_city_for_object(selected_object - 1);
482-
const empire_city *city = g_empire.city(selected_city);
483454
}
484455

485456
if (input_go_back_requested(m, h)) {
@@ -500,7 +471,6 @@ int empire_window::ui_handle_mouse(const mouse *m) {
500471
}
501472

502473
void empire_window::draw_empire_object(const empire_object* obj) {
503-
auto &data = g_empire_window;
504474
if (obj->type == EMPIRE_OBJECT_LAND_TRADE_ROUTE || obj->type == EMPIRE_OBJECT_SEA_TRADE_ROUTE) {
505475
if (!g_empire.is_trade_route_open(obj->trade_route_id)) {
506476
return;
@@ -525,46 +495,32 @@ void empire_window::draw_empire_object(const empire_object* obj) {
525495
if (city->type == EMPIRE_CITY_EGYPTIAN_TRADING || city->type == EMPIRE_CITY_FOREIGN_TRADING || city->type == EMPIRE_CITY_PHARAOH_TRADING) {
526496
e_empire_route_state state = ROUTE_CLOSED;
527497
if (city->is_open) {
528-
state = (g_empire_map.selected_object() && data.selected_city == g_empire.get_city_for_object(obj->id))
498+
state = (g_empire_map.selected_object() && selected_city == g_empire.get_city_for_object(obj->id))
529499
? ROUTE_OPEN_SELECTED
530500
: ROUTE_OPEN;
531501
} else {
532-
state = (g_empire_map.selected_object() && data.selected_city == g_empire.get_city_for_object(obj->id))
502+
state = (g_empire_map.selected_object() && selected_city == g_empire.get_city_for_object(obj->id))
533503
? ROUTE_CLOSED_SELECTED
534504
: ROUTE_CLOSED;
535505
}
536506
draw_trade_route(city->route_id, state);
537507
}
538508

539-
int text_group = (g_settings.city_names_style == CITIES_OLD_NAMES)
540-
? data.text_group_old_names
541-
: data.text_group_new_names;
542-
509+
int text_group = (g_settings.city_names_style == CITIES_OLD_NAMES) ? text_group_old_names : text_group_new_names;
543510
int letter_height = get_letter_height((const uint8_t*)"H", FONT_SMALL_PLAIN);
544-
vec2i text_pos = data.draw_offset + pos + vec2i{0, -letter_height};
511+
vec2i text_pos = draw_offset + pos + vec2i{0, -letter_height};
545512

546513
switch (obj->text_align) {
547-
case 0:
548-
lang_text_draw_centered_colored(text_group, city->name_id, text_pos.x, text_pos.y, obj->width, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED);
549-
break;
550-
551-
case 1:
552-
lang_text_draw_centered_colored(text_group, city->name_id, text_pos.x, text_pos.y, obj->width, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED);
553-
break;
554-
555-
case 2:
556-
lang_text_draw_centered_colored(text_group, city->name_id, text_pos.x, text_pos.y, obj->width, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED);
557-
break;
558-
559-
case 3:
560-
lang_text_draw_centered_colored(text_group, city->name_id, text_pos.x, text_pos.y, obj->width, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED);
561-
break;
514+
case 0: ui::label_colored(ui::str(text_group, city->name_id), text_pos, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED, obj->width); break;
515+
case 1: ui::label_colored(ui::str(text_group, city->name_id), text_pos, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED, obj->width); break;
516+
case 2: ui::label_colored(ui::str(text_group, city->name_id), text_pos, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED, obj->width); break;
517+
case 3: ui::label_colored(ui::str(text_group, city->name_id), text_pos, FONT_SMALL_PLAIN, COLOR_FONT_DARK_RED, obj->width); break;
562518
}
563519
} else if (obj->type == EMPIRE_OBJECT_TEXT) {
564520
const full_empire_object* full = empire_get_full_object(obj->id);
565-
vec2i text_pos = data.draw_offset + pos;
521+
vec2i text_pos = draw_offset + pos;
566522

567-
lang_text_draw_centered_colored(196, full->city_name_id, text_pos.x - 5, text_pos.y, 100, FONT_SMALL_PLAIN, COLOR_FONT_SHITTY_BROWN);
523+
ui::label_colored(ui::str(196, full->city_name_id), text_pos - vec2i{5, 0}, FONT_SMALL_PLAIN, COLOR_FONT_SHITTY_BROWN, 100);
568524
return;
569525
}
570526

@@ -587,14 +543,12 @@ void empire_window::draw_empire_object(const empire_object* obj) {
587543
return;
588544
}
589545

590-
painter ctx = game.painter();
591546
image_id = image_id_remap(image_id);
592-
const image_t* img = image_get(PACK_GENERAL, image_id);
593-
ImageDraw::img_generic(ctx, PACK_GENERAL, image_id, data.draw_offset + pos);
547+
const image_t *img = ui::eimage(image_id, draw_offset + pos);
594548

595549
if (img && img->animation.speed_id) {
596550
int new_animation = empire_object_update_animation(obj, image_id);
597-
ImageDraw::img_generic(ctx, PACK_GENERAL, image_id + new_animation, data.draw_offset + pos + img->animation.sprite_offset);
551+
ui::eimage({ PACK_GENERAL, image_id + new_animation }, draw_offset + pos + img->animation.sprite_offset);
598552
}
599553
}
600554

0 commit comments

Comments
 (0)