Multitouch (up to 3 fingers)

Post Reply
User avatar
J3d1
Posts: 175
Joined: Wed Oct 15, 2008 4:49 am
Location: г. Абакан, Хакасия
Contact:

Multitouch (up to 3 fingers)

Post by J3d1 » Tue Oct 09, 2012 5:01 pm

Code: Select all

screen = get_screen()
xsize = get_xsize(screen)
ysize = get_ysize(screen)
drawEnabled = 0
while(1)
{
    print("Press left/right/up/down with up to 3 fingers", 0, -ysize / 2 + 8, WHITE)
    frame()
    while(get_event())
    {
	
		if (EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN)) | (EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP)) | (EVT [EVT_TYPE] == (EVT_MOUSEMOVE)) |
		(EVT [EVT_TYPE] == (EVT_TOUCHBEGIN)) | (EVT [EVT_TYPE] == (EVT_TOUCHEND)) | (EVT [EVT_TYPE] == (EVT_TOUCHMOVE))
			{
	    		if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1} // enable drawing (for compatibility with non-multitouch devices)
				if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} // disable drawing
				scancode = EVT [EVT_SCANCODE]
	    		if (scancode == 0) & (drawEnabled == 1) // for first touch
	    			{
	    				x0 = EVT [EVT_X]
	    				y0 = EVT [EVT_Y]
	    				fbox(x0, y0, 2, 2, WHITE)
	    			}
	    		 if (scancode == 1) & (drawEnabled == 1) // for second touch
	    			{
	    				x1 = EVT [EVT_X]
	    				y1 = EVT [EVT_Y]
	    				fbox(x1, y1, 2, 2, RED)
	    			}
	    		 if (scancode == 2) & (drawEnabled == 1) // for third touch
	    			{
	    				x2 = EVT [EVT_X]
	    				y2 = EVT [EVT_Y]
	    				fbox(x2, y2, 2, 2, YELLOW)
	    			}		  				    			
			}
	 
		if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}
Буду благодарен если улучшите этот код.
I would be grateful if you improve this code.

Al Rado
Posts: 6
Joined: Wed Oct 10, 2012 1:28 am

Re: Multitouch (up to 3 fingers)

Post by Al Rado » Wed Oct 10, 2012 1:40 am

сделал две ревизии кода
первый вариант читается проще

Code: Select all

brushSize = 2
screen = get_screen()
xsize = get_xsize(screen)
ysize = get_ysize(screen)
drawEnabled = 0
while(1)
{
    print("Press left/right/up/down with up to 3 fingers", 0, -ysize / 2 + 8, WHITE)
    frame()
    while(get_event())
    {
      if (EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN)) | (EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP)) | (EVT [EVT_TYPE] == (EVT_MOUSEMOVE)) |
      (EVT [EVT_TYPE] == (EVT_TOUCHBEGIN)) | (EVT [EVT_TYPE] == (EVT_TOUCHEND)) | (EVT [EVT_TYPE] == (EVT_TOUCHMOVE))
         {
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1} // enable drawing (for compatibility with non-multitouch devices)
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} // disable drawing
            	scancode = EVT [EVT_SCANCODE]
		        x = EVT [EVT_X]
             	y = EVT [EVT_Y]
		        if (drawEnabled == 1){
             		if (scancode == 0)// for first touch
              		{
                	drawBrush(x, y, WHITE)
               		}
              		if (scancode == 1)  // for second touch
                	{
		   	        drawBrush(x, y, RED)
                	}
              		if (scancode == 2)  // for third touch
               		{
                   	drawBrush(x, y, YELLOW)
                	}                                 
         }
	}
      if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}

fn drawBrush( $x, $y, $color ) 
{
  fbox($x, $y, brushSize, brushSize, $color)
}
во втором попытался вынести определение интерактивного события в отдельный метод дабы избавиться от кучи if, но блин, получилось даже более громоздко:

Code: Select all

EVENTS_NUM = 6
EVENT_TYPES = new (EVENTS_NUM)
EVENT_TYPES[0] = EVT_MOUSEBUTTONDOWN
EVENT_TYPES[1] = EVT_MOUSEBUTTONUP
EVENT_TYPES[2] = EVT_MOUSEMOVE
EVENT_TYPES[3] = EVT_TOUCHBEGIN
EVENT_TYPES[4] = EVT_TOUCHEND
EVENT_TYPES[5] = EVT_TOUCHMOVE

brushSize = 2
screen = get_screen()
xsize = get_xsize(screen)
ysize = get_ysize(screen)
drawEnabled = 0
while(1)
{
    print("Press left/right/up/down with up to 3 fingers", 0, -ysize / 2 + 8, WHITE)
    frame()
    while(get_event()) {
        if (isInteractiveEvent == 1) {
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1} // enable drawing (for compatibility with non-multitouch devices)
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} // disable drawing
		    if (drawEnabled == 1) {
                // for first touch
                if (EVT [EVT_SCANCODE] == 0) {
                    drawBrush(EVT [EVT_X], EVT [EVT_Y], WHITE)
                }
                // for second touch
                if (EVT [EVT_SCANCODE] == 1) {
                    drawBrush(EVT [EVT_X], EVT [EVT_Y], RED)
                }
                // for third touch
                if (EVT [EVT_SCANCODE] == 2) {
                    drawBrush(EVT [EVT_X], EVT [EVT_Y], YELLOW)
                }
            }
	    }
      if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}

fn drawBrush( $x, $y, $color ) 
{
  fbox($x, $y, brushSize, brushSize, $color)
}

fn isInteractiveEvent()
{
  $i = 0
  while( $i < EVENTS_NUM ){
    if (EVT [EVT_TYPE] == EVENT_TYPES[$i] ) {
        ret(1)
    }
  $i+1
  }
  ret(0)
}
а все потому, что я не нашел как узнать длинну массива(контейнера), ну и жаль что нельзя написать так для более краткого вида:

Code: Select all

EVENT_TYPES = [EVT_MOUSEBUTTONDOWN, EVT_MOUSEBUTTONUP, EVT_MOUSEMOVE, EVT_TOUCHBEGIN, EVT_TOUCHEND, EVT_TOUCHMOVE]
уверен, можно еще улучшить )

User avatar
J3d1
Posts: 175
Joined: Wed Oct 15, 2008 4:49 am
Location: г. Абакан, Хакасия
Contact:

Re: Multitouch (up to 3 fingers)

Post by J3d1 » Wed Oct 10, 2012 6:57 am

Покопался в исходном коде pixilang3.4 (в части назначения констант событий), посмотрел варианты AlRado и сделал такой вариант. Да, & в условных операторах if заменил на && - так, теоретически, будет вернее.

Code: Select all

screen = get_screen()
xsize = get_xsize(screen)
ysize = get_ysize(screen)
drawEnabled = 0
while(1)
{
    print("Slide on screen with up to 3 fingers", 0, -ysize / 2 + 8, WHITE)
    frame()
    while(get_event())
    {
		if EVT [EVT_TYPE] > 0 && EVT [EVT_TYPE] < 7
			{
	    		if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1} // enable drawing (for compatibility with non-multitouch devices)
				if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} // disable drawing
	    		if (EVT [EVT_SCANCODE] == 0) && (drawEnabled == 1) // for first touch
	    			{
	    				drawBrush (WHITE)
	    			}
	    		 if (EVT [EVT_SCANCODE] == 1) && (drawEnabled == 1) // for second touch
	    			{
	    				drawBrush (RED)
	    			}
	    		 if (EVT [EVT_SCANCODE] == 2) && (drawEnabled == 1) // for third touch
	    			{
	    				drawBrush (YELLOW)
	    			}		  				    			
			}
		if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}
//===== functions================================
fn drawBrush ($color)
	{
		fbox (EVT[EVT_X], EVT [EVT_Y], 2, 2, $color)
	}

Al Rado
Posts: 6
Joined: Wed Oct 10, 2012 1:28 am

Re: Multitouch (up to 3 fingers)

Post by Al Rado » Wed Oct 10, 2012 8:51 am

вариант еще проще есть, 5 пальцев

Code: Select all

brushSize = 2
screen = get_screen()
xsize = get_xsize(screen)
ysize = get_ysize(screen)
COLORS[0] = WHITE
COLORS[1] = RED
COLORS[2] = YELLOW
COLORS[3] = GREEN
COLORS[4] = BLUE
drawEnabled = 0

while(1) {
    print("Press left/right/up/down with up to 3 fingers", 0, -ysize / 2 + 8, WHITE)
    frame()
    while(get_event()) {
        if (EVT [EVT_TYPE] > 0 && EVT [EVT_TYPE] < 7) {
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1} // enable drawing (for compatibility with non-multitouch devices)
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} // disable drawing

            if (drawEnabled == 1){
                drawBrush()
            }
	}
    if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}

fn drawBrush() 
{
  $color = COLORS[EVT [EVT_SCANCODE]]
  fbox(EVT [EVT_X], EVT [EVT_Y], brushSize, brushSize, $color)
}

User avatar
J3d1
Posts: 175
Joined: Wed Oct 15, 2008 4:49 am
Location: г. Абакан, Хакасия
Contact:

Re: Multitouch (up to 3 fingers)

Post by J3d1 » Wed Oct 10, 2012 8:55 am

Al Rado wrote:вариант еще проще есть, 5 пальцев...
Класс, тоже думал так сделать.

Al Rado
Posts: 6
Joined: Wed Oct 10, 2012 1:28 am

Re: Multitouch (up to 3 fingers)

Post by Al Rado » Wed Oct 10, 2012 10:32 am

ну и по традиции еще проще :)

Code: Select all

COLORS[0] = WHITE
COLORS[1] = RED
COLORS[2] = YELLOW
COLORS[3] = GREEN
COLORS[4] = BLUE

while(1) {
    print("Press left/right/up/down with up to 3 fingers", 0, -get_ysize(get_screen()) / 2 + 8, WHITE)
    frame()
    while(get_event()) {
        if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) {drawEnabled = 1}
        if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONUP) {drawEnabled = 0} 

        if (EVT [EVT_TYPE] > 0 && EVT [EVT_TYPE] < 7 && drawEnabled) {
            fbox(EVT [EVT_X], EVT [EVT_Y], 2, 2, COLORS[EVT [EVT_SCANCODE]])
        }

        if EVT [EVT_TYPE] == EVT_QUIT {halt}
    }
}

deniskolsanov
Posts: 8
Joined: Thu Dec 20, 2012 4:19 pm

Re: Multitouch (up to 3 fingers)

Post by deniskolsanov » Thu Dec 20, 2012 4:27 pm

В моей версии рисовать линиями, тестил, все работает:

Code: Select all

x = new(6)
y = new(6)
xsize = get_xsize(0)
ysize = get_ysize(0)
while(1) {
    frame()
    while(get_event()) {
            if EVT [EVT_TYPE] == EVT_QUIT {halt}
            ex = EVT [ EVT_X ]
            ey = EVT [ EVT_Y ]
            code = EVT [ EVT_SCANCODE ]
            if EVT [EVT_TYPE] == (EVT_MOUSEBUTTONDOWN) | EVT [EVT_TYPE] == (EVT_TOUCHBEGIN) {
                  x[ code ] = ex
                  y[ code ] = ey
            }
            line( ex, ey, x[ code ], y[ code ], #ffffff)
            x[ code ] = ex
            y[ code] = ey
    }
}

Post Reply