-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixelore_tools.c
122 lines (101 loc) · 3.72 KB
/
pixelore_tools.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Pixelore Easy and light pixel art editor
Copyright (C) 2022-2023 SolindekDev
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/* All pixelore includes */
#include <pixelore_tools.h>
#include <pixelore_app.h>
#include <pixelore_main.h>
#include <pixelore_input.h>
#include <pixelore_types.h>
#include <pixelore_window.h>
#include <pixelore_config.h>
#include <pixelore_button.h>
#include <pixelore_draw.h>
#include <pixelore_scroll.h>
/* STD Libraries */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/* SDL2 Library */
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
__GLOBAL__ i16 selected_tool = 0;
__GLOBAL__ tool_t tools_list[MAX_TOOLS];
__GLOBAL__ i16 tools_len = 0;
/* Function for creating tools */
void create_tool(window_t* win, str tool_name, TOOL_CALLBACK tool_func)
{
/* Copy tools len and append our new tool to the tools array */
tool_t tool = { tool_name, tool_func, win, tools_len };
tools_list[tools_len] = tool;
tools_len++;
#ifdef __DEBUG
DEBUG_NN("Successfully registered tool with name [");
printf("%s] and id [%d]\n", tools_list[tools_len - 1].tool_name
, tools_list[tools_len - 1].tool_id);
#endif
}
/* Function for getting tools by their name */
tool_t* get_tool_by_name(str tool_name)
{
/* Iterate by the tools_list array and if an element tool_name
* equals tool_name return this element */
ITER(tools_len)
if (tools_list[i].tool_name == tool_name) return &tools_list[i];
return NULL;
}
/* Function for getting tools by their id */
tool_t* get_tool_by_id(i16 tool_id)
{
/* Iterate by the tools_list array and if an element tool_id
* equals tool_id return this element */
ITER(tools_len)
if (tools_list[i].tool_id == tool_id) return &tools_list[i];
return NULL;
}
/* Function that is called when any tools is selected */
i32 tool_selection_callback(window_t* win, button_t btn, vec2_t _unused, bool __unused)
{
/* Quick little debug */
#ifdef __DEBUG
DEBUG_NN("Selected tool: ");
printf("%s\n", tools_list[btn.id - TOOLKIT_COLORS_LENGTH - 1].tool_name);
#endif
/* Set selecteed tool to button id - TOOLKIT_COLORS_LENGTH - 1
* It's not that complicated math lmao */
selected_tool = btn.id - TOOLKIT_COLORS_LENGTH - 1;
return 1;
}
/* This function return selected tool, just an easy wrapper */
i16 get_selected_tool()
{
return selected_tool;
}
/* This function is called when user is clicking on the
* bitmap */
i32 bitmap_tool_callback(window_t* win, button_t btn, vec2_t mouse, bool button_press)
{
/* Calculate the bitmap position from the mouse position lol
* Just substract mouse.x with btn.x and in the other way then
* get the scroll number and divide it by mouse.x and mouse.y*/
mouse.x = ((mouse.x - btn.x) / get_scroll());
mouse.y = ((mouse.y - btn.y) / get_scroll());
/* Get the selected tool by id, and then call the
* tool_func tool function */
tool_t* selected_tool = get_tool_by_id(get_selected_tool());
selected_tool->tool_func(win, mouse, button_press,
selected_tool);
return 0;
}