eco2d/code/game/source/editors/texed.c

256 lines
6.9 KiB
C
Raw Normal View History

2021-05-15 11:43:29 +00:00
#define ZPL_NO_WINDOWS_H
#include "zpl.h"
2021-05-14 05:59:33 +00:00
#include "editors/texed.h"
#include "raylib.h"
2021-05-15 11:43:29 +00:00
#include "utils/raylib_helpers.h"
#include "cwpack/cwpack.h"
2021-05-14 05:59:33 +00:00
#define RAYGUI_IMPLEMENTATION
#define RAYGUI_SUPPORT_ICONS
#include "raygui.h"
2021-05-15 11:43:29 +00:00
#define GUI_FILE_DIALOG_IMPLEMENTATION
#include "gui_file_dialog.h"
2021-05-15 16:41:30 +00:00
#define GUI_TEXTBOX_EXTENDED_IMPLEMENTATION
#include "gui_textbox_extended.h"
2021-05-14 05:59:33 +00:00
static uint16_t screenWidth = 1280;
static uint16_t screenHeight = 720;
2021-05-15 11:43:29 +00:00
static float zoom = 4.0f;
2021-05-15 18:00:57 +00:00
static Texture2D checker_tex;
static uint16_t old_screen_w;
static uint16_t old_screen_h;
2021-05-15 20:09:25 +00:00
static bool is_loading_prj = false;
2021-05-15 11:43:29 +00:00
#define TD_DEFAULT_IMG_WIDTH 64
#define TD_DEFAULT_IMG_HEIGHT 64
#define TD_UI_PADDING 5.0f
#define TD_UI_PREVIEW_BORDER 4.0f
typedef enum {
TPARAM_FLOAT,
2021-05-15 16:41:30 +00:00
TPARAM_COORD,
2021-05-15 11:43:29 +00:00
TPARAM_INT,
TPARAM_COLOR,
TPARAM_STRING,
TPARAM_FORCE_UINT8 = UINT8_MAX
} td_param_kind;
typedef struct {
td_param_kind kind;
2021-05-15 14:25:18 +00:00
char const *name;
char str[1000];
bool edit_mode;
2021-05-15 11:43:29 +00:00
union {
2021-05-15 13:23:04 +00:00
float flt;
2021-05-15 15:19:50 +00:00
uint32_t u32;
int32_t i32;
2021-05-15 11:43:29 +00:00
Color color;
2021-05-15 14:25:18 +00:00
char copy[4];
2021-05-15 11:43:29 +00:00
};
} td_param;
typedef enum {
2021-05-15 17:17:47 +00:00
TOP_NEW_IMAGE,
2021-05-15 11:43:29 +00:00
TOP_DRAW_RECT,
TOP_DRAW_LINE,
2021-05-15 15:19:50 +00:00
TOP_DITHER,
TOP_LOAD_IMAGE,
2021-05-15 16:41:30 +00:00
TOP_DRAW_TEXT,
2021-05-15 17:40:27 +00:00
TOP_RESIZE_IMAGE,
2021-05-15 11:43:29 +00:00
TOP_FORCE_UINT8 = UINT8_MAX
} td_op_kind;
typedef struct {
td_op_kind kind;
char const *name;
bool is_hidden;
uint8_t num_params;
td_param *params;
} td_op;
#define OP(n) .kind = n, .name = #n
typedef struct {
char *filepath;
Image img;
Texture2D tex;
GuiFileDialogState fileDialog;
td_op *ops; //< zpl_array
2021-05-15 14:25:18 +00:00
int selected_op;
2021-05-15 11:43:29 +00:00
} td_ctx;
static td_ctx ctx = {0};
static char filename[200];
2021-05-15 15:19:50 +00:00
#include "texed_ops_list.c"
2021-05-15 11:43:29 +00:00
void texed_new(int32_t w, int32_t h);
void texed_destroy(void);
void texed_load(void);
void texed_save(void);
void texed_repaint_preview(void);
void texed_process_ops(void);
2021-05-15 13:23:04 +00:00
void texed_process_params(void);
2021-05-15 11:43:29 +00:00
void texed_add_op(int idx);
void texed_rem_op(int idx);
void texed_swp_op(int idx, int idx2);
void texed_draw_oplist_pane(zpl_aabb2 r);
2021-05-15 14:25:18 +00:00
void texed_draw_props_pane(zpl_aabb2 r);
2021-05-15 11:43:29 +00:00
void texed_draw_topbar(zpl_aabb2 r);
static inline
void DrawAABB(zpl_aabb2 rect, Color color) {
DrawRectangleEco(rect.min.x, rect.min.y,
rect.max.x-rect.min.x,
rect.max.y-rect.min.y,
color);
}
static inline
2021-05-15 15:19:50 +00:00
Rectangle aabb2_ray(zpl_aabb2 r) {
return (Rectangle) {
.x = r.min.x,
.y = r.min.y,
.width = r.max.x-r.min.x,
.height = r.max.y-r.min.y
};
}
#include "texed_ops.c"
#include "texed_prj.c"
#include "texed_widgets.c"
2021-05-14 05:59:33 +00:00
void texed_run(void) {
InitWindow(screenWidth, screenHeight, "eco2d - texture editor");
2021-05-15 18:00:57 +00:00
SetWindowState(FLAG_WINDOW_RESIZABLE);
2021-05-14 05:59:33 +00:00
SetTargetFPS(60);
2021-05-15 11:43:29 +00:00
texed_new(TD_DEFAULT_IMG_WIDTH, TD_DEFAULT_IMG_HEIGHT);
2021-05-15 16:41:30 +00:00
GuiSetStyle(TEXTBOX, TEXT_COLOR_NORMAL, ColorToInt(RAYWHITE));
2021-05-15 11:43:29 +00:00
while (!WindowShouldClose()) {
2021-05-15 18:00:57 +00:00
zpl_aabb2 screen = {
.min = (zpl_vec2) {.x = 0.0f, .y = 0.0f},
.max = (zpl_vec2) {.x = GetScreenWidth(), .y = GetScreenHeight()},
};
zpl_aabb2 topbar = zpl_aabb2_cut_top(&screen, 25.0f);
zpl_aabb2 oplist_pane = zpl_aabb2_cut_right(&screen, screenWidth / 2.0f);
2021-05-15 20:27:35 +00:00
zpl_aabb2 property_pane = zpl_aabb2_cut_bottom(&screen, screenHeight / 2.0f);
zpl_aabb2 preview_window = screen;
2021-05-15 18:00:57 +00:00
// NOTE(zaklaus): contract all panes for a clean UI separation
oplist_pane = zpl_aabb2_contract(&oplist_pane, TD_UI_PADDING);
preview_window = zpl_aabb2_contract(&preview_window, TD_UI_PADDING);
property_pane = zpl_aabb2_contract(&property_pane, TD_UI_PADDING);
Rectangle preview_rect = aabb2_ray(preview_window);
if (old_screen_w != GetScreenWidth() || old_screen_h != GetScreenHeight()) {
old_screen_w = GetScreenWidth();
old_screen_h = GetScreenHeight();
Image checkerboard = GenImageChecked(preview_rect.width, preview_rect.height, 16, 16, BLACK, ColorAlpha(GRAY, 0.2f));
checker_tex = LoadTextureFromImage(checkerboard);
UnloadImage(checkerboard);
2021-05-15 20:09:25 +00:00
ctx.fileDialog = InitGuiFileDialog(420, 310, zpl_bprintf("%s/art", GetWorkingDirectory()), false);
2021-05-15 18:00:57 +00:00
}
2021-05-14 05:59:33 +00:00
BeginDrawing();
2021-05-15 11:43:29 +00:00
ClearBackground(GetColor(0x222034));
2021-05-14 05:59:33 +00:00
{
2021-05-15 11:43:29 +00:00
if (ctx.fileDialog.fileDialogActive) GuiLock();
DrawTextureEx(checker_tex, (Vector2){ preview_window.min.x, preview_window.min.y}, 0.0f, 1.0f, WHITE);
DrawTextureEx(ctx.tex, (Vector2){ preview_window.min.x, preview_window.min.y}, 0.0f, zoom, WHITE);
2021-05-14 05:59:33 +00:00
2021-05-15 11:43:29 +00:00
DrawAABB(topbar, RAYWHITE);
DrawAABB(property_pane, GetColor(0x422060));
DrawAABB(oplist_pane, GetColor(0x425060));
2021-05-14 05:59:33 +00:00
2021-05-15 11:43:29 +00:00
texed_draw_topbar(topbar);
2021-05-15 14:25:18 +00:00
texed_draw_props_pane(property_pane);
2021-05-15 11:43:29 +00:00
texed_draw_oplist_pane(oplist_pane);
2021-05-14 05:59:33 +00:00
2021-05-15 11:43:29 +00:00
if (ctx.fileDialog.fileDialogActive) GuiUnlock();
GuiFileDialog(&ctx.fileDialog);
2021-05-14 05:59:33 +00:00
}
EndDrawing();
}
2021-05-15 11:43:29 +00:00
UnloadTexture(checker_tex);
texed_destroy();
}
void texed_new(int32_t w, int32_t h) {
ctx.img = GenImageColor(w, h, WHITE);
ctx.filepath = NULL;
2021-05-15 14:25:18 +00:00
ctx.selected_op = 0;
2021-05-15 11:43:29 +00:00
zpl_array_init(ctx.ops, zpl_heap());
2021-05-15 17:17:47 +00:00
texed_add_op(TOP_NEW_IMAGE);
2021-05-15 11:43:29 +00:00
ctx.fileDialog = InitGuiFileDialog(420, 310, zpl_bprintf("%s/art", GetWorkingDirectory()), false);
}
void texed_destroy(void) {
UnloadTexture(ctx.tex);
UnloadImage(ctx.img);
zpl_array_free(ctx.ops);
}
void texed_repaint_preview(void) {
2021-05-15 20:09:25 +00:00
if (is_loading_prj) return;
2021-05-15 11:43:29 +00:00
UnloadTexture(ctx.tex);
2021-05-15 13:23:04 +00:00
texed_process_params();
2021-05-15 11:43:29 +00:00
texed_process_ops();
ctx.tex = LoadTextureFromImage(ctx.img);
2021-05-14 05:59:33 +00:00
}
2021-05-15 11:43:29 +00:00
void texed_add_op(int idx) {
assert(idx >= 0 && idx < DEF_OPS_LEN);
td_op *dop = &default_ops[idx];
td_op op = {
.kind = dop->kind,
.name = dop->name,
.num_params = dop->num_params,
.params = (td_param*)zpl_malloc(sizeof(td_param)*dop->num_params)
};
zpl_memcopy(op.params, dop->params, sizeof(td_param)*dop->num_params);
zpl_array_append(ctx.ops, op);
2021-05-15 14:25:18 +00:00
ctx.selected_op = zpl_array_count(ctx.ops)-1;
2021-05-15 11:43:29 +00:00
texed_repaint_preview();
}
void texed_swp_op(int idx, int idx2) {
assert(idx >= 0 && idx < (int)zpl_array_count(ctx.ops));
assert(idx2 >= 0 && idx2 < (int)zpl_array_count(ctx.ops));
td_op tmp = ctx.ops[idx2];
ctx.ops[idx2] = ctx.ops[idx];
ctx.ops[idx] = tmp;
2021-05-15 16:41:30 +00:00
ctx.selected_op = idx2;
2021-05-15 11:43:29 +00:00
texed_repaint_preview();
}
2021-05-15 15:19:50 +00:00
void texed_rem_op(int idx) {
assert(idx >= 0 && idx < (int)zpl_array_count(ctx.ops));
zpl_mfree(ctx.ops[idx].params);
zpl_array_remove_at(ctx.ops, idx);
2021-05-15 14:25:18 +00:00
2021-05-15 20:09:25 +00:00
if (zpl_array_count(ctx.ops) > 0 && idx <= ctx.selected_op) ctx.selected_op -= 1;
2021-05-15 12:51:44 +00:00
texed_repaint_preview();
2021-05-15 11:43:29 +00:00
}