@ -22,33 +22,68 @@ namespace lunarium { namespace editor
{
{
MapCanvas : : MapCanvas ( MapEditor * editor )
MapCanvas : : MapCanvas ( MapEditor * editor )
: Panel ( gui : : PanelType : : PT_MAP_CANVAS , " Map Canvas " , gui : : PanelDockZone : : DDZ_CENTER , true ) ,
: 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 )
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
// Check for input and update map
if ( mMap & & ImGui : : GetIO ( ) . MouseDown [ ImGuiMouseButton_Left ] & & mSelectedTile . TileSetID > - 1
if ( mMap & & ImGui : : GetIO ( ) . MouseDown [ ImGuiMouseButton_Left ] & & mSelectedTile . TileSetID > - 1
& & mSelectedTile . TileIndex . X > - 1 & & mSelectedTile . TileIndex . Y > - 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 )
if ( is_in_window )
{
{
// Convert to tile grid index
// Convert to tile grid index
x / = mMap - > GetTileSize ( ) . Width ;
x / = ( mMap - > GetTileSize ( ) . Width * mZoomFactor ) ;
y / = mMap - > GetTileSize ( ) . Height ;
y / = ( mMap - > GetTileSize ( ) . Height * mZoomFactor ) ;
// Don't update the map if the current tile is already set to the selected tile
// Don't update the map if the current tile is already set to the selected tile
if ( mMap - > GetTile ( { ( int ) x , ( int ) y } ) ! = mSelectedTile )
if ( mMap - > GetTile ( { ( int ) x , ( int ) y } ) ! = mSelectedTile )
@ -91,55 +126,63 @@ namespace lunarium { namespace editor
if ( ! mIsOpen )
if ( ! mIsOpen )
return false ;
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 ) )
if ( ! ImGui : : Begin ( GetName ( ) , & mIsOpen ) )
{
{
ImGui : : PopStyleVar ( 3 ) ;
ImGui : : End ( ) ;
ImGui : : End ( ) ;
return false ;
return false ;
}
}
ImGui : : PopStyleVar ( 3 ) ;
ImVec2 pos = ImGui : : GetWindowPos ( ) ;
ImVec2 pos = ImGui : : GetWindowPos ( ) ;
ImVec2 size = ImGui : : GetWindowSize ( ) ;
ImVec2 size = ImGui : : GetWindowSize ( ) ;
// Adjust for the tab bar
// tool bar
pos . y + = ImGui : : GetFrameHeight ( ) ;
float child_height = ImGui : : GetFrameHeight ( ) * 2 ;
size . y - = ImGui : : GetFrameHeight ( ) ;
ImGui : : BeginChild ( " Map Tool Bar " , ImVec2 ( ImGui : : GetWindowSize ( ) . x , child_height ) , true ) ;
ImGuiIO & io = ImGui : : GetIO ( ) ;
ImGui : : Text ( " Zoom: " ) ;
float x = io . MousePos . x - pos . x ;
ImGui : : SameLine ( ) ;
float y = io . MousePos . y - pos . y ;
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 ;
if ( mScrollDragged )
oss < < " Mouse Position on Panel: " ;
if ( x < 0.0f | | y < 0.0f | | x > size . x | | y > size . y )
{
{
oss < < " OUTSIDE PANEL " ;
ImGui : : SetScrollX ( mScrollOffset . X ) ;
ImGui : : SetScrollY ( mScrollOffset . Y ) ;
mScrollDragged = false ;
}
}
else
else
{
{
oss < < " ( " < < x < < " , " < < y < < " ) " ;
mScrollOffset = { ImGui : : GetScrollX ( ) , ImGui : : GetScrollY ( ) } ;
}
}
oss < < " \n FrameHeight: " < < ImGui : : GetFrameHeight ( ) ;
oss < < " \n FrameHeightWithSpacing: " < < 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
// Adjust pos and size to account for the title bar
mWorkAreaPos . Y + = ImGui : : GetFrameHeight ( ) ;
// mWorkAreaPos.Y += ImGui::GetFrameHeight();
mWorkAreaSize . 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());
//ImGui::Text(mMouseStatusInfo.c_str());
if ( mpCanvasImage )
if ( mpCanvasImage )
{
{
ImGui : : Image ( ( ImTextureID ) mpCanvasImage - > GetGLTextureID64 ( ) ,
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 ( ) ;
ImGui : : End ( ) ;
return mIsOpen ;
return mIsOpen ;
}
}