Porytiles
Loading...
Searching...
No Matches
layer.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <cctype>
4#include <cstdint>
5#include <format>
6#include <optional>
7#include <ostream>
8#include <string>
9
12
13namespace porytiles {
14
20enum class LayerMode { dual, triple };
21
29[[nodiscard]] inline LayerMode layer_mode_from_val(std::size_t s)
30{
31 if (s == 8) {
32 return LayerMode::dual;
33 }
34 if (s == 12) {
35 return LayerMode::triple;
36 }
37 panic("invalid LayerMode integer: " + std::to_string(s));
38}
39
44[[nodiscard]] inline std::optional<LayerMode> layer_mode_from_str(const std::string &s)
45{
46 if (s == "dual") {
47 return LayerMode::dual;
48 }
49 if (s == "triple") {
50 return LayerMode::triple;
51 }
52 return std::nullopt;
53}
54
59[[nodiscard]] inline std::string to_string(LayerMode mode)
60{
61 switch (mode) {
62 case LayerMode::dual:
63 return "dual";
65 return "triple";
66 }
67 panic("unhandled LayerMode value");
68}
69
75inline std::ostream &operator<<(std::ostream &os, const LayerMode mode)
76{
77 return os << to_string(mode);
78}
79
86enum class LayerType : unsigned int { normal = 0, covered = 1, split = 2 };
87
92[[nodiscard]] inline std::string to_string(LayerType layer_type)
93{
94 switch (layer_type) {
96 return "Normal - Middle/Top";
98 return "Covered - Bottom/Middle";
100 return "Split - Bottom/Top";
101 default:
102 panic("to_string(LayerType) unknown LayerType");
103 }
104}
105
110[[nodiscard]] inline ChainableResult<LayerType> layer_type_from_int(unsigned int i)
111{
112 if (i > static_cast<unsigned int>(LayerType::split)) {
113 return FormattableError{
114 "invalid layer type integer value '{}': must be 0, 1, or 2", FormatParam{i, Style::bold}};
115 }
116 return static_cast<LayerType>(i);
117}
118
128[[nodiscard]] inline std::string layer_type_csv_token(LayerType layer_type)
129{
130 switch (layer_type) {
132 return "normal";
134 return "covered";
135 case LayerType::split:
136 return "split";
137 default:
138 panic("layer_type_csv_token unknown LayerType");
139 }
140}
141
150[[nodiscard]] inline ChainableResult<LayerType> layer_type_from_csv_token(const std::string &token)
151{
152 std::string lower;
153 lower.reserve(token.size());
154 for (const unsigned char c : token) {
155 lower.push_back(static_cast<char>(std::tolower(c)));
156 }
157
158 if (lower == "normal") {
159 return LayerType::normal;
160 }
161 if (lower == "covered") {
162 return LayerType::covered;
163 }
164 if (lower == "split") {
165 return LayerType::split;
166 }
167 return FormattableError{
168 "invalid layer_type token '{}': must be 'normal', 'covered', or 'split'", FormatParam{token, Style::bold}};
169}
170
171} // namespace porytiles
172
173template <>
174struct std::formatter<porytiles::LayerMode> {
175 constexpr auto parse(std::format_parse_context &ctx)
176 {
177 return ctx.begin();
178 }
179
180 auto format(const porytiles::LayerMode &value, auto &ctx) const
181 {
182 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
183 }
184};
185
186template <>
187struct std::formatter<porytiles::LayerType> {
188 constexpr auto parse(std::format_parse_context &ctx)
189 {
190 return ctx.begin();
191 }
192
193 auto format(const porytiles::LayerType &value, auto &ctx) const
194 {
195 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
196 }
197};
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
static const Style bold
Bold text formatting.
std::optional< LayerMode > layer_mode_from_str(const std::string &s)
Converts a string to LayerMode.
Definition layer.hpp:44
LayerMode
Specifies whether a metatile uses dual-layer or triple-layer mode.
Definition layer.hpp:20
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
std::string layer_type_csv_token(LayerType layer_type)
Converts a LayerType to its lowercase CSV token.
Definition layer.hpp:128
LayerMode layer_mode_from_val(std::size_t s)
Converts a numeric value to LayerMode.
Definition layer.hpp:29
ChainableResult< LayerType > layer_type_from_int(unsigned int i)
Converts an integer to LayerType.
Definition layer.hpp:110
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
ChainableResult< LayerType > layer_type_from_csv_token(const std::string &token)
Parses a CSV layer_type token into a LayerType.
Definition layer.hpp:150
LayerType
Specifies which layers of a metatile are used for rendering.
Definition layer.hpp:86
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
@ split
Split the animation into separate animations for each palette variant (not yet implemented).
auto format(const porytiles::LayerMode &value, auto &ctx) const
Definition layer.hpp:180
constexpr auto parse(std::format_parse_context &ctx)
Definition layer.hpp:175
constexpr auto parse(std::format_parse_context &ctx)
Definition layer.hpp:188
auto format(const porytiles::LayerType &value, auto &ctx) const
Definition layer.hpp:193