refactor gesture

This commit is contained in:
idea4good 2019-10-09 17:25:44 +08:00
parent b75960f2f3
commit bfb670255d
6 changed files with 44 additions and 81 deletions

View File

@ -46,7 +46,7 @@ c_surface* c_display::alloc_surface(Z_ORDER_LEVEL max_zorder)
return m_surface_group[i]; return m_surface_group[i];
} }
int c_display::merge_surface(c_surface* s0, c_surface* s1, int x0, int x1, int y0, int y1, int offset) int c_display::swipe_surface(c_surface* s0, c_surface* s1, int x0, int x1, int y0, int y1, int offset)
{ {
int surface_width = s0->get_width(); int surface_width = s0->get_width();
int surface_height = s0->get_height(); int surface_height = s0->get_height();

View File

@ -13,7 +13,7 @@ public:
unsigned int surface_width, unsigned int surface_height, unsigned int surface_width, unsigned int surface_height,
unsigned int color_bytes, unsigned int surface_cnt, EXTERNAL_GFX_OP* gfx_op = 0); unsigned int color_bytes, unsigned int surface_cnt, EXTERNAL_GFX_OP* gfx_op = 0);
c_surface* alloc_surface(Z_ORDER_LEVEL max_zorder); c_surface* alloc_surface(Z_ORDER_LEVEL max_zorder);
int merge_surface(c_surface* s1, c_surface* s2, int x0, int x1, int y0, int y2, int offset); int swipe_surface(c_surface* s1, c_surface* s2, int x0, int x1, int y0, int y2, int offset);
unsigned int get_width() { return m_width; } unsigned int get_width() { return m_width; }
unsigned int get_height() { return m_height; } unsigned int get_height() { return m_height; }

View File

@ -9,46 +9,24 @@
#include "../widgets_include/slide_group.h" #include "../widgets_include/slide_group.h"
#include <stdlib.h> #include <stdlib.h>
//#define FLIP_STEP 300//for arm //#define SWIPE_STEP 300//for arm
#define FLIP_STEP 10//for PC & ANDROID #define SWIPE_STEP 10//for PC & ANDROID
#define MOVE_THRESHOLD 10 #define MOVE_THRESHOLD 10
void* c_gesture::task_handle_msg(void* param) c_gesture::c_gesture(c_slide_group* group)
{ {
c_gesture* This = (c_gesture*)param;
MSG_INFO msg;
while(1)
{
This->m_hid_fifo->read(&msg, sizeof(msg));
if(This->handle_flip(msg))
{
This->handle_hid_msg(msg);
}
}
return 0;
}
c_gesture::c_gesture(c_wnd* root, c_slide_group* group, c_fifo* hid_fifo)
{
m_root = root;
m_slide_group = group; m_slide_group = group;
m_hid_fifo = hid_fifo; m_state = TOUCH_IDLE;
m_action = TOUCH_IDLE;
m_down_x = m_down_y = m_move_x = m_move_y = 0; m_down_x = m_down_y = m_move_x = m_move_y = 0;
unsigned long pid;
create_thread(&pid, 0, task_handle_msg, this);
} }
bool c_gesture::handle_flip(MSG_INFO &msg) bool c_gesture::handle_swipe(int x, int y, TOUCH_ACTION action)
{ {
int x = msg.dwParam1; if(action == TOUCH_DOWN)//MOUSE_LBUTTONDOWN
if(msg.dwMsgId == 0x4700)//MOUSE_LBUTTONDOWN
{ {
if(m_action == TOUCH_IDLE) if(m_state == TOUCH_IDLE)
{ {
m_action = TOUCH_MOVE; m_state = TOUCH_MOVE;
m_move_x = m_down_x = x; m_move_x = m_down_x = x;
return true; return true;
} }
@ -57,12 +35,12 @@ bool c_gesture::handle_flip(MSG_INFO &msg)
return on_move(x); return on_move(x);
} }
} }
else if(msg.dwMsgId == 0x4600)//MOUSE_LBUTTONUP else if(action == TOUCH_UP)//MOUSE_LBUTTONUP
{ {
if(m_action == TOUCH_MOVE) if(m_state == TOUCH_MOVE)
{ {
m_action = TOUCH_IDLE; m_state = TOUCH_IDLE;
return on_flip(x); return on_swipe(x);
} }
else else
{ {
@ -97,7 +75,7 @@ bool c_gesture::on_move(int x)
return false; return false;
} }
bool c_gesture::on_flip(int x) bool c_gesture::on_swipe(int x)
{ {
if (m_slide_group == 0) if (m_slide_group == 0)
{ {
@ -113,11 +91,11 @@ bool c_gesture::on_flip(int x)
m_move_x = x; m_move_x = x;
if ((m_move_x - m_down_x) > 0) if ((m_move_x - m_down_x) > 0)
{ {
page = flip_right(); page = swipe_right();
} }
else else
{ {
page = flip_left(); page = swipe_left();
} }
if (page >= 0) if (page >= 0)
{ {
@ -130,7 +108,7 @@ bool c_gesture::on_flip(int x)
return false; return false;
} }
int c_gesture::flip_left() int c_gesture::swipe_left()
{ {
if (m_slide_group == 0) if (m_slide_group == 0)
{ {
@ -155,17 +133,17 @@ int c_gesture::flip_left()
m_slide_group->get_screen_rect(rc); m_slide_group->get_screen_rect(rc);
while(step < rc.Width()) while(step < rc.Width())
{ {
s1->get_display()->merge_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, step); s1->get_display()->swipe_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, step);
step += FLIP_STEP; step += SWIPE_STEP;
} }
if (step != rc.Width()) if (step != rc.Width())
{ {
s1->get_display()->merge_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, rc.Width()); s1->get_display()->swipe_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, rc.Width());
} }
return (index + 1); return (index + 1);
} }
int c_gesture::flip_right() int c_gesture::swipe_right()
{ {
if (m_slide_group == 0) if (m_slide_group == 0)
{ {
@ -190,12 +168,12 @@ int c_gesture::flip_right()
int step = rc.Width() - (m_move_x - m_down_x); int step = rc.Width() - (m_move_x - m_down_x);
while(step > 0) while(step > 0)
{ {
s1->get_display()->merge_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, step); s1->get_display()->swipe_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, step);
step -= FLIP_STEP; step -= SWIPE_STEP;
} }
if (step != 0) if (step != 0)
{ {
s1->get_display()->merge_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, 0); s1->get_display()->swipe_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, 0);
} }
return (index - 1); return (index - 1);
} }
@ -215,7 +193,7 @@ void c_gesture::move_left()
m_slide_group->get_screen_rect(rc); m_slide_group->get_screen_rect(rc);
if(s1->get_display() == s2->get_display()) if(s1->get_display() == s2->get_display())
{ {
s1->get_display()->merge_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, (m_down_x - m_move_x)); s1->get_display()->swipe_surface(s2, s1, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, (m_down_x - m_move_x));
} }
} }
@ -234,19 +212,6 @@ void c_gesture::move_right()
m_slide_group->get_screen_rect(rc); m_slide_group->get_screen_rect(rc);
if(s1->get_display() == s2->get_display()) if(s1->get_display() == s2->get_display())
{ {
s1->get_display()->merge_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, (rc.Width() - (m_move_x - m_down_x))); s1->get_display()->swipe_surface(s1, s2, rc.m_left, rc.m_right, rc.m_top, rc.m_bottom, (rc.Width() - (m_move_x - m_down_x)));
}
}
void c_gesture::handle_hid_msg(MSG_INFO &msg)
{
switch(msg.dwMsgId)
{
case 0x4700://MOUSE_LBUTTONDOWN
m_root->on_touch(msg.dwParam1, msg.dwParam2, TOUCH_DOWN);
break;
case 0x4600://MOUSE_LBUTTONUP
m_root->on_touch(msg.dwParam1, msg.dwParam2, TOUCH_UP);
break;
} }
} }

View File

@ -6,9 +6,10 @@
#include "../core_include/wnd.h" #include "../core_include/wnd.h"
#include "../core_include/surface.h" #include "../core_include/surface.h"
#include "../widgets_include/dialog.h" #include "../widgets_include/dialog.h"
#include "../widgets_include/gesture.h"
#include "../widgets_include/slide_group.h" #include "../widgets_include/slide_group.h"
c_slide_group::c_slide_group() c_slide_group::c_slide_group() : m_gesture(this)
{ {
for(int i = 0; i < MAX_PAGES; i++) for(int i = 0; i < MAX_PAGES; i++)
{ {
@ -157,9 +158,13 @@ bool c_slide_group::on_touch(int x, int y, TOUCH_ACTION action)
{ {
x -= m_wnd_rect.m_left; x -= m_wnd_rect.m_left;
y -= m_wnd_rect.m_top; y -= m_wnd_rect.m_top;
if (m_slides[m_active_slide_index])
if (m_gesture.handle_swipe(x, y, action))
{ {
m_slides[m_active_slide_index]->on_touch(x, y, action); if (m_slides[m_active_slide_index])
{
m_slides[m_active_slide_index]->on_touch(x, y, action);
}
} }
return true; return true;
} }

View File

@ -4,35 +4,27 @@
typedef enum{ typedef enum{
TOUCH_MOVE, TOUCH_MOVE,
TOUCH_IDLE TOUCH_IDLE
}ACTION; }TOUCH_STATE;
class c_slide_group; class c_slide_group;
class c_gesture{ class c_gesture{
public: public:
c_gesture(c_wnd* root, c_slide_group* group, c_fifo* hid_fifo); c_gesture(c_slide_group* group);
void set_page_group(c_slide_group* group){m_slide_group = group;} bool handle_swipe(int x, int y, TOUCH_ACTION action);
protected:
bool handle_flip(MSG_INFO &msg);
bool on_move(int x);
bool on_flip(int x);
private: private:
int flip_left(); bool on_move(int x);
int flip_right(); bool on_swipe(int x);
int swipe_left();
int swipe_right();
void move_left(); void move_left();
void move_right(); void move_right();
void handle_hid_msg(MSG_INFO &msg);
int m_down_x; int m_down_x;
int m_down_y; int m_down_y;
int m_move_x; int m_move_x;
int m_move_y; int m_move_y;
ACTION m_action; TOUCH_STATE m_state;
c_slide_group* m_slide_group; c_slide_group* m_slide_group;
c_wnd* m_root;
c_fifo* m_hid_fifo;
static void* task_handle_msg(void* param);
}; };
#endif #endif

View File

@ -23,6 +23,7 @@ protected:
virtual c_wnd* clone(){return new c_slide_group();} virtual c_wnd* clone(){return new c_slide_group();}
c_wnd* m_slides[MAX_PAGES]; c_wnd* m_slides[MAX_PAGES];
int m_active_slide_index; int m_active_slide_index;
c_gesture m_gesture;
}; };
#endif #endif