Skip to content

Commit

Permalink
Draws position indicator, some code cleanup.
Browse files Browse the repository at this point in the history
Draws position indicator for 3 seconds, when requested by some window like Go To Position.
  • Loading branch information
Mignari committed Jan 23, 2023
1 parent 099fa55 commit 25b5482
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 42 deletions.
4 changes: 2 additions & 2 deletions source/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,11 +1223,11 @@ void GUI::UpdateMenubar()
root->UpdateMenubar();
}

void GUI::SetScreenCenterPosition(Position position)
void GUI::SetScreenCenterPosition(const Position& position, bool showIndicator)
{
MapTab* mapTab = GetCurrentMapTab();
if(mapTab)
mapTab->SetScreenCenterPosition(position);
mapTab->SetScreenCenterPosition(position, showIndicator);
}

void GUI::DoCut()
Expand Down
2 changes: 1 addition & 1 deletion source/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class GUI
bool IsVersionLoaded() const {return loaded_version != CLIENT_VERSION_NONE;}

// Centers current view on position
void SetScreenCenterPosition(Position pos);
void SetScreenCenterPosition(const Position& position, bool showIndicator = true);
// Refresh the view canvas
void RefreshView();
// Fit all/specified current map view to map dimensions
Expand Down
6 changes: 6 additions & 0 deletions source/map_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ void MapCanvas::OnPaint(wxPaintEvent& event)
editor.SendNodeRequests();
}

void MapCanvas::ShowPositionIndicator(const Position& position)
{
if (drawer)
drawer->ShowPositionIndicator(position);
}

void MapCanvas::TakeScreenshot(wxFileName path, wxString format)
{
int screensize_x, screensize_y;
Expand Down
1 change: 1 addition & 0 deletions source/map_display.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class MapCanvas : public wxGLCanvas {

Position GetCursorPosition() const;

void ShowPositionIndicator(const Position& position);
void TakeScreenshot(wxFileName path, wxString format);

protected:
Expand Down
99 changes: 71 additions & 28 deletions source/map_drawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ void MapDrawer::DrawMap()
}
}
}

DrawPositionIndicator(map_z);
}

if(only_colors)
Expand Down Expand Up @@ -1396,19 +1398,14 @@ void MapDrawer::DrawTile(TileLocation* location)
{
if(!location)
return;
Tile* tile = location->get();

Tile* tile = location->get();
if(!tile)
return;

if(options.show_only_modified && !tile->isModified())
return;

int map_x = location->getX();
int map_y = location->getY();
int map_z = location->getZ();


if(options.show_tooltips && location->getWaypointCount() > 0) {
Waypoint* waypoint = canvas->editor.map.waypoints.getWaypoint(location);
if(waypoint)
Expand All @@ -1418,14 +1415,9 @@ void MapDrawer::DrawTile(TileLocation* location)
bool as_minimap = options.show_as_minimap;
bool only_colors = as_minimap || options.show_only_colors;

int offset;
if (map_z <= GROUND_LAYER)
offset = (GROUND_LAYER - map_z) * TILE_SIZE;
else
offset = TILE_SIZE * (floor - map_z);

int draw_x = ((map_x * TILE_SIZE) - view_scroll_x) - offset;
int draw_y = ((map_y * TILE_SIZE) - view_scroll_y) - offset;
const Position& position = location->getPosition();
int draw_x, draw_y;
getDrawPosition(position, draw_x, draw_y);

uint8_t r = 255,g = 255,b = 255;
if(tile->ground || only_colors) {
Expand Down Expand Up @@ -1501,14 +1493,14 @@ void MapDrawer::DrawTile(TileLocation* location)
BlitItem(draw_x, draw_y, tile, tile->ground, false, r, g, b);
}

if(options.show_tooltips && map_z == floor)
if(options.show_tooltips && position.z == floor)
WriteTooltip(tile->ground, tooltip);
}

if(!only_colors) {
if(zoom < 10.0 || !options.hide_items_when_zoomed) {
for(ItemVector::iterator it = tile->items.begin(); it != tile->items.end(); it++) {
if(options.show_tooltips && map_z == floor)
if(options.show_tooltips && position.z == floor)
WriteTooltip(*it, tooltip);

if(options.show_preview && zoom <= 2.0)
Expand Down Expand Up @@ -1618,18 +1610,8 @@ void MapDrawer::DrawTileIndicators(TileLocation* location)
if(!tile)
return;

int map_x = location->getX();
int map_y = location->getY();
int map_z = location->getZ();

int offset;
if (map_z <= GROUND_LAYER)
offset = (GROUND_LAYER - map_z) * TILE_SIZE;
else
offset = TILE_SIZE * (floor - map_z);

int x = ((map_x * TILE_SIZE) - view_scroll_x) - offset;
int y = ((map_y * TILE_SIZE) - view_scroll_y) - offset;
int x, y;
getDrawPosition(location->getPosition(), x, y);

if(zoom < 10.0 && (options.show_pickupables || options.show_moveables)) {
uint8_t red = 0xFF, green = 0xFF, blue = 0xFF;
Expand Down Expand Up @@ -1678,6 +1660,36 @@ void MapDrawer::DrawIndicator(int x, int y, int indicator, uint8_t r, uint8_t g,
glBlitTexture(x, y, textureId, r, g, b, a, true);
}

void MapDrawer::DrawPositionIndicator(int z)
{
if (z != pos_indicator.z
|| pos_indicator.x < start_x
|| pos_indicator.x > end_x
|| pos_indicator.y < start_y
|| pos_indicator.y > end_y) {
return;
}

constexpr int duration = 3000;
const long time = pos_indicator_timer.Time();
if (time >= duration)
return;

int x, y;
getDrawPosition(pos_indicator, x, y);

int size = static_cast<int>(TILE_SIZE * (0.3f + std::abs(500 - time % 1000) / 1000.f));
int offset = (TILE_SIZE - size) / 2;

glDisable(GL_TEXTURE_2D);
drawRect(x + offset + 2, y + offset + 2, size - 4, size - 4, *wxWHITE, 2);
drawRect(x + offset + 1, y + offset + 1, size - 2, size - 2, *wxBLACK, 2);
glEnable(GL_TEXTURE_2D);

if (time >= duration)
pos_indicator_timer.Pause();
}

void MapDrawer::DrawTooltips()
{
for(std::vector<MapTooltip*>::const_iterator it = tooltips.begin(); it != tooltips.end(); ++it) {
Expand Down Expand Up @@ -1815,6 +1827,12 @@ void MapDrawer::TakeScreenshot(uint8_t* screenshot_buffer)
glReadPixels(0, screensize_y - i, screensize_x, 1, GL_RGB, GL_UNSIGNED_BYTE, (GLubyte*)(screenshot_buffer) + 3*screensize_x*i);
}

void MapDrawer::ShowPositionIndicator(const Position& position)
{
pos_indicator = position;
pos_indicator_timer.Start();
}

void MapDrawer::glBlitTexture(int x, int y, int textureId, int red, int green, int blue, int alpha, bool adjustZoom)
{
if (textureId <= 0)
Expand Down Expand Up @@ -1918,3 +1936,28 @@ void MapDrawer::glColorCheck(Brush* brush, const Position& pos)
else
glColor(COLOR_INVALID);
}

void MapDrawer::drawRect(int x, int y, int w, int h, const wxColour& color, int width)
{
glLineWidth(width);
glColor4ub(color.Red(), color.Green(), color.Blue(), color.Alpha());
glBegin(GL_LINE_LOOP);
glVertex2f(x, y);
glVertex2f(x + w, y);
glVertex2f(x + w, y + h);
glVertex2f(x, y + h);
glVertex2f(x, y);
glEnd();
}

void MapDrawer::getDrawPosition(const Position& position, int& x, int& y)
{
int offset;
if (position.z <= GROUND_LAYER)
offset = (GROUND_LAYER - position.z) * TILE_SIZE;
else
offset = TILE_SIZE * (floor - position.z);

x = ((position.x * TILE_SIZE) - view_scroll_x) - offset;
y = ((position.y * TILE_SIZE) - view_scroll_y) - offset;
}
12 changes: 10 additions & 2 deletions source/map_drawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ class MapDrawer
std::vector<MapTooltip*> tooltips;
std::ostringstream tooltip;

wxStopWatch pos_indicator_timer;
Position pos_indicator;

public:
MapDrawer(MapCanvas* canvas);
~MapDrawer();
Expand All @@ -127,6 +130,8 @@ class MapDrawer

void TakeScreenshot(uint8_t* screenshot_buffer);

void ShowPositionIndicator(const Position& position);

DrawingOptions& getOptions() { return options; }

protected:
Expand All @@ -141,6 +146,7 @@ class MapDrawer
void DrawHookIndicator(int x, int y, const ItemType& type);
void DrawTileIndicators(TileLocation* location);
void DrawIndicator(int x, int y, int indicator, uint8_t r = 255, uint8_t g = 255, uint8_t b = 255, uint8_t a = 255);
void DrawPositionIndicator(int z);
void WriteTooltip(Item* item, std::ostringstream& stream);
void WriteTooltip(Waypoint* item, std::ostringstream& stream);
void MakeTooltip(int screenx, int screeny, const std::string& text, uint8_t r = 255, uint8_t g = 255, uint8_t b = 255);
Expand All @@ -162,8 +168,10 @@ class MapDrawer
void glColor(wxColor color);
void glColor(BrushColor color);
void glColorCheck(Brush* brush, const Position& pos);
};
void drawRect(int x, int y, int w, int h, const wxColour& color, int width = 1);

private:
void getDrawPosition(const Position& position, int &x, int &y);
};

#endif

9 changes: 4 additions & 5 deletions source/map_region.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,10 @@ class TileLocation
int size() const;
bool empty() const;

Position getPosition() const { return position; }

int getX() const {return position.x;}
int getY() const {return position.y;}
int getZ() const {return position.z;}
const Position& getPosition() const noexcept { return position; }
int getX() const noexcept { return position.x; }
int getY() const noexcept { return position.y; }
int getZ() const noexcept { return position.z; }

size_t getSpawnCount() const {return spawn_count;}
void increaseSpawnCount() {spawn_count++;}
Expand Down
7 changes: 5 additions & 2 deletions source/map_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Position MapWindow::GetScreenCenterPosition()
return Position(x, y, canvas->GetFloor());
}

void MapWindow::SetScreenCenterPosition(const Position& position)
void MapWindow::SetScreenCenterPosition(const Position& position, bool showIndicator)
{
if(position == Position())
return;
Expand All @@ -156,11 +156,14 @@ void MapWindow::SetScreenCenterPosition(const Position& position)

Scroll(x, y, true);
canvas->ChangeFloor(position.z);

if (showIndicator)
canvas->ShowPositionIndicator(position);
}

void MapWindow::GoToPreviousCenterPosition()
{
SetScreenCenterPosition(previous_position);
SetScreenCenterPosition(previous_position, true);
}

void MapWindow::Scroll(int x, int y, bool center)
Expand Down
2 changes: 1 addition & 1 deletion source/map_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MapWindow : public wxPanel

// Screen position.
Position GetScreenCenterPosition();
void SetScreenCenterPosition(const Position& position);
void SetScreenCenterPosition(const Position& position, bool showIndicator = false);
void GoToPreviousCenterPosition();

// Return the containing canvas
Expand Down
2 changes: 1 addition & 1 deletion source/position.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class Position {
return *this;
}

bool operator==(const Position& p) const {
bool operator==(const Position& p) const noexcept {
return p.x == x && p.y == y && p.z == z;
}

Expand Down

0 comments on commit 25b5482

Please sign in to comment.