Skip to content

Commit 9377ed3

Browse files
committed
Make C4EditCursor and C4ToolsDlg work with sections
1 parent 8037a54 commit 9377ed3

File tree

8 files changed

+129
-50
lines changed

8 files changed

+129
-50
lines changed

src/C4Console.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,12 @@ void C4Console::ClearPointers(C4Object *pObj)
11501150
PropertyDlg.ClearPointers(pObj);
11511151
}
11521152

1153+
void C4Console::ClearSectionPointers(C4Section &section)
1154+
{
1155+
EditCursor.ClearSectionPointers(section);
1156+
ToolsDlg.ClearSectionPointers(section);
1157+
}
1158+
11531159
void C4Console::Default()
11541160
{
11551161
EditCursor.Default();

src/C4Console.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class C4Console : public C4ConsoleBase
6565
bool Init(CStdApp *app, const char *title, const class C4Rect &bounds = CStdWindow::DefaultBounds, CStdWindow *parent = nullptr) override;
6666
void Execute();
6767
void ClearPointers(C4Object *pObj);
68+
void ClearSectionPointers(C4Section &section);
6869
bool Message(const char *szMessage, bool fQuery = false);
6970
void SetCaption(const char *szCaption);
7071
bool In(const char *szText);

src/C4EditCursor.cpp

Lines changed: 76 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ void C4EditCursor::Execute()
6262
switch (Console.ToolsDlg.Tool)
6363
{
6464
case C4TLS_Fill:
65-
//if (Hold) if (!Game.HaltCount) if (Console.Editing) ApplyToolFill();
65+
if (Hold) if (!Game.HaltCount) if (Console.Editing) ApplyToolFill();
6666
break;
6767
}
6868
break;
@@ -117,8 +117,19 @@ void C4EditCursor::ClearPointers(C4Object *pObj)
117117
OnSelectionChanged();
118118
}
119119

120+
void C4EditCursor::ClearSectionPointers(C4Section &section)
121+
{
122+
if (CurrentSection == &section)
123+
{
124+
CurrentSection = nullptr;
125+
ResetSectionDependentState();
126+
}
127+
}
128+
120129
bool C4EditCursor::Move(C4Section &section, int32_t iX, int32_t iY, uint16_t wKeyFlags)
121130
{
131+
UpdateCurrentSection(section);
132+
122133
// Offset movement
123134
int32_t xoff = iX - X; int32_t yoff = iY - Y; X = iX; Y = iY;
124135

@@ -129,7 +140,7 @@ bool C4EditCursor::Move(C4Section &section, int32_t iX, int32_t iY, uint16_t wKe
129140
if (!DragFrame && Hold)
130141
{
131142
MoveSelection(xoff, yoff);
132-
UpdateDropTarget(section, wKeyFlags);
143+
UpdateDropTarget(wKeyFlags);
133144
}
134145
// Update target
135146
// Shift always indicates a target outside the current selection
@@ -147,7 +158,7 @@ bool C4EditCursor::Move(C4Section &section, int32_t iX, int32_t iY, uint16_t wKe
147158
switch (Console.ToolsDlg.Tool)
148159
{
149160
case C4TLS_Brush:
150-
if (Hold) ApplyToolBrush(section);
161+
if (Hold) ApplyToolBrush();
151162
break;
152163
case C4TLS_Line: case C4TLS_Rect:
153164
break;
@@ -178,8 +189,7 @@ bool C4EditCursor::UpdateStatusBar()
178189
break;
179190

180191
case C4CNS_ModeDraw:
181-
// text = std::format("{}/{} ({})", X, Y, (Section->MatValid(Section->Landscape.GetMat(X, Y)) ? Section->Material.Map[Section->Landscape.GetMat(X, Y)].Name : LoadResStr(C4ResStrTableKey::IDS_CNS_NOTHING)));
182-
text = "FIXME: C4CNS_ModeDraw"; // FIXME
192+
text = std::format("{}/{} ({})", X, Y, (CurrentSection->MatValid(CurrentSection->Landscape.GetMat(X, Y)) ? CurrentSection->Material.Map[CurrentSection->Landscape.GetMat(X, Y)].Name : LoadResStr(C4ResStrTableKey::IDS_CNS_NOTHING)));
183193
break;
184194
}
185195
return Console.UpdateCursorBar(text.c_str());
@@ -192,6 +202,8 @@ void C4EditCursor::OnSelectionChanged()
192202

193203
bool C4EditCursor::LeftButtonDown(C4Section &section, bool fControl)
194204
{
205+
UpdateCurrentSection(section);
206+
195207
// Hold
196208
Hold = true;
197209

@@ -223,7 +235,7 @@ bool C4EditCursor::LeftButtonDown(C4Section &section, bool fControl)
223235
case C4CNS_ModeDraw:
224236
switch (Console.ToolsDlg.Tool)
225237
{
226-
case C4TLS_Brush: ApplyToolBrush(section); break;
238+
case C4TLS_Brush: ApplyToolBrush(); break;
227239
case C4TLS_Line: DragLine = true; X2 = X; Y2 = Y; break;
228240
case C4TLS_Rect: DragFrame = true; X2 = X; Y2 = Y; break;
229241
case C4TLS_Fill:
@@ -232,7 +244,7 @@ bool C4EditCursor::LeftButtonDown(C4Section &section, bool fControl)
232244
Hold = false; Console.Message(LoadResStr(C4ResStrTableKey::IDS_CNS_FILLNOHALT)); return false;
233245
}
234246
break;
235-
case C4TLS_Picker: ApplyToolPicker(section); break;
247+
case C4TLS_Picker: ApplyToolPicker(); break;
236248
}
237249
break;
238250
}
@@ -243,8 +255,10 @@ bool C4EditCursor::LeftButtonDown(C4Section &section, bool fControl)
243255
return true;
244256
}
245257

246-
bool C4EditCursor::RightButtonDown(bool fControl)
258+
bool C4EditCursor::RightButtonDown(C4Section &section, bool fControl)
247259
{
260+
UpdateCurrentSection(section);
261+
248262
switch (Mode)
249263
{
250264
case C4CNS_ModeEdit:
@@ -278,22 +292,24 @@ bool C4EditCursor::RightButtonDown(bool fControl)
278292

279293
bool C4EditCursor::LeftButtonUp(C4Section &section)
280294
{
295+
UpdateCurrentSection(section);
296+
281297
// Finish edit/tool
282298
switch (Mode)
283299
{
284300
case C4CNS_ModeEdit:
285-
if (DragFrame) FrameSelection(section);
301+
if (DragFrame) FrameSelection();
286302
if (DropTarget) PutContents();
287303
break;
288304

289305
case C4CNS_ModeDraw:
290306
switch (Console.ToolsDlg.Tool)
291307
{
292308
case C4TLS_Line:
293-
if (DragLine) ApplyToolLine(section);
309+
if (DragLine) ApplyToolLine();
294310
break;
295311
case C4TLS_Rect:
296-
if (DragFrame) ApplyToolRect(section);
312+
if (DragFrame) ApplyToolRect();
297313
break;
298314
}
299315
break;
@@ -330,8 +346,9 @@ bool SetMenuItemText(HMENU hMenu, WORD id, const char *szText)
330346

331347
#endif
332348

333-
bool C4EditCursor::RightButtonUp()
349+
bool C4EditCursor::RightButtonUp(C4Section &section)
334350
{
351+
UpdateCurrentSection(section);
335352
Target = nullptr;
336353

337354
DoContextMenu();
@@ -343,9 +360,10 @@ bool C4EditCursor::RightButtonUp()
343360

344361
void C4EditCursor::MiddleButtonUp(C4Section &section)
345362
{
363+
UpdateCurrentSection(section);
346364
if (Hold) return;
347365

348-
ApplyToolPicker(section);
366+
ApplyToolPicker();
349367
}
350368

351369
bool C4EditCursor::Delete()
@@ -368,7 +386,7 @@ bool C4EditCursor::OpenPropTools()
368386
Console.PropertyDlg.Update(Selection);
369387
break;
370388
case C4CNS_ModeDraw:
371-
Console.ToolsDlg.Open();
389+
Console.ToolsDlg.Open(*CurrentSection);
372390
break;
373391
}
374392
return true;
@@ -448,11 +466,11 @@ void C4EditCursor::MoveSelection(int32_t iXOff, int32_t iYOff)
448466
EMMoveObject(EMMO_Move, iXOff, iYOff, nullptr, Selection);
449467
}
450468

451-
void C4EditCursor::FrameSelection(C4Section &section)
469+
void C4EditCursor::FrameSelection()
452470
{
453471
Selection.Clear();
454472
C4Object *cobj; C4ObjectLink *clnk;
455-
for (clnk = section.Objects.First; clnk && (cobj = clnk->Obj); clnk = clnk->Next)
473+
for (clnk = CurrentSection->Objects.First; clnk && (cobj = clnk->Obj); clnk = clnk->Next)
456474
if (cobj->Status) if (cobj->OCF & OCF_NotContained)
457475
{
458476
if (Inside(cobj->x, (std::min)(X, X2), (std::max)(X, X2)) && Inside(cobj->y, (std::min)(Y, Y2), (std::max)(Y, Y2)))
@@ -548,36 +566,36 @@ bool C4EditCursor::ToggleMode()
548566
return true;
549567
}
550568

551-
void C4EditCursor::ApplyToolBrush(C4Section &section)
569+
void C4EditCursor::ApplyToolBrush()
552570
{
553571
if (!EditingOK()) return;
554572
C4ToolsDlg *pTools = &Console.ToolsDlg;
555573
// execute/send control
556-
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Brush, section.Landscape.Mode, X, Y, 0, 0, pTools->Grade, !!pTools->ModeIFT, section.Number, pTools->Material, pTools->Texture));
574+
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Brush, CurrentSection->Landscape.Mode, X, Y, 0, 0, pTools->Grade, !!pTools->ModeIFT, CurrentSection->Number, pTools->Material, pTools->Texture));
557575
}
558576

559-
void C4EditCursor::ApplyToolLine(C4Section &section)
577+
void C4EditCursor::ApplyToolLine()
560578
{
561579
if (!EditingOK()) return;
562580
C4ToolsDlg *pTools = &Console.ToolsDlg;
563581
// execute/send control
564-
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Line, section.Landscape.Mode, X, Y, X2, Y2, pTools->Grade, !!pTools->ModeIFT, section.Number, pTools->Material, pTools->Texture));
582+
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Line, CurrentSection->Landscape.Mode, X, Y, X2, Y2, pTools->Grade, !!pTools->ModeIFT, CurrentSection->Number, pTools->Material, pTools->Texture));
565583
}
566584

567-
void C4EditCursor::ApplyToolRect(C4Section &section)
585+
void C4EditCursor::ApplyToolRect()
568586
{
569587
if (!EditingOK()) return;
570588
C4ToolsDlg *pTools = &Console.ToolsDlg;
571589
// execute/send control
572-
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Rect, section.Landscape.Mode, X, Y, X2, Y2, pTools->Grade, !!pTools->ModeIFT, section.Number, pTools->Material, pTools->Texture));
590+
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Rect, CurrentSection->Landscape.Mode, X, Y, X2, Y2, pTools->Grade, !!pTools->ModeIFT, CurrentSection->Number, pTools->Material, pTools->Texture));
573591
}
574592

575-
void C4EditCursor::ApplyToolFill(C4Section &section)
593+
void C4EditCursor::ApplyToolFill()
576594
{
577595
if (!EditingOK()) return;
578596
C4ToolsDlg *pTools = &Console.ToolsDlg;
579597
// execute/send control
580-
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Fill, section.Landscape.Mode, X, Y, 0, Y2, pTools->Grade, false, section.Number, pTools->Material));
598+
EMControl(CID_EMDrawTool, new C4ControlEMDrawTool(EMDT_Fill, CurrentSection->Landscape.Mode, X, Y, 0, Y2, pTools->Grade, false, CurrentSection->Number, pTools->Material));
581599
}
582600

583601
bool C4EditCursor::DoContextMenu()
@@ -634,15 +652,15 @@ void C4EditCursor::GrabContents()
634652
EMMoveObject(EMMO_Exit, 0, 0, nullptr, Selection);
635653
}
636654

637-
void C4EditCursor::UpdateDropTarget(C4Section &section, uint16_t wKeyFlags)
655+
void C4EditCursor::UpdateDropTarget(uint16_t wKeyFlags)
638656
{
639657
C4Object *cobj; C4ObjectLink *clnk;
640658

641659
DropTarget = nullptr;
642660

643661
if (wKeyFlags & MK_CONTROL)
644662
if (Selection.GetObject())
645-
for (clnk = section.Objects.First; clnk && (cobj = clnk->Obj); clnk = clnk->Next)
663+
for (clnk = CurrentSection->Objects.First; clnk && (cobj = clnk->Obj); clnk = clnk->Next)
646664
if (cobj->Status)
647665
if (!cobj->Contained)
648666
if (Inside<int32_t>(X - (cobj->x + cobj->Shape.x), 0, cobj->Shape.Wdt - 1))
@@ -680,17 +698,17 @@ int32_t C4EditCursor::GetMode()
680698
return Mode;
681699
}
682700

683-
void C4EditCursor::ApplyToolPicker(C4Section &section)
701+
void C4EditCursor::ApplyToolPicker()
684702
{
685703
int32_t iMaterial;
686704
uint8_t byIndex;
687-
switch (section.Landscape.Mode)
705+
switch (CurrentSection->Landscape.Mode)
688706
{
689707
case C4LSC_Static:
690708
// Material-texture from map
691-
if (byIndex = section.Landscape.GetMapIndex(X / section.Landscape.MapZoom, Y / section.Landscape.MapZoom))
709+
if (byIndex = CurrentSection->Landscape.GetMapIndex(X / CurrentSection->Landscape.MapZoom, Y / CurrentSection->Landscape.MapZoom))
692710
{
693-
const C4TexMapEntry *pTex = section.TextureMap.GetEntry(byIndex & (IFT - 1));
711+
const C4TexMapEntry *pTex = CurrentSection->TextureMap.GetEntry(byIndex & (IFT - 1));
694712
if (pTex)
695713
{
696714
Console.ToolsDlg.SelectMaterial(pTex->GetMaterialName());
@@ -703,10 +721,10 @@ void C4EditCursor::ApplyToolPicker(C4Section &section)
703721
break;
704722
case C4LSC_Exact:
705723
// Material only from landscape
706-
if (section.MatValid(iMaterial = section.Landscape.GetMat(X, Y)))
724+
if (CurrentSection->MatValid(iMaterial = CurrentSection->Landscape.GetMat(X, Y)))
707725
{
708-
Console.ToolsDlg.SelectMaterial(section.Material.Map[iMaterial].Name);
709-
Console.ToolsDlg.SetIFT(section.Landscape.GBackIFT(X, Y));
726+
Console.ToolsDlg.SelectMaterial(CurrentSection->Material.Map[iMaterial].Name);
727+
Console.ToolsDlg.SetIFT(CurrentSection->Landscape.GBackIFT(X, Y));
710728
}
711729
else
712730
Console.ToolsDlg.SelectMaterial(C4TLS_MatSky);
@@ -730,14 +748,38 @@ void C4EditCursor::EMMoveObject(C4ControlEMObjectAction eAction, int32_t tx, int
730748
}
731749

732750
// execute control
733-
EMControl(CID_EMMoveObj, new C4ControlEMMoveObject(eAction, tx, ty, pTargetObj->Section->Number, pTargetObj, iObjCnt, pObjIDs, szScript, Config.Developer.ConsoleScriptStrictness));
751+
EMControl(CID_EMMoveObj, new C4ControlEMMoveObject(eAction, tx, ty, CurrentSection->Number, pTargetObj, iObjCnt, pObjIDs, szScript, Config.Developer.ConsoleScriptStrictness));
734752
}
735753

736754
void C4EditCursor::EMControl(C4PacketType eCtrlType, C4ControlPacket *pCtrl)
737755
{
738756
Game.Control.DoInput(eCtrlType, pCtrl, CDT_Decide);
739757
}
740758

759+
void C4EditCursor::UpdateCurrentSection(C4Section &section)
760+
{
761+
if (CurrentSection != &section)
762+
{
763+
CurrentSection = &section;
764+
ResetSectionDependentState();
765+
}
766+
}
767+
768+
void C4EditCursor::ResetSectionDependentState()
769+
{
770+
Target = nullptr;
771+
DropTarget = nullptr;
772+
Selection.Clear();
773+
DragFrame = false;
774+
DragLine = false;
775+
fSelectionChanged = true;
776+
777+
if (CurrentSection && Mode == C4CNS_ModeDraw)
778+
{
779+
Console.ToolsDlg.SetSection(*CurrentSection);
780+
}
781+
}
782+
741783
#ifdef WITH_DEVELOPER_MODE
742784

743785
// GTK+ callbacks

src/C4EditCursor.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class C4EditCursor
3838
int32_t X, Y, X2, Y2;
3939
bool Hold, DragFrame, DragLine;
4040
C4Object *Target, *DropTarget;
41+
C4Section *CurrentSection{nullptr};
4142
#ifdef _WIN32
4243
HMENU hMenu;
4344
#elif defined(WITH_DEVELOPER_MODE)
@@ -55,6 +56,7 @@ class C4EditCursor
5556
void Clear();
5657
void Execute();
5758
void ClearPointers(C4Object *pObj);
59+
void ClearSectionPointers(C4Section &section);
5860
bool ToggleMode();
5961
void Draw(C4FacetEx &cgo);
6062
int32_t GetMode();
@@ -66,8 +68,8 @@ class C4EditCursor
6668
bool Delete();
6769
bool LeftButtonUp(C4Section &section);
6870
bool LeftButtonDown(C4Section &section, bool fControl);
69-
bool RightButtonUp();
70-
bool RightButtonDown(bool fControl);
71+
bool RightButtonUp(C4Section &section);
72+
bool RightButtonDown(C4Section &section, bool fControl);
7173
void MiddleButtonUp(C4Section &section);
7274
bool Move(C4Section &section, int32_t iX, int32_t iY, uint16_t wKeyFlags);
7375
bool Init();
@@ -80,20 +82,22 @@ class C4EditCursor
8082

8183
protected:
8284
bool UpdateStatusBar();
83-
void ApplyToolPicker(C4Section &section);
85+
void ApplyToolPicker();
8486
void PutContents();
85-
void UpdateDropTarget(C4Section &section, uint16_t wKeyFlags);
87+
void UpdateDropTarget(uint16_t wKeyFlags);
8688
void GrabContents();
8789
bool DoContextMenu();
88-
void ApplyToolFill(C4Section &section);
89-
void ApplyToolRect(C4Section &section);
90-
void ApplyToolLine(C4Section &section);
91-
void ApplyToolBrush(C4Section &section);
90+
void ApplyToolFill();
91+
void ApplyToolRect();
92+
void ApplyToolLine();
93+
void ApplyToolBrush();
9294
void DrawSelectMark(C4Facet &cgo);
93-
void FrameSelection(C4Section &section);
95+
void FrameSelection();
9496
void MoveSelection(int32_t iXOff, int32_t iYOff);
9597
void EMMoveObject(enum C4ControlEMObjectAction eAction, int32_t tx, int32_t ty, C4Object *pTargetObj, const C4ObjectList &objects, const char *szScript = nullptr);
9698
void EMControl(enum C4PacketType eCtrlType, class C4ControlPacket *pCtrl);
99+
void UpdateCurrentSection(C4Section &section);
100+
void ResetSectionDependentState();
97101

98102
#ifdef WITH_DEVELOPER_MODE
99103
static void OnDelete(GtkWidget *widget, gpointer data);

src/C4Game.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ bool C4Game::RemoveSection(uint32_t number)
11221122
}
11231123

11241124
GraphicsSystem.ClearSectionPointers(*section);
1125+
Console.ClearSectionPointers(*section);
11251126

11261127
return true;
11271128
}

0 commit comments

Comments
 (0)