| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#include <stdio.h> |
| 50 |
#include <stdlib.h> |
| 51 |
#include <string.h> |
| 52 |
|
| 53 |
#define SDL_MAIN_HANDLED |
| 54 |
#include <SDL.h> |
| 55 |
|
| 56 |
#include <cdogs/XGetopt.h> |
| 57 |
#include <cdogs/actors.h> |
| 58 |
#include <cdogs/automap.h> |
| 59 |
#include <cdogs/collision/collision.h> |
| 60 |
#include <cdogs/config_io.h> |
| 61 |
#include <cdogs/draw/draw.h> |
| 62 |
#include <cdogs/draw/drawtools.h> |
| 63 |
#include <cdogs/events.h> |
| 64 |
#include <cdogs/files.h> |
| 65 |
#include <cdogs/font_utils.h> |
| 66 |
#include <cdogs/log.h> |
| 67 |
#include <cdogs/map_wolf.h> |
| 68 |
#include <cdogs/player_template.h> |
| 69 |
|
| 70 |
#include <tinydir/tinydir.h> |
| 71 |
|
| 72 |
#include <cdogsed/char_editor.h> |
| 73 |
#include <cdogsed/editor_ui.h> |
| 74 |
#include <cdogsed/editor_ui_common.h> |
| 75 |
#include <cdogsed/file_dialog.h> |
| 76 |
|
| 77 |
|
| 78 |
static UIObject *sObjs; |
| 79 |
static CArray sDrawObjs; |
| 80 |
static UIObject *sLastHighlightedObj = NULL; |
| 81 |
static UIObject *sTooltipObj = NULL; |
| 82 |
static DrawBuffer sDrawBuffer; |
| 83 |
static bool sJustLoaded = true; |
| 84 |
static bool sHasUnbakedChanges = false; |
| 85 |
static int sAutosaveIndex = 0; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static bool sIgnoreMouse = false; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
typedef struct |
| 94 |
{ |
| 95 |
GraphicsDevice *g; |
| 96 |
struct vec2 camera; |
| 97 |
} EditorContext; |
| 98 |
EditorContext ec; |
| 99 |
static char lastFile[CDOGS_PATH_MAX]; |
| 100 |
static EditorBrush brush; |
| 101 |
#define CAMERA_PAN_SPEED 3 |
| 102 |
Mission currentMission; |
| 103 |
Mission lastMission; |
| 104 |
#define AUTOSAVE_INTERVAL_SECONDS 60 |
| 105 |
Uint32 ticksAutosave; |
| 106 |
Uint32 sTicksElapsed; |
| 107 |
bool fileChanged = false; |
| 108 |
|
| 109 |
static struct vec2i GetMouseTile(EventHandlers *e) |
| 110 |
{ |
| 111 |
Mission *m = CampaignGetCurrentMission(&gCampaign); |
| 112 |
if (!m) |
| 113 |
{ |
| 114 |
return svec2i(-1, -1); |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
return svec2i( |
| 119 |
(e->mouse.currentPos.x - sDrawBuffer.dx) / TILE_WIDTH + |
| 120 |
sDrawBuffer.xStart, |
| 121 |
(e->mouse.currentPos.y - sDrawBuffer.dy) / TILE_HEIGHT + |
| 122 |
sDrawBuffer.yStart); |
| 123 |
} |
| 124 |
} |
| 125 |
static struct vec2i GetScreenPos(struct vec2i mapTile) |
| 126 |
{ |
| 127 |
return svec2i( |
| 128 |
(mapTile.x - sDrawBuffer.xStart) * TILE_WIDTH + sDrawBuffer.dx, |
| 129 |
(mapTile.y - sDrawBuffer.yStart) * TILE_HEIGHT + sDrawBuffer.dy); |
| 130 |
} |
| 131 |
|
| 132 |
static void MakeBackground(const bool changedMission) |
| 133 |
{ |
| 134 |
if (changedMission) |
| 135 |
{ |
| 136 |
|
| 137 |
Mission *m = gMission.missionData; |
| 138 |
struct vec2i focusTile = svec2i_scale_divide(m->Size, 2); |
| 139 |
|
| 140 |
if (m->Type == MAPTYPE_STATIC && !svec2i_is_zero(m->u.Static.Start)) |
| 141 |
{ |
| 142 |
focusTile = m->u.Static.Start; |
| 143 |
} |
| 144 |
ec.camera = Vec2CenterOfTile(focusTile); |
| 145 |
} |
| 146 |
|
| 147 |
DrawBufferArgs args; |
| 148 |
memset(&args, 0, sizeof args); |
| 149 |
args.Editor = true; |
| 150 |
args.GuideImage = &brush.GuideImagePic; |
| 151 |
args.GuideImageAlpha = brush.GuideImageAlpha; |
| 152 |
|
| 153 |
DrawBufferTerminate(&sDrawBuffer); |
| 154 |
ClearScreen(ec.g); |
| 155 |
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), ec.g); |
| 156 |
GrafxMakeBackground( |
| 157 |
ec.g, &sDrawBuffer, &gCampaign, &gMission, &gMap, tintNone, true, |
| 158 |
ec.camera, &args); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
typedef struct |
| 163 |
{ |
| 164 |
bool Redraw; |
| 165 |
bool WillDisplayAutomap; |
| 166 |
bool Done; |
| 167 |
} HandleInputResult; |
| 168 |
|
| 169 |
static void Display(HandleInputResult result) |
| 170 |
{ |
| 171 |
char s[128]; |
| 172 |
int y = 5; |
| 173 |
const int w = ec.g->cachedConfig.Res.x; |
| 174 |
const int h = ec.g->cachedConfig.Res.y; |
| 175 |
|
| 176 |
WindowContextPreRender(&ec.g->gameWindow); |
| 177 |
ClearScreen(ec.g); |
| 178 |
|
| 179 |
const Mission *mission = CampaignGetCurrentMission(&gCampaign); |
| 180 |
if (mission) |
| 181 |
{ |
| 182 |
|
| 183 |
if (gEventHandlers.HasResolutionChanged) |
| 184 |
{ |
| 185 |
MakeBackground(false); |
| 186 |
} |
| 187 |
|
| 188 |
DrawBufferArgs args; |
| 189 |
memset(&args, 0, sizeof args); |
| 190 |
args.Editor = true; |
| 191 |
args.GuideImage = &brush.GuideImagePic; |
| 192 |
args.GuideImageAlpha = brush.GuideImageAlpha; |
| 193 |
GrafxDrawBackground(ec.g, &sDrawBuffer, tintNone, ec.camera, &args); |
| 194 |
BlitClearBuf(ec.g); |
| 195 |
|
| 196 |
|
| 197 |
if (brush.IsActive && |
| 198 |
Rect2iIsInside(Rect2iNew(svec2i_zero(), mission->Size), brush.Pos)) |
| 199 |
{ |
| 200 |
EditorBrushSetHighlightedTiles(&brush); |
| 201 |
} |
| 202 |
CA_FOREACH(struct vec2i, pos, brush.HighlightedTiles) |
| 203 |
struct vec2i screenPos = GetScreenPos(*pos); |
| 204 |
if (screenPos.x >= 0 && screenPos.x < w && screenPos.y >= 0 && |
| 205 |
screenPos.y < h) |
| 206 |
{ |
| 207 |
DrawRectangle(ec.g, screenPos, TILE_SIZE, colorWhite, false); |
| 208 |
} |
| 209 |
CA_FOREACH_END() |
| 210 |
|
| 211 |
if (brush.LastPos.x) |
| 212 |
{ |
| 213 |
sprintf(s, "(%d, %d)", brush.Pos.x, brush.Pos.y); |
| 214 |
FontStr(s, svec2i(w - 40, h - 16)); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
if (fileChanged) |
| 219 |
{ |
| 220 |
|
| 221 |
const Pic *pic = PicManagerGetPic(&gPicManager, "disk1"); |
| 222 |
PicRender( |
| 223 |
pic, gGraphicsDevice.gameWindow.renderer, svec2i(10, y), |
| 224 |
colorWhite, 0, svec2_one(), SDL_FLIP_NONE, Rect2iZero()); |
| 225 |
} |
| 226 |
|
| 227 |
FontStr( |
| 228 |
"Press " KMOD_CMD_NAME "+E to edit characters", |
| 229 |
svec2i(20, h - 20 - FontH() * 2)); |
| 230 |
FontStr("Press F1 for help", svec2i(20, h - 20 - FontH())); |
| 231 |
|
| 232 |
UIObjectDraw( |
| 233 |
sObjs, ec.g, svec2i_zero(), gEventHandlers.mouse.currentPos, |
| 234 |
&sDrawObjs); |
| 235 |
|
| 236 |
if (result.WillDisplayAutomap && mission) |
| 237 |
{ |
| 238 |
AutomapDraw(&gGraphicsDevice, NULL, AUTOMAP_FLAGS_SHOWALL, true); |
| 239 |
} |
| 240 |
else |
| 241 |
{ |
| 242 |
if (sTooltipObj && sTooltipObj->Tooltip) |
| 243 |
{ |
| 244 |
UITooltipDraw( |
| 245 |
ec.g, gEventHandlers.mouse.currentPos, sTooltipObj->Tooltip); |
| 246 |
} |
| 247 |
} |
| 248 |
BlitUpdateFromBuf(ec.g, ec.g->screen); |
| 249 |
WindowContextPostRender(&ec.g->gameWindow); |
| 250 |
} |
| 251 |
|
| 252 |
static void Setup(const bool changedMission); |
| 253 |
|
| 254 |
static void Change(UIObject *o, const int d, const bool shift) |
| 255 |
{ |
| 256 |
if (o == NULL) |
| 257 |
{ |
| 258 |
return; |
| 259 |
} |
| 260 |
const EditorResult r = UIObjectChange(o, d, shift); |
| 261 |
if (r & EDITOR_RESULT_CHANGED) |
| 262 |
{ |
| 263 |
fileChanged = true; |
| 264 |
} |
| 265 |
if (r & EDITOR_RESULT_CHANGED_AND_RELOAD) |
| 266 |
{ |
| 267 |
Setup(false); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
static void AddObjective(Mission *m) |
| 272 |
{ |
| 273 |
|
| 274 |
if (m->Objectives.size < OBJECTIVE_MAX_OLD) |
| 275 |
{ |
| 276 |
Objective o; |
| 277 |
memset(&o, 0, sizeof o); |
| 278 |
CArrayPushBack(&m->Objectives, &o); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
static void DeleteObjective(Mission *m, int idx) |
| 283 |
{ |
| 284 |
idx = CLAMP(idx, 0, (int)m->Objectives.size); |
| 285 |
CArrayDelete(&m->Objectives, idx); |
| 286 |
} |
| 287 |
|
| 288 |
static void DeleteCharacter(Mission *m, int idx) |
| 289 |
{ |
| 290 |
idx = CLAMP(idx, 0, (int)m->Enemies.size); |
| 291 |
CArrayDelete(&m->Enemies, idx); |
| 292 |
CharacterStoreDeleteBaddie(&gCampaign.Setting.characters, idx); |
| 293 |
} |
| 294 |
|
| 295 |
static void DeleteSpecial(Mission *m, int idx) |
| 296 |
{ |
| 297 |
idx = CLAMP(idx, 0, (int)m->SpecialChars.size); |
| 298 |
CArrayDelete(&m->SpecialChars, idx); |
| 299 |
CharacterStoreDeleteSpecial(&gCampaign.Setting.characters, idx); |
| 300 |
} |
| 301 |
|
| 302 |
static void DeleteItem(Mission *m, int idx) |
| 303 |
{ |
| 304 |
if (idx >= (int)m->MapObjectDensities.size) |
| 305 |
{ |
| 306 |
|
| 307 |
return; |
| 308 |
} |
| 309 |
idx = CLAMP(idx, 0, (int)m->MapObjectDensities.size); |
| 310 |
CArrayDelete(&m->MapObjectDensities, idx); |
| 311 |
} |
| 312 |
|
| 313 |
static void DeletePickup(Mission *m, int idx) |
| 314 |
{ |
| 315 |
if (idx >= (int)m->PickupCounts.size) |
| 316 |
{ |
| 317 |
|
| 318 |
return; |
| 319 |
} |
| 320 |
idx = CLAMP(idx, 0, (int)m->PickupCounts.size); |
| 321 |
CArrayDelete(&m->PickupCounts, idx); |
| 322 |
} |
| 323 |
|
| 324 |
static void AdjustYC(int *yc) |
| 325 |
{ |
| 326 |
Mission *mission = CampaignGetCurrentMission(&gCampaign); |
| 327 |
if (mission) |
| 328 |
{ |
| 329 |
if (mission->Objectives.size) |
| 330 |
{ |
| 331 |
*yc = CLAMP_OPPOSITE( |
| 332 |
*yc, 0, YC_OBJECTIVES + (int)mission->Objectives.size - 1); |
| 333 |
} |
| 334 |
else |
| 335 |
{ |
| 336 |
*yc = CLAMP_OPPOSITE(*yc, 0, YC_OBJECTIVES); |
| 337 |
} |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
static void AdjustXC(int yc, int *xc) |
| 342 |
{ |
| 343 |
Mission *mission = CampaignGetCurrentMission(&gCampaign); |
| 344 |
switch (yc) |
| 345 |
{ |
| 346 |
case YC_MISSIONTITLE: |
| 347 |
*xc = CLAMP_OPPOSITE(*xc, 0, XC_MUSICFILE); |
| 348 |
break; |
| 349 |
|
| 350 |
case YC_MISSIONLOOKS: |
| 351 |
*xc = CLAMP_OPPOSITE(*xc, 0, XC_COLOR4); |
| 352 |
break; |
| 353 |
|
| 354 |
case YC_CHARACTERS: |
| 355 |
if (mission && mission->Enemies.size > 0) |
| 356 |
{ |
| 357 |
*xc = CLAMP_OPPOSITE(*xc, 0, (int)mission->Enemies.size - 1); |
| 358 |
} |
| 359 |
break; |
| 360 |
|
| 361 |
case YC_SPECIALS: |
| 362 |
if (mission && mission->SpecialChars.size > 0) |
| 363 |
{ |
| 364 |
*xc = CLAMP_OPPOSITE(*xc, 0, (int)mission->SpecialChars.size - 1); |
| 365 |
} |
| 366 |
break; |
| 367 |
|
| 368 |
case YC_ITEMS: |
| 369 |
if (mission && mission->MapObjectDensities.size > 0) |
| 370 |
{ |
| 371 |
*xc = CLAMP_OPPOSITE( |
| 372 |
*xc, 0, (int)mission->MapObjectDensities.size - 1); |
| 373 |
} |
| 374 |
break; |
| 375 |
|
| 376 |
case YC_PICKUPS: |
| 377 |
if (mission && mission->PickupCounts.size > 0) |
| 378 |
{ |
| 379 |
*xc = CLAMP_OPPOSITE(*xc, 0, (int)mission->PickupCounts.size - 1); |
| 380 |
} |
| 381 |
break; |
| 382 |
|
| 383 |
default: |
| 384 |
break; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
static void Autosave(void) |
| 389 |
{ |
| 390 |
if (fileChanged && sTicksElapsed > ticksAutosave) |
| 391 |
{ |
| 392 |
ticksAutosave = sTicksElapsed + AUTOSAVE_INTERVAL_SECONDS * 1000; |
| 393 |
char dirname[CDOGS_PATH_MAX]; |
| 394 |
PathGetDirname(dirname, lastFile); |
| 395 |
char buf[CDOGS_PATH_MAX]; |
| 396 |
sprintf( |
| 397 |
buf, "%s~%d%s", dirname, sAutosaveIndex, |
| 398 |
PathGetBasename(lastFile)); |
| 399 |
MapArchiveSave(buf, &gCampaign.Setting); |
| 400 |
sAutosaveIndex++; |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
static void Setup(const bool changedMission) |
| 405 |
{ |
| 406 |
Mission *m = CampaignGetCurrentMission(&gCampaign); |
| 407 |
if (!m) |
| 408 |
{ |
| 409 |
return; |
| 410 |
} |
| 411 |
MissionCopy(&lastMission, ¤tMission); |
| 412 |
MissionCopy(¤tMission, m); |
| 413 |
MissionOptionsTerminate(&gMission); |
| 414 |
CampaignAndMissionSetup(&gCampaign, &gMission); |
| 415 |
MakeBackground(changedMission); |
| 416 |
|
| 417 |
Autosave(); |
| 418 |
|
| 419 |
sJustLoaded = true; |
| 420 |
sHasUnbakedChanges = false; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
static void ReloadUI(void) |
| 425 |
{ |
| 426 |
UIObjectDestroy(sObjs); |
| 427 |
sObjs = CreateMainObjs(&gCampaign, &brush, svec2i(320, 240)); |
| 428 |
CArrayTerminate(&sDrawObjs); |
| 429 |
sLastHighlightedObj = NULL; |
| 430 |
sTooltipObj = NULL; |
| 431 |
} |
| 432 |
|
| 433 |
static bool TryOpen(const char *filename); |
| 434 |
static void ShowFailedToOpenMsg(const char *filename); |
| 435 |
static void Open(void) |
| 436 |
{ |
| 437 |
char dirname[CDOGS_PATH_MAX]; |
| 438 |
PathGetDirname(dirname, lastFile); |
| 439 |
char buf[CDOGS_PATH_MAX]; |
| 440 |
RealPath(dirname, buf); |
| 441 |
FixPathSeparator(dirname, buf); |
| 442 |
|
| 443 |
if (TryOpenDir(buf, &gEventHandlers, dirname, PathGetBasename(lastFile))) |
| 444 |
{ |
| 445 |
WindowContextPreRender(&gGraphicsDevice.gameWindow); |
| 446 |
ClearScreen(&gGraphicsDevice); |
| 447 |
|
| 448 |
FontStrCenter("Loading..."); |
| 449 |
|
| 450 |
BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen); |
| 451 |
WindowContextPostRender(&gGraphicsDevice.gameWindow); |
| 452 |
if (!TryOpen(buf)) |
| 453 |
{ |
| 454 |
ShowFailedToOpenMsg(buf); |
| 455 |
} |
| 456 |
} |
| 457 |
} |
| 458 |
static bool TryOpen(const char *filename) |
| 459 |
{ |
| 460 |
|
| 461 |
CampaignSettingTerminateAll(&gCampaign.Setting); |
| 462 |
CampaignSettingInit(&gCampaign.Setting); |
| 463 |
char buf[CDOGS_PATH_MAX]; |
| 464 |
RealPath(filename, buf); |
| 465 |
sAutosaveIndex = 0; |
| 466 |
gCampaign.MissionIndex = 0; |
| 467 |
fileChanged = false; |
| 468 |
bool loaded = false; |
| 469 |
if (!MapNewLoad(buf, &gCampaign.Setting)) |
| 470 |
{ |
| 471 |
Setup(true); |
| 472 |
strcpy(lastFile, filename); |
| 473 |
loaded = true; |
| 474 |
} |
| 475 |
|
| 476 |
ReloadUI(); |
| 477 |
return loaded; |
| 478 |
} |
| 479 |
static void ShowFailedToOpenMsg(const char *filename) |
| 480 |
{ |
| 481 |
char msgBuf[CDOGS_PATH_MAX]; |
| 482 |
sprintf(msgBuf, "Failed to open file %s", filename); |
| 483 |
SDL_ShowSimpleMessageBox( |
| 484 |
SDL_MESSAGEBOX_ERROR, "Error", msgBuf, |
| 485 |
gGraphicsDevice.gameWindow.window); |
| 486 |
} |
| 487 |
|
| 488 |
static void Save(void) |
| 489 |
{ |
| 490 |
char dirname[CDOGS_PATH_MAX]; |
| 491 |
PathGetDirname(dirname, lastFile); |
| 492 |
char buf[CDOGS_PATH_MAX]; |
| 493 |
if (TrySaveFile(buf, &gEventHandlers, dirname, PathGetBasename(lastFile))) |
| 494 |
{ |
| 495 |
WindowContextPreRender(&gGraphicsDevice.gameWindow); |
| 496 |
ClearScreen(&gGraphicsDevice); |
| 497 |
|
| 498 |
FontStrCenter("Saving..."); |
| 499 |
|
| 500 |
BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen); |
| 501 |
WindowContextPostRender(&gGraphicsDevice.gameWindow); |
| 502 |
if (!MapArchiveSave(buf, &gCampaign.Setting)) |
| 503 |
{ |
| 504 |
SDL_ShowSimpleMessageBox( |
| 505 |
SDL_MESSAGEBOX_INFORMATION, "Error", "Error when saving campaign!", |
| 506 |
gGraphicsDevice.gameWindow.window); |
| 507 |
} |
| 508 |
fileChanged = false; |
| 509 |
strcpy(lastFile, buf); |
| 510 |
sAutosaveIndex = 0; |
| 511 |
char msgBuf[CDOGS_PATH_MAX]; |
| 512 |
sprintf(msgBuf, "Saved to %s", buf); |
| 513 |
SDL_ShowSimpleMessageBox( |
| 514 |
SDL_MESSAGEBOX_INFORMATION, "Campaign Saved", msgBuf, |
| 515 |
gGraphicsDevice.gameWindow.window); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
static void HelpScreen(void) |
| 520 |
{ |
| 521 |
struct vec2i pos = svec2i(20, 20); |
| 522 |
const char *helpText = |
| 523 |
"Help\n" |
| 524 |
"====\n" |
| 525 |
"Use mouse to select controls; keyboard to type text\n" |
| 526 |
"Open files by dragging them over the editor shortcut\n" |
| 527 |
"\n" |
| 528 |
"Common commands\n" |
| 529 |
"===============\n" |
| 530 |
"left/right click, page up/down: Increase/decrease value\n" |
| 531 |
"shift + left/right click: Increase/decrease number of " |
| 532 |
"items\n" |
| 533 |
"insert: Add new item\n" |
| 534 |
"delete: Delete selected item\n" |
| 535 |
"arrow keys: Move camera\n" |
| 536 |
"\n" |
| 537 |
"Other commands\n" |
| 538 |
"==============\n" |
| 539 |
"Escape: Back or quit\n" KMOD_CMD_NAME |
| 540 |
"+E: Go to character editor\n" KMOD_CMD_NAME |
| 541 |
"+N: New mission\n" KMOD_CMD_NAME |
| 542 |
"+O: Open file\n" KMOD_CMD_NAME |
| 543 |
"+S: Save file\n" KMOD_CMD_NAME |
| 544 |
"+X, C, V: Cut/copy/paste\n" |
| 545 |
"tab: Preview automap\n" |
| 546 |
"F1: This screen\n"; |
| 547 |
WindowContextPreRender(&gGraphicsDevice.gameWindow); |
| 548 |
ClearScreen(&gGraphicsDevice); |
| 549 |
FontStr(helpText, pos); |
| 550 |
BlitUpdateFromBuf(&gGraphicsDevice, gGraphicsDevice.screen); |
| 551 |
WindowContextPostRender(&gGraphicsDevice.gameWindow); |
| 552 |
GetKey(&gEventHandlers); |
| 553 |
} |
| 554 |
|
| 555 |
static void Delete(int xc, int yc) |
| 556 |
{ |
| 557 |
Mission *mission = CampaignGetCurrentMission(&gCampaign); |
| 558 |
bool changedMission = false; |
| 559 |
switch (yc) |
| 560 |
{ |
| 561 |
case YC_CHARACTERS: |
| 562 |
if (xc >= (int)mission->Enemies.size) |
| 563 |
{ |
| 564 |
|
| 565 |
return; |
| 566 |
} |
| 567 |
DeleteCharacter(mission, xc); |
| 568 |
break; |
| 569 |
|
| 570 |
case YC_SPECIALS: |
| 571 |
if (xc >= (int)mission->SpecialChars.size) |
| 572 |
{ |
| 573 |
|
| 574 |
return; |
| 575 |
} |
| 576 |
DeleteSpecial(mission, xc); |
| 577 |
break; |
| 578 |
|
| 579 |
case YC_ITEMS: |
| 580 |
DeleteItem(mission, xc); |
| 581 |
break; |
| 582 |
|
| 583 |
case YC_PICKUPS: |
| 584 |
DeletePickup(mission, xc); |
| 585 |
break; |
| 586 |
|
| 587 |
default: |
| 588 |
if (yc >= YC_OBJECTIVES) |
| 589 |
{ |
| 590 |
if (mission->Objectives.size == 0) |
| 591 |
{ |
| 592 |
|
| 593 |
return; |
| 594 |
} |
| 595 |
DeleteObjective(mission, yc - YC_OBJECTIVES); |
| 596 |
} |
| 597 |
else |
| 598 |
{ |
| 599 |
if (gCampaign.Setting.Missions.size == 0 || |
| 600 |
gCampaign.MissionIndex >= (int)gCampaign.Setting.Missions.size) |
| 601 |
{ |
| 602 |
|
| 603 |
return; |
| 604 |
} |
| 605 |
CampaignDeleteMission(&gCampaign, gCampaign.MissionIndex); |
| 606 |
changedMission = true; |
| 607 |
} |
| 608 |
AdjustYC(&yc); |
| 609 |
break; |
| 610 |
} |
| 611 |
fileChanged = true; |
| 612 |
Setup(changedMission); |
| 613 |
} |
| 614 |
|
| 615 |
static UIObject *OnUIInput( |
| 616 |
HandleInputResult *result, const EventHandlers *event, const int m, |
| 617 |
int *xc, int *yc, int *xcOld, int *ycOld, SDL_Scancode *sc, |
| 618 |
const Mission *mission); |
| 619 |
static void InputInsert(int *xc, const int yc, Mission *mission); |
| 620 |
static void InputDelete(const int xc, const int yc); |
| 621 |
static HandleInputResult HandleInput( |
| 622 |
SDL_Scancode sc, const int m, int *xc, int *yc, int *xcOld, int *ycOld, |
| 623 |
Mission *scrap) |
| 624 |
{ |
| 625 |
HandleInputResult result = {false, false, false}; |
| 626 |
Mission *mission = CampaignGetCurrentMission(&gCampaign); |
| 627 |
UIObject *o = NULL; |
| 628 |
const struct vec2i brushLastDrawPos = brush.Pos; |
| 629 |
brush.Pos = GetMouseTile(&gEventHandlers); |
| 630 |
const bool shift = gEventHandlers.keyboard.modState & KMOD_SHIFT; |
| 631 |
|
| 632 |
|
| 633 |
const bool hadTooltip = sTooltipObj != NULL; |
| 634 |
bool isContextMenu = false; |
| 635 |
sTooltipObj = NULL; |
| 636 |
const struct vec2i mousePos = gEventHandlers.mouse.currentPos; |
| 637 |
UITryGetObject(sLastHighlightedObj, mousePos, &sTooltipObj); |
| 638 |
if (sTooltipObj == NULL) |
| 639 |
{ |
| 640 |
UITryGetObject(sObjs, mousePos, &sTooltipObj); |
| 641 |
} |
| 642 |
if (sTooltipObj != NULL) |
| 643 |
{ |
| 644 |
if (sTooltipObj->Parent && |
| 645 |
sTooltipObj->Parent->Type == UITYPE_CONTEXT_MENU) |
| 646 |
{ |
| 647 |
isContextMenu = true; |
| 648 |
} |
| 649 |
if (!sTooltipObj->Tooltip) |
| 650 |
{ |
| 651 |
sTooltipObj = NULL; |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
if (hadTooltip || sTooltipObj || isContextMenu) |
| 657 |
{ |
| 658 |
result.Redraw = true; |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
if (gEventHandlers.HasResolutionChanged) |
| 665 |
{ |
| 666 |
result.Redraw = true; |
| 667 |
} |
| 668 |
|
| 669 |
|
| 670 |
if (brush.IsActive && !svec2i_is_equal(brushLastDrawPos, brush.Pos)) |
| 671 |
{ |
| 672 |
result.Redraw = true; |
| 673 |
} |
| 674 |
|
| 675 |
if (m && (m == SDL_BUTTON_LEFT || m == SDL_BUTTON_RIGHT || |
| 676 |
MouseWheel(&gEventHandlers.mouse).y != 0)) |
| 677 |
{ |
| 678 |
o = OnUIInput( |
| 679 |
&result, &gEventHandlers, m, xc, yc, xcOld, ycOld, &sc, mission); |
| 680 |
} |
| 681 |
if (!brush.IsActive) |
| 682 |
{ |
| 683 |
MouseSetCursor(&gEventHandlers.mouse, SDL_SYSTEM_CURSOR_ARROW); |
| 684 |
} |
| 685 |
if (!o && |
| 686 |
(MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_LEFT) || |
| 687 |
MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_RIGHT)) && |
| 688 |
!sIgnoreMouse) |
| 689 |
{ |
| 690 |
result.Redraw = true; |
| 691 |
if (brush.IsActive && mission && mission->Type == MAPTYPE_STATIC) |
| 692 |
{ |
| 693 |
|
| 694 |
if (Rect2iIsInside( |
| 695 |
Rect2iNew(svec2i_zero(), mission->Size), brush.Pos)) |
| 696 |
{ |
| 697 |
const bool isMain = |
| 698 |
MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_LEFT); |
| 699 |
const EditorResult r = |
| 700 |
EditorBrushStartPainting(&brush, mission, isMain); |
| 701 |
if (r & EDITOR_RESULT_CHANGED) |
| 702 |
{ |
| 703 |
fileChanged = true; |
| 704 |
Autosave(); |
| 705 |
sHasUnbakedChanges = true; |
| 706 |
} |
| 707 |
if (r & EDITOR_RESULT_RELOAD) |
| 708 |
{ |
| 709 |
Setup(false); |
| 710 |
} |
| 711 |
} |
| 712 |
} |
| 713 |
} |
| 714 |
else |
| 715 |
{ |
| 716 |
if (mission) |
| 717 |
{ |
| 718 |
|
| 719 |
brush.Pos = svec2i_clamp( |
| 720 |
brush.Pos, svec2i_zero(), |
| 721 |
svec2i_subtract(mission->Size, svec2i_one())); |
| 722 |
const EditorResult r = EditorBrushStopPainting(&brush, mission); |
| 723 |
if (r & EDITOR_RESULT_CHANGED) |
| 724 |
{ |
| 725 |
fileChanged = true; |
| 726 |
Autosave(); |
| 727 |
result.Redraw = true; |
| 728 |
sHasUnbakedChanges = true; |
| 729 |
} |
| 730 |
if (r & EDITOR_RESULT_RELOAD) |
| 731 |
{ |
| 732 |
Setup(false); |
| 733 |
} |
| 734 |
} |
| 735 |
} |
| 736 |
|
| 737 |
if (mission) |
| 738 |
{ |
| 739 |
if (KeyIsDown(&gEventHandlers.keyboard, SDL_SCANCODE_LEFT)) |
| 740 |
{ |
| 741 |
ec.camera.x -= CAMERA_PAN_SPEED; |
| 742 |
result.Redraw = true; |
| 743 |
} |
| 744 |
else if (KeyIsDown(&gEventHandlers.keyboard, SDL_SCANCODE_RIGHT)) |
| 745 |
{ |
| 746 |
ec.camera.x += CAMERA_PAN_SPEED; |
| 747 |
result.Redraw = true; |
| 748 |
} |
| 749 |
if (KeyIsDown(&gEventHandlers.keyboard, SDL_SCANCODE_UP)) |
| 750 |
{ |
| 751 |
ec.camera.y -= CAMERA_PAN_SPEED; |
| 752 |
result.Redraw = true; |
| 753 |
} |
| 754 |
else if (KeyIsDown(&gEventHandlers.keyboard, SDL_SCANCODE_DOWN)) |
| 755 |
{ |
| 756 |
ec.camera.y += CAMERA_PAN_SPEED; |
| 757 |
result.Redraw = true; |
| 758 |
} |
| 759 |
|
| 760 |
if (MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_MIDDLE)) |
| 761 |
{ |
| 762 |
ec.camera = svec2_add( |
| 763 |
ec.camera, svec2_assign_vec2i(svec2i_subtract( |
| 764 |
gEventHandlers.mouse.previousPos, |
| 765 |
gEventHandlers.mouse.currentPos))); |
| 766 |
result.Redraw = true; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
if (MouseIsPressed(&gEventHandlers.mouse, SDL_BUTTON_MIDDLE)) |
| 771 |
{ |
| 772 |
MouseSetCursor(&gEventHandlers.mouse, SDL_SYSTEM_CURSOR_SIZEALL); |
| 773 |
} |
| 774 |
if (MouseIsReleased(&gEventHandlers.mouse, SDL_BUTTON_MIDDLE)) |
| 775 |
{ |
| 776 |
MouseSetCursor(&gEventHandlers.mouse, SDL_SYSTEM_CURSOR_ARROW); |
| 777 |
} |
| 778 |
ec.camera.x = CLAMP(ec.camera.x, 0, Vec2CenterOfTile(mission->Size).x); |
| 779 |
ec.camera.y = CLAMP(ec.camera.y, 0, Vec2CenterOfTile(mission->Size).y); |
| 780 |
} |
| 781 |
bool hasQuit = false; |
| 782 |
if (sc != SDL_SCANCODE_UNKNOWN) |
| 783 |
{ |
| 784 |
result.Redraw = true; |
| 785 |
} |
| 786 |
if (gEventHandlers.keyboard.modState & KMOD_CMD) |
| 787 |
{ |
| 788 |
const SDL_Keycode kc = SDL_GetKeyFromScancode(sc); |
| 789 |
switch (kc) |
| 790 |
{ |
| 791 |
case 'z': |
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
if (sHasUnbakedChanges) |
| 810 |
{ |
| 811 |
MissionCopy(&lastMission, mission); |
| 812 |
} |
| 813 |
else if (mission != NULL) |
| 814 |
{ |
| 815 |
MissionCopy(mission, &lastMission); |
| 816 |
MissionCopy(&lastMission, ¤tMission); |
| 817 |
} |
| 818 |
fileChanged = true; |
| 819 |
Setup(false); |
| 820 |
break; |
| 821 |
|
| 822 |
case 'x': |
| 823 |
MissionTerminate(scrap); |
| 824 |
MissionCopy(scrap, mission); |
| 825 |
Delete(*xc, *yc); |
| 826 |
|
| 827 |
UIObjectUnhighlight(sObjs, true); |
| 828 |
CArrayTerminate(&sDrawObjs); |
| 829 |
sLastHighlightedObj = NULL; |
| 830 |
break; |
| 831 |
|
| 832 |
case 'c': |
| 833 |
MissionTerminate(scrap); |
| 834 |
MissionCopy(scrap, mission); |
| 835 |
SDL_ShowSimpleMessageBox( |
| 836 |
SDL_MESSAGEBOX_INFORMATION, "Editor", "Mission copied", |
| 837 |
gGraphicsDevice.gameWindow.window); |
| 838 |
break; |
| 839 |
|
| 840 |
case 'v': |
| 841 |
|
| 842 |
if (!svec2i_is_zero(scrap->Size)) |
| 843 |
{ |
| 844 |
InsertMission(&gCampaign, scrap, gCampaign.MissionIndex); |
| 845 |
fileChanged = true; |
| 846 |
Setup(false); |
| 847 |
} |
| 848 |
break; |
| 849 |
|
| 850 |
case 'i': |
| 851 |
InputInsert(xc, *yc, mission); |
| 852 |
break; |
| 853 |
|
| 854 |
case 'd': |
| 855 |
InputDelete(*xc, *yc); |
| 856 |
break; |
| 857 |
|
| 858 |
case 'q': |
| 859 |
hasQuit = true; |
| 860 |
break; |
| 861 |
|
| 862 |
case 'n': |
| 863 |
InsertMission( |
| 864 |
&gCampaign, NULL, (int)gCampaign.Setting.Missions.size); |
| 865 |
gCampaign.MissionIndex = |
| 866 |
(int)(gCampaign.Setting.Missions.size - 1); |
| 867 |
fileChanged = true; |
| 868 |
Setup(true); |
| 869 |
break; |
| 870 |
|
| 871 |
case 'o': |
| 872 |
if (!fileChanged || ConfirmScreen( |
| 873 |
"File has been modified, but not saved", |
| 874 |
"Open anyway? (Y/N)")) |
| 875 |
{ |
| 876 |
Open(); |
| 877 |
} |
| 878 |
break; |
| 879 |
|
| 880 |
case 's': |
| 881 |
Save(); |
| 882 |
break; |
| 883 |
|
| 884 |
case 'e': |
| 885 |
CharEditor( |
| 886 |
ec.g, &gCampaign.Setting, &gEventHandlers, &fileChanged); |
| 887 |
Setup(false); |
| 888 |
UIObjectUnhighlight(sObjs, true); |
| 889 |
CArrayTerminate(&sDrawObjs); |
| 890 |
break; |
| 891 |
|
| 892 |
default: |
| 893 |
|
| 894 |
break; |
| 895 |
} |
| 896 |
} |
| 897 |
else |
| 898 |
{ |
| 899 |
switch (sc) |
| 900 |
{ |
| 901 |
case SDL_SCANCODE_F1: |
| 902 |
HelpScreen(); |
| 903 |
break; |
| 904 |
|
| 905 |
case SDL_SCANCODE_HOME: |
| 906 |
if (gCampaign.MissionIndex > 0) |
| 907 |
{ |
| 908 |
gCampaign.MissionIndex--; |
| 909 |
} |
| 910 |
Setup(true); |
| 911 |
break; |
| 912 |
|
| 913 |
case SDL_SCANCODE_END: |
| 914 |
if (gCampaign.MissionIndex < (int)gCampaign.Setting.Missions.size) |
| 915 |
{ |
| 916 |
gCampaign.MissionIndex++; |
| 917 |
} |
| 918 |
Setup(true); |
| 919 |
break; |
| 920 |
|
| 921 |
case SDL_SCANCODE_INSERT: |
| 922 |
InputInsert(xc, *yc, mission); |
| 923 |
break; |
| 924 |
|
| 925 |
case SDL_SCANCODE_DELETE: |
| 926 |
InputDelete(*xc, *yc); |
| 927 |
break; |
| 928 |
|
| 929 |
case SDL_SCANCODE_PAGEUP: |
| 930 |
Change(o, 1, shift); |
| 931 |
break; |
| 932 |
|
| 933 |
case SDL_SCANCODE_PAGEDOWN: |
| 934 |
Change(o, -1, shift); |
| 935 |
break; |
| 936 |
|
| 937 |
case SDL_SCANCODE_ESCAPE: |
| 938 |
hasQuit = true; |
| 939 |
break; |
| 940 |
|
| 941 |
case SDL_SCANCODE_GRAVE: |
| 942 |
ToggleCollapse(sObjs->Data, 0); |
| 943 |
OnUIInput( |
| 944 |
&result, &gEventHandlers, m, xc, yc, xcOld, ycOld, &sc, |
| 945 |
mission); |
| 946 |
break; |
| 947 |
|
| 948 |
case SDL_SCANCODE_BACKSPACE: |
| 949 |
fileChanged = UIObjectDelChar(sObjs) || fileChanged; |
| 950 |
break; |
| 951 |
|
| 952 |
case SDL_SCANCODE_KP_0: |
| 953 |
if (!(SDL_GetModState() & KMOD_NUM)) |
| 954 |
{ |
| 955 |
InputInsert(xc, *yc, mission); |
| 956 |
} |
| 957 |
break; |
| 958 |
|
| 959 |
case SDL_SCANCODE_KP_PERIOD: |
| 960 |
if (!(SDL_GetModState() & KMOD_NUM)) |
| 961 |
{ |
| 962 |
InputDelete(*xc, *yc); |
| 963 |
} |
| 964 |
break; |
| 965 |
|
| 966 |
case SDL_SCANCODE_TAB: |
| 967 |
result.WillDisplayAutomap = true; |
| 968 |
break; |
| 969 |
|
| 970 |
default: |
| 971 |
|
| 972 |
break; |
| 973 |
} |
| 974 |
|
| 975 |
char *c = gEventHandlers.keyboard.Typed; |
| 976 |
while (c && *c >= ' ' && *c <= '~') |
| 977 |
{ |
| 978 |
fileChanged = UIObjectAddChar(sObjs, *c) || fileChanged; |
| 979 |
c++; |
| 980 |
} |
| 981 |
} |
| 982 |
if (gEventHandlers.DropFile != NULL) |
| 983 |
{ |
| 984 |
|
| 985 |
char buf[CDOGS_PATH_MAX]; |
| 986 |
strcpy(buf, gEventHandlers.DropFile); |
| 987 |
if (IsCampaignOK(buf, NULL, NULL)) |
| 988 |
{ |
| 989 |
if (!fileChanged || ConfirmScreen( |
| 990 |
"File has been modified, but not saved", |
| 991 |
"Open anyway? (Y/N)")) |
| 992 |
{ |
| 993 |
if (!TryOpen(buf)) |
| 994 |
{ |
| 995 |
ShowFailedToOpenMsg(buf); |
| 996 |
} |
| 997 |
} |
| 998 |
} |
| 999 |
else if (mission && mission->Type == MAPTYPE_STATIC) |
| 1000 |
{ |
| 1001 |
|
| 1002 |
if (EditorBrushTryLoadGuideImage(&brush, buf)) |
| 1003 |
{ |
| 1004 |
strcpy(brush.GuideImage, buf); |
| 1005 |
result.Redraw = true; |
| 1006 |
} |
| 1007 |
} |
| 1008 |
} |
| 1009 |
if (gEventHandlers.HasQuit) |
| 1010 |
{ |
| 1011 |
hasQuit = true; |
| 1012 |
|
| 1013 |
gEventHandlers.HasQuit = false; |
| 1014 |
} |
| 1015 |
if (hasQuit) |
| 1016 |
{ |
| 1017 |
|
| 1018 |
|
| 1019 |
result.Redraw = true; |
| 1020 |
result.Done = |
| 1021 |
!fileChanged || |
| 1022 |
ConfirmScreen( |
| 1023 |
"File has been modified, but not saved", "Quit anyway? (Y/N)"); |
| 1024 |
} |
| 1025 |
if (!MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_LEFT) && |
| 1026 |
!MouseIsDown(&gEventHandlers.mouse, SDL_BUTTON_RIGHT)) |
| 1027 |
{ |
| 1028 |
sIgnoreMouse = false; |
| 1029 |
} |
| 1030 |
return result; |
| 1031 |
} |
| 1032 |
UIObject *OnUIInput( |
| 1033 |
HandleInputResult *result, const EventHandlers *event, const int m, |
| 1034 |
int *xc, int *yc, int *xcOld, int *ycOld, SDL_Scancode *sc, |
| 1035 |
const Mission *mission) |
| 1036 |
{ |
| 1037 |
result->Redraw = true; |
| 1038 |
UIObject *o = NULL; |
| 1039 |
const struct vec2i mousePos = event->mouse.currentPos; |
| 1040 |
const bool shift = event->keyboard.modState & KMOD_SHIFT; |
| 1041 |
if (sLastHighlightedObj && !sLastHighlightedObj->IsBackground) |
| 1042 |
{ |
| 1043 |
UITryGetObject(sLastHighlightedObj, mousePos, &o); |
| 1044 |
} |
| 1045 |
if (o == NULL) |
| 1046 |
{ |
| 1047 |
UITryGetObject(sObjs, mousePos, &o); |
| 1048 |
} |
| 1049 |
if (o != NULL) |
| 1050 |
{ |
| 1051 |
if (!o->DoNotHighlight) |
| 1052 |
{ |
| 1053 |
if (sLastHighlightedObj) |
| 1054 |
{ |
| 1055 |
if (UIObjectUnhighlight(sLastHighlightedObj, true)) |
| 1056 |
{ |
| 1057 |
Setup(false); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
sLastHighlightedObj = o; |
| 1061 |
UIObjectHighlight(o, shift); |
| 1062 |
sIgnoreMouse = true; |
| 1063 |
} |
| 1064 |
CArrayTerminate(&sDrawObjs); |
| 1065 |
*xcOld = *xc; |
| 1066 |
*ycOld = *yc; |
| 1067 |
|
| 1068 |
if (m == SDL_BUTTON_LEFT || m == SDL_BUTTON_RIGHT) |
| 1069 |
{ |
| 1070 |
if (!(o->Flags & UI_LEAVE_YC)) |
| 1071 |
{ |
| 1072 |
*yc = o->Id; |
| 1073 |
AdjustYC(yc); |
| 1074 |
} |
| 1075 |
if (!(o->Flags & UI_LEAVE_XC)) |
| 1076 |
{ |
| 1077 |
*xc = o->Id2; |
| 1078 |
AdjustXC(*yc, xc); |
| 1079 |
} |
| 1080 |
} |
| 1081 |
if (!(o->Flags & UI_SELECT_ONLY) && |
| 1082 |
(!(o->Flags & UI_SELECT_ONLY_FIRST) || |
| 1083 |
(*xc == *xcOld && *yc == *ycOld))) |
| 1084 |
{ |
| 1085 |
if (m == SDL_BUTTON_LEFT || |
| 1086 |
MouseWheel(&gEventHandlers.mouse).y > 0) |
| 1087 |
{ |
| 1088 |
*sc = SDL_SCANCODE_PAGEUP; |
| 1089 |
} |
| 1090 |
else if ( |
| 1091 |
m == SDL_BUTTON_RIGHT || |
| 1092 |
MouseWheel(&gEventHandlers.mouse).y < 0) |
| 1093 |
{ |
| 1094 |
*sc = SDL_SCANCODE_PAGEDOWN; |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
else |
| 1099 |
{ |
| 1100 |
if (!(brush.IsActive && mission)) |
| 1101 |
{ |
| 1102 |
UIObjectUnhighlight(sObjs, true); |
| 1103 |
CArrayTerminate(&sDrawObjs); |
| 1104 |
sLastHighlightedObj = NULL; |
| 1105 |
} |
| 1106 |
} |
| 1107 |
return o; |
| 1108 |
} |
| 1109 |
static void InputInsert(int *xc, const int yc, Mission *mission) |
| 1110 |
{ |
| 1111 |
bool changedMission = false; |
| 1112 |
switch (yc) |
| 1113 |
{ |
| 1114 |
case YC_CHARACTERS: |
| 1115 |
if (gCampaign.Setting.characters.OtherChars.size > 0) |
| 1116 |
{ |
| 1117 |
int ch = 0; |
| 1118 |
CArrayPushBack(&mission->Enemies, &ch); |
| 1119 |
CharacterStoreAddBaddie(&gCampaign.Setting.characters, ch); |
| 1120 |
*xc = (int)(mission->Enemies.size - 1); |
| 1121 |
} |
| 1122 |
break; |
| 1123 |
|
| 1124 |
case YC_SPECIALS: |
| 1125 |
if (gCampaign.Setting.characters.OtherChars.size > 0) |
| 1126 |
{ |
| 1127 |
int ch = 0; |
| 1128 |
CArrayPushBack(&mission->SpecialChars, &ch); |
| 1129 |
CharacterStoreAddSpecial(&gCampaign.Setting.characters, ch); |
| 1130 |
*xc = (int)(mission->SpecialChars.size - 1); |
| 1131 |
} |
| 1132 |
break; |
| 1133 |
|
| 1134 |
case YC_ITEMS: { |
| 1135 |
MapObjectDensity mod; |
| 1136 |
mod.M = IndexMapObject(0); |
| 1137 |
mod.Density = 0; |
| 1138 |
CArrayPushBack(&mission->MapObjectDensities, &mod); |
| 1139 |
*xc = (int)(mission->MapObjectDensities.size - 1); |
| 1140 |
} |
| 1141 |
break; |
| 1142 |
|
| 1143 |
case YC_PICKUPS: { |
| 1144 |
PickupCount pc; |
| 1145 |
pc.P = PickupClassGetById(&gPickupClasses, 0); |
| 1146 |
pc.Count = 1; |
| 1147 |
CArrayPushBack(&mission->PickupCounts, &pc); |
| 1148 |
*xc = (int)(mission->PickupCounts.size - 1); |
| 1149 |
} |
| 1150 |
break; |
| 1151 |
|
| 1152 |
default: |
| 1153 |
if (yc >= YC_OBJECTIVES) |
| 1154 |
{ |
| 1155 |
AddObjective(mission); |
| 1156 |
} |
| 1157 |
else |
| 1158 |
{ |
| 1159 |
InsertMission(&gCampaign, NULL, gCampaign.MissionIndex); |
| 1160 |
changedMission = true; |
| 1161 |
} |
| 1162 |
break; |
| 1163 |
} |
| 1164 |
fileChanged = true; |
| 1165 |
Setup(changedMission); |
| 1166 |
} |
| 1167 |
static void InputDelete(const int xc, const int yc) |
| 1168 |
{ |
| 1169 |
Delete(xc, yc); |
| 1170 |
|
| 1171 |
UIObjectUnhighlight(sObjs, true); |
| 1172 |
CArrayTerminate(&sDrawObjs); |
| 1173 |
sLastHighlightedObj = NULL; |
| 1174 |
} |
| 1175 |
|
| 1176 |
static void EditCampaign(void) |
| 1177 |
{ |
| 1178 |
int xc = 0, yc = 0; |
| 1179 |
int xcOld, ycOld; |
| 1180 |
Mission scrap; |
| 1181 |
memset(&scrap, 0, sizeof scrap); |
| 1182 |
|
| 1183 |
Setup(true); |
| 1184 |
|
| 1185 |
Uint32 ticksNow = SDL_GetTicks(); |
| 1186 |
sTicksElapsed = 0; |
| 1187 |
Uint32 ticksElapsed = 0; |
| 1188 |
ticksAutosave = AUTOSAVE_INTERVAL_SECONDS * 1000; |
| 1189 |
SDL_StartTextInput(); |
| 1190 |
for (;;) |
| 1191 |
{ |
| 1192 |
Uint32 ticksThen = ticksNow; |
| 1193 |
ticksNow = SDL_GetTicks(); |
| 1194 |
ticksElapsed += ticksNow - ticksThen; |
| 1195 |
if (ticksElapsed < 1000 / FPS_FRAMELIMIT * 2) |
| 1196 |
{ |
| 1197 |
SDL_Delay(1); |
| 1198 |
continue; |
| 1199 |
} |
| 1200 |
|
| 1201 |
EventPoll(&gEventHandlers, ticksElapsed, NULL); |
| 1202 |
const SDL_Scancode sc = KeyGetPressed(&gEventHandlers.keyboard); |
| 1203 |
const int m = MouseGetPressed(&gEventHandlers.mouse); |
| 1204 |
|
| 1205 |
HandleInputResult result = |
| 1206 |
HandleInput(sc, m, &xc, &yc, &xcOld, &ycOld, &scrap); |
| 1207 |
if (result.Done) |
| 1208 |
{ |
| 1209 |
break; |
| 1210 |
} |
| 1211 |
if (result.Redraw || sJustLoaded) |
| 1212 |
{ |
| 1213 |
sJustLoaded = false; |
| 1214 |
LOG(LM_EDIT, LL_TRACE, "Drawing UI"); |
| 1215 |
Display(result); |
| 1216 |
if (result.WillDisplayAutomap) |
| 1217 |
{ |
| 1218 |
GetKey(&gEventHandlers); |
| 1219 |
} |
| 1220 |
} |
| 1221 |
sTicksElapsed += ticksElapsed; |
| 1222 |
ticksElapsed = 0; |
| 1223 |
} |
| 1224 |
SDL_StopTextInput(); |
| 1225 |
} |
| 1226 |
|
| 1227 |
static void ResetLastFile(char *s); |
| 1228 |
int main(int argc, char *argv[]) |
| 1229 |
{ |
| 1230 |
#if defined(_MSC_VER) && !defined(NDEBUG) |
| 1231 |
FreeConsole(); |
| 1232 |
#endif |
| 1233 |
int loaded = 0; |
| 1234 |
|
| 1235 |
|
| 1236 |
char buf[CDOGS_PATH_MAX]; |
| 1237 |
struct option longopts[] = { |
| 1238 |
{"log", required_argument, NULL, 1000}, |
| 1239 |
{"logfile", required_argument, NULL, 1001}, |
| 1240 |
{0, 0, NULL, 0}}; |
| 1241 |
int opt = 0; |
| 1242 |
int idx = 0; |
| 1243 |
while ((opt = getopt_long(argc, argv, "\0:\0", longopts, &idx)) != -1) |
| 1244 |
{ |
| 1245 |
switch (opt) |
| 1246 |
{ |
| 1247 |
case 1000: { |
| 1248 |
char *comma = strchr(optarg, ','); |
| 1249 |
if (comma) |
| 1250 |
{ |
| 1251 |
|
| 1252 |
|
| 1253 |
*comma = '\0'; |
| 1254 |
const LogLevel ll = StrLogLevel(comma + 1); |
| 1255 |
LogModuleSetLevel(StrLogModule(optarg), ll); |
| 1256 |
printf("Logging %s at %s\n", optarg, LogLevelName(ll)); |
| 1257 |
} |
| 1258 |
else |
| 1259 |
{ |
| 1260 |
|
| 1261 |
const LogLevel ll = StrLogLevel(optarg); |
| 1262 |
for (int i = 0; i < (int)LM_COUNT; i++) |
| 1263 |
{ |
| 1264 |
LogModuleSetLevel((LogModule)i, ll); |
| 1265 |
} |
| 1266 |
printf("Logging everything at %s\n", LogLevelName(ll)); |
| 1267 |
} |
| 1268 |
} |
| 1269 |
break; |
| 1270 |
case 1001: |
| 1271 |
LogOpenFile(optarg); |
| 1272 |
break; |
| 1273 |
default: |
| 1274 |
|
| 1275 |
break; |
| 1276 |
} |
| 1277 |
} |
| 1278 |
|
| 1279 |
LogInit(); |
| 1280 |
printf("C-Dogs SDL Editor\n"); |
| 1281 |
|
| 1282 |
if (SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO) != 0) |
| 1283 |
{ |
| 1284 |
printf("Failed to start SDL!\n"); |
| 1285 |
return -1; |
| 1286 |
} |
| 1287 |
|
| 1288 |
GetDataFilePath(buf, ""); |
| 1289 |
printf("Data directory:\t\t%s\n", buf); |
| 1290 |
printf("Config directory:\t%s\n\n", GetConfigFilePath("")); |
| 1291 |
|
| 1292 |
ec.g = &gGraphicsDevice; |
| 1293 |
ec.camera = svec2_zero(); |
| 1294 |
|
| 1295 |
EditorBrushInit(&brush); |
| 1296 |
ResetLastFile(lastFile); |
| 1297 |
|
| 1298 |
gConfig = ConfigLoad(GetConfigFilePath(CONFIG_FILE)); |
| 1299 |
PicManagerInit(&gPicManager); |
| 1300 |
|
| 1301 |
ConfigGet(&gConfig, "Graphics.ScaleFactor")->u.Int.Value = 2; |
| 1302 |
ConfigGet(&gConfig, "Graphics.ScaleMode")->u.Enum.Value = SCALE_MODE_NN; |
| 1303 |
ConfigGet(&gConfig, "Graphics.WindowWidth")->u.Int.Value = 800; |
| 1304 |
ConfigGet(&gConfig, "Graphics.WindowHeight")->u.Int.Value = 600; |
| 1305 |
ConfigGet(&gConfig, "Graphics.SecondWindow")->u.Bool.Value = false; |
| 1306 |
ConfigSetChanged(&gConfig); |
| 1307 |
GraphicsInit(ec.g, &gConfig); |
| 1308 |
ec.g->cachedConfig.IsEditor = true; |
| 1309 |
GraphicsInitialize(ec.g); |
| 1310 |
if (!ec.g->IsInitialized) |
| 1311 |
{ |
| 1312 |
printf("Video didn't init!\n"); |
| 1313 |
exit(EXIT_FAILURE); |
| 1314 |
} |
| 1315 |
FontLoadFromJSON(&gFont, "graphics/font.png", "graphics/font.json"); |
| 1316 |
PicManagerLoad(&gPicManager); |
| 1317 |
CharSpriteClassesInit(&gCharSpriteClasses); |
| 1318 |
|
| 1319 |
ParticleClassesInit(&gParticleClasses, "data/particles.json"); |
| 1320 |
AmmoInitialize(&gAmmo, "data/ammo.json"); |
| 1321 |
BulletAndWeaponInitialize( |
| 1322 |
&gBulletClasses, &gWeaponClasses, "data/bullets.json", |
| 1323 |
"data/guns.json"); |
| 1324 |
CharacterClassesInitialize( |
| 1325 |
&gCharacterClasses, "data/character_classes.json"); |
| 1326 |
PlayerTemplatesLoad(&gPlayerTemplates, &gCharacterClasses); |
| 1327 |
PickupClassesInit( |
| 1328 |
&gPickupClasses, "data/pickups.json", &gAmmo, &gWeaponClasses); |
| 1329 |
MapObjectsInit( |
| 1330 |
&gMapObjects, "data/map_objects.json", &gAmmo, &gWeaponClasses); |
| 1331 |
CollisionSystemInit(&gCollisionSystem); |
| 1332 |
MapWolfInit(); |
| 1333 |
CampaignInit(&gCampaign); |
| 1334 |
MissionInit(&lastMission); |
| 1335 |
MissionInit(¤tMission); |
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
sObjs = CreateMainObjs(&gCampaign, &brush, svec2i(320, 240)); |
| 1340 |
memset(&sDrawObjs, 0, sizeof sDrawObjs); |
| 1341 |
DrawBufferInit(&sDrawBuffer, svec2i(X_TILES, Y_TILES), &gGraphicsDevice); |
| 1342 |
|
| 1343 |
|
| 1344 |
CampaignSettingTerminateAll(&gCampaign.Setting); |
| 1345 |
CampaignSettingInit(&gCampaign.Setting); |
| 1346 |
|
| 1347 |
EventInit(&gEventHandlers); |
| 1348 |
|
| 1349 |
for (int i = 1; i < argc; i++) |
| 1350 |
{ |
| 1351 |
if (!loaded) |
| 1352 |
{ |
| 1353 |
RealPath(argv[i], lastFile); |
| 1354 |
if (MapNewLoad(lastFile, &gCampaign.Setting) == 0) |
| 1355 |
{ |
| 1356 |
loaded = 1; |
| 1357 |
ReloadUI(); |
| 1358 |
LOG(LM_EDIT, LL_INFO, "Loaded map %s", lastFile); |
| 1359 |
} |
| 1360 |
else |
| 1361 |
{ |
| 1362 |
ResetLastFile(lastFile); |
| 1363 |
} |
| 1364 |
} |
| 1365 |
} |
| 1366 |
|
| 1367 |
EditCampaign(); |
| 1368 |
|
| 1369 |
MapTerminate(&gMap); |
| 1370 |
MapObjectsTerminate(&gMapObjects); |
| 1371 |
PickupClassesTerminate(&gPickupClasses); |
| 1372 |
ParticleClassesTerminate(&gParticleClasses); |
| 1373 |
AmmoTerminate(&gAmmo); |
| 1374 |
WeaponClassesTerminate(&gWeaponClasses); |
| 1375 |
BulletTerminate(&gBulletClasses); |
| 1376 |
CharacterClassesTerminate(&gCharacterClasses); |
| 1377 |
MapWolfTerminate(); |
| 1378 |
CampaignTerminate(&gCampaign); |
| 1379 |
MissionTerminate(&lastMission); |
| 1380 |
MissionTerminate(¤tMission); |
| 1381 |
CollisionSystemTerminate(&gCollisionSystem); |
| 1382 |
|
| 1383 |
DrawBufferTerminate(&sDrawBuffer); |
| 1384 |
GraphicsTerminate(ec.g); |
| 1385 |
CharSpriteClassesTerminate(&gCharSpriteClasses); |
| 1386 |
PicManagerTerminate(&gPicManager); |
| 1387 |
FontTerminate(&gFont); |
| 1388 |
PlayerTemplatesTerminate(&gPlayerTemplates); |
| 1389 |
|
| 1390 |
UIObjectDestroy(sObjs); |
| 1391 |
CArrayTerminate(&sDrawObjs); |
| 1392 |
EditorBrushTerminate(&brush); |
| 1393 |
|
| 1394 |
ConfigDestroy(&gConfig); |
| 1395 |
LogTerminate(); |
| 1396 |
|
| 1397 |
SDL_Quit(); |
| 1398 |
|
| 1399 |
exit(EXIT_SUCCESS); |
| 1400 |
} |
| 1401 |
static void ResetLastFile(char *s) |
| 1402 |
{ |
| 1403 |
char buf[CDOGS_PATH_MAX]; |
| 1404 |
|
| 1405 |
GetDataFilePath(buf, CDOGS_CAMPAIGN_DIR); |
| 1406 |
RelPath(s, buf, "."); |
| 1407 |
strcat(s, "/"); |
| 1408 |
} |