Short Click Long Click = different mouse.modes

Started by Miori, Thu 14/03/2013 11:47:20

Previous topic - Next topic

Miori

Hello =)

I have a problem I hope you can help me with. Its about mouse control. Normally I use left click for looking at objects and right click for interacting. Now I want to add an option to change control to touchscreen controls, because a touchscreen doesn't have right or left click, just... click XD So I thought about a solution and found one but... I already tried for some time but didn't make it to work.

So here's the problem: The player should click normally for looking at an object. If he holds the click for around 2 or 3 seconds and releases then he should be interacting with it... but somehow it doesn't want to work =/

Crimson Wizard

#1
Quote from: Miori on Thu 14/03/2013 11:47:20
So here's the problem: The player should click normally for looking at an object. If he holds the click for around 2 or 3 seconds and releases then he should be interacting with it... but somehow it doesn't want to work =/
Can you show a script, please? It is difficult to help person when you don't know what he did.

Basically, you should use Mouse.IsButtonDown to learn if mouse button is still pressed.
Here's the example from the manual (it's a bit silly, I must admit, but shows the general idea):
Code: ags

int timer=0;
function repeatedly_execute()
{
  if (mouse.IsButtonDown(eMouseRight)) {
   if (timer == 40) {
     Display("You pressed the right button for 1 sec");
     timer = 0;
   }
   else {
     timer++;
   }
  }
}

Miori

Well I tried to solve it with a timer, really nooby I guess XD. The main problem is, that I don't have a clue how to tell ags "when release mouse".

Code: AGS

if (mouse.IsButtonDown(eMouseLeft)) 
  {	SetTimer(20, 80);
if (IsTimerExpired(20)){
mouse.Mode = eModeInteract;
} else{	
	mouse.Mode = eModeLookat;			
}	
} else {
	ProcessClick(mouse.x,mouse.y, mouse.Mode);	
}

Crimson Wizard

#3
I'd recommend you to not change cursor mode like that, this may cause weird behavior.
Instead, call ProcessClick, passing mode explicitly.

This is my variant. Sorry, can't test it in real game right now.
Code: ags

bool mouse_is_down = false;
int mouse_press_timer = 0;

function repeatedly_execute()
{
  bool must_process_click = false;
  CursorMode click_mode = eModeWalkto;

  if (mouse.IsButtonDown(eMouseLeft)) // mouse button is down
  {
     if (mouse_is_down) // button is still down
     {
        mouse_press_timer += 1; // increase tick counter
        if (mouse_press_timer > 80) // at least 2 seconds has passed
        {
          mouse_press_timer = 0;
          mouse_is_down = false;
          must_process_click = true; // should process interaction
          click_mode = eModeInteract;
        }
     }
     else // button was not down, but now it is - restart counter
     {
        mouse_press_timer = 0;
        mouse_is_down = true;
     }
  }
  else // mouse button is NOT down
  {
     if (mouse_is_down) // but it was down before!
     {
        // should process look at
        mouse_press_timer = 0;
        mouse_is_down = false;
        must_process_click = true; // should process interaction
        click_mode = eModeLookat;
     }
  }

  if (must_process_click)
  {
    ProcessClick(mouse.x, mouse.y, click_mode);
  }
}




EDIT: I keep forgetting AGS does not have "++" operator. Fixed script.

Khris

Maybe because AGS supports using ++ and --?

What it doesn't support is *= and /=.

Crimson Wizard

Quote from: Khris on Thu 14/03/2013 13:02:19
Maybe because AGS supports using ++ and --?
Okay, now I am totally confused. I could swear it does not work...  (wtf).

Miori

I tried it out. It works, that the action just runs after the mouse is released, but changing the mode seems not to work. =O

Crimson Wizard

#7
Quote from: Miori on Thu 14/03/2013 13:44:48
I tried it out. It works, that the action just runs after the mouse is released, but changing the mode seems not to work. =O
Yes, this is how I wrote that script. So, you need to change cursor look too?
I am not sure it is a good idea for touchscreen controls to have different cursor modes, unless you can toggle them some other way, than different touches.
How many possible actions does your game require? Perhaps it should use just one generic mode all the time?

PS. If you really want the mode change, then replace "click_mode = something;" with "mouse.Mode = something;" and "ProcessClick(mouse.x, mouse.y, click_mode);" with "ProcessClick(mouse.x, mouse.y, mouse.Mode);"

PPS. Actually, thinking more on this, isn't the mouse cursor is supposed to be hidden when touchscreen controls are active?

SMF spam blocked by CleanTalk