Skip to content

Commit 9597b96

Browse files
committed
Add Rgba map to Styles. Use for tints of weapon rounds.
1 parent 5c4217f commit 9597b96

File tree

13 files changed

+69
-42
lines changed

13 files changed

+69
-42
lines changed

assets/styles.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
{
2+
"rgbas": {
3+
"black": "#231d2aff",
4+
"grey": "#535151ff",
5+
"mocha": "#6f5a48ff",
6+
"milk": "#e5cdaeff",
7+
"ice": "#0xd6dbe1e1"
8+
},
29
"buttons": {
310
"default": {
411
"background": {

src/spaced/spaced/game/player.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,7 @@ void Player::debug_switch_weapon() {
9494
return;
9595
}
9696

97-
auto kinetic = std::make_unique<GunKinetic>(*m_services);
98-
kinetic->projectile_config.tint = bave::cyan_v;
99-
m_weapon = std::move(kinetic);
97+
m_weapon = std::make_unique<GunKinetic>(*m_services);
10098
m_debug.shots_remaining = 10;
10199
}
102100
} // namespace spaced

src/spaced/spaced/game/weapons/gun_beam.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <bave/graphics/sprite.hpp>
22
#include <bave/imgui/im_text.hpp>
33
#include <spaced/game/weapons/gun_beam.hpp>
4+
#include <spaced/services/styles.hpp>
45

56
namespace spaced {
67
using bave::im_text;
@@ -96,6 +97,8 @@ class LaserCharge : public IWeaponRound {
9697
};
9798
} // namespace
9899

100+
GunBeam::GunBeam(Services const& services) : Weapon(services, "GunBeam") { config.beam_tint = services.get<Styles>().rgbas["grey"]; }
101+
99102
auto GunBeam::fire(glm::vec2 const muzzle_position) -> std::unique_ptr<Round> {
100103
if (!is_idle() || m_reload_remain > 0s) { return {}; }
101104

src/spaced/spaced/game/weapons/gun_beam.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class GunBeam final : public Weapon {
1212
float dps{1.0f};
1313
};
1414

15-
explicit GunBeam(Services const& services) : Weapon(services, "DeathRay") {}
15+
explicit GunBeam(Services const& services);
1616

1717
auto fire(glm::vec2 muzzle_position) -> std::unique_ptr<Round> final;
1818
[[nodiscard]] auto is_idle() const -> bool final { return m_fire_remain <= 0s; }

src/spaced/spaced/game/weapons/gun_kinetic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#include <bave/imgui/im_text.hpp>
22
#include <spaced/game/weapons/gun_kinetic.hpp>
3+
#include <spaced/services/styles.hpp>
34

45
namespace spaced {
56
using bave::im_text;
67
using bave::Rgba;
78
using bave::Seconds;
89

9-
GunKinetic::GunKinetic(Services const& services) : Weapon(services, "GunKinetic") {}
10+
GunKinetic::GunKinetic(Services const& services) : Weapon(services, "GunKinetic") { projectile_config.tint = services.get<Styles>().rgbas["black"]; }
1011

1112
auto GunKinetic::fire(glm::vec2 const muzzle_position) -> std::unique_ptr<Round> {
1213
if (m_reload_remain > 0s) { return {}; }

src/spaced/spaced/game/weapons/projectile.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ class Projectile : public IWeaponRound {
88
public:
99
struct Config {
1010
std::shared_ptr<bave::Texture> texture{};
11-
glm::vec2 size{60.0f, 5.0f};
12-
float corner_radius{2.5f};
11+
glm::vec2 size{60.0f, 10.0f};
12+
float corner_radius{0.5f * size.y};
1313
float x_speed{1500.0f};
14-
bave::Rgba tint{bave::white_v};
14+
bave::Rgba tint{bave::black_v};
1515
float damage{1.0f};
1616
};
1717

src/spaced/spaced/scenes/game.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <spaced/scenes/game.hpp>
77
#include <spaced/scenes/home.hpp>
88
#include <spaced/services/scene_switcher.hpp>
9+
#include <spaced/services/styles.hpp>
910

1011
namespace spaced {
1112
using bave::Action;
@@ -33,7 +34,9 @@ namespace {
3334
}
3435
} // namespace
3536

36-
Game::Game(App& app, Services const& services) : Scene(app, services, "Game"), m_player(services, make_player_controller(services)) {}
37+
Game::Game(App& app, Services const& services) : Scene(app, services, "Game"), m_player(services, make_player_controller(services)) {
38+
clear_colour = services.get<Styles>().rgbas["mocha"];
39+
}
3740

3841
void Game::on_focus(bave::FocusChange const& focus_change) { m_player.on_focus(focus_change); }
3942

src/spaced/spaced/services/styles.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
#include <bave/json_io.hpp>
12
#include <spaced/services/styles.hpp>
23
#include <spaced/util.hpp>
34

4-
namespace spaced::ui {
5+
namespace spaced {
56
namespace {
7+
using bave::from_json;
8+
using bave::to_json;
69
using util::from_json;
710
using util::to_json;
811

912
template <typename Type>
10-
void load_styles(Styles::Map<Type>& out, dj::Json const& json) {
13+
void load_styles(StringMap<Type>& out, dj::Json const& json) {
1114
for (auto const& [id, in_style] : json.object_view()) {
1215
if (id.empty()) { continue; }
1316
auto out_style = Type{};
@@ -17,41 +20,33 @@ void load_styles(Styles::Map<Type>& out, dj::Json const& json) {
1720
}
1821

1922
template <typename Type>
20-
void save_styles(Styles::Map<Type> const& styles, dj::Json& out, std::string_view const key) {
23+
void save_styles(StringMap<Type> const& styles, dj::Json& out, std::string_view const key) {
2124
auto out_styles = dj::Json{};
2225
for (auto const& [id, button_style] : styles) {
2326
if (id.empty()) { continue; }
2427
to_json(out_styles[id], button_style);
2528
}
2629
if (out_styles) { out[key] = std::move(out_styles); }
2730
}
28-
29-
template <typename Type>
30-
[[nodiscard]] auto get_or_default(Styles::Map<Type> const& map, std::string_view const id) -> Type const& {
31-
if (auto const it = map.find(id); it != map.end()) { return it->second; }
32-
static auto const fallback = Type{};
33-
return fallback;
34-
}
3531
} // namespace
3632

3733
auto Styles::load(dj::Json const& json) -> Styles {
3834
auto ret = Styles{};
35+
load_styles(ret.rgbas, json["rgbas"]);
3936
load_styles(ret.buttons, json["buttons"]);
4037
load_styles(ret.progress_bars, json["progress_bars"]);
4138
if (auto const& loading_screen = json["loading_screen"]) { from_json(loading_screen, ret.loading_screen); }
4239
if (auto const& collect_list_item = json["collect_list_item"]) { from_json(collect_list_item, ret.collect_list_item); }
4340
return ret;
4441
}
4542

46-
auto Styles::get_button(std::string_view id) const -> ButtonStyle const& { return get_or_default(buttons, id); }
47-
auto Styles::get_progress_bar(std::string_view id) const -> ProgressBarStyle const& { return get_or_default(progress_bars, id); }
48-
4943
auto Styles::save() const -> dj::Json {
5044
auto ret = dj::Json{};
45+
save_styles(rgbas, ret, "rgbas");
5146
save_styles(buttons, ret, "buttons");
5247
save_styles(progress_bars, ret, "progress_bars");
5348
to_json(ret["loading_screen"], loading_screen);
5449
to_json(ret["collect_list_item"], collect_list_item);
5550
return ret;
5651
}
57-
} // namespace spaced::ui
52+
} // namespace spaced

src/spaced/spaced/services/styles.hpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,22 @@
11
#pragma once
2-
#include <bave/core/string_hash.hpp>
32
#include <spaced/services/service.hpp>
3+
#include <spaced/string_map.hpp>
44
#include <spaced/ui/style.hpp>
5-
#include <unordered_map>
65

76
namespace dj {
87
class Json;
98
}
109

11-
namespace spaced::ui {
10+
namespace spaced {
1211
struct Styles : IService {
13-
template <typename Type>
14-
using Map = std::unordered_map<std::string, Type, bave::StringHash, std::equal_to<>>;
15-
16-
Map<ButtonStyle> buttons{};
17-
Map<ProgressBarStyle> progress_bars{};
18-
CollectListItemStyle collect_list_item{};
19-
LoadingScreenStyle loading_screen{};
12+
StringMap<bave::Rgba> rgbas{};
13+
StringMap<ui::ButtonStyle> buttons{};
14+
StringMap<ui::ProgressBarStyle> progress_bars{};
15+
ui::CollectListItemStyle collect_list_item{};
16+
ui::LoadingScreenStyle loading_screen{};
2017

2118
static auto load(dj::Json const& json) -> Styles;
2219

23-
[[nodiscard]] auto get_button(std::string_view id) const -> ButtonStyle const&;
24-
[[nodiscard]] auto get_progress_bar(std::string_view id) const -> ProgressBarStyle const&;
25-
2620
[[nodiscard]] auto save() const -> dj::Json;
2721
};
28-
} // namespace spaced::ui
22+
} // namespace spaced

src/spaced/spaced/spaced.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ void Spaced::load_resources() {
109109
resources->spinner = loader.load_texture("images/spinner.png", true);
110110
m_services.bind<Resources>(std::move(resources));
111111

112-
auto styles = std::make_unique<ui::Styles>();
112+
auto styles = std::make_unique<Styles>();
113113
if (auto const json = loader.load_json("styles.json")) {
114-
*styles = ui::Styles::load(json);
114+
*styles = Styles::load(json);
115115
m_log.info("loaded Styles from 'styles.json'");
116116
}
117117

@@ -122,7 +122,7 @@ void Spaced::load_resources() {
122122
json.to_file("styles.json");
123123
}
124124
}
125-
m_services.bind<ui::Styles>(std::move(styles));
125+
m_services.bind<Styles>(std::move(styles));
126126
}
127127

128128
void Spaced::set_layout() {

src/spaced/spaced/string_map.hpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <bave/core/string_hash.hpp>
3+
#include <unordered_map>
4+
5+
namespace spaced {
6+
template <typename ValueType>
7+
class StringMap : public std::unordered_map<std::string, ValueType, bave::StringHash, std::equal_to<>> {
8+
public:
9+
[[nodiscard]] auto get_or(std::string_view const key, ValueType const& fallback) const -> ValueType {
10+
if (auto const it = this->find(key); it != this->end()) { return it->second; }
11+
return fallback;
12+
}
13+
14+
[[nodiscard]] auto get(std::string_view const key) const -> ValueType
15+
requires(std::is_default_constructible_v<ValueType>)
16+
{
17+
return get_or(key, ValueType{});
18+
}
19+
20+
[[nodiscard]] auto operator[](std::string_view const key) const -> ValueType
21+
requires(std::is_default_constructible_v<ValueType>)
22+
{
23+
return get(key);
24+
}
25+
};
26+
} // namespace spaced

src/spaced/spaced/ui/button.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using bave::TextHeight;
1818
Button::Button(Services const& services) : m_layout(&services.get<ILayout>()), m_styles(&services.get<Styles>()) {
1919
m_text.set_font(services.get<Resources>().main_font);
2020
set_text_height(TextHeight::eDefault);
21-
set_style(m_styles->get_button("default"));
21+
set_style(m_styles->buttons["default"]);
2222
}
2323

2424
void Button::set_position(glm::vec2 const position) {

src/spaced/spaced/ui/progress_bar.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace spaced::ui {
55
using bave::Shader;
66

7-
ProgressBar::ProgressBar(Services const& services) : m_style(services.get<Styles>().get_progress_bar("default")) {}
7+
ProgressBar::ProgressBar(Services const& services) : m_style(services.get<Styles>().progress_bars["default"]) {}
88

99
void ProgressBar::set_progress(float const progress) {
1010
m_progress = progress;

0 commit comments

Comments
 (0)