Skip to content

Commit 5414e86

Browse files
committed
ui: redesigned trade prices window
1 parent 418ce33 commit 5414e86

File tree

6 files changed

+93
-72
lines changed

6 files changed

+93
-72
lines changed

src/empire/empire_map.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ void empire_load_editor(int empire_id, int viewport_width, int viewport_height)
5555
// }
5656
// check_scroll_boundaries();
5757
}
58+
5859
void empire_map_t::init_scenario() {
5960
scroll_x = initial_scroll_x;
6061
scroll_y = initial_scroll_y;

src/empire/trade_prices.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ struct trade_price {
1111

1212
static const struct trade_price DEFAULT_PRICES[RESOURCES_MAX] = {
1313
{0, 0},
14-
{28, 21}, // grain
15-
{47, 35}, // meat
16-
{33, 25}, // lettuce
17-
{33, 25}, // chickpeas
18-
{33, 25}, // pomegranates
19-
{33, 25}, // figs
20-
{42, 33}, // fish
21-
{44, 34}, // gamemeat
14+
{28, 21}, // grain
15+
{47, 35}, // meat
16+
{33, 25}, // lettuce
17+
{33, 25}, // chickpeas
18+
{33, 25}, // pomegranates
19+
{33, 25}, // figs
20+
{42, 33}, // fish
21+
{44, 34}, // gamemeat
2222

2323
{21, 16}, // straw
2424
{325, 275}, // weapons
@@ -56,6 +56,7 @@ void trade_prices_reset(void) {
5656
prices[i] = DEFAULT_PRICES[i];
5757
}
5858
}
59+
5960
int trade_price_change(int resource, int amount) {
6061
if (amount < 0 && prices[resource].sell <= 0) {
6162
// cannot lower the price to negative
@@ -74,14 +75,12 @@ int trade_price_change(int resource, int amount) {
7475
int trade_price_buy(int resource) {
7576
return prices[resource].buy;
7677
}
78+
7779
int trade_price_sell(int resource, int bonus_inclusion) {
7880
switch (bonus_inclusion) {
79-
case PRICE_WITH_BONUS:
80-
return prices[resource].sell * (city_data.religion.ra_150_export_profits_months_left > 0 ? 1.5f : 1.0f);
81-
case PRICE_ONLY_BASE:
82-
return prices[resource].sell;
83-
case PRICE_ONLY_BONUS:
84-
return prices[resource].sell * (city_data.religion.ra_150_export_profits_months_left > 0 ? 0.5f : 0.0f);
81+
case PRICE_WITH_BONUS: return prices[resource].sell * (city_data.religion.ra_150_export_profits_months_left > 0 ? 1.5f : 1.0f);
82+
case PRICE_ONLY_BASE: return prices[resource].sell;
83+
case PRICE_ONLY_BONUS: return prices[resource].sell * (city_data.religion.ra_150_export_profits_months_left > 0 ? 0.5f : 0.0f);
8584
}
8685

8786
return 999;

src/empire/trade_prices.h

+3-32
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
1-
#ifndef EMPIRE_TRADE_PRICES_H
2-
#define EMPIRE_TRADE_PRICES_H
1+
#pragma once
32

43
#include "core/buffer.h"
54
#include "game/resource.h"
65

7-
/**
8-
* @file
9-
* Trade prices.
10-
*/
11-
12-
/**
13-
* Reset trade prices to the default
14-
*/
15-
void trade_prices_reset(void);
16-
17-
/**
18-
* Get the buy price for the resource
19-
* @param resource Resource
20-
*/
6+
void trade_prices_reset();
217
int trade_price_buy(int resource);
22-
238
enum { PRICE_WITH_BONUS, PRICE_ONLY_BASE, PRICE_ONLY_BONUS };
24-
25-
/**
26-
* Get the sell price for the resource
27-
* @param resource Resource
28-
*/
299
int trade_price_sell(int resource, int bonus_inclusion = PRICE_WITH_BONUS);
30-
31-
/**
32-
* Change the trade price for resource by amount
33-
* @param resource Resource to change
34-
* @param amount Amount to change, can be positive or negative
35-
* @return True if the price has been changed
36-
*/
37-
int trade_price_change(int resource, int amount);
38-
39-
#endif // EMPIRE_TRADE_PRICES_H
10+
int trade_price_change(int resource, int amount);

src/scripts/common.js

+1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ var building_mud_tower = {}
131131
var building_road = {}
132132
var building_burning_ruin = {}
133133
var building_marble_quarry = {}
134+
var trade_prices_window = {}
134135

135136
// houses
136137
var building_house_crude_hut = {}

src/window/trade_prices.cpp

+73-21
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,86 @@
1111
#include "graphics/window.h"
1212
#include "input/input.h"
1313
#include "window/advisors.h"
14+
#include "window/autoconfig_window.h"
1415
#include "io/gamefiles/lang.h"
1516
#include "game/game.h"
1617

17-
static void draw_background() {
18+
struct trade_prices_window : autoconfig_window_t<trade_prices_window> {
19+
virtual int handle_mouse(const mouse *m) override { return 0; }
20+
virtual int get_tooltip_text() override { return 0; }
21+
virtual void draw_foreground() override {}
22+
virtual int draw_background() override;
23+
virtual void ui_draw_foreground() override;
24+
virtual int ui_handle_mouse(const mouse *m) override;
25+
virtual void init() {}
26+
27+
virtual void load(archive arch, pcstr section) override {
28+
autoconfig_window::load(arch, section);
29+
30+
next_row_offset = arch.r_vec2i("next_row_offset");
31+
next_item_offset = arch.r_vec2i("next_item_offset");
32+
receive_offset = arch.r_vec2i("receive_offset");
33+
buyer_offset = arch.r_vec2i("buyer_offset");
34+
next = arch.r_int("next");
35+
}
36+
37+
vec2i next_row_offset;
38+
vec2i next_item_offset;
39+
vec2i receive_offset;
40+
vec2i buyer_offset;
41+
int next;
42+
};
43+
44+
trade_prices_window trade_prices_w;
45+
46+
int trade_prices_window::draw_background() {
1847
window_draw_underlying_window();
1948

20-
graphics_set_to_dialog();
49+
return 0;
50+
}
51+
52+
void trade_prices_window::ui_draw_foreground() {
53+
ui.begin_widget(ui.pos);
54+
ui.draw();
2155

22-
graphics_shade_rect(vec2i{33, 53}, vec2i{574, 334}, 0x80);
23-
outer_panel_draw(vec2i{16, 144}, 38, 11);
24-
lang_text_draw(54, 21, 26, 153, FONT_LARGE_BLACK_ON_LIGHT);
25-
lang_text_draw(54, 22, 26, 228, FONT_NORMAL_BLACK_ON_LIGHT);
26-
lang_text_draw(54, 23, 26, 253, FONT_NORMAL_BLACK_ON_LIGHT);
56+
vec2i items_pos = ui["items"].pos;
57+
vec2i current_pos = items_pos;
58+
int start_i = 0;
59+
for (int i = 1; (start_i + i) < RESOURCES_MAX; i++) {
60+
int image_offset = i;
61+
if (i >= next) {
62+
start_i += next;
63+
i = 0;
64+
items_pos += next_row_offset;
65+
current_pos = items_pos;
66+
}
2767

28-
painter ctx = game.painter();
29-
for (int i = 1; i < 16; i++) {
30-
int image_offset = i + resource_image_offset(i, RESOURCE_IMAGE_ICON);
31-
ImageDraw::img_generic(ctx, image_id_resource_icon(image_offset), vec2i{126 + 30 * i, 194});
32-
text_draw_number_centered(trade_price_buy(i), 120 + 30 * i, 229, 30, FONT_SMALL_OUTLINED);
33-
text_draw_number_centered(trade_price_sell(i), 120 + 30 * i, 254, 30, FONT_SMALL_OUTLINED);
68+
e_resource resource = e_resource(start_i + i);
69+
if (resource == RESOURCE_UNUSED12) {
70+
continue;
71+
}
72+
73+
ui.icon(current_pos, resource);
74+
75+
const int buy_price = trade_price_buy(i);
76+
const int sell_price = trade_price_sell(i);
77+
ui.label(bstring32(buy_price), current_pos + buyer_offset , FONT_NORMAL_BLACK_ON_LIGHT);
78+
ui.label(bstring32(sell_price), current_pos + receive_offset, FONT_NORMAL_BLACK_ON_LIGHT);
79+
80+
current_pos += next_item_offset;
3481
}
35-
lang_text_draw_centered(13, 1, 16, 296, 608, FONT_NORMAL_BLACK_ON_LIGHT);
3682

37-
graphics_reset_dialog();
83+
ui.end_widget();
3884
}
3985

40-
static void handle_input(const mouse* m, const hotkeys* h) {
41-
if (input_go_back_requested(m, h))
86+
int trade_prices_window::ui_handle_mouse(const mouse *m) {
87+
const hotkeys *h = hotkey_state();
88+
if (input_go_back_requested(m, h)) {
4289
window_advisors_show();
90+
return 0;
91+
}
92+
93+
return autoconfig_window::ui_handle_mouse(m);
4394
}
4495

4596
static int get_tooltip_resource(tooltip_context* c) {
@@ -68,10 +119,11 @@ static void get_tooltip(tooltip_context* c) {
68119
void window_trade_prices_show(void) {
69120
static window_type window = {
70121
WINDOW_TRADE_PRICES,
71-
draw_background,
72-
0,
73-
handle_input,
74-
get_tooltip
122+
[] { trade_prices_w.draw_background(); },
123+
[] { trade_prices_w.ui_draw_foreground(); },
124+
[] (const mouse *m, const hotkeys *h) { trade_prices_w.ui_handle_mouse(m); },
125+
nullptr
75126
};
127+
76128
window_show(&window);
77129
}

src/window/trade_prices.h

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#ifndef WINDOW_TRADE_PRICES_H
2-
#define WINDOW_TRADE_PRICES_H
1+
#pragma once
32

4-
void window_trade_prices_show(void);
5-
6-
#endif // WINDOW_TRADE_PRICES_H
3+
void window_trade_prices_show();

0 commit comments

Comments
 (0)