rootston: rotate views!

This commit is contained in:
emersion 2017-10-03 21:06:32 +02:00
parent a40d38798c
commit 9b88f25208
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
3 changed files with 52 additions and 14 deletions

View File

@ -83,6 +83,7 @@ struct roots_input {
struct roots_view *active_view;
int offs_x, offs_y;
int view_x, view_y, view_width, view_height;
float view_rotation;
uint32_t resize_edges;
// Ring buffer of input events that could trigger move/resize/rotate

View File

@ -1,6 +1,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
// TODO: BSD et al
#include <linux/input-event-codes.h>
#include <wayland-server.h>
@ -25,15 +26,16 @@ const struct roots_input_event *get_input_event(struct roots_input *input,
void view_begin_move(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view) {
input->mode = ROOTS_CURSOR_MOVE;
input->offs_x = cursor->x - view->x;
input->offs_y = cursor->y - view->y;
input->offs_x = cursor->x;
input->offs_y = cursor->y;
input->view_x = view->x;
input->view_y = view->y;
wlr_seat_pointer_clear_focus(input->wl_seat);
}
void view_begin_resize(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view, uint32_t edges) {
input->mode = ROOTS_CURSOR_RESIZE;
wlr_log(L_DEBUG, "begin resize");
input->offs_x = cursor->x;
input->offs_y = cursor->y;
input->view_x = view->x;
@ -46,6 +48,15 @@ void view_begin_resize(struct roots_input *input, struct wlr_cursor *cursor,
wlr_seat_pointer_clear_focus(input->wl_seat);
}
void view_begin_rotate(struct roots_input *input, struct wlr_cursor *cursor,
struct roots_view *view) {
input->mode = ROOTS_CURSOR_ROTATE;
input->offs_x = cursor->x;
input->offs_y = cursor->y;
input->view_rotation = view->rotation;
wlr_seat_pointer_clear_focus(input->wl_seat);
}
void cursor_update_position(struct roots_input *input, uint32_t time) {
struct roots_desktop *desktop = input->server->desktop;
struct roots_view *view;
@ -64,16 +75,18 @@ void cursor_update_position(struct roots_input *input, uint32_t time) {
break;
case ROOTS_CURSOR_MOVE:
if (input->active_view) {
input->active_view->x = input->cursor->x - input->offs_x;
input->active_view->y = input->cursor->y - input->offs_y;
int dx = input->cursor->x - input->offs_x,
dy = input->cursor->y - input->offs_y;
input->active_view->x = input->view_x + dx;
input->active_view->y = input->view_y + dy;
}
break;
case ROOTS_CURSOR_RESIZE:
if (input->active_view) {
int dx = input->cursor->x - input->offs_x;
int dy = input->cursor->y - input->offs_y;
int width = input->view_width;
int height = input->view_height;
int dx = input->cursor->x - input->offs_x,
dy = input->cursor->y - input->offs_y;
int width = input->view_width,
height = input->view_height;
if (input->resize_edges & ROOTS_CURSOR_RESIZE_EDGE_TOP) {
input->active_view->y = input->view_y + dy;
height -= dy;
@ -92,6 +105,17 @@ void cursor_update_position(struct roots_input *input, uint32_t time) {
}
break;
case ROOTS_CURSOR_ROTATE:
if (input->active_view) {
struct roots_view *view = input->active_view;
int ox = view->x + view->wlr_surface->current->width/2,
oy = view->y + view->wlr_surface->current->height/2;
int ux = input->offs_x - ox,
uy = input->offs_y - oy;
int vx = input->cursor->x - ox,
vy = input->cursor->y - oy;
float angle = atan2(vx*uy - vy*ux, vx*ux + vy*uy);
view->rotation = input->view_rotation + angle;
}
break;
}
}
@ -175,6 +199,8 @@ static void do_cursor_button_press(struct roots_input *input,
ROOTS_CURSOR_RESIZE_EDGE_RIGHT |
ROOTS_CURSOR_RESIZE_EDGE_BOTTOM);
break;
case BTN_MIDDLE:
view_begin_rotate(input, cursor, view);
}
return;
}

View File

@ -18,7 +18,7 @@ static inline int64_t timespec_to_msec(const struct timespec *a) {
static void render_surface(struct wlr_surface *surface,
struct roots_desktop *desktop, struct wlr_output *wlr_output,
struct timespec *when, double lx, double ly) {
struct timespec *when, double lx, double ly, float rotation) {
wlr_surface_flush_damage(surface);
if (surface->texture->valid) {
int width = surface->current->buffer_width;
@ -27,10 +27,20 @@ static void render_surface(struct wlr_surface *surface,
wlr_output_layout_output_coords(desktop->layout, wlr_output, &ox, &oy);
if (wlr_output_layout_intersects(desktop->layout, wlr_output,
lx, ly, lx + width, ly + height)) {
lx, ly, lx + width, ly + height)) {
float matrix[16];
float translate_origin[16];
wlr_matrix_translate(&translate_origin,
ox + width/2, oy + height/2, 0);
float rotate[16];
wlr_matrix_rotate(&rotate, rotation);
float translate_center[16];
wlr_matrix_translate(&translate_center, -width/2, -height/2, 0);
float transform[16];
wlr_matrix_translate(&transform, ox, oy, 0);
wlr_matrix_mul(&translate_origin, &rotate, &transform);
wlr_matrix_mul(&transform, &translate_center, &transform);
wlr_surface_get_matrix(surface, &matrix,
&wlr_output->transform_matrix, &transform);
wlr_render_with_matrix(desktop->server->renderer,
@ -48,7 +58,8 @@ static void render_surface(struct wlr_surface *surface,
wl_list_for_each(subsurface, &surface->subsurface_list, parent_link) {
render_surface(subsurface->surface, desktop, wlr_output, when,
lx + subsurface->surface->current->subsurface_position.x,
ly + subsurface->surface->current->subsurface_position.y);
ly + subsurface->surface->current->subsurface_position.y,
rotation);
}
}
}
@ -56,7 +67,7 @@ static void render_surface(struct wlr_surface *surface,
static void render_view(struct roots_view *view, struct roots_desktop *desktop,
struct wlr_output *wlr_output, struct timespec *when) {
render_surface(view->wlr_surface, desktop, wlr_output, when,
view->x, view->y);
view->x, view->y, view->rotation);
}
static void output_frame_notify(struct wl_listener *listener, void *data) {