Skip to content

Commit 8b3c989

Browse files
committed
ui: split bazaar info window for orders/common parts
1 parent 1f4f999 commit 8b3c989

11 files changed

+150
-144
lines changed

src/graphics/elements/ui.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,11 @@ void ui::widget::load(pcstr section) {
626626
});
627627
}
628628

629+
bool ui::widget::contains(const xstring &id) const {
630+
auto it = std::find_if(elements.begin(), elements.end(), [xid = xstring(id)] (const auto &e) { return e->id == xid; });
631+
return (it != elements.end());
632+
}
633+
629634
ui::element& ui::widget::operator[](pcstr id) {
630635
auto it = std::find_if(elements.begin(), elements.end(), [xid = xstring(id)] (const auto &e) { return e->id == xid; });
631636
if (it == elements.end()) {

src/graphics/elements/ui.h

+1
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ struct widget {
364364

365365
widget() : ui(*this) {}
366366

367+
bool contains(const xstring &id) const;
367368
element& operator[](pcstr id);
368369
inline element &operator[](const bstring32 &id) { return (*this)[id.c_str()]; }
369370
inline element &operator[](const xstring &id) { return (*this)[id.c_str()]; }

src/scripts/ui.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -851,12 +851,11 @@ bazaar_orders_window = {
851851
orders_panel : inner_panel({pos : [16, 42], size: [27, 10] }),
852852
button_help : image_button({margin:{left:14, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134 }),
853853
button_close : image_button({margin:{right:-40, bottom:-40}, size:[27, 27], pack:PACK_GENERAL, id:134, offset:4 }),
854-
empty_all : button({pos:[80, -1], size:[300, 24], margin:{bottom:-64} }),
855854
accept_none : button({pos:[80, -1], size:[300, 24], text:{group:99, id:7}, margin:{bottom:-38} }),
856855

857856
item_orders_column : dummy({margin:{centerx:0}}),
858-
item_arrows_column : dummy({margin:{centerx:-36}}),
859857
item_icon_column : dummy({pos:[25, 0]}),
858+
item_name_column : dummy({pos:[55, 0]}),
860859
item_row : dummy({size:[px(13), 20]}),
861860
items_area : dummy({pos:[0, 50]}),
862861
}

src/window/building/distribution.cpp

+51-53
Original file line numberDiff line numberDiff line change
@@ -140,64 +140,62 @@ uint8_t granary_3quarters_button_text[] = "18";
140140
uint8_t granary_half_button_text[] = "12";
141141
uint8_t granary_quarter_button_text[] = "6";
142142

143-
std::pair<bstring64, e_font> window_building_get_order_instruction(int instr_kind, const storage_t *storage, e_resource resource, int market_order) {
144-
if (storage != nullptr) {
145-
switch (storage->resource_state[resource]) {
146-
case STORAGE_STATE_PHARAOH_ACCEPT:
147-
{
148-
int max_accept = storage->resource_max_accept[resource];
149-
bstring64 text;
150-
text.printf("%d", max_accept);
151-
152-
if (max_accept == 3200) { text = ui::str(99, 28); }
153-
else if (max_accept == 2400) { text = ui::str(99, 27); }
154-
else if (max_accept == 1600) { text = ui::str(99, 26); }
155-
else if (max_accept == 800) { text = ui::str(99, 25); }
156-
157-
pcstr adv_text = "";
158-
if (max_accept == 2400 || max_accept == 1600 || max_accept == 800)
159-
adv_text = ui::str(99, 29);
143+
std::pair<bstring64, e_font> window_market_get_order_instruction(int instr_kind, e_resource resource, int market_order) {
144+
switch (market_order) {
145+
case BAZAAR_ORDER_STATE_BUY:
146+
return { ui::str(97, 8), FONT_NORMAL_WHITE_ON_DARK };
160147

161-
bstring64 full_text;
162-
full_text.printf("%s %s %s", ui::str(99, 18), text, adv_text);
163-
return { full_text, FONT_NORMAL_WHITE_ON_DARK };
164-
}
165-
166-
case STORAGE_STATE_PHARAOH_REFUSE:
167-
return { ui::str(99, 8), FONT_NORMAL_BLACK_ON_DARK };
168-
169-
case STORAGE_STATE_PHARAOH_GET:
170-
{
171-
int max_get = storage->resource_max_get[resource];
172-
bstring64 text;
173-
text.printf("%d", max_get);
174-
if (max_get == 3200) { text = ui::str(99, 31); }
175-
else if (max_get == 2400) { text = ui::str(99, 27); }
176-
else if (max_get == 1600) { text = ui::str(99, 26); }
177-
else if (max_get == 800) { text = ui::str(99, 25); }
178-
179-
pcstr adv_text = "";
180-
if (max_get == 2400 || max_get == 1600 || max_get == 800)
181-
adv_text = ui::str(99, 29);
148+
case BAZAAR_ORDER_STATE_DONT_BUY:
149+
return { ui::str(97, 9), FONT_NORMAL_BLACK_ON_DARK };
150+
}
151+
}
182152

183-
bstring64 full_text;
184-
full_text.printf("%s %s %s", ui::str(99, 19), text, adv_text);
185-
return { full_text, FONT_NORMAL_YELLOW };
186-
}
187-
case STORAGE_STATE_PHARAOH_EMPTY:
188-
return { ui::str(99, 21), FONT_NORMAL_BLACK_ON_DARK };
189-
}
190-
} else {
191-
switch (market_order) {
192-
case BAZAAR_ORDER_STATE_BUY:
193-
return { ui::str(97, 8), FONT_NORMAL_WHITE_ON_DARK };
153+
std::pair<bstring64, e_font> window_building_get_order_instruction(int instr_kind, const storage_t &storage, e_resource resource) {
154+
switch (storage.resource_state[resource]) {
155+
case STORAGE_STATE_PHARAOH_ACCEPT: {
156+
int max_accept = storage.resource_max_accept[resource];
157+
bstring64 text;
158+
text.printf("%d", max_accept);
159+
160+
if (max_accept == 3200) { text = ui::str(99, 28); }
161+
else if (max_accept == 2400) { text = ui::str(99, 27); }
162+
else if (max_accept == 1600) { text = ui::str(99, 26); }
163+
else if (max_accept == 800) { text = ui::str(99, 25); }
164+
165+
pcstr adv_text = "";
166+
if (max_accept == 2400 || max_accept == 1600 || max_accept == 800)
167+
adv_text = ui::str(99, 29);
168+
169+
bstring64 full_text;
170+
full_text.printf("%s %s %s", ui::str(99, 18), text, adv_text);
171+
return { full_text, FONT_NORMAL_WHITE_ON_DARK };
172+
}
194173

195-
case BAZAAR_ORDER_STATE_DONT_BUY:
196-
return { ui::str(97, 9), FONT_NORMAL_BLACK_ON_DARK };
197-
}
174+
case STORAGE_STATE_PHARAOH_REFUSE:
175+
return { ui::str(99, 8), FONT_NORMAL_BLACK_ON_DARK };
176+
177+
case STORAGE_STATE_PHARAOH_GET: {
178+
int max_get = storage.resource_max_get[resource];
179+
bstring64 text;
180+
text.printf("%d", max_get);
181+
if (max_get == 3200) { text = ui::str(99, 31); }
182+
else if (max_get == 2400) { text = ui::str(99, 27); }
183+
else if (max_get == 1600) { text = ui::str(99, 26); }
184+
else if (max_get == 800) { text = ui::str(99, 25); }
185+
186+
pcstr adv_text = "";
187+
if (max_get == 2400 || max_get == 1600 || max_get == 800)
188+
adv_text = ui::str(99, 29);
189+
190+
bstring64 full_text;
191+
full_text.printf("%s %s %s", ui::str(99, 19), text, adv_text);
192+
return { full_text, FONT_NORMAL_YELLOW };
193+
}
194+
case STORAGE_STATE_PHARAOH_EMPTY:
195+
return { ui::str(99, 21), FONT_NORMAL_BLACK_ON_DARK };
198196
}
199197

200-
return { "unknow_storage", FONT_NORMAL_BLACK_ON_DARK };
198+
return { "unknown_storage", FONT_NORMAL_BLACK_ON_DARK };
201199
}
202200

203201
void window_building_draw_order_instruction(int instr_kind, const storage_t* storage, e_resource resource, vec2i pos, int market_order) {

src/window/building/distribution.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ enum e_instr {
99
INSTR_GRANARY = 1,
1010
INSTR_DOCK = 2,
1111
INSTR_OTHERS = 3,
12+
INSTR_MARKET = 4,
1213
};
1314

1415
struct window_building_distribution {
@@ -42,4 +43,6 @@ textid window_building_get_tooltip_granary_orders();
4243
textid window_building_get_tooltip_warehouse_orders();
4344

4445
void window_building_draw_order_instruction(int instr_kind, const storage_t *storage, e_resource resource, vec2i pos, int market_order = -1);
45-
std::pair<bstring64, e_font> window_building_get_order_instruction(int instr_kind, const storage_t *storage, e_resource resource, int market_order = -1);
46+
47+
std::pair<bstring64, e_font> window_market_get_order_instruction(int instr_kind, e_resource resource, int market_order);
48+
std::pair<bstring64, e_font> window_building_get_order_instruction(int instr_kind, const storage_t &storage, e_resource resource);

src/window/window_bazaar_info.cpp

+9-76
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,28 @@ struct bazaar_info_window : public building_info_window_t<bazaar_info_window>{
2424
resource_text_group = arch.r_int("resource_text_group");
2525
}
2626

27-
virtual void window_info_background(object_info &c) override;
28-
virtual void window_info_foreground(object_info &c) override;
2927
virtual void init(object_info &c) override;
3028
virtual bool check(object_info &c) override {
3129
return c.building_get()->type == BUILDING_BAZAAR;
3230
}
33-
34-
void draw_simple_background(object_info &c);
35-
void draw_orders_background(object_info &c);
36-
void draw_orders_foreground(object_info &c);
3731
};
3832

3933
bazaar_info_window g_bazaar_infow;
4034

41-
void bazaar_info_window::draw_simple_background(object_info &c) {
42-
auto bazaar = c.building_get()->dcast_bazaar();
43-
44-
const auto &meta = bazaar->get_info();
35+
void bazaar_info_window::init(object_info &c) {
36+
building_info_window::init(c);
4537

38+
auto bazaar = c.building_get()->dcast_bazaar();
4639
window_building_play_sound(&c, bazaar->get_sound());
4740

48-
std::pair<int, int> reason = {0, 0};
41+
std::pair<int, int> reason = { 0, 0 };
4942
if (!c.has_road_access) {
50-
reason = {69, 25};
43+
reason = { 69, 25 };
5144
}
5245

46+
const auto &meta = bazaar->get_info();
5347
if (bazaar->num_workers() <= 0) {
54-
reason = {meta.text_id, 2};
48+
reason = { meta.text_id, 2 };
5549
}
5650

5751
if (reason.first) {
@@ -100,69 +94,8 @@ void bazaar_info_window::draw_simple_background(object_info &c) {
10094
ui[id_text].font(bazaar->is_good_accepted(INVENTORY_GOOD1 + i) ? FONT_NORMAL_BLACK_ON_LIGHT : FONT_NORMAL_YELLOW);
10195
ui[id_text].text_var(good_res ? "%u" : "", data.market.inventory[INVENTORY_GOOD1 + i]);
10296
}
103-
}
104-
105-
void bazaar_info_window::window_info_foreground(object_info &ctx) {
106-
if (ctx.storage_show_special_orders) {
107-
draw_orders_foreground(ctx);
108-
} else {
109-
g_bazaar_infow.draw();
110-
}
111-
}
11297

113-
void bazaar_info_window::init(object_info &c) {
114-
ui["orders"].onclick([&c] (int, int) {
115-
c.storage_show_special_orders = 1;
116-
window_invalidate();
98+
ui["orders"].onclick([&c] {
99+
window_bazaar_orders_show(c);
117100
});
118-
}
119-
120-
void bazaar_info_window::draw_orders_foreground(object_info &c) {
121-
auto &data = g_window_building_distribution;
122-
draw_orders_background(c);
123-
int line_x = c.offset.x + 215;
124-
int y_offset = window_building_get_vertical_offset(&c, 28 - 11);
125-
painter ctx = game.painter();
126-
127-
building_bazaar* bazaar = c.building_get()->dcast_bazaar();
128-
// backup_storage_settings(storage_id); // TODO: market state backup
129-
const resource_list &resources = city_resource_get_available_market_goods();
130-
for (const auto &r: resources) {
131-
int i = std::distance(&*resources.begin(), &r);
132-
int line_y = 20 * i;
133-
int image_id = image_id_resource_icon(r.type) + resource_image_offset(r.type, RESOURCE_IMAGE_ICON);
134-
135-
ImageDraw::img_generic(ctx, image_id, c.offset.x + 25, y_offset + 48 + line_y);
136-
lang_text_draw(23, r.type, c.offset.x + 52, y_offset + 50 + line_y, FONT_NORMAL_WHITE_ON_DARK);
137-
if (data.resource_focus_button_id - 1 == i) {
138-
button_border_draw(line_x - 10, y_offset + 46 + line_y, data.orders_resource_buttons[i].width, data.orders_resource_buttons[i].height, true);
139-
}
140-
141-
// order status
142-
window_building_draw_order_instruction(INSTR_STORAGE_YARD, nullptr, r.type, vec2i{ line_x, y_offset + 51 + line_y }, bazaar->is_good_accepted(i));
143-
}
144-
145-
// accept none button
146-
// button_border_draw(c->offset.x + 80, y_offset + 382 - 10 * 16, 16 * (c->width_blocks - 10), 20,
147-
// data.orders_focus_button_id == 2 ? 1 : 0);
148-
// lang_text_draw_centered(99, 7, c->offset.x + 80, y_offset + 386 - 10 * 16,
149-
// 16 * (c->width_blocks - 10), FONT_NORMAL_BLACK);
150-
}
151-
152-
void bazaar_info_window::window_info_background(object_info &c) {
153-
building_info_window::window_info_background(c);
154-
155-
if (c.storage_show_special_orders) {
156-
draw_orders_background(c);
157-
} else {
158-
draw_simple_background(c);
159-
}
160-
}
161-
162-
void bazaar_info_window::draw_orders_background(object_info &c) {
163-
c.help_id = 2;
164-
int y_offset = window_building_get_vertical_offset(&c, 28 - 11);
165-
outer_panel_draw(vec2i{c.offset.x, y_offset}, 29, 28 - 11);
166-
lang_text_draw_centered(97, 7, c.offset.x, y_offset + 10, 16 * c.bgsize.x, FONT_LARGE_BLACK_ON_LIGHT);
167-
inner_panel_draw(c.offset + vec2i{ 16, 42 }, { c.bgsize.x - 2, 21 - 10 });
168101
}

src/window/window_bazaar_orders.cpp

+65-1
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,88 @@
66
#include "city/object_info.h"
77
#include "city/city_resource.h"
88
#include "graphics/window.h"
9+
#include "input/input.h"
910
#include "game/game.h"
1011
#include "js/js_game.h"
1112

1213
struct bazaar_orders_window : public building_info_window_t<bazaar_orders_window> {
1314
virtual void window_info_background(object_info &c) override;
1415
virtual void window_info_foreground(object_info &c) override;
16+
virtual int window_info_handle_mouse(const mouse *m, object_info &c) override;
17+
virtual void init(object_info &c) override;
1518
};
1619

1720
bazaar_orders_window bazaar_ordersw;
1821

1922

2023
void window_bazaar_orders_show(object_info &c) {
2124
c.ui = &bazaar_ordersw;
22-
c.ui->window_info_background(c);
25+
c.ui->init(c);
26+
}
27+
28+
void bazaar_orders_window::init(object_info &c) {
29+
building *b = c.building_get();
30+
ui["accept_none"].onclick([building_id = b->id] {
31+
building_bazaar *b = ::building_get(building_id)->dcast_bazaar();
32+
b->unaccept_all_goods();
33+
});
34+
35+
ui["button_close"].onclick([grid_offset = c.grid_offset] {
36+
window_info_show(tile2i(grid_offset), /*avoid_mouse*/true);
37+
});
2338
}
2439

2540
void bazaar_orders_window::window_info_background(object_info &c) {
41+
building_info_window::window_info_background(c);
2642
}
2743

2844
void bazaar_orders_window::window_info_foreground(object_info &c) {
45+
building_bazaar *bazaar = c.building_get()->dcast_bazaar();
46+
const int building_id = bazaar->id();
47+
48+
auto &data = g_window_building_distribution;
49+
ui.begin_widget(c.offset);
50+
ui.draw();
51+
52+
painter ctx = game.painter();
53+
54+
// backup_storage_settings(storage_id); // TODO: market state backup
55+
const auto &items_area = ui["items_area"];
56+
const auto &item_row = ui["item_row"];
57+
const auto &item_icon_column = ui["item_icon_column"];
58+
const auto &item_name_column = ui["item_name_column"];
59+
const auto &item_orders_column = ui["item_orders_column"];
60+
61+
const resource_list &resources = city_resource_get_available_market_goods();
62+
int row_y = 0;
63+
for (const auto &r : resources) {
64+
ui.icon(items_area.pos + item_icon_column.pos + vec2i{ 0, row_y }, r.type);
65+
ui.label(ui::str(23, r.type), items_area.pos + item_name_column.pos + vec2i{ 0, row_y });
66+
67+
auto status = window_market_get_order_instruction(INSTR_MARKET, r.type, bazaar->is_good_accepted(r.type));
68+
ui.button(status.first, items_area.pos + vec2i{ item_orders_column.pos.x, row_y }, item_row.size, fonts_vec{ status.second }, UiFlags_NoBody | UiFlags_AlignYCentered)
69+
.onclick([building_id, resource = r.type] {
70+
building_bazaar *b = ::building_get(building_id)->dcast_bazaar();
71+
b->toggle_good_accepted(resource);
72+
});
73+
74+
row_y += item_row.size.y;
75+
}
76+
77+
ui.end_widget();
78+
}
79+
80+
int bazaar_orders_window::window_info_handle_mouse(const mouse *m, object_info &c) {
81+
ui.begin_widget(c.offset, true);
82+
int result = ui.handle_mouse(m);
83+
ui.end_widget();
84+
85+
const hotkeys *h = hotkey_state();
86+
if (!result && input_go_back_requested(m, h)) {
87+
//storage_settings_backup_check();
88+
window_go_back();
89+
return -1;
90+
}
91+
92+
return result;
2993
}

0 commit comments

Comments
 (0)