Skip to content
Myr

properties and links

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

authored by Sean Barrett · committed by Sean Barrett · parent 283bc54 · Browse files · 1 file+735 added−130 removed
This commit

State

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

Diff stats

1 file+735 added−130 removed

  • other:+735 added−130 removed

1 file+735 added−130 removed

modified:stb_tilemap_editor.h+735 added-130 removed
2 unmodified lines
3//4// Embeddable tilemap editor for C/C++5//6//7// REVISION HISTORY8// 0.30 properties9// - 10// 0.20 initial release 11//12// COMPILING13//14// This header file contains both the header file and the15// implementation file in one. To create the implementation,
7 unmodified lines
23// // this must draw a filled rectangle (exclusive on right/bottom)24// // color = (r<<16)|(g<<8)|(b)25// 26// void STBTE_DRAW_TILE(int x0, int y0,22// unsigned short id, int highlight);27// unsigned short id, int highlight, stbte_props *data);28// // this draws the tile image identified by 'id' in one of several24// // highlight modes (see STBTE_drawmode_* in the header section,25// // x0,y0,highlight:int; id:unsigned short29// // highlight modes (see STBTE_drawmode_* in the header section);30// // if 'data' is NULL, it's drawing the tile in a palette; if 'data'31// // is not NULL, it's drawing a tile on the map, and that is the data32// // associated with that map tile33//34// #include "stb_tilemap_editor.h"35//36// Optionally you can define the following functions before the include;37// note these must be macros (which can just call a single function) so31// we can detect if you've defined them:38// this library can #ifdef to detect if you've defined them:39//33// [[ support for these is not implemented yet ]]40// #define STBTE_PROP_TYPE(int n, short *tiledata, float *params) ...41// // Returns the type of the n'th property of a given tile, which42// // controls how it is edited. Legal types are:43// // 0 /* no editable property in this slot */44// // STBTE_PROP_int /* uses a slider to adjust value */45// // STBTE_PROP_float /* uses a weird multi-axis control */46// // STBTE_PROP_bool /* uses a checkbox to change value */47// // And you can bitwise-OR in the following flags:48// // STBTE_PROP_disabled49// // Note that all of these are stored as floats in the param array.50// //51// // Since the tiledata is passed to you, you can choose which property52// // is bound to that slot based on that data.53// //54// // Changing the type of a parameter does not cause the underlying55// // value to be clamped to the type min/max except when the tile is56// // explicitly selected.57// 58// #define STBTE_PROP_NAME(int n, short *tiledata, float *params) ...59// // these return a string with the name for slot #n in the float60// // property list for the tile.61// // (if any), or62// // based on the values of other params. 63//64// #define STBTE_PROP_MIN(int n, short *tiledata) ...your code here...65// #define STBTE_PROP_MAX(int n, short *tiledata) ...your code here...66// // These return the allowable range for the property values for67// // the specified slot. It is never called for boolean types.68//69// #define STBTE_ALLOW_LINK(short *src, float *src_data, \70// short *dest, float *dest_data) ...your code...71// // this returns true or false depending on whether you allow a link72// // to be drawn from a tile 'src' to a tile 'dest'. if you don't73// // define this, linking will not be supported74//75// #define STBTE_LINK_COLOR(short *src, float *src_data, \76// short *dest, float *dest_data) ...your code...77// // return a color encoded as a 24-bit unsigned integer in the78// // form 0xRRGGBB. If you don't define this, default colors will79// // be used.80//81//82// [[ support for those below is not implemented yet ]]83//84// #define STBTE_HITTEST_TILE(x0,y0,id,mx,my) ...your code here...85// // this returns true or false depending on whether the mouse86// // pointer at mx,my is over (touching) a tile of type 'id'87// // displayed at x0,y0. Normally stb_tilemap_editor just does88// // this hittest based on the tile geometry, but if you have89// // tiles whose images extend out of the tile, you'll need this.90//42// #define STBTE_DRAW_ICON(x0,y0,id,highlight) ...your code here...43// // this must draw a tile image identified by 'id' but now the44// // tile image must be drawn to fit in the tile palette, which45// // means it cannot exceed your specified palette spacing.46//91// ADDITIONAL CONFIGURATION92//93// The following symbols set static limits which determine how much94// memory will be allocated for the editor. You can override them
4 unmodified lines
99// #define STBTE_MAX_LAYERS 8 // max 32100// #define STBTE_MAX_CATEGORIES 100101// #define STBTE_UNDO_BUFFER_BYTES (1 << 20) // 1MB102// #define STBTE_MAX_COPY 90000 // e.g. 300x300103// #define STBTE_MAX_PROP 10 // max properties per tile104//105// API106//107// Further documentation appears in the header-file section below.
49 unmodified lines
157 STBTE_drawmode_normal = 0,158 STBTE_drawmode_emphasize = 1,159};160161// these are the property types162#define STBTE_PROP_none 0163#define STBTE_PROP_int 1164#define STBTE_PROP_float 2165#define STBTE_PROP_bool 3166#define STBTE_PROP_disabled 4167168////////169//170// creation171//
134 unmodified lines
306#ifndef STBTE_UNDO_BUFFER_BYTES307#define STBTE_UNDO_BUFFER_BYTES (1 << 20) // 1MB308#endif309310#ifndef STBTE_MAX_PROP311#define STBTE_MAX_PROP 10312#endif313314#ifndef STBTE_PROP_MIN315#define STBTE_PROP_MIN(n,td,tp) 0316#endif317318#ifndef STBTE_PROP_MAX319#define STBTE_PROP_MAX(n,td,tp) 100.0320#endif321322#ifndef STBTE_PROP_FLOAT_SCALE323#define STBTE_PROP_FLOAT_SCALE(n,td,tp) 1 // default scale size324#endif325326#define STBTE__UNDO_BUFFER_COUNT (STBTE_UNDO_BUFFER_BYTES>>1)327328#if STBTE_MAX_TILEMAP_X > 4096 || STBTE_MAX_TILEMAP_Y > 4096329#error "Maximum editable map size is 4096 x 4096"
6 unmodified lines
336#endif337338static int *stbte__colors;339340typedef struct341{342 short x,y;343} stbte__link;344345enum346{347 STBTE__base,348 STBTE__outline,
109 unmodified lines
458#define STBTE_COLOR_SELECTION_OUTLINE2 0x303030459#define STBTE_COLOR_TILEPALETTE_OUTLINE 0xffffff460#define STBTE_COLOR_TILEPALETTE_BACKGROUND 0x000000461462#ifndef STBTE_LINK_COLOR463#define STBTE_LINK_COLOR(src,sp,dest,dp) 0x5030ff464#endif465466#ifndef STBTE_LINK_COLOR_DRAWING467#define STBTE_LINK_COLOR_DRAWING 0xff40ff468#endif469470#ifndef STBTE_LINK_COLOR_DISALLOWED471#define STBTE_LINK_COLOR_DISALLOWED 0x602060472#endif473474475// disabled, selected, down, over476static unsigned char stbte__state_to_index[2][2][2][2] =477{478 {
8 unmodified lines
487#define STBTE__INDEX_FOR_ID(id,disable,select) STBTE__INDEX_FOR_STATE(disable,select,STBTE__IS_ACTIVE(id),STBTE__IS_HOT(id))488489#define STBTE__FONT_HEIGHT 9490static short stbte__font_offset[95+16];405static short stbte__fontdata[762] =491static short stbte__fontdata[769] =492{407 4,4,4,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,493 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,494 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,495 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,410 0,0,0,0,0,0,0,0,0,0,0,0,146,511,146,146,511,146,146,511,146,511,257,341,297,411 341,297,341,257,511,16,56,124,16,16,16,124,56,16,96,144,270,261,262,136,80,412 48,224,192,160,80,40,22,14,15,3,448,496,496,240,232,20,10,5,2,112,232,452,413 450,225,113,58,28,63,30,60,200,455,257,257,0,0,0,257,257,455,120,204,132,414 132,159,14,4,4,14,159,132,132,204,120,8,24,56,120,56,24,8,32,48,56,60,56,415 48,32,0,0,0,0,111,111,7,7,0,0,7,7,34,127,127,34,34,127,127,34,36,46,107,107,416 58,18,99,51,24,12,102,99,48,122,79,93,55,114,80,4,7,3,62,127,99,65,65,99,417 127,62,8,42,62,28,28,62,42,8,8,8,62,62,8,8,128,224,96,8,8,8,8,8,8,96,96,96,418 48,24,12,6,3,62,127,89,77,127,62,64,66,127,127,64,64,98,115,89,77,71,66,33,419 97,73,93,119,35,24,28,22,127,127,16,39,103,69,69,125,57,62,127,73,73,121,420 48,1,1,113,121,15,7,54,127,73,73,127,54,6,79,73,105,63,30,54,54,128,246,118,421 8,28,54,99,65,20,20,20,20,65,99,54,28,8,2,3,105,109,7,2,30,63,33,45,47,46,422 124,126,19,19,126,124,127,127,73,73,127,54,62,127,65,65,99,34,127,127,65,423 99,62,28,127,127,73,73,73,65,127,127,9,9,9,1,62,127,65,73,121,121,127,127,424 8,8,127,127,65,65,127,127,65,65,32,96,64,64,127,63,127,127,8,28,54,99,65,425 127,127,64,64,64,64,127,127,6,12,6,127,127,127,127,6,12,24,127,127,62,127,426 65,65,65,127,62,127,127,9,9,15,6,62,127,65,81,49,127,94,127,127,9,25,127,427 102,70,79,73,73,121,49,1,1,127,127,1,1,63,127,64,64,127,63,15,31,48,96,48,428 31,15,127,127,48,24,48,127,127,99,119,28,28,119,99,7,15,120,120,15,7,97,113,429 89,77,71,67,127,127,65,65,3,6,12,24,48,96,65,65,127,127,8,12,6,3,6,12,8,64,430 64,64,64,64,64,64,3,7,4,32,116,84,84,124,120,127,127,68,68,124,56,56,124,431 68,68,68,56,124,68,68,127,127,56,124,84,84,92,24,8,124,126,10,10,56,380,324,432 324,508,252,127,127,4,4,124,120,72,122,122,64,256,256,256,506,250,126,126,433 16,56,104,64,66,126,126,64,124,124,24,56,28,124,120,124,124,4,4,124,120,56,434 124,68,68,124,56,508,508,68,68,124,56,56,124,68,68,508,508,124,124,4,4,12,435 8,72,92,84,84,116,36,4,4,62,126,68,68,60,124,64,64,124,124,28,60,96,96,60,436 28,28,124,112,56,112,124,28,68,108,56,56,108,68,284,316,352,320,508,252,68,437 100,116,92,76,68,8,62,119,65,65,127,127,65,65,119,62,8,16,24,12,12,24,24,438 12,4,496 0,0,0,0,2,253,130,456,156,8,72,184,64,2,125,66,64,160,64,146,511,146,146,497 511,146,146,511,146,511,257,341,297,341,297,341,257,511,16,56,124,16,16,16,498 124,56,16,96,144,270,261,262,136,80,48,224,192,160,80,40,22,14,15,3,448,496,499 496,240,232,20,10,5,2,112,232,452,450,225,113,58,28,63,30,60,200,455,257,500 257,0,0,0,257,257,455,120,204,132,132,159,14,4,4,14,159,132,132,204,120,8,501 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,502 127,34,34,127,127,34,36,46,107,107,58,18,99,51,24,12,102,99,48,122,79,93,503 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,504 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,505 127,127,64,64,98,115,89,77,71,66,33,97,73,93,119,35,24,28,22,127,127,16,39,506 103,69,69,125,57,62,127,73,73,121,48,1,1,113,121,15,7,54,127,73,73,127,54,507 6,79,73,105,63,30,54,54,128,246,118,8,28,54,99,65,20,20,20,20,65,99,54,28,508 8,2,3,105,109,7,2,30,63,33,45,47,46,124,126,19,19,126,124,127,127,73,73,127,509 54,62,127,65,65,99,34,127,127,65,99,62,28,127,127,73,73,73,65,127,127,9,9,510 9,1,62,127,65,73,121,121,127,127,8,8,127,127,65,65,127,127,65,65,32,96,64,511 64,127,63,127,127,8,28,54,99,65,127,127,64,64,64,64,127,127,6,12,6,127,127,512 127,127,6,12,24,127,127,62,127,65,65,65,127,62,127,127,9,9,15,6,62,127,65,513 81,49,127,94,127,127,9,25,127,102,70,79,73,73,121,49,1,1,127,127,1,1,63,127,514 64,64,127,63,15,31,48,96,48,31,15,127,127,48,24,48,127,127,99,119,28,28,119,515 99,7,15,120,120,15,7,97,113,89,77,71,67,127,127,65,65,3,6,12,24,48,96,65,516 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,517 127,127,68,68,124,56,56,124,68,68,68,56,124,68,68,127,127,56,124,84,84,92,518 24,8,124,126,10,10,56,380,324,324,508,252,127,127,4,4,124,120,72,122,122,519 64,256,256,256,506,250,126,126,16,56,104,64,66,126,126,64,124,124,24,56,28,520 124,120,124,124,4,4,124,120,56,124,68,68,124,56,508,508,68,68,124,56,56,124,521 68,68,508,508,124,124,4,4,12,8,72,92,84,84,116,36,4,4,62,126,68,68,60,124,522 64,64,124,124,28,60,96,96,60,28,28,124,112,56,112,124,28,68,108,56,56,108,523 68,284,316,352,320,508,252,68,100,116,92,76,68,8,62,119,65,65,127,127,65,524 65,119,62,8,16,24,12,12,24,24,12,4,525};526527typedef struct528{
14 unmodified lines
543 STBTE__panel_toolbar,544 STBTE__panel_colorpick,545 STBTE__panel_info,546 STBTE__panel_layers,547 STBTE__panel_props,548 STBTE__panel_categories,549 STBTE__panel_tiles,550551 STBTE__num_panel,
14 unmodified lines
566 STBTE__tool_erase,567 STBTE__tool_rect,568 STBTE__tool_eyedrop,569 STBTE__tool_fill,570 STBTE__tool_link,571484 STBTE__tool_grid,572 STBTE__tool_showgrid,573 STBTE__tool_showlinks,574575 STBTE__tool_undo,576 STBTE__tool_redo,577 // copy/cut/paste aren't included here because they're displayed differently578579 STBTE__num_tool,580};581582// icons are stored in the 0-31 range of ASCII in the font493static int toolchar[] = { 26,24,25,20,23,22, 19,29,28, };583static int toolchar[] = { 26,24,25,20,23,22,18, 19,17, 29,28, };584585enum586{587 STBTE__paint,
21 unmodified lines
609{610 int x0,y0,x1,y1,color;611} stbte__colorrect;612613#define STBTE__MAX_DELAYRECT 256614#define STBTE__MAX_DELAYLINK 4096615616typedef struct617{618 int tool, active_event;619 int active_id, hot_id, next_hot_id;620 int event;528 int mx,my;621 int mx,my, dx,dy;622 int ms_time;623 int shift, scrollkey;624 int initted;625 int side_extended[2];533 stbte__colorrect delayrect[1024];534 int delaycount;535 int show_grid;626 stbte__colorrect delayrect[STBTE__MAX_DELAYRECT];627 stbte__colorrect delaylink[STBTE__MAX_DELAYLINK];628 int delaycount, delaylinkcount;629 int show_grid, show_links;630 int brush_state; // used to decide which kind of erasing631 int eyedrop_x, eyedrop_y, eyedrop_last_layer;632 int pasting, paste_x, paste_y;633 int scrolling, start_x, start_y;634 int last_mouse_x, last_mouse_y;635 int accum_x, accum_y;636 int linking;637 int dragging;638 int drag_x, drag_y, drag_w, drag_h;639 int drag_offx, drag_offy, drag_dest_x, drag_dest_y;640 int undoing;
34 unmodified lines
675676struct stbte_tilemap677{678 stbte__tiledata data[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_LAYERS];679 float props[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X][STBTE_MAX_PROP];680 #ifdef STBTE_ALLOW_LINK681 stbte__link link[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];682 int linkcount[STBTE_MAX_TILEMAP_Y][STBTE_MAX_TILEMAP_X];683 #endif684 int max_x, max_y, num_layers;685 int spacing_x, spacing_y;686 int palette_spacing_x, palette_spacing_y;687 int scroll_x,scroll_y;
1 unmodified line
689 char *categories[STBTE_MAX_CATEGORIES];690 int num_categories, category_scroll;691 stbte__tileinfo *tiles;692 int num_tiles, max_tiles, digits;693 unsigned char undo_available_valid;694 unsigned char undo_available;695 unsigned char redo_available;696 unsigned char padding;697 int cur_palette_count;698 int palette_scroll;699 int tileinfo_dirty;700 stbte__layer layerinfo[STBTE_MAX_LAYERS];
12 unmodified lines
713{714 int i,n;715 stbte__ui.initted = 1;716 // init UI state717 stbte__ui.show_links = 1;718 for (i=0; i < STBTE__num_panel; ++i) {719 stbte__ui.panel[i].expanded = 1; // visible if not autohidden720 stbte__ui.panel[i].delta_height = 0;721 stbte__ui.panel[i].side = STBTE__side_left;
52 unmodified lines
774 tm->undo_pos = 0;775 tm->category_scroll = 0;776 tm->layer_scroll = 0;777 tm->has_layer_names = 0;778 tm->undo_available_valid = 0;779780 for (i=0; i < tm->num_layers; ++i) {781 tm->layerinfo[i].hidden = 0;782 tm->layerinfo[i].locked = STBTE__unlocked;
106 unmodified lines
889 for (i=0; i < STBTE_MAX_TILEMAP_X * STBTE_MAX_TILEMAP_Y; ++i) {890 tm->data[0][i][0] = tm->background_tile;891 for (j=1; j < tm->num_layers; ++j)892 tm->data[0][i][j] = STBTE__NO_TILE;893 for (j=0; j < STBTE_MAX_PROP; ++j)894 tm->props[0][i][j] = 0;895 #ifdef STBTE_ALLOW_LINK896 tm->link[0][i].x = -1;897 tm->link[0][i].y = -1;898 tm->linkcount[0][i] = 0;899 #endif900 }901}902903void stbte_set_tile(stbte_tilemap *tm, int x, int y, int layer, signed short tile)
75 unmodified lines
979//980// 2) end_of_redo_record981// -2:short982//868// 2) tile update983// 3) tile update984// tile_id:short (-1..32767)870// y_coord:short985// x_coord:short986// y_coord:short987// layer:short (0..31)988//989// 4) property update (also used for links)990// value_hi:short991// value_lo:short992// y_coord:short993// x_coord:short994// property:short (256+prop#)995//996// Since we use a circular buffer, we might overwrite the undo storage.997// To detect this, before playing back commands we scan back and see998// if we see an end_of_undo_record before hitting the relevant boundary,999// it's wholly contained.1000//1001// When we read back through, we see them in reverse order, so880// we'll see the layer number first1002// we'll see the layer number or property number first1003//1004// To be clearer about the circular buffer, there are two cases:1005// 1. a single record is larger than the whole buffer.1006// this is caught because the end_of_undo_record will1007// get overwritten.1008// 2. multiple records written are larger than the whole1009// buffer, so some of them have been overwritten by1010// the later ones. this is handled by explicitly tracking1011// the undo length; we never try to parse the data that1012// got overwritten10131014// given two points, compute the length between them1015#define stbte__wrap(pos) ((pos) & (STBTE__UNDO_BUFFER_COUNT-1))1016
7 unmodified lines
1024 tm->undo_buffer[pos] = value;1025 tm->undo_pos = stbte__wrap(pos+1);1026 tm->undo_len += (tm->undo_len < STBTE__UNDO_BUFFER_COUNT-2);1027 tm->redo_len -= (tm->redo_len > 0);1028 tm->undo_available_valid = 0;1029}10301031static void stbte__write_redo(stbte_tilemap *tm, short value)1032{
1 unmodified line
1034 tm->undo_buffer[pos] = value;1035 tm->undo_pos = stbte__wrap(pos-1);1036 tm->redo_len += (tm->redo_len < STBTE__UNDO_BUFFER_COUNT-2);1037 tm->undo_len -= (tm->undo_len > 0);1038 tm->undo_available_valid = 0;1039}10401041static void stbte__begin_undo(stbte_tilemap *tm)1042{
39 unmodified lines
1082 stbte__write_redo(tm, y);1083 stbte__write_redo(tm, i);1084}1085952static void stbte__undo(stbte_tilemap *tm)1086static float stbte__extract_float(short s0, short s1)1087{1088 union { float f; short s[2]; } converter;1089 converter.s[0] = s0;1090 converter.s[1] = s1;1091 return converter.f;1092}10931094static short stbte__extract_short(float f, int slot)1095{1096 union { float f; short s[2]; } converter;1097 converter.f = f;1098 return converter.s[slot];1099}11001101static void stbte__undo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1102{1103 STBTE_ASSERT(stbte__ui.undoing);1104 if (stbte__ui.undoing) {1105 stbte__write_undo(tm, s1);1106 stbte__write_undo(tm, s0);1107 stbte__write_undo(tm, x);1108 stbte__write_undo(tm, y);1109 stbte__write_undo(tm, 256+i);1110 }1111}11121113static void stbte__undo_record_prop_float(stbte_tilemap *tm, int x, int y, int i, float f)1114{1115 stbte__undo_record_prop(tm, x,y,i, stbte__extract_short(f,0), stbte__extract_short(f,1));1116}11171118static void stbte__redo_record_prop(stbte_tilemap *tm, int x, int y, int i, short s0, short s1)1119{1120 stbte__write_redo(tm, s1);1121 stbte__write_redo(tm, s0);1122 stbte__write_redo(tm, x);1123 stbte__write_redo(tm, y);1124 stbte__write_redo(tm, 256+i);1125}112611271128static int stbte__undo_find_end(stbte_tilemap *tm)1129{1130 // first scan through for the end record955 int i, pos = stbte__wrap(tm->undo_pos-1), endpos;956 for (i=0; i < tm->undo_len; i += 4) {1131 int i, pos = stbte__wrap(tm->undo_pos-1);1132 for (i=0; i < tm->undo_len;) {1133 STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1134 if (tm->undo_buffer[pos] == STBTE__undo_record)1135 break;1136 if (tm->undo_buffer[pos] >= 255)1137 pos = stbte__wrap(pos-5), i += 5;1138 else960 pos = stbte__wrap(pos-4);1139 pos = stbte__wrap(pos-4), i += 4;1140 }1141 if (i >= tm->undo_len)1142 return -1;1143 return pos;1144}11451146static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y);1147static void stbte__undo(stbte_tilemap *tm)1148{1149 int i, pos, endpos;1150 endpos = stbte__undo_find_end(tm);1151 if (endpos < 0)1152 return;964 endpos = pos;11531154 // we found a complete undo record1155 pos = stbte__wrap(tm->undo_pos-1);1156
8 unmodified lines
1165 n = tm->undo_buffer[pos];1166 y = tm->undo_buffer[stbte__wrap(pos-1)];1167 x = tm->undo_buffer[stbte__wrap(pos-2)];1168 v = tm->undo_buffer[stbte__wrap(pos-3)];1169 if (n >= 255) {1170 short s0=0,s1=0;1171 int v2 = tm->undo_buffer[stbte__wrap(pos-4)];1172 pos = stbte__wrap(pos-5);1173 if (n > 255) {1174 float vf = stbte__extract_float(v, v2);1175 s0 = stbte__extract_short(tm->props[y][x][n-256], 0);1176 s1 = stbte__extract_short(tm->props[y][x][n-256], 1);1177 tm->props[y][x][n-256] = vf;1178 } else {1179#ifdef STBTE_ALLOW_LINK1180 s0 = tm->link[y][x].x;1181 s1 = tm->link[y][x].y;1182 stbte__set_link(tm, x,y, v, v2);1183#endif1184 }1185 // write the redo entry1186 stbte__redo_record_prop(tm, x, y, n-256, s0,s1);1187 // apply the undo entry1188 } else {981 pos = stbte__wrap(pos-4);982 // write the redo entry983 stbte__redo_record(tm, x, y, n, tm->data[y][x][n]);984 // apply the undo entry985 tm->data[y][x][n] = (short) v;1189 pos = stbte__wrap(pos-4);1190 // write the redo entry1191 stbte__redo_record(tm, x, y, n, tm->data[y][x][n]);1192 // apply the undo entry1193 tm->data[y][x][n] = (short) v;1194 }1195 }1196 // overwrite undo record with junk1197 tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1198}1199991static void stbte__redo(stbte_tilemap *tm)1200static int stbte__redo_find_end(stbte_tilemap *tm)1201{1202 // first scan through for the end record994 int i, pos = stbte__wrap(tm->undo_pos+1), endpos;995 for (i=0; i < tm->redo_len; i += 4) {1203 int i, pos = stbte__wrap(tm->undo_pos+1);1204 for (i=0; i < tm->redo_len;) {1205 STBTE_ASSERT(tm->undo_buffer[pos] != STBTE__undo_junk);1206 if (tm->undo_buffer[pos] == STBTE__redo_record)1207 break;1208 if (tm->undo_buffer[pos] >= 255)1209 pos = stbte__wrap(pos+5), i += 5;1210 else999 pos = stbte__wrap(pos+4);1211 pos = stbte__wrap(pos+4), i += 4;1212 }1213 if (i >= tm->redo_len)1002 return; // this should only ever happen if redo buffer is empty1003 endpos = pos;1214 return -1; // this should only ever happen if redo buffer is empty1215 return pos;1216}12171218static void stbte__redo(stbte_tilemap *tm)1219{1220 // first scan through for the end record1221 int i, pos, endpos;1222 endpos = stbte__redo_find_end(tm);1223 if (endpos < 0)1224 return;12251226 // we found a complete redo record1227 pos = stbte__wrap(tm->undo_pos+1);1228 1229 // start an undo record
4 unmodified lines
1234 n = tm->undo_buffer[pos];1235 y = tm->undo_buffer[stbte__wrap(pos+1)];1236 x = tm->undo_buffer[stbte__wrap(pos+2)];1237 v = tm->undo_buffer[stbte__wrap(pos+3)];1238 if (n >= 255) {1239 int v2 = tm->undo_buffer[stbte__wrap(pos+4)];1240 short s0=0,s1=0;1241 pos = stbte__wrap(pos+5);1242 if (n > 255) {1243 float vf = stbte__extract_float(v, v2);1244 s0 = stbte__extract_short(tm->props[y][x][n-256],0);1245 s1 = stbte__extract_short(tm->props[y][x][n-256],1);1246 tm->props[y][x][n-256] = vf;1247 } else {1248#ifdef STBTE_ALLOW_LINK1249 s0 = tm->link[y][x].x;1250 s1 = tm->link[y][x].y;1251 stbte__set_link(tm, x,y,v,v2);1252#endif1253 }1254 // don't use stbte__undo_record_prop because it's guarded1255 stbte__write_undo(tm, s1);1256 stbte__write_undo(tm, s0);1257 stbte__write_undo(tm, x);1258 stbte__write_undo(tm, y);1259 stbte__write_undo(tm, n);1260 } else {1017 pos = stbte__wrap(pos+4);1018 // don't use stbte__undo_record because it's guarded1019 stbte__write_undo(tm, tm->data[y][x][n]);1020 stbte__write_undo(tm, x);1021 stbte__write_undo(tm, y);1022 stbte__write_undo(tm, n);1023 tm->data[y][x][n] = (short) v;1261 pos = stbte__wrap(pos+4);1262 // don't use stbte__undo_record because it's guarded1263 stbte__write_undo(tm, tm->data[y][x][n]);1264 stbte__write_undo(tm, x);1265 stbte__write_undo(tm, y);1266 stbte__write_undo(tm, n);1267 tm->data[y][x][n] = (short) v;1268 }1269 }1270 tm->undo_buffer[tm->undo_pos] = STBTE__undo_junk;1271}12721273// because detecting that undo is available 1274static void stbte__recompute_undo_available(stbte_tilemap *tm)1275{1276 tm->undo_available = (stbte__undo_find_end(tm) >= 0);1277 tm->redo_available = (stbte__redo_find_end(tm) >= 0);1278}12791280static int stbte__undo_available(stbte_tilemap *tm)1281{1282 if (!tm->undo_available_valid)1283 stbte__recompute_undo_available(tm);1284 return tm->undo_available;1285}12861287static int stbte__redo_available(stbte_tilemap *tm)1288{1289 if (!tm->undo_available_valid)1290 stbte__recompute_undo_available(tm);1291 return tm->undo_available;1292}12931294///////////////////////////////////////////////////////////////////////////////////////////////////12951296#ifdef STBTE_ALLOW_LINK1297static void stbte__set_link(stbte_tilemap *tm, int src_x, int src_y, int dest_x, int dest_y)1298{1299 stbte__link *a;1300 STBTE_ASSERT(src_x >= 0 && src_x < STBTE_MAX_TILEMAP_X && src_y >= 0 && src_y < STBTE_MAX_TILEMAP_Y);1301 a = &tm->link[src_y][src_x];1302 // check if it's a do nothing1303 if (a->x == dest_x && a->y == dest_y)1304 return;1305 // otherwise, undo1306 stbte__begin_undo(tm);1307 stbte__undo_record_prop(tm, src_x, src_y, -1, a->x, a->y);1308 stbte__end_undo(tm);1309 // check if there's an existing link1310 if (a->x >= 0) {1311 // decrement existing link refcount1312 STBTE_ASSERT(tm->linkcount[a->y][a->x] > 0);1313 --tm->linkcount[a->y][a->x];1314 }1315 // increment new dest1316 if (dest_x >= 0) {1317 ++tm->linkcount[dest_y][dest_x];1318 }1319 a->x = dest_x;1320 a->y = dest_y;1321}1322#endif132313241325static void stbte__draw_rect(int x0, int y0, int x1, int y1, unsigned int color)1326{1327 STBTE_DRAW_RECT(x0,y0,x1,y1, color);1328}13291330static void stbte__draw_line(int x0, int y0, int x1, int y1, unsigned int color)1331{1332 int temp;1333 if (x1 < x0) temp=x0,x0=x1,x1=temp;1334 if (y1 < y0) temp=y0,y0=y1,y1=temp;1335 stbte__draw_rect(x0,y0,x1+1,y1+1,color);1336}13371338static void stbte__draw_link(int x0, int y0, int x1, int y1, unsigned int color)1339{1340 stbte__draw_line(x0,y0,x0,y1, color);1341 stbte__draw_line(x0,y1,x1,y1, color);1342}13431344static void stbte__draw_frame(int x0, int y0, int x1, int y1, unsigned int color)1345{1346 stbte__draw_rect(x0,y0,x1-1,y0+1,color);1347 stbte__draw_rect(x1-1,y0,x1,y1-1,color);
72 unmodified lines
1420}14211422static void stbte__draw_frame_delayed(int x0, int y0, int x1, int y1, int color)1423{1113 if (stbte__ui.delaycount < 1024) {1424 if (stbte__ui.delaycount < STBTE__MAX_DELAYRECT) {1425 stbte__colorrect r = { x0,y0,x1,y1,color };1426 stbte__ui.delayrect[stbte__ui.delaycount++] = r;1427 }1428}14291430static void stbte__draw_link_delayed(int x0, int y0, int x1, int y1, int color)1431{1432 if (stbte__ui.delaycount < STBTE__MAX_DELAYRECT) {1433 stbte__colorrect r = { x0,y0,x1,y1,color };1434 stbte__ui.delaylink[stbte__ui.delaylinkcount++] = r;1435 }1436}14371438static void stbte__flush_delay(void)1439{1121 stbte__colorrect *r = stbte__ui.delayrect;1440 stbte__colorrect *r;1441 int i;1442 r = stbte__ui.delayrect;1443 for (i=0; i < stbte__ui.delaycount; ++i,++r)1444 stbte__draw_frame(r->x0,r->y0,r->x1,r->y1,r->color);1445 r = stbte__ui.delaylink;1446 for (i=0; i < stbte__ui.delaylinkcount; ++i,++r)1447 stbte__draw_link(r->x0,r->y0,r->x1,r->y1,r->color);1448 stbte__ui.delaycount = 0;1449 stbte__ui.delaylinkcount = 0;1450}14511452static void stbte__activate(int id)1453{1454 stbte__ui.active_id = id;1455 stbte__ui.active_event = stbte__ui.event;1456 stbte__ui.accum_x = 0;1457 stbte__ui.accum_y = 0;1458}14591460static int stbte__hittest(int x0, int y0, int x1, int y1, int id)1461{
58 unmodified lines
1520 return 0;1521 return (stbte__button_core(id) == 1);1522}15231198static int stbte__button_icon(int colormode, char ch, int x, int y, int width, int id, int toggled)1524static int stbte__button_icon(int colormode, char ch, int x, int y, int width, int id, int toggled, int disabled)1525{1526 int x0=x,y0=y, x1=x+width,y1=y+STBTE__BUTTON_HEIGHT;1527 int s = STBTE__BUTTON_INTERNAL_SPACING;1528
1 unmodified line
1530 1531 if (stbte__ui.event == STBTE__paint) {1532 char label[2] = { ch, 0 };1533 int pad = (9 - stbte__get_char_width(ch))/2;1208 stbte__draw_textbox(x0,y0,x1,y1, label,s+pad,s, colormode, STBTE__INDEX_FOR_ID(id,0,toggled));1534 stbte__draw_textbox(x0,y0,x1,y1, label,s+pad,s, colormode, STBTE__INDEX_FOR_ID(id,disabled,toggled));1535 }1536 if (disabled)1537 return 0;1538 return (stbte__button_core(id) == 1);1539}15401541static int stbte__minibutton(int colormode, int x, int y, int ch, int id)
72 unmodified lines
16141615 return (stbte__button_core(id) == 1);1616}16171618enum1619{1620 STBTE__none,1621 STBTE__begin,1622 STBTE__end,1623 STBTE__change,1624};16251626// returns -1 if value changes, 1 at end of drag1627static int stbte__slider(int x0, int w, int y, int range, int *value, int id)1628{1629 int x1 = x0+w;1630 int pos = *value * w / (range+1);1631 int over = stbte__hittest(x0,y-2,x1,y+3,id);1632 int event_mouse_move = STBTE__change;1633 switch (stbte__ui.event) {1634 case STBTE__paint:1635 stbte__draw_rect(x0,y,x1,y+1, 0x808080);1636 stbte__draw_rect(x0+pos-1,y-1,x0+pos+2,y+2, 0xffffff);1637 break;1638 case STBTE__leftdown:1639 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {1640 stbte__activate(id);1641 event_mouse_move = STBTE__begin;1642 }1643 // fall through1644 case STBTE__mousemove:1645 if (STBTE__IS_ACTIVE(id)) {1646 int v = (stbte__ui.mx-x0)*(range+1)/w;1647 if (v < 0) v = 0; else if (v > range) v = range;1648 *value = v;1649 return event_mouse_move;1650 }1651 break;1652 case STBTE__leftup:1653 if (STBTE__IS_ACTIVE(id)) {1654 stbte__activate(0);1655 return STBTE__end;1656 }1657 break;1658 }1659 return STBTE__none;1660}16611662static int stbte__float_control(int x0, int y0, int w, float minv, float maxv, float scale, char *fmt, float *value, int colormode, int id)1663{1664 int x1 = x0+w;1665 int y1 = y0+11;1666 int over = stbte__hittest(x0,y0,x1,y1,id);1667 switch (stbte__ui.event) {1668 case STBTE__paint: {1669 char text[32];1670 sprintf(text, fmt ? fmt : "%6.2f", *value);1671 stbte__draw_textbox(x0,y0,x1,y1, text, 1,2, colormode, STBTE__INDEX_FOR_ID(id,0,0));1672 break;1673 }1674 case STBTE__leftdown:1675 case STBTE__rightdown:1676 if (STBTE__IS_HOT(id) && STBTE__INACTIVE())1677 stbte__activate(id);1678 return STBTE__begin;1679 break;1680 case STBTE__leftup:1681 case STBTE__rightup:1682 if (STBTE__IS_ACTIVE(id)) {1683 stbte__activate(0);1684 return STBTE__end;1685 }1686 break;1687 case STBTE__mousemove:1688 if (STBTE__IS_ACTIVE(id)) {1689 float v = *value, delta;1690 int ax = stbte__ui.accum_x/4;1691 int ay = stbte__ui.accum_y/4;1692 stbte__ui.accum_x -= ax*4;1693 stbte__ui.accum_y -= ay*4;1694 if (stbte__ui.shift) {1695 if (stbte__ui.active_event == STBTE__leftdown)1696 delta = ax * 1 + ay / 16.0;1697 else1698 delta = ax / 64.0 + ay / 1024.0;1699 } else {1700 if (stbte__ui.active_event == STBTE__leftdown)1701 delta = ax + ay * 0.1;1702 else1703 delta = ax * 0.01 + ay * 0.001;1704 }1705 v += delta * scale;1706 if (v < minv) v = minv;1707 if (v > maxv) v = maxv;1708 *value = v;1709 return STBTE__change;1710 }1711 break;1712 }1713 return STBTE__none;1714}17151716static void stbte__scrollbar(int x, int y0, int y1, int *val, int v0, int v1, int num_vis, int id)1717{1718 int over;1719 int thumbpos;
44 unmodified lines
1764 else1765 tm->digits = 2;1766}17671768static int stbte__is_single_selection(void)1769{1770 return stbte__ui.has_selection1771 && stbte__ui.select_x0 == stbte__ui.select_x11772 && stbte__ui.select_y0 == stbte__ui.select_y1;1773}17741775typedef struct1776{1777 int width, height;1778 int x,y;1779 int active;1780 float retracted;1781} stbte__region_t;17821350stbte__region_t stbte__region[4];1783static stbte__region_t stbte__region[4];17841785#define STBTE__TOOLBAR_ICON_SIZE (9+2*2)1786#define STBTE__TOOLBAR_PASTE_SIZE (34+2*2)1787
4 unmodified lines
1792{1793 int i, limit, w, k;1794 int window_width = stbte__ui.x1 - stbte__ui.x0;1795 int window_height = stbte__ui.y1 - stbte__ui.y0;1363 int min_width[STBTE__num_panel]={0,0,0,0,0,0};1364 int height[STBTE__num_panel]={0,0,0,0,0,0};1365 int panel_active[STBTE__num_panel]={1,0,1,1,1,1};1796 int min_width[STBTE__num_panel]={0,0,0,0,0,0,0};1797 int height[STBTE__num_panel]={0,0,0,0,0,0,0};1798 int panel_active[STBTE__num_panel]={1,0,1,1,1,1,1};1799 int vpos[4] = { 0,0,0,0 };1800 stbte__panel *p = stbte__ui.panel;1801 stbte__panel *pt = &p[STBTE__panel_toolbar];1802
11 unmodified lines
1814 panel_active[STBTE__panel_layers ] = tm->num_layers > 1;1815#ifdef STBTE__COLORPICKER1816 panel_active[STBTE__panel_colorpick ] = 1;1817#endif1818 panel_active[STBTE__panel_props ] = stbte__is_single_selection();18191820 // compute minimum widths for each panel (assuming they're on sides not top)1821 min_width[STBTE__panel_info ] = 8 + 11 + 7*tm->digits+17+7; // estimate min width of "w:0000"1822 min_width[STBTE__panel_colorpick ] = 120;1823 min_width[STBTE__panel_tiles ] = 4 + tm->palette_spacing_x + 5; // 5 for scrollbar1824 min_width[STBTE__panel_categories] = 4 + 42 + 5; // 42 is enough to show ~7 chars; 5 for scrollbar1825 min_width[STBTE__panel_layers ] = 4 + 54 + 30*tm->has_layer_names; // 2 digits plus 3 buttons plus scrollbar1826 min_width[STBTE__panel_toolbar ] = 4 + STBTE__TOOLBAR_PASTE_SIZE; // wide enough for 'Paste' button1827 min_width[STBTE__panel_props ] = 80; // narrowest info panel18281829 // compute minimum widths for left & right panels based on the above1830 stbte__region[0].width = stbte__ui.left_width;1831 stbte__region[1].width = stbte__ui.right_width;
55 unmodified lines
1887 k = (stbte__region[p[STBTE__panel_tiles].side].width - 8) / tm->palette_spacing_x;1888 if (k == 0) k = 1;1889 height[STBTE__panel_tiles] = ((tm->num_tiles+k-1)/k) * tm->palette_spacing_y + 8;18901891 // properties panel1892 height[STBTE__panel_props] = 9 + STBTE_MAX_PROP*14;18931894 // now compute the locations of all the panels1895 for (i=0; i < STBTE__num_panel; ++i) {1896 if (panel_active[i]) {1897 int side = p[i].side;
36 unmodified lines
1934 STBTE__panel_mover, // p1 is panel ID, p2 is destination side1935 STBTE__panel_sizer, // param panel ID1936 STBTE__scrollbar_id,1937 STBTE__colorpick_id,1938 STBTE__prop_flag,1939 STBTE__prop_float,1940 STBTE__prop_int,1941};19421943// id is: [ 24-bit data : 7-bit identifer ]1944// map id is: [ 12-bit y : 12 bit x : 7-bit identifier ]
493 unmodified lines
2438 }2439 stbte__paste_stack(tm, data, data, tm->data[stbte__ui.drag_y+oy][stbte__ui.drag_x+ox], !stbte__ui.shift);2440 }2441 if (data) {2001 for (i=0; i < tm->num_layers; ++i)2442 for (i=0; i < tm->num_layers; ++i) {2443 if (tm->data[mapy][mapx][i] != data[i]) {2444 stbte__undo_record(tm, mapx, mapy, i, tm->data[mapy][mapx][i]);2445 tm->data[mapy][mapx][i] = data[i];2446 }2447 }2448 }2449}24502451static void stbte__drag_place(stbte_tilemap *tm, int mapx, int mapy)
25 unmodified lines
2477 stbte__ui.select_x1 = stbte__ui.select_x0 + stbte__ui.drag_w;2478 stbte__ui.select_y1 = stbte__ui.select_y0 + stbte__ui.drag_h;2479}248020392481static void stbte__tile(stbte_tilemap *tm, int sx, int sy, int mapx, int mapy)2482{2483 int tool = stbte__ui.tool;2484 int i;
66 unmodified lines
2551 }2552 }2553 }25542555 {2114 for (i=0; i < tm->num_layers; ++i) {2115 if (i == tm->solo_layer || (!tm->layerinfo[i].hidden && tm->solo_layer < 0))2116 if (data[i] >= 0)2117 STBTE_DRAW_TILE(x0,y0, (unsigned short) data[i], 0);2118 if (i == 0 && stbte__ui.show_grid==1)2119 stbte__draw_halfframe(x0,y0,x0+tm->spacing_x, y0+tm->spacing_y, STBTE_COLOR_GRID);2556 for (i=0; i < tm->num_layers; ++i) {2557 if (i == tm->solo_layer || (!tm->layerinfo[i].hidden && tm->solo_layer < 0))2558 if (data[i] >= 0)2559 STBTE_DRAW_TILE(x0,y0, (unsigned short) data[i], 0, tm->props[mapy][mapx]);2560 if (i == 0 && stbte__ui.show_grid==1)2561 stbte__draw_halfframe(x0,y0,x0+tm->spacing_x, y0+tm->spacing_y, STBTE_COLOR_GRID);2562 }2563 }2564 if (stbte__ui.pasting || stbte__ui.dragging || stbte__ui.scrolling)2565 break;2566 if (stbte__ui.scrollkey && !STBTE__IS_MAP_ACTIVE())
16 unmodified lines
2583 }2584 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {2585 stbte__draw_frame_delayed(x0-1,y0-1,x1+1,y1+1, STBTE_COLOR_TILEMAP_HIGHLIGHT);2586 }2587#ifdef STBTE_ALLOW_LINK2588 if (stbte__ui.show_links && tm->link[mapy][mapx].x >= 0) {2589 int tx = tm->link[mapy][mapx].x;2590 int ty = tm->link[mapy][mapx].y;2591 int lx0,ly0,lx1,ly1;2592 lx0 = x0 + (tm->spacing_x >> 1) - 1;2593 ly0 = y0 + (tm->spacing_y >> 1) - 1;2594 lx1 = lx0 + (tx - mapx) * tm->spacing_x + 2;2595 ly1 = ly0 + (ty - mapy) * tm->spacing_y + 2;2596 stbte__draw_link_delayed(lx0,ly0,lx1,ly1,2597 STBTE_LINK_COLOR(tm->data[mapy][mapx], tm->props[mapy][mapx],2598 tm->data[ty ][tx ], tm->props[ty ][tx]));2599 }2600#endif2601 break;2602 }2603 }2604
83 unmodified lines
2688 break;2689 }2690 break;26912692#ifdef STBTE_ALLOW_LINK2693 case STBTE__tool_link:2694 switch (stbte__ui.event) {2695 case STBTE__leftdown:2696 if (STBTE__IS_HOT(id) && STBTE__INACTIVE()) {2697 stbte__activate(id);2698 stbte__ui.linking = 1;2699 stbte__ui.sx = mapx;2700 stbte__ui.sy = mapy;2701 // @TODO: undo2702 }2703 break;2704 case STBTE__leftup:2705 if (STBTE__IS_HOT(id) && STBTE__IS_MAP_ACTIVE()) {2706 if ((mapx != stbte__ui.sx || mapy != stbte__ui.sy) &&2707 STBTE_ALLOW_LINK(tm->data[stbte__ui.sy][stbte__ui.sx], tm->props[stbte__ui.sy][stbte__ui.sx],2708 tm->data[mapy][mapx], tm->props[mapy][mapx]))2709 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, mapx, mapy);2710 else2711 stbte__set_link(tm, stbte__ui.sx, stbte__ui.sy, -1,-1);2712 stbte__ui.linking = 0;2713 stbte__activate(0);2714 }2715 break;27162717 case STBTE__rightdown:2718 if (STBTE__IS_ACTIVE(id)) {2719 stbte__activate(0);2720 stbte__ui.linking = 0;2721 }2722 break;2723 }2724 break;2725#endif27262727 case STBTE__tool_erase:2728 switch (stbte__ui.event) {2729 case STBTE__mousemove:2730 if (STBTE__IS_MAP_ACTIVE() && over)
98 unmodified lines
28292830static void stbte__toolbar(stbte_tilemap *tm, int x0, int y0, int w, int h)2831{2832 int i;2341 int estimated_width = 13 * STBTE__num_tool + 8+8+ 120+4;2833 int estimated_width = 13 * STBTE__num_tool + 8+8+ 120+4 - 30;2834 int x = x0 + w/2 - estimated_width/2;2835 int y = y0+1;28362837 for (i=0; i < STBTE__num_tool; ++i) {2346 int highlight=0;2838 int highlight=0, disable=0;2839 highlight = (stbte__ui.tool == i);2348 if (i == STBTE__tool_undo || i == STBTE__tool_grid)2840 if (i == STBTE__tool_undo || i == STBTE__tool_showgrid)2841 x += 8;2350 if (i == STBTE__tool_grid && stbte__ui.show_grid)2351 highlight=1;2842 if (i == STBTE__tool_showgrid && stbte__ui.show_grid)2843 highlight = 1;2844 if (i == STBTE__tool_showlinks && stbte__ui.show_links)2845 highlight = 1;2846 if (i == STBTE__tool_fill)2847 continue;2848 #ifndef STBTE_ALLOW_LINK2849 if (i == STBTE__tool_link || i == STBTE__tool_showlinks)2850 disable = 1;2851 #endif2852 if (i == STBTE__tool_undo && !stbte__undo_available(tm))2853 disable = 1;2854 if (i == STBTE__tool_redo && !stbte__redo_available(tm))2855 disable = 1;2354 if (stbte__button_icon(STBTE__ctoolbar_button, toolchar[i], x, y, 13, STBTE__ID(STBTE__toolbarA, i), highlight)) {2856 if (stbte__button_icon(STBTE__ctoolbar_button, toolchar[i], x, y, 13, STBTE__ID(STBTE__toolbarA, i), highlight, disable)) {2857 switch (i) {2858 case STBTE__tool_eyedrop:2859 stbte__ui.eyedrop_last_layer = tm->num_layers; // flush eyedropper state2860 // fallthrough2861 default:2862 stbte__ui.tool = i;2863 stbte__ui.has_selection = 0;2864 break;2363 case STBTE__tool_grid:2865 case STBTE__tool_showlinks:2866 stbte__ui.show_links = !stbte__ui.show_links;2867 break;2868 case STBTE__tool_showgrid:2869 stbte__ui.show_grid = (stbte__ui.show_grid+1)%3;2870 break;2871 case STBTE__tool_undo:2872 stbte__undo(tm);
74 unmodified lines
2947 y += 15;2948 x = x0+2;2949 stbte__draw_text(x,y,"brush:",40,STBTE__TEXTCOLOR(STBTE__cpanel));2950 if (tm->cur_tile >= 0)2446 STBTE_DRAW_TILE(x+43,y-3,tm->tiles[tm->cur_tile].id,1);2951 STBTE_DRAW_TILE(x+43,y-3,tm->tiles[tm->cur_tile].id,1,0);2952}29532954static void stbte__layers(stbte_tilemap *tm, int x0, int y0, int w, int h)2955{
69 unmodified lines
3025 int over = stbte__hittest(x0,y0,x1,y1, id);3026 switch (stbte__ui.event) {3027 case STBTE__paint:3028 stbte__draw_rect(x,y,x+tm->palette_spacing_x-1,y+tm->palette_spacing_x-1, STBTE_COLOR_TILEPALETTE_BACKGROUND);2524 STBTE_DRAW_TILE(x,y,t->id, slot == tm->cur_tile);3029 STBTE_DRAW_TILE(x,y,t->id, slot == tm->cur_tile,0);3030 if (slot == tm->cur_tile)3031 stbte__draw_frame_delayed(x-1,y-1,x+tm->palette_spacing_x,y+tm->palette_spacing_y, STBTE_COLOR_TILEPALETTE_OUTLINE);3032 break;3033 default:
44 unmodified lines
3078 stbte__flush_delay();3079 stbte__scrollbar(x1-4, y0+6, y1-2, &tm->palette_scroll, 0, num_total_rows, num_vis_rows, STBTE__ID(STBTE__scrollbar_id, STBTE__palette));3080}30812577static int stbte__cp_mode, stbte__cp_aspect, stbte__cp_state, stbte__cp_index, stbte__save, stbte__cp_altered, stbte__color_copy;3082static float stbte__linear_remap(float n, float x0, float x1, float y0, float y1)3083{3084 return (n-x0)/(x1-x0)*(y1-y0) + y0;3085}30863087static float stbte__saved;3088static void stbte__props_panel(stbte_tilemap *tm, int x0, int y0, int w, int h)3089{3090 int x1 = x0+w, y1 = y0+h;3091 int i;3092 int y = y0 + 5, x = x0+2;3093 int slider_width = 60;3094 int mx,my;3095 float *p;3096 short *data;3097 if (!stbte__is_single_selection())3098 return;3099 mx = stbte__ui.select_x0;3100 my = stbte__ui.select_y0;3101 p = tm->props[my][mx];3102 data = tm->data[my][mx];3103 for (i=0; i < STBTE_MAX_PROP; ++i) {3104 unsigned int n = STBTE_PROP_TYPE(i, data, p);3105 if (n) {3106 char *s = STBTE_PROP_NAME(i, data, p);3107 if (s == NULL) s = "";3108 switch (n & 3) {3109 case STBTE_PROP_bool: {3110 int flag = (int) p[i];3111 if (stbte__layerbutton(x,y, flag ? 'x' : ' ', STBTE__ID(STBTE__prop_flag,i), flag, 0, 2)) {3112 stbte__begin_undo(tm);3113 stbte__undo_record_prop_float(tm,mx,my,i,flag);3114 p[i] = !flag;3115 stbte__end_undo(tm);3116 }3117 stbte__draw_text(x+13,y+1,s,x1-(x+13)-2,STBTE__TEXTCOLOR(STBTE__cpanel));3118 y += 13;3119 break;3120 }3121 case STBTE_PROP_int: {3122 int a = (int) STBTE_PROP_MIN(i,data,p);3123 int b = (int) STBTE_PROP_MAX(i,data,p);3124 int v = (int) p[i] - a;3125 if (a+v != p[i] || v < 0 || v > b-a) {3126 if (v < 0) v = 0;3127 if (v > b-a) v = b-a;3128 p[i] = a+v; // @TODO undo3129 }3130 switch (stbte__slider(x, slider_width, y+7, b-a, &v, STBTE__ID(STBTE__prop_int,i)))3131 {3132 case STBTE__begin:3133 stbte__saved = p[i];3134 // fallthrough3135 case STBTE__change:3136 p[i] = a+v; // @TODO undo3137 break;3138 case STBTE__end:3139 if (p[i] != stbte__saved) {3140 stbte__begin_undo(tm);3141 stbte__undo_record_prop_float(tm,mx,my,i,stbte__saved);3142 stbte__end_undo(tm);3143 }3144 break;3145 }3146 stbte__draw_text(x+slider_width+2,y+2, s, x1-1-(x+slider_width+2), STBTE__TEXTCOLOR(STBTE__cpanel));3147 y += 12;3148 break;3149 }3150 case STBTE_PROP_float: {3151 float a = (float) STBTE_PROP_MIN(i, data,p);3152 float b = (float) STBTE_PROP_MAX(i, data,p);3153 float c = STBTE_PROP_FLOAT_SCALE(i, data, p);3154 float old;3155 if (p[i] < a || p[i] > b) {3156 // @TODO undo3157 if (p[i] < a) p[i] = a;3158 if (p[i] > b) p[i] = b;3159 }3160 old = p[i];3161 switch (stbte__float_control(x, y, 50, a, b, c, "%8.4f", &p[i], STBTE__layer,STBTE__ID(STBTE__prop_float,i))) {3162 case STBTE__begin:3163 stbte__saved = old;3164 break;3165 case STBTE__end:3166 if (stbte__saved != p[i]) {3167 stbte__begin_undo(tm);3168 stbte__undo_record_prop_float(tm,mx,my,i, stbte__saved);3169 stbte__end_undo(tm);3170 }3171 break;3172 }3173 stbte__draw_text(x+53,y+1, s, x1-1-(x+53), STBTE__TEXTCOLOR(STBTE__cpanel));3174 y += 12;3175 break;3176 }3177 }3178 }3179 }3180}31813182static int stbte__cp_mode, stbte__cp_aspect, stbte__cp_state, stbte__cp_index, stbte__save, stbte__cp_altered, stbte__color_copy;3183#ifdef STBTE__COLORPICKER3184static void stbte__dump_colorstate(void)3185{3186 int i,j,k;
15 unmodified lines
3202 }3203 printf("};\n");3204}32052602static void stbte__slider(int x0, int w, int y, int *value, int id)2603{2604 int x1 = x0+w;2605 int pos = *value * w / 256;2606 int over = stbte__hittest(x0,y-2,x1,y+3,id);2607 switch (stbte__ui.event) {2608 case STBTE__paint:2609 stbte__draw_rect(x0,y,x1,y+1, 0x808080);2610 stbte__draw_rect(x0+pos-1,y-1,x0+pos+2,y+2, 0xffffff);2611 break;2612 case STBTE__leftdown:2613 if (STBTE__IS_HOT(id) && STBTE__INACTIVE())2614 stbte__activate(id);2615 // fall through2616 case STBTE__mousemove:2617 if (STBTE__IS_ACTIVE(id)) {2618 int v = (stbte__ui.mx-x0)*256/w;2619 if (v < 0) v = 0; else if (v > 255) v = 255;2620 *value = v;2621 }2622 break;2623 case STBTE__leftup:2624 if (STBTE__IS_ACTIVE(id)) {2625 stbte__activate(0);2626 stbte__dump_colorstate();2627 }2628 break;2629 }2630}26313206static void stbte__colorpicker(int x0, int y0, int w, int h)3207{3208 int x1 = x0+w, y1 = y0+h, x,y, i;3209
15 unmodified lines
3225 color = stbte__color_copy;32263227 rgb[0] = color >> 16; rgb[1] = (color>>8)&255; rgb[2] = color & 255;3228 for (i=0; i < 3; ++i) {2655 stbte__slider(x+8,64, y, rgb+i, STBTE__ID2(STBTE__colorpick_id,3,i));3229 if (stbte__slider(x+8,64, y, 255, rgb+i, STBTE__ID2(STBTE__colorpick_id,3,i)) > 0)3230 stbte__dump_colorstate();3231 y += 15;3232 }3233 if (stbte__ui.event != STBTE__paint && stbte__ui.event != STBTE__tick)3234 stbte__color_table[stbte__cp_mode][stbte__cp_aspect][stbte__cp_index] = (rgb[0]<<16)|(rgb[1]<<8)|(rgb[2]);
104 unmodified lines
3339 x1 = stbte__ui.x0 + (stbte__ui.select_x1 + 1) * tm->spacing_x - tm->scroll_x + 1;3340 y1 = stbte__ui.y0 + (stbte__ui.select_y1 + 1) * tm->spacing_y - tm->scroll_y + 1;3341 stbte__draw_frame(x0,y0,x1,y1, (stbte__ui.ms_time & 256 ? STBTE_COLOR_SELECTION_OUTLINE1 : STBTE_COLOR_SELECTION_OUTLINE2));3342 }33433344 stbte__flush_delay(); // draw a dynamic link on top of the queued links33453346 #ifdef STBTE_ALLOW_LINK3347 if (stbte__ui.linking && STBTE__IS_MAP_HOT()) {3348 int x0,y0,x1,y1;3349 int color;3350 int ex = ((stbte__ui.hot_id >> 19) & 4095);3351 int ey = ((stbte__ui.hot_id >> 7) & 4095);3352 x0 = stbte__ui.x0 + (stbte__ui.sx ) * tm->spacing_x - tm->scroll_x + (tm->spacing_x>>1)+1;3353 y0 = stbte__ui.y0 + (stbte__ui.sy ) * tm->spacing_y - tm->scroll_y + (tm->spacing_y>>1)+1;3354 x1 = stbte__ui.x0 + (ex ) * tm->spacing_x - tm->scroll_x + (tm->spacing_x>>1)-1;3355 y1 = stbte__ui.y0 + (ey ) * tm->spacing_y - tm->scroll_y + (tm->spacing_y>>1)-1;3356 if (STBTE_ALLOW_LINK(tm->data[stbte__ui.sy][stbte__ui.sx], tm->props[stbte__ui.sy][stbte__ui.sx], tm->data[ey][ex], tm->props[ey][ex]))3357 color = STBTE_LINK_COLOR_DRAWING;3358 else3359 color = STBTE_LINK_COLOR_DISALLOWED;3360 stbte__draw_link(x0,y0,x1,y1, color);3361 }3362 #endif3363 }3364 stbte__flush_delay();33653366 // step 2: traverse the panels
29 unmodified lines
3396 if (stbte__ui.event == STBTE__paint && p->side == stbte__ui.panel[STBTE__panel_categories].side)3397 stbte__draw_rect(p->x0+1,p->y0-1,p->x0+p->width-1,p->y0+1, stbte__color_table[STBTE__cpanel][STBTE__base][STBTE__idle]);3398 stbte__palette_of_tiles(tm,p->x0,p->y0,p->width,p->height);3399 break;3400 case STBTE__panel_props:3401 stbte__props_panel(tm,p->x0,p->y0,p->width,p->height);3402 break;3403 }3404 // draw the panel side selectors3405 for (j=0; j < 2; ++j) {3406 int result;
80 unmodified lines
3487 if (stbte__ui.undoing)3488 stbte__end_undo(tm);3489 stbte__ui.scrolling = 0;3490 stbte__ui.dragging = 0;3491 stbte__ui.linking = 0;3492 }3493 }3494 }3495
19 unmodified lines
3515{3516 stbte__ui.event = event;3517 stbte__ui.mx = x;3518 stbte__ui.my = y;3519 stbte__ui.dx = x - stbte__ui.last_mouse_x;3520 stbte__ui.dy = y - stbte__ui.last_mouse_y;3521 stbte__ui.last_mouse_x = x;3522 stbte__ui.last_mouse_y = y;3523 stbte__ui.accum_x += stbte__ui.dx;3524 stbte__ui.accum_y += stbte__ui.dy;3525}35263527void stbte_draw(stbte_tilemap *tm)3528{
68 unmodified lines