Skip to content
Myr

tilemap editor 0.30

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 2b5d5d9 · Browse files · 3 files+629 added−78 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+629 added−78 removed

  • tests:+193 added−0 removed
  • other:+436 added−78 removed

3 files+629 added−78 removed

added:tests/tilemap_editor_integration_example.c+193 added
1// This isn't compilable as-is, as it was extracted from a working2// integration-in-a-game and makes reference to symbols from that game.34#include <assert.h>5#include <ctype.h>6#include "game.h"7#include "SDL.h"8#include "stb_tilemap_editor.h"910extern void editor_draw_tile(int x, int y, unsigned short tile, int mode, float *props);11extern void editor_draw_rect(int x0, int y0, int x1, int y1, unsigned char r, unsigned char g, unsigned char b);1213static int is_platform(short *tiles);14static unsigned int prop_type(int n, short *tiles);15static char *prop_name(int n, short *tiles);16static float prop_range(int n, short *tiles, int is_max);17static int allow_link(short *src, short *dest);1819#define STBTE_MAX_PROPERTIES 82021#define STBTE_PROP_TYPE(n, tiledata, p) prop_type(n,tiledata)22#define STBTE_PROP_NAME(n, tiledata, p) prop_name(n,tiledata)23#define STBTE_PROP_MIN(n, tiledata, p) prop_range(n,tiledata,0)24#define STBTE_PROP_MAX(n, tiledata, p) prop_range(n,tiledata,1)25#define STBTE_PROP_FLOAT_SCALE(n,td,p) (0.1)2627#define STBTE_ALLOW_LINK(srctile, srcprop, desttile, destprop) \28 allow_link(srctile, desttile)2930#define STBTE_LINK_COLOR(srctile, srcprop, desttile, destprop) \31 (is_platform(srctile) ? 0xff80ff : 0x808040)3233#define STBTE_DRAW_RECT(x0,y0,x1,y1,c) \34 editor_draw_rect(x0,y0,x1,y1,(c)>>16,((c)>>8)&255,(c)&255)3536#define STBTE_DRAW_TILE(x,y,id,highlight,props) \37 editor_draw_tile(x,y,id,highlight,props)38394041#define STB_TILEMAP_EDITOR_IMPLEMENTATION42#include "stb_tilemap_editor.h"4344stbte_tilemap *edit_map;4546void editor_key(enum stbte_action act)47{48 stbte_action(edit_map, act);49}5051void editor_process_sdl_event(SDL_Event *e)52{53 switch (e->type) {54 case SDL_MOUSEMOTION:55 case SDL_MOUSEBUTTONDOWN:56 case SDL_MOUSEBUTTONUP:57 case SDL_MOUSEWHEEL:58 stbte_mouse_sdl(edit_map, e, 1.0f/editor_scale,1.0f/editor_scale,0,0);59 break;6061 case SDL_KEYDOWN:62 if (in_editor) {63 switch (e->key.keysym.sym) {64 case SDLK_RIGHT: editor_key(STBTE_scroll_right); break;65 case SDLK_LEFT : editor_key(STBTE_scroll_left ); break;66 case SDLK_UP : editor_key(STBTE_scroll_up ); break;67 case SDLK_DOWN : editor_key(STBTE_scroll_down ); break;68 }69 switch (e->key.keysym.scancode) {70 case SDL_SCANCODE_S: editor_key(STBTE_tool_select); break;71 case SDL_SCANCODE_B: editor_key(STBTE_tool_brush ); break;72 case SDL_SCANCODE_E: editor_key(STBTE_tool_erase ); break;73 case SDL_SCANCODE_R: editor_key(STBTE_tool_rectangle ); break;74 case SDL_SCANCODE_I: editor_key(STBTE_tool_eyedropper); break;75 case SDL_SCANCODE_L: editor_key(STBTE_tool_link); break;76 case SDL_SCANCODE_G: editor_key(STBTE_act_toggle_grid); break;77 }78 if ((e->key.keysym.mod & KMOD_CTRL) && !(e->key.keysym.mod & ~KMOD_CTRL)) {79 switch (e->key.keysym.scancode) {80 case SDL_SCANCODE_X: editor_key(STBTE_act_cut ); break;81 case SDL_SCANCODE_C: editor_key(STBTE_act_copy ); break;82 case SDL_SCANCODE_V: editor_key(STBTE_act_paste); break;83 case SDL_SCANCODE_Z: editor_key(STBTE_act_undo ); break;84 case SDL_SCANCODE_Y: editor_key(STBTE_act_redo ); break;85 }86 }87 }88 break;89 }90}9192void editor_init(void)93{94 int i;95 edit_map = stbte_create_map(20,14, 8, 16,16, 100);9697 stbte_set_background_tile(edit_map, T_empty);9899 for (i=0; i < T__num_types; ++i) {100 if (i != T_reserved1 && i != T_entry && i != T_doorframe)101 stbte_define_tile(edit_map, 0+i, 1, "Background");102 }103 stbte_define_tile(edit_map, 256+O_player , 8, "Char");104 stbte_define_tile(edit_map, 256+O_robot , 8, "Char");105 for (i=O_lockeddoor; i < O__num_types-2; ++i)106 if (i == O_platform || i == O_vplatform)107 stbte_define_tile(edit_map, 256+i, 4, "Object");108 else109 stbte_define_tile(edit_map, 256+i, 2, "Object");110111 //stbte_set_layername(edit_map, 0, "background");112 //stbte_set_layername(edit_map, 1, "objects");113 //stbte_set_layername(edit_map, 2, "platforms");114 //stbte_set_layername(edit_map, 3, "characters");115}116117static int is_platform(short *tiles)118{119 // platforms are only on layer #2120 return tiles[2] == 256 + O_platform || tiles[2] == 256 + O_vplatform;121}122123static int is_object(short *tiles)124{125 return (tiles[1] >= 256 || tiles[2] >= 256 || tiles[3] >= 256);126}127128static unsigned int prop_type(int n, short *tiles)129{130 if (is_platform(tiles)) {131 static unsigned int platform_types[STBTE_MAX_PROPERTIES] = {132 STBTE_PROP_bool, // phantom133 STBTE_PROP_int, // x_adjust134 STBTE_PROP_int, // y_adjust135 STBTE_PROP_float, // width136 STBTE_PROP_float, // lspeed137 STBTE_PROP_float, // rspeed138 STBTE_PROP_bool, // autoreturn139 STBTE_PROP_bool, // one-shot140 // remainder get 0, means 'no property in this slot'141 };142 return platform_types[n];143 } else if (is_object(tiles)) {144 if (n == 0)145 return STBTE_PROP_bool;146 }147 return 0;148}149150static char *prop_name(int n, short *tiles)151{152 if (is_platform(tiles)) {153 static char *platform_vars[STBTE_MAX_PROPERTIES] = {154 "phantom",155 "x_adjust",156 "y_adjust",157 "width",158 "lspeed",159 "rspeed",160 "autoreturn",161 "one-shot",162 };163 return platform_vars[n];164 }165 return "phantom";166}167168static float prop_range(int n, short *tiles, int is_max)169{170 if (is_platform(tiles)) {171 static float ranges[8][2] = {172 { 0, 1 }, // phantom-flag, range is ignored173 { -15, 15 }, // x_adjust174 { -15, 15 }, // y_adjust175 { 0, 6 }, // width176 { 0, 10 }, // lspeed177 { 0, 10 }, // rspeed178 { 0, 1 }, // autoreturn, range is ignored179 { 0, 1 }, // one-shot, range is ignored180 };181 return ranges[n][is_max];182 }183 return 0;184}185186static int allow_link(short *src, short *dest)187{188 if (is_platform(src))189 return dest[1] == 256+O_lever;190 if (src[1] == 256+O_endpoint)191 return is_platform(dest);192 return 0;193}
modified:README.md+1 added-1 removed
modified:stb_tilemap_editor.h+435 added-77 removed
1// stb_tilemap_editor.h - v0.20 - Sean Barrett - http://nothings.org/stb1// stb_tilemap_editor.h - v0.30 - Sean Barrett - http://nothings.org/stb2// placed in the public domain - not copyrighted - first released 2014-093//4// Embeddable tilemap editor for C/C++5//6//7// REVISION HISTORY8// 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 7// TABLE OF CONTENTS8// FAQ9// How to compile/use the library10// Additional configuration macros11// API documentation12// Info on editing multiple levels13// Revision history14// Todo15// Credits16// License17//18//23// COMPILING19// FAQ20//21// Q: What counts as a tilemap for this library?22//23// A: An array of rectangles, where each rectangle contains a small24// stack of images.25//26// Q: What are the limitations?27//28// A: Maps are limited to 4096x4096 in dimension.29// Each map square can only contain a stack of at most 32 images.30// A map can only use up to 32768 distinct image tiles.31//32// Q: How do I compile this?33//34// A: You need to #define several symbols before #including it, but only35// in one file. This will cause all the function definitions to be36// generated in that file. See the "HOW TO COMPILE" section.37//38// Q: What advantages does this have over a standalone editor?39//40// A: For one, you can integrate the editor into your game so you can41// flip between editing and testing without even switching windows.42// For another, you don't need an XML parser to get at the map data.43//44// Q: Can I live-edit my game maps?45//46// A: Not really, the editor keeps its own map representation.47//48// Q: How do I save and load maps?49//50// A: You have to do this yourself. The editor provides serialization51// functions (get & set) for reading and writing the map it holds.52// You can choose whatever format you want to store the map to on53// disk; you just need to provide functions to convert. (For example,54// I actually store the editor's map representation to disk basically55// as-is; then I have a single function that converts from the editor56// map representation to the game representation, which is used both57// to go from editor-to-game and from loaded-map-to-game.)58//59// Q: I want to have tiles change appearance based on what's60// adjacent, or other tile-display/substitution trickiness.61//62// A: You can do this when you convert from the editor's map63// representation to the game representation, but there's64// no way to show this live in the editor.65//66// Q: How do I scale the user interface?67//68// A: Since you do all the rendering, you can scale up all the rendering69// calls that the library makes to you. If you do, (a) you need70// to also scale up the mouse coordinates, and (b) you may want71// to scale the map display back down so that you're only scaling72// the UI and not everything. See the next question.73//74// Q: How do I scale the map display?75//76// A: Use stbte_set_spacing() to change the size that the map is displayed77// at. Note that the "callbacks" to draw tiles are used for both drawing78// the map and drawing the tile palette, so that callback may need to79// draw at two different scales. You should choose the scales to match80// You can tell them apart because the81// tile palette gets NULL for the property pointer.82//83// Q: How does object editing work?84//85// A: One way to think of this is that in the editor, you're placing86// spawners, not objects. Each spawner must be tile-aligned, because87// it's only a tile editor. Each tile (stack of layers) gets88// an associated set of properties, and it's up to you to89// determine what properties should appear for a given tile,90// based on e.g. the spawners that are in it.91//92// Q: How are properties themselves handled?93//94// A: All properties, regardless of UI behavior, are internally floats.95// Each tile has an array of floats associated with it, which is96// passed back to you when drawing the tiles so you can draw97// objects appropriately modified by the properties.98//99// Q: What if I want to have two different objects/spawners in100// one tile, both of which have their own properties?101//102// A: Make sure STBTE_MAX_PROPERTIES is large enough for the sum of103// properties in both objects, and then you have to explicitly104// map the property slot #s to the appropriate objects. They'll105// still all appear in a single property panel; there's no way106// to get multiple panels.107//108// Q: Can I do one-to-many linking?109//110// A: The library only supports one link per tile. However, you111// can have multiple tiles all link to a single tile. So, you112// can fake one-to-many linking by linking in the reverse113// direction.114//115// Q: What if I have two objects in the same tile, and they each116// need an independent link? Or I have two kinds of link associated117// with a single object?118//119// A: There is no way to do this. (Unless you can reverse one link.)120//121// Q: How does cut & paste interact with object properties & links?122//123// A: Currently the library has no idea which properties or links124// are associated with which layers of a tile. So currently, the125// library will only copy properties & links if the layer panel126// is set to allow all layers to be copied, OR if you set the127// "props" in the layer panel to "always". Similarly, you can128// set "props" to "none" so it will never copy.129//130// Q: What happens if the library gets a memory allocation failure131// while I'm editing? Will I lose my work?132//133// A: The library allocates all editor memory when you create134// the tilemap. It allocates a maximally-sized map and a 135// fixed-size undo buffer (and the fixed-size copy buffer136// is static), and never allocates memory while it's running.137// So it can't fail due to running out of memory.138//139// Q: What happens if the library crashes while I'm editing? Will140// I lose my work?141//142// A: Yes. Save often.143//144//145// HOW TO COMPILE146//147// This header file contains both the header file and the148// implementation file in one. To create the implementation,149// in one source file define a few symbols first and then150// include this header:
5 unmodified lines
156// // this must draw a filled rectangle (exclusive on right/bottom)157// // color = (r<<16)|(g<<8)|(b)158// 159// void STBTE_DRAW_TILE(int x0, int y0,38// unsigned short id, int highlight, stbte_props *data);160// unsigned short id, int highlight, float *data);161// // this draws the tile image identified by 'id' in one of several162// // highlight modes (see STBTE_drawmode_* in the header section);41// // if 'data' is NULL, it's drawing the tile in a palette; if 'data'163// // if 'data' is NULL, it's drawing the tile in the palette; if 'data'164// // is not NULL, it's drawing a tile on the map, and that is the data165// // associated with that map tile166//167// #include "stb_tilemap_editor.h"
25 unmodified lines
193// 194// #define STBTE_PROP_NAME(int n, short *tiledata, float *params) ...195// // these return a string with the name for slot #n in the float196// // property list for the tile.75// // (if any), or76// // based on the values of other params. 197//198// #define STBTE_PROP_MIN(int n, short *tiledata) ...your code here...199// #define STBTE_PROP_MAX(int n, short *tiledata) ...your code here...200// // These return the allowable range for the property values for
39 unmodified lines
240// #define STBTE_MAX_TILEMAP_X 200 // max 4096241// #define STBTE_MAX_TILEMAP_Y 200 // max 4096242// #define STBTE_MAX_LAYERS 8 // max 32243// #define STBTE_MAX_CATEGORIES 100124// #define STBTE_UNDO_BUFFER_BYTES (1 << 20) // 1MB244// #define STBTE_UNDO_BUFFER_BYTES (1 << 24) // 16 MB245// #define STBTE_MAX_COPY 90000 // e.g. 300x300126// #define STBTE_MAX_PROP 10 // max properties per tile246// #define STBTE_MAX_PROPERTIESERTIES 10 // max properties per tile247//248// API249//250// Further documentation appears in the header-file section below.
7 unmodified lines
258// stbte_tilemap keeps its own undo state. (The clipboard is global, so259// either approach allows cut&pasting between levels.)260//261// REVISION HISTORY262// 0.30 properties release263// - properties panel for editing user-defined "object" properties264// - can link each tile to one other tile265// - keyboard interface266// - fix eraser tool bug (worked in complex cases, failed in simple)267// - undo/redo tools have visible disabled state268// - tiles on higher layers draw on top of adjacent lower-layer tiles269// 0.20 erasable release270// - eraser tool271// - fix bug when pasting into protected layer272// - better color scheme273// - internal-use color picker274// 0.10 initial release 275//143// 0.20 - 2014-09-27 - eraser tool, bugfixes, new colorscheme144// 0.10 - 2014-09-23 - initial release145//276// TODO277//278// Separate scroll state for each category279// Implement paint bucket280// Support STBTE_HITTEST_TILE above151// Support STBTE_HITTEST_ICON above281// ?Cancel drags by clicking other button? - may be fixed153// Object properties (per-tile properties) 282// Finish support for toolbar at side283// Layer name buttons grow to fill box284//285// CREDITS286//287// Written by Sean Barrett, September & October 2014.288//289// LICENSE290//291// This software has been placed in the public domain by its author.292// Where that dedication is not recognized, you are granted a perpetual,293// irrevocable license to copy and modify this file as you see fit.294295296165166297///////////////////////////////////////////////////////////////////////298//299// HEADER SECTION300
111 unmodified lines
412// returns an array of shorts that is 'map_layers' in length. each short is413// either one of the tile_id values from define_tile, or STBTE_EMPTY.414415extern float *stbte_get_properties(stbte_tilemap *tm, int x, int y);416// get the property array associated with the tile at x,y. this is an417// array of floats that is STBTE_MAX_PROPERTIES in length; you have to418// interpret the slots according to the semantics you've chosen419287extern void stbte_get_link(stbte_tilemap *tm, int x, int y, int *dx, int *dy);420extern void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty);421// gets the link associated with the tile at x,y.422423extern void stbte_set_dimensions(stbte_tilemap *tm, int max_x, int max_y);424// set the dimensions of the level, overrides previous stbte_create_map()425// values or anything the user has changed
4 unmodified lines
430431extern void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile);432// tile is your tile_id from define_tile, or STBTE_EMPTY433434extern void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val);435// set the value of the n'th slot of the tile at x,y436437extern void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty);438// set a link going from x,y to destx,desty. to force no link,439// use destx=desty=-1440441////////442//443// optional444//
49 unmodified lines
494#define STBTE_MAX_COPY 65536495#endif496497#ifndef STBTE_UNDO_BUFFER_BYTES358#define STBTE_UNDO_BUFFER_BYTES (1 << 20) // 1MB498#define STBTE_UNDO_BUFFER_BYTES (1 << 24) // 16 MB499#endif500361#ifndef STBTE_MAX_PROP362#define STBTE_MAX_PROP 10501#ifndef STBTE_PROP_TYPE502#define STBTE__NO_PROPS503#define STBTE_PROP_TYPE(n,td,tp) 0504#endif505506#ifndef STBTE_PROP_NAME507#define STBTE_PROP_NAME(n,td,tp) ""508#endif509510#ifndef STBTE_MAX_PROPERTIES511#define STBTE_MAX_PROPERTIES 10512#endif513514#ifndef STBTE_PROP_MIN515#define STBTE_PROP_MIN(n,td,tp) 0516#endif517
8 unmodified lines
526#ifndef STBTE_FLOAT_CONTROL_GRANULARITY527#define STBTE_FLOAT_CONTROL_GRANULARITY 4528#endif529530531#define STBTE__UNDO_BUFFER_COUNT (STBTE_UNDO_BUFFER_BYTES>>1)532533#if STBTE_MAX_TILEMAP_X > 4096 || STBTE_MAX_TILEMAP_Y > 4096534#error "Maximum editable map size is 4096 x 4096"
4 unmodified lines
539#if STBTE_UNDO_BUFFER_COUNT & (STBTE_UNDO_BUFFER_COUNT-1)540#error "Undo buffer size must be a power of 2"541#endif542393static int *stbte__colors;543#if STBTE_MAX_PROPERTIES == 0544#define STBTE__NO_PROPS545#endif546547#ifdef STBTE__NO_PROPS548#undef STBTE_MAX_PROPERTIES549#define STBTE_MAX_PROPERTIES 1 // so we can declare arrays550#endif551552typedef struct553{554 short x,y;555} stbte__link;
239 unmodified lines
795static int toolchar[] = { 26,24,25,20,23,22,18, 19,17, 29,28, };796797enum798{799 STBTE__propmode_default,800 STBTE__propmode_always,801 STBTE__propmode_never,802};803804enum805{806 STBTE__paint,807808 // from here down does hittesting809 STBTE__tick,
52 unmodified lines
862 const char *alert_msg;863 float dt;864 stbte__panel panel[STBTE__num_panel];865 short copybuffer[STBTE_MAX_COPY][STBTE_MAX_LAYERS];866 float copyprops[STBTE_MAX_COPY][STBTE_MAX_PROPERTIES];867#ifdef STBTE_ALLOW_LINK868 stbte__link copylinks[STBTE_MAX_COPY];869#endif870 int copy_src_x, copy_src_y;871 stbte_tilemap *copy_src;702 int copy_width,copy_height,has_copy;872 int copy_width,copy_height,has_copy,copy_has_props;873} stbte__ui_t;874875// there's only one UI system at a time, so we can globalize this876static stbte__ui_t stbte__ui = { STBTE__tool_brush, 0 };
21 unmodified lines
898899struct stbte_tilemap900{901 stbte__tiledata data[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_LAYERS];732 float props[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_PROP];902 float props[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_PROPERTIES];903 #ifdef STBTE_ALLOW_LINK904 stbte__link link[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];905 int linkcount[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];906 #endif
15 unmodified lines
922 int tileinfo_dirty;923 stbte__layer layerinfo[STBTE_MAX_LAYERS];924 int has_layer_names;925 int layer_scroll;926 int propmode;927 int solo_layer;928 int undo_pos, undo_len, redo_len;929 short background_tile;930 unsigned char id_in_use[32768>>3];
66 unmodified lines
997 tm->redo_len = 0;998 tm->undo_pos = 0;999 tm->category_scroll = 0;1000 tm->layer_scroll = 0;1001 tm->propmode = 0;1002 tm->has_layer_names = 0;1003 tm->undo_available_valid = 0;10041005 for (i=0; i < tm->num_layers; ++i) {
98 unmodified lines
1104}11051106void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty)1107{1108 int gx=-1,gy=-1;1109 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1110#ifdef STBTE_ALLOW_LINK1111 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;1112 gx = tm->link[y][x].x;1113 gy = tm->link[y][x].y;1114 if (gx >= 0)1115 if (!STBTE_ALLOW_LINK(tm->data[y][x], tm->props[y][x], tm->data[gy][gx], tm->props[gy][gx]))1116 gx = gy = -1;1117 }1118#endif944 *destx = -1;945 *desty = -1;1119 *destx = gx;1120 *desty = gy;1121}11221123void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val)1124{1125 tm->props[y][x][n] = val;1126}11271128static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode);11291130enum1131{1132 STBTE__undo_none,1133 STBTE__undo_record,1134 STBTE__undo_block,1135};11361137void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty)1138{1139#ifdef STBTE_ALLOW_LINK1140 stbte__set_link(tm, x, y, destx, desty, STBTE__undo_none);1141#else1142 STBTE_ASSERT(0);1143#endif1144}114511461147// returns an array of map_layers shorts. each short is either1148// one of the tile_id values from define_tile, or STBTE_EMPTY11491150void stbte_set_dimensions(stbte_tilemap *tm, int map_x, int map_y)
12 unmodified lines
1163 for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i) {1164 tm->data[0][i][0] = tm->background_tile;1165 for (j=1; j < tm->num_layers; ++j)1166 tm->data[0][i][j] = STBTE__NO_TILE;970 for (j=0; j < STBTE_MAX_PROP; ++j)1167 for (j=0; j < STBTE_MAX_PROPERTIES; ++j)1168 tm->props[0][i][j] = 0;1169 #ifdef STBTE_ALLOW_LINK1170 tm->link[0][i].x = -1;1171 tm->link[0][i].y = -1;
244 unmodified lines
1416 return -1;1417 return pos;1418}14191223static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y);1420static void stbte__undo(stbte_tilemap *tm)1421{1422 int i, pos, endpos;1423 endpos = stbte__undo_find_end(tm);
27 unmodified lines
1451 } else {1452#ifdef STBTE_ALLOW_LINK1453 s0 = tm->link[y][x].x;1454 s1 = tm->link[y][x].y;1259 stbte__set_link(tm, x,y, v, v2);1455 stbte__set_link(tm, x,y, v, v2, STBTE__undo_none);1456#endif1457 }1458 // write the redo entry1459 stbte__redo_record_prop(tm, x, y, n-256, s0,s1);
60 unmodified lines
1520 } else {1521#ifdef STBTE_ALLOW_LINK1522 s0 = tm->link[y][x].x;1523 s1 = tm->link[y][x].y;1328 stbte__set_link(tm, x,y,v,v2);1524 stbte__set_link(tm, x,y,v,v2, STBTE__undo_none);1525#endif1526 }1527 // don't use stbte__undo_record_prop because it's guarded1528 stbte__write_undo(tm, s1);
37 unmodified lines
15661567///////////////////////////////////////////////////////////////////////////////////////////////////15681569#ifdef STBTE_ALLOW_LINK1374static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y)1570static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode)1571{1572 stbte__link *a;1573 STBTE_ASSERT(src_x >= 0 && src_x < STBTE_MAX_TILEMAP_X && src_y >= 0 && src_y < STBTE_MAX_TILEMAP_Y);1574 a = &tm->link[src_y][src_x];1575 // check if it's a do nothing1576 if (a->x == dest_x && a->y == dest_y)1577 return;1382 // otherwise, undo1383 stbte__begin_undo(tm);1384 stbte__undo_record_prop(tm, src_x, src_y, -1, a->x, a->y);1385 stbte__end_undo(tm);1578 if (undo_mode != STBTE__undo_none ) {1579 if (undo_mode == STBTE__undo_block) stbte__begin_undo(tm);1580 stbte__undo_record_prop(tm, src_x, src_y, -1, a->x, a->y);1581 if (undo_mode == STBTE__undo_block) stbte__end_undo(tm);1582 }1583 // check if there's an existing link1584 if (a->x >= 0) {1585 // decrement existing link refcount1586 STBTE_ASSERT(tm->linkcount[a->y][a->x] > 0);
473 unmodified lines
2060 int panel_active[STBTE__num_panel]={1,0,1,1,1,1,1};2061 int vpos[4] = { 0,0,0,0 };2062 stbte__panel *p = stbte__ui.panel;2063 stbte__panel *pt = &p[STBTE__panel_toolbar];2064#ifdef STBTE__NO_PROPS2065 int props = 0;2066#else2067 int props = 1;2068#endif20692070 for (i=0; i < 4; ++i) {2071 stbte__region[i].active = 0;2072 stbte__region[i].width = 0;
8 unmodified lines
2081 panel_active[STBTE__panel_layers ] = tm->num_layers > 1;2082#ifdef STBTE__COLORPICKER2083 panel_active[STBTE__panel_colorpick ] = 1;2084#endif1883 panel_active[STBTE__panel_props ] = stbte__is_single_selection();20852086 panel_active[STBTE__panel_props ] = props && stbte__is_single_selection();20872088 // compute minimum widths for each panel (assuming they're on sides not top)2089 min_width[STBTE__panel_info ] = 8 + 11 + 7*tm->digits+17+7; // estimate min width of "w:0000"2090 min_width[STBTE__panel_colorpick ] = 120;2091 min_width[STBTE__panel_tiles ] = 4 + tm->palette_spacing_x + 5; // 5 for scrollbar
50 unmodified lines
2142 height[STBTE__panel_info] = 5 + 11*4 + 2 + tm->palette_spacing_y;21432144 // layers2145 limit = 6 + stbte__ui.panel[STBTE__panel_layers].delta_height;1943 height[STBTE__panel_layers] = (tm->num_layers > limit ? limit : tm->num_layers)*15 + 7 + (tm->has_layer_names ? 0 : 11);2146 height[STBTE__panel_layers] = (tm->num_layers > limit ? limit : tm->num_layers)*15 + 7 + (tm->has_layer_names ? 0 : 11) + props*13;21472148 // categories2149 limit = 6 + stbte__ui.panel[STBTE__panel_categories].delta_height;2150 height[STBTE__panel_categories] = (tm->num_categories+1 > limit ? limit : tm->num_categories+1)*11 + 14;
5 unmodified lines
2156 if (k == 0) k = 1;2157 height[STBTE__panel_tiles] = ((tm->num_tiles+k-1)/k) * tm->palette_spacing_y + 8;21582159 // properties panel1957 height[STBTE__panel_props] = 9 + STBTE_MAX_PROP*14;2160 height[STBTE__panel_props] = 9 + STBTE_MAX_PROPERTIES*14;21612162 // now compute the locations of all the panels2163 for (i=0; i < STBTE__num_panel; ++i) {2164 if (panel_active[i]) {
347 unmodified lines
2512 return;2513 }2514}25152516static int stbte__should_copy_properties(stbte_tilemap *tm)2517{2518 int i;2519 if (tm->propmode == STBTE__propmode_always)2520 return 1;2521 if (tm->propmode == STBTE__propmode_never)2522 return 0;2523 if (tm->solo_layer >= 0 || tm->cur_layer >= 0)2524 return 0;2525 for (i=0; i < tm->num_layers; ++i)2526 if (tm->layerinfo[i].hidden || tm->layerinfo[i].locked)2527 return 0;2528 return 1;2529}25302531// compute the result of pasting into a tile non-destructively so we can preview it2532static void stbte__paste_stack(stbte_tilemap *tm, short result[], short dest[], short src[], int dragging)2533{2534 int i;
75 unmodified lines
2610 stbte__ui.select_y0 = (y0 < y1 ? y0 : y1);2611 stbte__ui.select_y1 = (y0 < y1 ? y1 : y0);2612}26132614static void stbte__copy_properties(float *dest, float *src)2615{2616 int i;2617 for (i=0; i < STBTE_MAX_PROPERTIES; ++i)2618 dest[i] = src[i];2619}26202621static void stbte__copy_cut(stbte_tilemap *tm, int cut)2622{2623 int i,j,n,w,h,p=0;2624 int copy_props = stbte__should_copy_properties(tm);2625 if (!stbte__ui.has_selection)2626 return;2627 w = stbte__ui.select_x1 - stbte__ui.select_x0 + 1;2628 h = stbte__ui.select_y1 - stbte__ui.select_y0 + 1;
28 unmodified lines
2657 stbte__undo_record(tm,i,j,n, tm->data[j][i][n]);2658 tm->data[j][i][n] = (n==0 ? tm->background_tile : -1);2659 }2660 }2661 if (copy_props) {2662 stbte__copy_properties(stbte__ui.copyprops[p], tm->props[j][i]);2663#ifdef STBTE_ALLOW_LINK2664 stbte__ui.copylinks[p] = tm->link[j][i];2665 if (cut)2666 stbte__set_link(tm, i,j,-1,-1, STBTE__undo_record);2667#endif2668 }2669 ++p;2670 }2671 }2672 if (cut)2673 stbte__end_undo(tm);2674 stbte__ui.copy_width = w;2675 stbte__ui.copy_height = h;2676 stbte__ui.has_copy = 1;2443 stbte__ui.has_selection = 0;2677 //stbte__ui.has_selection = 0;2678 stbte__ui.copy_has_props = copy_props;2679 stbte__ui.copy_src = tm; // used to give better semantics when copying links2680 stbte__ui.copy_src_x = stbte__ui.select_x0;2681 stbte__ui.copy_src_y = stbte__ui.select_y0;2682}26832684static int stbte__in_rect(int x, int y, int x0, int y0, int w, int h)2685{2686 return x >= x0 && x < x0+w && y >= y0 && y < y0+h;2687}26882689static int stbte__in_src_rect(int x, int y)2690{2691 return stbte__in_rect(x,y, stbte__ui.copy_src_x, stbte__ui.copy_src_y, stbte__ui.copy_width, stbte__ui.copy_height);2692}26932694static int stbte__in_dest_rect(int x, int y, int destx, int desty)2695{2696 return stbte__in_rect(x,y, destx, desty, stbte__ui.copy_width, stbte__ui.copy_height);2697}26982699static void stbte__paste(stbte_tilemap *tm, int mapx, int mapy)2700{2701 int w = stbte__ui.copy_width;2702 int h = stbte__ui.copy_height;2703 int i,j,k,p;2704 int x = mapx - (w>>1);2705 int y = mapy - (h>>1);2706 int copy_props = stbte__should_copy_properties(tm) && stbte__ui.copy_has_props;2707 if (stbte__ui.has_copy == 0)2708 return;2709 stbte__begin_undo(tm);2710 p = 0;
12 unmodified lines
2723 tm->data[y+j][x+i][k] = tilestack[k];2724 }2725 }2726 }2727 if (copy_props) {2728#ifdef STBTE_ALLOW_LINK2729 // need to decide how to paste a link, so there's a few cases2730 int destx = -1, desty = -1;2731 stbte__link *link = &stbte__ui.copylinks[p];27322733 // check if link is within-rect2734 if (stbte__in_src_rect(link->x, link->y)) {2735 // new link should point to copy (but only if copy is within map)2736 destx = x + (link->x - stbte__ui.copy_src_x);2737 desty = y + (link->y - stbte__ui.copy_src_y);2738 } else if (tm == stbte__ui.copy_src) {2739 // if same map, then preserve link unless target is overwritten2740 if (!stbte__in_dest_rect(link->x,link->y,x,y)) {2741 destx = link->x;2742 desty = link->y;2743 }2744 }2745 // this is necessary for offset-copy, but also in case max_x/max_y has changed2746 if (destx < 0 || destx >= tm->max_x || desty < 0 || desty >= tm->max_y)2747 destx = -1, desty = -1;2748 stbte__set_link(tm, x+i, y+j, destx, desty, STBTE__undo_record);2749#endif2750 for (k=0; k < STBTE_MAX_PROPERTIES; ++k) {2751 if (tm->props[y+j][x+i][k] != stbte__ui.copyprops[p][k])2752 stbte__undo_record_prop_float(tm, x+i, y+j, k, tm->props[y+j][x+i][k]);2753 }2754 stbte__copy_properties(tm->props[y+j][x+i], stbte__ui.copyprops[p]);2755 }2756 ++p;2757 }2758 }2759 stbte__end_undo(tm);2760}27612479static void stbte__drag_update(stbte_tilemap *tm, int mapx, int mapy)2762static void stbte__drag_update(stbte_tilemap *tm, int mapx, int mapy, int copy_props)2763{2764 int w = stbte__ui.drag_w, h = stbte__ui.drag_h;2482 int ox,oy,i;2765 int ox,oy,i,deleted=0,written=0;2766 short temp[STBTE_MAX_LAYERS];2767 short *data = NULL;2768 if (!stbte__ui.shift) {2769 ox = mapx - stbte__ui.drag_x;2770 oy = mapy - stbte__ui.drag_y;2771 if (ox >= 0 && ox < w && oy >= 0 && oy < h) {2772 deleted=1;2773 for (i=0; i < tm->num_layers; ++i)2774 temp[i] = tm->data[mapy][mapx][i];2775 data = temp;2776 stbte__clear_stack(tm, data);2777 }2778 }2779 ox = mapx - stbte__ui.drag_dest_x;2780 oy = mapy - stbte__ui.drag_dest_y;2781 // if this map square is in the target drag region2782 if (ox >= 0 && ox < w && oy >= 0 && oy < h) {2783 // and the src map square is on the map2784 if (stbte__in_rect(stbte__ui.drag_x+ox, stbte__ui.drag_y+oy, 0, 0, tm->max_x, tm->max_y)) {2785 written = 1;2498 if (data == NULL) {2499 for (i=0; i < tm->num_layers; ++i)2500 temp[i] = tm->data[mapy][mapx][i];2501 data = temp;2786 if (data == NULL) {2787 for (i=0; i < tm->num_layers; ++i)2788 temp[i] = tm->data[mapy][mapx][i];2789 data = temp;2790 }2791 stbte__paste_stack(tm, data, data, tm->data[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox], !stbte__ui.shift);2792 if (copy_props) {2793 for (i=0; i < STBTE_MAX_PROPERTIES; ++i) {2794 if (tm->props[mapy][mapx][i] != tm->props[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox][i]) {2795 stbte__undo_record_prop_float(tm, mapx, mapy, i, tm->props[mapy][mapx][i]);2796 tm->props[mapy][mapx][i] = tm->props[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox][i];2797 }2798 }2799 }2800 }2503 stbte__paste_stack(tm, data, data, tm->data[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox], !stbte__ui.shift);2801 }2802 if (data) {2803 for (i=0; i < tm->num_layers; ++i) {2804 if (tm->data[mapy][mapx][i] != data[i]) {
1 unmodified line
2806 tm->data[mapy][mapx][i] = data[i];2807 }2808 }2809 }2810 #ifdef STBTE_ALLOW_LINK2811 if (copy_props) {2812 int overwritten=0, moved=0, copied=0;2813 // since this function is called on EVERY tile, we can fix up even tiles not2814 // involved in the move28152816 stbte__link *k;2817 // first, determine what src link ends up here2818 k = &tm->link[mapy][mapx]; // by default, it's the one currently here2819 if (deleted) // if dragged away, it's erased2820 k = NULL;2821 if (written) // if dragged into, it gets that link2822 k = &tm->link[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox];28232824 // now check whether the *target* gets moved or overwritten2825 if (k && k->x >= 0) {2826 overwritten = stbte__in_rect(k->x, k->y, stbte__ui.drag_dest_x, stbte__ui.drag_dest_y, w, h);2827 if (!stbte__ui.shift)2828 moved = stbte__in_rect(k->x, k->y, stbte__ui.drag_x , stbte__ui.drag_y , w, h);2829 else2830 copied = stbte__in_rect(k->x, k->y, stbte__ui.drag_x , stbte__ui.drag_y , w, h);2831 }28322833 if (deleted || written || overwritten || moved || copied) {2834 // choose the final link value based on the above2835 if (k == NULL || k->x < 0)2836 stbte__set_link(tm, mapx, mapy, -1, -1, STBTE__undo_record);2837 else if (moved || (copied && written)) {2838 // if we move the target, we update to point to the new target;2839 // or, if we copy the target and the source is part ofthe copy, then update to new target2840 int x = k->x + (stbte__ui.drag_dest_x - stbte__ui.drag_x);2841 int y = k->y + (stbte__ui.drag_dest_y - stbte__ui.drag_y);2842 if (!(x >= 0 && y >= 0 && x < tm->max_x && y < tm->max_y))2843 x = -1, y = -1;2844 stbte__set_link(tm, mapx, mapy, x, y, STBTE__undo_record);2845 } else if (overwritten) {2846 stbte__set_link(tm, mapx, mapy, -1, -1, STBTE__undo_record);2847 } else2848 stbte__set_link(tm, mapx, mapy, k->x, k->y, STBTE__undo_record);2849 }2850 }2851 #endif2852}28532854static void stbte__drag_place(stbte_tilemap *tm, int mapx, int mapy)2855{2856 int i,j;2857 int copy_props = stbte__should_copy_properties(tm);2858 int move_x = (stbte__ui.drag_dest_x - stbte__ui.drag_x);2859 int move_y = (stbte__ui.drag_dest_y - stbte__ui.drag_y);2860 if (move_x == 0 && move_y == 0)2861 return;
4 unmodified lines
2866 // direction sensitive in the same way as memmove2867 if (move_y > 0 || (move_y == 0 && move_x > 0)) {2868 for (j=tm->max_y-1; j >= 0; --j)2869 for (i=tm->max_x-1; i >= 0; --i)2530 stbte__drag_update(tm,i,j);2870 stbte__drag_update(tm,i,j,copy_props);2871 } else {2872 for (j=0; j < tm->max_y; ++j)2873 for (i=0; i < tm->max_x; ++i)2534 stbte__drag_update(tm,i,j);2874 stbte__drag_update(tm,i,j,copy_props);2875 }2876 stbte__end_undo(tm);28772878 stbte__ui.has_selection = 1;2879 stbte__ui.select_x0 = stbte__ui.drag_dest_x;2880 stbte__ui.select_y0 = stbte__ui.drag_dest_y;2541 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;2881 stbte__ui.select_x1 = stbte__ui.select_x0 + stbte__ui.drag_w - 1;2882 stbte__ui.select_y1 = stbte__ui.select_y0 + stbte__ui.drag_h - 1;2883}28842885static void stbte__tile_paint(stbte_tilemap *tm, int sx, int sy, int mapx, int mapy, int layer)2886{
110 unmodified lines
2997 if (stbte__ui.show_links && tm->link[mapy][mapx].x >= 0) {2998 int tx = tm->link[mapy][mapx].x;2999 int ty = tm->link[mapy][mapx].y;3000 int lx0,ly0,lx1,ly1;3001 if (STBTE_ALLOW_LINK(tm->data[mapy][mapx], tm->props[mapy][mapx],3002 tm->data[ty ][tx ], tm->props[ty ][tx ]))3003 {2661 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;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]));3004 lx0 = x0 + (tm->spacing_x >> 1) - 1;3005 ly0 = y0 + (tm->spacing_y >> 1) - 1;3006 lx1 = lx0 + (tx - mapx) * tm->spacing_x + 2;3007 ly1 = ly0 + (ty - mapy) * tm->spacing_y + 2;3008 stbte__draw_link(lx0,ly0,lx1,ly1,3009 STBTE_LINK_COLOR(tm->data[mapy][mapx], tm->props[mapy][mapx],3010 tm->data[ty ][tx ], tm->props[ty ][tx]));3011 }3012 }3013#endif3014 break;3015 }
102 unmodified lines
3118 if (STBTE__IS_HOT(id) && STBTE__IS_MAP_ACTIVE()) {3119 if ((mapx != stbte__ui.sx || mapy != stbte__ui.sy) &&3120 STBTE_ALLOW_LINK(tm->data[stbte__ui.sy][stbte__ui.sx], tm->props[stbte__ui.sy][stbte__ui.sx],3121 tm->data[mapy][mapx], tm->props[mapy][mapx]))2778 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, mapx, mapy);3122 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, mapx, mapy, STBTE__undo_block);3123 else2780 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, -1,-1);3124 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, -1,-1, STBTE__undo_block);3125 stbte__ui.linking = 0;3126 stbte__activate(0);3127 }3128 break;
236 unmodified lines
3365}33663367static void stbte__layers(stbte_tilemap *tm, int x0, int y0, int w, int h)3368{3025 int i, y;3369 int i, y, n;3370 int x1 = x0+w;3371 int y1 = y0+h;3372 int xoff = tm->has_layer_names ? 50 : 20;3373 static char *propmodes[3] = {3374 "default", "always", "never"3375 };3376 int num_rows;3377 x0 += 2;3378 y0 += 5;3379 if (!tm->has_layer_names) {
2 unmodified lines
3382 }3383 y0 += 11;3384 }3385 num_rows = (y1-y0)/15;3386#ifndef STBTE_NO_PROPS3387 --num_rows;3388#endif3389 y = y0;3390 for (i=0; i < tm->num_layers; ++i) {3391 char text[3], *str = (char *) tm->layerinfo[i].name;3392 static char lockedchar[3] = { 'U', 'P', 'L' };
12 unmodified lines
3405 tm->solo_layer = (tm->solo_layer == i ? -1 : i);3406 y += 15;3407 }3408 }3059 stbte__scrollbar(x1-4, y0,y1-2, &tm->layer_scroll, 0, tm->num_layers, num_rows, STBTE__ID(STBTE__scrollbar_id, STBTE__layer));3409 stbte__scrollbar(x1-4, y0,y-2, &tm->layer_scroll, 0, tm->num_layers, num_rows, STBTE__ID(STBTE__scrollbar_id, STBTE__layer));3410#ifndef STBTE_NO_PROPS3411 n = stbte__text_width("prop:")+2;3412 stbte__draw_text(x0,y+2, "prop:", w, STBTE__TEXTCOLOR(STBTE__cpanel));3413 i = w - n - 4;3414 if (i > 45) i = 45;3415 if (stbte__button(STBTE__clayer_button, propmodes[tm->propmode], x0+n,y,0,i, STBTE__ID(STBTE__layer,256), 0,0))3416 tm->propmode = (tm->propmode+1)%3;3417#endif3418}34193420static void stbte__categories(stbte_tilemap *tm, int x0, int y0, int w, int h)3421{
104 unmodified lines
3526 mx = stbte__ui.select_x0;3527 my = stbte__ui.select_y0;3528 p = tm->props[my][mx];3529 data = tm->data[my][mx];3172 for (i=0; i < STBTE_MAX_PROP; ++i) {3530 for (i=0; i < STBTE_MAX_PROPERTIES; ++i) {3531 unsigned int n = STBTE_PROP_TYPE(i, data, p);3532 if (n) {3533 char *s = STBTE_PROP_NAME(i, data, p);3534 if (s == NULL) s = "";
547 unmodified lines