Skip to content

Commit a455eaa

Browse files
committed
Change view offset to a per section value
1 parent bfc9b40 commit a455eaa

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/C4Script.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5833,8 +5833,7 @@ static bool FnSetViewOffset(C4AulContext *ctx, C4ValueInt iPlayer, C4ValueInt iX
58335833
C4Viewport *pView = Game.GraphicsSystem.GetViewport(iPlayer);
58345834
if (!pView) return true; // sync safety
58355835
// set
5836-
pView->ViewOffsX = iX;
5837-
pView->ViewOffsY = iY;
5836+
pView->SetViewOffset(ctx->GetSection(), iX, iY);
58385837
// ok
58395838
return true;
58405839
}

src/C4Viewport.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ void C4Viewport::Clear()
835835
DrawX = DrawY = 0;
836836
Regions.Clear();
837837
dViewX = dViewY = -31337;
838-
ViewOffsX = ViewOffsY = 0;
838+
ViewOffsets.clear();
839839
previousViewRootSection = nullptr;
840840
}
841841

@@ -1247,8 +1247,15 @@ void C4Viewport::AdjustPosition()
12471247
// calc target view position
12481248
int32_t iTargetViewX = pPlr->ViewX - ViewWdt / 2;
12491249
int32_t iTargetViewY = pPlr->ViewY - ViewHgt / 2;
1250+
// get view offset
1251+
std::int32_t viewOffsetX{0};
1252+
std::int32_t viewOffsetY{0};
1253+
if (const auto it = ViewOffsets.find(&viewRootSection); it != ViewOffsets.end())
1254+
{
1255+
std::tie(viewOffsetX, viewOffsetY) = it->second;
1256+
}
12501257
// add mouse auto scroll
1251-
int32_t iPrefViewX = ViewX - ViewOffsX, iPrefViewY = ViewY - ViewOffsY;
1258+
int32_t iPrefViewX = ViewX - viewOffsetX, iPrefViewY = ViewY - viewOffsetY;
12521259
if (pPlr->MouseControl && Game.MouseControl.InitCentered && Config.General.MouseAScroll)
12531260
{
12541261
int32_t iAutoScrollBorder = (std::min)((std::min)(ViewWdt / 10, ViewHgt / 10), C4SymbolSize);
@@ -1298,12 +1305,12 @@ void C4Viewport::AdjustPosition()
12981305
dViewY = itofix(ViewY);
12991306
}
13001307
// apply offset
1301-
ViewX += ViewOffsX; ViewY += ViewOffsY;
1308+
ViewX += viewOffsetX; ViewY += viewOffsetY;
13021309
// store previous section
13031310
previousViewRootSection = &viewRootSection;
13041311
}
13051312
// NO_OWNER can't scroll
1306-
if (fIsNoOwnerViewport) { ViewOffsX = 0; ViewOffsY = 0; }
1313+
if (fIsNoOwnerViewport) { ViewOffsets.clear(); }
13071314
// clip at borders, update vars
13081315
UpdateViewPosition();
13091316
}
@@ -1339,6 +1346,23 @@ C4Section &C4Viewport::GetViewRootSection()
13391346
return *Game.Sections.front();
13401347
}
13411348

1349+
void C4Viewport::SetViewOffset(C4Section &section, const std::int32_t x, const std::int32_t y)
1350+
{
1351+
if (fIsNoOwnerViewport)
1352+
{
1353+
return;
1354+
}
1355+
1356+
if (x == 0 && y == 0)
1357+
{
1358+
ViewOffsets.erase(&section.GetRootSection());
1359+
}
1360+
else
1361+
{
1362+
ViewOffsets.insert_or_assign(&section.GetRootSection(), std::pair{x, y});
1363+
}
1364+
}
1365+
13421366
void C4Viewport::UpdateViewPosition()
13431367
{
13441368
C4Section &section{GetViewRootSection()};
@@ -1383,7 +1407,7 @@ void C4Viewport::Default()
13831407
SetRegions = nullptr;
13841408
Regions.Default();
13851409
dViewX = dViewY = -31337;
1386-
ViewOffsX = ViewOffsY = 0;
1410+
ViewOffsets.clear();
13871411
fIsNoOwnerViewport = false;
13881412
}
13891413

@@ -1626,6 +1650,8 @@ void C4Viewport::ClearSectionPointers(C4Section &section)
16261650
{
16271651
previousViewRootSection = nullptr;
16281652
}
1653+
1654+
ViewOffsets.erase(&section);
16291655
}
16301656

16311657
void C4Viewport::DrawMouseButtons(C4FacetEx &cgo)

src/C4Viewport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class C4Viewport
9393
C4Fixed dViewX, dViewY;
9494
int32_t ViewX, ViewY, ViewWdt, ViewHgt;
9595
int32_t BorderLeft, BorderTop, BorderRight, BorderBottom;
96-
int32_t ViewOffsX, ViewOffsY;
96+
std::unordered_map<C4Section *, std::pair<std::int32_t, std::int32_t>> ViewOffsets;
9797
int32_t DrawX, DrawY;
9898
bool fIsNoOwnerViewport; // this viewport is found for searches of NO_OWNER-viewports; even if it has a player assigned (for network obs)
9999
void Default();
@@ -116,6 +116,7 @@ class C4Viewport
116116
void CenterPosition();
117117
C4Section &GetViewSection();
118118
C4Section &GetViewRootSection();
119+
void SetViewOffset(C4Section &section, std::int32_t x, std::int32_t y);
119120

120121
auto MapPointToChildSectionPoint(const std::int32_t x, const std::int32_t y)
121122
{

0 commit comments

Comments
 (0)