Skip to content
Myr

stb_tilemap_editor.h

This file

Path

stb_tilemap_editor.h

4,172 lines · 142.9 KiB · C

Last changed by

change license to public-domain AND mit (based on twitter vote)Sean Barrettbefore adoptione248e30

History of this file

Take it

1// stb_tilemap_editor.h - v0.38 - 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// 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//19// 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: The editor appears to be put map location (0,0) at the top left?67//      I want to use a different coordinate system in my game (e.g. y68//      increasing upwards, or origin at the center).69//70//   A: You can do this when you convert from the editor's map71//      representation to the game representation. (Don't forget to72//      translate link coordinates as well!)73//74//   Q: The editor appears to put pixel (0,0) at the top left? I want75//      to use a different coordinate system in my game.76//77//   A: The editor defines an "editor pixel coordinate system" with78//      (0,0) at the top left and requires you to display things in79//      that coordinate system. You can freely remap those coordinates80//      to anything you want on screen.81//82//   Q: How do I scale the user interface?83//84//   A: Since you do all the rendering, you can scale up all the rendering85//      calls that the library makes to you. If you do, (a) you need86//      to also scale up the mouse coordinates, and (b) you may want87//      to scale the map display back down so that you're only scaling88//      the UI and not everything. See the next question.89//90//   Q: How do I scale the map display?91//92//   A: Use stbte_set_spacing() to change the size that the map is displayed93//      at. Note that the "callbacks" to draw tiles are used for both drawing94//      the map and drawing the tile palette, so that callback may need to95//      draw at two different scales. You should choose the scales to match96//       You can tell them apart because the97//      tile palette gets NULL for the property pointer.98//99//   Q: How does object editing work?100//101//   A: One way to think of this is that in the editor, you're placing102//      spawners, not objects. Each spawner must be tile-aligned, because103//      it's only a tile editor. Each tile (stack of layers) gets104//      an associated set of properties, and it's up to you to105//      determine what properties should appear for a given tile,106//      based on e.g. the spawners that are in it.107//108//   Q: How are properties themselves handled?109//110//   A: All properties, regardless of UI behavior, are internally floats.111//      Each tile has an array of floats associated with it, which is112//      passed back to you when drawing the tiles so you can draw113//      objects appropriately modified by the properties.114//115//   Q: What if I want to have two different objects/spawners in116//      one tile, both of which have their own properties?117//118//   A: Make sure STBTE_MAX_PROPERTIES is large enough for the sum of119//      properties in both objects, and then you have to explicitly120//      map the property slot #s to the appropriate objects. They'll121//      still all appear in a single property panel; there's no way122//      to get multiple panels.123//124//   Q: Can I do one-to-many linking?125//126//   A: The library only supports one link per tile. However, you127//      can have multiple tiles all link to a single tile. So, you128//      can fake one-to-many linking by linking in the reverse129//      direction.130//131//   Q: What if I have two objects in the same tile, and they each132//      need an independent link? Or I have two kinds of link associated133//      with a single object?134//135//   A: There is no way to do this. (Unless you can reverse one link.)136//137//   Q: How does cut & paste interact with object properties & links?138//139//   A: Currently the library has no idea which properties or links140//      are associated with which layers of a tile. So currently, the141//      library will only copy properties & links if the layer panel142//      is set to allow all layers to be copied, OR if you set the143//      "props" in the layer panel to "always". Similarly, you can144//      set "props" to "none" so it will never copy.145//146//   Q: What happens if the library gets a memory allocation failure147//      while I'm editing? Will I lose my work?148//149//   A: The library allocates all editor memory when you create150//      the tilemap. It allocates a maximally-sized map and a 151//      fixed-size undo buffer (and the fixed-size copy buffer152//      is static), and never allocates memory while it's running.153//      So it can't fail due to running out of memory.154//155//   Q: What happens if the library crashes while I'm editing? Will156//      I lose my work?157//158//   A: Yes. Save often.159//160//161// HOW TO COMPILE162//163//   This header file contains both the header file and the164//   implementation file in one. To create the implementation,165//   in one source file define a few symbols first and then166//   include this header:167//168//      #define STB_TILEMAP_EDITOR_IMPLEMENTATION169//      // this triggers the implementation170//171//      void STBTE_DRAW_RECT(int x0, int y0, int x1, int y1, uint color);172//      // this must draw a filled rectangle (exclusive on right/bottom)173//      // color = (r<<16)|(g<<8)|(b)174//      175//      void STBTE_DRAW_TILE(int x0, int y0,176//                    unsigned short id, int highlight, float *data);177//      // this draws the tile image identified by 'id' in one of several178//      // highlight modes (see STBTE_drawmode_* in the header section);179//      // if 'data' is NULL, it's drawing the tile in the palette; if 'data'180//      // is not NULL, it's drawing a tile on the map, and that is the data181//      // associated with that map tile182//183//      #include "stb_tilemap_editor.h"184//185//   Optionally you can define the following functions before the include;186//   note these must be macros (but they can just call a function) so187//   this library can #ifdef to detect if you've defined them:188//189//      #define STBTE_PROP_TYPE(int n, short *tiledata, float *params) ...190//      // Returns the type of the n'th property of a given tile, which191//      // controls how it is edited. Legal types are:192//      //     0                    /* no editable property in this slot */193//      //     STBTE_PROP_int       /* uses a slider to adjust value     */194//      //     STBTE_PROP_float     /* uses a weird multi-axis control   */195//      //     STBTE_PROP_bool      /* uses a checkbox to change value   */196//      // And you can bitwise-OR in the following flags:197//      //     STBTE_PROP_disabled198//      // Note that all of these are stored as floats in the param array.199//      // The integer slider is limited in precision based on the space200//      // available on screen, so for wide-ranged integers you may want201//      // to use floats instead.202//      //203//      // Since the tiledata is passed to you, you can choose which property204//      // is bound to that slot based on that data.205//      //206//      // Changing the type of a parameter does not cause the underlying207//      // value to be clamped to the type min/max except when the tile is208//      // explicitly selected.209// 210//      #define STBTE_PROP_NAME(int n, short *tiledata, float *params) ...211//      // these return a string with the name for slot #n in the float212//      // property list for the tile.213//214//      #define STBTE_PROP_MIN(int n, short *tiledata) ...your code here...215//      #define STBTE_PROP_MAX(int n, short *tiledata) ...your code here...216//      // These return the allowable range for the property values for217//      // the specified slot. It is never called for boolean types.218//219//      #define STBTE_PROP_FLOAT_SCALE(int n, short *tiledata, float *params)220//      // This rescales the float control for a given property; by default221//      // left mouse drags add integers, right mouse drags adds fractions,222//      // but you can rescale this per-property.223//224//      #define STBTE_FLOAT_CONTROL_GRANULARITY       ... value ...225//      // This returns the number of pixels of mouse motion necessary226//      // to advance the object float control. Default is 4227//228//      #define STBTE_ALLOW_LINK(short *src, float *src_data,  \229//                               short *dest, float *dest_data) ...your code...230//      // this returns true or false depending on whether you allow a link231//      // to be drawn from a tile 'src' to a tile 'dest'. if you don't232//      // define this, linking will not be supported233//234//      #define STBTE_LINK_COLOR(short *src, float *src_data,  \235//                               short *dest, float *dest_data) ...your code...236//      // return a color encoded as a 24-bit unsigned integer in the237//      // form 0xRRGGBB. If you don't define this, default colors will238//      // be used.239//240//241//      [[ support for those below is not implemented yet ]]242//243//      #define STBTE_HITTEST_TILE(x0,y0,id,mx,my)   ...your code here...244//      // this returns true or false depending on whether the mouse245//      // pointer at mx,my is over (touching) a tile of type 'id'246//      // displayed at x0,y0. Normally stb_tilemap_editor just does247//      // this hittest based on the tile geometry, but if you have248//      // tiles whose images extend out of the tile, you'll need this.249//250// ADDITIONAL CONFIGURATION251//252//   The following symbols set static limits which determine how much253//   memory will be allocated for the editor. You can override them254//   by making similiar definitions, but memory usage will increase.255//256//      #define STBTE_MAX_TILEMAP_X      200   // max 4096257//      #define STBTE_MAX_TILEMAP_Y      200   // max 4096258//      #define STBTE_MAX_LAYERS         8     // max 32259//      #define STBTE_MAX_CATEGORIES     100260//      #define STBTE_UNDO_BUFFER_BYTES  (1 << 24) // 16 MB261//      #define STBTE_MAX_COPY           90000  // e.g. 300x300262//      #define STBTE_MAX_PROPERTIES     10     // max properties per tile263//264// API265//266//   Further documentation appears in the header-file section below.267//268// EDITING MULTIPLE LEVELS269//270//   You can only have one active editor instance. To switch between multiple271//   levels, you can either store the levels in your own format and copy them272//   in and out of the editor format, or you can create multiple stbte_tilemap273//   objects and switch between them. The latter has the advantage that each274//   stbte_tilemap keeps its own undo state. (The clipboard is global, so275//   either approach allows cut&pasting between levels.)276//277// REVISION HISTORY278//   0.38  fix warning279//   0.37  fix warning280//   0.36  minor compiler support281//   0.35  layername button changes282//          - layername buttons grow with the layer panel283//          - fix stbte_create_map being declared as stbte_create284//          - fix declaration of stbte_create_map285//   0.30  properties release286//          - properties panel for editing user-defined "object" properties287//          - can link each tile to one other tile288//          - keyboard interface289//          - fix eraser tool bug (worked in complex cases, failed in simple)290//          - undo/redo tools have visible disabled state291//          - tiles on higher layers draw on top of adjacent lower-layer tiles292//   0.20  erasable release293//          - eraser tool294//          - fix bug when pasting into protected layer295//          - better color scheme296//          - internal-use color picker297//   0.10  initial release 298//299// TODO300//301//   Separate scroll state for each category302//   Implement paint bucket303//   Support STBTE_HITTEST_TILE above304//  ?Cancel drags by clicking other button? - may be fixed305//   Finish support for toolbar at side306//307// CREDITS308//309//310//   Main editor & features311//      Sean Barrett312//   Additional features:313//      Josh Huelsman314//   Bugfixes:315//      Ryan Whitworth316//      Eugene Opalev317//318// LICENSE319//320//   See end of file for license information.321322323324///////////////////////////////////////////////////////////////////////325//326//   HEADER SECTION327328#ifndef STB_TILEMAP_INCLUDE_STB_TILEMAP_EDITOR_H329#define STB_TILEMAP_INCLUDE_STB_TILEMAP_EDITOR_H330331#ifdef _WIN32332  #ifndef _CRT_SECURE_NO_WARNINGS333  #define _CRT_SECURE_NO_WARNINGS334  #endif335  #include <stdlib.h>336  #include <stdio.h>337#endif338339typedef struct stbte_tilemap stbte_tilemap;340341// these are the drawmodes used in STBTE_DRAW_TILE342enum343{344   STBTE_drawmode_deemphasize = -1,345   STBTE_drawmode_normal      =  0,346   STBTE_drawmode_emphasize   =  1,347};348349// these are the property types350#define STBTE_PROP_none     0351#define STBTE_PROP_int      1352#define STBTE_PROP_float    2353#define STBTE_PROP_bool     3354#define STBTE_PROP_disabled 4355356////////357//358// creation359//360361extern stbte_tilemap *stbte_create_map(int map_x, int map_y, int map_layers, int spacing_x, int spacing_y, int max_tiles);362// create an editable tilemap363//   map_x      : dimensions of map horizontally (user can change this in editor), <= STBTE_MAX_TILEMAP_X364//   map_y      : dimensions of map vertically (user can change this in editor)    <= STBTE_MAX_TILEMAP_Y365//   map_layers : number of layers to use (fixed), <= STBTE_MAX_LAYERS366//   spacing_x  : initial horizontal distance between left edges of map tiles in stb_tilemap_editor pixels367//   spacing_y  : initial vertical distance between top edges of map tiles in stb_tilemap_editor pixels368//   max_tiles  : maximum number of tiles that can defined369//370// If insufficient memory, returns NULL371372extern void stbte_define_tile(stbte_tilemap *tm, unsigned short id, unsigned int layermask, const char * category);373// call this repeatedly for each tile to install the tile definitions into the editable tilemap374//   tm        : tilemap created by stbte_create_map375//   id        : unique identifier for each tile, 0 <= id < 32768376//   layermask : bitmask of which layers tile is allowed on: 1 = layer 0, 255 = layers 0..7377//               (note that onscreen, the editor numbers the layers from 1 not 0)378//               layer 0 is the furthest back, layer 1 is just in front of layer 0, etc379//   category  : which category this tile is grouped in380381extern void stbte_set_display(int x0, int y0, int x1, int y1);382// call this once to set the size; if you resize, call it again383384385/////////386//387// every frame388//389390extern void stbte_draw(stbte_tilemap *tm);391392extern void stbte_tick(stbte_tilemap *tm, float time_in_seconds_since_last_frame);393394////////////395//396//  user input397//398399// if you're using SDL, call the next function for SDL_MOUSEMOVE, SDL_MOUSEBUTTON, SDL_MOUSEWHEEL;400// the transformation lets you scale from SDL mouse coords to stb_tilemap_editor coords401extern void stbte_mouse_sdl(stbte_tilemap *tm, const void *sdl_event, float xscale, float yscale, int xoffset, int yoffset);402403// otherwise, hook these up explicitly:404extern void stbte_mouse_move(stbte_tilemap *tm, int x, int y, int shifted, int scrollkey);405extern void stbte_mouse_button(stbte_tilemap *tm, int x, int y, int right, int down, int shifted, int scrollkey);406extern void stbte_mouse_wheel(stbte_tilemap *tm, int x, int y, int vscroll);407408// for keyboard, define your own mapping from keys to the following actions.409// this is totally optional, as all features are accessible with the mouse410enum stbte_action411{412   STBTE_tool_select,413   STBTE_tool_brush,414   STBTE_tool_erase,415   STBTE_tool_rectangle,416   STBTE_tool_eyedropper,417   STBTE_tool_link,418   STBTE_act_toggle_grid,419   STBTE_act_toggle_links,420   STBTE_act_undo,421   STBTE_act_redo,422   STBTE_act_cut,423   STBTE_act_copy,424   STBTE_act_paste,425   STBTE_scroll_left,426   STBTE_scroll_right,427   STBTE_scroll_up,428   STBTE_scroll_down,429};430extern void stbte_action(stbte_tilemap *tm, enum stbte_action act);431432////////////////433//434//  save/load 435//436//  There is no editor file format. You have to save and load the data yourself437//  through the following functions. You can also use these functions to get the438//  data to generate game-formatted levels directly. (But make sure you save439//  first! You may also want to autosave to a temp file periodically, etc etc.)440441#define STBTE_EMPTY    -1442443extern void stbte_get_dimensions(stbte_tilemap *tm, int *max_x, int *max_y);444// get the dimensions of the level, since the user can change them445446extern short* stbte_get_tile(stbte_tilemap *tm, int x, int y);447// returns an array of shorts that is 'map_layers' in length. each short is448// either one of the tile_id values from define_tile, or STBTE_EMPTY.449450extern float *stbte_get_properties(stbte_tilemap *tm, int x, int y);451// get the property array associated with the tile at x,y. this is an452// array of floats that is STBTE_MAX_PROPERTIES in length; you have to453// interpret the slots according to the semantics you've chosen454455extern void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty);456// gets the link associated with the tile at x,y.457458extern void stbte_set_dimensions(stbte_tilemap *tm, int max_x, int max_y);459// set the dimensions of the level, overrides previous stbte_create_map()460// values or anything the user has changed461462extern void stbte_clear_map(stbte_tilemap *tm);463// clears the map, including the region outside the defined region, so if the464// user expands the map, they won't see garbage there465466extern void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile);467// tile is your tile_id from define_tile, or STBTE_EMPTY468469extern void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val);470// set the value of the n'th slot of the tile at x,y471472extern void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty);473// set a link going from x,y to destx,desty. to force no link,474// use destx=desty=-1475476////////477//478// optional479//480481extern void stbte_set_background_tile(stbte_tilemap *tm, short id);482// selects the tile to fill the bottom layer with and used to clear bottom tiles to;483// should be same ID as 484485extern void stbte_set_sidewidths(int left, int right);486// call this once to set the left & right side widths. don't call487// it again since the user can change it488489extern void stbte_set_spacing(stbte_tilemap *tm, int spacing_x, int spacing_y, int palette_spacing_x, int palette_spacing_y);490// call this to set the spacing of map tiles and the spacing of palette tiles.491// if you rescale your display, call it again (e.g. you can implement map zooming yourself)492493extern void stbte_set_layername(stbte_tilemap *tm, int layer, const char *layername);494// sets a string name for your layer that shows in the layer selector. note that this495// makes the layer selector wider. 'layer' is from 0..(map_layers-1)496497#endif498499#ifdef STB_TILEMAP_EDITOR_IMPLEMENTATION500501#ifndef STBTE_ASSERT502#define STBTE_ASSERT assert503#include <assert.h>504#endif505506#ifdef _MSC_VER507#define STBTE__NOTUSED(v)  (void)(v)508#else509#define STBTE__NOTUSED(v)  (void)sizeof(v)510#endif511512#ifndef STBTE_MAX_TILEMAP_X513#define STBTE_MAX_TILEMAP_X      200514#endif515516#ifndef STBTE_MAX_TILEMAP_Y517#define STBTE_MAX_TILEMAP_Y      200518#endif519520#ifndef STBTE_MAX_LAYERS521#define STBTE_MAX_LAYERS         8522#endif523524#ifndef STBTE_MAX_CATEGORIES525#define STBTE_MAX_CATEGORIES     100526#endif527528#ifndef STBTE_MAX_COPY529#define STBTE_MAX_COPY           65536530#endif531532#ifndef STBTE_UNDO_BUFFER_BYTES533#define STBTE_UNDO_BUFFER_BYTES  (1 << 24) // 16 MB534#endif535536#ifndef STBTE_PROP_TYPE537#define STBTE__NO_PROPS538#define STBTE_PROP_TYPE(n,td,tp)   0539#endif540541#ifndef STBTE_PROP_NAME542#define STBTE_PROP_NAME(n,td,tp)  ""543#endif544545#ifndef STBTE_MAX_PROPERTIES546#define STBTE_MAX_PROPERTIES           10547#endif548549#ifndef STBTE_PROP_MIN550#define STBTE_PROP_MIN(n,td,tp)  0551#endif552553#ifndef STBTE_PROP_MAX554#define STBTE_PROP_MAX(n,td,tp)  100.0555#endif556557#ifndef STBTE_PROP_FLOAT_SCALE558#define STBTE_PROP_FLOAT_SCALE(n,td,tp)  1   // default scale size559#endif560561#ifndef STBTE_FLOAT_CONTROL_GRANULARITY562#define STBTE_FLOAT_CONTROL_GRANULARITY 4563#endif564565566#define STBTE__UNDO_BUFFER_COUNT  (STBTE_UNDO_BUFFER_BYTES>>1)567568#if STBTE_MAX_TILEMAP_X > 4096 || STBTE_MAX_TILEMAP_Y > 4096569#error "Maximum editable map size is 4096 x 4096"570#endif571#if STBTE_MAX_LAYERS > 32572#error "Maximum layers allowed is 32"573#endif574#if STBTE_UNDO_BUFFER_COUNT & (STBTE_UNDO_BUFFER_COUNT-1)575#error "Undo buffer size must be a power of 2"576#endif577578#if STBTE_MAX_PROPERTIES == 0579#define STBTE__NO_PROPS580#endif581582#ifdef STBTE__NO_PROPS583#undef STBTE_MAX_PROPERTIES584#define STBTE_MAX_PROPERTIES 1  // so we can declare arrays585#endif586587typedef struct588{589   short x,y;590} stbte__link;591592enum593{594   STBTE__base,595   STBTE__outline,596   STBTE__text,597598   STBTE__num_color_aspects,599};600601enum602{603   STBTE__idle,604   STBTE__over,605   STBTE__down,606   STBTE__over_down,607   STBTE__selected,608   STBTE__selected_over,609   STBTE__disabled,610   STBTE__num_color_states,611};612613enum614{615   STBTE__cexpander,616   STBTE__ctoolbar,617   STBTE__ctoolbar_button,618   STBTE__cpanel,619   STBTE__cpanel_sider,620   STBTE__cpanel_sizer,621   STBTE__cscrollbar,622   STBTE__cmapsize,623   STBTE__clayer_button,624   STBTE__clayer_hide,625   STBTE__clayer_lock,626   STBTE__clayer_solo,627   STBTE__ccategory_button,628629   STBTE__num_color_modes,630};631632#ifdef STBTE__COLORPICKER633static char *stbte__color_names[] =634{635   "expander", "toolbar", "tool button", "panel",636   "panel c1", "panel c2", "scollbar", "map button",637   "layer", "hide", "lock", "solo",638   "category",639};640#endif // STBTE__COLORPICKER641642      // idle,    over,     down,    over&down, selected, sel&over, disabled643static int stbte__color_table[STBTE__num_color_modes][STBTE__num_color_aspects][STBTE__num_color_states] =644{645   {646      { 0x000000, 0x84987c, 0xdcdca8, 0xdcdca8, 0x40c040, 0x60d060, 0x505050, },647      { 0xa4b090, 0xe0ec80, 0xffffc0, 0xffffc0, 0x80ff80, 0x80ff80, 0x606060, },648      { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },649   }, {650      { 0x808890, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, },651      { 0x605860, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, },652      { 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, },653   }, {654      { 0x3c5068, 0x7088a8, 0x647488, 0x94b4dc, 0x8890c4, 0x9caccc, 0x404040, },655      { 0x889cb8, 0x889cb8, 0x889cb8, 0x889cb8, 0x84c4e8, 0xacc8ff, 0x0c0c08, },656      { 0xbcc4cc, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x707074, },657   }, {658      { 0x403848, 0x403010, 0x403010, 0x403010, 0x403010, 0x403010, 0x303024, },659      { 0x68546c, 0xc08040, 0xc08040, 0xc08040, 0xc08040, 0xc08040, 0x605030, },660      { 0xf4e4ff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },661   }, {662      { 0xb4b04c, 0xacac60, 0xc0ffc0, 0xc0ffc0, 0x40c040, 0x60d060, 0x505050, },663      { 0xa0a04c, 0xd0d04c, 0xffff80, 0xffff80, 0x80ff80, 0x80ff80, 0x606060, },664      { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },665   }, {666      { 0x40c440, 0x60d060, 0xc0ffc0, 0xc0ffc0, 0x40c040, 0x60d060, 0x505050, },667      { 0x40c040, 0x80ff80, 0x80ff80, 0x80ff80, 0x80ff80, 0x80ff80, 0x606060, },668      { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },669   }, {670      { 0x9090ac, 0xa0a0b8, 0xbcb8cc, 0xbcb8cc, 0x909040, 0x909040, 0x909040, },671      { 0xa0a0b8, 0xb0b4d0, 0xa0a0b8, 0xa0a0b8, 0xa0a050, 0xa0a050, 0xa0a050, },672      { 0x808088, 0x808030, 0x808030, 0x808030, 0x808030, 0x808030, 0x808030, },673   }, {674      { 0x704c70, 0x885c8c, 0x9c68a4, 0xb870bc, 0xb490bc, 0xb490bc, 0x302828, },675      { 0x646064, 0xcca8d4, 0xc060c0, 0xa07898, 0xe0b8e0, 0xe0b8e0, 0x403838, },676      { 0xdccce4, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },677   }, {678      { 0x704c70, 0x885c8c, 0x9c68a4, 0xb870bc, 0xb490bc, 0xb490bc, 0x302828, },679      { 0xb09cb4, 0xcca8d4, 0xc060c0, 0xa07898, 0xe0b8e0, 0xe0b8e0, 0x403838, },680      { 0xdccce4, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },681   }, {682      { 0x646494, 0x888cb8, 0xb0b0b0, 0xb0b0cc, 0x9c9cf4, 0x8888b0, 0x50506c, },683      { 0x9090a4, 0xb0b4d4, 0xb0b0dc, 0xb0b0cc, 0xd0d0fc, 0xd0d4f0, 0x606060, },684      { 0xb4b4d4, 0xe4e4ff, 0xffffff, 0xffffff, 0xe0e4ff, 0xececff, 0x909090, },685   }, {686      { 0x646444, 0x888c64, 0xb0b0b0, 0xb0b088, 0xaca858, 0x88886c, 0x505050, },687      { 0x88886c, 0xb0b490, 0xb0b0b0, 0xb0b088, 0xd8d898, 0xd0d4b0, 0x606060, },688      { 0xb4b49c, 0xffffd8, 0xffffff, 0xffffd4, 0xffffdc, 0xffffcc, 0x909090, },689   }, {690      { 0x906464, 0xb48c8c, 0xd4b0b0, 0xdcb0b0, 0xff9c9c, 0xc88888, 0x505050, },691      { 0xb47c80, 0xd4b4b8, 0xc4a8a8, 0xdcb0b0, 0xffc0c0, 0xfce8ec, 0x606060, },692      { 0xe0b4b4, 0xffdcd8, 0xffd8d4, 0xffe0e4, 0xffece8, 0xffffff, 0x909090, },693   }, {694      { 0x403848, 0x403848, 0x403848, 0x886894, 0x7c80c8, 0x7c80c8, 0x302828, },695      { 0x403848, 0x403848, 0x403848, 0x403848, 0x7c80c8, 0x7c80c8, 0x403838, },696      { 0xc8c4c8, 0xffffff, 0xffffff, 0xffffff, 0xe8e8ec, 0xffffff, 0x909090, },697   },698};699700#define STBTE_COLOR_TILEMAP_BACKGROUND      0x000000701#define STBTE_COLOR_TILEMAP_BORDER          0x203060702#define STBTE_COLOR_TILEMAP_HIGHLIGHT       0xffffff703#define STBTE_COLOR_GRID                    0x404040704#define STBTE_COLOR_SELECTION_OUTLINE1      0xdfdfdf705#define STBTE_COLOR_SELECTION_OUTLINE2      0x303030706#define STBTE_COLOR_TILEPALETTE_OUTLINE     0xffffff707#define STBTE_COLOR_TILEPALETTE_BACKGROUND  0x000000708709#ifndef STBTE_LINK_COLOR710#define STBTE_LINK_COLOR(src,sp,dest,dp)    0x5030ff711#endif712713#ifndef STBTE_LINK_COLOR_DRAWING714#define STBTE_LINK_COLOR_DRAWING            0xff40ff715#endif716717#ifndef STBTE_LINK_COLOR_DISALLOWED718#define STBTE_LINK_COLOR_DISALLOWED         0x602060719#endif720721722// disabled, selected, down, over723static unsigned char stbte__state_to_index[2][2][2][2] =724{725   {726      { { STBTE__idle    , STBTE__over          }, { STBTE__down    , STBTE__over_down }, },727      { { STBTE__selected, STBTE__selected_over }, { STBTE__down    , STBTE__over_down }, },728   },{729      { { STBTE__disabled, STBTE__disabled      }, { STBTE__disabled, STBTE__disabled  }, },730      { { STBTE__selected, STBTE__selected_over }, { STBTE__disabled, STBTE__disabled  }, },731   }732};733#define STBTE__INDEX_FOR_STATE(disable,select,down,over) stbte__state_to_index[disable][select][down][over]734#define STBTE__INDEX_FOR_ID(id,disable,select) STBTE__INDEX_FOR_STATE(disable,select,STBTE__IS_ACTIVE(id),STBTE__IS_HOT(id))735736#define STBTE__FONT_HEIGHT    9737static short stbte__font_offset[95+16];738static short stbte__fontdata[769] =739{740   4,9,6,9,9,9,9,8,9,8,4,9,7,7,7,7,4,2,6,8,6,6,7,3,4,4,8,6,3,6,2,6,6,6,6,6,6,741   6,6,6,6,6,2,3,5,4,5,6,6,6,6,6,6,6,6,6,6,6,6,7,6,7,7,7,6,7,6,6,6,6,7,7,6,6,742   6,4,6,4,7,7,3,6,6,5,6,6,5,6,6,4,5,6,4,7,6,6,6,6,6,6,6,6,6,7,6,6,6,5,2,5,8,743   0,0,0,0,2,253,130,456,156,8,72,184,64,2,125,66,64,160,64,146,511,146,146,744   511,146,146,511,146,511,257,341,297,341,297,341,257,511,16,56,124,16,16,16,745   124,56,16,96,144,270,261,262,136,80,48,224,192,160,80,40,22,14,15,3,448,496,746   496,240,232,20,10,5,2,112,232,452,450,225,113,58,28,63,30,60,200,455,257,747   257,0,0,0,257,257,455,120,204,132,132,159,14,4,4,14,159,132,132,204,120,8,748   24,56,120,56,24,8,32,48,56,60,56,48,32,0,0,0,0,111,111,7,7,0,0,7,7,34,127,749   127,34,34,127,127,34,36,46,107,107,58,18,99,51,24,12,102,99,48,122,79,93,750   55,114,80,4,7,3,62,127,99,65,65,99,127,62,8,42,62,28,28,62,42,8,8,8,62,62,751   8,8,128,224,96,8,8,8,8,8,8,96,96,96,48,24,12,6,3,62,127,89,77,127,62,64,66,752   127,127,64,64,98,115,89,77,71,66,33,97,73,93,119,35,24,28,22,127,127,16,39,753   103,69,69,125,57,62,127,73,73,121,48,1,1,113,121,15,7,54,127,73,73,127,54,754   6,79,73,105,63,30,54,54,128,246,118,8,28,54,99,65,20,20,20,20,65,99,54,28,755   8,2,3,105,109,7,2,30,63,33,45,47,46,124,126,19,19,126,124,127,127,73,73,127,756   54,62,127,65,65,99,34,127,127,65,99,62,28,127,127,73,73,73,65,127,127,9,9,757   9,1,62,127,65,73,121,121,127,127,8,8,127,127,65,65,127,127,65,65,32,96,64,758   64,127,63,127,127,8,28,54,99,65,127,127,64,64,64,64,127,127,6,12,6,127,127,759   127,127,6,12,24,127,127,62,127,65,65,65,127,62,127,127,9,9,15,6,62,127,65,760   81,49,127,94,127,127,9,25,127,102,70,79,73,73,121,49,1,1,127,127,1,1,63,127,761   64,64,127,63,15,31,48,96,48,31,15,127,127,48,24,48,127,127,99,119,28,28,119,762   99,7,15,120,120,15,7,97,113,89,77,71,67,127,127,65,65,3,6,12,24,48,96,65,763   65,127,127,8,12,6,3,6,12,8,64,64,64,64,64,64,64,3,7,4,32,116,84,84,124,120,764   127,127,68,68,124,56,56,124,68,68,68,56,124,68,68,127,127,56,124,84,84,92,765   24,8,124,126,10,10,56,380,324,324,508,252,127,127,4,4,124,120,72,122,122,766   64,256,256,256,506,250,126,126,16,56,104,64,66,126,126,64,124,124,24,56,28,767   124,120,124,124,4,4,124,120,56,124,68,68,124,56,508,508,68,68,124,56,56,124,768   68,68,508,508,124,124,4,4,12,8,72,92,84,84,116,36,4,4,62,126,68,68,60,124,769   64,64,124,124,28,60,96,96,60,28,28,124,112,56,112,124,28,68,108,56,56,108,770   68,284,316,352,320,508,252,68,100,116,92,76,68,8,62,119,65,65,127,127,65,771   65,119,62,8,16,24,12,12,24,24,12,4,772};773774typedef struct775{776   short id;777   unsigned short category_id;778   char *category;779   unsigned int layermask;780} stbte__tileinfo;781782#define MAX_LAYERMASK    (1 << (8*sizeof(unsigned int)))783784typedef short stbte__tiledata;785786#define STBTE__NO_TILE   -1787788enum789{790   STBTE__panel_toolbar,791   STBTE__panel_colorpick,792   STBTE__panel_info,793   STBTE__panel_layers,794   STBTE__panel_props,795   STBTE__panel_categories,796   STBTE__panel_tiles,797798   STBTE__num_panel,799};800801enum802{803   STBTE__side_left,804   STBTE__side_right,805   STBTE__side_top,806   STBTE__side_bottom,807};808809enum810{811   STBTE__tool_select,812   STBTE__tool_brush,813   STBTE__tool_erase,814   STBTE__tool_rect,815   STBTE__tool_eyedrop,816   STBTE__tool_fill,817   STBTE__tool_link,818819   STBTE__tool_showgrid,820   STBTE__tool_showlinks,821822   STBTE__tool_undo,823   STBTE__tool_redo,824   // copy/cut/paste aren't included here because they're displayed differently825826   STBTE__num_tool,827};828829// icons are stored in the 0-31 range of ASCII in the font830static int toolchar[] = { 26,24,25,20,23,22,18, 19,17, 29,28, };831832enum833{834   STBTE__propmode_default,835   STBTE__propmode_always,836   STBTE__propmode_never,837};838839enum840{841   STBTE__paint,842843   // from here down does hittesting844   STBTE__tick,845   STBTE__mousemove,846   STBTE__mousewheel,847   STBTE__leftdown,848   STBTE__leftup,849   STBTE__rightdown,850   STBTE__rightup,851};852853typedef struct854{855   int expanded, mode;856   int delta_height;     // number of rows they've requested for this857   int side;858   int width,height;859   int x0,y0;860} stbte__panel;861862typedef struct863{864   int x0,y0,x1,y1,color;865} stbte__colorrect;866867#define STBTE__MAX_DELAYRECT 256868869typedef struct870{871   int tool, active_event;872   int active_id, hot_id, next_hot_id;873   int event;874   int mx,my, dx,dy;875   int ms_time;876   int shift, scrollkey;877   int initted;878   int side_extended[2];879   stbte__colorrect delayrect[STBTE__MAX_DELAYRECT];880   int delaycount;881   int show_grid, show_links;882   int brush_state; // used to decide which kind of erasing883   int eyedrop_x, eyedrop_y, eyedrop_last_layer;884   int pasting, paste_x, paste_y;885   int scrolling, start_x, start_y;886   int last_mouse_x, last_mouse_y;887   int accum_x, accum_y;888   int linking;889   int dragging;890   int drag_x, drag_y, drag_w, drag_h;891   int drag_offx, drag_offy, drag_dest_x, drag_dest_y;892   int undoing;893   int has_selection, select_x0, select_y0, select_x1, select_y1;894   int sx,sy;895   int x0,y0,x1,y1, left_width, right_width; // configurable widths896   float alert_timer;897   const char *alert_msg;898   float dt;899   stbte__panel panel[STBTE__num_panel];900   short copybuffer[STBTE_MAX_COPY][STBTE_MAX_LAYERS];901   float copyprops[STBTE_MAX_COPY][STBTE_MAX_PROPERTIES];902#ifdef STBTE_ALLOW_LINK903   stbte__link copylinks[STBTE_MAX_COPY];904#endif905   int copy_src_x, copy_src_y;906   stbte_tilemap *copy_src;907   int copy_width,copy_height,has_copy,copy_has_props;908} stbte__ui_t;909910// there's only one UI system at a time, so we can globalize this911static stbte__ui_t stbte__ui = { STBTE__tool_brush, 0 };912913#define STBTE__INACTIVE()     (stbte__ui.active_id == 0)914#define STBTE__IS_ACTIVE(id)  (stbte__ui.active_id == (id))915#define STBTE__IS_HOT(id)     (stbte__ui.hot_id    == (id))916917#define STBTE__BUTTON_HEIGHT            (STBTE__FONT_HEIGHT + 2 * STBTE__BUTTON_INTERNAL_SPACING)918#define STBTE__BUTTON_INTERNAL_SPACING  (2 + (STBTE__FONT_HEIGHT>>4))919920typedef struct921{922   const char *name;923   int locked;924   int hidden;925} stbte__layer;926927enum928{929   STBTE__unlocked,930   STBTE__protected,931   STBTE__locked,932};933934struct stbte_tilemap935{936    stbte__tiledata data[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_LAYERS];937    float props[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_PROPERTIES];938    #ifdef STBTE_ALLOW_LINK939    stbte__link link[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];940    int linkcount[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];941    #endif942    int max_x, max_y, num_layers;943    int spacing_x, spacing_y;944    int palette_spacing_x, palette_spacing_y;945    int scroll_x,scroll_y;946    int cur_category, cur_tile, cur_layer;947    char *categories[STBTE_MAX_CATEGORIES];948    int num_categories, category_scroll;949    stbte__tileinfo *tiles;950    int num_tiles, max_tiles, digits;951    unsigned char undo_available_valid;952    unsigned char undo_available;953    unsigned char redo_available;954    unsigned char padding;955    int cur_palette_count;956    int palette_scroll;957    int tileinfo_dirty;958    stbte__layer layerinfo[STBTE_MAX_LAYERS];959    int has_layer_names;960    int layername_width;961    int layer_scroll;962    int propmode;963    int solo_layer;964    int undo_pos, undo_len, redo_len;965    short background_tile;966    unsigned char id_in_use[32768>>3];967    short *undo_buffer;968};969970static char *default_category = "[unassigned]";971972static void stbte__init_gui(void)973{974   int i,n;975   stbte__ui.initted = 1;976   // init UI state977   stbte__ui.show_links = 1;978   for (i=0; i < STBTE__num_panel; ++i) {979      stbte__ui.panel[i].expanded     = 1; // visible if not autohidden980      stbte__ui.panel[i].delta_height = 0;981      stbte__ui.panel[i].side         = STBTE__side_left;982   }983   stbte__ui.panel[STBTE__panel_toolbar  ].side = STBTE__side_top;984   stbte__ui.panel[STBTE__panel_colorpick].side = STBTE__side_right;985986   if (stbte__ui.left_width == 0)987      stbte__ui.left_width = 80;988   if (stbte__ui.right_width == 0)989      stbte__ui.right_width = 80;990991   // init font992   n=95+16;993   for (i=0; i < 95+16; ++i) {994      stbte__font_offset[i] = n;995      n += stbte__fontdata[i];996   }997}998999stbte_tilemap *stbte_create_map(int map_x, int map_y, int map_layers, int spacing_x, int spacing_y, int max_tiles)1000{1001   int i;1002   stbte_tilemap *tm;1003   STBTE_ASSERT(map_layers >= 0 && map_layers <= STBTE_MAX_LAYERS);1004   STBTE_ASSERT(map_x >= 0 && map_x <= STBTE_MAX_TILEMAP_X);1005   STBTE_ASSERT(map_y >= 0 && map_y <= STBTE_MAX_TILEMAP_Y);1006   if (map_x < 0 || map_y < 0 || map_layers < 0 ||1007       map_x > STBTE_MAX_TILEMAP_X || map_y > STBTE_MAX_TILEMAP_Y || map_layers > STBTE_MAX_LAYERS)1008      return NULL;1009   1010   if (!stbte__ui.initted)1011      stbte__init_gui();10121013   tm = (stbte_tilemap *) malloc(sizeof(*tm) + sizeof(*tm->tiles) * max_tiles + STBTE_UNDO_BUFFER_BYTES);1014   if (tm == NULL)1015      return NULL;10161017   tm->tiles = (stbte__tileinfo *) (tm+1);1018   tm->undo_buffer = (short *) (tm->tiles + max_tiles);1019   tm->num_layers = map_layers;1020   tm->max_x = map_x;1021   tm->max_y = map_y;1022   tm->spacing_x = spacing_x;1023   tm->spacing_y = spacing_y;1024   tm->scroll_x = 0;1025   tm->scroll_y = 0;1026   tm->palette_scroll = 0;1027   tm->palette_spacing_x = spacing_x+1;1028   tm->palette_spacing_y = spacing_y+1;1029   tm->cur_category = -1;1030   tm->cur_tile = 0;1031   tm->solo_layer = -1;1032   tm->undo_len = 0;1033   tm->redo_len = 0;1034   tm->undo_pos = 0;1035   tm->category_scroll = 0;1036   tm->layer_scroll = 0;1037   tm->propmode = 0;1038   tm->has_layer_names = 0;1039   tm->layername_width = 0;1040   tm->undo_available_valid = 0;10411042   for (i=0; i < tm->num_layers; ++i) {1043      tm->layerinfo[i].hidden = 0;1044      tm->layerinfo[i].locked = STBTE__unlocked;1045      tm->layerinfo[i].name   = 0;1046   }10471048   tm->background_tile = STBTE__NO_TILE;1049   stbte_clear_map(tm);10501051   tm->max_tiles = max_tiles;1052   tm->num_tiles = 0;1053   for (i=0; i < 32768/8; ++i)1054      tm->id_in_use[i] = 0;1055   tm->tileinfo_dirty = 1;1056   return tm;1057}10581059void stbte_set_background_tile(stbte_tilemap *tm, short id)1060{1061   int i;1062   STBTE_ASSERT(id >= -1 && id < 32768);1063   if (id >= 32768 || id < -1)1064      return;1065   for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i)1066      if (tm->data[0][i][0] == -1)1067         tm->data[0][i][0] = id;1068   tm->background_tile = id;1069}10701071void stbte_set_spacing(stbte_tilemap *tm, int spacing_x, int spacing_y, int palette_spacing_x, int palette_spacing_y)1072{1073   tm->spacing_x = spacing_x;1074   tm->spacing_y = spacing_y;1075   tm->palette_spacing_x = palette_spacing_x;1076   tm->palette_spacing_y = palette_spacing_y;1077}10781079void stbte_set_sidewidths(int left, int right)1080{1081   stbte__ui.left_width  = left;1082   stbte__ui.right_width = right;1083}10841085void stbte_set_display(int x0, int y0, int x1, int y1)1086{1087   stbte__ui.x0 = x0;1088   stbte__ui.y0 = y0;1089   stbte__ui.x1 = x1;1090   stbte__ui.y1 = y1;1091}10921093void stbte_define_tile(stbte_tilemap *tm, unsigned short id, unsigned int layermask, const char * category_c)1094{1095   char *category = (char *) category_c;1096   STBTE_ASSERT(id < 32768);1097   STBTE_ASSERT(tm->num_tiles < tm->max_tiles);1098   STBTE_ASSERT((tm->id_in_use[id>>3]&(1<<(id&7))) == 0);1099   if (id >= 32768 || tm->num_tiles >= tm->max_tiles || (tm->id_in_use[id>>3]&(1<<(id&7))))1100      return;11011102   if (category == NULL)1103      category = (char*) default_category;1104   tm->id_in_use[id>>3] |= 1 << (id&7);1105   tm->tiles[tm->num_tiles].category    = category;1106   tm->tiles[tm->num_tiles].id        = id;1107   tm->tiles[tm->num_tiles].layermask = layermask;1108   ++tm->num_tiles;1109   tm->tileinfo_dirty = 1;1110}11111112static int stbte__text_width(const char *str);11131114void stbte_set_layername(stbte_tilemap *tm, int layer, const char *layername)1115{1116   STBTE_ASSERT(layer >= 0 && layer < tm->num_layers);1117   if (layer >= 0 && layer < tm->num_layers) {1118      int width;1119      tm->layerinfo[layer].name = layername;1120      tm->has_layer_names = 1;1121      width = stbte__text_width(layername);1122      tm->layername_width = (width > tm->layername_width ? width : tm->layername_width);1123   }1124}11251126void stbte_get_dimensions(stbte_tilemap *tm, int *max_x, int *max_y)1127{1128   *max_x = tm->max_x;1129   *max_y = tm->max_y;1130}11311132short* stbte_get_tile(stbte_tilemap *tm, int x, int y)1133{1134   STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1135   if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1136      return NULL;1137   return tm->data[y][x];1138}11391140float *stbte_get_properties(stbte_tilemap *tm, int x, int y)1141{1142   STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1143   if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1144      return NULL;1145   return tm->props[y][x];1146}11471148void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty)1149{1150   int gx=-1,gy=-1;1151   STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1152#ifdef STBTE_ALLOW_LINK1153   if (x >= 0 && x < STBTE_MAX_TILEMAP_X && y >= 0 && y < STBTE_MAX_TILEMAP_Y) {1154      gx = tm->link[y][x].x;1155      gy = tm->link[y][x].y;1156      if (gx >= 0)1157         if (!STBTE_ALLOW_LINK(tm->data[y][x], tm->props[y][x], tm->data[gy][gx], tm->props[gy][gx]))1158            gx = gy = -1;1159   }1160#endif1161   *destx = gx;1162   *desty = gy;1163}11641165void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val)1166{1167   tm->props[y][x][n] = val;1168}11691170static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode);11711172enum1173{1174   STBTE__undo_none,1175   STBTE__undo_record,1176   STBTE__undo_block,1177};11781179void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty)1180{1181#ifdef STBTE_ALLOW_LINK1182   stbte__set_link(tm, x, y, destx, desty, STBTE__undo_none);1183#else1184   STBTE_ASSERT(0);1185#endif1186}118711881189// returns an array of map_layers shorts. each short is either1190// one of the tile_id values from define_tile, or STBTE_EMPTY11911192void stbte_set_dimensions(stbte_tilemap *tm, int map_x, int map_y)1193{1194   STBTE_ASSERT(map_x >= 0 && map_x <= STBTE_MAX_TILEMAP_X);1195   STBTE_ASSERT(map_y >= 0 && map_y <= STBTE_MAX_TILEMAP_Y);1196   if (map_x < 0 || map_y < 0 || map_x > STBTE_MAX_TILEMAP_X || map_y > STBTE_MAX_TILEMAP_Y)1197      return;1198   tm->max_x = map_x;1199   tm->max_y = map_y;1200}12011202void stbte_clear_map(stbte_tilemap *tm)1203{1204   int i,j;1205   for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i) {1206      tm->data[0][i][0] = tm->background_tile;1207      for (j=1; j < tm->num_layers; ++j)1208         tm->data[0][i][j] = STBTE__NO_TILE;1209      for (j=0; j < STBTE_MAX_PROPERTIES; ++j)1210         tm->props[0][i][j] = 0;1211      #ifdef STBTE_ALLOW_LINK1212      tm->link[0][i].x = -1;1213      tm->link[0][i].y = -1;1214      tm->linkcount[0][i] = 0;1215      #endif1216   }1217}12181219void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile)1220{1221   STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1222   STBTE_ASSERT(layer >= 0 && layer < tm->num_layers);1223   STBTE_ASSERT(tile >= -1 && tile < 32768);1224   if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1225      return;1226   if (layer < 0 || layer >= tm->num_layers || tile < -1)1227      return;1228   tm->data[y][x][layer] = tile;1229}12301231static void stbte__choose_category(stbte_tilemap *tm, int category)1232{1233   int i,n=0;1234   tm->cur_category = category;1235   for (i=0; i < tm->num_tiles; ++i)1236      if (tm->tiles[i].category_id == category || category == -1)1237         ++n;1238   tm->cur_palette_count = n;1239   tm->palette_scroll = 0;1240}12411242static int stbte__strequal(char *p, char *q)1243{1244   while (*p)1245      if (*p++ != *q++) return 0;1246   return *q == 0;1247}12481249static void stbte__compute_tileinfo(stbte_tilemap *tm)1250{1251   int i,j,n=0;12521253   tm->num_categories=0;12541255   for (i=0; i < tm->num_tiles; ++i) {1256      stbte__tileinfo *t = &tm->tiles[i];1257      // find category1258      for (j=0; j < tm->num_categories; ++j)1259         if (stbte__strequal(t->category, tm->categories[j]))1260            goto found;1261      tm->categories[j] = t->category;1262      ++tm->num_categories;1263     found:1264      t->category_id = (unsigned short) j;1265   }12661267   // currently number of categories can never decrease because you1268   // can't remove tile definitions, but let's get it right anyway1269   if (tm->cur_category > tm->num_categories) {1270      tm->cur_category = -1;1271   }12721273   stbte__choose_category(tm, tm->cur_category);12741275   tm->tileinfo_dirty = 0;1276}12771278static void stbte__prepare_tileinfo(stbte_tilemap *tm)1279{1280   if (tm->tileinfo_dirty)1281      stbte__compute_tileinfo(tm);1282}128312841285/////////////////////// undo system ////////////////////////12861287// the undo system works by storing "commands" into a buffer, and1288// then playing back those commands. undo and redo have to store1289// the commands in different order. 1290//1291// the commands are:1292//1293// 1)  end_of_undo_record1294//       -1:short1295//1296// 2)  end_of_redo_record1297//       -2:short1298//1299// 3)  tile update1300//       tile_id:short (-1..32767)1301//       x_coord:short1302//       y_coord:short1303//       layer:short (0..31)1304//1305// 4)  property update (also used for links)1306//       value_hi:short1307//       value_lo:short1308//       y_coord:short1309//       x_coord:short1310//       property:short (256+prop#)1311//1312// Since we use a circular buffer, we might overwrite the undo storage.1313// To detect this, before playing back commands we scan back and see1314// if we see an end_of_undo_record before hitting the relevant boundary,1315// it's wholly contained.1316//1317// When we read back through, we see them in reverse order, so1318// we'll see the layer number or property number first1319//1320// To be clearer about the circular buffer, there are two cases:1321//     1. a single record is larger than the whole buffer.1322//        this is caught because the end_of_undo_record will1323//        get overwritten.1324//     2. multiple records written are larger than the whole1325//        buffer, so some of them have been overwritten by1326//        the later ones. this is handled by explicitly tracking1327//        the undo length; we never try to parse the data that1328//        got overwritten13291330// given two points, compute the length between them1331#define stbte__wrap(pos)            ((pos) & (STBTE__UNDO_BUFFER_COUNT-1))13321333#define STBTE__undo_record  -21334#define STBTE__redo_record  -31335#define STBTE__undo_junk    -4  // this is written underneath the undo pointer, never used13361337static void stbte__write_undo(stbte_tilemap *tm, short value)1338{1339   int pos = tm->undo_pos;1340   tm->undo_buffer[pos] = value;1341   tm->undo_pos = stbte__wrap(pos+1);1342   tm->undo_len += (tm->undo_len < STBTE__UNDO_BUFFER_COUNT-2);1343   tm->redo_len -= (tm->redo_len > 0);1344   tm->undo_available_valid = 0;1345}13461347static void stbte__write_redo(stbte_tilemap *tm, short value)1348{1349   int pos = tm->undo_pos;1350   tm->undo_buffer[pos] = value;1351   tm->undo_pos = stbte__wrap(pos-1);1352   tm->redo_len += (tm->redo_len < STBTE__UNDO_BUFFER_COUNT-2);1353   tm->undo_len -= (tm->undo_len > 0);1354   tm->undo_available_valid = 0;1355}13561357static void stbte__begin_undo(stbte_tilemap *tm)1358{1359   tm->redo_len = 0;1360   stbte__write_undo(tm, STBTE__undo_record);1361   stbte__ui.undoing = 1;1362   stbte__ui.alert_msg = 0; // clear alert if they start doing something1363}13641365static void stbte__end_undo(stbte_tilemap *tm)1366{1367   if (stbte__ui.undoing) {1368      // check if anything got written1369      int pos = stbte__wrap(tm->undo_pos-1);1370      if (tm->undo_buffer[pos] == STBTE__undo_record) {1371         // empty undo record, move back1372         tm->undo_pos = pos;1373         STBTE_ASSERT(tm->undo_len > 0);1374         tm->undo_len -= 1;1375      }1376      tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1377      // otherwise do nothing13781379      stbte__ui.undoing = 0;1380   }1381}13821383static void stbte__undo_record(stbte_tilemap *tm, int x, int y, int i, int v)1384{1385   STBTE_ASSERT(stbte__ui.undoing);1386   if (stbte__ui.undoing) {1387      stbte__write_undo(tm, v);1388      stbte__write_undo(tm, x);1389      stbte__write_undo(tm, y);1390      stbte__write_undo(tm, i);1391   }1392}13931394static void stbte__redo_record(stbte_tilemap *tm, int x, int y, int i, int v)1395{1396   stbte__write_redo(tm, v);1397   stbte__write_redo(tm, x);1398   stbte__write_redo(tm, y);1399   stbte__write_redo(tm, i);1400}14011402static float stbte__extract_float(short s0, short s1)1403{1404   union { float f; short s[2]; } converter;1405   converter.s[0] = s0;1406   converter.s[1] = s1;1407   return converter.f;1408}14091410static short stbte__extract_short(float f, int slot)1411{1412   union { float f; short s[2]; } converter;1413   converter.f = f;1414   return converter.s[slot];1415}14161417static void stbte__undo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1418{1419   STBTE_ASSERT(stbte__ui.undoing);1420   if (stbte__ui.undoing) {1421      stbte__write_undo(tm, s1);1422      stbte__write_undo(tm, s0);1423      stbte__write_undo(tm, x);1424      stbte__write_undo(tm, y);1425      stbte__write_undo(tm, 256+i);1426   }1427}14281429static void stbte__undo_record_prop_float(stbte_tilemap *tm, int x, int y, int i, float f)1430{1431   stbte__undo_record_prop(tm, x,y,i, stbte__extract_short(f,0), stbte__extract_short(f,1));1432}14331434static void stbte__redo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1435{1436   stbte__write_redo(tm, s1);1437   stbte__write_redo(tm, s0);1438   stbte__write_redo(tm, x);1439   stbte__write_redo(tm, y);1440   stbte__write_redo(tm, 256+i);1441}144214431444static int stbte__undo_find_end(stbte_tilemap *tm)1445{1446   // first scan through for the end record1447   int i, pos = stbte__wrap(tm->undo_pos-1);1448   for (i=0; i < tm->undo_len;) {1449      STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1450      if (tm->undo_buffer[pos] == STBTE__undo_record)1451         break;1452      if (tm->undo_buffer[pos] >= 255)1453         pos = stbte__wrap(pos-5), i += 5;1454      else1455         pos = stbte__wrap(pos-4), i += 4;1456   }1457   if (i >= tm->undo_len)1458      return -1;1459   return pos;1460}14611462static void stbte__undo(stbte_tilemap *tm)1463{1464   int i, pos, endpos;1465   endpos = stbte__undo_find_end(tm);1466   if (endpos < 0)1467      return;14681469   // we found a complete undo record1470   pos = stbte__wrap(tm->undo_pos-1);14711472   // start a redo record1473   stbte__write_redo(tm, STBTE__redo_record);14741475   // so now go back through undo and apply in reverse1476   // order, and copy it to redo1477   for (i=0; endpos != pos; i += 4) {1478      int x,y,n,v;1479      // get the undo entry1480      n = tm->undo_buffer[pos];1481      y = tm->undo_buffer[stbte__wrap(pos-1)];1482      x = tm->undo_buffer[stbte__wrap(pos-2)];1483      v = tm->undo_buffer[stbte__wrap(pos-3)];1484      if (n >= 255) {1485         short s0=0,s1=0;1486         int v2 = tm->undo_buffer[stbte__wrap(pos-4)];1487         pos = stbte__wrap(pos-5);1488         if (n > 255) {1489            float vf = stbte__extract_float(v, v2);1490            s0 = stbte__extract_short(tm->props[y][x][n-256], 0);1491            s1 = stbte__extract_short(tm->props[y][x][n-256], 1);1492            tm->props[y][x][n-256] = vf;1493         } else {1494#ifdef STBTE_ALLOW_LINK1495            s0 = tm->link[y][x].x;1496            s1 = tm->link[y][x].y;1497            stbte__set_link(tm, x,y, v, v2, STBTE__undo_none);1498#endif1499         }1500         // write the redo entry1501         stbte__redo_record_prop(tm, x, y, n-256, s0,s1);1502         // apply the undo entry1503      } else {1504         pos = stbte__wrap(pos-4);1505         // write the redo entry1506         stbte__redo_record(tm, x, y, n, tm->data[y][x][n]);1507         // apply the undo entry1508         tm->data[y][x][n] = (short) v;1509      }1510   }1511   // overwrite undo record with junk1512   tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1513}15141515static int stbte__redo_find_end(stbte_tilemap *tm)1516{1517   // first scan through for the end record1518   int i, pos = stbte__wrap(tm->undo_pos+1);1519   for (i=0; i < tm->redo_len;) {1520      STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1521      if (tm->undo_buffer[pos] == STBTE__redo_record)1522         break;1523      if (tm->undo_buffer[pos] >= 255)1524         pos = stbte__wrap(pos+5), i += 5;1525      else1526         pos = stbte__wrap(pos+4), i += 4;1527   }1528   if (i >= tm->redo_len)1529      return -1; // this should only ever happen if redo buffer is empty1530   return pos;1531}15321533static void stbte__redo(stbte_tilemap *tm)1534{1535   // first scan through for the end record1536   int i, pos, endpos;1537   endpos = stbte__redo_find_end(tm);1538   if (endpos < 0)1539      return;15401541   // we found a complete redo record1542   pos = stbte__wrap(tm->undo_pos+1);1543   1544   // start an undo record1545   stbte__write_undo(tm, STBTE__undo_record);15461547   for (i=0; pos != endpos; i += 4) {1548      int x,y,n,v;1549      n = tm->undo_buffer[pos];1550      y = tm->undo_buffer[stbte__wrap(pos+1)];1551      x = tm->undo_buffer[stbte__wrap(pos+2)];1552      v = tm->undo_buffer[stbte__wrap(pos+3)];1553      if (n >= 255) {1554         int v2 = tm->undo_buffer[stbte__wrap(pos+4)];1555         short s0=0,s1=0;1556         pos = stbte__wrap(pos+5);1557         if (n > 255) {1558            float vf = stbte__extract_float(v, v2);1559            s0 = stbte__extract_short(tm->props[y][x][n-256],0);1560            s1 = stbte__extract_short(tm->props[y][x][n-256],1);1561            tm->props[y][x][n-256] = vf;1562         } else {1563#ifdef STBTE_ALLOW_LINK1564            s0 = tm->link[y][x].x;1565            s1 = tm->link[y][x].y;1566            stbte__set_link(tm, x,y,v,v2, STBTE__undo_none);1567#endif1568         }1569         // don't use stbte__undo_record_prop because it's guarded1570         stbte__write_undo(tm, s1);1571         stbte__write_undo(tm, s0);1572         stbte__write_undo(tm, x);1573         stbte__write_undo(tm, y);1574         stbte__write_undo(tm, n);1575      } else {1576         pos = stbte__wrap(pos+4);1577         // don't use stbte__undo_record because it's guarded1578         stbte__write_undo(tm, tm->data[y][x][n]);1579         stbte__write_undo(tm, x);1580         stbte__write_undo(tm, y);1581         stbte__write_undo(tm, n);1582         tm->data[y][x][n] = (short) v;1583      }1584   }1585   tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1586}15871588// because detecting that undo is available 1589static void stbte__recompute_undo_available(stbte_tilemap *tm)1590{1591   tm->undo_available = (stbte__undo_find_end(tm) >= 0);1592   tm->redo_available = (stbte__redo_find_end(tm) >= 0);1593}15941595static int stbte__undo_available(stbte_tilemap *tm)1596{1597   if (!tm->undo_available_valid)1598      stbte__recompute_undo_available(tm);1599   return tm->undo_available;1600}16011602static int stbte__redo_available(stbte_tilemap *tm)1603{1604   if (!tm->undo_available_valid)1605      stbte__recompute_undo_available(tm);1606   return tm->redo_available;1607}16081609///////////////////////////////////////////////////////////////////////////////////////////////////16101611#ifdef STBTE_ALLOW_LINK1612static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode)1613{1614   stbte__link *a;1615   STBTE_ASSERT(src_x >= 0 && src_x < STBTE_MAX_TILEMAP_X && src_y >= 0 && src_y < STBTE_MAX_TILEMAP_Y);1616   a = &tm->link[src_y][src_x];1617   // check if it's a do nothing1618   if (a->x == dest_x && a->y == dest_y)1619      return;1620   if (undo_mode != STBTE__undo_none ) {1621      if (undo_mode == STBTE__undo_block) stbte__begin_undo(tm);1622      stbte__undo_record_prop(tm, src_x, src_y, -1, a->x, a->y);1623      if (undo_mode == STBTE__undo_block) stbte__end_undo(tm);1624   }1625   // check if there's an existing link1626   if (a->x >= 0) {1627      // decrement existing link refcount1628      STBTE_ASSERT(tm->linkcount[a->y][a->x] > 0);1629      --tm->linkcount[a->y][a->x];1630   }1631   // increment new dest1632   if (dest_x >= 0) {1633      ++tm->linkcount[dest_y][dest_x];1634   }1635   a->x = dest_x;1636   a->y = dest_y;1637}1638#endif163916401641static void stbte__draw_rect(int x0, int y0, int x1, int y1, unsigned int color)1642{1643   STBTE_DRAW_RECT(x0,y0,x1,y1, color);1644}16451646static void stbte__draw_line(int x0, int y0, int x1, int y1, unsigned int color)1647{1648   int temp;1649   if (x1 < x0) temp=x0,x0=x1,x1=temp;1650   if (y1 < y0) temp=y0,y0=y1,y1=temp;1651   stbte__draw_rect(x0,y0,x1+1,y1+1,color);1652}16531654static void stbte__draw_link(int x0, int y0, int x1, int y1, unsigned int color)1655{1656   stbte__draw_line(x0,y0,x0,y1, color);1657   stbte__draw_line(x0,y1,x1,y1, color);1658}16591660static void stbte__draw_frame(int x0, int y0, int x1, int y1, unsigned int color)1661{1662   stbte__draw_rect(x0,y0,x1-1,y0+1,color);1663   stbte__draw_rect(x1-1,y0,x1,y1-1,color);1664   stbte__draw_rect(x0+1,y1-1,x1,y1,color);1665   stbte__draw_rect(x0,y0+1,x0+1,y1,color);1666}16671668static void stbte__draw_halfframe(int x0, int y0, int x1, int y1, unsigned int color)1669{1670   stbte__draw_rect(x0,y0,x1,y0+1,color);1671   stbte__draw_rect(x0,y0+1,x0+1,y1,color);1672}16731674static int stbte__get_char_width(int ch)1675{1676   return stbte__fontdata[ch-16];1677}16781679static short *stbte__get_char_bitmap(int ch)1680{1681   return stbte__fontdata + stbte__font_offset[ch-16];1682}16831684static void stbte__draw_bitmask_as_columns(int x, int y, short bitmask, int color)1685{1686   int start_i = -1, i=0;1687   while (bitmask) {1688      if (bitmask & (1<<i)) {1689         if (start_i < 0)1690            start_i = i;   1691      } else if (start_i >= 0) {1692         stbte__draw_rect(x, y+start_i, x+1, y+i, color);1693         start_i = -1;1694         bitmask &= ~((1<<i)-1); // clear all the old bits; we don't clear them as we go to save code1695      }1696      ++i;1697   }1698}16991700static void stbte__draw_bitmap(int x, int y, int w, short *bitmap, int color)1701{1702   int i;1703   for (i=0; i < w; ++i)1704      stbte__draw_bitmask_as_columns(x+i, y, *bitmap++, color);1705}17061707static void stbte__draw_text_core(int x, int y, const char *str, int w, int color, int digitspace)1708{1709   int x_end = x+w;1710   while (*str) {1711      int c = *str++;1712      int cw = stbte__get_char_width(c);1713      if (x + cw > x_end)1714         break;1715      stbte__draw_bitmap(x, y, cw, stbte__get_char_bitmap(c), color);1716      if (digitspace && c == ' ')1717         cw = stbte__get_char_width('0');1718      x += cw+1;1719   }1720}17211722static void stbte__draw_text(int x, int y, const char *str, int w, int color)1723{1724   stbte__draw_text_core(x,y,str,w,color,0);1725}17261727static int stbte__text_width(const char *str)1728{1729   int x = 0;1730   while (*str) {1731      int c = *str++;1732      int cw = stbte__get_char_width(c);1733      x += cw+1;1734   }1735   return x;1736}17371738static void stbte__draw_frame_delayed(int x0, int y0, int x1, int y1, int color)1739{1740   if (stbte__ui.delaycount < STBTE__MAX_DELAYRECT) {1741      stbte__colorrect r = { x0,y0,x1,y1,color };1742      stbte__ui.delayrect[stbte__ui.delaycount++] = r;1743   }1744}17451746static void stbte__flush_delay(void)1747{1748   stbte__colorrect *r;1749   int i;1750   r = stbte__ui.delayrect;1751   for (i=0; i < stbte__ui.delaycount; ++i,++r)1752      stbte__draw_frame(r->x0,r->y0,r->x1,r->y1,r->color);1753   stbte__ui.delaycount = 0;1754}17551756static void stbte__activate(int id)1757{1758   stbte__ui.active_id = id;1759   stbte__ui.active_event = stbte__ui.event;1760   stbte__ui.accum_x = 0;1761   stbte__ui.accum_y = 0;1762}17631764static int stbte__hittest(int x0, int y0, int x1, int y1, int id)1765{1766   int over =    stbte__ui.mx >= x0 && stbte__ui.my >= y01767              && stbte__ui.mx <  x1 && stbte__ui.my <  y1;17681769   if (over && stbte__ui.event >= STBTE__tick)1770      stbte__ui.next_hot_id = id;17711772   return over;1773}17741775static int stbte__button_core(int id)1776{1777   switch (stbte__ui.event) {1778      case STBTE__leftdown:1779         if (stbte__ui.hot_id == id && STBTE__INACTIVE())1780            stbte__activate(id);1781         break;1782      case STBTE__leftup:1783         if (stbte__ui.active_id == id && STBTE__IS_HOT(id)) {1784            stbte__activate(0);1785            return 1;1786         }1787         break;1788      case STBTE__rightdown:1789         if (stbte__ui.hot_id == id && STBTE__INACTIVE())1790            stbte__activate(id);1791         break;1792      case STBTE__rightup:1793         if (stbte__ui.active_id == id && STBTE__IS_HOT(id)) {1794            stbte__activate(0);1795            return -1;1796         }1797         break;1798   }1799   return 0;1800}18011802static void stbte__draw_box(int x0, int y0, int x1, int y1, int colormode, int colorindex)1803{1804   stbte__draw_rect (x0,y0,x1,y1, stbte__color_table[colormode][STBTE__base   ][colorindex]);1805   stbte__draw_frame(x0,y0,x1,y1, stbte__color_table[colormode][STBTE__outline][colorindex]);1806}18071808static void stbte__draw_textbox(int x0, int y0, int x1, int y1, char *text, int xoff, int yoff, int colormode, int colorindex)1809{1810   stbte__draw_box(x0,y0,x1,y1,colormode,colorindex);1811   stbte__draw_text(x0+xoff,y0+yoff, text, x1-x0-xoff-1, stbte__color_table[colormode][STBTE__text][colorindex]);1812}18131814static int stbte__button(int colormode, char *label, int x, int y, int textoff, int width, int id, int toggled, int disabled)1815{1816   int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1817   int s = STBTE__BUTTON_INTERNAL_SPACING;18181819   int over = !disabled && stbte__hittest(x0,y0,x1,y1,id);1820      1821   if (stbte__ui.event == STBTE__paint)1822      stbte__draw_textbox(x0,y0,x1,y1, label,s+textoff,s, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));1823   if (disabled)1824      return 0;1825   return (stbte__button_core(id) == 1);1826}18271828static int stbte__button_icon(int colormode, char ch, int x, int y, int width, int id, int toggled, int disabled)1829{1830   int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1831   int s = STBTE__BUTTON_INTERNAL_SPACING;18321833   int over = stbte__hittest(x0,y0,x1,y1,id);1834      1835   if (stbte__ui.event == STBTE__paint) {1836      char label[2] = { ch, 0 };1837      int pad = (9 - stbte__get_char_width(ch))/2;1838      stbte__draw_textbox(x0,y0,x1,y1, label,s+pad,s, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));1839   }1840   if (disabled)1841      return 0;1842   return (stbte__button_core(id) == 1);1843}18441845static int stbte__minibutton(int colormode, int x, int y, int ch, int id)1846{1847   int x0 = x, y0 = y, x1 = x+8, y1 = y+7;1848   int over = stbte__hittest(x0,y0,x1,y1,id);1849   if (stbte__ui.event == STBTE__paint) {1850      char str[2] = { ch,0 };1851      stbte__draw_textbox(x0,y0,x1,y1, str,1,0,colormode, STBTE__INDEX_FOR_ID(id,0,0));1852   }1853   return stbte__button_core(id);1854}18551856static int stbte__layerbutton(int x, int y, int ch, int id, int toggled, int disabled, int colormode)1857{1858   int x0 = x, y0 = y, x1 = x+10, y1 = y+11;1859   int over = !disabled && stbte__hittest(x0,y0,x1,y1,id);1860   if (stbte__ui.event == STBTE__paint) {1861      char str[2] = { ch,0 };1862      int off = (9-stbte__get_char_width(ch))/2;1863      stbte__draw_textbox(x0,y0,x1,y1, str, off+1,2, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));1864   }1865   if (disabled)1866      return 0;1867   return stbte__button_core(id);1868}18691870static int stbte__microbutton(int x, int y, int size, int id, int colormode)1871{1872   int x0 = x, y0 = y, x1 = x+size, y1 = y+size;1873   int over = stbte__hittest(x0,y0,x1,y1,id);1874   if (stbte__ui.event == STBTE__paint) {1875      stbte__draw_box(x0,y0,x1,y1, colormode, STBTE__INDEX_FOR_ID(id,0,0));1876   }1877   return stbte__button_core(id);1878}18791880static int stbte__microbutton_dragger(int x, int y, int size, int id, int *pos)1881{1882   int x0 = x, y0 = y, x1 = x+size, y1 = y+size;1883   int over = stbte__hittest(x0,y0,x1,y1,id);1884   switch (stbte__ui.event) {1885      case STBTE__paint:1886         stbte__draw_box(x0,y0,x1,y1, STBTE__cexpander, STBTE__INDEX_FOR_ID(id,0,0));1887         break;1888      case STBTE__leftdown:1889         if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {1890            stbte__activate(id);1891            stbte__ui.sx = stbte__ui.mx - *pos;1892         }1893         break;1894      case STBTE__mousemove:1895         if (STBTE__IS_ACTIVE(id) && stbte__ui.active_event == STBTE__leftdown) {1896            *pos = stbte__ui.mx - stbte__ui.sx;  1897         }1898         break;1899      case STBTE__leftup:1900         if (STBTE__IS_ACTIVE(id))1901            stbte__activate(0);1902         break;1903      default:1904         return stbte__button_core(id);1905   }1906   return 0;1907}19081909static int stbte__category_button(char *label, int x, int y, int width, int id, int toggled)1910{1911   int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1912   int s = STBTE__BUTTON_INTERNAL_SPACING;19131914   int over = stbte__hittest(x0,y0,x1,y1,id);1915      1916   if (stbte__ui.event == STBTE__paint)1917      stbte__draw_textbox(x0,y0,x1,y1, label, s,s, STBTE__ccategory_button, STBTE__INDEX_FOR_ID(id,0,toggled));19181919   return (stbte__button_core(id) == 1);1920}19211922enum1923{1924   STBTE__none,1925   STBTE__begin,1926   STBTE__end,1927   STBTE__change,1928};19291930// returns -1 if value changes, 1 at end of drag1931static int stbte__slider(int x0, int w, int y, int range, int *value, int id)1932{1933   int x1 = x0+w;1934   int pos = *value * w / (range+1);1935   int over = stbte__hittest(x0,y-2,x1,y+3,id);1936   int event_mouse_move = STBTE__change;1937   switch (stbte__ui.event) {1938      case STBTE__paint:1939         stbte__draw_rect(x0,y,x1,y+1, 0x808080);1940         stbte__draw_rect(x0+pos-1,y-1,x0+pos+2,y+2, 0xffffff);1941         break;1942      case STBTE__leftdown:1943         if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {1944            stbte__activate(id);1945            event_mouse_move = STBTE__begin;1946         }1947         // fall through1948      case STBTE__mousemove:1949         if (STBTE__IS_ACTIVE(id)) {1950            int v = (stbte__ui.mx-x0)*(range+1)/w;1951            if (v < 0) v = 0; else if (v > range) v = range;1952            *value = v;1953            return event_mouse_move;1954         }1955         break;1956      case STBTE__leftup:1957         if (STBTE__IS_ACTIVE(id)) {1958            stbte__activate(0);1959            return STBTE__end;1960         }1961         break;1962   }1963   return STBTE__none;1964}19651966static int stbte__float_control(int x0, int y0, int w, float minv, float maxv, float scale, char *fmt, float *value, int colormode, int id)1967{1968   int x1 = x0+w;1969   int y1 = y0+11;1970   int over = stbte__hittest(x0,y0,x1,y1,id);1971   switch (stbte__ui.event) {1972      case STBTE__paint: {1973         char text[32];1974         sprintf(text, fmt ? fmt : "%6.2f", *value);1975         stbte__draw_textbox(x0,y0,x1,y1, text, 1,2, colormode, STBTE__INDEX_FOR_ID(id,0,0));1976         break;1977      }1978      case STBTE__leftdown:1979      case STBTE__rightdown:1980         if (STBTE__IS_HOT(id) && STBTE__INACTIVE())1981            stbte__activate(id);1982            return STBTE__begin;1983         break;1984      case STBTE__leftup:1985      case STBTE__rightup:1986         if (STBTE__IS_ACTIVE(id)) {1987            stbte__activate(0);1988            return STBTE__end;1989         }1990         break;1991      case STBTE__mousemove:1992         if (STBTE__IS_ACTIVE(id)) {1993            float v = *value, delta;1994            int ax = stbte__ui.accum_x/STBTE_FLOAT_CONTROL_GRANULARITY;1995            int ay = stbte__ui.accum_y/STBTE_FLOAT_CONTROL_GRANULARITY;1996            stbte__ui.accum_x -= ax*STBTE_FLOAT_CONTROL_GRANULARITY;1997            stbte__ui.accum_y -= ay*STBTE_FLOAT_CONTROL_GRANULARITY;1998            if (stbte__ui.shift) {1999               if (stbte__ui.active_event == STBTE__leftdown)2000                  delta = ax * 16.0f + ay;

The first 2,000 of 4,172 lines.