fix bug in paste to protected layer;
Before adoption: this commit came in with the history the project adopted, so no landing records it.
add eraser tool
authored by Sean Barrett · committed by Sean Barrett · parent 655d245 · Browse files · 1 file·+63 added−26 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+63 added−26 removed
- other:+63 added−26 removed
Provenance
- Commit
3ab22d3- Tree
7716a9e- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
655d245
1 file changed+63 added−26 removed
modified:stb_tilemap_editor.h+63 added-26 removed
261 unmodified lines262 #if STBTE_UNDO_BUFFER_COUNT & (STBTE_UNDO_BUFFER_COUNT-1)263 #error "Undo buffer size must be a power of 2"264 #endif265 266-#define STBTE_COLOR_TOOLBAR_BACKGROUND 0x606060266+#define STBTE_COLOR_TOOLBAR_BACKGROUND 0x606060 // stbte__color[STBTE__ctoolbar][STBTE__base][STBTE__idle]267 #define STBTE_COLOR_TILEMAP_BACKGROUND 0x000000268 #define STBTE_COLOR_TILEMAP_BORDER 0x203060269 #define STBTE_COLOR_TILEMAP_HIGHLIGHT 0xffffff270 #define STBTE_COLOR_PANEL_BACKGROUND 0x403010106 unmodified lines377 enum378 {379 STBTE__tool_select,380 STBTE__tool_brush,381+ STBTE__tool_erase,382 STBTE__tool_rect,383 STBTE__tool_eyedrop,384 STBTE__tool_fill,385 5 unmodified lines391 STBTE__num_tool,392 };393 394 // icons are stored in the 0-31 range of ASCII in the font394-static int toolchar[] = { 26,24,20,23,22, 19,29,28, };395+static int toolchar[] = { 26,24,25,20,23,22, 19,29,28, };396 397 enum398 {399 STBTE__paint,1040 unmodified lines1440 stbte__ui.alert_msg = msg;1441 stbte__ui.alert_timer = 3;1442 }1443 1444+#define STBTE__BG(tm,layer) ((layer) == 0 ? (tm)->background_tile : STBTE__NO_TILE)1445+1446+1447+1448 static void stbte__brush_predict(stbte_tilemap *tm, short result[])1449 {1450 int layer_to_paint = tm->cur_layer;1451 stbte__tileinfo *ti;9 unmodified lines1461 if (!(ti->layermask & (1 << i)))1462 continue;1463 1464 if (i != tm->solo_layer) {1460- short bg;1461-1465 // if there's a selected layer, can only paint on that1466 if (tm->cur_layer >= 0 && i != tm->cur_layer)1467 continue;1468 4 unmodified lines1473 // if the layer is locked, we can't write to it1474 if (tm->layerinfo[i].locked == STBTE__locked)1475 continue;1476 1474- bg = i == 0 ? tm->background_tile : STBTE__NO_TILE;1477 // if the layer is non-empty and protected, can't write to it1476- if (tm->layerinfo[i].locked == STBTE__protected && result[i] != bg)1478+ if (tm->layerinfo[i].locked == STBTE__protected && result[i] != STBTE__BG(tm,i))1479 continue;1480 }1481 1482 result[i] = ti->id;18 unmodified lines1501 if (!(ti->layermask & (1 << i)))1502 continue;1503 1504 if (i != tm->solo_layer) {1503- short bg;1504-1505 // if there's a selected layer, can only paint on that1506 if (tm->cur_layer >= 0 && i != tm->cur_layer)1507 continue;1508 4 unmodified lines1513 // if the layer is locked, we can't write to it1514 if (tm->layerinfo[i].locked == STBTE__locked)1515 continue;1516 1517- bg = i == 0 ? tm->background_tile : STBTE__NO_TILE;1517 // if the layer is non-empty and protected, can't write to it1519- if (tm->layerinfo[i].locked == STBTE__protected && tm->data[y][x][i] != bg)1518+ if (tm->layerinfo[i].locked == STBTE__protected && tm->data[y][x][i] != STBTE__BG(tm,i))1519 continue;1520 }1521 1522 stbte__undo_record(tm,x,y,i,tm->data[y][x][i]);8 unmodified lines1531 {1532 STBTE__erase_none = -1,1533 STBTE__erase_brushonly = 0,1534 STBTE__erase_any = 1,1535+ STBTE__erase_all = 2,1536 };1537 1538 static int stbte__erase_predict(stbte_tilemap *tm, short result[], int allow_any)1539 {30 unmodified lines1570 }1571 1572 // if multiple layers are legit, first scan all for brush data1573 1574- if (ti) {1574+ if (ti && allow_any != STBTE__erase_all) {1575 for (i=tm->num_layers-1; i >= 0; --i) {1576 if (result[i] != ti->id)1577 continue;1578 if (tm->layerinfo[i].locked || tm->layerinfo[i].hidden)1579 continue;1580 if (i == 0 && result[i] == tm->background_tile)1581 return STBTE__erase_none;1582- result[i] = (i == 0 ? tm->background_tile : STBTE__NO_TILE);1582+ result[i] = STBTE__BG(tm,i);1583 return STBTE__erase_brushonly;1584 }1585 }1586 7 unmodified lines1594 if (tm->layerinfo[i].locked || tm->layerinfo[i].hidden)1595 continue;1596 if (i == 0 && result[i] == tm->background_tile)1597 return STBTE__erase_none;1598- result[i] = (i == 0 ? tm->background_tile : STBTE__NO_TILE);1599- return STBTE__erase_any;1598+ result[i] = STBTE__BG(tm,i);1599+ if (allow_any != STBTE__erase_all)1600+ return STBTE__erase_any;1601 }1602 1603+ if (allow_any == STBTE__erase_all)1604+ return allow_any;1605 return STBTE__erase_none;1606 }1607 1608 34 unmodified lines1643 }1644 1645 // if multiple layers are legit, first scan all for brush data1646 1644- if (ti) {1647+ if (ti && allow_any != STBTE__erase_all) {1648 for (i=tm->num_layers-1; i >= 0; --i) {1649 if (tm->data[y][x][i] != ti->id)1650 continue;1651 if (tm->layerinfo[i].locked || tm->layerinfo[i].hidden)1652 continue;1653 if (i == 0 && tm->data[y][x][i] == tm->background_tile)1654 return STBTE__erase_none;1655 stbte__undo_record(tm,x,y,i,tm->data[y][x][i]);1653- tm->data[y][x][i] = (i == 0 ? tm->background_tile : STBTE__NO_TILE);1656+ tm->data[y][x][i] = STBTE__BG(tm,i);1657 return STBTE__erase_brushonly;1658 }1659 }1660 8 unmodified lines1669 continue;1670 if (i == 0 && tm->data[y][x][i] == tm->background_tile)1671 return STBTE__erase_none;1672 stbte__undo_record(tm,x,y,i,tm->data[y][x][i]);1670- tm->data[y][x][i] = (i == 0 ? tm->background_tile : STBTE__NO_TILE);1671- return STBTE__erase_any;1673+ tm->data[y][x][i] = STBTE__BG(tm,i);1674+ if (allow_any != STBTE__erase_all)1675+ return STBTE__erase_any;1676 }1673-1677+ if (allow_any == STBTE__erase_all)1678+ return allow_any;1679 return STBTE__erase_none;1680 }1681 1682 static int stbte__find_tile(stbte_tilemap *tm, int tile_id)56 unmodified lines1739 if (tm->solo_layer < 0) {1740 // check that we're allowed to write to it1741 if (tm->layerinfo[i].hidden) return;1742 if (tm->layerinfo[i].locked == STBTE__locked) return;1738- // if dragging w/o copy, we have to be allowed to erase1743+ // if protected, dest has to be empty1744+ if (tm->layerinfo[i].locked == STBTE__protected && dest[i] != STBTE__BG(tm,i)) return;1745+ // if dragging w/o copy, we will try to erase stuff, which protection disallows1746 if (dragging && tm->layerinfo[i].locked == STBTE__protected)1747 return;1748 }1749 result[i] = dest[i];1743- if (src[i] != STBTE__NO_TILE)1750+ if (src[i] != STBTE__BG(tm,i))1751 result[i] = src[i];1752 return;1753 }1754 1755 for (i=0; i < tm->num_layers; ++i) {1756 result[i] = dest[i];1757 if (src[i] != STBTE__NO_TILE) {1758 if (!tm->layerinfo[i].hidden && tm->layerinfo[i].locked != STBTE__locked)1752- if (!dragging || tm->layerinfo[i].locked == STBTE__unlocked)1759+ if (tm->layerinfo[i].locked == STBTE__unlocked || (!dragging && dest[i] == STBTE__BG(tm,i)))1760 result[i] = src[i];1761 }1762 }1763 }375 unmodified lines2139 case STBTE__rightdown:2140 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {2141 stbte__activate(id);2142 stbte__begin_undo(tm);2136- stbte__ui.brush_state = stbte__erase(tm, mapx, mapy, 1);2143+ if (stbte__erase(tm, mapx, mapy, STBTE__erase_any) == STBTE__erase_brushonly)2144+ stbte__ui.brush_state = STBTE__erase_brushonly;2145+ else2146+ stbte__ui.brush_state = STBTE__erase_any;2147 }2148 break;2149 case STBTE__leftup:2150 case STBTE__rightup:4 unmodified lines2155 break;2156 }2157 break;2158 2159+ case STBTE__tool_erase:2160+ switch (stbte__ui.event) {2161+ case STBTE__mousemove:2162+ if (STBTE__IS_MAP_ACTIVE() && over)2163+ stbte__erase(tm, mapx, mapy, STBTE__erase_all);2164+ break;2165+ case STBTE__leftdown:2166+ if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {2167+ stbte__activate(id);2168+ stbte__begin_undo(tm);2169+ stbte__erase(tm, mapx, mapy, STBTE__erase_all);2170+ }2171+ break;2172+ case STBTE__leftup:2173+ if (STBTE__IS_MAP_ACTIVE()) {2174+ stbte__end_undo(tm);2175+ stbte__activate(0);2176+ }2177+ break;2178+ }2179+ break;2180+2181 case STBTE__tool_select:2182 if (STBTE__IS_HOT(id)) {2183 switch (stbte__ui.event) {2184 case STBTE__leftdown:83 unmodified lines2268 2269 for (i=0; i < STBTE__num_tool; ++i) {2270 int highlight=0;2271 highlight = (stbte__ui.tool == i);2272+ if (i == STBTE__tool_undo || i == STBTE__tool_grid)2273+ x += 8;2274 if (i == STBTE__tool_grid && stbte__ui.show_grid)2275 highlight=1;2276 if (i == STBTE__tool_fill)2277 continue;17 unmodified lines2295 break;2296 }2297 }2298 x += 13;2265- if (i+1 == STBTE__tool_undo || i+1 == STBTE__tool_grid)2266- x += 8;2299 }2300 2301 x += 8;2302 if (stbte__button("cut" , x, y,10, 40, STBTE__ID(STBTE__toolbarB,0), 0)) {155 unmodified lines2458 {2459 int i,x,y;2460 int num_vis_rows = (h-6) / tm->palette_spacing_y;2461 int num_columns = (w-2-6) / tm->palette_spacing_x;2430- int num_total_rows = (tm->cur_palette_count + num_columns-1) / num_columns; // ceil()2462+ int num_total_rows;2463 int column,row;2464 int x1 = x0+w, y1=y0+h;2465 x = x0+2;2466 y = y0+6;2467 2468+ if (num_columns == 0)2469+ return;2470+2471+ num_total_rows = (tm->cur_palette_count + num_columns-1) / num_columns; // ceil()2472+2473 column = 0;2474 row = -tm->palette_scroll; 2475 for (i=0; i < tm->num_tiles; ++i) {2476 stbte__tileinfo *t = &tm->tiles[i];294 unmodified lines