stb_tilemap_editor.h
This file
Path
stb_tilemap_editor.h
4,160 lines · 142.4 KiB · C
Last changed by
Merge branch 'master' into workingSean Barrettbefore adoptiond940053
History of this fileTake it
The walk stopped after 200 commits: lines older than f5c9aaa point at it.
update version numbersSean Barrettbefore adoptionc963e40
line 1why, one step back1// stb_tilemap_editor.h - v0.40 - Sean Barrett - http://nothings.org/stbMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 2–170why, one step back2// 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//Merge branch 'patch-1' of https://github.com/sherjilozair/stb into workingSean Barrettbefore adoption9bb4bc1
line 171why, one step back171// void STBTE_DRAW_RECT(int x0, int y0, int x1, int y1, unsigned int color);Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 172–253why, one step back172// // 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 themMerge branch 'misc-typos' of https://github.com/luzpaz/stb into workingSean Barrettbefore adoption3005bcf
line 254why, one step back254// by making similar definitions, but memory usage will increase.Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 255–277why, one step back255//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 HISTORYupdate version numbersSean Barrettbefore adoptionc963e40
lines 278–279why, one step back278// 0.40 fix warning279// 0.39 fix warningmakefileSean Barrettbefore adoption84e2fc0
line 280why, one step back280// 0.38 fix warningMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 281–321why, one step back281// 0.37 fix warning282// 0.36 minor compiler support283// 0.35 layername button changes284// - layername buttons grow with the layer panel285// - fix stbte_create_map being declared as stbte_create286// - fix declaration of stbte_create_map287// 0.30 properties release288// - properties panel for editing user-defined "object" properties289// - can link each tile to one other tile290// - keyboard interface291// - fix eraser tool bug (worked in complex cases, failed in simple)292// - undo/redo tools have visible disabled state293// - tiles on higher layers draw on top of adjacent lower-layer tiles294// 0.20 erasable release295// - eraser tool296// - fix bug when pasting into protected layer297// - better color scheme298// - internal-use color picker299// 0.10 initial release 300//301// TODO302//303// Separate scroll state for each category304// Implement paint bucket305// Support STBTE_HITTEST_TILE above306// ?Cancel drags by clicking other button? - may be fixed307// Finish support for toolbar at side308//309// CREDITS310//311//312// Main editor & features313// Sean Barrett314// Additional features:315// Josh Huelsman316// Bugfixes:317// Ryan Whitworth318// Eugene Opalev319//320// LICENSE321//change license to public-domain AND mit (based on twitter vote)Sean Barrettbefore adoptione248e30
line 322why, one step back322// See end of file for license information.Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 323–400why, one step back323324325326///////////////////////////////////////////////////////////////////////327//328// HEADER SECTION329330#ifndef STB_TILEMAP_INCLUDE_STB_TILEMAP_EDITOR_H331#define STB_TILEMAP_INCLUDE_STB_TILEMAP_EDITOR_H332333#ifdef _WIN32334 #ifndef _CRT_SECURE_NO_WARNINGS335 #define _CRT_SECURE_NO_WARNINGS336 #endif337 #include <stdlib.h>338 #include <stdio.h>339#endif340341typedef struct stbte_tilemap stbte_tilemap;342343// these are the drawmodes used in STBTE_DRAW_TILE344enum345{346 STBTE_drawmode_deemphasize = -1,347 STBTE_drawmode_normal = 0,348 STBTE_drawmode_emphasize = 1,349};350351// these are the property types352#define STBTE_PROP_none 0353#define STBTE_PROP_int 1354#define STBTE_PROP_float 2355#define STBTE_PROP_bool 3356#define STBTE_PROP_disabled 4357358////////359//360// creation361//362363extern stbte_tilemap *stbte_create_map(int map_x, int map_y, int map_layers, int spacing_x, int spacing_y, int max_tiles);364// create an editable tilemap365// map_x : dimensions of map horizontally (user can change this in editor), <= STBTE_MAX_TILEMAP_X366// map_y : dimensions of map vertically (user can change this in editor) <= STBTE_MAX_TILEMAP_Y367// map_layers : number of layers to use (fixed), <= STBTE_MAX_LAYERS368// spacing_x : initial horizontal distance between left edges of map tiles in stb_tilemap_editor pixels369// spacing_y : initial vertical distance between top edges of map tiles in stb_tilemap_editor pixels370// max_tiles : maximum number of tiles that can defined371//372// If insufficient memory, returns NULL373374extern void stbte_define_tile(stbte_tilemap *tm, unsigned short id, unsigned int layermask, const char * category);375// call this repeatedly for each tile to install the tile definitions into the editable tilemap376// tm : tilemap created by stbte_create_map377// id : unique identifier for each tile, 0 <= id < 32768378// layermask : bitmask of which layers tile is allowed on: 1 = layer 0, 255 = layers 0..7379// (note that onscreen, the editor numbers the layers from 1 not 0)380// layer 0 is the furthest back, layer 1 is just in front of layer 0, etc381// category : which category this tile is grouped in382383extern void stbte_set_display(int x0, int y0, int x1, int y1);384// call this once to set the size; if you resize, call it again385386387/////////388//389// every frame390//391392extern void stbte_draw(stbte_tilemap *tm);393394extern void stbte_tick(stbte_tilemap *tm, float time_in_seconds_since_last_frame);395396////////////397//398// user input399//400Merge branch 'patch-1' of https://github.com/sherjilozair/stb into workingSean Barrettbefore adoption9bb4bc1
line 401why, one step back401// if you're using SDL, call the next function for SDL_MOUSEMOTION, SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONUP, SDL_MOUSEWHEEL;Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 402–408why, one step back402// the transformation lets you scale from SDL mouse coords to stb_tilemap_editor coords403extern void stbte_mouse_sdl(stbte_tilemap *tm, const void *sdl_event, float xscale, float yscale, int xoffset, int yoffset);404405// otherwise, hook these up explicitly:406extern void stbte_mouse_move(stbte_tilemap *tm, int x, int y, int shifted, int scrollkey);407extern void stbte_mouse_button(stbte_tilemap *tm, int x, int y, int right, int down, int shifted, int scrollkey);408extern void stbte_mouse_wheel(stbte_tilemap *tm, int x, int y, int vscroll);Merge branch 'patch-1' of https://github.com/sherjilozair/stb into workingSean Barrettbefore adoption9bb4bc1
lines 409–410why, one step back409410// note: at the moment, mouse wheel events (SDL_MOUSEWHEEL) are ignored.Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 411–973why, one step back411412// for keyboard, define your own mapping from keys to the following actions.413// this is totally optional, as all features are accessible with the mouse414enum stbte_action415{416 STBTE_tool_select,417 STBTE_tool_brush,418 STBTE_tool_erase,419 STBTE_tool_rectangle,420 STBTE_tool_eyedropper,421 STBTE_tool_link,422 STBTE_act_toggle_grid,423 STBTE_act_toggle_links,424 STBTE_act_undo,425 STBTE_act_redo,426 STBTE_act_cut,427 STBTE_act_copy,428 STBTE_act_paste,429 STBTE_scroll_left,430 STBTE_scroll_right,431 STBTE_scroll_up,432 STBTE_scroll_down,433};434extern void stbte_action(stbte_tilemap *tm, enum stbte_action act);435436////////////////437//438// save/load 439//440// There is no editor file format. You have to save and load the data yourself441// through the following functions. You can also use these functions to get the442// data to generate game-formatted levels directly. (But make sure you save443// first! You may also want to autosave to a temp file periodically, etc etc.)444445#define STBTE_EMPTY -1446447extern void stbte_get_dimensions(stbte_tilemap *tm, int *max_x, int *max_y);448// get the dimensions of the level, since the user can change them449450extern short* stbte_get_tile(stbte_tilemap *tm, int x, int y);451// returns an array of shorts that is 'map_layers' in length. each short is452// either one of the tile_id values from define_tile, or STBTE_EMPTY.453454extern float *stbte_get_properties(stbte_tilemap *tm, int x, int y);455// get the property array associated with the tile at x,y. this is an456// array of floats that is STBTE_MAX_PROPERTIES in length; you have to457// interpret the slots according to the semantics you've chosen458459extern void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty);460// gets the link associated with the tile at x,y.461462extern void stbte_set_dimensions(stbte_tilemap *tm, int max_x, int max_y);463// set the dimensions of the level, overrides previous stbte_create_map()464// values or anything the user has changed465466extern void stbte_clear_map(stbte_tilemap *tm);467// clears the map, including the region outside the defined region, so if the468// user expands the map, they won't see garbage there469470extern void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile);471// tile is your tile_id from define_tile, or STBTE_EMPTY472473extern void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val);474// set the value of the n'th slot of the tile at x,y475476extern void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty);477// set a link going from x,y to destx,desty. to force no link,478// use destx=desty=-1479480////////481//482// optional483//484485extern void stbte_set_background_tile(stbte_tilemap *tm, short id);486// selects the tile to fill the bottom layer with and used to clear bottom tiles to;487// should be same ID as 488489extern void stbte_set_sidewidths(int left, int right);490// call this once to set the left & right side widths. don't call491// it again since the user can change it492493extern void stbte_set_spacing(stbte_tilemap *tm, int spacing_x, int spacing_y, int palette_spacing_x, int palette_spacing_y);494// call this to set the spacing of map tiles and the spacing of palette tiles.495// if you rescale your display, call it again (e.g. you can implement map zooming yourself)496497extern void stbte_set_layername(stbte_tilemap *tm, int layer, const char *layername);498// sets a string name for your layer that shows in the layer selector. note that this499// makes the layer selector wider. 'layer' is from 0..(map_layers-1)500501#endif502503#ifdef STB_TILEMAP_EDITOR_IMPLEMENTATION504505#ifndef STBTE_ASSERT506#define STBTE_ASSERT assert507#include <assert.h>508#endif509510#ifdef _MSC_VER511#define STBTE__NOTUSED(v) (void)(v)512#else513#define STBTE__NOTUSED(v) (void)sizeof(v)514#endif515516#ifndef STBTE_MAX_TILEMAP_X517#define STBTE_MAX_TILEMAP_X 200518#endif519520#ifndef STBTE_MAX_TILEMAP_Y521#define STBTE_MAX_TILEMAP_Y 200522#endif523524#ifndef STBTE_MAX_LAYERS525#define STBTE_MAX_LAYERS 8526#endif527528#ifndef STBTE_MAX_CATEGORIES529#define STBTE_MAX_CATEGORIES 100530#endif531532#ifndef STBTE_MAX_COPY533#define STBTE_MAX_COPY 65536534#endif535536#ifndef STBTE_UNDO_BUFFER_BYTES537#define STBTE_UNDO_BUFFER_BYTES (1 << 24) // 16 MB538#endif539540#ifndef STBTE_PROP_TYPE541#define STBTE__NO_PROPS542#define STBTE_PROP_TYPE(n,td,tp) 0543#endif544545#ifndef STBTE_PROP_NAME546#define STBTE_PROP_NAME(n,td,tp) ""547#endif548549#ifndef STBTE_MAX_PROPERTIES550#define STBTE_MAX_PROPERTIES 10551#endif552553#ifndef STBTE_PROP_MIN554#define STBTE_PROP_MIN(n,td,tp) 0555#endif556557#ifndef STBTE_PROP_MAX558#define STBTE_PROP_MAX(n,td,tp) 100.0559#endif560561#ifndef STBTE_PROP_FLOAT_SCALE562#define STBTE_PROP_FLOAT_SCALE(n,td,tp) 1 // default scale size563#endif564565#ifndef STBTE_FLOAT_CONTROL_GRANULARITY566#define STBTE_FLOAT_CONTROL_GRANULARITY 4567#endif568569570#define STBTE__UNDO_BUFFER_COUNT (STBTE_UNDO_BUFFER_BYTES>>1)571572#if STBTE_MAX_TILEMAP_X > 4096 || STBTE_MAX_TILEMAP_Y > 4096573#error "Maximum editable map size is 4096 x 4096"574#endif575#if STBTE_MAX_LAYERS > 32576#error "Maximum layers allowed is 32"577#endif578#if STBTE_UNDO_BUFFER_COUNT & (STBTE_UNDO_BUFFER_COUNT-1)579#error "Undo buffer size must be a power of 2"580#endif581582#if STBTE_MAX_PROPERTIES == 0583#define STBTE__NO_PROPS584#endif585586#ifdef STBTE__NO_PROPS587#undef STBTE_MAX_PROPERTIES588#define STBTE_MAX_PROPERTIES 1 // so we can declare arrays589#endif590591typedef struct592{593 short x,y;594} stbte__link;595596enum597{598 STBTE__base,599 STBTE__outline,600 STBTE__text,601602 STBTE__num_color_aspects,603};604605enum606{607 STBTE__idle,608 STBTE__over,609 STBTE__down,610 STBTE__over_down,611 STBTE__selected,612 STBTE__selected_over,613 STBTE__disabled,614 STBTE__num_color_states,615};616617enum618{619 STBTE__cexpander,620 STBTE__ctoolbar,621 STBTE__ctoolbar_button,622 STBTE__cpanel,623 STBTE__cpanel_sider,624 STBTE__cpanel_sizer,625 STBTE__cscrollbar,626 STBTE__cmapsize,627 STBTE__clayer_button,628 STBTE__clayer_hide,629 STBTE__clayer_lock,630 STBTE__clayer_solo,631 STBTE__ccategory_button,632633 STBTE__num_color_modes,634};635636#ifdef STBTE__COLORPICKER637static char *stbte__color_names[] =638{639 "expander", "toolbar", "tool button", "panel",640 "panel c1", "panel c2", "scollbar", "map button",641 "layer", "hide", "lock", "solo",642 "category",643};644#endif // STBTE__COLORPICKER645646 // idle, over, down, over&down, selected, sel&over, disabled647static int stbte__color_table[STBTE__num_color_modes][STBTE__num_color_aspects][STBTE__num_color_states] =648{649 {650 { 0x000000, 0x84987c, 0xdcdca8, 0xdcdca8, 0x40c040, 0x60d060, 0x505050, },651 { 0xa4b090, 0xe0ec80, 0xffffc0, 0xffffc0, 0x80ff80, 0x80ff80, 0x606060, },652 { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },653 }, {654 { 0x808890, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, },655 { 0x605860, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, 0x606060, },656 { 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, },657 }, {658 { 0x3c5068, 0x7088a8, 0x647488, 0x94b4dc, 0x8890c4, 0x9caccc, 0x404040, },659 { 0x889cb8, 0x889cb8, 0x889cb8, 0x889cb8, 0x84c4e8, 0xacc8ff, 0x0c0c08, },660 { 0xbcc4cc, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x707074, },661 }, {662 { 0x403848, 0x403010, 0x403010, 0x403010, 0x403010, 0x403010, 0x303024, },663 { 0x68546c, 0xc08040, 0xc08040, 0xc08040, 0xc08040, 0xc08040, 0x605030, },664 { 0xf4e4ff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },665 }, {666 { 0xb4b04c, 0xacac60, 0xc0ffc0, 0xc0ffc0, 0x40c040, 0x60d060, 0x505050, },667 { 0xa0a04c, 0xd0d04c, 0xffff80, 0xffff80, 0x80ff80, 0x80ff80, 0x606060, },668 { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },669 }, {670 { 0x40c440, 0x60d060, 0xc0ffc0, 0xc0ffc0, 0x40c040, 0x60d060, 0x505050, },671 { 0x40c040, 0x80ff80, 0x80ff80, 0x80ff80, 0x80ff80, 0x80ff80, 0x606060, },672 { 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },673 }, {674 { 0x9090ac, 0xa0a0b8, 0xbcb8cc, 0xbcb8cc, 0x909040, 0x909040, 0x909040, },675 { 0xa0a0b8, 0xb0b4d0, 0xa0a0b8, 0xa0a0b8, 0xa0a050, 0xa0a050, 0xa0a050, },676 { 0x808088, 0x808030, 0x808030, 0x808030, 0x808030, 0x808030, 0x808030, },677 }, {678 { 0x704c70, 0x885c8c, 0x9c68a4, 0xb870bc, 0xb490bc, 0xb490bc, 0x302828, },679 { 0x646064, 0xcca8d4, 0xc060c0, 0xa07898, 0xe0b8e0, 0xe0b8e0, 0x403838, },680 { 0xdccce4, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },681 }, {682 { 0x704c70, 0x885c8c, 0x9c68a4, 0xb870bc, 0xb490bc, 0xb490bc, 0x302828, },683 { 0xb09cb4, 0xcca8d4, 0xc060c0, 0xa07898, 0xe0b8e0, 0xe0b8e0, 0x403838, },684 { 0xdccce4, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0xffffff, 0x909090, },685 }, {686 { 0x646494, 0x888cb8, 0xb0b0b0, 0xb0b0cc, 0x9c9cf4, 0x8888b0, 0x50506c, },687 { 0x9090a4, 0xb0b4d4, 0xb0b0dc, 0xb0b0cc, 0xd0d0fc, 0xd0d4f0, 0x606060, },688 { 0xb4b4d4, 0xe4e4ff, 0xffffff, 0xffffff, 0xe0e4ff, 0xececff, 0x909090, },689 }, {690 { 0x646444, 0x888c64, 0xb0b0b0, 0xb0b088, 0xaca858, 0x88886c, 0x505050, },691 { 0x88886c, 0xb0b490, 0xb0b0b0, 0xb0b088, 0xd8d898, 0xd0d4b0, 0x606060, },692 { 0xb4b49c, 0xffffd8, 0xffffff, 0xffffd4, 0xffffdc, 0xffffcc, 0x909090, },693 }, {694 { 0x906464, 0xb48c8c, 0xd4b0b0, 0xdcb0b0, 0xff9c9c, 0xc88888, 0x505050, },695 { 0xb47c80, 0xd4b4b8, 0xc4a8a8, 0xdcb0b0, 0xffc0c0, 0xfce8ec, 0x606060, },696 { 0xe0b4b4, 0xffdcd8, 0xffd8d4, 0xffe0e4, 0xffece8, 0xffffff, 0x909090, },697 }, {698 { 0x403848, 0x403848, 0x403848, 0x886894, 0x7c80c8, 0x7c80c8, 0x302828, },699 { 0x403848, 0x403848, 0x403848, 0x403848, 0x7c80c8, 0x7c80c8, 0x403838, },700 { 0xc8c4c8, 0xffffff, 0xffffff, 0xffffff, 0xe8e8ec, 0xffffff, 0x909090, },701 },702};703704#define STBTE_COLOR_TILEMAP_BACKGROUND 0x000000705#define STBTE_COLOR_TILEMAP_BORDER 0x203060706#define STBTE_COLOR_TILEMAP_HIGHLIGHT 0xffffff707#define STBTE_COLOR_GRID 0x404040708#define STBTE_COLOR_SELECTION_OUTLINE1 0xdfdfdf709#define STBTE_COLOR_SELECTION_OUTLINE2 0x303030710#define STBTE_COLOR_TILEPALETTE_OUTLINE 0xffffff711#define STBTE_COLOR_TILEPALETTE_BACKGROUND 0x000000712713#ifndef STBTE_LINK_COLOR714#define STBTE_LINK_COLOR(src,sp,dest,dp) 0x5030ff715#endif716717#ifndef STBTE_LINK_COLOR_DRAWING718#define STBTE_LINK_COLOR_DRAWING 0xff40ff719#endif720721#ifndef STBTE_LINK_COLOR_DISALLOWED722#define STBTE_LINK_COLOR_DISALLOWED 0x602060723#endif724725726// disabled, selected, down, over727static unsigned char stbte__state_to_index[2][2][2][2] =728{729 {730 { { STBTE__idle , STBTE__over }, { STBTE__down , STBTE__over_down }, },731 { { STBTE__selected, STBTE__selected_over }, { STBTE__down , STBTE__over_down }, },732 },{733 { { STBTE__disabled, STBTE__disabled }, { STBTE__disabled, STBTE__disabled }, },734 { { STBTE__selected, STBTE__selected_over }, { STBTE__disabled, STBTE__disabled }, },735 }736};737#define STBTE__INDEX_FOR_STATE(disable,select,down,over) stbte__state_to_index[disable][select][down][over]738#define STBTE__INDEX_FOR_ID(id,disable,select) STBTE__INDEX_FOR_STATE(disable,select,STBTE__IS_ACTIVE(id),STBTE__IS_HOT(id))739740#define STBTE__FONT_HEIGHT 9741static short stbte__font_offset[95+16];742static short stbte__fontdata[769] =743{744 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,745 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,746 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,747 0,0,0,0,2,253,130,456,156,8,72,184,64,2,125,66,64,160,64,146,511,146,146,748 511,146,146,511,146,511,257,341,297,341,297,341,257,511,16,56,124,16,16,16,749 124,56,16,96,144,270,261,262,136,80,48,224,192,160,80,40,22,14,15,3,448,496,750 496,240,232,20,10,5,2,112,232,452,450,225,113,58,28,63,30,60,200,455,257,751 257,0,0,0,257,257,455,120,204,132,132,159,14,4,4,14,159,132,132,204,120,8,752 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,753 127,34,34,127,127,34,36,46,107,107,58,18,99,51,24,12,102,99,48,122,79,93,754 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,755 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,756 127,127,64,64,98,115,89,77,71,66,33,97,73,93,119,35,24,28,22,127,127,16,39,757 103,69,69,125,57,62,127,73,73,121,48,1,1,113,121,15,7,54,127,73,73,127,54,758 6,79,73,105,63,30,54,54,128,246,118,8,28,54,99,65,20,20,20,20,65,99,54,28,759 8,2,3,105,109,7,2,30,63,33,45,47,46,124,126,19,19,126,124,127,127,73,73,127,760 54,62,127,65,65,99,34,127,127,65,99,62,28,127,127,73,73,73,65,127,127,9,9,761 9,1,62,127,65,73,121,121,127,127,8,8,127,127,65,65,127,127,65,65,32,96,64,762 64,127,63,127,127,8,28,54,99,65,127,127,64,64,64,64,127,127,6,12,6,127,127,763 127,127,6,12,24,127,127,62,127,65,65,65,127,62,127,127,9,9,15,6,62,127,65,764 81,49,127,94,127,127,9,25,127,102,70,79,73,73,121,49,1,1,127,127,1,1,63,127,765 64,64,127,63,15,31,48,96,48,31,15,127,127,48,24,48,127,127,99,119,28,28,119,766 99,7,15,120,120,15,7,97,113,89,77,71,67,127,127,65,65,3,6,12,24,48,96,65,767 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,768 127,127,68,68,124,56,56,124,68,68,68,56,124,68,68,127,127,56,124,84,84,92,769 24,8,124,126,10,10,56,380,324,324,508,252,127,127,4,4,124,120,72,122,122,770 64,256,256,256,506,250,126,126,16,56,104,64,66,126,126,64,124,124,24,56,28,771 124,120,124,124,4,4,124,120,56,124,68,68,124,56,508,508,68,68,124,56,56,124,772 68,68,508,508,124,124,4,4,12,8,72,92,84,84,116,36,4,4,62,126,68,68,60,124,773 64,64,124,124,28,60,96,96,60,28,28,124,112,56,112,124,28,68,108,56,56,108,774 68,284,316,352,320,508,252,68,100,116,92,76,68,8,62,119,65,65,127,127,65,775 65,119,62,8,16,24,12,12,24,24,12,4,776};777778typedef struct779{780 short id;781 unsigned short category_id;782 char *category;783 unsigned int layermask;784} stbte__tileinfo;785786#define MAX_LAYERMASK (1 << (8*sizeof(unsigned int)))787788typedef short stbte__tiledata;789790#define STBTE__NO_TILE -1791792enum793{794 STBTE__panel_toolbar,795 STBTE__panel_colorpick,796 STBTE__panel_info,797 STBTE__panel_layers,798 STBTE__panel_props,799 STBTE__panel_categories,800 STBTE__panel_tiles,801802 STBTE__num_panel,803};804805enum806{807 STBTE__side_left,808 STBTE__side_right,809 STBTE__side_top,810 STBTE__side_bottom,811};812813enum814{815 STBTE__tool_select,816 STBTE__tool_brush,817 STBTE__tool_erase,818 STBTE__tool_rect,819 STBTE__tool_eyedrop,820 STBTE__tool_fill,821 STBTE__tool_link,822823 STBTE__tool_showgrid,824 STBTE__tool_showlinks,825826 STBTE__tool_undo,827 STBTE__tool_redo,828 // copy/cut/paste aren't included here because they're displayed differently829830 STBTE__num_tool,831};832833// icons are stored in the 0-31 range of ASCII in the font834static int toolchar[] = { 26,24,25,20,23,22,18, 19,17, 29,28, };835836enum837{838 STBTE__propmode_default,839 STBTE__propmode_always,840 STBTE__propmode_never,841};842843enum844{845 STBTE__paint,846847 // from here down does hittesting848 STBTE__tick,849 STBTE__mousemove,850 STBTE__mousewheel,851 STBTE__leftdown,852 STBTE__leftup,853 STBTE__rightdown,854 STBTE__rightup,855};856857typedef struct858{859 int expanded, mode;860 int delta_height; // number of rows they've requested for this861 int side;862 int width,height;863 int x0,y0;864} stbte__panel;865866typedef struct867{868 int x0,y0,x1,y1,color;869} stbte__colorrect;870871#define STBTE__MAX_DELAYRECT 256872873typedef struct874{875 int tool, active_event;876 int active_id, hot_id, next_hot_id;877 int event;878 int mx,my, dx,dy;879 int ms_time;880 int shift, scrollkey;881 int initted;882 int side_extended[2];883 stbte__colorrect delayrect[STBTE__MAX_DELAYRECT];884 int delaycount;885 int show_grid, show_links;886 int brush_state; // used to decide which kind of erasing887 int eyedrop_x, eyedrop_y, eyedrop_last_layer;888 int pasting, paste_x, paste_y;889 int scrolling, start_x, start_y;890 int last_mouse_x, last_mouse_y;891 int accum_x, accum_y;892 int linking;893 int dragging;894 int drag_x, drag_y, drag_w, drag_h;895 int drag_offx, drag_offy, drag_dest_x, drag_dest_y;896 int undoing;897 int has_selection, select_x0, select_y0, select_x1, select_y1;898 int sx,sy;899 int x0,y0,x1,y1, left_width, right_width; // configurable widths900 float alert_timer;901 const char *alert_msg;902 float dt;903 stbte__panel panel[STBTE__num_panel];904 short copybuffer[STBTE_MAX_COPY][STBTE_MAX_LAYERS];905 float copyprops[STBTE_MAX_COPY][STBTE_MAX_PROPERTIES];906#ifdef STBTE_ALLOW_LINK907 stbte__link copylinks[STBTE_MAX_COPY];908#endif909 int copy_src_x, copy_src_y;910 stbte_tilemap *copy_src;911 int copy_width,copy_height,has_copy,copy_has_props;912} stbte__ui_t;913914// there's only one UI system at a time, so we can globalize this915static stbte__ui_t stbte__ui = { STBTE__tool_brush, 0 };916917#define STBTE__INACTIVE() (stbte__ui.active_id == 0)918#define STBTE__IS_ACTIVE(id) (stbte__ui.active_id == (id))919#define STBTE__IS_HOT(id) (stbte__ui.hot_id == (id))920921#define STBTE__BUTTON_HEIGHT (STBTE__FONT_HEIGHT + 2 * STBTE__BUTTON_INTERNAL_SPACING)922#define STBTE__BUTTON_INTERNAL_SPACING (2 + (STBTE__FONT_HEIGHT>>4))923924typedef struct925{926 const char *name;927 int locked;928 int hidden;929} stbte__layer;930931enum932{933 STBTE__unlocked,934 STBTE__protected,935 STBTE__locked,936};937938struct stbte_tilemap939{940 stbte__tiledata data[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_LAYERS];941 float props[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_PROPERTIES];942 #ifdef STBTE_ALLOW_LINK943 stbte__link link[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];944 int linkcount[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];945 #endif946 int max_x, max_y, num_layers;947 int spacing_x, spacing_y;948 int palette_spacing_x, palette_spacing_y;949 int scroll_x,scroll_y;950 int cur_category, cur_tile, cur_layer;951 char *categories[STBTE_MAX_CATEGORIES];952 int num_categories, category_scroll;953 stbte__tileinfo *tiles;954 int num_tiles, max_tiles, digits;955 unsigned char undo_available_valid;956 unsigned char undo_available;957 unsigned char redo_available;958 unsigned char padding;959 int cur_palette_count;960 int palette_scroll;961 int tileinfo_dirty;962 stbte__layer layerinfo[STBTE_MAX_LAYERS];963 int has_layer_names;964 int layername_width;965 int layer_scroll;966 int propmode;967 int solo_layer;968 int undo_pos, undo_len, redo_len;969 short background_tile;970 unsigned char id_in_use[32768>>3];971 short *undo_buffer;972};973Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 974why, one step back974static char *default_category = (char*) "[unassigned]";Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 975–1065why, one step back975976static void stbte__init_gui(void)977{978 int i,n;979 stbte__ui.initted = 1;980 // init UI state981 stbte__ui.show_links = 1;982 for (i=0; i < STBTE__num_panel; ++i) {983 stbte__ui.panel[i].expanded = 1; // visible if not autohidden984 stbte__ui.panel[i].delta_height = 0;985 stbte__ui.panel[i].side = STBTE__side_left;986 }987 stbte__ui.panel[STBTE__panel_toolbar ].side = STBTE__side_top;988 stbte__ui.panel[STBTE__panel_colorpick].side = STBTE__side_right;989990 if (stbte__ui.left_width == 0)991 stbte__ui.left_width = 80;992 if (stbte__ui.right_width == 0)993 stbte__ui.right_width = 80;994995 // init font996 n=95+16;997 for (i=0; i < 95+16; ++i) {998 stbte__font_offset[i] = n;999 n += stbte__fontdata[i];1000 }1001}10021003stbte_tilemap *stbte_create_map(int map_x, int map_y, int map_layers, int spacing_x, int spacing_y, int max_tiles)1004{1005 int i;1006 stbte_tilemap *tm;1007 STBTE_ASSERT(map_layers >= 0 && map_layers <= STBTE_MAX_LAYERS);1008 STBTE_ASSERT(map_x >= 0 && map_x <= STBTE_MAX_TILEMAP_X);1009 STBTE_ASSERT(map_y >= 0 && map_y <= STBTE_MAX_TILEMAP_Y);1010 if (map_x < 0 || map_y < 0 || map_layers < 0 ||1011 map_x > STBTE_MAX_TILEMAP_X || map_y > STBTE_MAX_TILEMAP_Y || map_layers > STBTE_MAX_LAYERS)1012 return NULL;1013 1014 if (!stbte__ui.initted)1015 stbte__init_gui();10161017 tm = (stbte_tilemap *) malloc(sizeof(*tm) + sizeof(*tm->tiles) * max_tiles + STBTE_UNDO_BUFFER_BYTES);1018 if (tm == NULL)1019 return NULL;10201021 tm->tiles = (stbte__tileinfo *) (tm+1);1022 tm->undo_buffer = (short *) (tm->tiles + max_tiles);1023 tm->num_layers = map_layers;1024 tm->max_x = map_x;1025 tm->max_y = map_y;1026 tm->spacing_x = spacing_x;1027 tm->spacing_y = spacing_y;1028 tm->scroll_x = 0;1029 tm->scroll_y = 0;1030 tm->palette_scroll = 0;1031 tm->palette_spacing_x = spacing_x+1;1032 tm->palette_spacing_y = spacing_y+1;1033 tm->cur_category = -1;1034 tm->cur_tile = 0;1035 tm->solo_layer = -1;1036 tm->undo_len = 0;1037 tm->redo_len = 0;1038 tm->undo_pos = 0;1039 tm->category_scroll = 0;1040 tm->layer_scroll = 0;1041 tm->propmode = 0;1042 tm->has_layer_names = 0;1043 tm->layername_width = 0;1044 tm->undo_available_valid = 0;10451046 for (i=0; i < tm->num_layers; ++i) {1047 tm->layerinfo[i].hidden = 0;1048 tm->layerinfo[i].locked = STBTE__unlocked;1049 tm->layerinfo[i].name = 0;1050 }10511052 tm->background_tile = STBTE__NO_TILE;1053 stbte_clear_map(tm);10541055 tm->max_tiles = max_tiles;1056 tm->num_tiles = 0;1057 for (i=0; i < 32768/8; ++i)1058 tm->id_in_use[i] = 0;1059 tm->tileinfo_dirty = 1;1060 return tm;1061}10621063void stbte_set_background_tile(stbte_tilemap *tm, short id)1064{1065 int i;various fixes for clangSean Barrettbefore adoption5fe7fb5
lines 1066–1068why, one step back1066 STBTE_ASSERT(id >= -1);1067 // STBTE_ASSERT(id < 32768);1068 if (id < -1)Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1069–1174why, one step back1069 return;1070 for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i)1071 if (tm->data[0][i][0] == -1)1072 tm->data[0][i][0] = id;1073 tm->background_tile = id;1074}10751076void stbte_set_spacing(stbte_tilemap *tm, int spacing_x, int spacing_y, int palette_spacing_x, int palette_spacing_y)1077{1078 tm->spacing_x = spacing_x;1079 tm->spacing_y = spacing_y;1080 tm->palette_spacing_x = palette_spacing_x;1081 tm->palette_spacing_y = palette_spacing_y;1082}10831084void stbte_set_sidewidths(int left, int right)1085{1086 stbte__ui.left_width = left;1087 stbte__ui.right_width = right;1088}10891090void stbte_set_display(int x0, int y0, int x1, int y1)1091{1092 stbte__ui.x0 = x0;1093 stbte__ui.y0 = y0;1094 stbte__ui.x1 = x1;1095 stbte__ui.y1 = y1;1096}10971098void stbte_define_tile(stbte_tilemap *tm, unsigned short id, unsigned int layermask, const char * category_c)1099{1100 char *category = (char *) category_c;1101 STBTE_ASSERT(id < 32768);1102 STBTE_ASSERT(tm->num_tiles < tm->max_tiles);1103 STBTE_ASSERT((tm->id_in_use[id>>3]&(1<<(id&7))) == 0);1104 if (id >= 32768 || tm->num_tiles >= tm->max_tiles || (tm->id_in_use[id>>3]&(1<<(id&7))))1105 return;11061107 if (category == NULL)1108 category = (char*) default_category;1109 tm->id_in_use[id>>3] |= 1 << (id&7);1110 tm->tiles[tm->num_tiles].category = category;1111 tm->tiles[tm->num_tiles].id = id;1112 tm->tiles[tm->num_tiles].layermask = layermask;1113 ++tm->num_tiles;1114 tm->tileinfo_dirty = 1;1115}11161117static int stbte__text_width(const char *str);11181119void stbte_set_layername(stbte_tilemap *tm, int layer, const char *layername)1120{1121 STBTE_ASSERT(layer >= 0 && layer < tm->num_layers);1122 if (layer >= 0 && layer < tm->num_layers) {1123 int width;1124 tm->layerinfo[layer].name = layername;1125 tm->has_layer_names = 1;1126 width = stbte__text_width(layername);1127 tm->layername_width = (width > tm->layername_width ? width : tm->layername_width);1128 }1129}11301131void stbte_get_dimensions(stbte_tilemap *tm, int *max_x, int *max_y)1132{1133 *max_x = tm->max_x;1134 *max_y = tm->max_y;1135}11361137short* stbte_get_tile(stbte_tilemap *tm, int x, int y)1138{1139 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1140 if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1141 return NULL;1142 return tm->data[y][x];1143}11441145float *stbte_get_properties(stbte_tilemap *tm, int x, int y)1146{1147 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1148 if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1149 return NULL;1150 return tm->props[y][x];1151}11521153void stbte_get_link(stbte_tilemap *tm, int x, int y, int *destx, int *desty)1154{1155 int gx=-1,gy=-1;1156 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1157#ifdef STBTE_ALLOW_LINK1158 if (x >= 0 && x < STBTE_MAX_TILEMAP_X && y >= 0 && y < STBTE_MAX_TILEMAP_Y) {1159 gx = tm->link[y][x].x;1160 gy = tm->link[y][x].y;1161 if (gx >= 0)1162 if (!STBTE_ALLOW_LINK(tm->data[y][x], tm->props[y][x], tm->data[gy][gx], tm->props[gy][gx]))1163 gx = gy = -1;1164 }1165#endif1166 *destx = gx;1167 *desty = gy;1168}11691170void stbte_set_property(stbte_tilemap *tm, int x, int y, int n, float val)1171{1172 tm->props[y][x][n] = val;1173}1174Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1175why, one step back1175#ifdef STBTE_ALLOW_LINKMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
line 1176why, one step back1176static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode);Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1177why, one step back1177#endifMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1178–1229why, one step back11781179enum1180{1181 STBTE__undo_none,1182 STBTE__undo_record,1183 STBTE__undo_block,1184};11851186void stbte_set_link(stbte_tilemap *tm, int x, int y, int destx, int desty)1187{1188#ifdef STBTE_ALLOW_LINK1189 stbte__set_link(tm, x, y, destx, desty, STBTE__undo_none);1190#else1191 STBTE_ASSERT(0);1192#endif1193}119411951196// returns an array of map_layers shorts. each short is either1197// one of the tile_id values from define_tile, or STBTE_EMPTY11981199void stbte_set_dimensions(stbte_tilemap *tm, int map_x, int map_y)1200{1201 STBTE_ASSERT(map_x >= 0 && map_x <= STBTE_MAX_TILEMAP_X);1202 STBTE_ASSERT(map_y >= 0 && map_y <= STBTE_MAX_TILEMAP_Y);1203 if (map_x < 0 || map_y < 0 || map_x > STBTE_MAX_TILEMAP_X || map_y > STBTE_MAX_TILEMAP_Y)1204 return;1205 tm->max_x = map_x;1206 tm->max_y = map_y;1207}12081209void stbte_clear_map(stbte_tilemap *tm)1210{1211 int i,j;1212 for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i) {1213 tm->data[0][i][0] = tm->background_tile;1214 for (j=1; j < tm->num_layers; ++j)1215 tm->data[0][i][j] = STBTE__NO_TILE;1216 for (j=0; j < STBTE_MAX_PROPERTIES; ++j)1217 tm->props[0][i][j] = 0;1218 #ifdef STBTE_ALLOW_LINK1219 tm->link[0][i].x = -1;1220 tm->link[0][i].y = -1;1221 tm->linkcount[0][i] = 0;1222 #endif1223 }1224}12251226void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile)1227{1228 STBTE_ASSERT(x >= 0 && x < tm->max_x && y >= 0 && y < tm->max_y);1229 STBTE_ASSERT(layer >= 0 && layer < tm->num_layers);various fixes for clangSean Barrettbefore adoption5fe7fb5
lines 1230–1231why, one step back1230 STBTE_ASSERT(tile >= -1);1231 //STBTE_ASSERT(tile < 32768);Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1232–1258why, one step back1232 if (x < 0 || x >= STBTE_MAX_TILEMAP_X || y < 0 || y >= STBTE_MAX_TILEMAP_Y)1233 return;1234 if (layer < 0 || layer >= tm->num_layers || tile < -1)1235 return;1236 tm->data[y][x][layer] = tile;1237}12381239static void stbte__choose_category(stbte_tilemap *tm, int category)1240{1241 int i,n=0;1242 tm->cur_category = category;1243 for (i=0; i < tm->num_tiles; ++i)1244 if (tm->tiles[i].category_id == category || category == -1)1245 ++n;1246 tm->cur_palette_count = n;1247 tm->palette_scroll = 0;1248}12491250static int stbte__strequal(char *p, char *q)1251{1252 while (*p)1253 if (*p++ != *q++) return 0;1254 return *q == 0;1255}12561257static void stbte__compute_tileinfo(stbte_tilemap *tm)1258{Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1259why, one step back1259 int i,j;Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1260–1653why, one step back12601261 tm->num_categories=0;12621263 for (i=0; i < tm->num_tiles; ++i) {1264 stbte__tileinfo *t = &tm->tiles[i];1265 // find category1266 for (j=0; j < tm->num_categories; ++j)1267 if (stbte__strequal(t->category, tm->categories[j]))1268 goto found;1269 tm->categories[j] = t->category;1270 ++tm->num_categories;1271 found:1272 t->category_id = (unsigned short) j;1273 }12741275 // currently number of categories can never decrease because you1276 // can't remove tile definitions, but let's get it right anyway1277 if (tm->cur_category > tm->num_categories) {1278 tm->cur_category = -1;1279 }12801281 stbte__choose_category(tm, tm->cur_category);12821283 tm->tileinfo_dirty = 0;1284}12851286static void stbte__prepare_tileinfo(stbte_tilemap *tm)1287{1288 if (tm->tileinfo_dirty)1289 stbte__compute_tileinfo(tm);1290}129112921293/////////////////////// undo system ////////////////////////12941295// the undo system works by storing "commands" into a buffer, and1296// then playing back those commands. undo and redo have to store1297// the commands in different order. 1298//1299// the commands are:1300//1301// 1) end_of_undo_record1302// -1:short1303//1304// 2) end_of_redo_record1305// -2:short1306//1307// 3) tile update1308// tile_id:short (-1..32767)1309// x_coord:short1310// y_coord:short1311// layer:short (0..31)1312//1313// 4) property update (also used for links)1314// value_hi:short1315// value_lo:short1316// y_coord:short1317// x_coord:short1318// property:short (256+prop#)1319//1320// Since we use a circular buffer, we might overwrite the undo storage.1321// To detect this, before playing back commands we scan back and see1322// if we see an end_of_undo_record before hitting the relevant boundary,1323// it's wholly contained.1324//1325// When we read back through, we see them in reverse order, so1326// we'll see the layer number or property number first1327//1328// To be clearer about the circular buffer, there are two cases:1329// 1. a single record is larger than the whole buffer.1330// this is caught because the end_of_undo_record will1331// get overwritten.1332// 2. multiple records written are larger than the whole1333// buffer, so some of them have been overwritten by1334// the later ones. this is handled by explicitly tracking1335// the undo length; we never try to parse the data that1336// got overwritten13371338// given two points, compute the length between them1339#define stbte__wrap(pos) ((pos) & (STBTE__UNDO_BUFFER_COUNT-1))13401341#define STBTE__undo_record -21342#define STBTE__redo_record -31343#define STBTE__undo_junk -4 // this is written underneath the undo pointer, never used13441345static void stbte__write_undo(stbte_tilemap *tm, short value)1346{1347 int pos = tm->undo_pos;1348 tm->undo_buffer[pos] = value;1349 tm->undo_pos = stbte__wrap(pos+1);1350 tm->undo_len += (tm->undo_len < STBTE__UNDO_BUFFER_COUNT-2);1351 tm->redo_len -= (tm->redo_len > 0);1352 tm->undo_available_valid = 0;1353}13541355static void stbte__write_redo(stbte_tilemap *tm, short value)1356{1357 int pos = tm->undo_pos;1358 tm->undo_buffer[pos] = value;1359 tm->undo_pos = stbte__wrap(pos-1);1360 tm->redo_len += (tm->redo_len < STBTE__UNDO_BUFFER_COUNT-2);1361 tm->undo_len -= (tm->undo_len > 0);1362 tm->undo_available_valid = 0;1363}13641365static void stbte__begin_undo(stbte_tilemap *tm)1366{1367 tm->redo_len = 0;1368 stbte__write_undo(tm, STBTE__undo_record);1369 stbte__ui.undoing = 1;1370 stbte__ui.alert_msg = 0; // clear alert if they start doing something1371}13721373static void stbte__end_undo(stbte_tilemap *tm)1374{1375 if (stbte__ui.undoing) {1376 // check if anything got written1377 int pos = stbte__wrap(tm->undo_pos-1);1378 if (tm->undo_buffer[pos] == STBTE__undo_record) {1379 // empty undo record, move back1380 tm->undo_pos = pos;1381 STBTE_ASSERT(tm->undo_len > 0);1382 tm->undo_len -= 1;1383 }1384 tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1385 // otherwise do nothing13861387 stbte__ui.undoing = 0;1388 }1389}13901391static void stbte__undo_record(stbte_tilemap *tm, int x, int y, int i, int v)1392{1393 STBTE_ASSERT(stbte__ui.undoing);1394 if (stbte__ui.undoing) {1395 stbte__write_undo(tm, v);1396 stbte__write_undo(tm, x);1397 stbte__write_undo(tm, y);1398 stbte__write_undo(tm, i);1399 }1400}14011402static void stbte__redo_record(stbte_tilemap *tm, int x, int y, int i, int v)1403{1404 stbte__write_redo(tm, v);1405 stbte__write_redo(tm, x);1406 stbte__write_redo(tm, y);1407 stbte__write_redo(tm, i);1408}14091410static float stbte__extract_float(short s0, short s1)1411{1412 union { float f; short s[2]; } converter;1413 converter.s[0] = s0;1414 converter.s[1] = s1;1415 return converter.f;1416}14171418static short stbte__extract_short(float f, int slot)1419{1420 union { float f; short s[2]; } converter;1421 converter.f = f;1422 return converter.s[slot];1423}14241425static void stbte__undo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1426{1427 STBTE_ASSERT(stbte__ui.undoing);1428 if (stbte__ui.undoing) {1429 stbte__write_undo(tm, s1);1430 stbte__write_undo(tm, s0);1431 stbte__write_undo(tm, x);1432 stbte__write_undo(tm, y);1433 stbte__write_undo(tm, 256+i);1434 }1435}14361437static void stbte__undo_record_prop_float(stbte_tilemap *tm, int x, int y, int i, float f)1438{1439 stbte__undo_record_prop(tm, x,y,i, stbte__extract_short(f,0), stbte__extract_short(f,1));1440}14411442static void stbte__redo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1443{1444 stbte__write_redo(tm, s1);1445 stbte__write_redo(tm, s0);1446 stbte__write_redo(tm, x);1447 stbte__write_redo(tm, y);1448 stbte__write_redo(tm, 256+i);1449}145014511452static int stbte__undo_find_end(stbte_tilemap *tm)1453{1454 // first scan through for the end record1455 int i, pos = stbte__wrap(tm->undo_pos-1);1456 for (i=0; i < tm->undo_len;) {1457 STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1458 if (tm->undo_buffer[pos] == STBTE__undo_record)1459 break;1460 if (tm->undo_buffer[pos] >= 255)1461 pos = stbte__wrap(pos-5), i += 5;1462 else1463 pos = stbte__wrap(pos-4), i += 4;1464 }1465 if (i >= tm->undo_len)1466 return -1;1467 return pos;1468}14691470static void stbte__undo(stbte_tilemap *tm)1471{1472 int i, pos, endpos;1473 endpos = stbte__undo_find_end(tm);1474 if (endpos < 0)1475 return;14761477 // we found a complete undo record1478 pos = stbte__wrap(tm->undo_pos-1);14791480 // start a redo record1481 stbte__write_redo(tm, STBTE__redo_record);14821483 // so now go back through undo and apply in reverse1484 // order, and copy it to redo1485 for (i=0; endpos != pos; i += 4) {1486 int x,y,n,v;1487 // get the undo entry1488 n = tm->undo_buffer[pos];1489 y = tm->undo_buffer[stbte__wrap(pos-1)];1490 x = tm->undo_buffer[stbte__wrap(pos-2)];1491 v = tm->undo_buffer[stbte__wrap(pos-3)];1492 if (n >= 255) {1493 short s0=0,s1=0;1494 int v2 = tm->undo_buffer[stbte__wrap(pos-4)];1495 pos = stbte__wrap(pos-5);1496 if (n > 255) {1497 float vf = stbte__extract_float(v, v2);1498 s0 = stbte__extract_short(tm->props[y][x][n-256], 0);1499 s1 = stbte__extract_short(tm->props[y][x][n-256], 1);1500 tm->props[y][x][n-256] = vf;1501 } else {1502#ifdef STBTE_ALLOW_LINK1503 s0 = tm->link[y][x].x;1504 s1 = tm->link[y][x].y;1505 stbte__set_link(tm, x,y, v, v2, STBTE__undo_none);1506#endif1507 }1508 // write the redo entry1509 stbte__redo_record_prop(tm, x, y, n-256, s0,s1);1510 // apply the undo entry1511 } else {1512 pos = stbte__wrap(pos-4);1513 // write the redo entry1514 stbte__redo_record(tm, x, y, n, tm->data[y][x][n]);1515 // apply the undo entry1516 tm->data[y][x][n] = (short) v;1517 }1518 }1519 // overwrite undo record with junk1520 tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1521}15221523static int stbte__redo_find_end(stbte_tilemap *tm)1524{1525 // first scan through for the end record1526 int i, pos = stbte__wrap(tm->undo_pos+1);1527 for (i=0; i < tm->redo_len;) {1528 STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1529 if (tm->undo_buffer[pos] == STBTE__redo_record)1530 break;1531 if (tm->undo_buffer[pos] >= 255)1532 pos = stbte__wrap(pos+5), i += 5;1533 else1534 pos = stbte__wrap(pos+4), i += 4;1535 }1536 if (i >= tm->redo_len)1537 return -1; // this should only ever happen if redo buffer is empty1538 return pos;1539}15401541static void stbte__redo(stbte_tilemap *tm)1542{1543 // first scan through for the end record1544 int i, pos, endpos;1545 endpos = stbte__redo_find_end(tm);1546 if (endpos < 0)1547 return;15481549 // we found a complete redo record1550 pos = stbte__wrap(tm->undo_pos+1);1551 1552 // start an undo record1553 stbte__write_undo(tm, STBTE__undo_record);15541555 for (i=0; pos != endpos; i += 4) {1556 int x,y,n,v;1557 n = tm->undo_buffer[pos];1558 y = tm->undo_buffer[stbte__wrap(pos+1)];1559 x = tm->undo_buffer[stbte__wrap(pos+2)];1560 v = tm->undo_buffer[stbte__wrap(pos+3)];1561 if (n >= 255) {1562 int v2 = tm->undo_buffer[stbte__wrap(pos+4)];1563 short s0=0,s1=0;1564 pos = stbte__wrap(pos+5);1565 if (n > 255) {1566 float vf = stbte__extract_float(v, v2);1567 s0 = stbte__extract_short(tm->props[y][x][n-256],0);1568 s1 = stbte__extract_short(tm->props[y][x][n-256],1);1569 tm->props[y][x][n-256] = vf;1570 } else {1571#ifdef STBTE_ALLOW_LINK1572 s0 = tm->link[y][x].x;1573 s1 = tm->link[y][x].y;1574 stbte__set_link(tm, x,y,v,v2, STBTE__undo_none);1575#endif1576 }1577 // don't use stbte__undo_record_prop because it's guarded1578 stbte__write_undo(tm, s1);1579 stbte__write_undo(tm, s0);1580 stbte__write_undo(tm, x);1581 stbte__write_undo(tm, y);1582 stbte__write_undo(tm, n);1583 } else {1584 pos = stbte__wrap(pos+4);1585 // don't use stbte__undo_record because it's guarded1586 stbte__write_undo(tm, tm->data[y][x][n]);1587 stbte__write_undo(tm, x);1588 stbte__write_undo(tm, y);1589 stbte__write_undo(tm, n);1590 tm->data[y][x][n] = (short) v;1591 }1592 }1593 tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1594}15951596// because detecting that undo is available 1597static void stbte__recompute_undo_available(stbte_tilemap *tm)1598{1599 tm->undo_available = (stbte__undo_find_end(tm) >= 0);1600 tm->redo_available = (stbte__redo_find_end(tm) >= 0);1601}16021603static int stbte__undo_available(stbte_tilemap *tm)1604{1605 if (!tm->undo_available_valid)1606 stbte__recompute_undo_available(tm);1607 return tm->undo_available;1608}16091610static int stbte__redo_available(stbte_tilemap *tm)1611{1612 if (!tm->undo_available_valid)1613 stbte__recompute_undo_available(tm);1614 return tm->redo_available;1615}16161617///////////////////////////////////////////////////////////////////////////////////////////////////16181619#ifdef STBTE_ALLOW_LINK1620static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y, int undo_mode)1621{1622 stbte__link *a;1623 STBTE_ASSERT(src_x >= 0 && src_x < STBTE_MAX_TILEMAP_X && src_y >= 0 && src_y < STBTE_MAX_TILEMAP_Y);1624 a = &tm->link[src_y][src_x];1625 // check if it's a do nothing1626 if (a->x == dest_x && a->y == dest_y)1627 return;1628 if (undo_mode != STBTE__undo_none ) {1629 if (undo_mode == STBTE__undo_block) stbte__begin_undo(tm);1630 stbte__undo_record_prop(tm, src_x, src_y, -1, a->x, a->y);1631 if (undo_mode == STBTE__undo_block) stbte__end_undo(tm);1632 }1633 // check if there's an existing link1634 if (a->x >= 0) {1635 // decrement existing link refcount1636 STBTE_ASSERT(tm->linkcount[a->y][a->x] > 0);1637 --tm->linkcount[a->y][a->x];1638 }1639 // increment new dest1640 if (dest_x >= 0) {1641 ++tm->linkcount[dest_y][dest_x];1642 }1643 a->x = dest_x;1644 a->y = dest_y;1645}1646#endif164716481649static void stbte__draw_rect(int x0, int y0, int x1, int y1, unsigned int color)1650{1651 STBTE_DRAW_RECT(x0,y0,x1,y1, color);1652}1653Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1654why, one step back1654#ifdef STBTE_ALLOW_LINKMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1655–1667why, one step back1655static void stbte__draw_line(int x0, int y0, int x1, int y1, unsigned int color)1656{1657 int temp;1658 if (x1 < x0) temp=x0,x0=x1,x1=temp;1659 if (y1 < y0) temp=y0,y0=y1,y1=temp;1660 stbte__draw_rect(x0,y0,x1+1,y1+1,color);1661}16621663static void stbte__draw_link(int x0, int y0, int x1, int y1, unsigned int color)1664{1665 stbte__draw_line(x0,y0,x0,y1, color);1666 stbte__draw_line(x0,y1,x1,y1, color);1667}Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1668why, one step back1668#endifMerge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1669–1817why, one step back16691670static void stbte__draw_frame(int x0, int y0, int x1, int y1, unsigned int color)1671{1672 stbte__draw_rect(x0,y0,x1-1,y0+1,color);1673 stbte__draw_rect(x1-1,y0,x1,y1-1,color);1674 stbte__draw_rect(x0+1,y1-1,x1,y1,color);1675 stbte__draw_rect(x0,y0+1,x0+1,y1,color);1676}16771678static int stbte__get_char_width(int ch)1679{1680 return stbte__fontdata[ch-16];1681}16821683static short *stbte__get_char_bitmap(int ch)1684{1685 return stbte__fontdata + stbte__font_offset[ch-16];1686}16871688static void stbte__draw_bitmask_as_columns(int x, int y, short bitmask, int color)1689{1690 int start_i = -1, i=0;1691 while (bitmask) {1692 if (bitmask & (1<<i)) {1693 if (start_i < 0)1694 start_i = i; 1695 } else if (start_i >= 0) {1696 stbte__draw_rect(x, y+start_i, x+1, y+i, color);1697 start_i = -1;1698 bitmask &= ~((1<<i)-1); // clear all the old bits; we don't clear them as we go to save code1699 }1700 ++i;1701 }1702}17031704static void stbte__draw_bitmap(int x, int y, int w, short *bitmap, int color)1705{1706 int i;1707 for (i=0; i < w; ++i)1708 stbte__draw_bitmask_as_columns(x+i, y, *bitmap++, color);1709}17101711static void stbte__draw_text_core(int x, int y, const char *str, int w, int color, int digitspace)1712{1713 int x_end = x+w;1714 while (*str) {1715 int c = *str++;1716 int cw = stbte__get_char_width(c);1717 if (x + cw > x_end)1718 break;1719 stbte__draw_bitmap(x, y, cw, stbte__get_char_bitmap(c), color);1720 if (digitspace && c == ' ')1721 cw = stbte__get_char_width('0');1722 x += cw+1;1723 }1724}17251726static void stbte__draw_text(int x, int y, const char *str, int w, int color)1727{1728 stbte__draw_text_core(x,y,str,w,color,0);1729}17301731static int stbte__text_width(const char *str)1732{1733 int x = 0;1734 while (*str) {1735 int c = *str++;1736 int cw = stbte__get_char_width(c);1737 x += cw+1;1738 }1739 return x;1740}17411742static void stbte__draw_frame_delayed(int x0, int y0, int x1, int y1, int color)1743{1744 if (stbte__ui.delaycount < STBTE__MAX_DELAYRECT) {1745 stbte__colorrect r = { x0,y0,x1,y1,color };1746 stbte__ui.delayrect[stbte__ui.delaycount++] = r;1747 }1748}17491750static void stbte__flush_delay(void)1751{1752 stbte__colorrect *r;1753 int i;1754 r = stbte__ui.delayrect;1755 for (i=0; i < stbte__ui.delaycount; ++i,++r)1756 stbte__draw_frame(r->x0,r->y0,r->x1,r->y1,r->color);1757 stbte__ui.delaycount = 0;1758}17591760static void stbte__activate(int id)1761{1762 stbte__ui.active_id = id;1763 stbte__ui.active_event = stbte__ui.event;1764 stbte__ui.accum_x = 0;1765 stbte__ui.accum_y = 0;1766}17671768static int stbte__hittest(int x0, int y0, int x1, int y1, int id)1769{1770 int over = stbte__ui.mx >= x0 && stbte__ui.my >= y01771 && stbte__ui.mx < x1 && stbte__ui.my < y1;17721773 if (over && stbte__ui.event >= STBTE__tick)1774 stbte__ui.next_hot_id = id;17751776 return over;1777}17781779static int stbte__button_core(int id)1780{1781 switch (stbte__ui.event) {1782 case STBTE__leftdown:1783 if (stbte__ui.hot_id == id && STBTE__INACTIVE())1784 stbte__activate(id);1785 break;1786 case STBTE__leftup:1787 if (stbte__ui.active_id == id && STBTE__IS_HOT(id)) {1788 stbte__activate(0);1789 return 1;1790 }1791 break;1792 case STBTE__rightdown:1793 if (stbte__ui.hot_id == id && STBTE__INACTIVE())1794 stbte__activate(id);1795 break;1796 case STBTE__rightup:1797 if (stbte__ui.active_id == id && STBTE__IS_HOT(id)) {1798 stbte__activate(0);1799 return -1;1800 }1801 break;1802 }1803 return 0;1804}18051806static void stbte__draw_box(int x0, int y0, int x1, int y1, int colormode, int colorindex)1807{1808 stbte__draw_rect (x0,y0,x1,y1, stbte__color_table[colormode][STBTE__base ][colorindex]);1809 stbte__draw_frame(x0,y0,x1,y1, stbte__color_table[colormode][STBTE__outline][colorindex]);1810}18111812static void stbte__draw_textbox(int x0, int y0, int x1, int y1, char *text, int xoff, int yoff, int colormode, int colorindex)1813{1814 stbte__draw_box(x0,y0,x1,y1,colormode,colorindex);1815 stbte__draw_text(x0+xoff,y0+yoff, text, x1-x0-xoff-1, stbte__color_table[colormode][STBTE__text][colorindex]);1816}1817Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1818why, one step back1818static int stbte__button(int colormode, const char *label, int x, int y, int textoff, int width, int id, int toggled, int disabled)Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1819–1823why, one step back1819{1820 int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1821 int s = STBTE__BUTTON_INTERNAL_SPACING;18221823 if (stbte__ui.event == STBTE__paint)Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1824why, one step back1824 stbte__draw_textbox(x0,y0,x1,y1, (char*) label,s+textoff,s, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1825–1848why, one step back1825 if (disabled)1826 return 0;1827 return (stbte__button_core(id) == 1);1828}18291830static int stbte__button_icon(int colormode, char ch, int x, int y, int width, int id, int toggled, int disabled)1831{1832 int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1833 int s = STBTE__BUTTON_INTERNAL_SPACING;18341835 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 if (stbte__ui.event == STBTE__paint) {Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1849why, one step back1849 char str[2] = { (char)ch, 0 };Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1850–1858why, one step back1850 stbte__draw_textbox(x0,y0,x1,y1, str,1,0,colormode, STBTE__INDEX_FOR_ID(id,0,0));1851 }1852 return stbte__button_core(id);1853}18541855static int stbte__layerbutton(int x, int y, int ch, int id, int toggled, int disabled, int colormode)1856{1857 int x0 = x, y0 = y, x1 = x+10, y1 = y+11;1858 if (stbte__ui.event == STBTE__paint) {Merge branch 'master' into workingSean Barrettbefore adoption5db03ef
line 1859why, one step back1859 char str[2] = { (char)ch, 0 };Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1860–1904why, one step back1860 int off = (9-stbte__get_char_width(ch))/2;1861 stbte__draw_textbox(x0,y0,x1,y1, str, off+1,2, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));1862 }1863 if (disabled)1864 return 0;1865 return stbte__button_core(id);1866}18671868static int stbte__microbutton(int x, int y, int size, int id, int colormode)1869{1870 int x0 = x, y0 = y, x1 = x+size, y1 = y+size;1871 if (stbte__ui.event == STBTE__paint) {1872 stbte__draw_box(x0,y0,x1,y1, colormode, STBTE__INDEX_FOR_ID(id,0,0));1873 }1874 return stbte__button_core(id);1875}18761877static int stbte__microbutton_dragger(int x, int y, int size, int id, int *pos)1878{1879 int x0 = x, y0 = y, x1 = x+size, y1 = y+size;1880 switch (stbte__ui.event) {1881 case STBTE__paint:1882 stbte__draw_box(x0,y0,x1,y1, STBTE__cexpander, STBTE__INDEX_FOR_ID(id,0,0));1883 break;1884 case STBTE__leftdown:1885 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {1886 stbte__activate(id);1887 stbte__ui.sx = stbte__ui.mx - *pos;1888 }1889 break;1890 case STBTE__mousemove:1891 if (STBTE__IS_ACTIVE(id) && stbte__ui.active_event == STBTE__leftdown) {1892 *pos = stbte__ui.mx - stbte__ui.sx; 1893 }1894 break;1895 case STBTE__leftup:1896 if (STBTE__IS_ACTIVE(id))1897 stbte__activate(0);1898 break;1899 default:1900 return stbte__button_core(id);1901 }1902 return 0;1903}1904Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1905why, one step back1905static int stbte__category_button(const char *label, int x, int y, int width, int id, int toggled)Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1906–1910why, one step back1906{1907 int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1908 int s = STBTE__BUTTON_INTERNAL_SPACING;1909 1910 if (stbte__ui.event == STBTE__paint)Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1911why, one step back1911 stbte__draw_textbox(x0,y0,x1,y1, (char*) label, s,s, STBTE__ccategory_button, STBTE__INDEX_FOR_ID(id,0,toggled));Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1912–1958why, one step back19121913 return (stbte__button_core(id) == 1);1914}19151916enum1917{1918 STBTE__none,1919 STBTE__begin,1920 STBTE__end,1921 STBTE__change,1922};19231924// returns -1 if value changes, 1 at end of drag1925static int stbte__slider(int x0, int w, int y, int range, int *value, int id)1926{1927 int x1 = x0+w;1928 int pos = *value * w / (range+1);1929 int event_mouse_move = STBTE__change;1930 switch (stbte__ui.event) {1931 case STBTE__paint:1932 stbte__draw_rect(x0,y,x1,y+1, 0x808080);1933 stbte__draw_rect(x0+pos-1,y-1,x0+pos+2,y+2, 0xffffff);1934 break;1935 case STBTE__leftdown:1936 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {1937 stbte__activate(id);1938 event_mouse_move = STBTE__begin;1939 }1940 // fall through1941 case STBTE__mousemove:1942 if (STBTE__IS_ACTIVE(id)) {1943 int v = (stbte__ui.mx-x0)*(range+1)/w;1944 if (v < 0) v = 0; else if (v > range) v = range;1945 *value = v;1946 return event_mouse_move;1947 }1948 break;1949 case STBTE__leftup:1950 if (STBTE__IS_ACTIVE(id)) {1951 stbte__activate(0);1952 return STBTE__end;1953 }1954 break;1955 }1956 return STBTE__none;1957}1958Merge branch 'master' into workingSean Barrettbefore adoptiond940053
lines 1959–1967why, one step back1959#if defined(_WIN32) && defined(__STDC_WANT_SECURE_LIB__)1960 #define stbte__sprintf sprintf_s1961 #define stbte__sizeof(s) , sizeof(s)1962#else1963 #define stbte__sprintf sprintf1964 #define stbte__sizeof(s) 1965#endif19661967static int stbte__float_control(int x0, int y0, int w, float minv, float maxv, float scale, const char *fmt, float *value, int colormode, int id)Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1968–1973why, one step back1968{1969 int x1 = x0+w;1970 int y1 = y0+11;1971 switch (stbte__ui.event) {1972 case STBTE__paint: {1973 char text[32];Merge branch 'master' into workingSean Barrettbefore adoptiond940053
line 1974why, one step back1974 stbte__sprintf(text stbte__sizeof(text), fmt ? fmt : "%6.2f", *value);Merge pull request #395 from dougallj/patch-1Sean Barrettbefore adoptionf5c9aaa
lines 1975–2252why, one step back1975 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,160 lines.