Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
35cad11
commit 9978a32
Showing
10 changed files
with
994 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
/* Reverse Engineer's Hex Editor | ||
* Copyright (C) 2024 Daniel Collins <solemnwarning@solemnwarning.net> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 as published by | ||
* the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
* more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program; if not, write to the Free Software Foundation, Inc., 51 | ||
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
#include "App.hpp" | ||
#include "ByteColourMap.hpp" | ||
|
||
const wxString &REHex::ByteColourMap::get_label() const | ||
{ | ||
return label; | ||
} | ||
|
||
void REHex::ByteColourMap::set_label(const wxString &label) | ||
{ | ||
this->label = label; | ||
} | ||
|
||
void REHex::ByteColourMap::set_colour(unsigned char byte, Colour colour1, Colour colour2, int colour_delta_steps, int colour_delta_pos) | ||
{ | ||
auto &elem = bytes[byte]; | ||
|
||
assert(colour_delta_steps >= colour_delta_pos); | ||
|
||
elem.colour1 = colour1; | ||
elem.colour2 = colour2; | ||
elem.colour_delta_steps = colour_delta_steps; | ||
elem.colour_delta_pos = colour_delta_pos; | ||
} | ||
|
||
void REHex::ByteColourMap::set_colour(unsigned char byte, Colour colour) | ||
{ | ||
set_colour(byte, colour, colour, 0, 0); | ||
} | ||
|
||
wxColour REHex::ByteColourMap::get_colour(unsigned char byte) const | ||
{ | ||
auto &elem = bytes[byte]; | ||
|
||
return elem.get_colour(); | ||
} | ||
|
||
const REHex::ByteColourMap::Value &REHex::ByteColourMap::get_value(unsigned char byte) const | ||
{ | ||
return bytes[byte]; | ||
} | ||
|
||
bool REHex::ByteColourMap::Value::is_single() const | ||
{ | ||
return colour_delta_steps == 0; | ||
} | ||
|
||
bool REHex::ByteColourMap::Value::is_start() const | ||
{ | ||
return colour_delta_pos == 0 && colour_delta_steps > 0; | ||
} | ||
|
||
bool REHex::ByteColourMap::Value::is_end() const | ||
{ | ||
return colour_delta_pos == colour_delta_steps && colour_delta_steps > 0; | ||
} | ||
|
||
wxColour REHex::ByteColourMap::Value::get_colour() const | ||
{ | ||
wxColour c1, c2; | ||
|
||
if(colour1.is_palette_colour()) | ||
{ | ||
c1 = (*active_palette)[colour1.palette_colour()]; | ||
} | ||
else if(colour1.is_highlight_colour()) | ||
{ | ||
HighlightColourMap highlight_colours = wxGetApp().settings->get_highlight_colours(); | ||
c2 = highlight_colours[colour1.highlight_colour()].primary_colour; | ||
} | ||
else{ | ||
abort(); /* Unreachable. */ | ||
} | ||
|
||
if(colour_delta_steps == 0) | ||
{ | ||
return c1; | ||
} | ||
|
||
if(colour2.is_palette_colour()) | ||
{ | ||
c2 = (*active_palette)[colour2.palette_colour()]; | ||
} | ||
else if(colour2.is_highlight_colour()) | ||
{ | ||
HighlightColourMap highlight_colours = wxGetApp().settings->get_highlight_colours(); | ||
c2 = highlight_colours[colour2.highlight_colour()].primary_colour; | ||
} | ||
else{ | ||
abort(); /* Unreachable. */ | ||
} | ||
|
||
float alpha = (float)(colour_delta_pos) / (float)(colour_delta_steps); | ||
|
||
unsigned char r = wxColour::AlphaBlend(c2.Red(), c1.Red(), alpha); | ||
unsigned char g = wxColour::AlphaBlend(c2.Green(), c1.Green(), alpha); | ||
unsigned char b = wxColour::AlphaBlend(c2.Blue(), c1.Blue(), alpha); | ||
|
||
return wxColour(r, g, b); | ||
} | ||
|
||
std::string REHex::ByteColourMap::serialise_colour_index(Colour index) | ||
{ | ||
char buf[64]; | ||
|
||
if(index.is_palette_colour() && index.palette_colour() == Palette::PAL_NORMAL_TEXT_FG) | ||
{ | ||
strcpy(buf, "PAL_NORMAL_TEXT_FG"); | ||
} | ||
else if(index.is_highlight_colour()) | ||
{ | ||
snprintf(buf, sizeof(buf), "highlight.%zu.primary", index.highlight_colour()); | ||
} | ||
else{ | ||
throw std::invalid_argument("Unexpected index in REHex::ByteColourMap::serialise_colour_index()"); | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
REHex::ByteColourMap::Colour REHex::ByteColourMap::deserialise_colour_index(const std::string &string) | ||
{ | ||
int index; | ||
|
||
if(string == "PAL_NORMAL_TEXT_FG") | ||
{ | ||
return Colour::PaletteColour(Palette::PAL_NORMAL_TEXT_FG); | ||
} | ||
else if(sscanf(string.c_str(), "highlight.%d.primary", &index) == 1 && index >= 0 && index < (int)(HighlightColourMap::MAX_NUM)) | ||
{ | ||
return Colour::HighlightColour(index); | ||
} | ||
|
||
throw std::invalid_argument("Invalid string passed to REHex::ByteColourMap::deserialise_colour_index()"); | ||
} | ||
|
||
REHex::ByteColourMap REHex::ByteColourMap::load(const wxConfigBase *config) | ||
{ | ||
ByteColourMap map; | ||
|
||
map.label = config->Read("label", map.label); | ||
|
||
for(int i = 0; i < 256; ++i) | ||
{ | ||
char path[8]; | ||
snprintf(path, sizeof(path), "%d/", i); | ||
|
||
if(!config->HasGroup(path)) | ||
{ | ||
continue; | ||
} | ||
|
||
wxConfigPathChanger scoped_path(config, path); | ||
|
||
map.bytes[i].colour1 = deserialise_colour_index(config->Read("colour1", wxEmptyString).ToStdString()); | ||
map.bytes[i].colour2 = deserialise_colour_index(config->Read("colour2", wxEmptyString).ToStdString()); | ||
|
||
map.bytes[i].colour_delta_steps = config->Read("colour_delta_steps", (long)(-1)); | ||
map.bytes[i].colour_delta_pos = config->Read("colour_delta_pos", (long)(-1)); | ||
|
||
if(map.bytes[i].colour_delta_steps < 0 | ||
|| map.bytes[i].colour_delta_pos < 0 | ||
|| map.bytes[i].colour_delta_pos > map.bytes[i].colour_delta_steps) | ||
{ | ||
throw std::invalid_argument("invalid colour_delta_steps/colour_delta_pos values"); | ||
} | ||
} | ||
|
||
return map; | ||
} | ||
|
||
void REHex::ByteColourMap::save(wxConfigBase *config) | ||
{ | ||
config->Write("label", label); | ||
|
||
for(int i = 0; i < 256; ++i) | ||
{ | ||
if(bytes[i] == Value()) | ||
{ | ||
/* Skip serialising default colours. */ | ||
continue; | ||
} | ||
|
||
char path[8]; | ||
snprintf(path, sizeof(path), "%d/", i); | ||
wxConfigPathChanger scoped_path(config, path); | ||
|
||
std::string colour1 = serialise_colour_index(bytes[i].colour1); | ||
std::string colour2 = serialise_colour_index(bytes[i].colour2); | ||
|
||
config->Write("colour1", wxString(colour1)); | ||
config->Write("colour2", wxString(colour2)); | ||
|
||
config->Write("colour_delta_steps", (long)(bytes[i].colour_delta_steps)); | ||
config->Write("colour_delta_pos", (long)(bytes[i].colour_delta_pos)); | ||
} | ||
} |
Oops, something went wrong.