Skip to content

Commit

Permalink
kramv - add atlas hud text
Browse files Browse the repository at this point in the history
Show the location and dims in pixels and the name in the hud.  This is based on the cursor location.  Pixel locations are scaled to the current mipNumber.
  • Loading branch information
alecazam committed Jan 3, 2023
1 parent e983b6e commit 7ed6017
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
30 changes: 21 additions & 9 deletions kramv/KramViewerBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -961,11 +961,14 @@ bool isPtInRect(float2 pt, float4 r)
return all((pt >= r.xy) & (pt <= r.xy + r.zw));
}

const Atlas* Data::findAtlasAtCursor(float2 pt)
const Atlas* Data::findAtlasAtUV(float2 pt)
{
if (_showSettings->atlas.empty()) return nullptr;
if (_showSettings->imageBoundsX == 0) return nullptr;

const Atlas* atlas = nullptr;

// TODO: rects are in uv, so need to convert pt
// Note: rects are in uv

// This might need to become an atlas array index instead of ptr
const Atlas* lastAtlas = _showSettings->lastAtlas;
Expand Down Expand Up @@ -1447,6 +1450,8 @@ void Data::showEyedropperData(const float2& uv)
break;
}

// TODO: indicate px, mip, etc (f.e. showAll)

// debug mode

// preview vs. not
Expand Down Expand Up @@ -1480,10 +1485,6 @@ void Data::showEyedropperData(const float2& uv)
mipX = (int32_t)(uv.x * mipX);
mipY = (int32_t)(uv.y * mipY);

// Has to be set in other call, not here
//_showSettings->textureLookupMipX = mipX;
//_showSettings->textureLookupMipY = mipY;

// TODO: may want to return mip in pixel readback
// don't have it right now, so don't display if preview is enabled
if (_showSettings->isPreview)
Expand Down Expand Up @@ -1605,17 +1606,28 @@ void Data::setEyedropperText(const char * text)
setTextSlot(kTextSlotEyedropper, text);
}

string Data::textFromSlots() const
void Data::setAtlasText(const char * text)
{
setTextSlot(kTextSlotAtlas, text);
}

string Data::textFromSlots(bool isFileListHidden) const
{
// combine textSlots
string text = _textSlots[kTextSlotHud];
if (!text.empty() && text.back() != '\n')
text += "\n";

// don't show eyedropper text with table up, it's many lines and overlaps
// TODO: fix
// if (!_tableView.hidden)
if (!isFileListHidden)
{
text += _textSlots[kTextSlotEyedropper];
if (!text.empty() && text.back() != '\n')
text += "\n";

text += _textSlots[kTextSlotAtlas];
}


return text;
}
Expand Down
10 changes: 6 additions & 4 deletions kramv/KramViewerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,8 @@ struct ActionState
enum TextSlot
{
kTextSlotHud,
kTextSlotEyedropper
kTextSlotEyedropper,
kTextSlotAtlas
};

struct File {
Expand Down Expand Up @@ -464,7 +465,7 @@ struct Data {

bool findFilename(const string& filename);
bool findFilenameShort(const string& filename);
const Atlas* findAtlasAtCursor(float2 pt);
const Atlas* findAtlasAtUV(float2 uv);
bool isArchive() const;
bool loadFile();

Expand All @@ -482,7 +483,7 @@ struct Data {
vector<Action>& actions() { return _actions; }
void initDisabledButtons();

string textFromSlots() const;
string textFromSlots(bool isFileListHidden) const;
void setTextSlot(TextSlot slot, const char* text);

void loadFilesFromUrls(vector<string>& urls, bool skipSubdirs);
Expand All @@ -507,8 +508,9 @@ struct Data {
public:
void showEyedropperData(const float2& uv);
void setEyedropperText(const char * text);
void setAtlasText(const char * text);
void updateTransforms();

//----------------
float4x4 _projectionMatrix;

Expand Down
26 changes: 23 additions & 3 deletions kramv/KramViewerMain.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1082,12 +1082,32 @@ -(void)updateEyedropperText
if (_showSettings->imageBoundsX == 0) return;

float2 uv;
uv.x = _showSettings->textureLookupX / _showSettings->imageBoundsX;
uv.y = _showSettings->textureLookupY / _showSettings->imageBoundsY;
uv.x = _showSettings->textureLookupX / (float)_showSettings->imageBoundsX;
uv.y = _showSettings->textureLookupY / (float)_showSettings->imageBoundsY;

// convert data to text
_data.showEyedropperData(uv);

const Atlas* atlas = _data.findAtlasAtUV(uv);
if (atlas) {
// convert back to pixels in the current mip
float mipBoundsX = std::max(1, _showSettings->imageBoundsX >> _showSettings->mipNumber);
float mipBoundsY = std::max(1, _showSettings->imageBoundsY >> _showSettings->mipNumber);

float4 rect = atlas->rect();
rect.xz *= mipBoundsX;
rect.yw *= mipBoundsY;

string atlasText;
sprintf(atlasText, "%d,%d %dx%d %s",
(int32_t)rect.x, (int32_t)rect.y,
(int32_t)rect.z, (int32_t)rect.w,
atlas->name.c_str());
_data.setAtlasText(atlasText.c_str());
}
else {
_data.setAtlasText("");
}
// This calls setNeedsDisplay on the hud section that displays the eyeDropper
[self updateHudText];
}
Expand All @@ -1107,7 +1127,7 @@ - (void)setHudText:(const char *)text
- (void)updateHudText
{
// combine textSlots
string text = _data.textFromSlots();
string text = _data.textFromSlots(_tableView.hidden);

NSString *textNS = [NSString stringWithUTF8String:text.c_str()];

Expand Down

0 comments on commit 7ed6017

Please sign in to comment.