Skip to content

Commit 1b034a1

Browse files
authored
Add Rgba map to Styles. Use for tints of weapon rounds. (#19)
* Add Rgba map to `Styles`. Use for tints of weapon rounds. * Exclude string_map.hpp from autoformat.
1 parent 5c4217f commit 1b034a1

File tree

17 files changed

+71
-91
lines changed

17 files changed

+71
-91
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 & 16 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,31 @@ 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); }
42-
if (auto const& collect_list_item = json["collect_list_item"]) { from_json(collect_list_item, ret.collect_list_item); }
4339
return ret;
4440
}
4541

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-
4942
auto Styles::save() const -> dj::Json {
5043
auto ret = dj::Json{};
44+
save_styles(rgbas, ret, "rgbas");
5145
save_styles(buttons, ret, "buttons");
5246
save_styles(progress_bars, ret, "progress_bars");
5347
to_json(ret["loading_screen"], loading_screen);
54-
to_json(ret["collect_list_item"], collect_list_item);
5548
return ret;
5649
}
57-
} // namespace spaced::ui
50+
} // namespace spaced

src/spaced/spaced/services/styles.hpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
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::LoadingScreenStyle loading_screen{};
2016

2117
static auto load(dj::Json const& json) -> Styles;
2218

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-
2619
[[nodiscard]] auto save() const -> dj::Json;
2720
};
28-
} // namespace spaced::ui
21+
} // 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;

src/spaced/spaced/ui/style.hpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,4 @@ struct ProgressBarStyle {
3838
bave::Rgba outline{bave::red_v};
3939
float outline_width{5.0f};
4040
};
41-
42-
struct CollectListItemStyle {
43-
float panel_diameter{60.0f};
44-
bave::Rgba panel_rgba{bave::white_v};
45-
float outline_width{5.0f};
46-
bave::Rgba outline_rgba{bave::red_v};
47-
glm::vec2 sprite_size{40.0f};
48-
float panel_y_offset{12.0f};
49-
bave::TextHeight text_height = bave::TextHeight{20};
50-
bave::Rgba text_rgba{bave::black_v};
51-
float text_y_offset{-43.0f};
52-
float item_x_pad{100.0f};
53-
};
5441
} // namespace spaced::ui

src/spaced/spaced/util.cpp

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,34 +71,6 @@ void util::from_json(dj::Json const& json, ui::ProgressBarStyle& out) {
7171
out.outline_width = json["outline_width"].as<float>();
7272
}
7373

74-
void util::to_json(dj::Json& out, ui::CollectListItemStyle const& collect_list_item_style) {
75-
using bave::to_json;
76-
out["panel_diameter"] = collect_list_item_style.panel_diameter;
77-
to_json(out["panel_rgba"], collect_list_item_style.panel_rgba);
78-
out["outline_width"] = collect_list_item_style.outline_width;
79-
to_json(out["outline_rgba"], collect_list_item_style.outline_rgba);
80-
to_json(out["sprite_size"], collect_list_item_style.sprite_size);
81-
out["panel_y_offset"] = collect_list_item_style.panel_y_offset;
82-
out["text_height"] = static_cast<int>(collect_list_item_style.text_height);
83-
to_json(out["text_rgba"], collect_list_item_style.text_rgba);
84-
out["text_y_offset"] = collect_list_item_style.text_y_offset;
85-
out["item_x_pad"] = collect_list_item_style.item_x_pad;
86-
}
87-
88-
void util::from_json(dj::Json const& json, ui::CollectListItemStyle& out) {
89-
using bave::from_json;
90-
out.panel_diameter = json["panel_diameter"].as<float>(out.panel_diameter);
91-
from_json(json["panel_rgba"], out.panel_rgba);
92-
out.outline_width = json["outline_width"].as<float>();
93-
from_json(json["outline_rgba"], out.outline_rgba);
94-
from_json(json["sprite_size"], out.sprite_size);
95-
out.panel_y_offset = json["panel_y_offset"].as<float>();
96-
out.text_height = bave::TextHeight{json["text_height"].as<int>()};
97-
from_json(json["text_rgba"], out.text_rgba);
98-
out.text_y_offset = json["text_y_offset"].as<float>();
99-
out.item_x_pad = json["item_x_pad"].as<float>();
100-
}
101-
10274
auto util::create_font_atlas_task(std::shared_ptr<bave::Font> font, std::vector<bave::TextHeight> heights) -> std::function<void()> {
10375
if (!font || heights.empty()) { return {}; }
10476
return [font = std::move(font), heights = std::move(heights)] {

src/spaced/spaced/util.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,5 @@ void from_json(dj::Json const& json, ui::ButtonStyle& out);
1717
void to_json(dj::Json& out, ui::ProgressBarStyle const& progress_bar_style);
1818
void from_json(dj::Json const& json, ui::ProgressBarStyle& out);
1919

20-
void to_json(dj::Json& out, ui::CollectListItemStyle const& collect_list_item_style);
21-
void from_json(dj::Json const& json, ui::CollectListItemStyle& out);
22-
2320
auto create_font_atlas_task(std::shared_ptr<bave::Font> font, std::vector<bave::TextHeight> heights) -> std::function<void()>;
2421
} // namespace spaced::util

tools/format_code.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#!/bin/bash
1+
#!/bin/bash
22

33
[[ ! $(clang-format --version) ]] && exit 1
44

5-
exclude_list="scene_switcher.hpp"
5+
exclude_list="scene_switcher.hpp string_map.hpp"
66

77
script_path=${0%/*}
88
tools_root=${script_path%/*}
@@ -15,7 +15,7 @@ fi
1515

1616
exclude_cmd=
1717
for exclude in $exclude_list; do
18-
exclude_cmd+="! -name $exclude"
18+
exclude_cmd+="! -name $exclude "
1919
done
2020

2121
files=$(find src/spaced src/android/app/src/main src/desktop -name "*.?pp" $exclude_cmd)

0 commit comments

Comments
 (0)