Zooming and middle mouse scrolling implemented on the tile map canvas

Gui_Panel_Refactor
Joeyrp 4 years ago
parent 30123cb8d2
commit 24f756da75

@ -3,3 +3,6 @@ High Importance:
✔ The Map Editor does not get the tile maps when a project is opened @high @done (3/3/2022, 2:47:41 PM)
✔ Tile Set IDs (as opposed to the Asset ID) is not saved or loaded yet @high @done (3/11/2022, 2:10:30 PM)
✔ Had to flip the V component of the UVs for the sprite vertices. This fixes the partial image drawing but will need to be accounted for in other places in the editor. @done (3/14/2022, 1:46:48 PM)
Medium Importance:
☐ Map Editor does not grab tile sets if the Map Editor is opened before a project is loaded

@ -50,11 +50,19 @@ Editor:
✔ Connect Selected Tile Set to the Canvas @done (3/11/2022, 6:11:07 PM)
✔ Handle mouse clicking in update @done (3/11/2022, 6:11:09 PM)
✔ Update current Map with current Tile on mouse click @done (3/11/2022, 6:11:12 PM)
✔ Tile map pallete @done (2/24/2022, 3:15:26 PM)
☐ Hideable grid
☐ Flood Fill
✔ Zoom ability @high @done (3/14/2022, 3:38:27 PM)
✔ Middle mouse button scrolling @done (3/14/2022, 3:38:43 PM)
✔ Tile map pallete @done (2/24/2022, 3:15:26 PM)
☐ Hideable grid
☐ Stamp creater
☐ Flood Fill
Tile Set Viewer:
☐ Zoom
☐ Middle mouse scrolling
☐ Stamp creation
Properties:

@ -22,33 +22,68 @@ namespace lunarium { namespace editor
{
MapCanvas::MapCanvas(MapEditor* editor)
: Panel(gui::PanelType::PT_MAP_CANVAS, "Map Canvas", gui::PanelDockZone::DDZ_CENTER, true),
mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, -1}), mFrameBuffer(-1), mMapSizeChanged(false)
mpMapEditor(editor), mpCanvasImage(nullptr), mMap(nullptr), mSelectedTile({-1, -1}), mFrameBuffer(-1), mMapSizeChanged(false),
mZoomFactor(1.0f), mScrollDragged(false)
{
}
void MapCanvas::Update(float delta)
{
// Get mouse coords
float x = ImGui::GetMousePos().x;
float y = ImGui::GetMousePos().y;
// Adjust for window pos
x -= mWorkAreaPos.X;
y -= mWorkAreaPos.Y;
// Check that mouse pos is within the window
bool is_in_window = x >= 0.0f && x < mWorkAreaSize.X && y >= 0.0f && y < mWorkAreaSize.Y;
// Do zooming
float wheel = ImGui::GetIO().MouseWheel;
if (!(wheel < 0.1f && wheel > -0.1f) && is_in_window)
{
if (ImGui::GetIO().KeyCtrl)
{
mZoomFactor += wheel * 0.05f;
mpCanvasImage = nullptr;
}
}
// Do middle mouse scrolling
if (ImGui::IsMouseDown(ImGuiMouseButton_Middle))
{
mScrollOffset.X -= ImGui::GetIO().MouseDelta.x;
if (mScrollOffset.X < 0.0f)
{
mScrollOffset.X = 0.0f;
}
mScrollOffset.Y -= ImGui::GetIO().MouseDelta.y;
if (mScrollOffset.Y < 0.0f)
{
mScrollOffset.Y = 0.0f;
}
mScrollDragged = true;
}
else
{
mScrollDragged = false;
}
// Check for input and update map
if (mMap && ImGui::GetIO().MouseDown[ImGuiMouseButton_Left] && mSelectedTile.TileSetID > -1
&& mSelectedTile.TileIndex.X > -1&& mSelectedTile.TileIndex.Y > -1)
{
// Get mouse coords
float x = ImGui::GetMousePos().x;
float y = ImGui::GetMousePos().y;
// Adjust for window pos
x -= mWorkAreaPos.X;
y -= mWorkAreaPos.Y;
// Check that mouse pos is within the window
bool is_in_window = x >= 0.0f && x < mWorkAreaSize.X && y >= 0.0f && y < mWorkAreaSize.Y;
if (is_in_window)
{
// Convert to tile grid index
x /= mMap->GetTileSize().Width;
y /= mMap->GetTileSize().Height;
x /= (mMap->GetTileSize().Width * mZoomFactor);
y /= (mMap->GetTileSize().Height * mZoomFactor);
// Don't update the map if the current tile is already set to the selected tile
if (mMap->GetTile({ (int)x, (int)y }) != mSelectedTile)
@ -91,55 +126,63 @@ namespace lunarium { namespace editor
if (!mIsOpen)
return false;
ImGui::PushStyleVar( ImGuiStyleVar_WindowRounding, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowBorderSize, 0.0f );
ImGui::PushStyleVar( ImGuiStyleVar_WindowPadding, ImVec2( 0.0f, 0.0f ) );
if (!ImGui::Begin(GetName(), &mIsOpen))
{
ImGui::PopStyleVar(3);
ImGui::End();
return false;
}
ImGui::PopStyleVar(3);
ImVec2 pos = ImGui::GetWindowPos();
ImVec2 size = ImGui::GetWindowSize();
// Adjust for the tab bar
pos.y += ImGui::GetFrameHeight();
size.y -= ImGui::GetFrameHeight();
ImGuiIO& io = ImGui::GetIO();
float x = io.MousePos.x - pos.x;
float y = io.MousePos.y - pos.y;
// tool bar
float child_height = ImGui::GetFrameHeight() * 2;
ImGui::BeginChild("Map Tool Bar", ImVec2(ImGui::GetWindowSize().x, child_height), true);
ImGui::Text("Zoom: ");
ImGui::SameLine();
ImGui::PushItemWidth(100.0f);
ImGui::InputFloat("", &mZoomFactor, 0.05f, 0.1f, "%.2f");
ImGui::PopItemWidth();
ImGui::EndChild();
ImGui::BeginChild("Map Canvas", ImVec2(ImGui::GetWindowSize().x, ImGui::GetWindowSize().y - child_height), true);
mWorkAreaPos = { ImGui::GetWindowPos().x, ImGui::GetWindowPos().y };
mWorkAreaSize = { ImGui::GetWindowSize().x, ImGui::GetWindowSize().y };
std::ostringstream oss;
oss << "Mouse Position on Panel: ";
if (x < 0.0f || y < 0.0f || x > size.x || y > size.y)
if (mScrollDragged)
{
oss << "OUTSIDE PANEL";
ImGui::SetScrollX(mScrollOffset.X);
ImGui::SetScrollY(mScrollOffset.Y);
mScrollDragged = false;
}
else
{
oss << "(" << x << ", " << y << ")";
mScrollOffset = { ImGui::GetScrollX(), ImGui::GetScrollY() };
}
oss << "\nFrameHeight: " << ImGui::GetFrameHeight();
oss <<"\nFrameHeightWithSpacing: " << ImGui::GetFrameHeightWithSpacing();
mMouseStatusInfo = oss.str();
mWorkAreaPos = { ImGui::GetWindowPos().x, ImGui::GetWindowPos().y };
mWorkAreaSize = { ImGui::GetWindowSize().x, ImGui::GetWindowSize().y };
mScrollOffset = { ImGui::GetScrollX(), ImGui::GetScrollY() };
// Adjust pos and size to account for the title bar
mWorkAreaPos.Y += ImGui::GetFrameHeight();
mWorkAreaSize.Y -= ImGui::GetFrameHeight();
// mWorkAreaPos.Y += ImGui::GetFrameHeight();
// mWorkAreaSize.Y -= ImGui::GetFrameHeight();
// // Adjust y pos and size to account for the tool bar
// mWorkAreaPos.Y += child_height;
// mWorkAreaSize.Y -= child_height;
//ImGui::Text(mMouseStatusInfo.c_str());
if (mpCanvasImage)
{
ImGui::Image((ImTextureID)mpCanvasImage->GetGLTextureID64(),
ImVec2(mpCanvasImage->GetWidth(), mpCanvasImage->GetHeight()), ImVec2(0, 1), ImVec2(1, 0));
ImVec2(mpCanvasImage->GetWidth() * mZoomFactor, mpCanvasImage->GetHeight() * mZoomFactor), ImVec2(0, 1), ImVec2(1, 0));
}
// TODO: If a tile on the map was changed this frame null out the canvas image so it will be redrawn
ImGui::EndChild();
ImGui::End();
return mIsOpen;
}

@ -41,6 +41,8 @@ namespace lunarium { namespace editor
Vec2f mWorkAreaPos;
Vec2f mWorkAreaSize;
Vec2f mScrollOffset;
bool mScrollDragged;
float mZoomFactor;
std::string mMouseStatusInfo;
int mFrameBuffer;
Image* mpCanvasImage;

Loading…
Cancel
Save