Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add touchscreen support to ui-scroll. #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdl.ceu
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ native @const _SDL_TRUE, _SDL_FALSE,
_SDL_BUTTON_RIGHT,
_SDL_KEYDOWN,
_SDL_MOUSEBUTTONDOWN,
_SDL_FINGERMOTION,
_SDL_PIXELFORMAT_UNKNOWN,
_SDL_SCANCODE_AC_BACK,
_SDL_RENDERER_ACCELERATED,
Expand Down
21 changes: 17 additions & 4 deletions sim.ceu
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
// - entre intervalos, usa eventos gravados
// - teoricamente, só precisa dos eventos

input void SDL_QUIT_;
input int SDL_DT_;
input _SDL_KeyboardEvent&& SDL_KEYDOWN_;
input _SDL_KeyboardEvent&& SDL_KEYUP_;
input void SDL_QUIT, SDL_QUIT_;
input int SDL_DT, SDL_DT_;
input _SDL_KeyboardEvent&& SDL_KEYDOWN, SDL_KEYDOWN_;
input _SDL_KeyboardEvent&& SDL_KEYUP, SDL_KEYUP_;
input _SDL_MouseButtonEvent&& SDL_MOUSEBUTTONDOWN, SDL_MOUSEBUTTONDOWN_;
input _SDL_MouseMotionEvent&& SDL_MOUSEMOTION, SDL_MOUSEMOTION_;
input _SDL_TouchFingerEvent&& SDL_FINGERMOTION, SDL_FINGERMOTION_;

#include "c.ceu"
#include "sdl.ceu"
Expand Down Expand Up @@ -146,6 +149,12 @@ do
nothing;
end;
end
with
var _SDL_TouchFingerEvent* tfe;
every tfe in SDL_FINGERMOTION do
_queue_put(_CEU_IN_SDL_FINGERMOTION, _CEU_EVTP(null),
sizeof(_SDL_TouchFingerEvent), (byte*)tfe);
end
end
end
await this.go_off;
Expand All @@ -162,6 +171,10 @@ do
async(val) do
emit SDL_KEYUP => &&val;
end
else/if qu:evt == _CEU_IN_SDL_FINGERMOTION then
async(qu) do
emit SDL_FINGERMOTION => qu:param.ptr;
end
else
_assert(0);
end
Expand Down
153 changes: 96 additions & 57 deletions ui-scroll.ceu
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

input void SDL_REDRAW;
input _SDL_MouseButtonEvent&& SDL_MOUSEBUTTONDOWN;
input _SDL_TouchFingerEvent&& SDL_FINGERDOWN;
input _SDL_TouchFingerEvent&& SDL_FINGERUP;
input _SDL_TouchFingerEvent&& SDL_FINGERMOTION;

class UIScroll with
interface UI;
Expand Down Expand Up @@ -45,74 +48,106 @@ do

/* BODY */

event void go_x, go_y;
event void go_x, go_y, new_position;
var bool should_go_x=false, should_go_y=false;

par do
loop do
var _SDL_MouseButtonEvent&& but = await SDL_MOUSEBUTTONDOWN;

var SDL_Point pt1 = SDL_Point(but:x,but:y);
var SDL_Point pt2;
var SDL_Point pt1, pt2;
var bool is_touch;

par/or do
var _SDL_MouseButtonEvent&& but = await SDL_MOUSEBUTTONDOWN;
pt1.x = but:x;
pt1.y = but:y;
is_touch = false;
with
var _SDL_TouchFingerEvent&& touch = await SDL_FINGERDOWN;
pt1.x = touch:x;
pt1.y = touch:y;
is_touch = true;
end

loop do
await 50ms;
var int s = _SDL_GetMouseState(&&pt2.x, &&pt2.y);
if not s then
break;
end
pt1.x = pt2.x - pt1.x;
pt1.y = pt2.y - pt1.y;

// LIMITS
if ui.rect.w > rect.w then
if pt1.x > 0 then
var int max_x = rect.x;
var int max_dx = max_x - ui.rect.x;
if pt1.x > max_dx then
pt1.x = max_dx;
end
else/if pt1.x < 0 then
var int min_x = rect.x - (ui.rect.w-rect.w);
var int min_dx = min_x - ui.rect.x;
if pt1.x < min_dx then
pt1.x = min_dx;
end
par/or do
await SDL_FINGERUP;
with
if is_touch then
loop do
var _SDL_TouchFingerEvent&& touch = await SDL_FINGERMOTION;
pt2.x = touch:x;
pt2.y = touch:y;
emit new_position;
end
emit go_x;
else
pt1.x = 0;
loop do
await 50ms;
var int s = _SDL_GetMouseState(&&pt2.x, &&pt2.y);
if s then
emit new_position;
else
break;
end
end
end

if ui.rect.h > rect.h then
if pt1.y > 0 then
var int max_y = rect.y;
var int max_dy = max_y - ui.rect.y;
if pt1.y > max_dy then
pt1.y = max_dy;
with
loop do
await new_position;
pt1.x = pt2.x - pt1.x;
pt1.y = pt2.y - pt1.y;

var int px = pt1.x;
var int py = pt1.y;

// LIMITS
if ui.rect.w > rect.w then
if pt1.x > 0 then
var int max_x = rect.x;
var int max_dx = max_x - ui.rect.x;
if pt1.x > max_dx then
pt1.x = max_dx;
end
else/if pt1.x < 0 then
var int min_x = rect.x - (ui.rect.w-rect.w);
var int min_dx = min_x - ui.rect.x;
if pt1.x < min_dx then
pt1.x = min_dx;
end
end
else/if pt1.y < 0 then
var int min_y = rect.y - (ui.rect.h-rect.h);
var int min_dy = min_y - ui.rect.y;
if pt1.y < min_dy then
pt1.y = min_dy;
emit go_x;
else
pt1.x = 0;
end

if ui.rect.h > rect.h then
if pt1.y > 0 then
var int max_y = rect.y;
var int max_dy = max_y - ui.rect.y;
if pt1.y > max_dy then
pt1.y = max_dy;
end
else/if pt1.y < 0 then
var int min_y = rect.y - (ui.rect.h-rect.h);
var int min_dy = min_y - ui.rect.y;
if pt1.y < min_dy then
pt1.y = min_dy;
end
end
emit go_y;
else
pt1.y = 0;
end
emit go_y;
else
pt1.y = 0;
end

if pt1.x!=0 or pt1.y!=0 then
ui.rect.x = ui.rect.x + pt1.x;
ui.rect.y = ui.rect.y + pt1.y;
call/rec ui.go(null);

if pt1.x!=0 or pt1.y!=0 then
ui.rect.x = ui.rect.x + pt1.x;
ui.rect.y = ui.rect.y + pt1.y;
call/rec ui.go(null);
end

//ui.rect.x = ui.rect.x + pt1.x;
//ui.rect.y = ui.rect.y + pt1.y;

pt1 = pt2;
end

//ui.rect.x = ui.rect.x + pt1.x;
//ui.rect.y = ui.rect.y + pt1.y;

pt1 = pt2;
end
end
with
Expand Down Expand Up @@ -251,8 +286,12 @@ with
_fprintf(_stderr, "G: %d\n", opt);
end
with
/*
_printf("scrolling will enable in 5 seconds...\n");
await 5s;
_printf("oi\n");
_printf("scrolling now enabled!\n");
*/
_printf("started!\n");

var SDL_Color c = SDL_COLOR_BLUE;
c.a = 0x77;
Expand Down