# Rewrite 5
- Move Libraries Source into pd directory and give them all their own CMakeLists.txt - Partial rewrite core (color, autogenerated vec), lithium (now uses UNIQUE PTR for Commands), UI7 - Use MenuV2 as new standart in UI7 - Implementz ViewPort Pre alpha to UI7 - Add Line Drawing to DrawList (not Working) - Implement a Complete new drievrs API (static Drivers) - NO SUPPORT FOR SHARED LIBRARY BUILDS IN VERSION 5 YET - Add Tools to Autogenerate Headers and Stuff
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/bit_util.hpp>
|
||||
|
||||
namespace PD::BitUtil {
|
||||
PD_CORE_API bool IsSingleBit(u32 v) { return v && !(v & (v - 1)); }
|
||||
PD_CORE_API u32 GetPow2(u32 v) {
|
||||
v--;
|
||||
v |= v >> 1;
|
||||
v |= v >> 2;
|
||||
v |= v >> 4;
|
||||
v |= v >> 8;
|
||||
v |= v >> 16;
|
||||
v++;
|
||||
return (v >= 64 ? v : 64);
|
||||
}
|
||||
} // namespace PD::BitUtil
|
||||
@@ -1,73 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/color.hpp>
|
||||
|
||||
namespace PD {
|
||||
// The Solution of the biggest performance issue
|
||||
// A Simple Lookup table
|
||||
static const std::map<char, int> HEX_DEC = {
|
||||
{'0', 0}, {'1', 1}, {'2', 2}, {'3', 3}, {'4', 4}, {'5', 5},
|
||||
{'6', 6}, {'7', 7}, {'8', 8}, {'9', 9}, {'a', 10}, {'b', 11},
|
||||
{'c', 12}, {'d', 13}, {'e', 14}, {'f', 15}, {'A', 10}, {'B', 11},
|
||||
{'C', 12}, {'D', 13}, {'E', 14}, {'F', 15}};
|
||||
|
||||
PD_CORE_API Color& Color::Hex(const std::string& hex) {
|
||||
#ifdef PD_NO_SAFE_CODE
|
||||
/// Safetey check (not required if you programm well xd)
|
||||
if (hex.length() != 7 || hex.length() != 9 || hex.length() != 6 ||
|
||||
hex.length() != 8 || std::find_if(hex.begin(), hex.end(), [](char c) {
|
||||
return !std::isxdigit(c);
|
||||
}) != hex.end()) {
|
||||
return *this;
|
||||
}
|
||||
#endif
|
||||
int offset = ((hex.length() == 7 || hex.length() == 9) ? 1 : 0);
|
||||
m_r = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_g = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
m_b = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
offset += 2;
|
||||
if (hex.length() == 9) {
|
||||
m_a = HEX_DEC.at(hex[offset]) * 16 + HEX_DEC.at(hex[offset + 1]);
|
||||
} else {
|
||||
m_a = 255;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
PD_CORE_API std::string Color::Hex(bool rgba) const {
|
||||
/** Need to int cast (so it is used as num and not char...) */
|
||||
std::stringstream s;
|
||||
s << "#";
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_r;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_g;
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_b;
|
||||
if (rgba) {
|
||||
s << std::hex << std::setw(2) << std::setfill('0') << (int)m_a;
|
||||
}
|
||||
return s.str();
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,55 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/common.hpp>
|
||||
#include <pd/core/strings.hpp>
|
||||
|
||||
#ifndef PALLADIUM_VERSION
|
||||
#define PALLADIUM_VERSION "unknown"
|
||||
#endif
|
||||
#ifndef PALLADIUM_GIT_COMMIT
|
||||
#define PALLADIUM_GIT_COMMIT "unknown"
|
||||
#endif
|
||||
#ifndef PALLADIUM_GIT_BRANCH
|
||||
#define PALLADIUM_GIT_BRANCH "unknown"
|
||||
#endif
|
||||
|
||||
PD_CORE_API const std::string PD::LibInfo::CompiledWith() {
|
||||
return Strings::GetCompilerVersion();
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::CxxVersion() {
|
||||
return "CPP: " + std::to_string(__cplusplus);
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::BuildTime() {
|
||||
return __DATE__ " - " __TIME__;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Version() {
|
||||
return PALLADIUM_VERSION;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Commit() {
|
||||
return PALLADIUM_GIT_COMMIT;
|
||||
}
|
||||
PD_CORE_API const std::string PD::LibInfo::Branch() {
|
||||
return PALLADIUM_GIT_BRANCH;
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/hid_driver.hpp>
|
||||
|
||||
/// Reform of the RenderD7 095 Hid Api
|
||||
/// Using Custom Keybindings for future
|
||||
/// Porting of the library
|
||||
|
||||
namespace PD {
|
||||
PD_CORE_API bool Hid::IsEvent(Event e, Key keys) {
|
||||
return key_events[0][e] & keys;
|
||||
}
|
||||
|
||||
PD_CORE_API void Hid::SwappyTable() {
|
||||
auto tkd = key_events[1][Event_Down];
|
||||
auto tkh = key_events[1][Event_Held];
|
||||
auto tku = key_events[1][Event_Up];
|
||||
key_events[1][Event_Down] = key_events[0][Event_Down];
|
||||
key_events[1][Event_Held] = key_events[0][Event_Held];
|
||||
key_events[1][Event_Up] = key_events[0][Event_Up];
|
||||
key_events[0][Event_Down] = tkd;
|
||||
key_events[0][Event_Held] = tkh;
|
||||
key_events[0][Event_Up] = tku;
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,191 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/io.hpp>
|
||||
|
||||
PD_CORE_API void DecompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; i < cpy.size(); i += 3) {
|
||||
for (size_t j = 0; j < cpy[i + 2]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; (i + 4) < cpy.size(); i += 5) {
|
||||
for (size_t j = 0; j < cpy[i + 4]; j++) {
|
||||
data.push_back(cpy[i]);
|
||||
data.push_back(cpy[i + 1]);
|
||||
data.push_back(cpy[i + 2]);
|
||||
data.push_back(cpy[i + 3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE16(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 1) < cpy.size()) {
|
||||
PD::u16 v = PD::u16(cpy[i]) | (PD::u16(cpy[i + 1]) << 8);
|
||||
size_t c = 1;
|
||||
while ((i + c * 2 + 1) < cpy.size() &&
|
||||
(PD::u16(cpy[i + c * 2]) | (PD::u16(cpy[i + c * 2 + 1])) << 8) ==
|
||||
v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 2;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE32(std::vector<PD::u8>& data) {
|
||||
std::vector<PD::u8> cpy = data;
|
||||
data.clear();
|
||||
size_t i = 0;
|
||||
while ((i + 3) < cpy.size()) {
|
||||
PD::u32 v = PD::u32(cpy[i]) | (PD::u32(cpy[i + 1]) << 8) |
|
||||
(PD::u32(cpy[i + 2]) << 16) | (PD::u32(cpy[i + 3]) << 24);
|
||||
size_t c = 1;
|
||||
while ((i + c * 4 + 3) < cpy.size() &&
|
||||
(PD::u32(cpy[i + c * 4]) | (PD::u32(cpy[i + c * 4 + 1]) << 8) |
|
||||
(PD::u32(cpy[i + c * 4 + 2]) << 16) |
|
||||
(PD::u32(cpy[i + c * 4 + 3]) << 24)) == v &&
|
||||
c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(PD::u8(v & 0xFF));
|
||||
data.push_back(PD::u8((v >> 8) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 16) & 0xFF));
|
||||
data.push_back(PD::u8((v >> 24) & 0xFF));
|
||||
data.push_back(c);
|
||||
i += c * 4;
|
||||
}
|
||||
}
|
||||
|
||||
namespace PD {
|
||||
namespace IO {
|
||||
PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path) {
|
||||
std::ifstream iff(path, std::ios::binary);
|
||||
if (!iff) {
|
||||
return std::vector<u8>();
|
||||
}
|
||||
iff.seekg(0, std::ios::end);
|
||||
size_t szs = iff.tellg();
|
||||
iff.seekg(0, std::ios::beg);
|
||||
std::vector<u8> res(szs, 0);
|
||||
iff.read(reinterpret_cast<char*>(res.data()), res.size());
|
||||
iff.close();
|
||||
return res;
|
||||
}
|
||||
|
||||
PD_CORE_API u32 HashMemory(const std::vector<u8>& data) {
|
||||
u32 hash = 4477;
|
||||
for (auto& it : data) {
|
||||
hash = (hash * 33) + it;
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE(std::vector<u8>& data) {
|
||||
if ((data.size() % 2) != 0) {
|
||||
return;
|
||||
}
|
||||
std::vector<u8> cpy = data;
|
||||
data.clear();
|
||||
for (size_t i = 0; i < cpy.size(); i += 2) {
|
||||
data.insert(data.end(), cpy[i + 1], cpy[i]);
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void DecompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (!data.size()) {
|
||||
return;
|
||||
}
|
||||
u8 fmt = data[0];
|
||||
data.erase(data.begin());
|
||||
if (fmt == 0) {
|
||||
DecompressRLE(data);
|
||||
} else if (fmt == 1) {
|
||||
DecompressRLE16(data);
|
||||
} else if (fmt == 2) {
|
||||
DecompressRLE32(data);
|
||||
}
|
||||
/** unknown returns input data */
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE(std::vector<u8>& data) {
|
||||
if (data.empty()) {
|
||||
/** No exceptions enabled :( */
|
||||
return;
|
||||
}
|
||||
std::vector<u8> cpy = data;
|
||||
data.clear();
|
||||
/** 8-Bit RLE */
|
||||
data.push_back(0);
|
||||
size_t i = 0;
|
||||
while (i < cpy.size()) {
|
||||
u8 v = cpy[i];
|
||||
u8 c = 1;
|
||||
while (i + c < cpy.size() && cpy[i + c] == v && c < 255) {
|
||||
c++; // c++ ...
|
||||
}
|
||||
data.push_back(v);
|
||||
data.push_back(c);
|
||||
i += c;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void CompressRLE_Ex(std::vector<u8>& data) {
|
||||
if (data.empty()) {
|
||||
/** No exceptions enabled :( */
|
||||
return;
|
||||
}
|
||||
std::vector<u8> _8 = data;
|
||||
std::vector<u8> _16 = data;
|
||||
std::vector<u8> _32 = data;
|
||||
CompressRLE(_8);
|
||||
CompressRLE16(_16);
|
||||
CompressRLE32(_32);
|
||||
if (_16.size() < _8.size() && _16.size() < _32.size()) {
|
||||
_16.insert(_16.begin(), 1);
|
||||
data = _16;
|
||||
return;
|
||||
} else if (_32.size() < _8.size()) {
|
||||
_32.insert(_32.begin(), 2);
|
||||
data = _32;
|
||||
return;
|
||||
}
|
||||
_8.insert(_8.begin(), 0);
|
||||
data = _8;
|
||||
}
|
||||
} // namespace IO
|
||||
} // namespace PD
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/mat.hpp>
|
||||
|
||||
namespace PD {
|
||||
PD_CORE_API void Mat4::Zeros() {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
m[i] = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void Mat4::Ortho(float left, float right, float bottom, float top,
|
||||
float near, float far) {
|
||||
m[0] = 2.0f / (right - left);
|
||||
m[1] = 0.0f;
|
||||
m[2] = 0.0f;
|
||||
m[3] = -(right + left) / (right - left);
|
||||
|
||||
m[4] = 0.0f;
|
||||
m[5] = 2.0f / (top - bottom);
|
||||
m[6] = 0.0f;
|
||||
m[7] = -(top + bottom) / (top - bottom);
|
||||
|
||||
m[8] = 0.0f;
|
||||
m[9] = 0.0f;
|
||||
m[10] = -2.0f / (far - near);
|
||||
m[11] = -(far + near) / (far - near);
|
||||
|
||||
m[12] = 0.0f;
|
||||
m[13] = 0.0f;
|
||||
m[14] = 0.0f;
|
||||
m[15] = 1.0f;
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,166 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/strings.hpp>
|
||||
|
||||
namespace PD::Strings {
|
||||
PD_CORE_API bool StringEndsWith(const std::string& str,
|
||||
const std::vector<std::string>& exts) {
|
||||
// Changed order to not do an substr on empty string
|
||||
if (str.empty()) {
|
||||
return false;
|
||||
} else if (str.substr(0, 2) == "._") {
|
||||
return false;
|
||||
}
|
||||
// Use a more modern way here now
|
||||
// to avoid strcasecmp
|
||||
if (exts.size() != 0) {
|
||||
for (const auto& ext : exts) {
|
||||
if (str.substr(str.length() - ext.length()) == ext) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
PD_CORE_API std::wstring MakeWstring(const std::string& s) {
|
||||
// Manually convert to wstring as they removed wstring_convert :(
|
||||
std::wstring result;
|
||||
size_t i = 0;
|
||||
while (i < s.size()) {
|
||||
uint8_t ch = static_cast<uint8_t>(s[i]);
|
||||
if (ch < 0x80) { // 1-byte chsr
|
||||
result += static_cast<wchar_t>(ch);
|
||||
i++;
|
||||
} else if ((ch >> 5) == 0b110) { // 2-byte char
|
||||
if (i + 1 >= s.size()) {
|
||||
return L""; // return empty if error
|
||||
}
|
||||
wchar_t wc = ((ch & 0x1F) << 6) | (s[i + 1] & 0x3F);
|
||||
result += wc;
|
||||
i += 2;
|
||||
} else if ((ch >> 4) == 0b1110) { // 3-byte char
|
||||
if (i + 2 >= s.size()) {
|
||||
return L""; // return empty if error
|
||||
}
|
||||
wchar_t wc =
|
||||
((ch & 0x0F) << 12) | ((s[i + 1] & 0x3F) << 6) | (s[i + 2] & 0x3F);
|
||||
result += wc;
|
||||
i += 3;
|
||||
} else if ((ch >> 3) == 0b11110) { // 4-byte char
|
||||
if (i + 3 >= s.size()) {
|
||||
return L""; // return empty if error
|
||||
}
|
||||
uint32_t codepoint = ((ch & 0x07) << 18) | ((s[i + 1] & 0x3F) << 12) |
|
||||
((s[i + 2] & 0x3F) << 6) | (s[i + 3] & 0x3F);
|
||||
codepoint -= 0x10000;
|
||||
result += static_cast<wchar_t>(0xD800 + (codepoint >> 10));
|
||||
result += static_cast<wchar_t>(0xDC00 + (codepoint & 0x3FF));
|
||||
i += 4;
|
||||
} else {
|
||||
return L"";
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
PD_CORE_API const std::string FormatNanos(unsigned long long nanos) {
|
||||
// Based on some code of my minecraft plugins
|
||||
if (nanos < 1000) {
|
||||
return std::format("{}ns", nanos);
|
||||
} else if (nanos < 1000000) {
|
||||
unsigned long long micros = nanos / 1000;
|
||||
return std::format("{}us {}ns", micros, nanos % 1000);
|
||||
} else if (nanos < 1000000000) {
|
||||
unsigned long long millis = nanos / 1000000;
|
||||
return std::format("{}ms {}us", millis, (nanos % 1000000) / 1000);
|
||||
} else if (nanos < 60000000000ULL) {
|
||||
unsigned long long seconds = nanos / 1000000000;
|
||||
return std::format("{}s {}ms", seconds, (nanos % 1000000000) / 1000000);
|
||||
} else {
|
||||
unsigned long long minutes = nanos / 60000000000ULL;
|
||||
unsigned long long seconds = (nanos % 60000000000ULL) / 1000000000;
|
||||
return std::format("{}m {}s", minutes, seconds);
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
PD_CORE_API const std::string FormatMillis(unsigned long long millis) {
|
||||
// Original Code can be found in some of my mv plugins
|
||||
if (millis < 1000) {
|
||||
return std::format("{}ms", millis);
|
||||
} else if (millis < 60000) {
|
||||
unsigned long long seconds = millis / 1000;
|
||||
return std::format("{}s {}ms", seconds, (millis % 1000));
|
||||
} else {
|
||||
unsigned long long minutes = millis / 60000;
|
||||
unsigned long long seconds = (millis % 60000) / 1000;
|
||||
return std::format("{}m {}s {}ms", minutes, seconds, (millis % 1000));
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
PD_CORE_API const std::string FormatBytes(unsigned long long bytes) {
|
||||
static const std::vector<std::string> endings = {
|
||||
"B", "KB", "MB", "GB", "TB", "Unk",
|
||||
};
|
||||
int i = 0;
|
||||
double b = bytes;
|
||||
while (b > 1024.0) {
|
||||
i++;
|
||||
b /= 1024;
|
||||
}
|
||||
if (i >= (int)endings.size()) {
|
||||
i = (int)endings.size() - 1;
|
||||
}
|
||||
return std::format("{:.1f} {}", b, endings[i]);
|
||||
}
|
||||
|
||||
PD_CORE_API const std::string GetFileName(const std::string& path,
|
||||
const std::string& saperators) {
|
||||
auto pos = path.find_last_of(saperators);
|
||||
if (pos != path.npos) {
|
||||
return path.substr(pos + 1);
|
||||
}
|
||||
// If No saperator was found return the entire path
|
||||
return path;
|
||||
}
|
||||
|
||||
PD_CORE_API const std::string PathRemoveExtension(const std::string& path) {
|
||||
auto pos = path.find_last_of('.');
|
||||
if (pos != path.npos) {
|
||||
return path.substr(0, pos);
|
||||
}
|
||||
// If No saperator was found return the entire path
|
||||
return path;
|
||||
}
|
||||
|
||||
PD_CORE_API u32 FastHash(const std::string& s) {
|
||||
u32 hash = 5381;
|
||||
for (auto& it : s) {
|
||||
hash = (hash * 33) + static_cast<u8>(it);
|
||||
}
|
||||
return hash;
|
||||
}
|
||||
} // namespace PD::Strings
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/sys.hpp>
|
||||
|
||||
namespace PD::Sys {
|
||||
TraceMap pd_sys_tm;
|
||||
PD_CORE_API u64 GetTime() {
|
||||
return std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
PD_CORE_API u64 GetNanoTime() {
|
||||
return std::chrono::duration_cast<std::chrono::nanoseconds>(
|
||||
std::chrono::steady_clock::now().time_since_epoch())
|
||||
.count();
|
||||
}
|
||||
PD_CORE_API TT::Res::Ref& GetTraceRef(const std::string& id) {
|
||||
// Auto Generate a New if doesnt exist
|
||||
if (pd_sys_tm.find(id) == pd_sys_tm.end()) {
|
||||
pd_sys_tm[id] = TT::Res::New();
|
||||
pd_sys_tm[id]->SetID(id);
|
||||
}
|
||||
return pd_sys_tm[id];
|
||||
}
|
||||
PD_CORE_API bool TraceExist(const std::string& id) {
|
||||
return pd_sys_tm.find(id) != pd_sys_tm.end();
|
||||
}
|
||||
PD_CORE_API TraceMap& GetTraceMap() { return pd_sys_tm; }
|
||||
} // namespace PD::Sys
|
||||
@@ -1,49 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/timer.hpp>
|
||||
|
||||
namespace PD {
|
||||
PD_CORE_API Timer::Timer(bool autostart) {
|
||||
is_running = autostart;
|
||||
Reset();
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Reset() {
|
||||
start = Sys::GetTime();
|
||||
now = start;
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Update() {
|
||||
if (is_running) {
|
||||
now = Sys::GetTime();
|
||||
}
|
||||
}
|
||||
|
||||
PD_CORE_API void Timer::Pause() { is_running = false; }
|
||||
PD_CORE_API void Timer::Rseume() { is_running = true; }
|
||||
PD_CORE_API bool Timer::IsRunning() const { return is_running; }
|
||||
PD_CORE_API u64 Timer::Get() { return now - start; }
|
||||
PD_CORE_API double Timer::GetSeconds() { return double(Get()) / 1000.0; }
|
||||
} // namespace PD
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/sys.hpp>
|
||||
#include <pd/core/timetrace.hpp>
|
||||
|
||||
namespace PD::TT {
|
||||
PD_CORE_API void Beg(const std::string& id) {
|
||||
auto trace = Sys::GetTraceRef(id);
|
||||
trace->SetStart(PD::Sys::GetNanoTime());
|
||||
}
|
||||
|
||||
PD_CORE_API void End(const std::string& id) {
|
||||
auto trace = Sys::GetTraceRef(id);
|
||||
trace->SetEnd(PD::Sys::GetNanoTime());
|
||||
}
|
||||
} // namespace PD::TT
|
||||
4
source/external/stb.cpp
vendored
4
source/external/stb.cpp
vendored
@@ -1,4 +0,0 @@
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <pd/external/stb_image.h>
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#include <pd/external/stb_truetype.h>
|
||||
@@ -1,158 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef PD_IMAGE_BUILD_SHARED
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#endif
|
||||
|
||||
#include <pd/external/stb_image.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <pd/image/image.hpp>
|
||||
#include <pd/image/img_convert.hpp>
|
||||
|
||||
namespace PD {
|
||||
PD_IMAGE_API void Image::Load(const std::string& path) {
|
||||
u8* img = stbi_load(path.c_str(), &pWidth, &pHeight, &fmt, 4);
|
||||
if (fmt == 3) {
|
||||
stbi_image_free(img);
|
||||
img = stbi_load(path.c_str(), &pWidth, &pHeight, &fmt, 3);
|
||||
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 3));
|
||||
pFmt = RGB;
|
||||
stbi_image_free(img);
|
||||
} else if (fmt == 4) {
|
||||
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 4));
|
||||
pFmt = RGBA;
|
||||
stbi_image_free(img);
|
||||
}
|
||||
}
|
||||
PD_IMAGE_API void Image::Load(const std::vector<u8>& buf) {
|
||||
u8* img =
|
||||
stbi_load_from_memory(buf.data(), buf.size(), &pWidth, &pHeight, &fmt, 4);
|
||||
if (fmt == 3) {
|
||||
stbi_image_free(img);
|
||||
img = stbi_load_from_memory(buf.data(), buf.size(), &pWidth, &pHeight, &fmt,
|
||||
3);
|
||||
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 3));
|
||||
pFmt = RGB;
|
||||
stbi_image_free(img);
|
||||
} else if (fmt == 4) {
|
||||
pBuffer = std::vector<PD::u8>(img, img + (pWidth * pHeight * 4));
|
||||
stbi_image_free(img);
|
||||
pFmt = RGBA;
|
||||
}
|
||||
}
|
||||
PD_IMAGE_API void Image::Copy(const std::vector<u8>& buf, int w, int h,
|
||||
int bpp) {
|
||||
this->fmt = bpp;
|
||||
if (buf.size() != (size_t)w * h * bpp) {
|
||||
// Size Error
|
||||
return;
|
||||
}
|
||||
this->pBuffer.resize(w * h * bpp);
|
||||
for (size_t i = 0; i < this->pBuffer.size(); i++) {
|
||||
pBuffer[i] = buf[i];
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void Image::Convert(Image::Ref img, Image::Format dst) {
|
||||
if (img->pFmt == dst) {
|
||||
return;
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::BGR) {
|
||||
ImgConvert::ReverseBuf(img->pBuffer, 3, img->pWidth, img->pHeight);
|
||||
img->pFmt = BGR;
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::RGBA) {
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
img->pBuffer.resize(img->pWidth * img->pHeight * 4);
|
||||
ImgConvert::RGB24toRGBA32(img->pBuffer, cpy, img->pWidth, img->pHeight);
|
||||
img->pFmt = RGBA;
|
||||
} else if (img->pFmt == Image::RGBA && dst == Image::RGB) {
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
img->pBuffer.resize(img->pWidth * img->pHeight * 3);
|
||||
ImgConvert::RGB32toRGBA24(img->pBuffer, cpy, img->pWidth, img->pHeight);
|
||||
img->pFmt = RGB;
|
||||
} else if (img->pFmt == Image::RGBA && dst == Image::RGB565) {
|
||||
Convert(img, Image::RGB);
|
||||
Convert(img, Image::RGB565);
|
||||
} else if (img->pFmt == Image::RGB && dst == Image::RGB565) {
|
||||
auto f = [](u8 r, u8 g, u8 b) -> u16 {
|
||||
u16 _r = (r >> 3);
|
||||
u16 _g = (g >> 2);
|
||||
u16 _b = (b >> 3);
|
||||
return (_r << 11) | (_g << 5) | _b;
|
||||
};
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
img->pBuffer.resize(img->pWidth * img->pHeight * 2);
|
||||
for (int y = 0; y < img->pWidth; y++) {
|
||||
for (int x = 0; x < img->pHeight; x++) {
|
||||
int src = (y * img->pWidth + x) * 3;
|
||||
int dst = (y * img->pWidth + x) * 2;
|
||||
u16 new_px = f(cpy[src + 0], cpy[src + 1], cpy[src + 2]);
|
||||
img->pBuffer[dst + 0] = new_px >> 8;
|
||||
img->pBuffer[dst + 1] = new_px & 0xff;
|
||||
}
|
||||
}
|
||||
img->pFmt = RGB565;
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API int Image::Fmt2Bpp(Format fmt) {
|
||||
switch (fmt) {
|
||||
case RGBA:
|
||||
case ABGR:
|
||||
return 4;
|
||||
break;
|
||||
case RGB:
|
||||
case BGR:
|
||||
return 3;
|
||||
break;
|
||||
case RGB565:
|
||||
return 2;
|
||||
break;
|
||||
|
||||
default:
|
||||
return 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void Image::ReTile(Image::Ref img,
|
||||
std::function<u32(int x, int y, int w)> src,
|
||||
std::function<u32(int x, int y, int w)> dst) {
|
||||
std::vector<PD::u8> cpy = img->pBuffer;
|
||||
/** could use fmt here but for 565 that woulnt work as it is not supported by
|
||||
* file loading where fmt is used */
|
||||
int bpp = Fmt2Bpp(img->pFmt);
|
||||
for (int y = 0; y < img->pHeight; y++) {
|
||||
for (int x = 0; x < img->pWidth; x++) {
|
||||
int src_idx = src(x, y, img->pWidth);
|
||||
int dst_idx = dst(x, y, img->pWidth);
|
||||
for (int i = 0; i < bpp; i++) {
|
||||
img->pBuffer[dst_idx + i] = cpy[src_idx + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,90 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <pd/image/img_blur.hpp>
|
||||
#include <pd/image/img_convert.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace ImgBlur {
|
||||
PD_IMAGE_API std::vector<float> GaussianKernel(int r, float si) {
|
||||
/// Define radius as r to be shorter
|
||||
int size = 2 * r + 1;
|
||||
std::vector<float> kernel(size);
|
||||
float sum = 0.0f;
|
||||
|
||||
for (int i = -r; i <= r; i++) {
|
||||
kernel[i + r] = exp(-0.5f * (i * i) / (si * si));
|
||||
sum += kernel[i + r];
|
||||
}
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
kernel[i] /= sum;
|
||||
}
|
||||
return kernel;
|
||||
}
|
||||
PD_IMAGE_API void GaussianBlur(std::vector<u8> &buf, int w, int h, float radius,
|
||||
float si,
|
||||
std::function<int(int, int, int)> idxfn) {
|
||||
GaussianBlur(buf.data(), w, h, 4, radius, si, idxfn);
|
||||
}
|
||||
|
||||
PD_IMAGE_API void GaussianBlur(void *buf, int w, int h, int bpp, float radius,
|
||||
float si,
|
||||
std::function<int(int, int, int)> idxfn) {
|
||||
if (bpp != 4 && bpp != 3) {
|
||||
return;
|
||||
}
|
||||
std::vector<float> kernel = GaussianKernel(radius, si);
|
||||
int hks = kernel.size() / 2;
|
||||
int end = w * h * bpp;
|
||||
std::vector<unsigned char> res((u8 *)buf, ((u8 *)buf) + end);
|
||||
ImgConvert::Reverse32(res, w, h);
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
float r = 0.0f, g = 0.0f, b = 0.0f;
|
||||
for (int ky = -hks; ky <= hks; ky++) {
|
||||
for (int kx = -hks; kx <= hks; kx++) {
|
||||
int xoff = std::min(std::max(x + kx, 0), w - 1);
|
||||
int yoff = std::min(std::max(y + ky, 0), h - 1);
|
||||
int idx = idxfn(xoff, yoff, w) * 4;
|
||||
|
||||
float weight = kernel[ky + hks] * kernel[kx + hks];
|
||||
r += ((u8 *)buf)[idx] * weight;
|
||||
g += ((u8 *)buf)[idx + 1] * weight;
|
||||
b += ((u8 *)buf)[idx + 2] * weight;
|
||||
}
|
||||
}
|
||||
int idx = idxfn(x, y, w) * bpp;
|
||||
res[idx] = std::min(std::max(int(r), 0), 255);
|
||||
res[idx + 1] = std::min(std::max(int(g), 0), 255);
|
||||
res[idx + 2] = std::min(std::max(int(b), 0), 255);
|
||||
}
|
||||
}
|
||||
ImgConvert::Reverse32(res, w, h);
|
||||
std::memcpy(buf, res.data(), res.size());
|
||||
}
|
||||
} // namespace ImgBlur
|
||||
} // namespace PD
|
||||
@@ -1,137 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/image/img_convert.hpp>
|
||||
|
||||
namespace PD::ImgConvert {
|
||||
|
||||
PD_IMAGE_API void RGB24toRGBA32(std::vector<u8> &out, const std::vector<u8> &in,
|
||||
const int &w, const int &h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 3;
|
||||
int dst = (y * w + x) * 4;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
out[dst + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void RGB32toRGBA24(std::vector<u8> &out, const std::vector<u8> &in,
|
||||
const int &w, const int &h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 4;
|
||||
int dst = (y * w + x) * 3;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void Reverse32(std::vector<u8> &buf, const int &w, const int &h) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int i = y * w + x;
|
||||
u8 t0 = buf[i + 0];
|
||||
u8 t1 = buf[i + 1];
|
||||
buf[i + 0] = buf[i + 3];
|
||||
buf[i + 1] = buf[i + 2];
|
||||
buf[i + 3] = t0;
|
||||
buf[i + 2] = t1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void ReverseBuf(std::vector<u8> &buf, size_t bpp, int w, int h) {
|
||||
std::vector<u8> cpy = buf;
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int pos = (y * w + x) * bpp;
|
||||
for (size_t i = 0; i < bpp; i++) {
|
||||
buf[pos + bpp - 1 - i] = cpy[pos + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void RGB24toRGBA32(PD::Vec<u8> &out, const PD::Vec<u8> &in,
|
||||
const int &w, const int &h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 3;
|
||||
int dst = (y * w + x) * 4;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
out[dst + 3] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void RGB32toRGBA24(PD::Vec<u8> &out, const PD::Vec<u8> &in,
|
||||
const int &w, const int &h) {
|
||||
// Converts RGB24 to RGBA32
|
||||
for (int y = 0; y < h; y++) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
int src = (y * w + x) * 4;
|
||||
int dst = (y * w + x) * 3;
|
||||
out[dst + 0] = in[src + 0];
|
||||
out[dst + 1] = in[src + 1];
|
||||
out[dst + 2] = in[src + 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void Reverse32(PD::Vec<u8> &buf, const int &w, const int &h) {
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int i = y * w + x;
|
||||
u8 t0 = buf[i + 0];
|
||||
u8 t1 = buf[i + 1];
|
||||
buf[i + 0] = buf[i + 3];
|
||||
buf[i + 1] = buf[i + 2];
|
||||
buf[i + 3] = t0;
|
||||
buf[i + 2] = t1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_IMAGE_API void ReverseBuf(PD::Vec<u8> &buf, size_t bpp, int w, int h) {
|
||||
PD::Vec<u8> cpy = buf;
|
||||
for (int x = 0; x < w; x++) {
|
||||
for (int y = 0; y < h; y++) {
|
||||
int pos = (y * w + x) * bpp;
|
||||
for (size_t i = 0; i < bpp; i++) {
|
||||
buf[pos + bpp - 1 - i] = cpy[pos + i];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace PD::ImgConvert
|
||||
@@ -1,147 +0,0 @@
|
||||
#include <pd/lithium/drawlist.hpp>
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
namespace PD {
|
||||
namespace LI {
|
||||
PD_LITHIUM_API Command::Ref DrawList::PreGenerateCmd() {
|
||||
Command::Ref cmd = Command::New();
|
||||
cmd->Layer = Layer;
|
||||
cmd->Index = pDrawList.Size();
|
||||
cmd->Tex = CurrentTex;
|
||||
return cmd;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathArcToN(const fvec2& c, float radius,
|
||||
float a_min, float a_max,
|
||||
int segments) {
|
||||
// Path.push_back(c);
|
||||
PathReserve(segments + 1);
|
||||
for (int i = 0; i < segments; i++) {
|
||||
float a = a_min + ((float)i / (float)segments) * (a_max - a_min);
|
||||
PathAdd(vec2(c.x + std::cos(a) * radius, c.y + std::sin(a) * radius));
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathRect(fvec2 a, fvec2 b, float rounding,
|
||||
u32 flags) {
|
||||
if (rounding == 0.f) {
|
||||
PathAdd(a);
|
||||
PathAdd(vec2(b.x, a.y));
|
||||
PathAdd(b);
|
||||
PathAdd(vec2(a.x, b.y));
|
||||
} else {
|
||||
PathArcToN(vec2(a.x + rounding, a.y + rounding), rounding, 4 * 6, 4 * 9,
|
||||
21);
|
||||
PathArcToN(vec2(b.x - rounding, a.y + rounding), rounding, 4 * 9, 4 * 12,
|
||||
21);
|
||||
PathArcToN(vec2(b.x - rounding, b.y - rounding), rounding, 4 * 0, 4 * 3,
|
||||
21);
|
||||
PathArcToN(vec2(a.x + rounding, b.y - rounding), rounding, 4 * 3, 4 * 6,
|
||||
21);
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawRect(const fvec2& pos, const fvec2& size,
|
||||
u32 color, int thickness) {
|
||||
PathRect(pos, pos + size);
|
||||
// Flags is currently hardcoded (1 = close)
|
||||
PathStroke(color, thickness, 1);
|
||||
}
|
||||
void DrawList::DrawRectFilled(const fvec2& pos, const fvec2& size, u32 color) {
|
||||
PathRect(pos, pos + size);
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawTriangle(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, u32 color,
|
||||
int thickness) {
|
||||
PathAdd(a);
|
||||
PathAdd(b);
|
||||
PathAdd(c);
|
||||
PathStroke(color, thickness, 1);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawTriangleFilled(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, u32 color) {
|
||||
PathAdd(a);
|
||||
PathAdd(b);
|
||||
PathAdd(c);
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawCircle(const fvec2& center, float rad,
|
||||
u32 color, int num_segments,
|
||||
int thickness) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(center, rad, 0.f, am, num_segments);
|
||||
}
|
||||
DrawSolid(); // Only Solid Color Supported
|
||||
PathStroke(color, thickness, (1 << 0));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawCircleFilled(const fvec2& center, float rad,
|
||||
u32 color, int num_segments) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(center, rad, 0.f, am, num_segments);
|
||||
}
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS
|
||||
PD_LITHIUM_API void DrawList::DrawPolyLine(const Vec<fvec2>& points, u32 clr,
|
||||
u32 flags, int thickness) {
|
||||
if (points.Size() < 2) {
|
||||
return;
|
||||
}
|
||||
CurrentTex = WhitePixel;
|
||||
auto cmd = PreGenerateCmd();
|
||||
bool close = (flags & (1 << 0));
|
||||
int num_points = close ? (int)points.Size() : (int)points.Size() - 1;
|
||||
if (flags & (1 << 1)) {
|
||||
// TODO: Find a way to draw less garbage looking lines
|
||||
} else {
|
||||
// Non antialiased lines look awful when rendering with thickness != 1
|
||||
for (int i = 0; i < num_points; i++) {
|
||||
int j = (i + 1) == (int)points.Size() ? 0 : (i + 1);
|
||||
auto line = Renderer::PrimLine(points[i], points[j], thickness);
|
||||
Renderer::CmdQuad(cmd, line, vec4(0.f, 1.f, 1.f, 0.f), clr);
|
||||
}
|
||||
}
|
||||
AddCommand(cmd);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawConvexPolyFilled(const Vec<fvec2>& points,
|
||||
u32 clr) {
|
||||
if (points.Size() < 3) {
|
||||
return; // Need at least three points
|
||||
}
|
||||
auto cmd = PreGenerateCmd();
|
||||
Renderer::CmdConvexPolyFilled(cmd, points, clr, CurrentTex);
|
||||
AddCommand(cmd);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawText(const fvec2& pos,
|
||||
const std::string& text, u32 color) {
|
||||
if (!pCurrentFont) {
|
||||
return;
|
||||
}
|
||||
PD::Vec<Command::Ref> cmds;
|
||||
pCurrentFont->CmdTextEx(cmds, pos, color, pFontScale, text);
|
||||
for (size_t i = 0; i < cmds.Size(); i++) {
|
||||
cmds[i]->Index = pDrawList.Size();
|
||||
cmds[i]->Layer = Layer;
|
||||
AddCommand(cmds[i]);
|
||||
}
|
||||
}
|
||||
} // namespace LI
|
||||
} // namespace PD
|
||||
@@ -1,278 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef PD_LITHIUM_BUILD_SHARED
|
||||
#define STB_TRUETYPE_IMPLEMENTATION
|
||||
#endif
|
||||
#include <pd/external/stb_truetype.h>
|
||||
|
||||
#include <pd/lithium/font.hpp>
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace LI {
|
||||
PD_LITHIUM_API void Font::LoadTTF(const std::string &path, int height) {
|
||||
TT::Scope st("LI_LoadTTF_" + path);
|
||||
PixelHeight = height; // Set internel pixel height
|
||||
// Use NextPow2 to be able to use sizes between for example 16 and 32
|
||||
// before it only was possible to use 8, 16, 32, 64 as size
|
||||
int texszs = BitUtil::GetPow2(height * 16);
|
||||
// Load stbtt
|
||||
stbtt_fontinfo inf;
|
||||
std::ifstream loader(path, std::ios::binary);
|
||||
if (!loader.is_open()) return;
|
||||
loader.seekg(0, std::ios::end);
|
||||
size_t len = loader.tellg();
|
||||
loader.seekg(0, std::ios::beg);
|
||||
unsigned char *buffer = new unsigned char[len];
|
||||
loader.read(reinterpret_cast<char *>(buffer), len);
|
||||
loader.close();
|
||||
stbtt_InitFont(&inf, buffer, 0);
|
||||
// clang-format off
|
||||
// Disable clang here cause dont want a garbage looking line
|
||||
std::vector<PD::u8> font_tex(texszs * texszs * 4); // Create font Texture
|
||||
// clang-format on
|
||||
float scale = stbtt_ScaleForPixelHeight(&inf, PixelHeight);
|
||||
|
||||
int ascent, descent, lineGap;
|
||||
stbtt_GetFontVMetrics(&inf, &ascent, &descent, &lineGap);
|
||||
int baseline = static_cast<int>(ascent * scale);
|
||||
|
||||
std::map<u32, int> buf_cache; // Cache to not render same codepoint tex twice
|
||||
|
||||
/// Load Codepoints
|
||||
auto tex = Texture::New();
|
||||
fvec2 off;
|
||||
for (u32 ii = 0x0000; ii < 0xFFFF; ii++) {
|
||||
int i = stbtt_FindGlyphIndex(&inf, ii);
|
||||
if (i == 0) {
|
||||
continue;
|
||||
}
|
||||
if (stbtt_IsGlyphEmpty(&inf, i)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Codepoint c;
|
||||
int w = 0, h = 0, xo = 0, yo = 0;
|
||||
unsigned char *bitmap =
|
||||
stbtt_GetCodepointBitmap(&inf, scale, scale, i, &w, &h, &xo, &yo);
|
||||
int x0, y0, x1, y1;
|
||||
stbtt_GetCodepointBitmapBox(&inf, i, scale, scale, &x0, &y0, &x1, &y1);
|
||||
|
||||
// Check if Codepoint exists as hash and if it is use its already written
|
||||
// data
|
||||
u32 hashed_map = IO::HashMemory(std::vector<u8>(bitmap, bitmap + (w * h)));
|
||||
if (buf_cache.find(hashed_map) != buf_cache.end()) {
|
||||
c = GetCodepoint(buf_cache[hashed_map]);
|
||||
c.pCodepoint = i;
|
||||
CodeMap[i] = c;
|
||||
free(bitmap);
|
||||
continue;
|
||||
} else {
|
||||
buf_cache[hashed_map] = i;
|
||||
}
|
||||
|
||||
if (off.x + w > texszs) {
|
||||
off.y += PixelHeight;
|
||||
off.x = 0;
|
||||
}
|
||||
|
||||
// Set UV Data
|
||||
fvec4 uvs;
|
||||
uvs.x = static_cast<float>(off.x) / texszs;
|
||||
uvs.y = static_cast<float>(off.y) / texszs;
|
||||
uvs.z = static_cast<float>((off.x + w) / texszs);
|
||||
uvs.w = static_cast<float>((off.y + h) / texszs);
|
||||
if (pBackend->Flags & LIBackendFlags_FlipUV_Y) {
|
||||
uvs.y = 1.f - uvs.y;
|
||||
uvs.w = 1.f - uvs.w;
|
||||
}
|
||||
c.SimpleUV = uvs;
|
||||
c.Tex = tex;
|
||||
c.Size = fvec2(w, h);
|
||||
c.Offset = baseline + yo;
|
||||
|
||||
// Render glyph
|
||||
for (int y = 0; y < h; ++y) {
|
||||
for (int x = 0; x < w; ++x) {
|
||||
int map_pos = (((off.y + y) * texszs + (off.x + x))) * 4;
|
||||
font_tex[map_pos + 0] = 255;
|
||||
font_tex[map_pos + 1] = 255;
|
||||
font_tex[map_pos + 2] = 255;
|
||||
font_tex[map_pos + 3] = bitmap[x + y * w];
|
||||
}
|
||||
}
|
||||
|
||||
free(bitmap);
|
||||
CodeMap[i] = c;
|
||||
|
||||
// Small Patch to avoid some possible artifacts
|
||||
off.x += w + 1;
|
||||
if (off.x + w > texszs) {
|
||||
off.y += PixelHeight;
|
||||
if (off.y + PixelHeight > texszs) {
|
||||
break;
|
||||
}
|
||||
off.x = 0;
|
||||
}
|
||||
}
|
||||
// Load the Texture and append to list
|
||||
{
|
||||
auto t = pBackend->LoadTexture(font_tex, texszs, texszs, Texture::RGBA32,
|
||||
Texture::LINEAR);
|
||||
tex->CopyOther(t);
|
||||
}
|
||||
Textures.push_back(tex);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Font::Codepoint &Font::GetCodepoint(u32 cp) {
|
||||
// Check if codepoijt exist or return a static invalid one
|
||||
auto res = CodeMap.find(cp);
|
||||
if (res == CodeMap.end()) {
|
||||
static Codepoint invalid;
|
||||
invalid.pInvalid = true;
|
||||
return invalid;
|
||||
}
|
||||
return res->second;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string &text, float scale) {
|
||||
// Use wstring for exemple for german äöü
|
||||
auto wtext = Strings::MakeWstring(text);
|
||||
// Create a temp position and offset as [0, 0]
|
||||
fvec2 res;
|
||||
float x = 0;
|
||||
// Curent Font Scale
|
||||
float cfs = (DefaultPixelHeight * scale) / (float)PixelHeight;
|
||||
float lh = (float)PixelHeight * cfs;
|
||||
size_t index = 0;
|
||||
for (auto &it : wtext) {
|
||||
if (it == '\0') {
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
auto cp = GetCodepoint(it);
|
||||
if (cp.pInvalid && it != '\n' && it != '\t' && it != ' ') {
|
||||
continue;
|
||||
}
|
||||
switch (it) {
|
||||
case '\n':
|
||||
res.y += lh;
|
||||
res.x = std::max(res.x, x);
|
||||
x = 0.f;
|
||||
break;
|
||||
case '\t':
|
||||
x += 16 * cfs;
|
||||
break;
|
||||
case ' ':
|
||||
x += 2 * cfs;
|
||||
// Fall trough here to get the same result as in
|
||||
// TextCommand if/else Section
|
||||
default:
|
||||
x += cp.Size.x * cfs;
|
||||
if (index != wtext.size()) {
|
||||
x += 2 * cfs;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
res.x = std::max(res.x, x);
|
||||
res.y += lh;
|
||||
return res;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::CmdTextEx(Vec<Command::Ref> &cmds, const fvec2 &pos,
|
||||
u32 color, float scale,
|
||||
const std::string &text, LITextFlags flags,
|
||||
const fvec2 &box) {
|
||||
fvec2 off;
|
||||
float cfs = (DefaultPixelHeight * scale) / (float)PixelHeight;
|
||||
float lh = (float)PixelHeight * cfs;
|
||||
fvec2 td;
|
||||
fvec2 rpos = pos;
|
||||
fvec2 rbox = box;
|
||||
if (flags & (LITextFlags_AlignMid | LITextFlags_AlignRight)) {
|
||||
td = GetTextBounds(text, scale);
|
||||
}
|
||||
if (flags & LITextFlags_AlignMid) {
|
||||
rpos = rbox * 0.5 - td * 0.5 + pos;
|
||||
}
|
||||
if (flags & LITextFlags_AlignRight) {
|
||||
rpos.x = rpos.x - td.x;
|
||||
}
|
||||
|
||||
std::vector<std::string> lines;
|
||||
std::istringstream iss(text);
|
||||
std::string tmp;
|
||||
while (std::getline(iss, tmp)) {
|
||||
lines.push_back(tmp);
|
||||
}
|
||||
|
||||
for (auto &it : lines) {
|
||||
/*if (flags & LITextFlags_Short) {
|
||||
fvec2 tmp_dim;
|
||||
it = ShortText(it, box.x() - pos.x(), tmp_dim);
|
||||
}*/
|
||||
auto wline = Strings::MakeWstring(it);
|
||||
auto cmd = Command::New();
|
||||
auto Tex = GetCodepoint(wline[0]).Tex;
|
||||
cmd->Tex = Tex;
|
||||
for (auto &jt : wline) {
|
||||
auto cp = GetCodepoint(jt);
|
||||
if ((cp.pInvalid && jt != '\n' && jt != '\t') && jt != '\r') {
|
||||
continue;
|
||||
}
|
||||
if (Tex != cp.Tex) {
|
||||
cmds.Add(cmd);
|
||||
cmd = Command::New();
|
||||
Tex = cp.Tex;
|
||||
cmd->Tex = Tex;
|
||||
}
|
||||
if (jt == '\t') {
|
||||
off.x += 16 * cfs;
|
||||
} else {
|
||||
if (jt != ' ') {
|
||||
if (flags & LITextFlags_Shaddow) {
|
||||
// Draw
|
||||
Rect rec = Renderer::PrimRect(
|
||||
rpos + vec2(off.x + 1, off.x + (cp.Offset * cfs)) + 1,
|
||||
cp.Size * cfs, 0.f);
|
||||
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, 0xff111111);
|
||||
}
|
||||
// Draw
|
||||
Rect rec = Renderer::PrimRect(
|
||||
rpos + off + fvec2(0, (cp.Offset * cfs)), cp.Size * cfs, 0.f);
|
||||
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, color);
|
||||
} else {
|
||||
off.x += 2 * cfs;
|
||||
}
|
||||
off.x += cp.Size.x * cfs + 2 * cfs;
|
||||
}
|
||||
}
|
||||
cmds.Add(cmd);
|
||||
off.y += lh;
|
||||
off.x = 0;
|
||||
}
|
||||
}
|
||||
} // namespace LI
|
||||
} // namespace PD
|
||||
@@ -1,152 +0,0 @@
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace LI {
|
||||
PD_LITHIUM_API Renderer::Renderer(Backend::Ref backend) {
|
||||
pBackend = backend;
|
||||
std::vector<PD::u8> white(16 * 16 * 4, 0xff);
|
||||
WhitePixel = pBackend->LoadTexture(white, 16, 16);
|
||||
CurrentTex = WhitePixel; // Make sure to have a texture set
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::Render() {
|
||||
pBackend->NewFrame();
|
||||
pBackend->RenderDrawData(DrawList);
|
||||
DrawList.Clear();
|
||||
for (auto it = pDrawLists.Begin(); it != pDrawLists.End(); it++) {
|
||||
pBackend->RenderDrawData((*it)->pDrawList);
|
||||
(*it)->Clear();
|
||||
}
|
||||
pDrawLists.Clear();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& pos, const fvec2& szs,
|
||||
const fvec4& rect) {
|
||||
return (pos.x + szs.x >= rect.x && pos.y + szs.y >= rect.y &&
|
||||
pos.x <= rect.z && pos.y <= rect.w);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& pos, const fvec4& rect) {
|
||||
return (pos.x > rect.x && pos.x < rect.x + rect.z && pos.y > rect.y &&
|
||||
pos.y < rect.y + rect.w);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& alpha, const fvec2& bravo,
|
||||
const fvec2& charlie, const fvec4& rect) {
|
||||
return ((alpha.x < rect.z && bravo.x < rect.z && charlie.x < rect.z) ||
|
||||
(alpha.y < rect.w && bravo.y < rect.w && charlie.y < rect.w) ||
|
||||
(alpha.x > 0 && bravo.x > 0 && charlie.x > 0) ||
|
||||
(alpha.y > 0 && bravo.y > 0 && charlie.y > 0));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::RotateCorner(fvec2& pos, float sinus,
|
||||
float cosinus) {
|
||||
float x = pos.x * cosinus - pos.y * sinus;
|
||||
float y = pos.y * cosinus - pos.x * sinus;
|
||||
pos = fvec2(x, y);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Rect Renderer::PrimRect(const fvec2& pos, const fvec2& size,
|
||||
float angle) {
|
||||
fvec2 c = size * 0.5f; // Center
|
||||
fvec2 corner[4] = {
|
||||
fvec2(-c.x, -c.y),
|
||||
fvec2(-c.x + size.x, -c.y),
|
||||
fvec2(-c.x, -c.y + size.y),
|
||||
fvec2(-c.x + size.x, -c.y + size.y),
|
||||
};
|
||||
|
||||
// Only rotate if required
|
||||
if (angle != 0.f) {
|
||||
float s = std::sin(angle);
|
||||
float co = std::cos(angle);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
RotateCorner(corner[i], s, co);
|
||||
}
|
||||
}
|
||||
|
||||
// Return Result
|
||||
return Rect(corner[0] + pos + c, corner[1] + pos + c, corner[2] + pos + c,
|
||||
corner[3] + pos + c);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Rect Renderer::PrimLine(const fvec2& a, const fvec2& b,
|
||||
int thickness) {
|
||||
// Using the vec maths api makes the code as short as it is
|
||||
vec2 dir = a - b;
|
||||
float len = dir.Len();
|
||||
vec2 unit_dir = dir / len;
|
||||
vec2 perpendicular(-unit_dir.y, unit_dir.x);
|
||||
vec2 off = perpendicular * ((float)thickness * 0.5f);
|
||||
|
||||
return Rect(a + off, b + off, a - off, b - off);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::CmdQuad(Command::Ref cmd, const Rect& quad,
|
||||
const Rect& uv, u32 color) {
|
||||
cmd->AppendIndex(0).AppendIndex(1).AppendIndex(2);
|
||||
cmd->AppendIndex(0).AppendIndex(2).AppendIndex(3);
|
||||
cmd->AppendVertex(Vertex(quad.BotRight(), uv.BotRight(), color));
|
||||
cmd->AppendVertex(Vertex(quad.TopRight(), uv.TopRight(), color));
|
||||
cmd->AppendVertex(Vertex(quad.TopLeft(), uv.TopLeft(), color));
|
||||
cmd->AppendVertex(Vertex(quad.BotLeft(), uv.BotLeft(), color));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::CmdTriangle(Command::Ref cmd, const fvec2 a,
|
||||
const fvec2 b, const fvec2 c,
|
||||
u32 clr) {
|
||||
cmd->AppendIndex(2).AppendIndex(1).AppendIndex(0);
|
||||
cmd->AppendVertex(Vertex(a, vec2(0.f, 1.f), clr));
|
||||
cmd->AppendVertex(Vertex(b, vec2(1.f, 1.f), clr));
|
||||
cmd->AppendVertex(Vertex(c, vec2(1.f, 0.f), clr));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Command::Ref Renderer::PreGenerateCmd() {
|
||||
Command::Ref res = Command::New();
|
||||
res->Index = DrawList.Size();
|
||||
res->Layer = Layer;
|
||||
res->Tex = CurrentTex;
|
||||
return res;
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS (Probably make it with a define as it
|
||||
// would probably be faster to render out of screen than checking if
|
||||
// it could be skipped)
|
||||
PD_LITHIUM_API void Renderer::CmdConvexPolyFilled(Command::Ref cmd,
|
||||
const Vec<fvec2>& points,
|
||||
u32 clr, Texture::Ref tex) {
|
||||
if (points.Size() < 3 || tex == nullptr) {
|
||||
return; // Need at least three points
|
||||
}
|
||||
|
||||
// Support for Custom Textures (UV calculation)
|
||||
float minX = points[0].x, minY = points[0].y;
|
||||
float maxX = minX, maxY = minY;
|
||||
// Check for the max and min Positions
|
||||
for (auto it = points.Begin(); it != points.End(); it++) {
|
||||
if ((*it).x < minX) minX = (*it).x;
|
||||
if ((*it).y < minY) minY = (*it).y;
|
||||
if ((*it).x > maxX) maxX = (*it).x;
|
||||
if ((*it).y > maxY) maxY = (*it).y;
|
||||
}
|
||||
// Get Short defines for UV
|
||||
// (Bottom Right is not required)
|
||||
auto uv_tl = tex->UV.TopLeft();
|
||||
auto uv_tr = tex->UV.TopRight();
|
||||
auto uv_bl = tex->UV.BotLeft();
|
||||
|
||||
// Render
|
||||
for (int i = 2; i < (int)points.Size(); i++) {
|
||||
cmd->AppendIndex(0).AppendIndex(i).AppendIndex(i - 1);
|
||||
}
|
||||
for (int i = 0; i < (int)points.Size(); i++) {
|
||||
// Calculate U and V coords
|
||||
float u =
|
||||
uv_tl.x + ((points[i].x - minX) / (maxX - minX)) * (uv_tr.x - uv_tl.x);
|
||||
float v =
|
||||
uv_tl.y + ((points[i].y - minY) / (maxY - minY)) * (uv_bl.y - uv_tl.y);
|
||||
cmd->AppendVertex(LI::Vertex(points[i], fvec2(u, v), clr));
|
||||
}
|
||||
}
|
||||
} // namespace LI
|
||||
} // namespace PD
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
/** Need to outsource this into the backend */
|
||||
|
||||
#include <pd/net/backend.hpp>
|
||||
#include <pd/net/socket.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Net {
|
||||
PD_NET_API bool Socket::Create() {
|
||||
pSocket = backend->NewSocket();
|
||||
return pSocket != backend->GetInvalidRef();
|
||||
}
|
||||
PD_NET_API bool Socket::Bind(u16 port) { return backend->Bind(pSocket, port); }
|
||||
|
||||
PD_NET_API bool Socket::Listen(int backlog) {
|
||||
return backend->Listen(pSocket, backlog);
|
||||
}
|
||||
|
||||
PD_NET_API bool Socket::WaitForRead(int timeout_ms) {
|
||||
return backend->WaitForRead(pSocket, timeout_ms);
|
||||
}
|
||||
|
||||
PD_NET_API bool Socket::Accept(Socket::Ref client) {
|
||||
return backend->Accept(pSocket, client);
|
||||
}
|
||||
|
||||
PD_NET_API bool Socket::Connect(const std::string& ip, u16 port) {
|
||||
return backend->Connect(pSocket, ip, port);
|
||||
}
|
||||
|
||||
PD_NET_API int Socket::Send(const std::string& data) {
|
||||
return backend->Send(pSocket, data);
|
||||
}
|
||||
|
||||
PD_NET_API int Socket::Receive(std::string& data, int size) {
|
||||
return backend->Receive(pSocket, data, size);
|
||||
}
|
||||
|
||||
PD_NET_API void Socket::Close() {
|
||||
if (IsValid()) {
|
||||
backend->Close(pSocket);
|
||||
pSocket = backend->GetInvalidRef();
|
||||
}
|
||||
}
|
||||
|
||||
PD_NET_API bool Socket::IsValid() const {
|
||||
return pSocket != backend->GetInvalidRef();
|
||||
}
|
||||
} // namespace Net
|
||||
} // namespace PD
|
||||
@@ -1,605 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/external/json.hpp>
|
||||
#include <pd/overlays/keyboard.hpp>
|
||||
// #include <pd/lib3ds/gamepad_icons.hpp>
|
||||
|
||||
namespace PD {
|
||||
struct Key {
|
||||
Key(const std::string& key, const fvec2& p, const fvec2& s,
|
||||
Keyboard::KeyOperation o) {
|
||||
k = key;
|
||||
pos = p;
|
||||
size = s;
|
||||
op = o;
|
||||
}
|
||||
std::string k;
|
||||
fvec2 pos;
|
||||
fvec2 size;
|
||||
Keyboard::KeyOperation op;
|
||||
};
|
||||
|
||||
using Layout = std::vector<Key>;
|
||||
Layout layouts[3] = {
|
||||
{
|
||||
// 1st row
|
||||
Key("`", fvec2(5, 0), 18, Keyboard::AppendSelf),
|
||||
Key("1", fvec2(25, 0), 18, Keyboard::AppendSelf),
|
||||
Key("2", fvec2(45, 0), 18, Keyboard::AppendSelf),
|
||||
Key("3", fvec2(65, 0), 18, Keyboard::AppendSelf),
|
||||
Key("4", fvec2(85, 0), 18, Keyboard::AppendSelf),
|
||||
Key("5", fvec2(105, 0), 18, Keyboard::AppendSelf),
|
||||
Key("6", fvec2(125, 0), 18, Keyboard::AppendSelf),
|
||||
Key("7", fvec2(145, 0), 18, Keyboard::AppendSelf),
|
||||
Key("8", fvec2(165, 0), 18, Keyboard::AppendSelf),
|
||||
Key("9", fvec2(185, 0), 18, Keyboard::AppendSelf),
|
||||
Key("0", fvec2(205, 0), 18, Keyboard::AppendSelf),
|
||||
Key("-", fvec2(225, 0), 18, Keyboard::AppendSelf),
|
||||
Key("=", fvec2(245, 0), 18, Keyboard::AppendSelf),
|
||||
Key("<---", fvec2(265, 0), fvec2(50, 18), Keyboard::Backspace),
|
||||
// 2nd row
|
||||
Key("Tab", fvec2(5, 20), fvec2(40, 18), Keyboard::Tab),
|
||||
Key("q", fvec2(47, 20), 18, Keyboard::AppendSelf),
|
||||
Key("w", fvec2(67, 20), 18, Keyboard::AppendSelf),
|
||||
Key("e", fvec2(87, 20), 18, Keyboard::AppendSelf),
|
||||
Key("r", fvec2(107, 20), 18, Keyboard::AppendSelf),
|
||||
Key("t", fvec2(127, 20), 18, Keyboard::AppendSelf),
|
||||
Key("y", fvec2(147, 20), 18, Keyboard::AppendSelf),
|
||||
Key("u", fvec2(167, 20), 18, Keyboard::AppendSelf),
|
||||
Key("i", fvec2(187, 20), 18, Keyboard::AppendSelf),
|
||||
Key("o", fvec2(207, 20), 18, Keyboard::AppendSelf),
|
||||
Key("p", fvec2(227, 20), 18, Keyboard::AppendSelf),
|
||||
Key("[", fvec2(247, 20), 18, Keyboard::AppendSelf),
|
||||
Key("]", fvec2(267, 20), 18, Keyboard::AppendSelf),
|
||||
Key("\\", fvec2(287, 20), fvec2(28, 18), Keyboard::AppendSelf),
|
||||
// 3rd row
|
||||
Key("Caps", fvec2(5, 40), fvec2(50, 18), Keyboard::Caps),
|
||||
Key("a", fvec2(57, 40), 18, Keyboard::AppendSelf),
|
||||
Key("s", fvec2(77, 40), 18, Keyboard::AppendSelf),
|
||||
Key("d", fvec2(97, 40), 18, Keyboard::AppendSelf),
|
||||
Key("f", fvec2(117, 40), 18, Keyboard::AppendSelf),
|
||||
Key("g", fvec2(137, 40), 18, Keyboard::AppendSelf),
|
||||
Key("h", fvec2(157, 40), 18, Keyboard::AppendSelf),
|
||||
Key("j", fvec2(177, 40), 18, Keyboard::AppendSelf),
|
||||
Key("k", fvec2(197, 40), 18, Keyboard::AppendSelf),
|
||||
Key("l", fvec2(217, 40), 18, Keyboard::AppendSelf),
|
||||
Key(";", fvec2(237, 40), 18, Keyboard::AppendSelf),
|
||||
Key("'", fvec2(257, 40), 18, Keyboard::AppendSelf),
|
||||
Key("Enter", fvec2(277, 40), fvec2(38, 18), Keyboard::Enter),
|
||||
// 4th row
|
||||
Key("Shift", fvec2(5, 60), fvec2(60, 18), Keyboard::Shift),
|
||||
Key("z", fvec2(67, 60), 18, Keyboard::AppendSelf),
|
||||
Key("x", fvec2(87, 60), 18, Keyboard::AppendSelf),
|
||||
Key("c", fvec2(107, 60), 18, Keyboard::AppendSelf),
|
||||
Key("v", fvec2(127, 60), 18, Keyboard::AppendSelf),
|
||||
Key("b", fvec2(147, 60), 18, Keyboard::AppendSelf),
|
||||
Key("n", fvec2(167, 60), 18, Keyboard::AppendSelf),
|
||||
Key("m", fvec2(187, 60), 18, Keyboard::AppendSelf),
|
||||
Key(",", fvec2(207, 60), 18, Keyboard::AppendSelf),
|
||||
Key(".", fvec2(227, 60), 18, Keyboard::AppendSelf),
|
||||
Key("/", fvec2(247, 60), 18, Keyboard::AppendSelf),
|
||||
Key("Shift", fvec2(267, 60), fvec2(48, 18), Keyboard::Shift),
|
||||
// 5th row
|
||||
Key("Cancel", fvec2(5, 80), fvec2(70, 18), Keyboard::OpCancel),
|
||||
Key("(X)", fvec2(77, 80), fvec2(23, 18), Keyboard::Op1),
|
||||
Key("Space", fvec2(102, 80), fvec2(108, 18), Keyboard::Space),
|
||||
Key("(!)", fvec2(212, 80), fvec2(23, 18), Keyboard::Op2),
|
||||
Key("Confirm", fvec2(237, 80), fvec2(78, 18), Keyboard::OpConfirm),
|
||||
},
|
||||
{
|
||||
// 1st row
|
||||
Key("`", fvec2(5, 0), 18, Keyboard::AppendSelf),
|
||||
Key("1", fvec2(25, 0), 18, Keyboard::AppendSelf),
|
||||
Key("2", fvec2(45, 0), 18, Keyboard::AppendSelf),
|
||||
Key("3", fvec2(65, 0), 18, Keyboard::AppendSelf),
|
||||
Key("4", fvec2(85, 0), 18, Keyboard::AppendSelf),
|
||||
Key("5", fvec2(105, 0), 18, Keyboard::AppendSelf),
|
||||
Key("6", fvec2(125, 0), 18, Keyboard::AppendSelf),
|
||||
Key("7", fvec2(145, 0), 18, Keyboard::AppendSelf),
|
||||
Key("8", fvec2(165, 0), 18, Keyboard::AppendSelf),
|
||||
Key("9", fvec2(185, 0), 18, Keyboard::AppendSelf),
|
||||
Key("0", fvec2(205, 0), 18, Keyboard::AppendSelf),
|
||||
Key("-", fvec2(225, 0), 18, Keyboard::AppendSelf),
|
||||
Key("=", fvec2(245, 0), 18, Keyboard::AppendSelf),
|
||||
Key("<---", fvec2(265, 0), fvec2(50, 18), Keyboard::Backspace),
|
||||
// 2nd row
|
||||
Key("Tab", fvec2(5, 20), fvec2(40, 18), Keyboard::Tab),
|
||||
Key("Q", fvec2(47, 20), 18, Keyboard::AppendSelf),
|
||||
Key("W", fvec2(67, 20), 18, Keyboard::AppendSelf),
|
||||
Key("E", fvec2(87, 20), 18, Keyboard::AppendSelf),
|
||||
Key("R", fvec2(107, 20), 18, Keyboard::AppendSelf),
|
||||
Key("T", fvec2(127, 20), 18, Keyboard::AppendSelf),
|
||||
Key("Y", fvec2(147, 20), 18, Keyboard::AppendSelf),
|
||||
Key("U", fvec2(167, 20), 18, Keyboard::AppendSelf),
|
||||
Key("I", fvec2(187, 20), 18, Keyboard::AppendSelf),
|
||||
Key("O", fvec2(207, 20), 18, Keyboard::AppendSelf),
|
||||
Key("P", fvec2(227, 20), 18, Keyboard::AppendSelf),
|
||||
Key("[", fvec2(247, 20), 18, Keyboard::AppendSelf),
|
||||
Key("]", fvec2(267, 20), 18, Keyboard::AppendSelf),
|
||||
Key("\\", fvec2(287, 20), fvec2(28, 18), Keyboard::AppendSelf),
|
||||
// 3rd row
|
||||
Key("Caps", fvec2(5, 40), fvec2(50, 18), Keyboard::Caps),
|
||||
Key("A", fvec2(57, 40), 18, Keyboard::AppendSelf),
|
||||
Key("S", fvec2(77, 40), 18, Keyboard::AppendSelf),
|
||||
Key("D", fvec2(97, 40), 18, Keyboard::AppendSelf),
|
||||
Key("F", fvec2(117, 40), 18, Keyboard::AppendSelf),
|
||||
Key("G", fvec2(137, 40), 18, Keyboard::AppendSelf),
|
||||
Key("H", fvec2(157, 40), 18, Keyboard::AppendSelf),
|
||||
Key("J", fvec2(177, 40), 18, Keyboard::AppendSelf),
|
||||
Key("K", fvec2(197, 40), 18, Keyboard::AppendSelf),
|
||||
Key("L", fvec2(217, 40), 18, Keyboard::AppendSelf),
|
||||
Key(";", fvec2(237, 40), 18, Keyboard::AppendSelf),
|
||||
Key("'", fvec2(257, 40), 18, Keyboard::AppendSelf),
|
||||
Key("Enter", fvec2(277, 40), fvec2(38, 18), Keyboard::Enter),
|
||||
// 4th row
|
||||
Key("Shift", fvec2(5, 60), fvec2(60, 18), Keyboard::Shift),
|
||||
Key("Z", fvec2(67, 60), 18, Keyboard::AppendSelf),
|
||||
Key("X", fvec2(87, 60), 18, Keyboard::AppendSelf),
|
||||
Key("C", fvec2(107, 60), 18, Keyboard::AppendSelf),
|
||||
Key("V", fvec2(127, 60), 18, Keyboard::AppendSelf),
|
||||
Key("B", fvec2(147, 60), 18, Keyboard::AppendSelf),
|
||||
Key("N", fvec2(167, 60), 18, Keyboard::AppendSelf),
|
||||
Key("M", fvec2(187, 60), 18, Keyboard::AppendSelf),
|
||||
Key(",", fvec2(207, 60), 18, Keyboard::AppendSelf),
|
||||
Key(".", fvec2(227, 60), 18, Keyboard::AppendSelf),
|
||||
Key("/", fvec2(247, 60), 18, Keyboard::AppendSelf),
|
||||
Key("Shift", fvec2(267, 60), fvec2(48, 18), Keyboard::Shift),
|
||||
// 5th row
|
||||
Key("Cancel", fvec2(5, 80), fvec2(70, 18), Keyboard::OpCancel),
|
||||
Key("(X)", fvec2(77, 80), fvec2(23, 18), Keyboard::Op1),
|
||||
Key("Space", fvec2(102, 80), fvec2(108, 18), Keyboard::Space),
|
||||
Key("(!)", fvec2(212, 80), fvec2(23, 18), Keyboard::Op2),
|
||||
Key("Confirm", fvec2(237, 80), fvec2(78, 18), Keyboard::OpConfirm),
|
||||
},
|
||||
{
|
||||
// 1st row
|
||||
Key("~", fvec2(5, 0), 18, Keyboard::AppendSelf),
|
||||
Key("!", fvec2(25, 0), 18, Keyboard::AppendSelf),
|
||||
Key("@", fvec2(45, 0), 18, Keyboard::AppendSelf),
|
||||
Key("#", fvec2(65, 0), 18, Keyboard::AppendSelf),
|
||||
Key("$", fvec2(85, 0), 18, Keyboard::AppendSelf),
|
||||
Key("%", fvec2(105, 0), 18, Keyboard::AppendSelf),
|
||||
Key("^", fvec2(125, 0), 18, Keyboard::AppendSelf),
|
||||
Key("&", fvec2(145, 0), 18, Keyboard::AppendSelf),
|
||||
Key("*", fvec2(165, 0), 18, Keyboard::AppendSelf),
|
||||
Key("(", fvec2(185, 0), 18, Keyboard::AppendSelf),
|
||||
Key(")", fvec2(205, 0), 18, Keyboard::AppendSelf),
|
||||
Key("_", fvec2(225, 0), 18, Keyboard::AppendSelf),
|
||||
Key("+", fvec2(245, 0), 18, Keyboard::AppendSelf),
|
||||
Key("<---", fvec2(265, 0), fvec2(50, 18), Keyboard::Backspace),
|
||||
// 2nd row
|
||||
Key("Tab", fvec2(5, 20), fvec2(40, 18), Keyboard::Tab),
|
||||
Key("Q", fvec2(47, 20), 18, Keyboard::AppendSelf),
|
||||
Key("W", fvec2(67, 20), 18, Keyboard::AppendSelf),
|
||||
Key("E", fvec2(87, 20), 18, Keyboard::AppendSelf),
|
||||
Key("R", fvec2(107, 20), 18, Keyboard::AppendSelf),
|
||||
Key("T", fvec2(127, 20), 18, Keyboard::AppendSelf),
|
||||
Key("Y", fvec2(147, 20), 18, Keyboard::AppendSelf),
|
||||
Key("U", fvec2(167, 20), 18, Keyboard::AppendSelf),
|
||||
Key("I", fvec2(187, 20), 18, Keyboard::AppendSelf),
|
||||
Key("O", fvec2(207, 20), 18, Keyboard::AppendSelf),
|
||||
Key("P", fvec2(227, 20), 18, Keyboard::AppendSelf),
|
||||
Key("{", fvec2(247, 20), 18, Keyboard::AppendSelf),
|
||||
Key("}", fvec2(267, 20), 18, Keyboard::AppendSelf),
|
||||
Key("|", fvec2(287, 20), fvec2(28, 18), Keyboard::AppendSelf),
|
||||
// 3rd row
|
||||
Key("Caps", fvec2(5, 40), fvec2(50, 18), Keyboard::Caps),
|
||||
Key("A", fvec2(57, 40), 18, Keyboard::AppendSelf),
|
||||
Key("S", fvec2(77, 40), 18, Keyboard::AppendSelf),
|
||||
Key("D", fvec2(97, 40), 18, Keyboard::AppendSelf),
|
||||
Key("F", fvec2(117, 40), 18, Keyboard::AppendSelf),
|
||||
Key("G", fvec2(137, 40), 18, Keyboard::AppendSelf),
|
||||
Key("H", fvec2(157, 40), 18, Keyboard::AppendSelf),
|
||||
Key("J", fvec2(177, 40), 18, Keyboard::AppendSelf),
|
||||
Key("K", fvec2(197, 40), 18, Keyboard::AppendSelf),
|
||||
Key("L", fvec2(217, 40), 18, Keyboard::AppendSelf),
|
||||
Key(":", fvec2(237, 40), 18, Keyboard::AppendSelf),
|
||||
Key("\"", fvec2(257, 40), 18, Keyboard::AppendSelf),
|
||||
Key("Enter", fvec2(277, 40), fvec2(38, 18), Keyboard::Enter),
|
||||
// 4th row
|
||||
Key("Shift", fvec2(5, 60), fvec2(60, 18), Keyboard::Shift),
|
||||
Key("Z", fvec2(67, 60), 18, Keyboard::AppendSelf),
|
||||
Key("X", fvec2(87, 60), 18, Keyboard::AppendSelf),
|
||||
Key("C", fvec2(107, 60), 18, Keyboard::AppendSelf),
|
||||
Key("V", fvec2(127, 60), 18, Keyboard::AppendSelf),
|
||||
Key("B", fvec2(147, 60), 18, Keyboard::AppendSelf),
|
||||
Key("N", fvec2(167, 60), 18, Keyboard::AppendSelf),
|
||||
Key("M", fvec2(187, 60), 18, Keyboard::AppendSelf),
|
||||
Key("<", fvec2(207, 60), 18, Keyboard::AppendSelf),
|
||||
Key(">", fvec2(227, 60), 18, Keyboard::AppendSelf),
|
||||
Key("?", fvec2(247, 60), 18, Keyboard::AppendSelf),
|
||||
Key("Shift", fvec2(267, 60), fvec2(48, 18), Keyboard::Shift),
|
||||
// 5th row
|
||||
Key("Cancel", fvec2(5, 80), fvec2(70, 18), Keyboard::OpCancel),
|
||||
Key("(X)", fvec2(77, 80), fvec2(23, 18), Keyboard::Op1),
|
||||
Key("Space", fvec2(102, 80), fvec2(108, 18), Keyboard::Space),
|
||||
Key("(!)", fvec2(212, 80), fvec2(23, 18), Keyboard::Op2),
|
||||
Key("Confirm", fvec2(237, 80), fvec2(78, 18), Keyboard::OpConfirm),
|
||||
},
|
||||
};
|
||||
|
||||
void DumpLayout(const std::string& path) {
|
||||
nlohmann::json l0;
|
||||
l0["name"] = "Default US";
|
||||
for (int i = 0; i < 3; i++) {
|
||||
nlohmann::json l1;
|
||||
for (size_t j = 0; j < layouts[0].size(); j++) {
|
||||
nlohmann::json key;
|
||||
key["display_char"] = layouts[i][j].k;
|
||||
key["pos_x"] = layouts[i][j].pos.x;
|
||||
key["pos_y"] = layouts[i][j].pos.y;
|
||||
key["size_x"] = layouts[i][j].size.x;
|
||||
key["size_y"] = layouts[i][j].size.y;
|
||||
key["op"] = layouts[i][j].op;
|
||||
l1.push_back(key);
|
||||
}
|
||||
l0[std::to_string(i)] = l1;
|
||||
}
|
||||
std::ofstream off(path);
|
||||
off << l0.dump(3);
|
||||
off.close();
|
||||
}
|
||||
|
||||
/// The Only One (too) is a static var to make sur
|
||||
/// THe Keyboard can only exist once in the overlay mgr
|
||||
int Keyboard::too = 0;
|
||||
|
||||
void Keyboard::MoveSelector() {
|
||||
/// Move from Current position to New Position
|
||||
selector.From(selector).To(layouts[0][raw_sel].pos).In(0.1f);
|
||||
/// If Button Size Changed, animate to the new size
|
||||
if (cselszs != layouts[0][raw_sel].size) {
|
||||
cselszs = layouts[0][raw_sel].size;
|
||||
sel_szs.Swap();
|
||||
sel_szs.To(cselszs).In(0.1);
|
||||
}
|
||||
}
|
||||
|
||||
void Keyboard::LoadTheKeys(LI::Renderer::Ref ren) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
keys[i] = LI::StaticObject::New();
|
||||
for (auto it : layouts[i]) {
|
||||
vec2 pos = it.pos + it.size * 0.5 - ren->GetTextDimensions(it.k) * 0.5;
|
||||
auto c = LI::Command::New();
|
||||
auto r = ren->CreateRect(it.pos, it.size, 0.f);
|
||||
int l = ren->Layer();
|
||||
ren->UseTex();
|
||||
ren->Layer(1);
|
||||
ren->SetupCommand(c);
|
||||
ren->QuadCommand(c, r, vec4(0.f, 1.f, 1.f, 0.f), 0xff444444);
|
||||
keys[i]->PushCommand(c);
|
||||
ren->Layer(2);
|
||||
ren->TextCommand(keys[i]->List(), pos, 0xffffffff, it.k, 0, 0);
|
||||
ren->Layer(l);
|
||||
}
|
||||
ren->OptiCommandList(keys[i]->List());
|
||||
}
|
||||
keys_loadet = true;
|
||||
}
|
||||
|
||||
void Keyboard::Movement(Hid::Ref inp) {
|
||||
/// Any Key if no selector
|
||||
if (raw_sel < 0) {
|
||||
/// Initial Selector PopUp
|
||||
if (inp->IsUp((Hid::Key)(inp->Up | inp->Down | inp->Left | inp->Right))) {
|
||||
raw_sel = 0;
|
||||
vec2 dst = layouts[0][0].pos;
|
||||
cselszs = layouts[0][0].size;
|
||||
selector.As(selector.Linear).From(dst + (cselszs * 0.5)).To(dst).In(0.1f);
|
||||
sel_szs.As(sel_szs.Linear).From(0).To(cselszs).In(0.1f);
|
||||
}
|
||||
} else {
|
||||
/// Go Up Movement
|
||||
if (inp->IsUp(inp->Up)) {
|
||||
vec2 tpos = layouts[0][raw_sel].pos;
|
||||
vec2 tsize = layouts[0][raw_sel].size;
|
||||
float tcen = tpos.x() + (tsize.x() * 0.5);
|
||||
int bidx = -1;
|
||||
float min_diff = std::numeric_limits<float>::max();
|
||||
float try_ = -1;
|
||||
|
||||
int start = raw_sel - 1;
|
||||
if (tpos.y() == layouts[0][0].pos.y()) {
|
||||
start = (int)layouts[0].size();
|
||||
}
|
||||
|
||||
for (int i = start; i >= 0; i--) {
|
||||
auto& tk = layouts[0][i];
|
||||
|
||||
if (tk.pos.y() != tpos.y()) {
|
||||
if (try_ == -1) {
|
||||
try_ = tk.pos.y();
|
||||
}
|
||||
if (tk.pos.y() != try_) {
|
||||
break;
|
||||
}
|
||||
|
||||
float tcenl = tk.pos.x() + (tk.size.x() * 0.5);
|
||||
float diff = std::abs(tcen - tcenl);
|
||||
if (diff < min_diff) {
|
||||
min_diff = diff;
|
||||
bidx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bidx != -1) {
|
||||
raw_sel = bidx;
|
||||
}
|
||||
MoveSelector();
|
||||
}
|
||||
|
||||
/// Go Down Movement
|
||||
if (inp->IsUp(inp->Down)) {
|
||||
vec2 tpos = layouts[0][raw_sel].pos;
|
||||
vec2 tsize = layouts[0][raw_sel].size;
|
||||
float tcen = tpos.x() + (tsize.x() * 0.5);
|
||||
int bidx = -1;
|
||||
float min_diff = std::numeric_limits<float>::max();
|
||||
float try_ = -1;
|
||||
|
||||
int start = raw_sel + 1;
|
||||
if (tpos.y() == layouts[0][layouts[0].size() - 1].pos.y()) {
|
||||
start = 0;
|
||||
}
|
||||
|
||||
for (int i = start; i < (int)layouts[0].size(); i++) {
|
||||
auto& tk = layouts[0][i];
|
||||
|
||||
if (tk.pos.y() != tpos.y()) {
|
||||
if (try_ == -1) {
|
||||
try_ = tk.pos.y();
|
||||
}
|
||||
if (tk.pos.y() != try_) {
|
||||
break;
|
||||
}
|
||||
|
||||
float tcenl = tk.pos.x() + (tk.size.x() * 0.5);
|
||||
float diff = std::abs(tcen - tcenl);
|
||||
if (diff < min_diff) {
|
||||
min_diff = diff;
|
||||
bidx = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (bidx != -1) {
|
||||
raw_sel = bidx;
|
||||
}
|
||||
MoveSelector();
|
||||
}
|
||||
/// Go right movement
|
||||
if (inp->IsUp(inp->Right)) {
|
||||
if ((raw_sel + 1 >= (int)layouts[0].size()) ||
|
||||
layouts[0][raw_sel].pos.y() != layouts[0][raw_sel + 1].pos.y()) {
|
||||
for (int i = raw_sel - 1; i > 0; i--) {
|
||||
if (i - 1 <= 0) {
|
||||
raw_sel = 0;
|
||||
break;
|
||||
}
|
||||
if (layouts[0][i].pos.y() != layouts[0][i - 1].pos.y()) {
|
||||
raw_sel = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
raw_sel++;
|
||||
}
|
||||
MoveSelector();
|
||||
}
|
||||
// Go left Movement
|
||||
if (inp->IsUp(inp->Left)) {
|
||||
if (raw_sel - 1 < 0 ||
|
||||
layouts[0][raw_sel].pos.y() != layouts[0][raw_sel - 1].pos.y()) {
|
||||
for (int i = raw_sel; i < (int)layouts[0].size(); i++) {
|
||||
if (i >= (int)layouts[0].size()) {
|
||||
raw_sel = (int)layouts[0].size();
|
||||
}
|
||||
if (layouts[0][i].pos.y() != layouts[0][i + 1].pos.y()) {
|
||||
raw_sel = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
raw_sel--;
|
||||
}
|
||||
MoveSelector();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Keyboard::DoOperation(KeyOperation op, const std::string& kname) {
|
||||
switch (op) {
|
||||
case AppendSelf:
|
||||
*text += kname;
|
||||
if (mode == 2) {
|
||||
mode = 0;
|
||||
}
|
||||
break;
|
||||
case OpCancel:
|
||||
Rem();
|
||||
*text = copy;
|
||||
break;
|
||||
case OpConfirm:
|
||||
Rem();
|
||||
break;
|
||||
case Shift:
|
||||
mode = mode == 2 ? 0 : 2;
|
||||
break;
|
||||
case Caps:
|
||||
mode = mode == 1 ? 0 : 1;
|
||||
break;
|
||||
case Backspace: {
|
||||
std::string c = *text;
|
||||
*text = c.substr(0, c.size() - 1);
|
||||
} break;
|
||||
case Space:
|
||||
*text += ' ';
|
||||
break;
|
||||
case Tab:
|
||||
*text += '\t';
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void Keyboard::RecolorBy(KeyOperation op, u32 color, int cm) {
|
||||
int i = 0;
|
||||
/// Not the fastest but the best for custom layouts
|
||||
for (auto& it : layouts[cm]) {
|
||||
if (it.op == op) {
|
||||
keys[cm]->ReColorQuad(i, PD::Color(0xaa222222));
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
void Keyboard::InputOpBind(Hid::Key k, KeyOperation op, Hid::Ref inp, int cm) {
|
||||
if (inp->IsUp(k)) {
|
||||
RecolorBy(op, PD::Color(0xaa222222), cm);
|
||||
DoOperation(op, "");
|
||||
} else if (inp->IsHeld(k)) {
|
||||
RecolorBy(op, PD::Color(0xaa333333), cm);
|
||||
}
|
||||
}
|
||||
|
||||
void Keyboard::Update(float delta, LI::Renderer::Ref ren, Hid::Ref inp) {
|
||||
/// Load Keys if not present
|
||||
if (!keys_loadet) {
|
||||
inp->Clear();
|
||||
LoadTheKeys(ren);
|
||||
}
|
||||
/// Unlock Input
|
||||
inp->Unlock();
|
||||
/// Kill Overlay if rem was toggeled and
|
||||
/// Animation is finished
|
||||
if (rem && flymgr.IsFinished()) {
|
||||
// Should be already unlocked ...
|
||||
// ist mit aber egal
|
||||
inp->Unlock();
|
||||
Kill();
|
||||
return; // Break to not lock again
|
||||
}
|
||||
/// Process Controller Movement
|
||||
Movement(inp);
|
||||
|
||||
/// Update animations
|
||||
flymgr.Update(delta);
|
||||
selector.Update(delta);
|
||||
sel_szs.Update(delta);
|
||||
/// Blend Top or|and Bottom Screen
|
||||
if (flags & Flags_BlendBottom || flags & Flags_BlendTop) {
|
||||
Color fade(0.3f, 0.3f, 0.3f);
|
||||
if (rem) {
|
||||
fade.a(fade.a() * (1.f - flymgr.Progress()));
|
||||
} else {
|
||||
fade.a(fade.a() * flymgr.Progress());
|
||||
}
|
||||
if (flags & Flags_BlendTop) {
|
||||
ren->OnScreen(ren->GetScreen(false));
|
||||
ren->DrawRectSolid(0, fvec2(400, 240), fade);
|
||||
}
|
||||
if (flags & Flags_BlendBottom) {
|
||||
ren->OnScreen(ren->GetScreen(true));
|
||||
ren->DrawRectSolid(0, fvec2(320, 240), fade);
|
||||
}
|
||||
}
|
||||
/// Get the current start possition
|
||||
vec2 start = flymgr;
|
||||
// Draw head and Keyboard background
|
||||
ren->DrawRectSolid(
|
||||
fvec2(0, start.y()), fvec2(320, 125),
|
||||
PD::Color("#222222ff").a((flags & Flags_Transparency) ? 0xaa : 0xff));
|
||||
ren->DrawRectSolid(fvec2(0, start.y()), fvec2(320, 17), 0xaa000000);
|
||||
/// Grab the base layer and go one up for texts
|
||||
int l = ren->Layer();
|
||||
ren->Layer(l + 2);
|
||||
// if (ren->Font()->SystemFont()) {
|
||||
// std::stringstream s;
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::B) << " Backspace ";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::Y) << " Space\n";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::X) << " Cancel ";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::Start) << " Confirm\n";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::L) << " Shift ";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::R) << " CAPS\n";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::Dpad) << " Move ";
|
||||
// s << GamePadIcons::GetIcon(GamePadIcons::A) << " Select\n";
|
||||
// ren->DrawText(fvec2(5, start.y() -
|
||||
// ren->GetTextDimensions(s.str()).y()+16),
|
||||
// 0xffffffff, s.str());
|
||||
// }
|
||||
ren->DrawText(fvec2(5, start.y()), 0xffffffff, "> " + *text);
|
||||
ren->Layer(l + 1);
|
||||
/// Offset Keys start height by 22
|
||||
start[1] += 22;
|
||||
/// Cache Mode to not render on 0, 0
|
||||
int cm = mode;
|
||||
keys[cm]->ReCopy();
|
||||
int ii = 0;
|
||||
for (auto& it : layouts[mode]) {
|
||||
PD::Color bgc(0xaa444444);
|
||||
if (((ren->InBox(inp->TouchPosLast(), vec4(start + it.pos, it.size)) &&
|
||||
inp->IsHeld(inp->Touch)) ||
|
||||
(inp->IsHeld(inp->A) && ii == raw_sel)) &&
|
||||
flymgr.IsFinished()) {
|
||||
bgc = PD::Color(0xaa333333);
|
||||
}
|
||||
if (((ren->InBox(inp->TouchPosLast(), vec4(start + it.pos, it.size)) &&
|
||||
inp->IsUp(inp->Touch)) ||
|
||||
(inp->IsUp(inp->A) && ii == raw_sel)) &&
|
||||
flymgr.IsFinished()) {
|
||||
bgc = PD::Color(0xaa222222);
|
||||
DoOperation(it.op, it.k);
|
||||
}
|
||||
/// This is hardcoded shit guessing that the
|
||||
/// Buttons are in the beginning of the list
|
||||
/// (Should be the case as OptiCmdList sorts by layer)
|
||||
keys[cm]->ReColorQuad(ii, bgc);
|
||||
ii++;
|
||||
}
|
||||
|
||||
// Bind Key Operations
|
||||
InputOpBind(inp->B, Backspace, inp, cm);
|
||||
InputOpBind(inp->Y, Space, inp, cm);
|
||||
InputOpBind(inp->Start, OpConfirm, inp, cm);
|
||||
InputOpBind(inp->X, OpCancel, inp, cm);
|
||||
InputOpBind(inp->L, Shift, inp, cm);
|
||||
InputOpBind(inp->R, Caps, inp, cm);
|
||||
|
||||
if (raw_sel != -1) {
|
||||
ren->Layer(l);
|
||||
ren->DrawRectSolid(start + selector - fvec2(1), fvec2(sel_szs) + fvec2(2),
|
||||
0xaaffffff);
|
||||
ren->Layer(l);
|
||||
}
|
||||
keys[cm]->ReLayer(l);
|
||||
keys[cm]->MoveIt(start);
|
||||
for (auto it : keys[cm]->List()) {
|
||||
ren->PushCommand(it);
|
||||
}
|
||||
inp->Lock();
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,147 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/overlays/message_mgr.hpp>
|
||||
|
||||
namespace PD {
|
||||
MessageMgr::Container::Container(const std::string& title,
|
||||
const std::string& msg) {
|
||||
this->title = title;
|
||||
this->msg = msg;
|
||||
size = fvec2(150, 50);
|
||||
// Precalculate colors
|
||||
col_bg = PD::Color("#111111aa");
|
||||
col_text = PD::Color("#ffffff");
|
||||
// Setup Flyin Animation
|
||||
FlyIn();
|
||||
}
|
||||
void MessageMgr::Container::Render(PD::LI::Renderer::Ref ren) {
|
||||
// Create a Temp var to not recalculate
|
||||
// the position everytime we use it
|
||||
// even if the calculation would always
|
||||
// result in the same we would waste a lot
|
||||
// of cpu performance which is a big issue
|
||||
// espeacilly on the Old3ds...
|
||||
fvec2 tpos = pos;
|
||||
// Check if it goes out of screen
|
||||
// Instant kills cause it will never be on
|
||||
// Screen agains
|
||||
if (tpos.y + size.y < 0) {
|
||||
kill = true;
|
||||
}
|
||||
// If should be removed modify the color by fade
|
||||
// Use a temp var as well to not call it twice
|
||||
if (tbr) {
|
||||
float f = 1.f - pos.Progress();
|
||||
col_bg.a(col_bg.a() * f);
|
||||
col_text.a(col_text.a() * f);
|
||||
}
|
||||
// Create a backup Layer to Render
|
||||
// Text onto the next layer
|
||||
//int l = ren->Layer();
|
||||
ren->DrawRectSolid(tpos, size, col_bg);
|
||||
ren->Layer(l + 1);
|
||||
ren->DrawText(tpos + vec2(4, 2), col_text, title);
|
||||
ren->DrawText(tpos + vec2(4, 16), col_text, msg);
|
||||
ren->Layer(l);
|
||||
}
|
||||
void MessageMgr::Container::Update(int slot, float delta) {
|
||||
// Increase lifetime
|
||||
lifetime += delta / 1000.f;
|
||||
// Trigger move up Animation if
|
||||
// the slot got changed
|
||||
if (s != slot) {
|
||||
ToBeMoved(slot);
|
||||
s = slot;
|
||||
}
|
||||
// Update the animations
|
||||
pos.Update(delta);
|
||||
// Trigger the remove Event if lifetime
|
||||
// goes beyond 4 secods
|
||||
if (lifetime > 4 && !tbr) {
|
||||
ToBeRemoved();
|
||||
}
|
||||
}
|
||||
void MessageMgr::Container::FlyIn() {
|
||||
// Come from out of the screen to 5, 185 into
|
||||
// The screen as EaseInSine in 0.5 seconds
|
||||
pos.From(vec2(-size[0], 240 - size[1] - 5))
|
||||
.To(vec2(5, 240 - size[1] - 5))
|
||||
.In(0.5)
|
||||
.As(pos.EaseInSine);
|
||||
}
|
||||
void MessageMgr::Container::ToBeMoved(int slot) {
|
||||
// Bit more special
|
||||
// Get the current pos as temp one
|
||||
vec2 tpos = pos;
|
||||
// Calculate a temp Startpos which
|
||||
// is the endpos of Flyin if
|
||||
// it is fully playd (see next comment)
|
||||
float spos = 240 - size[1] - 5;
|
||||
// Now from the Current Position Move up by
|
||||
// The Number of slot * the size.y + 5
|
||||
// and make sure it flys diagonal if the
|
||||
// Flyin hasn't ended yet. This animation uses EaseInSine
|
||||
// And does it's move in 0.4 seconds to not have
|
||||
// The new one and this one in collision for to much time
|
||||
pos.From(tpos)
|
||||
.To(vec2(5, spos - slot * (size[1] + 5)))
|
||||
.As(pos.EaseInSine)
|
||||
.In(0.4);
|
||||
}
|
||||
|
||||
void MessageMgr::Container::ToBeRemoved() {
|
||||
// This effect uses EaseOutSine and as well uses the fade anim
|
||||
tbr = true;
|
||||
// We Force set the Position to Finished to avoid collision
|
||||
// to the ToBeMoved Animation And then set an EaseOutSine in 0.5
|
||||
// seconds to move the Message 30 pixels up while fading out
|
||||
pos.Finish();
|
||||
vec2 tpos = pos;
|
||||
pos.From(tpos).To(tpos - vec2(0, 30)).As(pos.EaseOutSine).In(0.5);
|
||||
}
|
||||
|
||||
void MessageMgr::Push(const std::string& title, const std::string& text) {
|
||||
// Simply Add a New Message Container
|
||||
msgs.push_back(Container::New(title, text));
|
||||
}
|
||||
|
||||
void MessageMgr::Update(float delta) {
|
||||
ren->OnScreen(ren->GetScreen(false));
|
||||
for (size_t i = 0; i < msgs.size(); i++) {
|
||||
// Update the Animation Handlers and Move older
|
||||
// Messages up if a new one got pushed
|
||||
msgs[i]->Update(msgs.size() - i - 1, delta);
|
||||
msgs[i]->Render(ren);
|
||||
}
|
||||
/// OMG HE LOOPS TWICE OVER THE OBJECTS TO
|
||||
/// CHECK IF THEY CAN BE REMOVED WOW.......
|
||||
/// Just my Stupid fix for the flickering
|
||||
/// Of the other messages if one get's removed
|
||||
for (size_t i = 0; i < msgs.size(); i++) {
|
||||
if (msgs[i]->ShouldBeRemoved()) {
|
||||
msgs.erase(msgs.begin() + i);
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/overlays/overlay_mgr.hpp>
|
||||
|
||||
namespace PD {
|
||||
void OverlayMgr::Push(Overlay::Ref overlay) { overlays.push_back(overlay); }
|
||||
void OverlayMgr::Update(float delta) {
|
||||
for (size_t i = 0; i < overlays.size(); i++) {
|
||||
if (overlays[i]->IsKilled()) {
|
||||
overlays.erase(overlays.begin() + i);
|
||||
continue;
|
||||
}
|
||||
overlays[i]->Update(delta, ren, inp);
|
||||
}
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,70 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/strings.hpp>
|
||||
#include <pd/core/sys.hpp>
|
||||
#include <pd/overlays/performance.hpp>
|
||||
|
||||
namespace PD {
|
||||
int Performance::too = 0;
|
||||
|
||||
void Performance::Update(float delta, LI::Renderer::Ref ren, Hid::Ref inp) {
|
||||
if (*skill) {
|
||||
Kill();
|
||||
}
|
||||
ren->OnScreen(ren->GetScreen(*screen));
|
||||
ren->TextScale(0.6);
|
||||
vec2 pos;
|
||||
Line(pos, std::format("{:.1f} FPS / {:.2f}ms", 1000.f / delta, delta), ren);
|
||||
Line(pos, "Ren [AVG]: " + TSA("LI_RenderAll"), ren);
|
||||
Line(pos, "App [AVG]: " + TSA("App_MainLoop"), ren);
|
||||
Line(pos, "Ovl [AVG]: " + TSA("Ovl_Update"), ren);
|
||||
Line(pos,
|
||||
"VI: [" + std::to_string(ren->Vertices()) + ", " +
|
||||
std::to_string(ren->Indices()) + "]",
|
||||
ren);
|
||||
Line(pos,
|
||||
"DC: [" + std::to_string(ren->DrawCalls()) + ", " +
|
||||
std::to_string(ren->Commands()) + "]",
|
||||
ren);
|
||||
Line(pos, "AST: " + std::to_string(ren->AstUsage()), ren);
|
||||
Line(pos, "TMS: " + std::to_string(ren->TmsUsage()), ren);
|
||||
ren->DefaultTextScale();
|
||||
}
|
||||
|
||||
void Performance::Line(vec2& pos, const std::string& text,
|
||||
LI::Renderer::Ref ren) {
|
||||
auto tbs = ren->GetTextDimensions(text);
|
||||
int l = ren->Layer();
|
||||
ren->DrawRectSolid(pos, tbs, 0xaa000000);
|
||||
ren->Layer(l + 1);
|
||||
ren->DrawText(pos, 0xffff00ff, text);
|
||||
ren->Layer(l);
|
||||
pos[1] += tbs[1]; // Auto set new pos
|
||||
}
|
||||
|
||||
std::string Performance::TSA(const std::string& id) {
|
||||
return PD::Strings::FormatNanos(
|
||||
PD::Sys::GetTraceRef(id)->GetProtocol()->GetAverage());
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,40 +0,0 @@
|
||||
#include <pd/overlays/settings.hpp>
|
||||
|
||||
namespace PD {
|
||||
int SettingsMenu::too = 0;
|
||||
void SettingsMenu::Update(float delta, LI::Renderer::Ref ren, Hid::Ref inp) {
|
||||
if (!ctx) {
|
||||
ctx = UI7::Context::New(ren, inp);
|
||||
ctx->RootLayer(70);
|
||||
}
|
||||
flymgr.Update(delta);
|
||||
if (rem && flymgr.IsFinished()) {
|
||||
this->Kill();
|
||||
}
|
||||
ren->OnScreen(ren->GetScreen(false));
|
||||
if (ctx->BeginMenu("Palladium - Settings", UI7MenuFlags_CenterTitle)) {
|
||||
auto m = ctx->GetCurrentMenu();
|
||||
m->PushAlignment(UI7Align_Center);
|
||||
m->SeparatorText("Library Info");
|
||||
m->Label(LibInfo::CompiledWith());
|
||||
m->Join();
|
||||
m->Label(LibInfo::CxxVersion());
|
||||
m->Join();
|
||||
m->Label("Version: " + LibInfo::Version() + "[" + LibInfo::Commit() + "]");
|
||||
m->Join();
|
||||
m->Label("Build Time: " + LibInfo::BuildTime());
|
||||
m->JoinAlign(UI7Align_Mid);
|
||||
ctx->EndMenu();
|
||||
}
|
||||
ren->OnScreen(ren->GetScreen(true));
|
||||
if (ctx->BeginMenu("pdovlssettings", UI7MenuFlags_NoTitlebar)) {
|
||||
auto m = ctx->GetCurrentMenu();
|
||||
m->SeparatorText("Settings");
|
||||
if (m->Button("Exit")) {
|
||||
this->Rem();
|
||||
}
|
||||
ctx->EndMenu();
|
||||
}
|
||||
ctx->Update(delta);
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,72 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/sound/mp3.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Music {
|
||||
int Mp3Decoder::Init(const std::string& path) {
|
||||
int ret = 0;
|
||||
int encoding = 0;
|
||||
if ((ret = mpg123_init() != MPG123_OK)) {
|
||||
return ret;
|
||||
}
|
||||
if ((handle = mpg123_new(nullptr, &ret)) == nullptr) {
|
||||
return ret;
|
||||
}
|
||||
int cnls = 0;
|
||||
long _rate = 0;
|
||||
if (mpg123_open(handle, path.c_str()) != MPG123_OK ||
|
||||
mpg123_getformat(handle, &_rate, &cnls, &encoding)) {
|
||||
return ret;
|
||||
}
|
||||
rate = _rate;
|
||||
channels = cnls;
|
||||
mpg123_format_none(handle);
|
||||
mpg123_format(handle, rate, channels, encoding);
|
||||
buf_size = mpg123_outblock(handle) * 16;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void Mp3Decoder::Deinit() {
|
||||
mpg123_close(handle);
|
||||
mpg123_delete(handle);
|
||||
mpg123_exit();
|
||||
}
|
||||
|
||||
u32 Mp3Decoder::GetSampleRate() { return rate; }
|
||||
u8 Mp3Decoder::GetChannels() { return channels; }
|
||||
u64 Mp3Decoder::Decode(u16* buf_address) {
|
||||
size_t done = 0;
|
||||
mpg123_read(handle, buf_address, buf_size, &done);
|
||||
return done / sizeof(u16);
|
||||
}
|
||||
size_t Mp3Decoder::GetFileSamples() {
|
||||
off_t len = mpg123_length(handle);
|
||||
if (len != MPG123_ERR) {
|
||||
return len * size_t(channels);
|
||||
}
|
||||
return -1; // NotExist
|
||||
}
|
||||
} // namespace Music
|
||||
} // namespace PD
|
||||
@@ -1,64 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/button.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Button::HandleInput() {
|
||||
/// Ensure to only check input once
|
||||
if (inp_done) {
|
||||
return;
|
||||
}
|
||||
/// Ensure it gets sed to false and stays if not pressed
|
||||
pressed = false;
|
||||
color = UI7Color_Button;
|
||||
// Assert(screen.get(), "Screen is not set up!");
|
||||
// if (screen->ScreenType() == Screen::Bottom) {
|
||||
if (io->InputHandler->DragObject(this->GetID(), vec4(FinalPos(), size))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
color = UI7Color_ButtonActive;
|
||||
pressed = true;
|
||||
} else {
|
||||
color = UI7Color_ButtonHovered;
|
||||
}
|
||||
}
|
||||
//}
|
||||
inp_done = true;
|
||||
}
|
||||
PD_UI7_API void Button::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
list->AddRectangle(FinalPos(), size, io->Theme->Get(color));
|
||||
list->Layer++;
|
||||
list->AddText(FinalPos() + size * 0.5 - tdim * 0.5, label,
|
||||
io->Theme->Get(UI7Color_Text));
|
||||
list->Layer--;
|
||||
}
|
||||
|
||||
PD_UI7_API void Button::Update() {
|
||||
// Assert(io.get(), "Did you run Container::Init correctly?");
|
||||
this->SetSize(tdim + io->FramePadding);
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,66 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/checkbox.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Checkbox::HandleInput() {
|
||||
/// Ensure to only check input once
|
||||
if (inp_done) {
|
||||
return;
|
||||
}
|
||||
color = UI7Color_FrameBackground;
|
||||
/// Ensure it gets sed to false and stays if not pressed
|
||||
// Assert(screen.get(), "Screen is not set up!");
|
||||
// if (screen->ScreenType() == Screen::Bottom) {
|
||||
if (io->InputHandler->DragObject(this->GetID(), vec4(FinalPos(), size))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
color = UI7Color_FrameBackgroundHovered;
|
||||
usr_ref = !usr_ref;
|
||||
} else {
|
||||
color = UI7Color_FrameBackgroundHovered;
|
||||
}
|
||||
}
|
||||
//}
|
||||
inp_done = true;
|
||||
}
|
||||
PD_UI7_API void Checkbox::Draw() {
|
||||
// Assert(list.get() && io.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
list->AddRectangle(FinalPos(), cbs, io->Theme->Get(color));
|
||||
if (usr_ref) {
|
||||
list->AddRectangle(FinalPos() + 2, cbs - 4,
|
||||
io->Theme->Get(UI7Color_Checkmark));
|
||||
}
|
||||
list->AddText(
|
||||
FinalPos() + fvec2(cbs.x + io->ItemSpace.x, cbs.y * 0.5 - tdim.y * 0.5),
|
||||
label, io->Theme->Get(UI7Color_Text));
|
||||
}
|
||||
|
||||
PD_UI7_API void Checkbox::Update() {
|
||||
// Assert(io.get(), "Did you run Container::Init correctly?");
|
||||
this->SetSize(cbs + fvec2(tdim.x + io->ItemSpace.x, 0));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,65 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/coloredit.hpp>
|
||||
#include <pd/ui7/container/label.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void ColorEdit::HandleInput() {
|
||||
/// Ensure to only check input once
|
||||
if (inp_done) {
|
||||
return;
|
||||
}
|
||||
// Assert(screen.get(), "Screen is not set up!");
|
||||
// if (screen->ScreenType() == Screen::Bottom) {
|
||||
if (io->InputHandler->DragObject(this->GetID(), vec4(FinalPos(), size))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
is_shown = !is_shown;
|
||||
}
|
||||
}
|
||||
//}
|
||||
inp_done = true;
|
||||
}
|
||||
PD_UI7_API void ColorEdit::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
list->AddRectangle(FinalPos(), fvec2(20, 20), *color_ref);
|
||||
list->AddText(FinalPos() + fvec2(io->ItemSpace.x + 20, 0), label,
|
||||
io->Theme->Get(UI7Color_Text));
|
||||
if (is_shown) {
|
||||
if (!layout) {
|
||||
layout = Layout::New(GetID(), io);
|
||||
}
|
||||
layout->AddObject(PD::New<Label>("Hello World!", io));
|
||||
layout->Update();
|
||||
io->RegisterDrawList(GetID(), layout->GetDrawList());
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void ColorEdit::Update() {
|
||||
// Assert(io.get(), "Did you run Container::Init correctly?");
|
||||
this->SetSize(fvec2(tdim.x + io->ItemSpace.x + 20, 20));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,44 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/container.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Container::HandleScrolling(fvec2 scrolling, fvec4 viewport) {
|
||||
if (last_use != 0 && Sys::GetTime() - last_use > 5000) {
|
||||
rem = true;
|
||||
}
|
||||
last_use = Sys::GetTime();
|
||||
pos -= fvec2(0, scrolling.y);
|
||||
skippable = !LI::Renderer::InBox(
|
||||
pos, size,
|
||||
fvec4(viewport.x, viewport.y, viewport.x + viewport.z,
|
||||
viewport.y + viewport.w));
|
||||
}
|
||||
|
||||
PD_UI7_API void Container::HandleInternalInput() {
|
||||
/** Requires Handle Scrolling First */
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,115 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/dragdata.hpp>
|
||||
#include <pd/ui7/container/label.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
// Setup Supported Datatypes (Probably making this Object
|
||||
// header only to not care about datatype support)
|
||||
template class PD_UI7_API DragData<float>;
|
||||
template class PD_UI7_API DragData<int>;
|
||||
template class PD_UI7_API DragData<double>;
|
||||
template class PD_UI7_API DragData<u8>;
|
||||
template class PD_UI7_API DragData<u16>;
|
||||
template class PD_UI7_API DragData<u32>;
|
||||
template class PD_UI7_API DragData<u64>;
|
||||
template <typename T>
|
||||
PD_UI7_API void DragData<T>::HandleInput() {
|
||||
/// Ensure to only check input once
|
||||
if (inp_done) {
|
||||
return;
|
||||
}
|
||||
// Assert(screen.get(), "Screen is not set up!");
|
||||
// if (screen->ScreenType() == Screen::Bottom) {
|
||||
float off_x = 0;
|
||||
for (size_t i = 0; i < elm_count; i++) {
|
||||
std::string p;
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
p = std::format("{:.{}f}", data[i], precision);
|
||||
} else {
|
||||
p = std::format("{}", data[i]);
|
||||
}
|
||||
vec2 tdim = io->Font->GetTextBounds(p, io->FontScale);
|
||||
// Unsafe but is the fastest solution
|
||||
if (io->InputHandler->DragObject(
|
||||
this->GetID() + i + 1,
|
||||
fvec4(FinalPos() + fvec2(off_x, 0), tdim + io->FramePadding))) {
|
||||
data[i] = std::clamp(
|
||||
T(data[i] + (step * (io->InputHandler->DragPosition.x -
|
||||
io->InputHandler->DragLastPosition.x))),
|
||||
this->min, this->max);
|
||||
}
|
||||
off_x += tdim.x + io->ItemSpace.x + io->FramePadding.x;
|
||||
}
|
||||
//}
|
||||
inp_done = true;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PD_UI7_API void DragData<T>::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
float off_x = 0.f;
|
||||
for (size_t i = 0; i < elm_count; i++) {
|
||||
std::string p;
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
p = std::format("{:.{}f}", data[i], precision);
|
||||
} else {
|
||||
p = std::format("{}", data[i]);
|
||||
}
|
||||
vec2 td = io->Font->GetTextBounds(p, io->FontScale);
|
||||
list->AddRectangle(FinalPos() + fvec2(off_x, 0), td + io->FramePadding,
|
||||
io->Theme->Get(UI7Color_Button));
|
||||
list->Layer++;
|
||||
list->AddText(FinalPos() + fvec2(off_x, 0), p,
|
||||
io->Theme->Get(UI7Color_Text), LITextFlags_AlignMid,
|
||||
td + io->FramePadding);
|
||||
list->Layer--;
|
||||
off_x += td.x + io->ItemSpace.x + io->FramePadding.x;
|
||||
}
|
||||
list->AddText(FinalPos() + fvec2(off_x, io->FramePadding.y * 0.5), label,
|
||||
io->Theme->Get(UI7Color_Text));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PD_UI7_API void DragData<T>::Update() {
|
||||
// Assert(io.get(), "Did you run Container::Init correctly?");
|
||||
// Probably need to find a faster solution (caching sizes calculated here)
|
||||
float off_x = 0;
|
||||
for (size_t i = 0; i < elm_count; i++) {
|
||||
std::string p;
|
||||
if constexpr (std::is_floating_point_v<T>) {
|
||||
p = std::format("{:.{}f}", data[i], precision);
|
||||
} else {
|
||||
p = std::format("{}", data[i]);
|
||||
}
|
||||
vec2 tdim = io->Font->GetTextBounds(p, io->FontScale);
|
||||
off_x += tdim.x + io->ItemSpace.x + io->FramePadding.x;
|
||||
}
|
||||
this->SetSize(vec2(tdim.x + off_x, tdim.y + io->FramePadding.y));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,33 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/dynobj.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void DynObj::Draw() { pRenFun(io, list, this); }
|
||||
PD_UI7_API void DynObj::HandleInput() {}
|
||||
PD_UI7_API void DynObj::Update() {}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,37 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/image.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Image::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// Assert(img.get(), "Image is nullptr!");
|
||||
// io->Ren->OnScreen(screen);
|
||||
list->Layer++;
|
||||
list->AddImage(FinalPos(), img, newsize, this->cuv);
|
||||
list->Layer--;
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/container/label.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Label::Draw() {
|
||||
// Assert(io.get() && list.get(), "Did you run Container::Init correctly?");
|
||||
// io->Ren->OnScreen(screen);
|
||||
list->AddText(FinalPos(), label, io->Theme->Get(UI7Color_Text));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,288 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/drawlist.hpp>
|
||||
#include <pd/ui7/io.hpp>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void DrawList::PathArcToN(const fvec2& c, float radius, float a_min,
|
||||
float a_max, int segments) {
|
||||
// Path.push_back(c);
|
||||
PathReserve(segments + 1);
|
||||
for (int i = 0; i < segments; i++) {
|
||||
float a = a_min + ((float)i / (float)segments) * (a_max - a_min);
|
||||
PathNext(vec2(c.x + std::cos(a) * radius, c.y + std::sin(a) * radius));
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::PathRect(fvec2 a, fvec2 b, float rounding,
|
||||
UI7DrawFlags flags) {
|
||||
if (rounding == 0.f) {
|
||||
PathNext(a);
|
||||
PathNext(vec2(b.x, a.y));
|
||||
PathNext(b);
|
||||
PathNext(vec2(a.x, b.y));
|
||||
} else {
|
||||
PathArcToN(vec2(a.x + rounding, a.y + rounding), rounding, 4 * 6, 4 * 9,
|
||||
21);
|
||||
PathArcToN(vec2(b.x - rounding, a.y + rounding), rounding, 4 * 9, 4 * 12,
|
||||
21);
|
||||
PathArcToN(vec2(b.x - rounding, b.y - rounding), rounding, 4 * 0, 4 * 3,
|
||||
21);
|
||||
PathArcToN(vec2(a.x + rounding, b.y - rounding), rounding, 4 * 3, 4 * 6,
|
||||
21);
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddRect(const fvec2& pos, const fvec2& size,
|
||||
const UI7Color& clr, int thickness) {
|
||||
PathRect(pos, pos + size);
|
||||
PathStroke(clr, thickness, UI7DrawFlags_Close);
|
||||
}
|
||||
PD_UI7_API void DrawList::AddRectangle(fvec2 pos, fvec2 szs,
|
||||
const UI7Color& clr) {
|
||||
PathRect(pos, pos + szs);
|
||||
PathFill(clr);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddTriangle(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, const UI7Color& clr,
|
||||
int thickness) {
|
||||
PathNext(a);
|
||||
PathNext(b);
|
||||
PathNext(c);
|
||||
PathStroke(clr, thickness, UI7DrawFlags_Close);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddTriangleFilled(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c,
|
||||
const UI7Color& clr) {
|
||||
PathNext(a);
|
||||
PathNext(b);
|
||||
PathNext(c);
|
||||
PathFill(clr);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddCircle(const fvec2& pos, float rad, UI7Color col,
|
||||
int num_segments, int thickness) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(pos, rad, 0.f, am, num_segments);
|
||||
}
|
||||
PathStroke(col, thickness, UI7DrawFlags_Close);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddCircleFilled(const fvec2& pos, float rad,
|
||||
UI7Color col, int num_segments) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(pos, rad, 0.f, am, num_segments);
|
||||
}
|
||||
PathFill(col);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddText(fvec2 pos, const std::string& text,
|
||||
const UI7Color& clr, u32 flags, fvec2 box) {
|
||||
Vec<LI::Command::Ref> cmds;
|
||||
pIO->Font->CmdTextEx(cmds, pos, clr, pIO->FontScale, text, flags, box);
|
||||
for (size_t i = 0; i < cmds.Size(); i++) {
|
||||
ClipCmd(cmds[i]);
|
||||
cmds[i]->Layer = Layer;
|
||||
cmds[i]->Index = Commands.Size();
|
||||
Commands.Add(cmds[i]);
|
||||
}
|
||||
// if (!IO->Ren->Font()) {
|
||||
// return;
|
||||
// }
|
||||
// u32 id = Strings::FastHash(text);
|
||||
// LI::StaticText::Ref e;
|
||||
// auto f = static_text.find(id);
|
||||
// if (static_text.find(id) == static_text.end()) {
|
||||
// e = LI::StaticText::New();
|
||||
// static_text[id] = e;
|
||||
// } else {
|
||||
// e = f->second;
|
||||
// }
|
||||
// if (!e->IsSetup() || e->Font() != IO->Ren->Font()) {
|
||||
// int l = IO->Ren->Layer();
|
||||
// IO->Ren->Layer(layer);
|
||||
// e->Setup(ren.get(), pos, clr, text, flags, box);
|
||||
// e->Font(IO->Ren->Font());
|
||||
// IO->Ren->Layer(l);
|
||||
// }
|
||||
// e->SetPos(pos);
|
||||
// e->SetColor(clr);
|
||||
// e->SetLayer(layer);
|
||||
// if (!clip_rects.empty()) {
|
||||
// e->SetScissorMode(LI::ScissorMode_Normal);
|
||||
// e->ScissorRect(clip_rects.top());
|
||||
// }
|
||||
// for (auto it : e->GetRawObject()->List()) {
|
||||
// this->commands.push_back(std::make_pair(
|
||||
// IO->Ren->CurrentScreen()->ScreenType() == Screen::Bottom, it));
|
||||
// }
|
||||
// e->GetRawObject()->ReCopy();
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddImage(fvec2 pos, LI::Texture::Ref img, fvec2 size,
|
||||
LI::Rect uv) {
|
||||
size = size == 0.f ? fvec2(img->GetSize().x, img->GetSize().y) : size;
|
||||
uv = (uv.Top() == 0.0f && uv.Bot() == 0.0f) ? img->GetUV() : uv;
|
||||
LI::Command::Ref cmd = LI::Command::New();
|
||||
cmd->Layer = Layer;
|
||||
cmd->Index = Commands.Size();
|
||||
cmd->Tex = img;
|
||||
auto r = LI::Renderer::PrimRect(pos, size);
|
||||
LI::Renderer::CmdQuad(cmd, r, uv, 0xffffffff);
|
||||
// auto rect = IO->Ren->CreateRect(pos, size, 0.f);
|
||||
// auto cmd = LI::Command::New();
|
||||
// IO->Ren->UseTex(img);
|
||||
// IO->Ren->SetupCommand(cmd);
|
||||
// cmd->Layer(layer);
|
||||
// if (!clip_rects.empty()) {
|
||||
// cmd->SetScissorMode(LI::ScissorMode_Normal);
|
||||
// cmd->ScissorRect(clip_rects.top());
|
||||
// }
|
||||
// IO->Ren->QuadCommand(cmd, rect, uv, 0xffffffff);
|
||||
// commands.push_back(std::make_pair(
|
||||
// IO->Ren->CurrentScreen()->ScreenType() == Screen::Bottom, cmd));
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::AddLine(const fvec2& a, const fvec2& b,
|
||||
const UI7Color& clr, int t) {
|
||||
PathNext(a);
|
||||
PathNext(b);
|
||||
PathStroke(clr, t);
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS
|
||||
PD_UI7_API void DrawList::AddPolyLine(const Vec<fvec2>& points,
|
||||
const UI7Color& clr, UI7DrawFlags flags,
|
||||
int thickness) {
|
||||
if (points.Size() < 2) {
|
||||
return;
|
||||
}
|
||||
auto cmd = LI::Command::New();
|
||||
cmd->Index = Commands.Size();
|
||||
cmd->Layer = Layer;
|
||||
cmd->Tex = pIO->Ren->WhitePixel;
|
||||
ClipCmd(cmd);
|
||||
bool close = (flags & UI7DrawFlags_Close);
|
||||
int num_points = close ? (int)points.Size() : (int)points.Size() - 1;
|
||||
if (flags & UI7DrawFlags_AALines) {
|
||||
// TODO: Find a way to draw less garbage looking lines
|
||||
} else {
|
||||
// Non antialiased lines look awful when rendering with thickness != 1
|
||||
for (int i = 0; i < num_points; i++) {
|
||||
int j = (i + 1) == (int)points.Size() ? 0 : (i + 1);
|
||||
auto line = LI::Renderer::PrimLine(points[i], points[j], thickness);
|
||||
LI::Renderer::CmdQuad(cmd, line, vec4(0.f, 1.f, 1.f, 0.f), clr);
|
||||
}
|
||||
}
|
||||
Commands.Add(cmd);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::ClipCmd(LI::Command::Ref cmd) {
|
||||
if (!pClipRects.IsEmpty()) {
|
||||
cmd->ScissorEnabled = true;
|
||||
fvec4 sr = pClipRects.Top();
|
||||
cmd->ScissorRect = ivec4(sr.x, sr.y, sr.z, sr.w);
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS (Probably make it with a define as it
|
||||
// would probably be faster to render out of screen than checking if
|
||||
// it could be skipped)
|
||||
PD_UI7_API void DrawList::AddConvexPolyFilled(const Vec<fvec2>& points,
|
||||
const UI7Color& clr) {
|
||||
if (points.Size() < 3) {
|
||||
return; // Need at least three points
|
||||
}
|
||||
auto cmd = LI::Command::New();
|
||||
cmd->Index = Commands.Size();
|
||||
cmd->Layer = Layer;
|
||||
auto tex = CurrentTex;
|
||||
if (!tex) {
|
||||
tex = pIO->Ren->WhitePixel;
|
||||
}
|
||||
cmd->Tex = tex;
|
||||
ClipCmd(cmd);
|
||||
for (int i = 2; i < (int)points.Size(); i++) {
|
||||
cmd->AppendIndex(0).AppendIndex(i).AppendIndex(i - 1);
|
||||
}
|
||||
for (int i = 0; i < (int)points.Size(); i++) {
|
||||
cmd->AppendVertex(LI::Vertex(points[i], fvec2(0, 0), clr));
|
||||
}
|
||||
Commands.Add(cmd);
|
||||
}
|
||||
|
||||
PD_UI7_API void DrawList::Clear() { Commands.Clear(); }
|
||||
|
||||
/** Process [Render] the Drawlist */
|
||||
PD_UI7_API void DrawList::Process(LI::DrawList::Ref d) {
|
||||
std::sort(Commands.Begin(), Commands.End(),
|
||||
[](LI::Command::Ref a, LI::Command::Ref b) {
|
||||
/** Advanced (for saving Drawcalls)
|
||||
* - Probably could handle this by creating diffrent layers
|
||||
* for texts and solid objectives
|
||||
* if(a->Tex == b->Tex) { return a->Layer < b->Layer; }
|
||||
* return a->Tex < b->Tex;
|
||||
*/
|
||||
/** Simple */
|
||||
return a->Layer < b->Layer;
|
||||
});
|
||||
NumVertices = 0;
|
||||
NumIndices = 0;
|
||||
for (auto command = Commands.Begin(); command != Commands.End(); command++) {
|
||||
// IO->Ren->OnScreen(IO->Ren->GetScreen(command.first));
|
||||
(*command)->Layer = (*command)->Layer + Base;
|
||||
d->AddCommand(*command);
|
||||
NumVertices += (*command)->VertexBuffer.Size();
|
||||
NumIndices += (*command)->IndexBuffer.Size();
|
||||
}
|
||||
Commands.Clear();
|
||||
Layer = 0;
|
||||
std::vector<u32> rem;
|
||||
// for (auto it : static_text) {
|
||||
// if (!it.second->Used()) {
|
||||
// rem.push_back(it.first);
|
||||
// }
|
||||
// it.second->SetUnused();
|
||||
// }
|
||||
// for (auto& it : rem) {
|
||||
// static_text.erase(it);
|
||||
// }
|
||||
pClipRects.Clear();
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/io.hpp>
|
||||
|
||||
namespace PD {
|
||||
PD_UI7_API void UI7::IO::Update() {
|
||||
u64 current = Sys::GetNanoTime();
|
||||
Delta = static_cast<float>(current - LastTime) / 1000000.f;
|
||||
LastTime = current;
|
||||
DeltaStats->Add(Delta * 1000);
|
||||
Time->Update();
|
||||
InputHandler->Update();
|
||||
Framerate = 1000.f / Delta;
|
||||
DrawListRegestry.Clear();
|
||||
DrawListRegestry.PushFront(Pair<UI7::ID, DrawList::Ref>("CtxBackList", Back));
|
||||
// RegisterDrawList("CtxBackList", Back);
|
||||
}
|
||||
} // namespace PD
|
||||
@@ -1,140 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/layout.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Layout::CursorInit() { Cursor = fvec2(WorkRect.x, WorkRect.y); }
|
||||
|
||||
PD_UI7_API void Layout::SameLine() {
|
||||
BackupCursor = LastObjSize;
|
||||
Cursor = SamelineCursor;
|
||||
}
|
||||
|
||||
PD_UI7_API void Layout::CursorMove(const fvec2& size) {
|
||||
LastObjSize = size;
|
||||
SamelineCursor = Cursor + fvec2(size.x + IO->ItemSpace.x, 0);
|
||||
if (BeforeSameLine.y) {
|
||||
Cursor =
|
||||
fvec2(IO->MenuPadding.x, Cursor.y + BeforeSameLine.y + IO->ItemSpace.y);
|
||||
BeforeSameLine = 0.f;
|
||||
} else {
|
||||
Cursor = fvec2(IO->MenuPadding.x + InitialCursorOffset.x,
|
||||
Cursor.y + size.y + IO->ItemSpace.y);
|
||||
}
|
||||
// Logical Issue here as x should use a max check
|
||||
MaxPosition = fvec2(std::max(MaxPosition.x, SamelineCursor.x), Cursor.y);
|
||||
}
|
||||
|
||||
PD_UI7_API bool Layout::ObjectWorkPos(fvec2& movpos) {
|
||||
if (Scrolling[1]) {
|
||||
movpos.y -= ScrollOffset.y;
|
||||
if (!IO->Ren->InBox(movpos, LastObjSize,
|
||||
fvec4(WorkRect.x, WorkRect.y, WorkRect.x + WorkRect.z,
|
||||
WorkRect.y + WorkRect.w))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
PD_UI7_API void Layout::AddObject(Container::Ref obj) {
|
||||
obj->Init(IO, DrawList);
|
||||
obj->SetPos(AlignPosition(Cursor, obj->GetSize(), WorkRect, GetAlignment()));
|
||||
obj->Update();
|
||||
CursorMove(obj->GetSize());
|
||||
obj->HandleScrolling(ScrollOffset, WorkRect);
|
||||
Objects.PushBack(obj);
|
||||
}
|
||||
|
||||
PD_UI7_API void Layout::AddObjectEx(Container::Ref obj, u32 flags) {
|
||||
obj->Init(IO, DrawList);
|
||||
if (!(flags & 1)) {
|
||||
obj->SetPos(
|
||||
AlignPosition(Cursor, obj->GetSize(), WorkRect, GetAlignment()));
|
||||
}
|
||||
obj->Update();
|
||||
if (!(flags & 1)) {
|
||||
CursorMove(obj->GetSize());
|
||||
}
|
||||
if (!(flags & 2)) {
|
||||
obj->HandleScrolling(ScrollOffset, WorkRect);
|
||||
}
|
||||
if (!(flags & 4)) {
|
||||
Objects.PushFront(obj);
|
||||
} else {
|
||||
Objects.PushBack(obj);
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API Container::Ref Layout::FindObject(u32 id) {
|
||||
for (auto& it : IDObjects) {
|
||||
if (it->GetID() == id) {
|
||||
return it;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PD_UI7_API fvec2 Layout::AlignPosition(fvec2 pos, fvec2 size, fvec4 area,
|
||||
UI7Align alignment) {
|
||||
vec2 p = pos;
|
||||
if (alignment & UI7Align_Center) {
|
||||
p.x = (area.x + area.z) * 0.5 - (pos.x - area.x + size.x * 0.5);
|
||||
} else if (alignment & UI7Align_Right) {
|
||||
}
|
||||
if (alignment & UI7Align_Mid) {
|
||||
p.y = (area.y + area.w) * 0.5 - (pos.y - area.y + size.y * 0.5);
|
||||
} else if (alignment & UI7Align_Bottom) {
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
PD_UI7_API void Layout::Update() {
|
||||
for (auto& it : Objects) {
|
||||
if (it->GetID() != 0 && !FindObject(it->GetID())) {
|
||||
IDObjects.push_back(it);
|
||||
}
|
||||
if (!it->Skippable()) {
|
||||
it->SetPos(it->GetPos() + Pos);
|
||||
it->HandleInput();
|
||||
it->UnlockInput();
|
||||
it->Draw();
|
||||
}
|
||||
}
|
||||
std::vector<size_t> tbr;
|
||||
for (size_t i = 0; i < IDObjects.size(); i++) {
|
||||
if (IDObjects[i]->Removable()) {
|
||||
tbr.push_back(i);
|
||||
}
|
||||
}
|
||||
for (auto& it : tbr) {
|
||||
IDObjects.erase(IDObjects.begin() + it);
|
||||
}
|
||||
Objects.Clear();
|
||||
WorkRect = fvec4(fvec2(WorkRect.x, WorkRect.y), Size - IO->MenuPadding);
|
||||
CursorInit();
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,661 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/menu.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void UI7::Menu::Label(const std::string& label) {
|
||||
// Layout API
|
||||
auto r = PD::New<UI7::Label>(label, io);
|
||||
Layout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API bool UI7::Menu::Button(const std::string& label) {
|
||||
bool ret = false;
|
||||
u32 id = Strings::FastHash("btn" + label + std::to_string(count_btn++));
|
||||
Container::Ref r = Layout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::Button>(label, io);
|
||||
r->SetID(id);
|
||||
}
|
||||
Layout->AddObject(r);
|
||||
if (!r->Skippable()) {
|
||||
ret = std::static_pointer_cast<UI7::Button>(r)->IsPressed();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::ColorEdit(const std::string& label, u32* color) {
|
||||
u32 id = Strings::FastHash("cle" + label + std::to_string(count_btn++));
|
||||
Container::Ref r = Layout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::ColorEdit>(label, color, io);
|
||||
r->SetID(id);
|
||||
}
|
||||
Layout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::DragFloat(const std::string& label, float* data,
|
||||
size_t num_elms) {
|
||||
u32 id = Strings::FastHash("dfl" + label + std::to_string(count_btn++));
|
||||
Container::Ref r = Layout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::DragData<float>>(label, data, num_elms, io);
|
||||
r->SetID(id);
|
||||
}
|
||||
Layout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::Checkbox(const std::string& label, bool& v) {
|
||||
u32 id = Strings::FastHash("cbx" + label + std::to_string(count_cbx++));
|
||||
Container::Ref r = Layout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::Checkbox>(label, v, io);
|
||||
r->SetID(id);
|
||||
}
|
||||
Layout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::Image(LI::Texture::Ref img, fvec2 size,
|
||||
LI::Rect uv) {
|
||||
Container::Ref r = PD::New<UI7::Image>(img, size, uv);
|
||||
Layout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::DebugLabels(Menu::Ref m, Menu::Ref t) {
|
||||
/*if (!m) {
|
||||
return;
|
||||
}
|
||||
if (t == nullptr) {
|
||||
t = m;
|
||||
}
|
||||
std::stringstream s;
|
||||
s << "Name: " << m->name << " [";
|
||||
s << std::hex << std::setw(8) << std::setfill('0') << m->id;
|
||||
s << std::dec << "]";
|
||||
t->Label(s.str());
|
||||
t->Label(std::format("Max Size: {:.2f}, {:.2f}", m->Layout->MaxPosition.x(),
|
||||
m->Layout->MaxPosition.y()));
|
||||
t->Label(std::format("Pos: {:.2f}, {:.2f} Size: {:.2f}, {:.2f}",
|
||||
m->Layout->Pos.x(), m->Layout->Pos.y(),
|
||||
m->Layout->Size.x(), m->Layout->Size.y()));
|
||||
t->Label(std::format("Flags: {:#08x}", m->flags));
|
||||
t->Label(
|
||||
"Pre: " +
|
||||
Strings::FormatNanos(
|
||||
Sys::GetTraceRef("MPRE_" + m->name)->GetProtocol()->GetAverage()));
|
||||
t->Label(
|
||||
"Post: " +
|
||||
Strings::FormatNanos(
|
||||
Sys::GetTraceRef("MPOS_" + m->name)->GetProtocol()->GetAverage()));
|
||||
t->Label(
|
||||
"Update: " +
|
||||
Strings::FormatNanos(
|
||||
Sys::GetTraceRef("MUPT_" + m->name)->GetProtocol()->GetAverage()));
|
||||
t->Label(
|
||||
"MUser: " +
|
||||
Strings::FormatNanos(
|
||||
Sys::GetTraceRef("MUSR_" + m->name)->GetProtocol()->GetAverage()));*/
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::Update(float delta) {
|
||||
TT::Scope st("MUPT_" + name);
|
||||
MenuFocusHandler();
|
||||
if (!(flags & UI7MenuFlags_NoTitlebar)) {
|
||||
CollapseHandler();
|
||||
CloseButtonHandler();
|
||||
MoveHandler();
|
||||
}
|
||||
scroll_anim.Update(delta);
|
||||
if (!scroll_anim.IsFinished()) {
|
||||
Layout->ScrollOffset = scroll_anim;
|
||||
}
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
Layout->DrawList->PushClipRect(
|
||||
fvec4(Layout->Pos.x + io->MenuPadding.x, Layout->Pos.y + tbh,
|
||||
Layout->Size.x - io->MenuPadding.x, Layout->Size.y - tbh));
|
||||
}
|
||||
Layout->DrawList->Layer = 10;
|
||||
Layout->Update();
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
Layout->DrawList->PopClipRect();
|
||||
}
|
||||
PostScrollHandler();
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::PreHandler(UI7MenuFlags flags) {
|
||||
TT::Scope st("MPRE_" + name);
|
||||
// No touch means no Input System
|
||||
if (!has_touch) {
|
||||
header = UI7Color_Header;
|
||||
}
|
||||
if (io->InputHandler->FocusedMenu == id) {
|
||||
header = UI7Color_Header;
|
||||
}
|
||||
DrawList::Ref list = Layout->GetDrawList();
|
||||
// Resetup [updating] variables
|
||||
count_btn = 0;
|
||||
count_cbx = 0;
|
||||
tbh = 0.f;
|
||||
this->flags = flags;
|
||||
Layout->Scrolling[1] = flags & UI7MenuFlags_VtScrolling;
|
||||
has_touch = true; // io->Ren->CurrentScreen()->ScreenType() ==
|
||||
// Screen::Bottom;
|
||||
if (!(flags & UI7MenuFlags_NoTitlebar)) {
|
||||
// Title bar setup and Rendering
|
||||
tbh = io->FontScale * io->Font->DefaultPixelHeight;
|
||||
list->Layer = 20;
|
||||
list->AddRectangle(Layout->Pos, fvec2(Layout->Size.x, tbh),
|
||||
io->Theme->Get(header));
|
||||
fvec2 tpos(
|
||||
io->MenuPadding.x,
|
||||
tbh * 0.5 - io->Font->GetTextBounds(name, io->FontScale).y * 0.5);
|
||||
if (!(flags & UI7MenuFlags_NoCollapse)) {
|
||||
tpos.x += 18;
|
||||
}
|
||||
// LITextFlags tflags = LITextFlags_None;
|
||||
if (flags & UI7MenuFlags_CenterTitle) {
|
||||
tpos = 0;
|
||||
// tflags = LITextFlags_AlignMid;
|
||||
}
|
||||
list->Layer++;
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
int extra = is_shown != nullptr && !(flags & UI7MenuFlags_NoClose)
|
||||
? (20 + io->ItemSpace.x)
|
||||
: 0;
|
||||
Layout->DrawList->PushClipRect(
|
||||
fvec4(Layout->Pos, fvec2(Layout->Size.x - extra, tbh)));
|
||||
}
|
||||
list->AddText(Layout->Pos + tpos, this->name, io->Theme->Get(UI7Color_Text),
|
||||
0, fvec2(Layout->Size.x, tbh));
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
Layout->DrawList->PopClipRect();
|
||||
}
|
||||
|
||||
/// Close Button Rendering
|
||||
if (!(flags & UI7MenuFlags_NoClose) && is_shown) {
|
||||
fvec2 size = tbh - io->FramePadding.y * 2; // Fixed quad size
|
||||
// Need to clamp this way as the math lib lacks a less and greater
|
||||
// operator in vec2 (don't checked if it would make sense yet)
|
||||
size.x = std::clamp(size.x, 5.f, std::numeric_limits<float>::max());
|
||||
size.y = std::clamp(size.y, 5.f, std::numeric_limits<float>::max());
|
||||
// Probably should fix the minsize to be locked on y
|
||||
fvec2 cpos =
|
||||
fvec2(Layout->Pos.x + Layout->Size.x - size.x - io->FramePadding.x,
|
||||
Layout->Pos.y + io->FramePadding.y);
|
||||
Layout->DrawList->AddLine(cpos, cpos + size,
|
||||
io->Theme->Get(UI7Color_FrameBackground), 2);
|
||||
Layout->DrawList->AddLine(cpos + fvec2(0, size.y),
|
||||
cpos + fvec2(size.x, 0),
|
||||
io->Theme->Get(UI7Color_FrameBackground), 2);
|
||||
/*fvec2 cpos =
|
||||
fvec2(Layout->Pos.x + Layout->Size.x - 12 - io->FramePadding.x,
|
||||
Layout->Pos.y + io->FramePadding.y);
|
||||
Layout->GetDrawList()->AddLine(cpos, cpos + 12,
|
||||
io->Theme->Get(clr_close_btn), 2);
|
||||
Layout->GetDrawList()->AddLine(cpos + fvec2(0, 12), cpos + fvec2(12, 0),
|
||||
io->Theme->Get(clr_close_btn), 2);*/
|
||||
}
|
||||
/// Collapse Triangle Rendering
|
||||
if (!(flags & UI7MenuFlags_NoCollapse)) {
|
||||
Layout->DrawList->Layer = 21;
|
||||
/** Fixed Size */
|
||||
fvec2 size = tbh - io->FramePadding.y * 2;
|
||||
fvec2 cpos = Layout->Pos + io->FramePadding;
|
||||
/** Symbol (Position Swapping set by pIsOpen ? openpos : closepos;) */
|
||||
Layout->DrawList->AddTriangleFilled(
|
||||
cpos, cpos + fvec2(size.x, is_open ? 0 : size.y * 0.5),
|
||||
cpos + fvec2(is_open ? size.x * 0.5 : 0, size.y),
|
||||
io->Theme->Get(UI7Color_FrameBackground));
|
||||
Layout->DrawList->Layer = 20;
|
||||
/*fvec2 cpos = Layout->Pos + io->FramePadding;
|
||||
fvec2 positions[2] = {
|
||||
fvec2(12, 6),
|
||||
fvec2(0, 12),
|
||||
};
|
||||
if (is_open) {
|
||||
float t = positions[0].y;
|
||||
positions[0].y = positions[1].x;
|
||||
positions[1].x = t;
|
||||
}
|
||||
Layout->GetDrawList()->AddTriangleFilled(
|
||||
cpos, cpos + positions[0], cpos + positions[1],
|
||||
io->Theme->Get(clr_collapse_tri));*/
|
||||
}
|
||||
Layout->WorkRect.y = io->MenuPadding.y + tbh;
|
||||
Layout->CursorInit();
|
||||
}
|
||||
if (!(flags & UI7MenuFlags_NoBackground) && is_open) {
|
||||
list->Layer = 0;
|
||||
list->AddRectangle(Layout->Pos + fvec2(0, tbh),
|
||||
Layout->Size - fvec2(0, tbh),
|
||||
io->Theme->Get(UI7Color_Background));
|
||||
}
|
||||
if (io->ShowMenuBorder) {
|
||||
vec2 bsize = Layout->Size;
|
||||
if (!is_open) {
|
||||
bsize.y = tbh;
|
||||
}
|
||||
list->Layer = 20;
|
||||
list->AddRect(Layout->Pos, bsize, io->Theme->Get(UI7Color_Border));
|
||||
}
|
||||
// Add a clip Rect for Separators
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
Layout->DrawList->PushClipRect(
|
||||
fvec4(Layout->Pos.x + io->MenuPadding.x, Layout->Pos.y + tbh,
|
||||
Layout->Size.x - io->MenuPadding.x, Layout->Size.y - tbh));
|
||||
}
|
||||
list->Layer = 10;
|
||||
TT::Beg("MUSR_" + name);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::PostHandler() {
|
||||
TT::Scope st("MPOS_" + name);
|
||||
TT::End("MUSR_" + name);
|
||||
// Remove the Clip Rect
|
||||
if (!(flags & UI7MenuFlags_NoClipRect)) {
|
||||
Layout->DrawList->PopClipRect();
|
||||
}
|
||||
ResizeHandler();
|
||||
if (Layout->Scrolling[1]) {
|
||||
scroll_allowed[1] =
|
||||
(Layout->MaxPosition.y > Layout->Size.y - io->MenuPadding.y);
|
||||
if (Layout->MaxPosition.y < Layout->Size.y - io->MenuPadding.y) {
|
||||
Layout->ScrollOffset.y = 0.f;
|
||||
}
|
||||
scrollbar[1] = scroll_allowed[1];
|
||||
|
||||
if (scrollbar[1]) {
|
||||
/// Setup Some Variables hare [they are self described]
|
||||
int screen_w = Layout->Size.x;
|
||||
int tsp = io->MenuPadding.y + tbh;
|
||||
int slider_w = 4;
|
||||
int szs = Layout->Size.y - tsp - io->MenuPadding.y;
|
||||
/// Actually dont have a Horizontal bar yet
|
||||
if (scrollbar[0]) szs -= slider_w - 2;
|
||||
int lslider_h =
|
||||
io->MinSliderDragSize.y; // Dont go less heigt for the drag
|
||||
float slider_h = (szs - 4) * (float(szs - 4) / Layout->MaxPosition.y);
|
||||
/// Visual Slider Height (How it looks in the end)
|
||||
int vslider_h = std::clamp(slider_h, float(lslider_h), float(szs - 4));
|
||||
|
||||
/// Check if we overscroll to the bottom and Auto scroll back...
|
||||
/// Probably schould use Tween ENgine here
|
||||
if (Layout->ScrollOffset.y > Layout->MaxPosition.y - Layout->Size.y &&
|
||||
Layout->MaxPosition.y != 0.f &&
|
||||
Layout->MaxPosition.y >= Layout->Size.y - io->MenuPadding.y) {
|
||||
Layout->ScrollOffset.y -= io->OverScrollMod * io->Delta;
|
||||
if (Layout->ScrollOffset.y < Layout->MaxPosition.y - Layout->Size.y) {
|
||||
Layout->ScrollOffset.y = Layout->MaxPosition.y - Layout->Size.y;
|
||||
}
|
||||
}
|
||||
|
||||
/// Do the Same as above just for Overscroll back to the top
|
||||
if (Layout->ScrollOffset.y < 0) {
|
||||
Layout->ScrollOffset.y += io->OverScrollMod * io->Delta;
|
||||
if (Layout->ScrollOffset.y > 0) {
|
||||
Layout->ScrollOffset.y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// Effect
|
||||
/*if (scroll_mod[1] != 0) {
|
||||
Layout->ScrollOffset[1] += scroll_mod[1];
|
||||
}
|
||||
if (scroll_mod[1] < 0.f) {
|
||||
scroll_mod[1] += 0.4f;
|
||||
if (scroll_mod[1] > 0.f) {
|
||||
scroll_mod[1] = 0;
|
||||
}
|
||||
}
|
||||
if (scroll_mod[1] > 0.f) {
|
||||
scroll_mod[1] -= 0.4f;
|
||||
if (scroll_mod[1] < 0.f) {
|
||||
scroll_mod[1] = 0;
|
||||
}
|
||||
}*/
|
||||
UI7Color sldr_drag = UI7Color_Button;
|
||||
/// Slider Dragging????
|
||||
/// Probably need a new API for this
|
||||
if (has_touch &&
|
||||
io->InputHandler->DragObject(name + "sldr",
|
||||
fvec4(Layout->Pos.x + screen_w - 12,
|
||||
Layout->Pos.y + tsp, 8, szs)) &&
|
||||
!io->InputHandler->DragReleasedAW) {
|
||||
sldr_drag = UI7Color_ButtonHovered;
|
||||
float drag_center = vslider_h / 2.0f;
|
||||
float drag_pos =
|
||||
std::clamp(static_cast<float>(
|
||||
((io->InputHandler->DragPosition.y - Layout->Pos.y) -
|
||||
tsp - drag_center) /
|
||||
(szs - vslider_h - 4)),
|
||||
0.0f, 1.0f);
|
||||
|
||||
Layout->ScrollOffset.y =
|
||||
drag_pos * (Layout->MaxPosition.y - Layout->Size.y);
|
||||
}
|
||||
int srpos =
|
||||
tsp + std::clamp(float(szs - vslider_h - 4) *
|
||||
(Layout->ScrollOffset.y /
|
||||
(Layout->MaxPosition.y - Layout->Size.y)),
|
||||
0.f, float(szs - vslider_h - 4));
|
||||
|
||||
/// Rendering Stage
|
||||
auto list = Layout->DrawList;
|
||||
list->Layer = 20;
|
||||
list->AddRectangle(Layout->Pos + fvec2(screen_w - 12, tsp),
|
||||
fvec2(slider_w * 2, szs),
|
||||
io->Theme->Get(UI7Color_FrameBackground));
|
||||
list->AddRectangle(Layout->Pos + fvec2(screen_w - 10, tsp + 2),
|
||||
fvec2(slider_w, szs - 4),
|
||||
io->Theme->Get(UI7Color_FrameBackgroundHovered));
|
||||
list->AddRectangle(Layout->Pos + fvec2(screen_w - 10, srpos + 2),
|
||||
fvec2(slider_w, vslider_h), io->Theme->Get(sldr_drag));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::Separator() {
|
||||
// Dynamic Objects are very simple...
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[=, this](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
l->AddRect(self->FinalPos(), self->GetSize(),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
});
|
||||
// Set size before pushing (cause Cursor Move will require it)
|
||||
r->SetSize(fvec2(Layout->Size.x - 10, 1));
|
||||
Layout->AddObject(r);
|
||||
/*return;
|
||||
vec2 pos = Layout->Cursor;
|
||||
vec2 size = fvec2(Layout->Size.x - (scrollbar[1] ? 24 : 10), 1);
|
||||
Layout->CursorMove(size);
|
||||
if (Layout->ObjectWorkPos(pos)) {
|
||||
return;
|
||||
}
|
||||
Layout->GetDrawList()->AddRectangle(Layout->Pos + pos, size,
|
||||
io->Theme->Get(UI7Color_TextDead));*/
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::SeparatorText(const std::string& label) {
|
||||
// Also note to use [=, this] instead of [&] to not undefined access label
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[=, this](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
fvec2 size = self->GetSize();
|
||||
fvec2 tdim = io->Font->GetTextBounds(label, io->FontScale);
|
||||
fvec2 pos = self->FinalPos();
|
||||
auto align = Layout->GetAlignment();
|
||||
vec2 rpos = Layout->AlignPosition(
|
||||
pos, tdim, fvec4(Layout->Pos, Layout->Size), align);
|
||||
if (!(align & UI7Align_Left)) {
|
||||
l->AddRectangle(fvec2(rpos.x + io->FramePadding.x, tdim.y * 0.5),
|
||||
fvec2(pos.x - rpos.x - io->MenuPadding.x, 1),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
if (!(align & UI7Align_Right)) {
|
||||
l->AddRectangle(
|
||||
pos + fvec2(tdim.x + io->FramePadding.x, tdim.y * 0.5),
|
||||
fvec2(size.x - tdim.x - io->MenuPadding.x, 1),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
l->AddText(rpos, label, io->Theme->Get(UI7Color_Text), 0,
|
||||
fvec2(Layout->Size.x, self->GetSize().y));
|
||||
});
|
||||
// Set size before pushing (cause Cursor Move will require it)
|
||||
r->SetSize(fvec2(Layout->Size.x - 10, io->Font->PixelHeight * io->FontScale));
|
||||
Layout->AddObject(r);
|
||||
return;
|
||||
fvec2 size = fvec2(Layout->Size.x - (scrollbar[1] ? 24 : 10), 1);
|
||||
fvec2 tdim = io->Font->GetTextBounds(label, io->FontScale);
|
||||
fvec2 pos = Layout->Cursor;
|
||||
Layout->CursorMove(fvec2(size.x, tdim.y));
|
||||
|
||||
if (Layout->ObjectWorkPos(pos)) {
|
||||
return;
|
||||
}
|
||||
auto alignment = Layout->GetAlignment();
|
||||
vec2 rpos = Layout->AlignPosition(Layout->Pos + pos, tdim,
|
||||
vec4(Layout->Pos, Layout->Size), alignment);
|
||||
|
||||
if (!(alignment & UI7Align_Left)) {
|
||||
Layout->GetDrawList()->AddRectangle(
|
||||
rpos +
|
||||
fvec2(-(rpos.x - Layout->Pos.x - io->MenuPadding.x), tdim.y * 0.5),
|
||||
fvec2(rpos.x - Layout->Pos.x - io->MenuPadding.x - io->FramePadding.x,
|
||||
size.y),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
if (!(alignment & UI7Align_Right)) {
|
||||
Layout->GetDrawList()->AddRectangle(
|
||||
rpos + fvec2(tdim.x + io->FramePadding.x, tdim.y * 0.5),
|
||||
fvec2(size.x - (tdim.x + io->FramePadding.x), size.y),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
Layout->GetDrawList()->AddText(rpos, label, io->Theme->Get(UI7Color_Text), 0,
|
||||
fvec2(Layout->Size.x, 20));
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::Join() {
|
||||
// Assert(Layout->Objects.size(), "Objects list is empty!");
|
||||
join.push_back(Layout->Objects.Back().get());
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::JoinAlign(UI7Align a) {
|
||||
if (a == 0) {
|
||||
a = UI7Align_Default;
|
||||
}
|
||||
this->Join();
|
||||
|
||||
fvec2 spos = join.front()->GetPos();
|
||||
fvec2 szs = join.back()->GetPos() + join.back()->GetSize() - spos;
|
||||
for (auto it : join) {
|
||||
szs.x = std::max(szs.x, it->GetPos().x + it->GetSize().x - spos.x);
|
||||
}
|
||||
fvec2 off;
|
||||
if (a & UI7Align_Center) {
|
||||
off.x = (Layout->Pos.x + Layout->Size.x * 0.5) - (spos.x + szs.x * 0.5);
|
||||
}
|
||||
if (a & UI7Align_Mid) {
|
||||
off.y = (Layout->Pos.y + Layout->Size.y * 0.5) - (spos.y + szs.y * 0.5);
|
||||
}
|
||||
for (auto it : join) {
|
||||
it->SetPos(it->GetPos() + off);
|
||||
}
|
||||
join.clear();
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::AfterAlign(UI7Align a) {
|
||||
Container* ref = Layout->Objects.Back().get();
|
||||
fvec2 p = ref->GetPos();
|
||||
fvec2 s = ref->GetSize();
|
||||
fvec2 np = p;
|
||||
if (a & UI7Align_Center) {
|
||||
np.x = (Layout->Pos.x + Layout->Size.x * 0.5) - (p.x + s.x * 0.5);
|
||||
}
|
||||
if (a & UI7Align_Mid) {
|
||||
np.y = (Layout->Pos.y + Layout->Size.y * 0.5) - (p.y + s.y * 0.5);
|
||||
}
|
||||
ref->SetPos(np);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::CreateParent() {
|
||||
// Assert(!tmp_parent, "There is already an existing Parent container!");
|
||||
tmp_parent = Container::New();
|
||||
tmp_parent->SetPos(0);
|
||||
tmp_parent->SetSize(0);
|
||||
}
|
||||
|
||||
PD_UI7_API bool UI7::Menu::BeginTreeNode(const UI7::ID& id) {
|
||||
auto n = tree_nodes.find((u32)id);
|
||||
if (n == tree_nodes.end()) {
|
||||
tree_nodes[(u32)id] = false;
|
||||
n = tree_nodes.find((u32)id);
|
||||
}
|
||||
fvec2 pos = Layout->Cursor;
|
||||
fvec2 tdim = io->Font->GetTextBounds(id.GetName(), io->FontScale);
|
||||
fvec2 size = fvec2(tdim.x + 10 + io->ItemSpace.x, tdim.y);
|
||||
if (n->second) {
|
||||
Layout->InitialCursorOffset.x += 10.f;
|
||||
}
|
||||
Layout->CursorMove(size);
|
||||
if (Layout->ObjectWorkPos(pos)) {
|
||||
return n->second;
|
||||
}
|
||||
fvec2 ts = Layout->Pos + pos + fvec2(0, 3);
|
||||
fvec2 positions[2] = {
|
||||
fvec2(10, 5),
|
||||
fvec2(0, 10),
|
||||
};
|
||||
if (n->second) {
|
||||
float t = positions[0].y;
|
||||
positions[0].y = positions[1].x;
|
||||
positions[1].x = t;
|
||||
}
|
||||
Layout->GetDrawList()->AddTriangleFilled(
|
||||
ts, ts + positions[0], ts + positions[1],
|
||||
io->Theme->Get(UI7Color_FrameBackground));
|
||||
Layout->GetDrawList()->AddText(
|
||||
Layout->Pos + pos + fvec2(10 + io->ItemSpace.x, 0), id.GetName(),
|
||||
io->Theme->Get(UI7Color_Text));
|
||||
if (has_touch && io->InputHandler->DragObject(
|
||||
name + id.GetName(), vec4(Layout->Pos + pos, size))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
n->second = !n->second;
|
||||
if (!n->second) {
|
||||
Layout->InitialCursorOffset.x -= 10;
|
||||
Layout->Cursor.x -= 10;
|
||||
}
|
||||
}
|
||||
}
|
||||
return n->second;
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::EndTreeNode() {
|
||||
Layout->InitialCursorOffset.x -= 10.f;
|
||||
Layout->Cursor.x -= 10.f;
|
||||
if (Layout->InitialCursorOffset.x < 0.f) {
|
||||
Layout->InitialCursorOffset.x = 0.f;
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::CloseButtonHandler() {
|
||||
// Close Logic
|
||||
if (!(flags & UI7MenuFlags_NoClose) && is_shown != nullptr) {
|
||||
vec2 cpos = fvec2(Layout->Pos.x + Layout->Size.x - 12 - io->FramePadding.x,
|
||||
Layout->Pos.y + io->FramePadding.y);
|
||||
|
||||
clr_close_btn = UI7Color_FrameBackground;
|
||||
if (has_touch && io->InputHandler->DragObject(UI7::ID(name + "clse"),
|
||||
fvec4(cpos, fvec2(12)))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
*is_shown = !(*is_shown);
|
||||
}
|
||||
clr_close_btn = UI7Color_FrameBackgroundHovered;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::ResizeHandler() {
|
||||
if (!(flags & UI7MenuFlags_NoResize)) {
|
||||
if (has_touch &&
|
||||
io->InputHandler->DragObject(
|
||||
name + "rszs", fvec4(Layout->Pos + Layout->Size - 20, 20))) {
|
||||
vec2 szs = Layout->Size + (io->InputHandler->DragPosition -
|
||||
io->InputHandler->DragLastPosition);
|
||||
if (szs.x < 30) szs.x = 30;
|
||||
if (szs.y < 30) szs.y = 30;
|
||||
Layout->Size = szs;
|
||||
}
|
||||
Layout->DrawList->Layer = 21;
|
||||
Layout->DrawList->AddTriangleFilled(
|
||||
Layout->Pos + Layout->Size, Layout->Pos + Layout->Size - fvec2(0, 15),
|
||||
Layout->Pos + Layout->Size - fvec2(15, 0),
|
||||
io->Theme->Get(UI7Color_FrameBackground));
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::MoveHandler() {
|
||||
// Menu Movement
|
||||
if (!(flags & UI7MenuFlags_NoMove)) {
|
||||
if (has_touch &&
|
||||
io->InputHandler->DragObject(
|
||||
name + "tmv", fvec4(Layout->Pos, fvec2(Layout->Size.x, tbh)))) {
|
||||
if (io->InputHandler->DragDoubleRelease) {
|
||||
is_open = !is_open;
|
||||
}
|
||||
Layout->Pos = Layout->Pos + (io->InputHandler->DragPosition -
|
||||
io->InputHandler->DragLastPosition);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::CollapseHandler() {
|
||||
// Collapse logic
|
||||
if (!(flags & UI7MenuFlags_NoCollapse)) {
|
||||
vec2 cpos = Layout->Pos + io->FramePadding;
|
||||
clr_collapse_tri = UI7Color_FrameBackground;
|
||||
if (has_touch &&
|
||||
io->InputHandler->DragObject(UI7::ID(name + "clbse"),
|
||||
fvec4(cpos, fvec2(18, tbh)))) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
is_open = !is_open;
|
||||
}
|
||||
clr_collapse_tri = UI7Color_FrameBackgroundHovered;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::PostScrollHandler() {
|
||||
if (has_touch &&
|
||||
io->InputHandler->DragObject(id, vec4(Layout->Pos, Layout->Size)) &&
|
||||
Layout->Scrolling[1] && flags & UI7MenuFlags_VtScrolling &&
|
||||
Layout->MaxPosition.y - Layout->Size.y + io->MenuPadding.y > 0) {
|
||||
if (io->InputHandler->DragReleased) {
|
||||
// scroll_mod = (io->DragPosition - io->DragLastPosition);
|
||||
} else {
|
||||
Layout->ScrollOffset.y = std::clamp(
|
||||
Layout->ScrollOffset.y - (io->InputHandler->DragPosition.y -
|
||||
io->InputHandler->DragLastPosition.y),
|
||||
-40.f, (Layout->MaxPosition.y - Layout->Size.y) + 40.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Menu::MenuFocusHandler() {
|
||||
// Check if menu can be focused for Selective Menu Input API
|
||||
vec4 newarea = vec4(Layout->Pos, Layout->Size);
|
||||
if (!is_open) {
|
||||
newarea = fvec4(Layout->Pos, fvec2(Layout->Size.x, tbh));
|
||||
}
|
||||
if (has_touch && io->Inp->IsDown(io->Inp->Touch) &&
|
||||
io->Ren->InBox(io->Inp->TouchPos(), newarea) &&
|
||||
!io->Ren->InBox(io->Inp->TouchPos(), io->InputHandler->FocusedMenuRect)) {
|
||||
io->InputHandler->FocusedMenu = id;
|
||||
}
|
||||
if (io->InputHandler->FocusedMenu == id) {
|
||||
io->InputHandler->FocusedMenuRect = newarea;
|
||||
}
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,269 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/ui7/containers.hpp>
|
||||
#include <pd/ui7/remenu.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void UI7::ReMenu::Label(const std::string& label) {
|
||||
// Layout API
|
||||
auto r = PD::New<UI7::Label>(label, pIO);
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API bool UI7::ReMenu::Button(const std::string& label) {
|
||||
bool ret = false;
|
||||
u32 id = Strings::FastHash("btn" + label +
|
||||
std::to_string(pLayout->Objects.Size()));
|
||||
Container::Ref r = pLayout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::Button>(label, pIO);
|
||||
r->SetID(id);
|
||||
}
|
||||
pLayout->AddObject(r);
|
||||
if (!r->Skippable()) {
|
||||
ret = std::static_pointer_cast<UI7::Button>(r)->IsPressed();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*PD_UI7_API void UI7::ReMenu::DragFloat(const std::string& label, float* data,
|
||||
size_t num_elms) {
|
||||
u32 id = Strings::FastHash("dfl" + label + std::to_string(count_btn++));
|
||||
Container::Ref r = Layout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::DragData<float>>(label, data, num_elms, io);
|
||||
r->SetID(id);
|
||||
}
|
||||
Layout->AddObject(r);
|
||||
}*/
|
||||
|
||||
PD_UI7_API void UI7::ReMenu::Checkbox(const std::string& label, bool& v) {
|
||||
u32 id = Strings::FastHash("cbx" + label +
|
||||
std::to_string(pLayout->Objects.Size()));
|
||||
Container::Ref r = pLayout->FindObject(id);
|
||||
if (!r) {
|
||||
r = PD::New<UI7::Checkbox>(label, v, pIO);
|
||||
r->SetID(id);
|
||||
}
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::ReMenu::Image(LI::Texture::Ref img, fvec2 size,
|
||||
LI::Rect uv) {
|
||||
Container::Ref r = PD::New<UI7::Image>(img, size, uv);
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void ReMenu::Separator() {
|
||||
// Dynamic Objects are very simple...
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[=, this](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
l->AddRect(self->FinalPos(), self->GetSize(),
|
||||
pIO->Theme->Get(UI7Color_TextDead));
|
||||
});
|
||||
// Set size before pushing (cause Cursor Move will require it)
|
||||
r->SetSize(fvec2(pLayout->Size.x - 10, 1));
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
|
||||
PD_UI7_API void ReMenu::SeparatorText(const std::string& label) {
|
||||
// Also note to use [=] instead of [&] to not undefined access label
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[=, this](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
fvec2 size = self->GetSize();
|
||||
fvec2 tdim = io->Font->GetTextBounds(label, io->FontScale);
|
||||
fvec2 pos = self->FinalPos();
|
||||
auto align = pLayout->GetAlignment();
|
||||
vec2 rpos = pLayout->AlignPosition(
|
||||
pos, tdim, fvec4(pLayout->Pos, pLayout->Size), align);
|
||||
if (!(align & UI7Align_Left)) {
|
||||
l->AddRectangle(fvec2(rpos.x + io->FramePadding.x, tdim.y * 0.5),
|
||||
fvec2(pos.x - rpos.x - io->MenuPadding.x, 1),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
if (!(align & UI7Align_Right)) {
|
||||
l->AddRectangle(
|
||||
pos + fvec2(tdim.x + io->FramePadding.x, tdim.y * 0.5),
|
||||
fvec2(size.x - tdim.x - io->MenuPadding.x, 1),
|
||||
io->Theme->Get(UI7Color_TextDead));
|
||||
}
|
||||
l->AddText(rpos, label, io->Theme->Get(UI7Color_Text), 0,
|
||||
fvec2(pLayout->Size.x, self->GetSize().y));
|
||||
});
|
||||
// Set size before pushing (cause Cursor Move will require it)
|
||||
r->SetSize(
|
||||
fvec2(pLayout->Size.x - 10, pIO->Font->PixelHeight * pIO->FontScale));
|
||||
pLayout->AddObject(r);
|
||||
}
|
||||
PD_UI7_API void ReMenu::HandleFocus() {
|
||||
// Check if menu can be focused for Selective Menu Input API
|
||||
vec4 newarea = fvec4(pLayout->Pos, pLayout->Size);
|
||||
if (!pIsOpen) {
|
||||
newarea = fvec4(pLayout->Pos, fvec2(pLayout->Size.x, TitleBarHeight));
|
||||
}
|
||||
if (pIO->Inp->IsDown(pIO->Inp->Touch) &&
|
||||
pIO->Ren->InBox(pIO->Inp->TouchPos(), newarea) &&
|
||||
!pIO->Ren->InBox(pIO->Inp->TouchPos(),
|
||||
pIO->InputHandler->FocusedMenuRect)) {
|
||||
pIO->InputHandler->FocusedMenu = pID;
|
||||
}
|
||||
if (pIO->InputHandler->FocusedMenu == pID) {
|
||||
pIO->InputHandler->FocusedMenuRect = newarea;
|
||||
}
|
||||
}
|
||||
PD_UI7_API void ReMenu::HandleScrolling() {}
|
||||
PD_UI7_API void ReMenu::HandleTitlebarActions() {
|
||||
// Collapse
|
||||
if (!(Flags & UI7MenuFlags_NoCollapse)) {
|
||||
vec2 cpos = pLayout->Pos + pIO->FramePadding;
|
||||
// clr_collapse_tri = UI7Color_FrameBackground;
|
||||
if (pIO->InputHandler->DragObject(UI7::ID(pID.GetName() + "clbse"),
|
||||
fvec4(cpos, fvec2(18, TitleBarHeight)))) {
|
||||
if (pIO->InputHandler->DragReleased) {
|
||||
pIsOpen = !pIsOpen;
|
||||
}
|
||||
// clr_collapse_tri = UI7Color_FrameBackgroundHovered;
|
||||
}
|
||||
}
|
||||
// Close Logic
|
||||
if (!(Flags & UI7MenuFlags_NoClose) && pIsShown != nullptr) {
|
||||
vec2 cpos =
|
||||
fvec2(pLayout->Pos.x + pLayout->Size.x - 12 - pIO->FramePadding.x,
|
||||
pLayout->Pos.y + pIO->FramePadding.y);
|
||||
|
||||
// clr_close_btn = UI7Color_FrameBackground;
|
||||
if (pIO->InputHandler->DragObject(UI7::ID(pID.GetName() + "clse"),
|
||||
fvec4(cpos, fvec2(12)))) {
|
||||
if (pIO->InputHandler->DragReleased) {
|
||||
*pIsShown = !(*pIsShown);
|
||||
}
|
||||
// clr_close_btn = UI7Color_FrameBackgroundHovered;
|
||||
}
|
||||
}
|
||||
// Menu Movement
|
||||
if (!(Flags & UI7MenuFlags_NoMove)) {
|
||||
if (pIO->InputHandler->DragObject(
|
||||
pID.GetName() + "tmv",
|
||||
fvec4(pLayout->Pos, fvec2(pLayout->Size.x, TitleBarHeight)))) {
|
||||
if (pIO->InputHandler->DragDoubleRelease) {
|
||||
pIsOpen = !pIsOpen;
|
||||
}
|
||||
pLayout->Pos = pLayout->Pos + (pIO->InputHandler->DragPosition -
|
||||
pIO->InputHandler->DragLastPosition);
|
||||
// Have no ViewPort Yet :(
|
||||
// pLayout->Pos = std::clamp(pLayout->Pos, fvec2(10), fvec2(1270, 710));
|
||||
}
|
||||
}
|
||||
}
|
||||
PD_UI7_API void ReMenu::DrawBaseLayout() {
|
||||
if (pIsOpen) {
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
l->Layer = 0;
|
||||
l->AddRectangle(self->FinalPos(), self->GetSize(),
|
||||
io->Theme->Get(UI7Color_Background));
|
||||
});
|
||||
// Set size before pushing (cause Cursor Move will require it)
|
||||
r->SetSize(
|
||||
fvec2(pLayout->GetSize().x, pLayout->GetSize().y - TitleBarHeight));
|
||||
r->SetPos(fvec2(0, TitleBarHeight));
|
||||
pLayout->AddObjectEx(r, UI7LytAdd_NoCursorUpdate |
|
||||
UI7LytAdd_NoScrollHandle | UI7LytAdd_Front);
|
||||
}
|
||||
if (!(Flags & UI7MenuFlags_NoTitlebar)) {
|
||||
Container::Ref r = PD::New<UI7::DynObj>(
|
||||
[=, this](UI7::IO::Ref io, UI7::DrawList::Ref l, UI7::Container* self) {
|
||||
l->Layer = 20;
|
||||
/** Header Bar */
|
||||
l->AddRectangle(self->FinalPos(), self->GetSize(),
|
||||
io->Theme->Get(UI7Color_Header));
|
||||
l->Layer = 21;
|
||||
/** Inline if statement to shift the Text if collapse sym is shown */
|
||||
/** What the hell is this code btw (didn't found a better way) */
|
||||
l->AddText(self->FinalPos() +
|
||||
fvec2(Flags & UI7MenuFlags_NoClose
|
||||
? 0
|
||||
: (TitleBarHeight - pIO->FramePadding.y * 2 +
|
||||
(io->FramePadding.x * 2)),
|
||||
0),
|
||||
pID.GetName(), io->Theme->Get(UI7Color_Text));
|
||||
});
|
||||
r->SetSize(fvec2(pLayout->GetSize().x, TitleBarHeight));
|
||||
r->SetPos(0);
|
||||
pLayout->AddObjectEx(r,
|
||||
UI7LytAdd_NoCursorUpdate | UI7LytAdd_NoScrollHandle);
|
||||
|
||||
/** Collapse Sym */
|
||||
if (!(Flags & UI7MenuFlags_NoCollapse)) {
|
||||
r = PD::New<UI7::DynObj>([=, this](UI7::IO::Ref io, UI7::DrawList::Ref l,
|
||||
UI7::Container* self) {
|
||||
/** This sym actually requires layer 21 (i dont know why) */
|
||||
l->Layer = 21;
|
||||
/**
|
||||
* Symbol (Position Swapping set by pIsOpen ? openpos : closepos;)
|
||||
*/
|
||||
l->AddTriangleFilled(
|
||||
self->FinalPos(),
|
||||
self->FinalPos() +
|
||||
fvec2(self->GetSize().x, pIsOpen ? 0 : self->GetSize().y * 0.5),
|
||||
self->FinalPos() +
|
||||
fvec2(pIsOpen ? self->GetSize().x * 0.5 : 0, self->GetSize().y),
|
||||
io->Theme->Get(UI7Color_FrameBackground));
|
||||
});
|
||||
r->SetSize(TitleBarHeight - pIO->FramePadding.y * 2);
|
||||
r->SetPos(pIO->FramePadding);
|
||||
pLayout->AddObjectEx(r,
|
||||
UI7LytAdd_NoCursorUpdate | UI7LytAdd_NoScrollHandle);
|
||||
}
|
||||
/** Close Sym (only shown if pIsShown is not nullptr) */
|
||||
if (!(Flags & UI7MenuFlags_NoClose) && pIsShown) {
|
||||
fvec2 size = TitleBarHeight - pIO->FramePadding.y * 2; // Fixed quad size
|
||||
// Need to clamp this way as the math lib lacks a less and greater
|
||||
// operator in vec2 (don't checked if it would make sense yet)
|
||||
size.x = std::clamp(size.x, 5.f, std::numeric_limits<float>::max());
|
||||
size.y = std::clamp(size.y, 5.f, std::numeric_limits<float>::max());
|
||||
// Probably should fix the minsize to be locked on y
|
||||
fvec2 cpos =
|
||||
fvec2(pLayout->Pos.x + pLayout->Size.x - size.x - pIO->FramePadding.x,
|
||||
pLayout->Pos.y + pIO->FramePadding.y);
|
||||
pLayout->DrawList->AddLine(cpos, cpos + size,
|
||||
pIO->Theme->Get(UI7Color_FrameBackground), 2);
|
||||
pLayout->DrawList->AddLine(cpos + fvec2(0, size.y),
|
||||
cpos + fvec2(size.x, 0),
|
||||
pIO->Theme->Get(UI7Color_FrameBackground), 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
PD_UI7_API void ReMenu::Update() {
|
||||
HandleFocus();
|
||||
if (!(Flags & UI7MenuFlags_NoTitlebar)) {
|
||||
HandleTitlebarActions();
|
||||
}
|
||||
DrawBaseLayout();
|
||||
pLayout->Update();
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/theme.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace UI7 {
|
||||
PD_UI7_API void Theme::Default(Theme& theme) {
|
||||
theme.Set(UI7Color_Text, Color("#FFFFFFFF"));
|
||||
theme.Set(UI7Color_TextDead, Color("#AAAAAAFF"));
|
||||
theme.Set(UI7Color_Background, Color("#222222ff"));
|
||||
theme.Set(UI7Color_Border, Color("#999999ff"));
|
||||
theme.Set(UI7Color_Button, Color("#111111FF"));
|
||||
theme.Set(UI7Color_ButtonDead, Color("#080808FF"));
|
||||
theme.Set(UI7Color_ButtonActive, Color("#2A2A2AFF"));
|
||||
theme.Set(UI7Color_ButtonHovered, Color("#222222FF"));
|
||||
theme.Set(UI7Color_Header, Color("#111111FF"));
|
||||
theme.Set(UI7Color_HeaderDead, Color("#080808FF"));
|
||||
theme.Set(UI7Color_Selector, Color("#222222FF"));
|
||||
theme.Set(UI7Color_Checkmark, Color("#2A2A2AFF"));
|
||||
theme.Set(UI7Color_FrameBackground, Color("#555555FF"));
|
||||
theme.Set(UI7Color_FrameBackgroundHovered, Color("#777777FF"));
|
||||
theme.Set(UI7Color_Progressbar, Color("#00FF00FF"));
|
||||
theme.Set(UI7Color_ListEven, Color("#CCCCCCFF"));
|
||||
theme.Set(UI7Color_ListOdd, Color("#BBBBBBFF"));
|
||||
}
|
||||
|
||||
PD_UI7_API void Theme::Flashbang(Theme& theme) {
|
||||
theme.Set(UI7Color_Text, Color("#000000FF"));
|
||||
theme.Set(UI7Color_TextDead, Color("#333333FF"));
|
||||
theme.Set(UI7Color_Background, Color("#eeeeeeFF"));
|
||||
theme.Set(UI7Color_Border, Color("#777777ff"));
|
||||
theme.Set(UI7Color_Button, Color("#ccccccFF"));
|
||||
theme.Set(UI7Color_ButtonDead, Color("#bbbbbbFF"));
|
||||
theme.Set(UI7Color_ButtonActive, Color("#ccccccFF"));
|
||||
theme.Set(UI7Color_ButtonHovered, Color("#acacacFF"));
|
||||
theme.Set(UI7Color_Header, Color("#ddddddFF"));
|
||||
theme.Set(UI7Color_HeaderDead, Color("#cdcdcdFF"));
|
||||
theme.Set(UI7Color_Selector, Color("#222222FF"));
|
||||
theme.Set(UI7Color_Checkmark, Color("#ccccccFF"));
|
||||
theme.Set(UI7Color_FrameBackground, Color("#aaaaaaFF"));
|
||||
theme.Set(UI7Color_FrameBackgroundHovered, Color("#909090FF"));
|
||||
theme.Set(UI7Color_Progressbar, Color("#00FF00FF"));
|
||||
theme.Set(UI7Color_ListEven, Color("#CCCCCCFF"));
|
||||
theme.Set(UI7Color_ListOdd, Color("#BBBBBBFF"));
|
||||
}
|
||||
} // namespace UI7
|
||||
} // namespace PD
|
||||
@@ -1,367 +0,0 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/core/core.hpp>
|
||||
#include <pd/ui7/ui7.hpp>
|
||||
|
||||
// Helpers
|
||||
|
||||
std::string _UI7DV4(PD::vec4<float> v) {
|
||||
return std::format("[{:.2f}, {:.2f}, {:.2f}, {:.2f}]", v.x, v.y, v.z, v.w);
|
||||
}
|
||||
|
||||
std::string _UI7DV2(PD::vec2<float> v) {
|
||||
return std::format("[{:.2f}, {:.2f}]", v.x, v.y);
|
||||
}
|
||||
|
||||
#define UI7DV4(x) #x ": " + _UI7DV4(x)
|
||||
#define UI7DV4N(x) _UI7DV4(x)
|
||||
#define UI7DV2(x) #x ": " + _UI7DV2(x)
|
||||
#define UI7DV2N(x) _UI7DV2(x)
|
||||
#define UI7DHX32(x) std::format("{}: {:#08x}", #x, x)
|
||||
#define UI7DTF(x) PD::Strings::FormatNanos(x)
|
||||
|
||||
namespace PD {
|
||||
PD_UI7_API std::string UI7::GetVersion(bool show_build) {
|
||||
std::stringstream s;
|
||||
s << ((UI7_VERSION >> 24) & 0xFF) << ".";
|
||||
s << ((UI7_VERSION >> 16) & 0xFF) << ".";
|
||||
s << ((UI7_VERSION >> 8) & 0xFF);
|
||||
if (show_build) s << "-" << ((UI7_VERSION) & 0xFF);
|
||||
return s.str();
|
||||
}
|
||||
|
||||
PD_UI7_API bool UI7::Context::BeginMenu(const ID& id, UI7MenuFlags flags,
|
||||
bool* show) {
|
||||
// Assert(!this->current, "You are already in another Menu!");
|
||||
// Assert(std::find(amenus.begin(), amenus.end(), (u32)id) == amenus.end(),
|
||||
// "Menu Name Already used or\nContext::Update not called!");
|
||||
if (show != nullptr) {
|
||||
if (!(*show)) {
|
||||
if (io->InputHandler->FocusedMenu == id) {
|
||||
io->InputHandler->FocusedMenu = 0;
|
||||
io->InputHandler->FocusedMenuRect = 0;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
auto menu = this->menus.find(id);
|
||||
if (menu == this->menus.end()) {
|
||||
this->menus[id] = Menu::New(id, io);
|
||||
// this->menus[id]->Layout->SetSize(io->Ren->GetViewport().zw());
|
||||
menu = this->menus.find(id);
|
||||
}
|
||||
this->current = menu->second;
|
||||
this->current->is_shown = show;
|
||||
this->io->InputHandler->CurrentMenu = this->current->id;
|
||||
io->RegisterDrawList(id, this->current->Layout->GetDrawList());
|
||||
this->current->PreHandler(flags);
|
||||
amenus.push_back(this->current->GetID());
|
||||
if (!this->current->is_open) {
|
||||
this->current = nullptr;
|
||||
}
|
||||
return this->current != nullptr;
|
||||
}
|
||||
|
||||
PD_UI7_API UI7::Menu::Ref UI7::Context::GetCurrentMenu() {
|
||||
// Assert(current != nullptr, "Not in a Menu!");
|
||||
return current;
|
||||
}
|
||||
|
||||
PD_UI7_API UI7::Menu::Ref UI7::Context::FindMenu(const ID& id) {
|
||||
auto e = this->menus.find(id);
|
||||
if (e != this->menus.end()) {
|
||||
return e->second;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Context::EndMenu() {
|
||||
this->current->PostHandler();
|
||||
this->current = nullptr;
|
||||
this->io->InputHandler->CurrentMenu = 0;
|
||||
}
|
||||
|
||||
PD_UI7_API bool UI7::Context::DoMenuEx(
|
||||
const UI7::ID& id, UI7MenuFlags flags,
|
||||
std::function<void(UI7::ReMenu::Ref m)> f) {
|
||||
if (!Current) {
|
||||
Current = ReMenu::New(id, io);
|
||||
}
|
||||
// Current->pIsShown = show;
|
||||
io->InputHandler->CurrentMenu = Current->pID;
|
||||
io->RegisterDrawList(id, Current->pLayout->GetDrawList());
|
||||
if (Current->pIsOpen) {
|
||||
f(Current);
|
||||
}
|
||||
return Current != nullptr;
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Context::Update(float) {
|
||||
TT::Scope st("UI7_Update");
|
||||
// Assert(current == nullptr, "Still in a Menu!");
|
||||
if (!io->InputHandler->FocusedMenu && amenus.size() > 0) {
|
||||
io->InputHandler->FocusedMenu = amenus[amenus.size() - 1];
|
||||
}
|
||||
bool focused_exist = false;
|
||||
if (aml.size() == 0) {
|
||||
aml = amenus;
|
||||
} else {
|
||||
std::vector<size_t> tbr;
|
||||
for (size_t i = 0; i < aml.size(); i++) {
|
||||
if (std::find(amenus.begin(), amenus.end(), aml[i]) == amenus.end()) {
|
||||
tbr.push_back(i);
|
||||
}
|
||||
}
|
||||
for (auto& it : tbr) {
|
||||
aml.erase(aml.begin() + it);
|
||||
}
|
||||
}
|
||||
for (auto& it : amenus) {
|
||||
if (std::find(aml.begin(), aml.end(), it) == aml.end()) {
|
||||
aml.push_back(it);
|
||||
}
|
||||
}
|
||||
auto ptf = std::find(aml.begin(), aml.end(), io->InputHandler->FocusedMenu);
|
||||
if (ptf != aml.end() && ptf != aml.begin()) {
|
||||
std::rotate(aml.begin(), ptf, ptf + 1);
|
||||
}
|
||||
for (auto it : aml) {
|
||||
auto m = menus[it];
|
||||
io->InputHandler->CurrentMenu = m->id;
|
||||
m->Update(io->Delta);
|
||||
io->InputHandler->CurrentMenu = 0;
|
||||
if (it == io->InputHandler->FocusedMenu) {
|
||||
focused_exist = true;
|
||||
}
|
||||
}
|
||||
io->InputHandler->CurrentMenu = Current->pID;
|
||||
Current->Update();
|
||||
/*if (!focused_exist && io->CurrentMenu != Current->pID) {
|
||||
io->FocusedMenu = 0;
|
||||
io->FocusedMenuRect = 0;
|
||||
}*/
|
||||
int list = 0;
|
||||
u32 vtx_counter = 0;
|
||||
u32 idx_counter = 0;
|
||||
// Register Front List as last element
|
||||
io->RegisterDrawList("CtxFrontList", io->Front);
|
||||
// io->DrawListRegestry.Reverse();
|
||||
for (auto it : io->DrawListRegestry) {
|
||||
it.Second->Base = list * 30;
|
||||
it.Second->Process(io->pRDL);
|
||||
vtx_counter += it.Second->NumVertices;
|
||||
idx_counter += it.Second->NumIndices;
|
||||
list++;
|
||||
}
|
||||
io->Ren->RegisterDrawList(io->pRDL);
|
||||
io->NumIndices = idx_counter;
|
||||
io->NumVertices = vtx_counter;
|
||||
this->amenus.clear();
|
||||
this->io->Update();
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Context::AboutMenu(bool* show) {
|
||||
if (this->BeginMenu("About UI7", UI7MenuFlags_Scrolling, show)) {
|
||||
auto m = this->GetCurrentMenu();
|
||||
|
||||
m->Label("Palladium - UI7 " + GetVersion());
|
||||
m->Separator();
|
||||
m->Label("(c) 2023-2025 René Amthor");
|
||||
m->Label("UI7 is licensed under the MIT License.");
|
||||
m->Label("See LICENSE for more information.");
|
||||
static bool show_build;
|
||||
m->Checkbox("Show Build Info", show_build);
|
||||
if (show_build) {
|
||||
m->SeparatorText("Build Info");
|
||||
m->Label("Full Version -> " + GetVersion(true));
|
||||
m->Label("sizeof(size_t) -> " + std::to_string(sizeof(size_t)));
|
||||
m->Label("sizeof(LI::Vertex) -> " + std::to_string(sizeof(LI::Vertex)));
|
||||
m->Label("__cplusplus -> " + std::to_string(__cplusplus));
|
||||
m->Label("Compiler -> " + LibInfo::CompiledWith());
|
||||
}
|
||||
this->EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Context::MetricsMenu(bool* show) {
|
||||
if (this->BeginMenu("UI7 Metrics", UI7MenuFlags_Scrolling, show)) {
|
||||
auto m = this->GetCurrentMenu();
|
||||
|
||||
m->Label("Palladium - UI7 " + GetVersion());
|
||||
m->Separator();
|
||||
m->Label(
|
||||
std::format("Average {:.3f} ms/f ({:.1f} FPS)",
|
||||
((float)io->DeltaStats->GetAverage() / 1000.f),
|
||||
1000.f / ((float)io->DeltaStats->GetAverage() / 1000.f)));
|
||||
m->Label(std::format("NumVertices: {}", io->NumVertices));
|
||||
m->Label(std::format("NumIndices: {} -> {} Tris", io->NumIndices,
|
||||
io->NumIndices / 3));
|
||||
m->Label("Menus: " + std::to_string(menus.size()));
|
||||
/*if (m->BeginTreeNode("Font")) {
|
||||
for (u32 i = 0; i <= 0x00ff; i++) {
|
||||
auto& c = io->Ren->Font()->GetCodepoint(i);
|
||||
if (!c.invalid()) {
|
||||
m->Image(c.tex(), c.size(), c.uv());
|
||||
if ((i % 15) != 0 || i == 0) {
|
||||
m->SameLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}*/
|
||||
m->SeparatorText("TimeTrace");
|
||||
if (m->BeginTreeNode("Traces (" +
|
||||
std::to_string(Sys::GetTraceMap().size()) + ")")) {
|
||||
for (auto& it : Sys::GetTraceMap()) {
|
||||
if (m->BeginTreeNode(it.second->GetID())) {
|
||||
m->Label("Diff: " + UI7DTF(it.second->GetLastDiff()));
|
||||
m->Label("Protocol Len: " +
|
||||
std::to_string(it.second->GetProtocol()->GetLen()));
|
||||
m->Label("Average: " +
|
||||
UI7DTF(it.second->GetProtocol()->GetAverage()));
|
||||
m->Label("Min: " + UI7DTF(it.second->GetProtocol()->GetMin()));
|
||||
m->Label("Max: " + UI7DTF(it.second->GetProtocol()->GetMax()));
|
||||
m->EndTreeNode();
|
||||
}
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
m->SeparatorText("IO");
|
||||
if (m->BeginTreeNode("Menus (" + std::to_string(menus.size()) + ")")) {
|
||||
for (auto& it : menus) {
|
||||
if (m->BeginTreeNode(it.second->name)) {
|
||||
m->Label("Name: " + it.second->name);
|
||||
/*m->Label("Pos: " + UI7DV2N(it.second->Layout->GetPosition()));
|
||||
m->Label("Size: " + UI7DV2N(it.second->Layout->GetSize()));
|
||||
m->Label("Work Rect: " + UI7DV4N(it.second->Layout->WorkRect));
|
||||
m->Label("Cursor: " + UI7DV2N(it.second->Layout->Cursor));*/
|
||||
if (m->BeginTreeNode(
|
||||
"ID Objects (" +
|
||||
std::to_string(it.second->Layout->IDObjects.size()) + ")")) {
|
||||
for (auto& jt : it.second->Layout->IDObjects) {
|
||||
m->Label(UI7DHX32(jt->GetID()));
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
if (m->BeginTreeNode("Active Menus (" + std::to_string(aml.size()) + ")")) {
|
||||
for (auto& it : aml) {
|
||||
if (m->BeginTreeNode(menus[it]->name)) {
|
||||
m->Label("Name: " + menus[it]->name);
|
||||
/*m->Label("Pos: " + UI7DV2N(it.second->Layout->Pos));
|
||||
m->Label("Size: " + UI7DV2N(it.second->Layout->GetSize()));
|
||||
m->Label("Work Rect: " + UI7DV4N(it.second->Layout->WorkRect));
|
||||
m->Label("Cursor: " + UI7DV2N(it.second->Layout->Cursor));*/
|
||||
if (m->BeginTreeNode(
|
||||
"ID Objects (" +
|
||||
std::to_string(menus[it]->Layout->IDObjects.size()) + ")")) {
|
||||
for (auto& jt : menus[it]->Layout->IDObjects) {
|
||||
m->Label(UI7DHX32(jt->GetID()));
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
if (m->BeginTreeNode("DrawLists (" +
|
||||
std::to_string(io->DrawListRegestry.Size()) + ")")) {
|
||||
for (auto& it : io->DrawListRegestry) {
|
||||
if (m->BeginTreeNode(it.First.GetName())) {
|
||||
m->Label("Vertices: " + std::to_string(it.Second->NumVertices));
|
||||
m->Label("Indices: " + std::to_string(it.Second->NumIndices));
|
||||
m->Label("Base Layer: " + std::to_string(it.Second->Base));
|
||||
m->EndTreeNode();
|
||||
}
|
||||
}
|
||||
m->EndTreeNode();
|
||||
}
|
||||
m->Label("io->Time: " + Strings::FormatMillis(io->Time->Get()));
|
||||
m->Label(std::format("io->Delta: {:.3f}", io->Delta));
|
||||
m->Label(std::format("io->Framerate: {:.2f}", io->Framerate));
|
||||
m->Label(UI7DHX32(io->InputHandler->FocusedMenu));
|
||||
m->Label(UI7DHX32(io->InputHandler->DraggedObject));
|
||||
m->Label(std::format("io->DragTime: {:.2f}s",
|
||||
io->InputHandler->DragTime->GetSeconds()));
|
||||
m->Label(UI7DV4(io->InputHandler->DragDestination));
|
||||
m->Label(UI7DV2(io->InputHandler->DragSourcePos));
|
||||
m->Label(UI7DV2(io->InputHandler->DragPosition));
|
||||
m->Label(UI7DV2(io->InputHandler->DragLastPosition));
|
||||
this->EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
PD_UI7_API void UI7::Context::StyleEditor(bool* show) {
|
||||
if (this->BeginMenu("UI7 Style Editor", UI7MenuFlags_Scrolling, show)) {
|
||||
auto m = this->GetCurrentMenu();
|
||||
|
||||
m->Label("Palladium - UI7 " + GetVersion() + " Style Editor");
|
||||
m->Separator();
|
||||
m->DragData("MenuPadding", (float*)&io->MenuPadding, 2, 0.f, 100.f);
|
||||
m->DragData("FramePadding", (float*)&io->FramePadding, 2, 0.f, 100.f);
|
||||
m->DragData("ItemSpace", (float*)&io->ItemSpace, 2, 0.f, 100.f);
|
||||
m->DragData("MinSliderSize", (float*)&io->MinSliderDragSize, 2, 1.f, 100.f);
|
||||
m->DragData("OverScroll Modifier", &io->OverScrollMod, 1, 0.01f,
|
||||
std::numeric_limits<float>::max(), 0.01f, 2);
|
||||
m->Checkbox("Menu Border", io->ShowMenuBorder);
|
||||
m->Checkbox("Frame Border", io->ShowFrameBorder);
|
||||
m->SeparatorText("Theme");
|
||||
if (m->Button("Dark")) {
|
||||
UI7::Theme::Default(*io->Theme.get());
|
||||
}
|
||||
m->SameLine();
|
||||
if (m->Button("Flashbang")) {
|
||||
UI7::Theme::Flashbang(*io->Theme.get());
|
||||
}
|
||||
/// Small trick to print without prefix
|
||||
#define ts(x) m->ColorEdit(std::string(#x).substr(9), &io->Theme->GetRef(x));
|
||||
#define ts2(x) \
|
||||
m->DragData(std::string(#x).substr(9), (u8*)&io->Theme->GetRef(x), 4, (u8)0, \
|
||||
(u8)255);
|
||||
ts2(UI7Color_Background);
|
||||
ts2(UI7Color_Border);
|
||||
ts2(UI7Color_Button);
|
||||
ts2(UI7Color_ButtonDead);
|
||||
ts2(UI7Color_ButtonActive);
|
||||
ts2(UI7Color_ButtonHovered);
|
||||
ts2(UI7Color_Text);
|
||||
ts2(UI7Color_TextDead);
|
||||
ts2(UI7Color_Header);
|
||||
ts2(UI7Color_HeaderDead);
|
||||
ts2(UI7Color_Selector);
|
||||
ts2(UI7Color_Checkmark);
|
||||
ts2(UI7Color_FrameBackground);
|
||||
ts2(UI7Color_FrameBackgroundHovered);
|
||||
ts2(UI7Color_Progressbar);
|
||||
ts2(UI7Color_ListEven);
|
||||
ts2(UI7Color_ListOdd);
|
||||
this->EndMenu();
|
||||
}
|
||||
}
|
||||
} // namespace PD
|
||||
Reference in New Issue
Block a user