Compare commits

...

45 Commits

Author SHA1 Message Date
19a4120113 [studio/modlib] Make iterateSelection return errors properly
All checks were successful
Build / build (push) Successful in 2m29s
2024-05-25 22:33:01 -05:00
f69b8afa62 [nostalgia] Remove use of deleted function 2024-05-25 21:34:08 -05:00
9c98b5e2c2 [studio/modlib] Remove color.hpp 2024-05-25 21:33:27 -05:00
1f87216da4 [nostalgia/core] Add applySelectionColor 2024-05-25 21:32:57 -05:00
94c59604ee [nostalgia/core/opengl] Fix for Ox changes 2024-05-25 21:32:18 -05:00
8ee016c130 [studio/modlib] Add SelectionTracker 2024-05-25 21:31:47 -05:00
dc20c66797 [ox/std] Add conversion functions for geo types, cleanup 2024-05-25 21:29:54 -05:00
407e54246f [ox/std] Remove SmallMap dtor, replace timing code with steady_clock 2024-05-25 00:47:49 -05:00
3b18869665 [ox/claw] Remove enum type from ClawFormat 2024-05-24 22:09:08 -05:00
0fab6c7c20 [ox/preloader] Remove debug code 2024-05-24 22:08:35 -05:00
a72b865dc9 [studio/modlib] Add function for exporting selection color 2024-05-24 22:06:48 -05:00
c0479604aa [studio,nostalgia/studio] Make executing UndoCommands report errors
All checks were successful
Build / build (push) Successful in 2m31s
2024-05-23 21:50:27 -05:00
a1c89906bd [nostalgia/studio] Make UndoCommand undo/redo return ox::Error
All checks were successful
Build / build (push) Successful in 2m29s
2024-05-23 21:29:57 -05:00
7fb0549c25 [nostalgia/core] Revert some auto formatting done by CLion...
All checks were successful
Build / build (push) Successful in 2m28s
2024-05-23 21:01:10 -05:00
37e65ab0a6 [nostalgia/core/studio] Fix Subsheet width to update properly
All checks were successful
Build / build (push) Successful in 2m30s
2024-05-23 20:52:55 -05:00
9105b1ece6 [ox/std] Fix Linux build
All checks were successful
Build / build (push) Successful in 2m29s
2024-05-23 01:38:57 -05:00
fbeb08156a [ox/model] Fix type params in buildTypeId 2024-05-23 01:32:41 -05:00
b882a47e58 [ox/std] Fix resize to set null terminator 2024-05-23 01:27:59 -05:00
660f2f5633 [ox/std] Rework FileReader into StreamReader 2024-05-23 00:45:28 -05:00
aa83c2a62b [nostalgia/core/studio] Remove some unnecessary copying 2024-05-22 23:19:01 -05:00
4a2b1fd743 [studio,keel] Make fileChanged emit UUID as well as path, add uuidUrlToUuid 2024-05-22 23:17:28 -05:00
08f958fbac [ox/std] Add IntegerRange_c 2024-05-22 23:14:06 -05:00
a651d45a2f [ox/std] Fix Vector insert functions 2024-05-22 23:13:52 -05:00
9e9f317c13 [studio] Make UndoCommand::mergeWith take a reference 2024-05-22 02:11:58 -05:00
f5a02ce94f [nostalgia/core/gba] Fix build 2024-05-22 00:41:07 -05:00
6971c3109a [studio] Add NoChangeException 2024-05-22 00:35:20 -05:00
c47f48eba7 [keel] Add/cleanup UUID/path lookup functions 2024-05-22 00:32:49 -05:00
76771e7bdd [nostalgia/core] Add tileColumns and tileRows functions 2024-05-22 00:31:10 -05:00
f6a0ae2027 [ox/std] Fix some Windows warnings 2024-05-22 00:26:56 -05:00
752c8c1db3 [glutils] Fix type conversion that happened on Windows 2024-05-22 00:26:03 -05:00
af3bff1a3f [glutils] Add FrameBuffer::sizef 2024-05-22 00:25:03 -05:00
87416e13ee [ox/std] Make MallocaPtr call destructor 2024-05-20 02:46:19 -05:00
047b43968e [ox/std] Make Point and Size members int32_t 2024-05-20 02:06:27 -05:00
40b8da4d3c [studio/modlib] Cleanup 2024-05-19 22:15:54 -05:00
123c41254f [ox/std] Add SmallMap::pairs(), SmallMap model 2024-05-19 22:15:13 -05:00
963ec5d3f7 [ox/std] Add operator-> to SpanIterator 2024-05-19 22:14:28 -05:00
6df77a23ab [glutils] Add size function to FrameBuffer 2024-05-19 22:13:57 -05:00
df412cf8d0 [ox/std] Add missing typenames 2024-05-18 13:01:06 -05:00
ae30ef3609 Merge commit 'b66cef7127e97269fc6072a6f66ccc08990f6d2e' 2024-05-18 12:17:23 -05:00
095a1135e7 Merge commit 'f48824793cfce315971fe2e699ece198c7a79407' 2024-05-11 02:09:10 -05:00
ce1836ab10 Merge commit '1e041bd2ebfe5ace7bed3906faf60345aa98a8bc' 2024-05-10 02:21:42 -05:00
7d1641faee Merge commit '420fa96463f59c4a4a7cd66b16b0ad01ab0d55e6' 2024-05-08 23:55:07 -05:00
423212b2ad [studio] Add missing include 2024-05-06 22:27:20 -05:00
60da1063d1 Merge commit 'bd416f82e25f1b710ab2b7890274571dd3fcd53d' 2024-05-03 23:42:00 -05:00
60d1996f0a [keel] Minor optimization 2024-05-03 23:00:38 -05:00
66 changed files with 589 additions and 251 deletions

View File

@@ -13,6 +13,7 @@
#include <ox/std/error.hpp> #include <ox/std/error.hpp>
#include <ox/std/size.hpp> #include <ox/std/size.hpp>
#include <ox/std/string.hpp> #include <ox/std/string.hpp>
#include <ox/std/vec.hpp>
#include <ox/std/vector.hpp> #include <ox/std/vector.hpp>
namespace glutils { namespace glutils {
@@ -137,6 +138,22 @@ struct FrameBuffer {
constexpr operator const GLuint&() const noexcept { constexpr operator const GLuint&() const noexcept {
return fbo.id; return fbo.id;
} }
[[nodiscard]]
constexpr ox::Vec2 sizef() const noexcept {
return {
static_cast<float>(width),
static_cast<float>(height),
};
}
[[nodiscard]]
constexpr ox::Size size() const noexcept {
return {
width,
height,
};
}
}; };
class FrameBufferBind { class FrameBufferBind {

View File

@@ -102,7 +102,7 @@ void setupShaderParams(
ox::Vector<ShaderVarSet> const&vars, ox::Vector<ShaderVarSet> const&vars,
GLsizei vertexRowLen) noexcept { GLsizei vertexRowLen) noexcept {
// setup vars // setup vars
for (auto lenWritten = 0LU; auto const&v : vars) { for (size_t lenWritten = 0; auto const&v : vars) {
auto const attr = static_cast<GLuint>(glGetAttribLocation(shader, v.name.c_str())); auto const attr = static_cast<GLuint>(glGetAttribLocation(shader, v.name.c_str()));
glEnableVertexAttribArray(attr); glEnableVertexAttribArray(attr);
glVertexAttribPointer( glVertexAttribPointer(

View File

@@ -10,7 +10,7 @@
namespace ox { namespace ox {
enum class ClawFormat: int { enum class ClawFormat {
None, None,
Metal, Metal,
Organic, Organic,

View File

@@ -34,15 +34,16 @@ constexpr auto buildTypeId() noexcept {
return ox::sfmt("{};{}", name, version); return ox::sfmt("{};{}", name, version);
} }
static constexpr auto buildTypeId(CRStringView name, int version, static constexpr auto buildTypeId(
const TypeParamPack &typeParams = {}) noexcept { CRStringView name, int version,
const TypeParamPack &typeParams = {}) noexcept {
String tp; String tp;
if (!typeParams.empty()) { if (!typeParams.empty()) {
tp = "#"; tp = "#";
for (const auto &p : typeParams) { for (const auto &p : typeParams) {
tp += p + ","; tp += p + ",";
} }
tp.resize(tp.len() - 1); tp.resize(tp.len());
tp += "#"; tp += "#";
} }
return ox::sfmt("{}{};{}", name, tp, version); return ox::sfmt("{}{};{}", name, tp, version);

View File

@@ -381,8 +381,6 @@ constexpr ox::Error Preloader<PlatSpec>::fieldVector(
template<typename PlatSpec> template<typename PlatSpec>
constexpr ox::Error Preloader<PlatSpec>::fieldArray(CRStringView, ox::ModelValueArray const*val) noexcept { constexpr ox::Error Preloader<PlatSpec>::fieldArray(CRStringView, ox::ModelValueArray const*val) noexcept {
oxDebugf("array size: {}", val->size());
oxDebugf("array sizeOf: {}", sizeOf<PlatSpec>(val));
oxReturnError(pad(&(*val)[0])); oxReturnError(pad(&(*val)[0]));
for (auto const&v : *val) { for (auto const&v : *val) {
oxReturnError(this->interface()->field({}, &v)); oxReturnError(this->interface()->field({}, &v));

View File

@@ -96,6 +96,7 @@ install(
buildinfo.hpp buildinfo.hpp
byteswap.hpp byteswap.hpp
concepts.hpp concepts.hpp
conv.hpp
def.hpp def.hpp
defer.hpp defer.hpp
defines.hpp defines.hpp

View File

@@ -32,7 +32,7 @@ constexpr To bit_cast(const From &src) noexcept requires(sizeof(To) == sizeof(Fr
namespace ox { namespace ox {
template<typename To, typename From> template<typename To, typename From>
constexpr typename enable_if<sizeof(To) == sizeof(From), To>::type cbit_cast(From src) noexcept { constexpr To cbit_cast(From src) noexcept requires(sizeof(To) == sizeof(From)) {
To dst = {}; To dst = {};
ox::memcpy(&dst, &src, sizeof(src)); ox::memcpy(&dst, &src, sizeof(src));
return dst; return dst;

View File

@@ -8,6 +8,7 @@
#pragma once #pragma once
#include "bit.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
namespace ox { namespace ox {
@@ -29,4 +30,6 @@ concept OxString_c = isOxString_v<T>;
template<typename T> template<typename T>
concept Integral_c = ox::is_integral_v<T>; concept Integral_c = ox::is_integral_v<T>;
template<typename T, size_t max>
concept IntegerRange_c = ox::is_integer_v<T> && ox::MaxValue<T> >= max;
} }

45
deps/ox/src/ox/std/conv.hpp vendored Normal file
View File

@@ -0,0 +1,45 @@
/*
* Copyright 2015 - 2024 gary@drinkingtea.net
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#pragma once
#include "point.hpp"
#include "size.hpp"
#include "vec.hpp"
namespace ox {
constexpr Vec2::operator Point() const noexcept {
return {
static_cast<int32_t>(x),
static_cast<int32_t>(y),
};
}
constexpr Vec2::operator Size() const noexcept {
return {
static_cast<int32_t>(x),
static_cast<int32_t>(y),
};
}
constexpr Point::operator Vec2() const noexcept {
return {
static_cast<float>(x),
static_cast<float>(y),
};
}
constexpr Size::operator Vec2() const noexcept {
return {
static_cast<float>(width),
static_cast<float>(height),
};
}
}

View File

@@ -128,6 +128,10 @@ struct SpanIterator {
return operator-=(1); return operator-=(1);
} }
constexpr PtrType operator->() const noexcept {
return &m_t[m_offset];
}
constexpr RefType operator*() const noexcept { constexpr RefType operator*() const noexcept {
return m_t[m_offset]; return m_t[m_offset];
} }

View File

@@ -102,6 +102,7 @@ class MallocaPtr {
} }
constexpr ~MallocaPtr() noexcept { constexpr ~MallocaPtr() noexcept {
m_val->~T();
if (m_onHeap && m_val) { if (m_onHeap && m_val) {
delete[] reinterpret_cast<uint8_t*>(m_val); delete[] reinterpret_cast<uint8_t*>(m_val);
} }

View File

@@ -17,8 +17,8 @@ class Point {
public: public:
static constexpr auto TypeName = "net.drinkingtea.ox.Point"; static constexpr auto TypeName = "net.drinkingtea.ox.Point";
static constexpr auto TypeVersion = 1; static constexpr auto TypeVersion = 1;
int x = 0; int32_t x = 0;
int y = 0; int32_t y = 0;
constexpr Point() noexcept = default; constexpr Point() noexcept = default;
@@ -64,6 +64,7 @@ class Point {
constexpr bool operator!=(const Point&) const noexcept; constexpr bool operator!=(const Point&) const noexcept;
explicit constexpr operator class Vec2() const noexcept;
}; };
constexpr Point::Point(int x, int y) noexcept { constexpr Point::Point(int x, int y) noexcept {

View File

@@ -8,7 +8,7 @@
#ifdef OX_USE_STDLIB #ifdef OX_USE_STDLIB
#include <cstdio> #include <istream>
#include "array.hpp" #include "array.hpp"
#include "reader.hpp" #include "reader.hpp"
@@ -16,41 +16,56 @@
namespace ox { namespace ox {
[[nodiscard]] [[nodiscard]]
constexpr int sdMap(ox::ios_base::seekdir in) noexcept { constexpr std::ios_base::seekdir sdMap(ox::ios_base::seekdir in) noexcept {
switch (in) { switch (in) {
case ox::ios_base::beg: case ox::ios_base::beg:
return SEEK_SET; return std::ios_base::beg;
case ox::ios_base::end: case ox::ios_base::end:
return SEEK_END; return std::ios_base::end;
case ox::ios_base::cur: case ox::ios_base::cur:
return SEEK_CUR; return std::ios_base::cur;
} }
return -1; return std::ios_base::beg;
} }
ox::Result<char> FileReader::peek() const noexcept { ox::Result<char> StreamReader::peek() const noexcept {
auto const c = fgetc(m_file); try {
auto const ok = c != EOF; char c{};
if (ok && ungetc(c, m_file)) [[unlikely]] { m_strm.get(c);
return OxError(1, "Unable to unget character"); auto const ok = c != EOF;
if (ok && m_strm.unget()) [[unlikely]] {
return OxError(1, "Unable to unget character");
}
return {static_cast<char>(c), OxError(!ok, "File peek failed")};
} catch (std::exception const&) {
return OxError(1, "peek failed");
} }
return {static_cast<char>(c), OxError(!ok, "File peek failed")};
} }
ox::Result<std::size_t> FileReader::read(char *v, std::size_t cnt) noexcept { ox::Result<std::size_t> StreamReader::read(char *v, std::size_t cnt) noexcept {
return fread(v, 1, cnt, m_file); return static_cast<size_t>(m_strm.read(v, static_cast<std::streamsize>(cnt)).gcount());
} }
ox::Error FileReader::seekg(std::size_t p) noexcept { ox::Error StreamReader::seekg(std::size_t p) noexcept {
return OxError(fseek(m_file, static_cast<int64_t>(p), SEEK_CUR) != 0); try {
m_strm.seekg(static_cast<long long int>(p), std::ios_base::cur);
} catch (std::exception const&) {
return OxError(1, "seekg failed");
}
return {};
} }
ox::Error FileReader::seekg(int64_t p, ios_base::seekdir sd) noexcept { ox::Error StreamReader::seekg(int64_t p, ios_base::seekdir sd) noexcept {
return OxError(fseek(m_file, p, sdMap(sd)) != 0); try {
m_strm.seekg(p, sdMap(sd));
} catch (std::exception const&) {
return OxError(1, "seekg failed");
}
return {};
} }
ox::Result<std::size_t> FileReader::tellg() noexcept { ox::Result<std::size_t> StreamReader::tellg() noexcept {
const auto sz = ftell(m_file); const auto sz = m_strm.tellg();
return {static_cast<std::size_t>(sz), OxError(sz == -1)}; return {static_cast<std::size_t>(sz), OxError(sz == -1)};
} }

View File

@@ -9,7 +9,7 @@
#pragma once #pragma once
#ifdef OX_USE_STDLIB #ifdef OX_USE_STDLIB
#include <cstdio> #include <istream>
#endif #endif
#include "concepts.hpp" #include "concepts.hpp"
@@ -65,11 +65,11 @@ class ReaderT: public Reader_v {
}; };
#ifdef OX_USE_STDLIB #ifdef OX_USE_STDLIB
class FileReader: public Reader_v { class StreamReader: public Reader_v {
private: private:
FILE *m_file = nullptr; std::istream &m_strm;
public: public:
constexpr explicit FileReader(FILE *file) noexcept: m_file(file) {} constexpr explicit StreamReader(std::istream &stream) noexcept: m_strm(stream) {}
ox::Result<char> peek() const noexcept override; ox::Result<char> peek() const noexcept override;
ox::Result<std::size_t> read(char *v, std::size_t cnt) noexcept override; ox::Result<std::size_t> read(char *v, std::size_t cnt) noexcept override;
ox::Error seekg(std::size_t p) noexcept override; ox::Error seekg(std::size_t p) noexcept override;

View File

@@ -17,8 +17,8 @@ class Size {
public: public:
static constexpr auto TypeName = "net.drinkingtea.ox.Size"; static constexpr auto TypeName = "net.drinkingtea.ox.Size";
static constexpr auto TypeVersion = 1; static constexpr auto TypeVersion = 1;
int width = 0; int32_t width = 0;
int height = 0; int32_t height = 0;
constexpr Size() noexcept = default; constexpr Size() noexcept = default;
@@ -64,6 +64,7 @@ class Size {
constexpr bool operator!=(const Size&) const noexcept; constexpr bool operator!=(const Size&) const noexcept;
explicit constexpr operator class Vec2() const noexcept;
}; };
constexpr Size::Size(int width, int height) noexcept { constexpr Size::Size(int width, int height) noexcept {

View File

@@ -8,7 +8,7 @@
#pragma once #pragma once
#include "algorithm.hpp" #include "def.hpp"
#include "hash.hpp" #include "hash.hpp"
#include "ignore.hpp" #include "ignore.hpp"
#include "stringview.hpp" #include "stringview.hpp"
@@ -28,7 +28,7 @@ class SmallMap {
T value{}; T value{};
}; };
private: protected:
using PairVector = Vector<Pair, SmallSz>; using PairVector = Vector<Pair, SmallSz>;
PairVector m_pairs; PairVector m_pairs;
@@ -39,8 +39,6 @@ class SmallMap {
constexpr SmallMap(SmallMap &&other) noexcept; constexpr SmallMap(SmallMap &&other) noexcept;
constexpr ~SmallMap();
constexpr bool operator==(SmallMap const&other) const; constexpr bool operator==(SmallMap const&other) const;
constexpr SmallMap &operator=(SmallMap const&other); constexpr SmallMap &operator=(SmallMap const&other);
@@ -79,6 +77,11 @@ class SmallMap {
[[nodiscard]] [[nodiscard]]
constexpr Pair &get(size_t i) noexcept; constexpr Pair &get(size_t i) noexcept;
[[nodiscard]]
constexpr ox::SpanView<Pair> pairs() const noexcept {
return m_pairs;
}
constexpr void clear(); constexpr void clear();
private: private:
@@ -100,11 +103,6 @@ constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&other) noe
m_pairs = std::move(other.m_pairs); m_pairs = std::move(other.m_pairs);
} }
template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T, SmallSz>::~SmallMap() {
clear();
}
template<typename K, typename T, size_t SmallSz> template<typename K, typename T, size_t SmallSz>
constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const { constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
return m_pairs == other.m_pairs; return m_pairs == other.m_pairs;
@@ -204,12 +202,12 @@ constexpr T &SmallMap<K, T, SmallSz>::value(size_t i) noexcept {
} }
template<typename K, typename T, size_t SmallSz> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::get(size_t i) const noexcept { constexpr typename SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::get(size_t i) const noexcept {
return m_pairs[i]; return m_pairs[i];
} }
template<typename K, typename T, size_t SmallSz> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::get(size_t i) noexcept { constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::get(size_t i) noexcept {
return m_pairs[i]; return m_pairs[i];
} }
@@ -246,4 +244,12 @@ constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::acces
return pairs.emplace_back(); return pairs.emplace_back();
} }
template<typename T, typename K, typename V, size_t SmallSz>
constexpr Error model(T *io, ox::CommonPtrWith<SmallMap<K, V, SmallSz>> auto *obj) noexcept {
using Map = SmallMap<K, V, SmallSz>;
oxReturnError(io->template setTypeInfo<Map>());
oxReturnError(io->field("pairs", &obj->m_pairs));
return {};
}
} }

View File

@@ -26,7 +26,7 @@
namespace ox { namespace ox {
#if defined(OX_USE_STDLIB) && __has_include(<unistd.h>) #if defined(OX_USE_STDLIB) && __has_include(<unistd.h>)
[[nodiscard]] [[nodiscard]] [[maybe_unused]]
static auto symbolicate([[maybe_unused]]void **frames, static auto symbolicate([[maybe_unused]]void **frames,
[[maybe_unused]]std::size_t frameCnt, [[maybe_unused]]std::size_t frameCnt,
[[maybe_unused]]const char *linePrefix) { [[maybe_unused]]const char *linePrefix) {

View File

@@ -16,6 +16,7 @@
#include "istring.hpp" #include "istring.hpp"
#include "byteswap.hpp" #include "byteswap.hpp"
#include "concepts.hpp" #include "concepts.hpp"
#include "conv.hpp"
#include "cstringview.hpp" #include "cstringview.hpp"
#include "cstrops.hpp" #include "cstrops.hpp"
#include "def.hpp" #include "def.hpp"

View File

@@ -194,6 +194,7 @@ class BasicString {
constexpr void resize(size_t sz) noexcept { constexpr void resize(size_t sz) noexcept {
m_buff.resize(sz); m_buff.resize(sz);
m_buff[sz - 1] = 0;
} }
[[nodiscard]] [[nodiscard]]

View File

@@ -18,12 +18,12 @@
#include <ox/std/std.hpp> #include <ox/std/std.hpp>
[[nodiscard]] [[nodiscard]]
static uint64_t nowMs() { static uint64_t steadyNowMs() {
#if __has_include(<chrono>) #if __has_include(<chrono>)
using namespace std::chrono; using namespace std::chrono;
return static_cast<uint64_t>( return static_cast<uint64_t>(
duration_cast<milliseconds>( duration_cast<milliseconds>(
system_clock::now().time_since_epoch()).count()); steady_clock::now().time_since_epoch()).count());
#else #else
return 0; return 0;
#endif #endif
@@ -41,13 +41,13 @@ uint64_t timeMapStrToUuid(int elemCnt, int lookups, uint64_t seed = 4321) noexce
auto const keys = map.keys(); auto const keys = map.keys();
ox::Random rand; ox::Random rand;
// start // start
auto const startTime = nowMs(); auto const startTime = steadyNowMs();
for (int i = 0; i < lookups; ++i) { for (int i = 0; i < lookups; ++i) {
auto const&k = keys[rand.gen() % keys.size()]; auto const&k = keys[rand.gen() % keys.size()];
map[k]; map[k];
oxExpect(map[k], ox::UUID::fromString(k).unwrap()); oxExpect(map[k], ox::UUID::fromString(k).unwrap());
} }
return nowMs() - startTime; return steadyNowMs() - startTime;
} }
template<typename Map = ox::SmallMap<ox::UUID, ox::String>> template<typename Map = ox::SmallMap<ox::UUID, ox::String>>
@@ -62,16 +62,16 @@ uint64_t timeMapUuidToStr(int elemCnt, int lookups, uint64_t seed = 4321) noexce
auto const keys = map.keys(); auto const keys = map.keys();
ox::Random rand; ox::Random rand;
// start // start
auto const startTime = nowMs(); auto const startTime = steadyNowMs();
for (int i = 0; i < lookups; ++i) { for (int i = 0; i < lookups; ++i) {
auto const&k = keys[rand.gen() % keys.size()]; auto const&k = keys[rand.gen() % keys.size()];
oxExpect(map[k], k.toString()); oxExpect(map[k], k.toString());
} }
return nowMs() - startTime; return steadyNowMs() - startTime;
} }
static ox::Error compareMaps(int lookupCnt = 1'000'000) { static ox::Error compareMaps(int lookupCnt = 1'000'000) {
auto const seed = nowMs(); auto const seed = steadyNowMs();
uint64_t hashTime{}; uint64_t hashTime{};
uint64_t smallTime{}; uint64_t smallTime{};
int elemCnt = 1; int elemCnt = 1;

View File

@@ -185,6 +185,8 @@ struct enable_if<true, T> {
using type = T; using type = T;
}; };
template<bool B, typename T>
using enable_if_t = typename enable_if<B, T>::type;
template<typename T> template<typename T>
struct is_pointer { struct is_pointer {

View File

@@ -23,17 +23,16 @@
namespace ox { namespace ox {
template<typename T> class Vec2 {
struct Vec {
public: public:
using value_type = T; using value_type = float;
using size_type = std::size_t; using size_type = std::size_t;
static constexpr auto TypeName = "net.drinkingtea.ox.Point"; static constexpr auto TypeName = "net.drinkingtea.ox.Point";
static constexpr auto TypeVersion = 1; static constexpr auto TypeVersion = 1;
T x = 0; float x = 0;
T y = 0; float y = 0;
template<typename RefType = value_type&, typename PtrType = value_type*, bool reverse = false> template<typename RefType = value_type&, typename PtrType = value_type*, bool reverse = false>
struct iterator: public ox::Iterator<std::bidirectional_iterator_tag, value_type> { struct iterator: public ox::Iterator<std::bidirectional_iterator_tag, value_type> {
@@ -141,14 +140,14 @@ struct Vec {
}; };
constexpr Vec() noexcept = default; constexpr Vec2() noexcept = default;
template<typename ...Args> template<typename ...Args>
constexpr Vec(T pX, T pY) noexcept: x(pX), y(pY) { constexpr Vec2(float pX, float pY) noexcept: x(pX), y(pY) {
} }
#if __has_include(<imgui.h>) #if __has_include(<imgui.h>)
explicit constexpr Vec(const ImVec2 &v) noexcept: Vec(v.x, v.y) { explicit constexpr Vec2(const ImVec2 &v) noexcept: Vec2(v.x, v.y) {
} }
explicit inline operator ImVec2() const noexcept { explicit inline operator ImVec2() const noexcept {
@@ -228,7 +227,7 @@ struct Vec {
} }
} }
constexpr auto operator==(const Vec &v) const noexcept { constexpr auto operator==(const Vec2 &v) const noexcept {
for (auto i = 0u; i < v.size(); ++i) { for (auto i = 0u; i < v.size(); ++i) {
if ((*this)[i] != v[i]) { if ((*this)[i] != v[i]) {
return false; return false;
@@ -237,29 +236,71 @@ struct Vec {
return true; return true;
} }
constexpr auto operator!=(const Vec &v) const noexcept { constexpr auto operator!=(const Vec2 &v) const noexcept {
return !operator==(v); return !operator==(v);
} }
explicit constexpr operator class Point() const noexcept;
explicit constexpr operator class Size() const noexcept;
[[nodiscard]] [[nodiscard]]
constexpr std::size_t size() const noexcept { constexpr std::size_t size() const noexcept {
return 2; return 2;
} }
constexpr Vec2 operator+(float i) const noexcept {
return {x + i, y + i};
}
constexpr Vec2 operator+=(float i) noexcept {
x += i;
y += i;
return *this;
}
constexpr Vec2 operator-(float i) const noexcept {
return {x - i, y - i};
}
constexpr Vec2 operator-=(float i) noexcept {
x -= i;
y -= i;
return *this;
}
constexpr Vec2 operator*(float i) const noexcept {
return {x * i, y * i};
}
constexpr Vec2 operator*=(float i) noexcept {
x *= i;
y *= i;
return *this;
}
constexpr Vec2 operator/(float i) const noexcept {
return {x / i, y / i};
}
constexpr Vec2 operator/=(float i) noexcept {
x /= i;
y /= i;
return *this;
}
protected: protected:
[[nodiscard]] [[nodiscard]]
constexpr T *start() noexcept { constexpr float *start() noexcept {
return &x; return&x;
} }
[[nodiscard]] [[nodiscard]]
constexpr const T *start() const noexcept { constexpr const float *start() const noexcept {
return &x; return &x;
} }
}; };
using Vec2 = Vec<float>;
template<typename T> template<typename T>
constexpr Error model(T *io, ox::CommonPtrWith<Vec2> auto *obj) noexcept { constexpr Error model(T *io, ox::CommonPtrWith<Vec2> auto *obj) noexcept {
oxReturnError(io->template setTypeInfo<Vec2>()); oxReturnError(io->template setTypeInfo<Vec2>());

View File

@@ -271,7 +271,7 @@ class Vector: detail::VectorAllocator<T, Allocator, SmallVectorSize> {
constexpr bool contains(MaybeView_t<T> const&) const noexcept; constexpr bool contains(MaybeView_t<T> const&) const noexcept;
constexpr iterator<T&, T*, false> insert( constexpr iterator<T&, T*, false> insert(
std::size_t pos, std::size_t cnt, T val) noexcept(useNoexcept); std::size_t pos, std::size_t cnt, T const&val) noexcept(useNoexcept);
constexpr iterator<T&, T*, false> insert(std::size_t pos, T val) noexcept(useNoexcept); constexpr iterator<T&, T*, false> insert(std::size_t pos, T val) noexcept(useNoexcept);
@@ -531,29 +531,23 @@ constexpr bool Vector<T, SmallVectorSize, Allocator>::contains(MaybeView_t<T> co
template<typename T, std::size_t SmallVectorSize, typename Allocator> template<typename T, std::size_t SmallVectorSize, typename Allocator>
constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false> constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&, T*, false>
Vector<T, SmallVectorSize, Allocator>::insert( Vector<T, SmallVectorSize, Allocator>::insert(
std::size_t pos, std::size_t cnt, T val) noexcept(useNoexcept) { std::size_t pos, std::size_t cnt, T const&val) noexcept(useNoexcept) {
if (m_size + cnt > m_cap) { if (m_size + cnt > m_cap) {
reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt); reserveInsert(m_cap ? m_size + cnt : initialCap, pos, cnt);
if (pos < m_size) { }
m_items[pos] = std::move(val); if (pos < m_size) {
} else { for (auto i = m_size + cnt - 1; i > pos; --i) {
for (auto i = 0u; i < cnt; ++i) { std::construct_at(&m_items[i], std::move(m_items[i - cnt]));
std::construct_at(&m_items[pos + i], m_items[pos]); }
} for (auto i = pos; i < pos + cnt; ++i) {
m_items[i] = val;
} }
} else { } else {
if (pos < m_size) { for (auto i = 0u; i < cnt; ++i) {
for (auto i = m_size + cnt - 1; i > pos; --i) { std::construct_at(&m_items[pos + i], m_items[pos]);
std::construct_at(&m_items[i], std::move(m_items[i - cnt]));
}
m_items[pos] = std::move(val);
} else {
for (auto i = 0u; i < cnt; ++i) {
std::construct_at(&m_items[pos + i], m_items[pos]);
}
} }
} }
++m_size; m_size += cnt;
return begin() + pos; return begin() + pos;
} }
@@ -562,20 +556,14 @@ constexpr typename Vector<T, SmallVectorSize, Allocator>::template iterator<T&,
Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, T val) noexcept(useNoexcept) { Vector<T, SmallVectorSize, Allocator>::insert(std::size_t pos, T val) noexcept(useNoexcept) {
if (m_size == m_cap) { if (m_size == m_cap) {
reserveInsert(m_cap ? m_cap * 2 : initialCap, pos); reserveInsert(m_cap ? m_cap * 2 : initialCap, pos);
if (pos < m_size) { }
m_items[pos] = std::move(val); if (pos < m_size) {
} else { for (auto i = m_size; i > pos; --i) {
std::construct_at(&m_items[pos], m_items[pos]); std::construct_at(&m_items[i], std::move(m_items[i - 1]));
} }
m_items[pos] = std::move(val);
} else { } else {
if (pos < m_size) { std::construct_at(&m_items[pos], std::move(val));
for (auto i = m_size; i > pos; --i) {
std::construct_at(&m_items[i], std::move(m_items[i - 1]));
}
m_items[pos] = std::move(val);
} else {
std::construct_at(&m_items[pos], m_items[pos]);
}
} }
++m_size; ++m_size;
return begin() + pos; return begin() + pos;

View File

@@ -4,6 +4,7 @@
#pragma once #pragma once
#include <ox/std/math.hpp>
#include <ox/std/types.hpp> #include <ox/std/types.hpp>
namespace nostalgia::core { namespace nostalgia::core {
@@ -135,7 +136,7 @@ constexpr Color16 color16(int r, int g, int b, int a = 0) noexcept {
return static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(r), 31)) return static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(r), 31))
| static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(g), 31) << 5) | static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(g), 31) << 5)
| static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(b), 31) << 10) | static_cast<Color16>(ox::min<uint8_t>(static_cast<uint8_t>(b), 31) << 10)
| static_cast<Color16>(a << 15); | static_cast<Color16>(a << 15);
} }
[[nodiscard]] [[nodiscard]]
@@ -151,4 +152,13 @@ static_assert(color16(16, 31, 0) == 1008);
static_assert(color16(16, 31, 8) == 9200); static_assert(color16(16, 31, 8) == 9200);
static_assert(color16(16, 32, 8) == 9200); static_assert(color16(16, 32, 8) == 9200);
[[nodiscard]]
constexpr Color16 applySelectionColor(Color16 const color) noexcept {
namespace core = nostalgia::core;
auto const r = core::red16(color) / 2;
auto const g = (core::green16(color) + 20) / 2;
auto const b = (core::blue16(color) + 31) / 2;
return core::color16(r, g, b);
}
} }

View File

@@ -103,6 +103,12 @@ oxModelEnd()
void addEntry(TileSheetSet &set, ox::FileAddress path, int32_t begin = 0, int32_t size = -1) noexcept; void addEntry(TileSheetSet &set, ox::FileAddress path, int32_t begin = 0, int32_t size = -1) noexcept;
[[nodiscard]]
int tileColumns(Context&) noexcept;
[[nodiscard]]
int tileRows(Context&) noexcept;
ox::Error loadBgPalette( ox::Error loadBgPalette(
Context &ctx, Context &ctx,
size_t palBank, size_t palBank,

View File

@@ -222,6 +222,14 @@ ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) n
[[nodiscard]] [[nodiscard]]
unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept; unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept;
/**
*
* @param ss
* @param pBpp
* @param sz size of Subsheet in tiles (not pixels)
*/
ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept;
/** /**
* validateSubSheetIdx takes a SubSheetIdx and moves the index to the * validateSubSheetIdx takes a SubSheetIdx and moves the index to the
* preceding or parent sheet if the current corresponding sheet does * preceding or parent sheet if the current corresponding sheet does

View File

@@ -22,8 +22,6 @@
namespace nostalgia::core { namespace nostalgia::core {
constexpr auto GbaTileColumns = 32;
constexpr auto GbaTileRows = 32;
constexpr auto SpriteCount = 128; constexpr auto SpriteCount = 128;
struct GbaTileMapTarget { struct GbaTileMapTarget {
@@ -284,8 +282,8 @@ ox::Error loadSpriteTileSheet(
return {}; return {};
} }
void setBgTile(Context&, uint_t bgIdx, int column, int row, BgTile const&tile) noexcept { void setBgTile(Context &ctx, uint_t bgIdx, int column, int row, BgTile const&tile) noexcept {
auto const tileIdx = static_cast<std::size_t>(row * GbaTileColumns + column); auto const tileIdx = static_cast<std::size_t>(row * tileColumns(ctx) + column);
// see Tonc 9.3 // see Tonc 9.3
MEM_BG_MAP[bgIdx][tileIdx] = MEM_BG_MAP[bgIdx][tileIdx] =
static_cast<uint16_t>(tile.tileIdx & 0b1'1111'1111) | static_cast<uint16_t>(tile.tileIdx & 0b1'1111'1111) |
@@ -294,8 +292,8 @@ void setBgTile(Context&, uint_t bgIdx, int column, int row, BgTile const&tile) n
static_cast<uint16_t>(tile.palBank << 0xc); static_cast<uint16_t>(tile.palBank << 0xc);
} }
void clearBg(Context&, uint_t bgIdx) noexcept { void clearBg(Context &ctx, uint_t bgIdx) noexcept {
memset(MEM_BG_MAP[bgIdx].data(), 0, GbaTileRows * GbaTileColumns); memset(MEM_BG_MAP[bgIdx].data(), 0, static_cast<size_t>(tileRows(ctx) * tileColumns(ctx)));
} }
uint8_t bgStatus(Context&) noexcept { uint8_t bgStatus(Context&) noexcept {

View File

@@ -6,6 +6,17 @@
namespace nostalgia::core { namespace nostalgia::core {
constexpr auto GbaTileColumns = 32;
constexpr auto GbaTileRows = 32;
int tileColumns(Context&) noexcept {
return GbaTileColumns;
}
int tileRows(Context&) noexcept {
return GbaTileRows;
}
// map ASCII values to the nostalgia charset // map ASCII values to the nostalgia charset
constexpr ox::Array<char, 128> charMap = { constexpr ox::Array<char, 128> charMap = {
0, 0,

View File

@@ -385,7 +385,8 @@ static void setSprite(
uint_t const idx, uint_t const idx,
Sprite const&s) noexcept { Sprite const&s) noexcept {
// Tonc Table 8.4 // Tonc Table 8.4
static constexpr ox::Array<ox::Vec<uint_t>, 12> dimensions{ struct Sz { uint_t x{}, y{}; };
static constexpr ox::Array<Sz, 12> dimensions{
// col 0 // col 0
{1, 1}, // 0, 0 {1, 1}, // 0, 0
{2, 2}, // 0, 1 {2, 2}, // 0, 1

View File

@@ -79,13 +79,13 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
if (ImGui::Button("Add", sz)) { if (ImGui::Button("Add", sz)) {
auto const colorSz = static_cast<int>(colors(m_pal, m_page)); auto const colorSz = static_cast<int>(colors(m_pal, m_page));
constexpr Color16 c = 0; constexpr Color16 c = 0;
undoStack()->push(ox::make_unique<AddColorCommand>(&m_pal, c, m_page, colorSz)); std::ignore = undoStack()->push(ox::make_unique<AddColorCommand>(&m_pal, c, m_page, colorSz));
} }
ImGui::SameLine(); ImGui::SameLine();
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page)); ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page));
{ {
if (ImGui::Button("Remove", sz)) { if (ImGui::Button("Remove", sz)) {
undoStack()->push( std::ignore = undoStack()->push(
ox::make_unique<RemoveColorCommand>( ox::make_unique<RemoveColorCommand>(
&m_pal, &m_pal,
color(m_pal, m_page, static_cast<std::size_t>(m_selectedColorRow)), color(m_pal, m_page, static_cast<std::size_t>(m_selectedColorRow)),
@@ -97,7 +97,7 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
ImGui::BeginDisabled(m_selectedColorRow <= 0); ImGui::BeginDisabled(m_selectedColorRow <= 0);
{ {
if (ImGui::Button("Move Up", sz)) { if (ImGui::Button("Move Up", sz)) {
undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, -1)); std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, -1));
--m_selectedColorRow; --m_selectedColorRow;
} }
} }
@@ -106,7 +106,7 @@ void PaletteEditorImGui::drawColorsEditor() noexcept {
ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page) - 1); ImGui::BeginDisabled(m_selectedColorRow >= colors(m_pal, m_page) - 1);
{ {
if (ImGui::Button("Move Down", sz)) { if (ImGui::Button("Move Down", sz)) {
undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, 1)); std::ignore = undoStack()->push(ox::make_unique<MoveColorCommand>(&m_pal, m_page, m_selectedColorRow, 1));
++m_selectedColorRow; ++m_selectedColorRow;
} }
} }
@@ -155,17 +155,17 @@ void PaletteEditorImGui::drawPagesEditor() noexcept {
constexpr auto toolbarHeight = 40; constexpr auto toolbarHeight = 40;
auto const btnSz = ImVec2(paneSz.x / 3 - 5.5f, 24); auto const btnSz = ImVec2(paneSz.x / 3 - 5.5f, 24);
if (ImGui::Button("Add", btnSz)) { if (ImGui::Button("Add", btnSz)) {
undoStack()->push(ox::make_unique<AddPageCommand>(m_pal)); std::ignore = undoStack()->push(ox::make_unique<AddPageCommand>(m_pal));
m_page = m_pal.pages.size() - 1; m_page = m_pal.pages.size() - 1;
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Remove", btnSz)) { if (ImGui::Button("Remove", btnSz)) {
undoStack()->push(ox::make_unique<RemovePageCommand>(m_pal, m_page)); std::ignore = undoStack()->push(ox::make_unique<RemovePageCommand>(m_pal, m_page));
m_page = std::min(m_page, m_pal.pages.size() - 1); m_page = std::min(m_page, m_pal.pages.size() - 1);
} }
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Duplicate", btnSz)) { if (ImGui::Button("Duplicate", btnSz)) {
undoStack()->push(ox::make_unique<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size())); std::ignore = undoStack()->push(ox::make_unique<DuplicatePageCommand>(m_pal, m_page, m_pal.pages.size()));
} }
ImGui::BeginTable("PageSelect", 2, tableFlags, ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5))); ImGui::BeginTable("PageSelect", 2, tableFlags, ImVec2(paneSz.x, paneSz.y - (toolbarHeight + 5)));
{ {
@@ -198,7 +198,7 @@ void PaletteEditorImGui::drawColorEditor() noexcept {
ImGui::InputInt("Blue", &b, 1, 5); ImGui::InputInt("Blue", &b, 1, 5);
auto const newColor = color16(r, g, b, a); auto const newColor = color16(r, g, b, a);
if (c != newColor) { if (c != newColor) {
undoStack()->push(ox::make_unique<UpdateColorCommand>( std::ignore = undoStack()->push(ox::make_unique<UpdateColorCommand>(
&m_pal, m_page, static_cast<int>(m_selectedColorRow), c, newColor)); &m_pal, m_page, static_cast<int>(m_selectedColorRow), c, newColor));
} }
} }

View File

@@ -14,12 +14,13 @@ int AddPageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddPage); return static_cast<int>(PaletteEditorCommandId::AddPage);
} }
void AddPageCommand::redo() noexcept { ox::Error AddPageCommand::redo() noexcept {
m_pal.pages.emplace_back(); m_pal.pages.emplace_back();
return {};
} }
void AddPageCommand::undo() noexcept { ox::Error AddPageCommand::undo() noexcept {
std::ignore = m_pal.pages.erase(static_cast<std::size_t>(m_pal.pages.size() - 1)); return m_pal.pages.erase(static_cast<std::size_t>(m_pal.pages.size() - 1)).error;
} }
@@ -37,13 +38,14 @@ int DuplicatePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::DuplicatePage); return static_cast<int>(PaletteEditorCommandId::DuplicatePage);
} }
void DuplicatePageCommand::redo() noexcept { ox::Error DuplicatePageCommand::redo() noexcept {
m_pal.pages.emplace(m_dstIdx, std::move(m_page)); m_pal.pages.emplace(m_dstIdx, std::move(m_page));
return {};
} }
void DuplicatePageCommand::undo() noexcept { ox::Error DuplicatePageCommand::undo() noexcept {
m_page = std::move(m_pal.pages[m_dstIdx]); m_page = std::move(m_pal.pages[m_dstIdx]);
std::ignore = m_pal.pages.erase(static_cast<std::size_t>(m_dstIdx)); return m_pal.pages.erase(static_cast<std::size_t>(m_dstIdx)).error;
} }
size_t DuplicatePageCommand::insertIdx() const noexcept { size_t DuplicatePageCommand::insertIdx() const noexcept {
@@ -60,13 +62,14 @@ int RemovePageCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemovePage); return static_cast<int>(PaletteEditorCommandId::RemovePage);
} }
void RemovePageCommand::redo() noexcept { ox::Error RemovePageCommand::redo() noexcept {
m_page = std::move(m_pal.pages[m_idx]); m_page = std::move(m_pal.pages[m_idx]);
std::ignore = m_pal.pages.erase(static_cast<std::size_t>(m_idx)); return m_pal.pages.erase(static_cast<std::size_t>(m_idx)).error;
} }
void RemovePageCommand::undo() noexcept { ox::Error RemovePageCommand::undo() noexcept {
m_pal.pages.insert(m_idx, std::move(m_page)); m_pal.pages.insert(m_idx, std::move(m_page));
return {};
} }
@@ -81,12 +84,13 @@ int AddColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::AddColor); return static_cast<int>(PaletteEditorCommandId::AddColor);
} }
void AddColorCommand::redo() noexcept { ox::Error AddColorCommand::redo() noexcept {
m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color); m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color);
return {};
} }
void AddColorCommand::undo() noexcept { ox::Error AddColorCommand::undo() noexcept {
std::ignore = m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)); return m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)).error;
} }
@@ -101,12 +105,13 @@ int RemoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::RemoveColor); return static_cast<int>(PaletteEditorCommandId::RemoveColor);
} }
void RemoveColorCommand::redo() noexcept { ox::Error RemoveColorCommand::redo() noexcept {
std::ignore = m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)); return m_pal->pages[m_page].erase(static_cast<std::size_t>(m_idx)).error;
} }
void RemoveColorCommand::undo() noexcept { ox::Error RemoveColorCommand::undo() noexcept {
m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color); m_pal->pages[m_page].insert(static_cast<std::size_t>(m_idx), m_color);
return {};
} }
@@ -124,11 +129,11 @@ UpdateColorCommand::UpdateColorCommand(
//setObsolete(m_oldColor == m_newColor); //setObsolete(m_oldColor == m_newColor);
} }
bool UpdateColorCommand::mergeWith(const UndoCommand *cmd) noexcept { bool UpdateColorCommand::mergeWith(UndoCommand const&cmd) noexcept {
if (cmd->commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) { if (cmd.commandId() != static_cast<int>(PaletteEditorCommandId::UpdateColor)) {
return false; return false;
} }
auto ucCmd = static_cast<const UpdateColorCommand*>(cmd); auto ucCmd = static_cast<UpdateColorCommand const*>(&cmd);
if (m_idx != ucCmd->m_idx) { if (m_idx != ucCmd->m_idx) {
return false; return false;
} }
@@ -141,12 +146,14 @@ int UpdateColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::UpdateColor); return static_cast<int>(PaletteEditorCommandId::UpdateColor);
} }
void UpdateColorCommand::redo() noexcept { ox::Error UpdateColorCommand::redo() noexcept {
m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_newColor; m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_newColor;
return {};
} }
void UpdateColorCommand::undo() noexcept { ox::Error UpdateColorCommand::undo() noexcept {
m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_oldColor; m_pal->pages[m_page][static_cast<std::size_t>(m_idx)] = m_oldColor;
return {};
} }
@@ -161,12 +168,14 @@ int MoveColorCommand::commandId() const noexcept {
return static_cast<int>(PaletteEditorCommandId::MoveColor); return static_cast<int>(PaletteEditorCommandId::MoveColor);
} }
void MoveColorCommand::redo() noexcept { ox::Error MoveColorCommand::redo() noexcept {
moveColor(static_cast<int>(m_idx), m_offset); moveColor(static_cast<int>(m_idx), m_offset);
return {};
} }
void MoveColorCommand::undo() noexcept { ox::Error MoveColorCommand::undo() noexcept {
moveColor(static_cast<int>(m_idx) + m_offset, -m_offset); moveColor(static_cast<int>(m_idx) + m_offset, -m_offset);
return {};
} }
void MoveColorCommand::moveColor(int idx, int offset) noexcept { void MoveColorCommand::moveColor(int idx, int offset) noexcept {

View File

@@ -35,9 +35,9 @@ class AddPageCommand: public studio::UndoCommand {
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
}; };
@@ -55,9 +55,9 @@ class DuplicatePageCommand: public studio::UndoCommand {
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
size_t insertIdx() const noexcept; size_t insertIdx() const noexcept;
@@ -78,9 +78,9 @@ class RemovePageCommand: public studio::UndoCommand {
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
}; };
@@ -99,9 +99,9 @@ class AddColorCommand: public studio::UndoCommand {
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept override; int commandId() const noexcept override;
void redo() noexcept override; ox::Error redo() noexcept override;
void undo() noexcept override; ox::Error undo() noexcept override;
}; };
@@ -120,9 +120,9 @@ class RemoveColorCommand: public studio::UndoCommand {
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept override; int commandId() const noexcept override;
void redo() noexcept override; ox::Error redo() noexcept override;
void undo() noexcept override; ox::Error undo() noexcept override;
}; };
@@ -140,14 +140,14 @@ class UpdateColorCommand: public studio::UndoCommand {
~UpdateColorCommand() noexcept override = default; ~UpdateColorCommand() noexcept override = default;
[[nodiscard]] [[nodiscard]]
bool mergeWith(const UndoCommand *cmd) noexcept final; bool mergeWith(const UndoCommand &cmd) noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
}; };
@@ -167,9 +167,9 @@ class MoveColorCommand: public studio::UndoCommand {
int commandId() const noexcept override; int commandId() const noexcept override;
public: public:
void redo() noexcept override; ox::Error redo() noexcept override;
void undo() noexcept override; ox::Error undo() noexcept override;
private: private:
void moveColor(int idx, int offset) noexcept; void moveColor(int idx, int offset) noexcept;

View File

@@ -24,7 +24,7 @@ AddSubSheetCommand::AddSubSheetCommand(
} }
} }
void AddSubSheetCommand::redo() noexcept { ox::Error AddSubSheetCommand::redo() noexcept {
auto &parent = getSubSheet(m_img, m_parentIdx); auto &parent = getSubSheet(m_img, m_parentIdx);
if (m_addedSheets.size() < 2) { if (m_addedSheets.size() < 2) {
auto i = parent.subsheets.size(); auto i = parent.subsheets.size();
@@ -35,9 +35,10 @@ void AddSubSheetCommand::redo() noexcept {
parent.columns = 0; parent.columns = 0;
parent.subsheets.emplace_back(m_img.idIt++, "Subsheet 1", 1, 1, m_img.bpp); parent.subsheets.emplace_back(m_img.idIt++, "Subsheet 1", 1, 1, m_img.bpp);
} }
return {};
} }
void AddSubSheetCommand::undo() noexcept { ox::Error AddSubSheetCommand::undo() noexcept {
auto &parent = getSubSheet(m_img, m_parentIdx); auto &parent = getSubSheet(m_img, m_parentIdx);
if (parent.subsheets.size() == 2) { if (parent.subsheets.size() == 2) {
auto s = parent.subsheets[0]; auto s = parent.subsheets[0];
@@ -47,9 +48,10 @@ void AddSubSheetCommand::undo() noexcept {
parent.subsheets.clear(); parent.subsheets.clear();
} else { } else {
for (auto idx = m_addedSheets.rbegin(); idx != m_addedSheets.rend(); ++idx) { for (auto idx = m_addedSheets.rbegin(); idx != m_addedSheets.rend(); ++idx) {
oxLogError(rmSubSheet(m_img, *idx)); oxReturnError(rmSubSheet(m_img, *idx));
} }
} }
return {};
} }
int AddSubSheetCommand::commandId() const noexcept { int AddSubSheetCommand::commandId() const noexcept {

View File

@@ -17,9 +17,9 @@ class AddSubSheetCommand: public TileSheetCommand {
public: public:
AddSubSheetCommand(TileSheet &img, TileSheet::SubSheetIdx parentIdx) noexcept; AddSubSheetCommand(TileSheet &img, TileSheet::SubSheetIdx parentIdx) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -41,18 +41,20 @@ CutPasteCommand::CutPasteCommand(
} }
} }
void CutPasteCommand::redo() noexcept { ox::Error CutPasteCommand::redo() noexcept {
auto &subsheet = getSubSheet(m_img, m_subSheetIdx); auto &subsheet = getSubSheet(m_img, m_subSheetIdx);
for (const auto &c : m_changes) { for (const auto &c : m_changes) {
setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.newPalIdx)); setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.newPalIdx));
} }
return {};
} }
void CutPasteCommand::undo() noexcept { ox::Error CutPasteCommand::undo() noexcept {
auto &subsheet = getSubSheet(m_img, m_subSheetIdx); auto &subsheet = getSubSheet(m_img, m_subSheetIdx);
for (const auto &c : m_changes) { for (const auto &c : m_changes) {
setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx)); setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx));
} }
return {};
} }
int CutPasteCommand::commandId() const noexcept { int CutPasteCommand::commandId() const noexcept {

View File

@@ -69,9 +69,9 @@ class CutPasteCommand: public TileSheetCommand {
ox::Point const&dstEnd, ox::Point const&dstEnd,
TileSheetClipboard const&cb) noexcept; TileSheetClipboard const&cb) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -28,7 +28,7 @@ core::DeleteTilesCommand::DeleteTilesCommand(
} }
} }
void core::DeleteTilesCommand::redo() noexcept { ox::Error core::DeleteTilesCommand::redo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto srcPos = m_deletePos + m_deleteSz; auto srcPos = m_deletePos + m_deleteSz;
@@ -37,9 +37,10 @@ void core::DeleteTilesCommand::redo() noexcept {
const auto dst2 = p.data() + (p.size() - m_deleteSz); const auto dst2 = p.data() + (p.size() - m_deleteSz);
ox::memmove(dst1, src, p.size() - srcPos); ox::memmove(dst1, src, p.size() - srcPos);
ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0]))); ox::memset(dst2, 0, m_deleteSz * sizeof(decltype(p[0])));
return {};
} }
void DeleteTilesCommand::undo() noexcept { ox::Error DeleteTilesCommand::undo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
const auto src = p.data() + m_deletePos; const auto src = p.data() + m_deletePos;
@@ -48,6 +49,7 @@ void DeleteTilesCommand::undo() noexcept {
const auto sz = p.size() - m_deletePos - m_deleteSz; const auto sz = p.size() - m_deletePos - m_deleteSz;
ox::memmove(dst1, src, sz); ox::memmove(dst1, src, sz);
ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size());
return {};
} }
int DeleteTilesCommand::commandId() const noexcept { int DeleteTilesCommand::commandId() const noexcept {

View File

@@ -23,9 +23,9 @@ class DeleteTilesCommand: public TileSheetCommand {
std::size_t tileIdx, std::size_t tileIdx,
std::size_t tileCnt) noexcept; std::size_t tileCnt) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -56,18 +56,20 @@ bool DrawCommand::append(const ox::Vector<std::size_t> &idxList) noexcept {
return out; return out;
} }
void DrawCommand::redo() noexcept { ox::Error DrawCommand::redo() noexcept {
auto &subsheet = getSubSheet(m_img, m_subSheetIdx); auto &subsheet = getSubSheet(m_img, m_subSheetIdx);
for (const auto &c : m_changes) { for (const auto &c : m_changes) {
setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(m_palIdx)); setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(m_palIdx));
} }
return {};
} }
void DrawCommand::undo() noexcept { ox::Error DrawCommand::undo() noexcept {
auto &subsheet = getSubSheet(m_img, m_subSheetIdx); auto &subsheet = getSubSheet(m_img, m_subSheetIdx);
for (const auto &c : m_changes) { for (const auto &c : m_changes) {
setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx)); setPixel(subsheet, m_img.bpp, c.idx, static_cast<uint8_t>(c.oldPalIdx));
} }
return {};
} }
int DrawCommand::commandId() const noexcept { int DrawCommand::commandId() const noexcept {

View File

@@ -40,9 +40,9 @@ class DrawCommand: public TileSheetCommand {
bool append(const ox::Vector<std::size_t> &idxList) noexcept; bool append(const ox::Vector<std::size_t> &idxList) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -28,7 +28,7 @@ core::InsertTilesCommand::InsertTilesCommand(
} }
} }
void InsertTilesCommand::redo() noexcept { ox::Error InsertTilesCommand::redo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
auto dstPos = m_insertPos + m_insertCnt; auto dstPos = m_insertPos + m_insertCnt;
@@ -36,9 +36,10 @@ void InsertTilesCommand::redo() noexcept {
const auto src = p.data() + m_insertPos; const auto src = p.data() + m_insertPos;
ox::memmove(dst, src, p.size() - dstPos); ox::memmove(dst, src, p.size() - dstPos);
ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0]))); ox::memset(src, 0, m_insertCnt * sizeof(decltype(p[0])));
return {};
} }
void InsertTilesCommand::undo() noexcept { ox::Error InsertTilesCommand::undo() noexcept {
auto &s = getSubSheet(m_img, m_idx); auto &s = getSubSheet(m_img, m_idx);
auto &p = s.pixels; auto &p = s.pixels;
const auto srcIdx = m_insertPos + m_insertCnt; const auto srcIdx = m_insertPos + m_insertCnt;
@@ -48,6 +49,7 @@ void InsertTilesCommand::undo() noexcept {
const auto sz = p.size() - srcIdx; const auto sz = p.size() - srcIdx;
ox::memmove(dst1, src, sz); ox::memmove(dst1, src, sz);
ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size()); ox::memcpy(dst2, m_deletedPixels.data(), m_deletedPixels.size());
return {};
} }
int InsertTilesCommand::commandId() const noexcept { int InsertTilesCommand::commandId() const noexcept {

View File

@@ -23,9 +23,9 @@ class InsertTilesCommand: public TileSheetCommand {
std::size_t tileIdx, std::size_t tileIdx,
std::size_t tileCnt) noexcept; std::size_t tileCnt) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -16,12 +16,14 @@ core::PaletteChangeCommand::PaletteChangeCommand(
m_newPalette(ox::FileAddress(ox::sfmt<ox::IString<43>>("uuid://{}", newPalette))) { m_newPalette(ox::FileAddress(ox::sfmt<ox::IString<43>>("uuid://{}", newPalette))) {
} }
void PaletteChangeCommand::redo() noexcept { ox::Error PaletteChangeCommand::redo() noexcept {
m_img.defaultPalette = m_newPalette; m_img.defaultPalette = m_newPalette;
return {};
} }
void PaletteChangeCommand::undo() noexcept { ox::Error PaletteChangeCommand::undo() noexcept {
m_img.defaultPalette = m_oldPalette; m_img.defaultPalette = m_oldPalette;
return {};
} }
int PaletteChangeCommand::commandId() const noexcept { int PaletteChangeCommand::commandId() const noexcept {

View File

@@ -21,9 +21,9 @@ class PaletteChangeCommand: public TileSheetCommand {
TileSheet &img, TileSheet &img,
ox::CRStringView newPalette) noexcept; ox::CRStringView newPalette) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -11,19 +11,20 @@ core::RmSubSheetCommand::RmSubSheetCommand(TileSheet &img, TileSheet::SubSheetId
m_idx(std::move(idx)), m_idx(std::move(idx)),
m_parentIdx(m_idx) { m_parentIdx(m_idx) {
m_parentIdx.pop_back(); m_parentIdx.pop_back();
auto &parent = getSubSheet(m_img, m_parentIdx);
m_sheet = parent.subsheets[*m_idx.back().value];
} }
void RmSubSheetCommand::redo() noexcept { ox::Error RmSubSheetCommand::redo() noexcept {
auto &parent = getSubSheet(m_img, m_parentIdx); auto &parent = getSubSheet(m_img, m_parentIdx);
oxLogError(parent.subsheets.erase(*m_idx.back().value).error); m_sheet = std::move(parent.subsheets[*m_idx.back().value]);
oxReturnError(parent.subsheets.erase(*m_idx.back().value).error);
return {};
} }
void RmSubSheetCommand::undo() noexcept { ox::Error RmSubSheetCommand::undo() noexcept {
auto &parent = getSubSheet(m_img, m_parentIdx); auto &parent = getSubSheet(m_img, m_parentIdx);
auto i = *m_idx.back().value; auto const i = *m_idx.back().value;
parent.subsheets.insert(i, m_sheet); parent.subsheets.insert(i, std::move(m_sheet));
return {};
} }
int RmSubSheetCommand::commandId() const noexcept { int RmSubSheetCommand::commandId() const noexcept {

View File

@@ -18,9 +18,9 @@ class RmSubSheetCommand: public TileSheetCommand {
public: public:
RmSubSheetCommand(TileSheet &img, TileSheet::SubSheetIdx idx) noexcept; RmSubSheetCommand(TileSheet &img, TileSheet::SubSheetIdx idx) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -20,17 +20,17 @@ core::UpdateSubSheetCommand::UpdateSubSheetCommand(
m_newRows(rows) { m_newRows(rows) {
} }
void UpdateSubSheetCommand::redo() noexcept { ox::Error UpdateSubSheetCommand::redo() noexcept {
auto &sheet = getSubSheet(m_img, m_idx); auto &sheet = getSubSheet(m_img, m_idx);
sheet.name = m_newName; sheet.name = m_newName;
sheet.columns = m_newCols; oxLogError(resizeSubsheet(sheet, m_img.bpp, {m_newCols, m_newRows}));
sheet.rows = m_newRows; return {};
oxLogError(setPixelCount(sheet, m_img.bpp, static_cast<std::size_t>(PixelsPerTile * m_newCols * m_newRows)));
} }
void UpdateSubSheetCommand::undo() noexcept { ox::Error UpdateSubSheetCommand::undo() noexcept {
auto &sheet = getSubSheet(m_img, m_idx); auto &sheet = getSubSheet(m_img, m_idx);
sheet = m_sheet; sheet = m_sheet;
return {};
} }
int UpdateSubSheetCommand::commandId() const noexcept { int UpdateSubSheetCommand::commandId() const noexcept {

View File

@@ -25,9 +25,9 @@ class UpdateSubSheetCommand: public TileSheetCommand {
int cols, int cols,
int rows) noexcept; int rows) noexcept;
void redo() noexcept final; ox::Error redo() noexcept final;
void undo() noexcept final; ox::Error undo() noexcept final;
[[nodiscard]] [[nodiscard]]
int commandId() const noexcept final; int commandId() const noexcept final;

View File

@@ -305,7 +305,7 @@ void TileSheetEditorModel::getFillPixels(bool *pixels, ox::Point const&pt, int o
} }
void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept { void TileSheetEditorModel::pushCommand(studio::UndoCommand *cmd) noexcept {
m_undoStack.push(ox::UPtr<studio::UndoCommand>(cmd)); std::ignore = m_undoStack.push(ox::UPtr<studio::UndoCommand>(cmd));
m_ongoingDrawCommand = dynamic_cast<DrawCommand*>(cmd); m_ongoingDrawCommand = dynamic_cast<DrawCommand*>(cmd);
m_updated = true; m_updated = true;
} }

View File

@@ -139,10 +139,7 @@ void TileSheetPixels::setBufferObjects(ox::Vec2 const&paneSize) noexcept {
return; return;
} }
if (m_model.pixelSelected(i)) { if (m_model.pixelSelected(i)) {
auto const r = red16(color) / 2; color = applySelectionColor(color);
auto const g = (green16(color) + 20) / 2;
auto const b = (blue16(color) + 31) / 2;
color = color16(r, g, b);
} }
setPixelBufferObject(paneSize, static_cast<unsigned>(i * VertexVboRows), fx, fy, color, vbo, ebo); setPixelBufferObject(paneSize, static_cast<unsigned>(i * VertexVboRows), fx, fy, color, vbo, ebo);
}); });

View File

@@ -15,11 +15,11 @@ std::size_t idx(TileSheet::SubSheet const&ss, ox::Point const&pt) noexcept {
} }
[[nodiscard]] [[nodiscard]]
static TileSheet::SubSheet const*getSubsheet(TileSheet::SubSheet const&ss, SubSheetId const id) noexcept { static TileSheet::SubSheet const *getSubsheet(TileSheet::SubSheet const&ss, SubSheetId const id) noexcept {
if (ss.id == id) { if (ss.id == id) {
return &ss; return &ss;
} }
for (auto const&child : ss.subsheets) { for (auto const&child: ss.subsheets) {
if (auto out = getSubsheet(child, id)) { if (auto out = getSubsheet(child, id)) {
return out; return out;
} }
@@ -34,7 +34,7 @@ static size_t getTileCnt(TileSheet::SubSheet const&ss, int const bpp) noexcept {
return ss.pixels.size() / bytesPerTile; return ss.pixels.size() / bytesPerTile;
} else { } else {
size_t out{}; size_t out{};
for (auto const&child : ss.subsheets) { for (auto const&child: ss.subsheets) {
out += getTileCnt(child, bpp); out += getTileCnt(child, bpp);
} }
return out; return out;
@@ -45,7 +45,7 @@ size_t getTileCnt(TileSheet const&ts) noexcept {
return getTileCnt(ts.subsheet, ts.bpp); return getTileCnt(ts.subsheet, ts.bpp);
} }
TileSheet::SubSheet const*getSubsheet(TileSheet const&ts, SubSheetId const id) noexcept { TileSheet::SubSheet const *getSubsheet(TileSheet const&ts, SubSheetId const id) noexcept {
return getSubsheet(ts.subsheet, id); return getSubsheet(ts.subsheet, id);
} }
@@ -54,7 +54,7 @@ static ox::Optional<size_t> getPixelIdx(
SubSheetId const id, SubSheetId const id,
size_t idx, size_t idx,
int8_t const bpp) noexcept { int8_t const bpp) noexcept {
for (auto const&child : ss.subsheets) { for (auto const&child: ss.subsheets) {
if (child.id == id) { if (child.id == id) {
return ox::Optional<size_t>(ox::in_place, idx); return ox::Optional<size_t>(ox::in_place, idx);
} }
@@ -106,8 +106,8 @@ uint8_t getPixel(TileSheet::SubSheet const&ss, int8_t pBpp, ox::Point const&pt)
return getPixel(ss, pBpp, idx); return getPixel(ss, pBpp, idx);
} }
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept { static void setPixel(ox::Vector<uint8_t> &pixels, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept {
auto &pixel = ss.pixels[static_cast<std::size_t>(idx / 2)]; auto &pixel = pixels[static_cast<std::size_t>(idx / 2)];
if (pBpp == 4) { if (pBpp == 4) {
if (idx & 1) { if (idx & 1) {
pixel = static_cast<uint8_t>((pixel & 0b0000'1111) | (palIdx << 4)); pixel = static_cast<uint8_t>((pixel & 0b0000'1111) | (palIdx << 4));
@@ -119,29 +119,59 @@ void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx
} }
} }
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, uint64_t idx, uint8_t palIdx) noexcept {
setPixel(ss.pixels, pBpp, idx, palIdx);
}
static void setPixel(ox::Vector<uint8_t> &pixels, int columns, int8_t pBpp, ox::Point const&pt, uint8_t palIdx) noexcept {
const auto idx = ptToIdx(pt, columns);
setPixel(pixels, pBpp, idx, palIdx);
}
void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const&pt, uint8_t palIdx) noexcept { void setPixel(TileSheet::SubSheet &ss, int8_t pBpp, ox::Point const&pt, uint8_t palIdx) noexcept {
const auto idx = ptToIdx(pt, ss.columns); const auto idx = ptToIdx(pt, ss.columns);
setPixel(ss, pBpp, idx, palIdx); setPixel(ss, pBpp, idx, palIdx);
} }
ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept { static ox::Error setPixelCount(ox::Vector<uint8_t> &pixels, int8_t pBpp, std::size_t cnt) noexcept {
switch (pBpp) { switch (pBpp) {
case 4: case 4:
ss.pixels.resize(cnt / 2); pixels.resize(cnt / 2);
return OxError(0); return OxError(0);
case 8: case 8:
ss.pixels.resize(cnt); pixels.resize(cnt);
return OxError(0); return OxError(0);
default: default:
return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount"); return OxError(1, "Invalid pBpp used for TileSheet::SubSheet::setPixelCount");
} }
} }
ox::Error setPixelCount(TileSheet::SubSheet &ss, int8_t pBpp, std::size_t cnt) noexcept {
return setPixelCount(ss.pixels, pBpp, cnt);
}
unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept { unsigned pixelCnt(TileSheet::SubSheet const&ss, int8_t pBpp) noexcept {
const auto pixelsSize = static_cast<unsigned>(ss.pixels.size()); const auto pixelsSize = static_cast<unsigned>(ss.pixels.size());
return pBpp == 4 ? pixelsSize * 2 : pixelsSize; return pBpp == 4 ? pixelsSize * 2 : pixelsSize;
} }
ox::Error resizeSubsheet(TileSheet::SubSheet &ss, int8_t pBpp, ox::Size const&sz) noexcept {
ox::Vector<uint8_t> out;
oxReturnError(setPixelCount(out, pBpp, static_cast<size_t>(sz.width * sz.height) * PixelsPerTile));
auto const w = sz.width * TileWidth;
auto const h = sz.height * TileHeight;
for (auto x = 0; x < w; ++x) {
for (auto y = 0; y < h; ++y) {
auto const palIdx = getPixel(ss, pBpp, {x, y});
setPixel(out, sz.width, pBpp, {x, y}, palIdx);
}
}
ss.columns = sz.width;
ss.rows = sz.height;
ss.pixels = std::move(out);
return {};
}
ox::Result<ox::StringView> getNameFor(TileSheet::SubSheet const&ss, SubSheetId pId) noexcept { ox::Result<ox::StringView> getNameFor(TileSheet::SubSheet const&ss, SubSheetId pId) noexcept {
if (ss.id == pId) { if (ss.id == pId) {
return ox::StringView(ss.name); return ox::StringView(ss.name);

View File

@@ -93,9 +93,17 @@ void createUuidMapping(Context &ctx, ox::StringView filePath, ox::UUID const&uui
ox::Error buildUuidMap(Context &ctx) noexcept; ox::Error buildUuidMap(Context &ctx) noexcept;
ox::Result<ox::String> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept; ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept;
ox::Result<ox::String> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept; constexpr ox::Result<ox::UUID> uuidUrlToUuid(ox::StringView uuidUrl) noexcept {
return ox::UUID::fromString(substr(uuidUrl, 7));
}
ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept;
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept;
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept; ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept;

View File

@@ -83,19 +83,29 @@ ox::Result<ox::UUID> pathToUuid(Context &ctx, ox::CRStringView path) noexcept {
#endif #endif
} }
ox::Result<ox::String> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept { ox::Result<ox::CStringView> uuidUrlToPath(Context &ctx, ox::StringView uuid) noexcept {
uuid = substr(uuid, 7);
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL
oxRequire(out, ctx.uuidToPath.at(uuid)); oxRequireM(out, ctx.uuidToPath.at(uuid));
return *out; return ox::CStringView(*out);
#else #else
return OxError(1, "UUID to path conversion not supported on this platform"); return OxError(1, "UUID to path conversion not supported on this platform");
#endif #endif
} }
ox::Result<ox::String> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept { ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::CRStringView uuid) noexcept {
#ifndef OX_BARE_METAL #ifndef OX_BARE_METAL
oxRequire(out, ctx.uuidToPath.at(uuid.toString())); oxRequireM(out, ctx.uuidToPath.at(uuid));
return *out; return ox::CStringView(*out);
#else
return OxError(1, "UUID to path conversion not supported on this platform");
#endif
}
ox::Result<ox::CStringView> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept {
#ifndef OX_BARE_METAL
oxRequireM(out, ctx.uuidToPath.at(uuid.toString()));
return ox::CStringView(*out);
#else #else
return OxError(1, "UUID to path conversion not supported on this platform"); return OxError(1, "UUID to path conversion not supported on this platform");
#endif #endif

View File

@@ -30,7 +30,7 @@ static ox::Error pathToInode(
} }
if (beginsWith(path, "uuid://")) { if (beginsWith(path, "uuid://")) {
auto const uuid = ox::substr(path, 7); auto const uuid = ox::substr(path, 7);
oxReturnError(keel::uuidToPath(ctx, uuid).moveTo(path)); oxReturnError(keel::uuidToPath(ctx, uuid).to<ox::String>().moveTo(path));
} }
oxRequire(s, dest.stat(path)); oxRequire(s, dest.stat(path));
oxReturnError(o.at("type").unwrap()->set(static_cast<int8_t>(ox::FileAddressType::Inode))); oxReturnError(o.at("type").unwrap()->set(static_cast<int8_t>(ox::FileAddressType::Inode)));

View File

@@ -2,6 +2,7 @@
* Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved. * Copyright 2016 - 2024 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/ */
#include <algorithm>
#include <filesystem> #include <filesystem>
#include <imgui.h> #include <imgui.h>
@@ -196,10 +197,10 @@ void StudioUI::drawMenu() noexcept {
if (ImGui::BeginMenu("Edit")) { if (ImGui::BeginMenu("Edit")) {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack && undoStack->canUndo())) { if (ImGui::MenuItem("Undo", "Ctrl+Z", false, undoStack && undoStack->canUndo())) {
undoStack->undo(); oxLogError(undoStack->undo());
} }
if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack && undoStack->canRedo())) { if (ImGui::MenuItem("Redo", "Ctrl+Y", false, undoStack && undoStack->canRedo())) {
undoStack->redo(); oxLogError(undoStack->redo());
} }
ImGui::Separator(); ImGui::Separator();
if (ImGui::MenuItem("Copy", "Ctrl+C", false, m_activeEditor && m_activeEditor->copyEnabled())) { if (ImGui::MenuItem("Copy", "Ctrl+C", false, m_activeEditor && m_activeEditor->copyEnabled())) {
@@ -314,14 +315,14 @@ void StudioUI::toggleProjectExplorer() noexcept {
void StudioUI::redo() noexcept { void StudioUI::redo() noexcept {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canRedo()) { if (undoStack && undoStack->canRedo()) {
m_activeEditor->undoStack()->redo(); oxLogError(m_activeEditor->undoStack()->redo());
} }
} }
void StudioUI::undo() noexcept { void StudioUI::undo() noexcept {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr; auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (undoStack && undoStack->canUndo()) { if (undoStack && undoStack->canUndo()) {
m_activeEditor->undoStack()->undo(); oxLogError(m_activeEditor->undoStack()->undo());
} }
} }

View File

@@ -131,14 +131,14 @@ class Editor: public studio::BaseEditor {
[[nodiscard]] [[nodiscard]]
UndoStack *undoStack() noexcept final; UndoStack *undoStack() noexcept final;
void pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept; ox::Error pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept;
template<typename UC, typename ...Args> template<typename UC, typename ...Args>
void pushCommand(Args&&... args) noexcept { ox::Error pushCommand(Args&&... args) noexcept {
try { try {
m_undoStack.push(ox::make_unique<UC>(ox::forward<Args>(args)...)); return m_undoStack.push(ox::make_unique<UC>(ox::forward<Args>(args)...));
} catch (ox::Exception const&ex) { } catch (ox::Exception const&ex) {
oxLogError(ex.toError()); return ex.toError();
} }
} }

View File

@@ -34,7 +34,7 @@ ox::Result<T> getDragDropPayload(ox::CStringView name) noexcept {
static_cast<size_t>(payload->DataSize)}); static_cast<size_t>(payload->DataSize)});
} }
ox::Error setDragDropPayload(ox::CStringView name, auto const &obj) noexcept { ox::Error setDragDropPayload(ox::CStringView name, auto const&obj) noexcept {
oxRequire(buff, ox::writeClaw(obj, ox::ClawFormat::Metal)); oxRequire(buff, ox::writeClaw(obj, ox::ClawFormat::Metal));
ImGui::SetDragDropPayload(name.c_str(), buff.data(), buff.size()); ImGui::SetDragDropPayload(name.c_str(), buff.data(), buff.size());
return {}; return {};

View File

@@ -115,7 +115,7 @@ class Project {
// file. // file.
ox::Signal<ox::Error(ox::CRStringView)> fileRecognized; ox::Signal<ox::Error(ox::CRStringView)> fileRecognized;
ox::Signal<ox::Error(ox::CRStringView)> fileDeleted; ox::Signal<ox::Error(ox::CRStringView)> fileDeleted;
ox::Signal<ox::Error(ox::CRStringView)> fileUpdated; ox::Signal<ox::Error(ox::StringView, ox::UUID)> fileUpdated;
}; };
@@ -135,7 +135,8 @@ ox::Error Project::writeObj(ox::CRStringView path, T const&obj, ox::ClawFormat f
oxReturnError(writeTypeStore()); oxReturnError(writeTypeStore());
} }
oxReturnError(keel::setAsset(m_ctx, path, obj)); oxReturnError(keel::setAsset(m_ctx, path, obj));
fileUpdated.emit(path); oxRequire(uuid, pathToUuid(m_ctx, path));
fileUpdated.emit(path, uuid);
return {}; return {};
} }

View File

@@ -0,0 +1,94 @@
#pragma once
#include <imgui.h>
#include <ox/std/math.hpp>
#include <ox/std/point.hpp>
#include <ox/std/typetraits.hpp>
namespace studio {
struct Selection {ox::Point a, b;};
constexpr auto iterateSelection(studio::Selection const&sel, auto const&cb) {
constexpr auto retErr = ox::is_same_v<decltype(cb(0, 0)), ox::Error>;
for (auto x = sel.a.x; x <= sel.b.x; ++x) {
for (auto y = sel.a.y; y <= sel.b.y; ++y) {
if constexpr(retErr) {
oxReturnError(cb(x, y));
} else {
cb(x, y);
}
}
}
if constexpr(retErr) {
return ox::Error{};
}
};
class SelectionTracker {
private:
bool m_selectionOngoing{};
ox::Point m_pointA;
ox::Point m_pointB;
public:
[[nodiscard]]
constexpr bool selectionOngoing() const noexcept {
return m_selectionOngoing;
}
constexpr void startSelection(ox::Point cursor) noexcept {
m_pointA = cursor;
m_pointB = cursor;
m_selectionOngoing = true;
}
constexpr void updateCursorPoint(ox::Point cursor, bool allowStart = true) noexcept {
if (!m_selectionOngoing && allowStart) {
m_pointA = cursor;
m_selectionOngoing = true;
}
if (m_selectionOngoing) {
m_pointB = cursor;
}
}
constexpr void updateCursorPoint(ox::Vec2 cursor, bool allowStart = true) noexcept {
updateCursorPoint(
ox::Point{
static_cast<int32_t>(cursor.x),
static_cast<int32_t>(cursor.y),
},
allowStart);
}
constexpr void updateCursorPoint(ImVec2 cursor, bool allowStart = true) noexcept {
updateCursorPoint(
ox::Point{
static_cast<int32_t>(cursor.x),
static_cast<int32_t>(cursor.y),
},
allowStart);
}
constexpr void finishSelection() noexcept {
m_selectionOngoing = {};
}
[[nodiscard]]
constexpr Selection selection() const noexcept {
return {
{
ox::min(m_pointA.x, m_pointB.x),
ox::min(m_pointA.y, m_pointB.y),
},
{
ox::max(m_pointA.x, m_pointB.x),
ox::max(m_pointA.y, m_pointB.y),
},
};
}
};
}

View File

@@ -12,6 +12,7 @@
#include <studio/itemmaker.hpp> #include <studio/itemmaker.hpp>
#include <studio/popup.hpp> #include <studio/popup.hpp>
#include <studio/project.hpp> #include <studio/project.hpp>
#include <studio/selectiontracker.hpp>
#include <studio/task.hpp> #include <studio/task.hpp>
#include <studio/undocommand.hpp> #include <studio/undocommand.hpp>
#include <studio/undostack.hpp> #include <studio/undostack.hpp>

View File

@@ -4,16 +4,26 @@
#pragma once #pragma once
#include <source_location>
#include <ox/std/error.hpp>
namespace studio { namespace studio {
class NoChangesException: public ox::Exception {
public:
inline NoChangesException(std::source_location sloc = std::source_location::current()):
ox::Exception(sloc.file_name(), sloc.line(), 1, "Command makes no changes.") {}
};
class UndoCommand { class UndoCommand {
public: public:
virtual ~UndoCommand() noexcept = default; virtual ~UndoCommand() noexcept = default;
virtual void redo() noexcept = 0; virtual ox::Error redo() noexcept = 0;
virtual void undo() noexcept = 0; virtual ox::Error undo() noexcept = 0;
[[nodiscard]] [[nodiscard]]
virtual int commandId() const noexcept = 0; virtual int commandId() const noexcept = 0;
virtual bool mergeWith(UndoCommand const*cmd) noexcept; virtual bool mergeWith(UndoCommand const&cmd) noexcept;
}; };
} }

View File

@@ -19,11 +19,11 @@ class UndoStack {
std::size_t m_stackIdx = 0; std::size_t m_stackIdx = 0;
public: public:
void push(ox::UPtr<UndoCommand> &&cmd) noexcept; ox::Error push(ox::UPtr<UndoCommand> &&cmd) noexcept;
void redo() noexcept; ox::Error redo() noexcept;
void undo() noexcept; ox::Error undo() noexcept;
[[nodiscard]] [[nodiscard]]
constexpr bool canRedo() const noexcept { constexpr bool canRedo() const noexcept {

View File

@@ -127,8 +127,8 @@ ox::CStringView Editor::itemDisplayName() const noexcept {
return m_itemName; return m_itemName;
} }
void Editor::pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept { ox::Error Editor::pushCommand(ox::UPtr<UndoCommand> &&cmd) noexcept {
m_undoStack.push(std::move(cmd)); return m_undoStack.push(std::move(cmd));
} }
UndoStack *Editor::undoStack() noexcept { UndoStack *Editor::undoStack() noexcept {

View File

@@ -57,7 +57,7 @@ ox::FileSystem &Project::romFs() noexcept {
ox::Error Project::mkdir(ox::CRStringView path) const noexcept { ox::Error Project::mkdir(ox::CRStringView path) const noexcept {
oxReturnError(m_fs.mkdir(path, true)); oxReturnError(m_fs.mkdir(path, true));
fileUpdated.emit(path); fileUpdated.emit(path, {});
return {}; return {};
} }
@@ -115,9 +115,9 @@ ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexc
ox::Buffer outBuff; ox::Buffer outBuff;
outBuff.reserve(buff.size() + HdrSz); outBuff.reserve(buff.size() + HdrSz);
ox::BufferWriter writer(&outBuff); ox::BufferWriter writer(&outBuff);
auto const [uuid, err] = m_ctx.pathToUuid.at(path); auto const [uuid, err] = pathToUuid(m_ctx, path);
if (!err) { if (!err) {
oxReturnError(keel::writeUuidHeader(writer, *uuid)); oxReturnError(keel::writeUuidHeader(writer, uuid));
} }
oxReturnError(writer.write(buff.data(), buff.size())); oxReturnError(writer.write(buff.data(), buff.size()));
auto const newFile = m_fs.stat(path).error != 0; auto const newFile = m_fs.stat(path).error != 0;
@@ -126,7 +126,7 @@ ox::Error Project::writeBuff(ox::CRStringView path, ox::Buffer const&buff) noexc
fileAdded.emit(path); fileAdded.emit(path);
indexFile(path); indexFile(path);
} else { } else {
fileUpdated.emit(path); fileUpdated.emit(path, uuid);
} }
return {}; return {};
} }

View File

@@ -3,7 +3,7 @@
namespace studio { namespace studio {
bool UndoCommand::mergeWith(UndoCommand const*) noexcept { bool UndoCommand::mergeWith(UndoCommand const&) noexcept {
return false; return false;
} }

View File

@@ -6,35 +6,39 @@
namespace studio { namespace studio {
void UndoStack::push(ox::UPtr<UndoCommand> &&cmd) noexcept { ox::Error UndoStack::push(ox::UPtr<UndoCommand> &&cmd) noexcept {
for (auto const i = m_stackIdx; i < m_stack.size();) { for (auto const i = m_stackIdx; i < m_stack.size();) {
std::ignore = m_stack.erase(i); std::ignore = m_stack.erase(i);
} }
cmd->redo(); oxReturnError(cmd->redo());
redoTriggered.emit(cmd.get()); redoTriggered.emit(cmd.get());
changeTriggered.emit(cmd.get()); changeTriggered.emit(cmd.get());
if (m_stack.empty() || !(*m_stack.back().value)->mergeWith(cmd.get())) { if (m_stack.empty() || !(*m_stack.back().value)->mergeWith(*cmd)) {
m_stack.emplace_back(std::move(cmd)); m_stack.emplace_back(std::move(cmd));
++m_stackIdx; ++m_stackIdx;
} }
return {};
} }
void UndoStack::redo() noexcept { ox::Error UndoStack::redo() noexcept {
if (m_stackIdx < m_stack.size()) { if (m_stackIdx < m_stack.size()) {
auto &c = m_stack[m_stackIdx++]; auto &c = m_stack[m_stackIdx];
c->redo(); oxReturnError(c->redo());
++m_stackIdx;
redoTriggered.emit(c.get()); redoTriggered.emit(c.get());
changeTriggered.emit(c.get()); changeTriggered.emit(c.get());
} }
return {};
} }
void UndoStack::undo() noexcept { ox::Error UndoStack::undo() noexcept {
if (m_stackIdx) { if (m_stackIdx) {
auto &c = m_stack[--m_stackIdx]; auto &c = m_stack[--m_stackIdx];
c->undo(); oxReturnError(c->undo());
undoTriggered.emit(c.get()); undoTriggered.emit(c.get());
changeTriggered.emit(c.get()); changeTriggered.emit(c.get());
} }
return {};
} }
} }