Skip to content

Commit

Permalink
wip of property rework
Browse files Browse the repository at this point in the history
  • Loading branch information
gulrak committed Sep 20, 2024
1 parent 8d75175 commit 1c0d8af
Show file tree
Hide file tree
Showing 16 changed files with 221 additions and 192 deletions.
109 changes: 53 additions & 56 deletions src/cadmium.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ class Cadmium : public emu::EmuHostEx
#ifdef PLATFORM_WEB
JsClipboard_AddJsHook();
#else
_cfgPath = (fs::path(dataPath())/"config.json").string();
_volume = _volumeSlider = _cfg.volume;
_styleManager.updateStyle(_cfg.guiHue, _cfg.guiSat, false);
#endif
Expand Down Expand Up @@ -884,7 +885,7 @@ class Cadmium : public emu::EmuHostEx
return Vector3Distance(labC1, labC2);;
}

static inline uint32_t rgb332To888(uint8_t c)
static uint32_t rgb332To888(uint8_t c)
{
static uint8_t b3[] = {0, 0x20, 0x40, 0x60, 0x80, 0xA0, 0xC0, 0xff};
static uint8_t b2[] = {0, 0x60, 0xA0, 0xff};
Expand Down Expand Up @@ -975,9 +976,11 @@ class Cadmium : public emu::EmuHostEx
}
else {
// TraceLog(LOG_INFO, "Updating MC8 screen!");
const auto* screen = _chipEmu->getScreenRGBA();
screen->convert(pixel, _screen.width, _chipEmu->getScreenAlpha(), _chipEmu->getWorkRGBA());
UpdateTexture(_screenTexture, _screen.data);
const auto* screenRgb = _chipEmu->getScreenRGBA();
if(screenRgb) {
screenRgb->convert(pixel, _screen.width, _chipEmu->getScreenAlpha(), _chipEmu->getWorkRGBA());
UpdateTexture(_screenTexture, _screen.data);
}
}
}
}
Expand Down Expand Up @@ -1667,22 +1670,22 @@ class Cadmium : public emu::EmuHostEx

static int editProperty(emu::Property& prop, bool forceUpdate, PropertyAlign pa = PA_RIGHT)
{
gui::BeginColumns();
gui::SetSpacing(4);
gui::SetNextWidth(90);
auto prevTextAlignment = GuiGetStyle(LABEL, TEXT_ALIGNMENT);
if(pa == PA_RIGHT) {
gui::BeginColumns();
gui::SetSpacing(4);
gui::SetNextWidth(90);
gui::SetStyle(LABEL, TEXT_ALIGNMENT, TEXT_ALIGN_RIGHT);
gui::Label(fmt::format("{}", prop.getName()).c_str());
gui::SetStyle(LABEL, TEXT_ALIGNMENT, prevTextAlignment);
}
//gui::SetNextWidth(150);
if (prop.isReadonly())
if (prop.access() != emu::eWritable)
GuiDisable();
const auto rc = std::visit(emu::visitor{
[](std::nullptr_t) -> int { gui::Label(""); return 0; },
[pa, &prop](bool& val) -> int { val = gui::CheckBox(pa == PA_RIGHT ? "" : prop.getName().c_str(), val); return val ? 1 : 0; },
[](emu::Property::Integer& val) -> int { gui::Spinner("", &val.intValue, val.minValue, val.maxValue); return val.intValue; },
[pa, &prop](emu::Property::Integer& val) -> int { gui::Spinner(pa == PA_RIGHT ? "" : prop.getName().c_str(), &val.intValue, val.minValue, val.maxValue); return val.intValue; },
[](std::string& val) -> int {
auto prevTextAlignment = GuiGetStyle(TEXTBOX, TEXT_ALIGNMENT);
gui::SetStyle(TEXTBOX, TEXT_ALIGNMENT, TEXT_ALIGN_CENTER);
Expand All @@ -1696,9 +1699,11 @@ class Cadmium : public emu::EmuHostEx
return val.index;
}},
prop.getValue());
if (prop.isReadonly())
if (prop.access() != emu::eWritable)
GuiEnable();
gui::EndColumns();
if(pa == PA_RIGHT) {
gui::EndColumns();
}
return rc;
}

Expand All @@ -1718,7 +1723,7 @@ class Cadmium : public emu::EmuHostEx
int editPropertySpinner(std::string_view key, bool forceUpdate, int defaultValue = 0)
{
if(_properties.containsFuzzy(key)) {
return editProperty(_properties.at(key), forceUpdate);
return editProperty(_properties.at(key), forceUpdate, PA_LEFT);
}
static int dummyInt = defaultValue;
GuiDisable();
Expand Down Expand Up @@ -1863,32 +1868,34 @@ class Cadmium : public emu::EmuHostEx
SetNextWidth(colWidth1);
Begin();
SetSpacing(2);
auto* rcb = dynamic_cast<emu::Chip8RealCoreBase*>(_chipEmu.get());
if(rcb) {
static emu::Properties propsMemento;
auto& props = _properties;
if(props != propsMemento)
propsMemento = props;
for(size_t i = 0; i < props.numProperties(); ++i) {
auto& prop = props[i];
if(prop.getName().empty()) {
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
Begin();
SetSpacing(2);
static emu::Properties propsMemento;
auto& props = _properties;
if(props != propsMemento)
propsMemento = props;
for(size_t i = 0; i < props.numProperties(); ++i) {
auto& prop = props[i];
if(prop.getName().empty()) {
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
End();
Begin();
SetSpacing(2);
}
else if(prop.access() != emu::eInvisible && !fuzzyAnyOf(prop.getName(), {"TraceLog", "InstructionsPerFrame", "FrameRate"})) {
if(props.numProperties() > 20 && std::holds_alternative<bool>(prop.getValue())) {
editProperty(prop, forceUpdate, PA_LEFT);
}
else if(!fuzzyAnyOf(prop.getName(), {"TraceLog"})) {
else {
editProperty(prop, forceUpdate);
++rowCount;
}
++rowCount;
}
/*auto* changedProp = props.changedProperty(propsMemento);
if(changedProp) {
// on change...
updateEmulatorOptions(_options);
//rcb->updateProperties(*changedProp);
}*/
}
auto* changedProp = props.changedProperty(propsMemento);
if(changedProp) {
// on change...
updateEmulatorOptions(props);
//rcb->updateProperties(*changedProp);
}
auto used = GetCurrentPos().y - startY;
Space(quirksHeight - used - 4);
Expand All @@ -1901,18 +1908,21 @@ class Cadmium : public emu::EmuHostEx
StyleManager::Scope guard;
BeginColumns();
auto pos = GetCurrentPos();
pos.x = std::ceil(pos.x);
pos.y = std::ceil(pos.y);
SetNextWidth(52.0f + 16*18);
Label("Colors:");
for (int i = 0; i < 16; ++i) {
DrawRectangle(pos.x + 52 + i * 18 + 2, pos.y + 2 , 12, 12, GetColor(_colorPalette[i]));
bool hover = CheckCollisionPointRec(GetMousePosition(), {pos.x + 52 + i * 18, pos.y, 16, 16});
DrawRectangle(pos.x + 52 + i * 18, pos.y, 16, 16, GetColor(guard.getStyle(hover ? Style::BORDER_COLOR_FOCUSED : Style::BORDER_COLOR_NORMAL)));
DrawRectangle(pos.x + 52 + i * 18 + 1, pos.y + 1 , 14, 14, GetColor(guard.getStyle(Style::BACKGROUND_COLOR)));
DrawRectangle(pos.x + 52 + i * 18 + 2, pos.y + 2 , 12, 12, GetColor(_colorPalette[i]));
if(!GuiIsLocked() && IsMouseButtonReleased(0) && hover) {
_selectedColor = &_colorPalette[i];
_previousColor = _colorPalette[i];
_colorText = fmt::format("{:06x}", _colorPalette[i]>>8);
_colorSelectOpen = true;
}
DrawRectangleLines(pos.x + 52 + i * 18, pos.y, 16, 16, GetColor(guard.getStyle(hover ? Style::BORDER_COLOR_FOCUSED : Style::BORDER_COLOR_NORMAL)));
}
static std::vector<uint32_t> prevPalette(_colorPalette.begin(), _colorPalette.end());
if(std::memcmp(prevPalette.data(), _colorPalette.data(), 16*sizeof(uint32_t)) != 0) {
Expand Down Expand Up @@ -2175,7 +2185,7 @@ class Cadmium : public emu::EmuHostEx
//}
// TODO: Fix this
// auto options = _options;
loadRom(_librarian.fullPath(selectedInfo.filePath).c_str(), LoadOptions::None);
loadRom(_librarian.fullPath(selectedInfo.filePath).c_str(), LoadOptions::DontChangeOptions);
// TODO: Fix this
// updateEmulatorOptions(options);
_mainView = _lastView;
Expand Down Expand Up @@ -2321,7 +2331,6 @@ class Cadmium : public emu::EmuHostEx

void saveConfig()
{
return; // TODO: Reactivate config saving
#ifndef PLATFORM_WEB
if(!_cfgPath.empty()) {
auto opt = _properties;
Expand All @@ -2330,7 +2339,7 @@ class Cadmium : public emu::EmuHostEx
pal[i] = fmt::format("#{:06x}", _defaultPalette[i] >> 8);
}
// opt.advanced["palette"] = pal;
_cfg.emuOptions = _properties;
_cfg.emuProperties = _properties;
_cfg.workingDirectory = _currentDirectory;
_cfg.databaseDirectory = _databaseDirectory;
if(!_cfg.save(_cfgPath)) {
Expand All @@ -2342,22 +2351,10 @@ class Cadmium : public emu::EmuHostEx

void updateBehaviorSelects()
{
_subBehaviorSel = static_cast<int>(emu::CoreRegistry::variantIndex(_properties).index);
// TODO: Fix this
/*
auto result = std::find_if(_presetMapping.begin(), _presetMapping.end(), [this](const auto& pair) {return pair.second == _options.behaviorBase; });
if(result != _presetMapping.end()) {
_behaviorSel = result->first.first;
if(_behaviorSel == 12)
_subBehaviorSel = result->first.second, _subBehaviorSel2 = 0;
else if(_behaviorSel == 13)
_subBehaviorSel2 = result->first.second, _subBehaviorSel = 0;
else
_subBehaviorSel = _subBehaviorSel2 = 0;
if (auto idx = _cores.classIndex(_properties); idx >= 0) {
_behaviorSel = idx;
_subBehaviorSel = static_cast<int>(emu::CoreRegistry::variantIndex(_properties).index);
}
else
_behaviorSel = emu::Chip8EmulatorOptions::eXOCHIP;
*/
}

void whenEmuChanged(emu::IEmulationCore& emu) override
Expand Down Expand Up @@ -2842,7 +2839,7 @@ int main(int argc, char* argv[])
auto oldCat = cli.category(fmt::format("{} Options (only available if preset uses {} core)", name, info->prefix().empty() ? "default" : toOptionName(info->prefix())));
for(size_t i = 0; i < proto.numProperties(); ++i) {
auto& prop = proto[i];
if(!prop.isReadonly()) {
if(prop.access() == emu::eWritable) {
auto dependencyCheck = [&presetName, info](){ return info->hasVariant(presetName); };
std::visit(emu::visitor{
[](std::nullptr_t) -> void { },
Expand Down Expand Up @@ -2913,7 +2910,7 @@ int main(int argc, char* argv[])
CadmiumConfiguration config;
auto cfgPath = (fs::path(dataPath())/"config.json").string();
if(config.load(cfgPath)) {
coreProperties = config.emuOptions;
coreProperties = config.emuProperties;
}

try {
Expand Down
4 changes: 2 additions & 2 deletions src/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void to_json(nlohmann::json& j, const CadmiumConfiguration& cc) {
{"guiSaturation", cc.guiSat},
{"workingDirectory", cc.workingDirectory},
{"databaseDirectory", cc.databaseDirectory},
{"emuOptions", cc.emuOptions},
{"emuProperties", cc.emuProperties},
{"romConfigs", cc.romConfigs}
};
}
Expand All @@ -47,7 +47,7 @@ void from_json(const nlohmann::json& j, CadmiumConfiguration& cc) {
cc.guiHue = j.value("guiHue", 192);
cc.guiSat = j.value("guiSaturation", 90);
try {
j.at("emuOptions").get_to(cc.emuOptions);
j.at("emuProperties").get_to(cc.emuProperties);
}
catch(...) {}
try {
Expand Down
2 changes: 1 addition & 1 deletion src/configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct CadmiumConfiguration
uint8_t guiSat{80};
std::string workingDirectory;
std::string databaseDirectory;
emu::Properties emuOptions;
emu::Properties emuProperties;
std::map<std::string,emu::Properties> romConfigs;
bool load(const std::string& filepath);
bool save(const std::string& filepath);
Expand Down
2 changes: 1 addition & 1 deletion src/emuhostex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ EmuHostEx::EmuHostEx(CadmiumConfiguration& cfg)
, _librarian(_cfg)
{
#ifndef PLATFORM_WEB
_currentDirectory = _cfg.workingDirectory;
_currentDirectory = _cfg.workingDirectory.empty() ? fs::current_path().string() : _cfg.workingDirectory;
_databaseDirectory = _cfg.databaseDirectory;
_librarian.fetchDir(_currentDirectory);
#endif
Expand Down
Loading

0 comments on commit 1c0d8af

Please sign in to comment.