various updates
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Sean Barrett · committed by Sean Barrett · parent c6c767f · Browse files · 2 files·+263 added−136 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+263 added−136 removed
- other:+263 added−136 removed
Provenance
- Commit
2b5d5d9- Tree
d939e0e- Author
Sean Barrett <sean2@nothings.org>- Committer
Sean Barrett <sean2@nothings.org>- Parent
c6c767f
2 files changed+263 added−136 removed
added:data/atari_8bit_font_revised.pngbinary
A binary file, added.
modified:stb_tilemap_editor.h+263 added-136 removed
3 unmodified lines4 // Embeddable tilemap editor for C/C++5 //6 //7 // REVISION HISTORY8-// 0.30 properties9-// - 10-// 0.20 initial release 8+// 0.30 properties release9+// - properties panel for editing user-defined "object" properties10+// - can link each tile to one other tile11+// - keyboard interface12+// - fix eraser tool bug (worked in complex cases, failed in simple)13+// - undo/redo tools have visible disabled state14+// - tiles on higher layers draw on top of adjacent lower-layer tiles15+// 0.20 erasable16+// - eraser tool17+// - fix bug when pasting into protected layer18+// - better color scheme19+// - internal-use color picker20+// 0.10 initial release 21 //22+//23 // COMPILING24 //25 // This header file contains both the header file and the26 // implementation file in one. To create the implementation,16-// in one source file, define a few symbols first and then27+// in one source file define a few symbols first and then28 // include this header:29 //30 // #define STB_TILEMAP_EDITOR_IMPLEMENTATION31 // // this triggers the implementation12 unmodified lines44 //45 // #include "stb_tilemap_editor.h"46 //47 // Optionally you can define the following functions before the include;37-// note these must be macros (which can just call a single function) so48+// note these must be macros (but they can just call a function) so49 // this library can #ifdef to detect if you've defined them:50 //51 // #define STBTE_PROP_TYPE(int n, short *tiledata, float *params) ...52 // // Returns the type of the n'th property of a given tile, which4 unmodified lines57 // // STBTE_PROP_bool /* uses a checkbox to change value */58 // // And you can bitwise-OR in the following flags:59 // // STBTE_PROP_disabled60 // // Note that all of these are stored as floats in the param array.61+// // The integer slider is limited in precision based on the space62+// // available on screen, so for wide-ranged integers you may want63+// // to use floats instead.64 // //65 // // Since the tiledata is passed to you, you can choose which property66 // // is bound to that slot based on that data.67 // //11 unmodified lines79 // #define STBTE_PROP_MAX(int n, short *tiledata) ...your code here...80 // // These return the allowable range for the property values for81 // // the specified slot. It is never called for boolean types.82 //83+// #define STBTE_PROP_FLOAT_SCALE(int n, short *tiledata, float *params)84+// // This rescales the float control for a given property; by default85+// // left mouse drags add integers, right mouse drags adds fractions,86+// // but you can rescale this per-property.87+//88+// #define STBTE_FLOAT_CONTROL_GRANULARITY ... value ...89+// // This returns the number of pixels of mouse motion necessary90+// // to advance the object float control. Default is 491+//92 // #define STBTE_ALLOW_LINK(short *src, float *src_data, \93 // short *dest, float *dest_data) ...your code...94 // // this returns true or false depending on whether you allow a link95 // // to be drawn from a tile 'src' to a tile 'dest'. if you don't143 unmodified lines239 extern void stbte_mouse_move(stbte_tilemap *tm, int x, int y, int shifted, int scrollkey);240 extern void stbte_mouse_button(stbte_tilemap *tm, int x, int y, int right, int down, int shifted, int scrollkey);241 extern void stbte_mouse_wheel(stbte_tilemap *tm, int x, int y, int vscroll);242 243+// for keyboard, define your own mapping from keys to the following actions.244+// this is totally optional, as all features are accessible with the mouse245+enum stbte_action246+{247+ STBTE_tool_select,248+ STBTE_tool_brush,249+ STBTE_tool_erase,250+ STBTE_tool_rectangle,251+ STBTE_tool_eyedropper,252+ STBTE_tool_link,253+ STBTE_act_toggle_grid,254+ STBTE_act_toggle_links,255+ STBTE_act_undo,256+ STBTE_act_redo,257+ STBTE_act_cut,258+ STBTE_act_copy,259+ STBTE_act_paste,260+ STBTE_scroll_left,261+ STBTE_scroll_right,262+ STBTE_scroll_up,263+ STBTE_scroll_down,264+};265+extern void stbte_action(stbte_tilemap *tm, enum stbte_action act);266 267 ////////////////268 //269 // save/load 11 unmodified lines281 extern short* stbte_get_tile(stbte_tilemap *tm, int x, int y);282 // returns an array of shorts that is 'map_layers' in length. each short is283 // either one of the tile_id values from define_tile, or STBTE_EMPTY.284 285+extern float *stbte_get_properties(stbte_tilemap *tm, int x, int y);286+287+extern void stbte_get_link(stbte_tilemap *tm, int x, int y, int *dx, int *dy);288+289 extern void stbte_set_dimensions(stbte_tilemap *tm, int max_x, int max_y);290 // set the dimensions of the level, overrides previous stbte_create_map()291 // values or anything the user has changed292 3 unmodified lines296 297 extern void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile);298 // tile is your tile_id from define_tile, or STBTE_EMPTY299 300+301 ////////302 //303 // optional304 //68 unmodified lines373 #ifndef STBTE_PROP_FLOAT_SCALE374 #define STBTE_PROP_FLOAT_SCALE(n,td,tp) 1 // default scale size375 #endif376 377+#ifndef STBTE_FLOAT_CONTROL_GRANULARITY378+#define STBTE_FLOAT_CONTROL_GRANULARITY 4379+#endif380+381 #define STBTE__UNDO_BUFFER_COUNT (STBTE_UNDO_BUFFER_BYTES>>1)382 383 #if STBTE_MAX_TILEMAP_X > 4096 || STBTE_MAX_TILEMAP_Y > 4096384 #error "Maximum editable map size is 4096 x 4096"280 unmodified lines665 int x0,y0,x1,y1,color;666 } stbte__colorrect;667 668 #define STBTE__MAX_DELAYRECT 256614-#define STBTE__MAX_DELAYLINK 4096669 670 typedef struct671 {672 int tool, active_event;4 unmodified lines677 int shift, scrollkey;678 int initted;679 int side_extended[2];680 stbte__colorrect delayrect[STBTE__MAX_DELAYRECT];627- stbte__colorrect delaylink[STBTE__MAX_DELAYLINK];628- int delaycount, delaylinkcount;681+ int delaycount;682 int show_grid, show_links;683 int brush_state; // used to decide which kind of erasing684 int eyedrop_x, eyedrop_y, eyedrop_last_layer;685 int pasting, paste_x, paste_y;228 unmodified lines914 *max_x = tm->max_x;915 *max_y = tm->max_y;916 }917 865-extern short* stbte_get_tile(stbte_tilemap *tm, int x, int y)918+short* stbte_get_tile(stbte_tilemap *tm, int x, int y)919 {920 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);921 if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)922 return NULL;923 return tm->data[y][x];924 }925 926+float *stbte_get_properties(stbte_tilemap *tm, int x, int y)927+{928+ STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);929+ if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)930+ return NULL;931+ return tm->props[y][x];932+}933+934+void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty)935+{936+ STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);937+#ifdef STBTE_ALLOW_LINK938+ if (x >= 0 && x < STBTE_MAX_TILEMAP_X && y >= 0 && y < STBTE_MAX_TILEMAP_Y) {939+ *destx = tm->link[y][x].x;940+ *desty = tm->link[y][x].y;941+ return;942+ }943+#endif944+ *destx = -1;945+ *desty = -1;946+}947+948+949+950 // returns an array of map_layers shorts. each short is either951 // one of the tile_id values from define_tile, or STBTE_EMPTY952 953 void stbte_set_dimensions(stbte_tilemap *tm, int map_x, int map_y)410 unmodified lines1364 static int stbte__redo_available(stbte_tilemap *tm)1365 {1366 if (!tm->undo_available_valid)1367 stbte__recompute_undo_available(tm);1291- return tm->undo_available;1368+ return tm->redo_available;1369 }1370 1371 ///////////////////////////////////////////////////////////////////////////////////////////////////1372 130 unmodified lines1503 stbte__ui.delayrect[stbte__ui.delaycount++] = r;1504 }1505 }1506 1430-static void stbte__draw_link_delayed(int x0, int y0, int x1, int y1, int color)1431-{1432- if (stbte__ui.delaycount < STBTE__MAX_DELAYRECT) {1433- stbte__colorrect r = { x0,y0,x1,y1,color };1434- stbte__ui.delaylink[stbte__ui.delaylinkcount++] = r;1435- }1436-}1437-1507 static void stbte__flush_delay(void)1508 {1509 stbte__colorrect *r;1510 int i;1511 r = stbte__ui.delayrect;1512 for (i=0; i < stbte__ui.delaycount; ++i,++r)1513 stbte__draw_frame(r->x0,r->y0,r->x1,r->y1,r->color);1445- r = stbte__ui.delaylink;1446- for (i=0; i < stbte__ui.delaylinkcount; ++i,++r)1447- stbte__draw_link(r->x0,r->y0,r->x1,r->y1,r->color);1514 stbte__ui.delaycount = 0;1449- stbte__ui.delaylinkcount = 0;1515 }1516 1517 static void stbte__activate(int id)1518 {232 unmodified lines1751 break;1752 case STBTE__mousemove:1753 if (STBTE__IS_ACTIVE(id)) {1754 float v = *value, delta;1690- int ax = stbte__ui.accum_x/4;1691- int ay = stbte__ui.accum_y/4;1692- stbte__ui.accum_x -= ax*4;1693- stbte__ui.accum_y -= ay*4;1755+ int ax = stbte__ui.accum_x/STBTE_FLOAT_CONTROL_GRANULARITY;1756+ int ay = stbte__ui.accum_y/STBTE_FLOAT_CONTROL_GRANULARITY;1757+ stbte__ui.accum_x -= ax*STBTE_FLOAT_CONTROL_GRANULARITY;1758+ stbte__ui.accum_y -= ay*STBTE_FLOAT_CONTROL_GRANULARITY;1759 if (stbte__ui.shift) {1760 if (stbte__ui.active_event == STBTE__leftdown)1696- delta = ax * 1 + ay / 16.0;1761+ delta = ax * 16 + ay;1762 else1698- delta = ax / 64.0 + ay / 1024.0;1763+ delta = ax / 16.0 + ay / 256.0;1764 } else {1765 if (stbte__ui.active_event == STBTE__leftdown)1701- delta = ax + ay * 0.1;1766+ delta = ax*10 + ay;1767 else1703- delta = ax * 0.01 + ay * 0.001;1768+ delta = ax * 0.1 + ay * 0.01;1769 }1770 v += delta * scale;1771 if (v < minv) v = minv;1772 if (v > maxv) v = maxv;395 unmodified lines2168 return STBTE__erase_brushonly;2169 }2170 }2171 2107- if (allow_any != STBTE__erase_any)2172+ if (allow_any != STBTE__erase_any && allow_any != STBTE__erase_all)2173 return STBTE__erase_none;2174 2175 // apply layer filters, erase from top2176 for (i=tm->num_layers-1; i >= 0; --i) {12 unmodified lines2189 return allow_any;2190 return STBTE__erase_none;2191 }2192 2128-2193 static int stbte__erase(stbte_tilemap *tm, int x, int y, int allow_any)2194 {2195 stbte__tileinfo *ti = tm->cur_tile >= 0 ? &tm->tiles[tm->cur_tile] : NULL;2196 int i;44 unmodified lines2241 return STBTE__erase_brushonly;2242 }2243 }2244 2181- if (allow_any != STBTE__erase_any)2245+ if (allow_any != STBTE__erase_any && allow_any != STBTE__erase_all)2246 return STBTE__erase_none;2247 2248 // apply layer filters, erase from top2249 for (i=tm->num_layers-1; i >= 0; --i) {291 unmodified lines2541 stbte__ui.select_x1 = stbte__ui.select_x0 + stbte__ui.drag_w;2542 stbte__ui.select_y1 = stbte__ui.select_y0 + stbte__ui.drag_h;2543 }2544 2481-static void stbte__tile(stbte_tilemap *tm, int sx, int sy, int mapx, int mapy)2545+static void stbte__tile_paint(stbte_tilemap *tm, int sx, int sy, int mapx, int mapy, int layer)2546 {2483- int tool = stbte__ui.tool;2547 int i;2548+ int id = STBTE__IDMAP(mapx,mapy);2549 int x0=sx, y0=sy;2550 int x1=sx+tm->spacing_x, y1=sy+tm->spacing_y;2487- int id = STBTE__IDMAP(mapx,mapy);2551 int over = stbte__hittest(x0,y0,x1,y1, id);2489- switch (stbte__ui.event) {2490- case STBTE__paint: {2491- short *data = tm->data[mapy][mapx];2492- short temp[STBTE_MAX_LAYERS];2552+ short *data = tm->data[mapy][mapx];2553+ short temp[STBTE_MAX_LAYERS];2554 2494- if (STBTE__IS_MAP_HOT()) {2495- if (stbte__ui.pasting) {2496- int ox = mapx - stbte__ui.paste_x;2497- int oy = mapy - stbte__ui.paste_y;2498- if (ox >= 0 && ox < stbte__ui.copy_width && oy >= 0 && oy < stbte__ui.copy_height) {2499- stbte__paste_stack(tm, temp, tm->data[mapy][mapx], stbte__ui.copybuffer[oy*stbte__ui.copy_width+ox], 0);2500- data = temp;2501- }2502- } else if (stbte__ui.dragging) {2503- int ox,oy;2504- for (i=0; i < tm->num_layers; ++i)2505- temp[i] = tm->data[mapy][mapx][i];2506- data = temp;2555+ if (STBTE__IS_MAP_HOT()) {2556+ if (stbte__ui.pasting) {2557+ int ox = mapx - stbte__ui.paste_x;2558+ int oy = mapy - stbte__ui.paste_y;2559+ if (ox >= 0 && ox < stbte__ui.copy_width && oy >= 0 && oy < stbte__ui.copy_height) {2560+ stbte__paste_stack(tm, temp, tm->data[mapy][mapx], stbte__ui.copybuffer[oy*stbte__ui.copy_width+ox], 0);2561+ data = temp;2562+ }2563+ } else if (stbte__ui.dragging) {2564+ int ox,oy;2565+ for (i=0; i < tm->num_layers; ++i)2566+ temp[i] = tm->data[mapy][mapx][i];2567+ data = temp;2568 2508- // if it's in the source area, remove things unless shift-dragging2509- ox = mapx - stbte__ui.drag_x;2510- oy = mapy - stbte__ui.drag_y;2511- if (!stbte__ui.shift && ox >= 0 && ox < stbte__ui.drag_w && oy >= 0 && oy < stbte__ui.drag_h) {2512- stbte__clear_stack(tm, temp);2513- }2569+ // if it's in the source area, remove things unless shift-dragging2570+ ox = mapx - stbte__ui.drag_x;2571+ oy = mapy - stbte__ui.drag_y;2572+ if (!stbte__ui.shift && ox >= 0 && ox < stbte__ui.drag_w && oy >= 0 && oy < stbte__ui.drag_h) {2573+ stbte__clear_stack(tm, temp);2574+ }2575 2515- ox = mapx - stbte__ui.drag_dest_x;2516- oy = mapy - stbte__ui.drag_dest_y;2517- if (ox >= 0 && ox < stbte__ui.drag_w && oy >= 0 && oy < stbte__ui.drag_h) {2518- stbte__paste_stack(tm, temp, temp, tm->data[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox], !stbte__ui.shift);2519- }2520- } else if (STBTE__IS_MAP_ACTIVE()) {2521- if (stbte__ui.tool == STBTE__tool_rect) {2522- if ((stbte__ui.ms_time & 511) < 380) {2523- int ex = ((stbte__ui.hot_id >> 19) & 4095);2524- int ey = ((stbte__ui.hot_id >> 7) & 4095);2525- int sx = stbte__ui.sx;2526- int sy = stbte__ui.sy;2527-2528- if ( ((mapx >= sx && mapx < ex+1) || (mapx >= ex && mapx < sx+1))2529- && ((mapy >= sy && mapy < ey+1) || (mapy >= ey && mapy < sy+1))) {2530- int i;2531- for (i=0; i < tm->num_layers; ++i)2532- temp[i] = tm->data[mapy][mapx][i];2533- data = temp;2534- if (stbte__ui.active_event == STBTE__leftdown)2535- stbte__brush_predict(tm, temp);2536- else2537- stbte__erase_predict(tm, temp, STBTE__erase_any);2538- }2539- }2540- }2541- }2576+ ox = mapx - stbte__ui.drag_dest_x;2577+ oy = mapy - stbte__ui.drag_dest_y;2578+ if (ox >= 0 && ox < stbte__ui.drag_w && oy >= 0 && oy < stbte__ui.drag_h) {2579+ stbte__paste_stack(tm, temp, temp, tm->data[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox], !stbte__ui.shift);2580 }2581+ } else if (STBTE__IS_MAP_ACTIVE()) {2582+ if (stbte__ui.tool == STBTE__tool_rect) {2583+ if ((stbte__ui.ms_time & 511) < 380) {2584+ int ex = ((stbte__ui.hot_id >> 19) & 4095);2585+ int ey = ((stbte__ui.hot_id >> 7) & 4095);2586+ int sx = stbte__ui.sx;2587+ int sy = stbte__ui.sy;2588 2544- if (STBTE__IS_HOT(id) && STBTE__INACTIVE() && !stbte__ui.pasting) {2545- if (stbte__ui.tool == STBTE__tool_brush) {2546- if ((stbte__ui.ms_time & 511) < 300) {2547- data = temp;2589+ if ( ((mapx >= sx && mapx < ex+1) || (mapx >= ex && mapx < sx+1))2590+ && ((mapy >= sy && mapy < ey+1) || (mapy >= ey && mapy < sy+1))) {2591+ int i;2592 for (i=0; i < tm->num_layers; ++i)2593 temp[i] = tm->data[mapy][mapx][i];2594+ data = temp;2595+ if (stbte__ui.active_event == STBTE__leftdown)2550- stbte__brush_predict(tm, temp);2596+ stbte__brush_predict(tm, temp);2597+ else2598+ stbte__erase_predict(tm, temp, STBTE__erase_any);2599 }2600 }2601 }2602+ }2603+ }2604 2555- {2556- for (i=0; i < tm->num_layers; ++i) {2557- if (i == tm->solo_layer || (!tm->layerinfo[i].hidden && tm->solo_layer < 0))2558- if (data[i] >= 0)2559- STBTE_DRAW_TILE(x0,y0, (unsigned short) data[i], 0, tm->props[mapy][mapx]);2560- if (i == 0 && stbte__ui.show_grid==1)2561- stbte__draw_halfframe(x0,y0,x0+tm->spacing_x, y0+tm->spacing_y, STBTE_COLOR_GRID);2562- }2605+ if (STBTE__IS_HOT(id) && STBTE__INACTIVE() && !stbte__ui.pasting) {2606+ if (stbte__ui.tool == STBTE__tool_brush) {2607+ if ((stbte__ui.ms_time & 511) < 300) {2608+ data = temp;2609+ for (i=0; i < tm->num_layers; ++i)2610+ temp[i] = tm->data[mapy][mapx][i];2611+ stbte__brush_predict(tm, temp);2612 }2613+ }2614+ }2615+2616+ {2617+ i = layer;2618+ if (i == tm->solo_layer || (!tm->layerinfo[i].hidden && tm->solo_layer < 0))2619+ if (data[i] >= 0)2620+ STBTE_DRAW_TILE(x0,y0, (unsigned short) data[i], 0, tm->props[mapy][mapx]);2621+ }2622+}2623+2624+static void stbte__tile(stbte_tilemap *tm, int sx, int sy, int mapx, int mapy)2625+{2626+ int tool = stbte__ui.tool;2627+ int x0=sx, y0=sy;2628+ int x1=sx+tm->spacing_x, y1=sy+tm->spacing_y;2629+ int id = STBTE__IDMAP(mapx,mapy);2630+ int over = stbte__hittest(x0,y0,x1,y1, id);2631+ switch (stbte__ui.event) {2632+ case STBTE__paint: {2633 if (stbte__ui.pasting || stbte__ui.dragging || stbte__ui.scrolling)2634 break;2635 if (stbte__ui.scrollkey && !STBTE__IS_MAP_ACTIVE())2636 break;9 unmodified lines2646 rx0 -= tm->spacing_x/2;2647 ry0 -= tm->spacing_y/2;2648 rx1 += tm->spacing_x/2;2649 ry1 += tm->spacing_y/2;2581- stbte__draw_frame_delayed(rx0-1,ry0-1,rx1+1,ry1+1, STBTE_COLOR_TILEMAP_HIGHLIGHT);2650+ stbte__draw_frame(rx0-1,ry0-1,rx1+1,ry1+1, STBTE_COLOR_TILEMAP_HIGHLIGHT);2651 break;2652 }2653 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {2585- stbte__draw_frame_delayed(x0-1,y0-1,x1+1,y1+1, STBTE_COLOR_TILEMAP_HIGHLIGHT);2654+ stbte__draw_frame(x0-1,y0-1,x1+1,y1+1, STBTE_COLOR_TILEMAP_HIGHLIGHT);2655 }2656 #ifdef STBTE_ALLOW_LINK2657 if (stbte__ui.show_links && tm->link[mapy][mapx].x >= 0) {2658 int tx = tm->link[mapy][mapx].x;2 unmodified lines2661 lx0 = x0 + (tm->spacing_x >> 1) - 1;2662 ly0 = y0 + (tm->spacing_y >> 1) - 1;2663 lx1 = lx0 + (tx - mapx) * tm->spacing_x + 2;2664 ly1 = ly0 + (ty - mapy) * tm->spacing_y + 2;2596- stbte__draw_link_delayed(lx0,ly0,lx1,ly1,2665+ stbte__draw_link(lx0,ly0,lx1,ly1,2666 STBTE_LINK_COLOR(tm->data[mapy][mapx], tm->props[mapy][mapx],2667 tm->data[ty ][tx ], tm->props[ty ][tx]));2668 }2669 #endif225 unmodified lines2895 break;2896 }2897 }2898 2899+static void stbte__start_paste(stbte_tilemap *tm)2900+{2901+ if (stbte__ui.has_copy) {2902+ stbte__ui.pasting = 1;2903+ stbte__activate(STBTE__ID(STBTE__toolbarB,3));2904+ }2905+}2906+2907 static void stbte__toolbar(stbte_tilemap *tm, int x0, int y0, int w, int h)2908 {2909 int i;2910 int estimated_width = 13 * STBTE__num_tool + 8+8+ 120+4 - 30;45 unmodified lines2956 x += 13;2957 }2958 2959 x += 8;2883- if (stbte__button(STBTE__ctoolbar_button, "cut" , x, y,10, 40, STBTE__ID(STBTE__toolbarB,0), 0, !stbte__ui.has_selection)) {2884- if (stbte__ui.has_selection)2885- stbte__copy_cut(tm, 1);2886- }2960+ if (stbte__button(STBTE__ctoolbar_button, "cut" , x, y,10, 40, STBTE__ID(STBTE__toolbarB,0), 0, !stbte__ui.has_selection))2961+ stbte__copy_cut(tm, 1);2962 x += 42;2888- if (stbte__button(STBTE__ctoolbar_button, "copy" , x, y, 5, 40, STBTE__ID(STBTE__toolbarB,1), 0, !stbte__ui.has_selection)) {2889- if (stbte__ui.has_selection)2890- stbte__copy_cut(tm, 0);2891- }2963+ if (stbte__button(STBTE__ctoolbar_button, "copy" , x, y, 5, 40, STBTE__ID(STBTE__toolbarB,1), 0, !stbte__ui.has_selection))2964+ stbte__copy_cut(tm, 0);2965 x += 42;2893- if (stbte__button(STBTE__ctoolbar_button, "paste", x, y, 0, 40, STBTE__ID(STBTE__toolbarB,2), stbte__ui.pasting, !stbte__ui.has_copy)) {2894- if (stbte__ui.has_copy) {2895- stbte__ui.pasting = 1;2896- stbte__activate(STBTE__ID(STBTE__toolbarB,3));2897- }2898- }2966+ if (stbte__button(STBTE__ctoolbar_button, "paste", x, y, 0, 40, STBTE__ID(STBTE__toolbarB,2), stbte__ui.pasting, !stbte__ui.has_copy))2967+ stbte__start_paste(tm);2968 }2969 2970 #define STBTE__TEXTCOLOR(n) stbte__color_table[n][STBTE__text][STBTE__idle]2971 382 unmodified lines3354 #endif3355 3356 static void stbte__editor_traverse(stbte_tilemap *tm)3357 {3289- int i,j;3358+ int i,j,i0,j0,i1,j1,n;3359 3360 if (tm == NULL)3361 return;3362 if (stbte__ui.x0 == stbte__ui.x1 || stbte__ui.y0 == stbte__ui.y1)12 unmodified lines3375 stbte__ui.y0 - tm->scroll_y + tm->spacing_y * tm->max_y, STBTE_COLOR_TILEMAP_BACKGROUND);3376 }3377 3378 // step 1: traverse all the tilemap data...3310- // @OPTIMIZE crop to only region visible between UI widgets -- also necessary to avoid painting under it, etc3311- // @OPTIMIZE crop to visible region by computing correct i,j range3312- for (j=0; j < tm->max_y; ++j) {3313- int y = stbte__ui.y0 + j * tm->spacing_y - tm->scroll_y;3314- if (y + tm->spacing_y < stbte__ui.y0 || y > stbte__ui.y1)3315- continue;3316- for (i=0; i < tm->max_x; ++i) {3317- int x = stbte__ui.x0 + i * tm->spacing_x - tm->scroll_x;3318- if (x + tm->spacing_x >= stbte__ui.x0 && x < stbte__ui.x1)3319- stbte__tile(tm, x, y, i, j);3379+3380+ i0 = (tm->scroll_x - tm->spacing_x) / tm->spacing_x;3381+ j0 = (tm->scroll_y - tm->spacing_y) / tm->spacing_y;3382+ i1 = (tm->scroll_x + stbte__ui.x1 - stbte__ui.x0) / tm->spacing_x + 1;3383+ j1 = (tm->scroll_y + stbte__ui.y1 - stbte__ui.y0) / tm->spacing_y + 1;3384+3385+ if (i0 < 0) i0 = 0;3386+ if (j0 < 0) j0 = 0;3387+ if (i1 > tm->max_x) i1 = tm->max_x;3388+ if (j1 > tm->max_y) j1 = tm->max_y;3389+3390+ if (stbte__ui.event == STBTE__paint) {3391+ // draw all of layer 0, then all of layer 1, etc, instead of old3392+ // way which drew entire stack of each tile at once3393+ for (n=0; n < tm->num_layers; ++n) {3394+ for (j=j0; j < j1; ++j) {3395+ for (i=i0; i < i1; ++i) {3396+ int x = stbte__ui.x0 + i * tm->spacing_x - tm->scroll_x;3397+ int y = stbte__ui.y0 + j * tm->spacing_y - tm->scroll_y;3398+ stbte__tile_paint(tm, x, y, i, j, n);3399+ }3400+ }3401+ if (n == 0 && stbte__ui.show_grid == 1) {3402+ int x = stbte__ui.x0 + i0 * tm->spacing_x - tm->scroll_x;3403+ int y = stbte__ui.y0 + j0 * tm->spacing_y - tm->scroll_y;3404+ for (i=0; x < stbte__ui.x1 && i <= i1; ++i, x += tm->spacing_x)3405+ stbte__draw_rect(x, stbte__ui.y0, x+1, stbte__ui.y1, STBTE_COLOR_GRID);3406+ for (j=0; y < stbte__ui.y1 && j <= j1; ++j, y += tm->spacing_y)3407+ stbte__draw_rect(stbte__ui.x0, y, stbte__ui.x1, y+1, STBTE_COLOR_GRID);3408+ }3409 }3410 }3411 3412+ if (stbte__ui.event == STBTE__paint) {3323- // draw grid on top of everything3324- if (stbte__ui.show_grid == 2) {3325- int x = stbte__ui.x0 - tm->scroll_x;3326- int y = stbte__ui.y0 - tm->scroll_y;3327- for (j=0; j < tm->max_y; ++j, y += tm->spacing_y)3328- stbte__draw_rect(stbte__ui.x0, y, stbte__ui.x1, y+1, STBTE_COLOR_GRID);3329- for (i=0; i < tm->max_x; ++i, x += tm->spacing_x)3330- stbte__draw_rect(x, stbte__ui.y0, x+1, stbte__ui.y1, STBTE_COLOR_GRID);3413+ // draw grid on top of everything except UI3414+ if (stbte__ui.show_grid == 2) {3415+ int x = stbte__ui.x0 + i0 * tm->spacing_x - tm->scroll_x;3416+ int y = stbte__ui.y0 + j0 * tm->spacing_y - tm->scroll_y;3417+ for (i=0; x < stbte__ui.x1 && i <= i1; ++i, x += tm->spacing_x)3418+ stbte__draw_rect(x, stbte__ui.y0, x+1, stbte__ui.y1, STBTE_COLOR_GRID);3419+ for (j=0; y < stbte__ui.y1 && j <= j1; ++j, y += tm->spacing_y)3420+ stbte__draw_rect(stbte__ui.x0, y, stbte__ui.x1, y+1, STBTE_COLOR_GRID);3421+ }3422 }3423 3333- // draw the selection border3424+ for (j=j0; j < j1; ++j) {3425+ for (i=i0; i < i1; ++i) {3426+ int x = stbte__ui.x0 + i * tm->spacing_x - tm->scroll_x;3427+ int y = stbte__ui.y0 + j * tm->spacing_y - tm->scroll_y;3428+ stbte__tile(tm, x, y, i, j);3429+ }3430+ }3431+3432 if (stbte__ui.event == STBTE__paint) {3433+ // draw the selection border3434 if (stbte__ui.has_selection) {3435 int x0,y0,x1,y1;3436 x0 = stbte__ui.x0 + (stbte__ui.select_x0 ) * tm->spacing_x - tm->scroll_x;3437 y0 = stbte__ui.y0 + (stbte__ui.select_y0 ) * tm->spacing_y - tm->scroll_y;118 unmodified lines3556 stbte__draw_frame(x-w/2-4,y-8, x+w/2+4,y+8, 0x906030);3557 stbte__draw_text (x-w/2,y-4, stbte__ui.alert_msg, w+1, 0xff8040);3558 }3559 3560+#ifdef STBTE_SHOW_CURSOR3561+ if (stbte__ui.event == STBTE__paint)3562+ stbte__draw_bitmap(stbte__ui.mx, stbte__ui.my, stbte__get_char_width(26), stbte__get_char_bitmap(26), 0xe0e0e0);3563+#endif3564+3565 if (stbte__ui.event == STBTE__tick && stbte__ui.alert_msg) {3566 stbte__ui.alert_timer -= stbte__ui.dt;3567 if (stbte__ui.alert_timer < 0) {3568 stbte__ui.alert_timer = 0;85 unmodified lines3654 }3655 3656 void stbte_mouse_wheel(stbte_tilemap *tm, int x, int y, int vscroll)3657 {3658+ // not implemented yet -- need different way of hittesting3659+}3660 3661+void stbte_action(stbte_tilemap *tm, enum stbte_action act)3662+{3663+ switch (act) {3664+ case STBTE_tool_select: stbte__ui.tool = STBTE__tool_select; break;3665+ case STBTE_tool_brush: stbte__ui.tool = STBTE__tool_brush; break;3666+ case STBTE_tool_erase: stbte__ui.tool = STBTE__tool_erase; break;3667+ case STBTE_tool_rectangle: stbte__ui.tool = STBTE__tool_rect; break;3668+ case STBTE_tool_eyedropper: stbte__ui.tool = STBTE__tool_eyedrop; break;3669+ case STBTE_tool_link: stbte__ui.tool = STBTE__tool_link; break;3670+ case STBTE_act_toggle_grid: stbte__ui.show_grid = (stbte__ui.show_grid+1) % 3; break;3671+ case STBTE_act_toggle_links: stbte__ui.show_links ^= 1; break;3672+ case STBTE_act_undo: stbte__undo(tm); break;3673+ case STBTE_act_redo: stbte__redo(tm); break;3674+ case STBTE_act_cut: stbte__copy_cut(tm, 1); break;3675+ case STBTE_act_copy: stbte__copy_cut(tm, 0); break;3676+ case STBTE_act_paste: stbte__start_paste(tm); break;3677+ case STBTE_scroll_left: tm->scroll_x -= tm->spacing_x; break;3678+ case STBTE_scroll_right: tm->scroll_x += tm->spacing_x; break;3679+ case STBTE_scroll_up: tm->scroll_y -= tm->spacing_y; break;3680+ case STBTE_scroll_down: tm->scroll_y += tm->spacing_y; break;3681+ }3682 }3683 3684 void stbte_tick(stbte_tilemap *tm, float dt)3685 {38 unmodified lines