| 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 |
#include "editor_brush.h" |
| 29 |
|
| 30 |
#include <assert.h> |
| 31 |
|
| 32 |
#include <cdogs/algorithms.h> |
| 33 |
#include <cdogs/map.h> |
| 34 |
#include <cdogs/map_build.h> |
| 35 |
#include <cdogs/mission_convert.h> |
| 36 |
|
| 37 |
void EditorBrushInit(EditorBrush *b) |
| 38 |
{ |
| 39 |
memset(b, 0, sizeof *b); |
| 40 |
b->Type = BRUSHTYPE_POINT; |
| 41 |
b->MainType = 0; |
| 42 |
b->SecondaryType = 1; |
| 43 |
b->PaintType = b->MainType; |
| 44 |
b->IsActive = 0; |
| 45 |
b->IsPainting = 0; |
| 46 |
b->BrushSize = 1; |
| 47 |
b->LastPos = svec2i(-1, -1); |
| 48 |
b->Pos = svec2i(-1, -1); |
| 49 |
b->GuideImageAlpha = 64; |
| 50 |
CArrayInit(&b->HighlightedTiles, sizeof(struct vec2i)); |
| 51 |
} |
| 52 |
void EditorBrushTerminate(EditorBrush *b) |
| 53 |
{ |
| 54 |
CArrayTerminate(&b->HighlightedTiles); |
| 55 |
PicFree(&b->GuideImagePic); |
| 56 |
} |
| 57 |
|
| 58 |
static void EditorBrushHighlightPoint(void *data, struct vec2i p) |
| 59 |
{ |
| 60 |
EditorBrush *b = data; |
| 61 |
struct vec2i v; |
| 62 |
for (v.y = 0; v.y < b->BrushSize; v.y++) |
| 63 |
{ |
| 64 |
for (v.x = 0; v.x < b->BrushSize; v.x++) |
| 65 |
{ |
| 66 |
struct vec2i pos = svec2i_add(p, v); |
| 67 |
CArrayPushBack(&b->HighlightedTiles, &pos); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
void EditorBrushSetHighlightedTiles(EditorBrush *b) |
| 72 |
{ |
| 73 |
bool useSimpleHighlight = true; |
| 74 |
switch (b->Type) |
| 75 |
{ |
| 76 |
case BRUSHTYPE_POINT: |
| 77 |
useSimpleHighlight = true; |
| 78 |
break; |
| 79 |
case BRUSHTYPE_LINE: |
| 80 |
if (b->IsPainting) |
| 81 |
{ |
| 82 |
useSimpleHighlight = 0; |
| 83 |
|
| 84 |
CArrayClear(&b->HighlightedTiles); |
| 85 |
AlgoLineDrawData data; |
| 86 |
data.Draw = EditorBrushHighlightPoint; |
| 87 |
data.data = b; |
| 88 |
BresenhamLineDraw(b->LastPos, b->Pos, &data); |
| 89 |
} |
| 90 |
break; |
| 91 |
case BRUSHTYPE_BOX: |
| 92 |
case BRUSHTYPE_BOX_AND_FILL: |
| 93 |
case BRUSHTYPE_SET_EXIT: |
| 94 |
if (b->IsPainting) |
| 95 |
{ |
| 96 |
struct vec2i v; |
| 97 |
struct vec2i d = svec2i( |
| 98 |
b->Pos.x > b->LastPos.x ? 1 : -1, |
| 99 |
b->Pos.y > b->LastPos.y ? 1 : -1); |
| 100 |
useSimpleHighlight = 0; |
| 101 |
CArrayClear(&b->HighlightedTiles); |
| 102 |
for (v.y = b->LastPos.y; v.y != b->Pos.y + d.y; v.y += d.y) |
| 103 |
{ |
| 104 |
for (v.x = b->LastPos.x; v.x != b->Pos.x + d.x; v.x += d.x) |
| 105 |
{ |
| 106 |
if (v.x == b->LastPos.x || v.x == b->Pos.x || |
| 107 |
v.y == b->LastPos.y || v.y == b->Pos.y) |
| 108 |
{ |
| 109 |
EditorBrushHighlightPoint(b, v); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
break; |
| 115 |
case BRUSHTYPE_BOX_FILLED: |
| 116 |
if (b->IsPainting) |
| 117 |
{ |
| 118 |
struct vec2i v; |
| 119 |
struct vec2i d = svec2i( |
| 120 |
b->Pos.x > b->LastPos.x ? 1 : -1, |
| 121 |
b->Pos.y > b->LastPos.y ? 1 : -1); |
| 122 |
useSimpleHighlight = 0; |
| 123 |
CArrayClear(&b->HighlightedTiles); |
| 124 |
for (v.y = b->LastPos.y; v.y != b->Pos.y + d.y; v.y += d.y) |
| 125 |
{ |
| 126 |
for (v.x = b->LastPos.x; v.x != b->Pos.x + d.x; v.x += d.x) |
| 127 |
{ |
| 128 |
EditorBrushHighlightPoint(b, v); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
break; |
| 133 |
case BRUSHTYPE_SELECT: |
| 134 |
if (b->IsPainting) |
| 135 |
{ |
| 136 |
if (b->IsMoving) |
| 137 |
{ |
| 138 |
struct vec2i v; |
| 139 |
struct vec2i offset = |
| 140 |
svec2i(b->Pos.x - b->DragPos.x, b->Pos.y - b->DragPos.y); |
| 141 |
useSimpleHighlight = 0; |
| 142 |
CArrayClear(&b->HighlightedTiles); |
| 143 |
for (v.y = 0; v.y < b->SelectionSize.y; v.y++) |
| 144 |
{ |
| 145 |
for (v.x = 0; v.x < b->SelectionSize.x; v.x++) |
| 146 |
{ |
| 147 |
struct vec2i vOffset = svec2i_add( |
| 148 |
svec2i_add(v, b->SelectionStart), offset); |
| 149 |
EditorBrushHighlightPoint(b, vOffset); |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
else |
| 154 |
{ |
| 155 |
|
| 156 |
struct vec2i v; |
| 157 |
struct vec2i d = svec2i( |
| 158 |
b->Pos.x > b->LastPos.x ? 1 : -1, |
| 159 |
b->Pos.y > b->LastPos.y ? 1 : -1); |
| 160 |
useSimpleHighlight = 0; |
| 161 |
CArrayClear(&b->HighlightedTiles); |
| 162 |
for (v.y = b->LastPos.y; v.y != b->Pos.y + d.y; v.y += d.y) |
| 163 |
{ |
| 164 |
for (v.x = b->LastPos.x; v.x != b->Pos.x + d.x; v.x += d.x) |
| 165 |
{ |
| 166 |
EditorBrushHighlightPoint(b, v); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
else |
| 172 |
{ |
| 173 |
|
| 174 |
if (b->SelectionSize.x > 0 && b->SelectionSize.y > 0) |
| 175 |
{ |
| 176 |
struct vec2i v; |
| 177 |
useSimpleHighlight = 0; |
| 178 |
CArrayClear(&b->HighlightedTiles); |
| 179 |
for (v.y = 0; v.y < b->SelectionSize.y; v.y++) |
| 180 |
{ |
| 181 |
for (v.x = 0; v.x < b->SelectionSize.x; v.x++) |
| 182 |
{ |
| 183 |
struct vec2i vOffset = |
| 184 |
svec2i_add(v, b->SelectionStart); |
| 185 |
EditorBrushHighlightPoint(b, vOffset); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
default: |
| 191 |
|
| 192 |
break; |
| 193 |
} |
| 194 |
if (useSimpleHighlight) |
| 195 |
{ |
| 196 |
|
| 197 |
CArrayClear(&b->HighlightedTiles); |
| 198 |
EditorBrushHighlightPoint(b, b->Pos); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
static void SetTile( |
| 203 |
MapBuilder *mb, Mission *m, const struct vec2i pos, const int tile) |
| 204 |
{ |
| 205 |
if (MissionStaticTrySetTile(&m->u.Static, mb->Map->Size, pos, tile)) |
| 206 |
{ |
| 207 |
const TileClass *tc = MissionStaticGetTileClass( |
| 208 |
&mb->mission->u.Static, mb->Map->Size, pos); |
| 209 |
if (tc != NULL) |
| 210 |
{ |
| 211 |
MapBuildTile(mb, pos, tc); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
typedef struct |
| 217 |
{ |
| 218 |
EditorBrush *brush; |
| 219 |
Mission *mission; |
| 220 |
} EditorBrushPaintTilesAtData; |
| 221 |
static void EditorBrushPaintTilesAt(void *data, struct vec2i pos) |
| 222 |
{ |
| 223 |
struct vec2i v; |
| 224 |
EditorBrushPaintTilesAtData *paintData = data; |
| 225 |
EditorBrush *b = paintData->brush; |
| 226 |
Mission *m = paintData->mission; |
| 227 |
MapBuilder mb; |
| 228 |
MapBuilderInit(&mb, &gMap, m, GAME_MODE_NORMAL, NULL); |
| 229 |
for (v.y = 0; v.y < b->BrushSize; v.y++) |
| 230 |
{ |
| 231 |
for (v.x = 0; v.x < b->BrushSize; v.x++) |
| 232 |
{ |
| 233 |
SetTile(&mb, m, svec2i_add(pos, v), b->PaintType); |
| 234 |
} |
| 235 |
} |
| 236 |
MapBuilderTerminate(&mb); |
| 237 |
} |
| 238 |
static void EditorBrushPaintLine(EditorBrush *b, Mission *m) |
| 239 |
{ |
| 240 |
|
| 241 |
EditorBrushPaintTilesAtData paintData; |
| 242 |
paintData.brush = b; |
| 243 |
paintData.mission = m; |
| 244 |
if (b->IsPainting) |
| 245 |
{ |
| 246 |
AlgoLineDrawData data; |
| 247 |
data.Draw = EditorBrushPaintTilesAt; |
| 248 |
data.data = &paintData; |
| 249 |
BresenhamLineDraw(b->LastPos, b->Pos, &data); |
| 250 |
} |
| 251 |
EditorBrushPaintTilesAt(&paintData, b->Pos); |
| 252 |
b->IsPainting = 1; |
| 253 |
b->LastPos = b->Pos; |
| 254 |
} |
| 255 |
typedef struct |
| 256 |
{ |
| 257 |
MapBuilder mb; |
| 258 |
Mission *m; |
| 259 |
int fromType; |
| 260 |
int toType; |
| 261 |
} PaintFloodFillData; |
| 262 |
static void MissionFillTile(void *data, const struct vec2i v); |
| 263 |
static bool MissionIsTileSame(void *data, const struct vec2i v); |
| 264 |
EditorResult EditorBrushStartPainting(EditorBrush *b, Mission *m, int isMain) |
| 265 |
{ |
| 266 |
if (!b->IsPainting) |
| 267 |
{ |
| 268 |
b->LastPos = b->Pos; |
| 269 |
} |
| 270 |
b->PaintType = isMain ? b->MainType : b->SecondaryType; |
| 271 |
switch (b->Type) |
| 272 |
{ |
| 273 |
case BRUSHTYPE_POINT: |
| 274 |
b->IsPainting = true; |
| 275 |
EditorBrushPaintLine(b, m); |
| 276 |
return EDITOR_RESULT_CHANGED; |
| 277 |
case BRUSHTYPE_LINE: |
| 278 |
case BRUSHTYPE_BOX: |
| 279 |
case BRUSHTYPE_BOX_FILLED: |
| 280 |
case BRUSHTYPE_BOX_AND_FILL: |
| 281 |
case BRUSHTYPE_SET_EXIT: |
| 282 |
|
| 283 |
break; |
| 284 |
case BRUSHTYPE_SELECT: |
| 285 |
|
| 286 |
if (!b->IsPainting) |
| 287 |
{ |
| 288 |
if (!b->IsMoving) |
| 289 |
{ |
| 290 |
|
| 291 |
|
| 292 |
if (b->Pos.x >= b->SelectionStart.x && |
| 293 |
b->Pos.y >= b->SelectionStart.y && |
| 294 |
b->Pos.x < b->SelectionStart.x + b->SelectionSize.x && |
| 295 |
b->Pos.y < b->SelectionStart.y + b->SelectionSize.y) |
| 296 |
{ |
| 297 |
b->IsMoving = 1; |
| 298 |
b->DragPos = b->Pos; |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
break; |
| 303 |
case BRUSHTYPE_FILL: |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
if (MissionStaticIdTileClass(&m->u.Static, b->PaintType)->Type == |
| 309 |
TILE_CLASS_DOOR) |
| 310 |
{ |
| 311 |
break; |
| 312 |
} |
| 313 |
{ |
| 314 |
const int tile = |
| 315 |
MissionStaticGetTile(&m->u.Static, m->Size, b->Pos); |
| 316 |
if (tile == b->PaintType) |
| 317 |
{ |
| 318 |
break; |
| 319 |
} |
| 320 |
FloodFillData data; |
| 321 |
data.Fill = MissionFillTile; |
| 322 |
data.IsSame = MissionIsTileSame; |
| 323 |
PaintFloodFillData pData; |
| 324 |
MapBuilderInit(&pData.mb, &gMap, m, GAME_MODE_NORMAL, NULL); |
| 325 |
pData.m = m; |
| 326 |
pData.fromType = tile; |
| 327 |
pData.toType = b->PaintType; |
| 328 |
data.data = &pData; |
| 329 |
const bool flooded = CFloodFill(b->Pos, &data); |
| 330 |
MapBuilderTerminate(&pData.mb); |
| 331 |
if (!flooded) |
| 332 |
{ |
| 333 |
break; |
| 334 |
} |
| 335 |
return EDITOR_RESULT_CHANGED; |
| 336 |
} |
| 337 |
case BRUSHTYPE_SET_PLAYER_START: |
| 338 |
if (TileIsClear(MapGetTile(&gMap, b->Pos))) |
| 339 |
{ |
| 340 |
m->u.Static.Start = b->Pos; |
| 341 |
return EDITOR_RESULT_CHANGED; |
| 342 |
} |
| 343 |
break; |
| 344 |
case BRUSHTYPE_ADD_ITEM: |
| 345 |
if (isMain) |
| 346 |
{ |
| 347 |
const Tile *tile = MapGetTile(&gMap, b->Pos); |
| 348 |
const Tile *tileAbove = |
| 349 |
MapGetTile(&gMap, svec2i(b->Pos.x, b->Pos.y - 1)); |
| 350 |
|
| 351 |
if (MapObjectIsTileOK(b->u.MapObject, tile, tileAbove) && |
| 352 |
MissionStaticTryAddItem(&m->u.Static, b->u.MapObject, b->Pos)) |
| 353 |
{ |
| 354 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 355 |
} |
| 356 |
} |
| 357 |
else |
| 358 |
{ |
| 359 |
if (MissionStaticTryRemoveItemAt(&m->u.Static, b->Pos)) |
| 360 |
{ |
| 361 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 362 |
} |
| 363 |
} |
| 364 |
break; |
| 365 |
case BRUSHTYPE_ADD_CHARACTER: |
| 366 |
if (isMain) |
| 367 |
{ |
| 368 |
const Tile *tile = MapGetTile(&gMap, b->Pos); |
| 369 |
if (TileIsClear(tile)) |
| 370 |
{ |
| 371 |
CharacterPlace cp = {b->Pos, DIRECTION_DOWN}; |
| 372 |
MissionStaticAddCharacter(&m->u.Static, b->u.ItemIndex, cp); |
| 373 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 374 |
} |
| 375 |
else if (MissionStaticTryRotateCharacterAt(&m->u.Static, b->Pos)) |
| 376 |
{ |
| 377 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 378 |
} |
| 379 |
} |
| 380 |
else |
| 381 |
{ |
| 382 |
if (MissionStaticTryRemoveCharacterAt(&m->u.Static, b->Pos)) |
| 383 |
{ |
| 384 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 385 |
} |
| 386 |
} |
| 387 |
break; |
| 388 |
case BRUSHTYPE_ADD_OBJECTIVE: |
| 389 |
if (isMain) |
| 390 |
{ |
| 391 |
MissionStaticAddObjective( |
| 392 |
m, &m->u.Static, b->u.ItemIndex, b->Index2, b->Pos, false); |
| 393 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 394 |
} |
| 395 |
else |
| 396 |
{ |
| 397 |
if (MissionStaticTryRemoveObjective(m, &m->u.Static, b->Pos)) |
| 398 |
{ |
| 399 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 400 |
} |
| 401 |
} |
| 402 |
break; |
| 403 |
case BRUSHTYPE_ADD_KEY: |
| 404 |
if (isMain) |
| 405 |
{ |
| 406 |
const Tile *tile = MapGetTile(&gMap, b->Pos); |
| 407 |
if (TileIsClear(tile)) |
| 408 |
{ |
| 409 |
MissionStaticAddKey(&m->u.Static, b->u.ItemIndex, b->Pos); |
| 410 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 411 |
} |
| 412 |
} |
| 413 |
else |
| 414 |
{ |
| 415 |
if (MissionStaticTryRemoveKeyAt(&m->u.Static, b->Pos)) |
| 416 |
{ |
| 417 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 418 |
} |
| 419 |
} |
| 420 |
break; |
| 421 |
case BRUSHTYPE_ADD_PICKUP: |
| 422 |
if (isMain) |
| 423 |
{ |
| 424 |
const Tile *tile = MapGetTile(&gMap, b->Pos); |
| 425 |
if (TileCanWalk(tile) && |
| 426 |
MissionStaticTryAddPickup(&m->u.Static, b->u.Pickup, b->Pos)) |
| 427 |
{ |
| 428 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 429 |
} |
| 430 |
} |
| 431 |
else |
| 432 |
{ |
| 433 |
if (MissionStaticTryRemovePickupAt(&m->u.Static, b->Pos)) |
| 434 |
{ |
| 435 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 436 |
} |
| 437 |
} |
| 438 |
break; |
| 439 |
case BRUSHTYPE_SET_KEY: |
| 440 |
if (isMain) |
| 441 |
{ |
| 442 |
if (MissionStaticTrySetKey( |
| 443 |
&m->u.Static, b->u.ItemIndex, m->Size, b->Pos)) |
| 444 |
{ |
| 445 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 446 |
} |
| 447 |
} |
| 448 |
else |
| 449 |
{ |
| 450 |
if (MissionStaticTryUnsetKeyAt(&m->u.Static, m->Size, b->Pos)) |
| 451 |
{ |
| 452 |
return EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 453 |
} |
| 454 |
} |
| 455 |
break; |
| 456 |
default: |
| 457 |
CASSERT(false, "unknown brush type"); |
| 458 |
break; |
| 459 |
} |
| 460 |
b->IsPainting = 1; |
| 461 |
return EDITOR_RESULT_NONE; |
| 462 |
} |
| 463 |
static void MissionFillTile(void *data, const struct vec2i v) |
| 464 |
{ |
| 465 |
PaintFloodFillData *pData = data; |
| 466 |
SetTile(&pData->mb, pData->m, v, pData->toType); |
| 467 |
} |
| 468 |
static bool MissionIsTileSame(void *data, const struct vec2i v) |
| 469 |
{ |
| 470 |
PaintFloodFillData *pData = data; |
| 471 |
if (!Rect2iIsInside(Rect2iNew(svec2i_zero(), pData->mb.Map->Size), v)) |
| 472 |
{ |
| 473 |
return false; |
| 474 |
} |
| 475 |
return MissionStaticGetTile( |
| 476 |
&pData->mb.mission->u.Static, pData->mb.Map->Size, v) == |
| 477 |
pData->fromType; |
| 478 |
} |
| 479 |
static void EditorBrushPaintBox( |
| 480 |
EditorBrush *b, Mission *m, const int lineType, const int fillType) |
| 481 |
{ |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
struct vec2i v; |
| 487 |
struct vec2i d = svec2i( |
| 488 |
b->Pos.x > b->LastPos.x ? 1 : -1, b->Pos.y > b->LastPos.y ? 1 : -1); |
| 489 |
EditorBrushPaintTilesAtData paintData; |
| 490 |
paintData.brush = b; |
| 491 |
paintData.mission = m; |
| 492 |
|
| 493 |
if (fillType >= 0) |
| 494 |
{ |
| 495 |
b->PaintType = fillType; |
| 496 |
for (v.y = b->LastPos.y; v.y != b->Pos.y + d.y; v.y += d.y) |
| 497 |
{ |
| 498 |
for (v.x = b->LastPos.x; v.x != b->Pos.x + d.x; v.x += d.x) |
| 499 |
{ |
| 500 |
if (v.x != b->LastPos.x && v.x != b->Pos.x && |
| 501 |
v.y != b->LastPos.y && v.y != b->Pos.y) |
| 502 |
{ |
| 503 |
EditorBrushPaintTilesAt(&paintData, v); |
| 504 |
} |
| 505 |
} |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
if (lineType >= 0) |
| 510 |
{ |
| 511 |
b->PaintType = lineType; |
| 512 |
for (v.y = b->LastPos.y; v.y != b->Pos.y + d.y; v.y += d.y) |
| 513 |
{ |
| 514 |
for (v.x = b->LastPos.x; v.x != b->Pos.x + d.x; v.x += d.x) |
| 515 |
{ |
| 516 |
if (v.x == b->LastPos.x || v.x == b->Pos.x || |
| 517 |
v.y == b->LastPos.y || v.y == b->Pos.y) |
| 518 |
{ |
| 519 |
EditorBrushPaintTilesAt(&paintData, v); |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
EditorResult EditorBrushStopPainting(EditorBrush *b, Mission *m) |
| 526 |
{ |
| 527 |
EditorResult result = EDITOR_RESULT_NONE; |
| 528 |
if (b->IsPainting) |
| 529 |
{ |
| 530 |
switch (b->Type) |
| 531 |
{ |
| 532 |
case BRUSHTYPE_LINE: |
| 533 |
EditorBrushPaintLine(b, m); |
| 534 |
result = EDITOR_RESULT_CHANGED; |
| 535 |
break; |
| 536 |
case BRUSHTYPE_BOX: |
| 537 |
EditorBrushPaintBox(b, m, b->PaintType, -1); |
| 538 |
result = EDITOR_RESULT_CHANGED; |
| 539 |
break; |
| 540 |
case BRUSHTYPE_BOX_FILLED: |
| 541 |
EditorBrushPaintBox(b, m, b->PaintType, b->PaintType); |
| 542 |
result = EDITOR_RESULT_CHANGED; |
| 543 |
break; |
| 544 |
case BRUSHTYPE_BOX_AND_FILL: |
| 545 |
EditorBrushPaintBox(b, m, b->MainType, b->SecondaryType); |
| 546 |
result = EDITOR_RESULT_CHANGED; |
| 547 |
break; |
| 548 |
case BRUSHTYPE_SELECT: |
| 549 |
if (b->IsMoving) |
| 550 |
{ |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
CArray movedTiles; |
| 555 |
int delta; |
| 556 |
CArrayInit(&movedTiles, m->u.Static.Tiles.elemSize); |
| 557 |
|
| 558 |
|
| 559 |
RECT_FOREACH(Rect2iNew(b->SelectionStart, b->SelectionSize)) |
| 560 |
const int tile = |
| 561 |
MissionStaticGetTile(&m->u.Static, m->Size, _v); |
| 562 |
CArrayPushBack(&movedTiles, &tile); |
| 563 |
MissionStaticClearTile(&m->u.Static, m->Size, _v); |
| 564 |
RECT_FOREACH_END() |
| 565 |
|
| 566 |
b->SelectionStart.x += b->Pos.x - b->DragPos.x; |
| 567 |
b->SelectionStart.y += b->Pos.y - b->DragPos.y; |
| 568 |
|
| 569 |
|
| 570 |
RECT_FOREACH(Rect2iNew(b->SelectionStart, b->SelectionSize)) |
| 571 |
if (_v.x >= 0 && _v.x < m->Size.x && _v.y >= 0 && |
| 572 |
_v.y < m->Size.y) |
| 573 |
{ |
| 574 |
const int tile = *(int *)CArrayGet(&movedTiles, _i); |
| 575 |
MissionStaticTrySetTile(&m->u.Static, m->Size, _v, tile); |
| 576 |
result = EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 577 |
} |
| 578 |
RECT_FOREACH_END() |
| 579 |
CArrayTerminate(&movedTiles); |
| 580 |
|
| 581 |
delta = -b->SelectionStart.x; |
| 582 |
if (delta > 0) |
| 583 |
{ |
| 584 |
b->SelectionStart.x += delta; |
| 585 |
b->SelectionSize.x -= delta; |
| 586 |
} |
| 587 |
delta = -b->SelectionStart.y; |
| 588 |
if (delta > 0) |
| 589 |
{ |
| 590 |
b->SelectionStart.y += delta; |
| 591 |
b->SelectionSize.y -= delta; |
| 592 |
} |
| 593 |
delta = b->SelectionStart.x + b->SelectionSize.x - m->Size.x; |
| 594 |
if (delta > 0) |
| 595 |
{ |
| 596 |
b->SelectionSize.x -= delta; |
| 597 |
} |
| 598 |
delta = b->SelectionStart.y + b->SelectionSize.y - m->Size.y; |
| 599 |
if (delta > 0) |
| 600 |
{ |
| 601 |
b->SelectionSize.y -= delta; |
| 602 |
} |
| 603 |
|
| 604 |
if (b->SelectionSize.x < 0 || b->SelectionSize.y < 0) |
| 605 |
{ |
| 606 |
b->SelectionSize = svec2i_zero(); |
| 607 |
} |
| 608 |
|
| 609 |
b->IsMoving = 0; |
| 610 |
} |
| 611 |
else |
| 612 |
{ |
| 613 |
|
| 614 |
b->SelectionStart = svec2i_min(b->LastPos, b->Pos); |
| 615 |
b->SelectionSize.x = abs(b->LastPos.x - b->Pos.x) + 1; |
| 616 |
b->SelectionSize.y = abs(b->LastPos.y - b->Pos.y) + 1; |
| 617 |
|
| 618 |
if (b->SelectionSize.x <= 1 && b->SelectionSize.y <= 1) |
| 619 |
{ |
| 620 |
b->SelectionSize = svec2i_zero(); |
| 621 |
} |
| 622 |
} |
| 623 |
break; |
| 624 |
case BRUSHTYPE_SET_EXIT: { |
| 625 |
struct vec2i exitStart = svec2i_min(b->LastPos, b->Pos); |
| 626 |
struct vec2i exitEnd = svec2i_max(b->LastPos, b->Pos); |
| 627 |
|
| 628 |
exitStart = svec2i_clamp( |
| 629 |
exitStart, svec2i_zero(), |
| 630 |
svec2i_subtract(m->Size, svec2i_one())); |
| 631 |
exitEnd = svec2i_clamp( |
| 632 |
exitEnd, svec2i_zero(), |
| 633 |
svec2i_subtract(m->Size, svec2i_one())); |
| 634 |
|
| 635 |
const struct vec2i size = svec2i_subtract(exitEnd, exitStart); |
| 636 |
if (size.x < 2 || size.y < 2) |
| 637 |
{ |
| 638 |
break; |
| 639 |
} |
| 640 |
|
| 641 |
Exit *exit = CArrayGet(&m->u.Static.Exits, b->u.ItemIndex); |
| 642 |
if (svec2i_is_equal(exitStart, exit->R.Pos) && |
| 643 |
svec2i_is_equal(size, exit->R.Size)) |
| 644 |
{ |
| 645 |
break; |
| 646 |
} |
| 647 |
exit->R.Pos = exitStart; |
| 648 |
exit->R.Size = size; |
| 649 |
result = EDITOR_RESULT_CHANGED_AND_RELOAD; |
| 650 |
} |
| 651 |
break; |
| 652 |
default: |
| 653 |
|
| 654 |
break; |
| 655 |
} |
| 656 |
} |
| 657 |
b->IsPainting = 0; |
| 658 |
CArrayClear(&b->HighlightedTiles); |
| 659 |
return result; |
| 660 |
} |
| 661 |
|
| 662 |
bool EditorBrushTryLoadGuideImage(EditorBrush *b, const char *filename) |
| 663 |
{ |
| 664 |
PicFree(&b->GuideImagePic); |
| 665 |
SDL_Surface *s = LoadImgToSurface(filename); |
| 666 |
if (s == NULL) |
| 667 |
return false; |
| 668 |
SDL_Surface *sc = SDL_ConvertSurface(s, gGraphicsDevice.Format, 0); |
| 669 |
SDL_FreeSurface(s); |
| 670 |
PicLoad(&b->GuideImagePic, svec2i(sc->w, sc->h), svec2i_zero(), sc, false); |
| 671 |
SDL_FreeSurface(sc); |
| 672 |
return true; |
| 673 |
} |