mirror of https://git.sr.ht/~kikoodx/sle
80 lines
1.8 KiB
C
80 lines
1.8 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
/* Copyright (C) 2021 KikooDX */
|
|
|
|
#include "conf.h"
|
|
#include "mouse.h"
|
|
#include "shared_data.h"
|
|
#include "tile_picker/draw.h"
|
|
#include <raylib.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
static void init_mouse(void);
|
|
static void update_mouse(int *mouse_x, int *mouse_y,
|
|
struct SharedData *shared_data);
|
|
|
|
int tile_picker_main(char *tileset_path, struct SharedData *shared_data)
|
|
{
|
|
int mouse_x;
|
|
int mouse_y;
|
|
Texture2D tileset;
|
|
|
|
/* initialize raylib */
|
|
InitWindow(picker_window_width, picker_window_height,
|
|
"SLE secondary window");
|
|
SetTargetFPS(picker_target_fps);
|
|
init_mouse();
|
|
/* load textures */
|
|
tileset = LoadTexture(tileset_path);
|
|
|
|
/* only proceed if tileset is well loaded */
|
|
if (tileset.width > 0) {
|
|
while (!shared_data->end_child) {
|
|
/* update */
|
|
update_mouse(&mouse_x, &mouse_y, shared_data);
|
|
|
|
/* draw */
|
|
BeginDrawing();
|
|
|
|
ClearBackground(BLACK);
|
|
tileset_draw(tileset,
|
|
shared_data->selected_tile);
|
|
|
|
EndDrawing();
|
|
}
|
|
}
|
|
|
|
/* unload textures */
|
|
UnloadTexture(tileset);
|
|
|
|
CloseWindow();
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static void init_mouse(void)
|
|
{
|
|
SetMouseOffset(-PICKER_PADDING, -PICKER_PADDING);
|
|
SetMouseScale(
|
|
1.0 / ((TILE_WIDTH + PICKER_PADDING) * PICKER_SCALE),
|
|
1.0 / ((TILE_HEIGHT + PICKER_PADDING) * PICKER_SCALE));
|
|
}
|
|
|
|
static void update_mouse(int *mouse_x, int *mouse_y,
|
|
struct SharedData *shared_data)
|
|
{
|
|
const bool left_click = IsMouseButtonDown(0);
|
|
const bool right_click = IsMouseButtonDown(1);
|
|
|
|
update_mouse_position(
|
|
mouse_x, mouse_y,
|
|
TILESET_MIN_WIDTH_PX + TILESET_WIDTH * PICKER_PADDING,
|
|
TILESET_MIN_HEIGHT_PX + TILESET_HEIGHT * PICKER_PADDING);
|
|
|
|
/* set left button tile */
|
|
if (left_click)
|
|
shared_data->selected_tile =
|
|
*mouse_x + *mouse_y * TILESET_WIDTH;
|
|
}
|