(#779) Implement ColorPicker
parent
9fd1811f08
commit
884fa38b7a
|
@ -151,6 +151,8 @@ add_executable(nothing
|
|||
src/game/level/level_editor/proto_rect.c
|
||||
src/game/level/level_editor.h
|
||||
src/game/level/level_editor.c
|
||||
src/game/level/level_editor/color_picker.h
|
||||
src/game/level/level_editor/color_picker.c
|
||||
)
|
||||
|
||||
add_executable(svg2level
|
||||
|
|
|
@ -421,3 +421,33 @@ Vec camera_map_screen(const Camera *camera,
|
|||
es),
|
||||
camera->position);
|
||||
}
|
||||
|
||||
int camera_fill_rect_screen(Camera *camera,
|
||||
Rect rect,
|
||||
Color color)
|
||||
{
|
||||
trace_assert(camera);
|
||||
|
||||
const SDL_Rect sdl_rect = rect_for_sdl(rect);
|
||||
const SDL_Color sdl_color = color_for_sdl(camera->blackwhite_mode ? color_desaturate(color) : color);
|
||||
|
||||
if (camera->debug_mode) {
|
||||
if (SDL_SetRenderDrawColor(camera->renderer, sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a / 2) < 0) {
|
||||
log_fail("SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
} else {
|
||||
if (SDL_SetRenderDrawColor(camera->renderer, sdl_color.r, sdl_color.g, sdl_color.b, sdl_color.a) < 0) {
|
||||
log_fail("SDL_SetRenderDrawColor: %s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (SDL_RenderFillRect(camera->renderer, &sdl_rect) < 0) {
|
||||
log_fail("SDL_RenderFillRect: %s\n", SDL_GetError());
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
|
|
@ -65,4 +65,8 @@ Rect camera_view_port(const Camera *camera);
|
|||
Vec camera_map_screen(const Camera *camera,
|
||||
Sint32 x, Sint32 y);
|
||||
|
||||
int camera_fill_rect_screen(Camera *camera,
|
||||
Rect rect,
|
||||
Color color);
|
||||
|
||||
#endif // CAMERA_H_
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
#include "game/camera.h"
|
||||
#include "game/level/boxes.h"
|
||||
#include "game/level/level_editor/proto_rect.h"
|
||||
#include "game/level/level_editor/color_picker.h"
|
||||
#include "system/stacktrace.h"
|
||||
#include "system/nth_alloc.h"
|
||||
|
||||
|
@ -13,16 +14,23 @@ struct LevelEditor
|
|||
Vec camera_position;
|
||||
float camera_scale;
|
||||
ProtoRect proto_rect;
|
||||
ColorPicker color_picker;
|
||||
Boxes *boxes;
|
||||
};
|
||||
|
||||
LevelEditor *create_level_editor(Boxes *boxes)
|
||||
{
|
||||
LevelEditor *level_editor = nth_calloc(1, sizeof(LevelEditor));
|
||||
if (level_editor == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
level_editor->camera_position = vec(0.0f, 0.0f);
|
||||
level_editor->camera_scale = 1.0f;
|
||||
level_editor->boxes = boxes;
|
||||
level_editor->proto_rect.color = rgba(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
level_editor->color_picker.position = vec(0.0f, 0.0f);
|
||||
level_editor->color_picker.proto_rect = &level_editor->proto_rect;
|
||||
|
||||
return level_editor;
|
||||
}
|
||||
|
@ -43,6 +51,10 @@ int level_editor_render(const LevelEditor *level_editor,
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (color_picker_render(&level_editor->color_picker, camera) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -90,6 +102,10 @@ int level_editor_event(LevelEditor *level_editor,
|
|||
return -1;
|
||||
}
|
||||
|
||||
if (color_picker_event(&level_editor->color_picker, event) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
#include <stdbool.h>
|
||||
|
||||
#include "game/level/boxes.h"
|
||||
#include "system/stacktrace.h"
|
||||
#include "game/camera.h"
|
||||
#include "proto_rect.h"
|
||||
#include "color_picker.h"
|
||||
#include "color.h"
|
||||
|
||||
#define COLOR_CELL_WIDTH 50.0f
|
||||
#define COLOR_CELL_HEIGHT 50.0f
|
||||
|
||||
// TODO: Colors of ColorPicker are poor
|
||||
static Color colors[] = {
|
||||
{1.0f, 0.0f, 0.0f, 1.0f},
|
||||
{0.0f, 1.0f, 0.0f, 1.0f},
|
||||
{0.0f, 0.0f, 1.0f, 1.0f}
|
||||
};
|
||||
static const size_t colors_count = sizeof(colors) / sizeof(Color);
|
||||
|
||||
int color_picker_render(const ColorPicker *color_picker,
|
||||
Camera *camera)
|
||||
{
|
||||
trace_assert(color_picker);
|
||||
trace_assert(camera);
|
||||
|
||||
for (size_t i = 0; i < colors_count; ++i) {
|
||||
if (camera_fill_rect_screen(
|
||||
camera,
|
||||
rect(COLOR_CELL_WIDTH * (float) i, 0,
|
||||
COLOR_CELL_WIDTH,
|
||||
COLOR_CELL_HEIGHT),
|
||||
colors[i]) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int color_picker_update(ColorPicker *color_picker,
|
||||
float delta_time)
|
||||
{
|
||||
trace_assert(color_picker);
|
||||
(void) delta_time;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int color_picker_event(ColorPicker *color_picker,
|
||||
const SDL_Event *event)
|
||||
{
|
||||
trace_assert(color_picker);
|
||||
trace_assert(event);
|
||||
|
||||
switch (event->type) {
|
||||
case SDL_MOUSEBUTTONDOWN: {
|
||||
switch (event->button.button) {
|
||||
case SDL_BUTTON_LEFT: {
|
||||
for (size_t i = 0; i < colors_count; ++i) {
|
||||
const Vec mouse_position = vec((float) event->button.x, (float) event->button.y);
|
||||
const Rect color_cell =
|
||||
rect(COLOR_CELL_WIDTH * (float) i, 0,
|
||||
COLOR_CELL_WIDTH,
|
||||
COLOR_CELL_HEIGHT);
|
||||
if (rect_contains_point(color_cell, mouse_position)) {
|
||||
color_picker->proto_rect->color = colors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
} break;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef COLOR_PICKER_H_
|
||||
#define COLOR_PICKER_H_
|
||||
|
||||
typedef struct {
|
||||
ProtoRect *proto_rect;
|
||||
Vec position;
|
||||
} ColorPicker;
|
||||
|
||||
int color_picker_render(const ColorPicker *color_picker,
|
||||
Camera *camera);
|
||||
int color_picker_update(ColorPicker *color_picker,
|
||||
float delta_time);
|
||||
int color_picker_event(ColorPicker *color_picker,
|
||||
const SDL_Event *event);
|
||||
|
||||
#endif // COLOR_PICKER_H_
|
Loading…
Reference in New Issue