MouseButtonDown... possebly a module

Started by Wonkyth, Wed 03/06/2009 02:52:01

Previous topic - Next topic

Wonkyth

I probably would have posted this in the module section, but I couldn't find a "start new topic" button there for some reason.

Just recently I started transcribing a game I was working on in C# to AGS(for reasons not worth mentioning).
Unfortunately, my game used things like KeyDown and KeyUp, which AGS doesn't (currently) support.
So I started making a module to allow it.
what I have here is the mouse version, because I haven't yet finished the Keyboard one (it has allot more input to handle).

Code: ags

bool MouseLeftDown;
bool MouseRightDown;

function Mouse_Left_Down(){
  
}

function Mouse_Left_Up(){
  
}


function repeatedly_execute() {

  if (mouse.IsButtonDown(eMouseLeft)){
    if (MouseLeftDown==false){
      Mouse_Left_Down();
      MouseLeftDown = true;
    }
    
  }
  else if(MouseLeftDown==true){
  Mouse_Left_Up();
  MouseLeftDown = false;
  }
  
if (mouse.IsButtonDown(eMouseRight)){
    if (MouseRightDown==false){
      Mouse_Right_Down();
      MouseRightDown = true;
    }
    
  }
  else if(MouseRightDown==true){
  Mouse_Right_Up();
  MouseRightDown = false;
  }
  

}
 


Obviously this isn't the best way to go about it, because there is still a hell of a lot commands I don't understand.
So I was wondering if anyone could give me a few pointers as to how I might get this code closer to acceptable module code?

Anything I add to here I'll also implement in the Keyboard version.

thanks.
"But with a ninja on your face, you live longer!"

Khris

The mods will move your thread to the module forum.

Code: ags
// header

struct UpDown {
  import static bool MouseUp(MouseButton button);
  import static bool MouseDown(MouseButton button);
};


// script

bool _was_down[3];

static bool UpDown::MouseUp(MouseButton button) {
  if (!mouse.IsButtonDown(button) && _was_down[button]) _was_down[button] = false;
  return !_was_down[button];
}

static bool UpDown::MouseDown(MouseButton button) {
  if (mouse.IsButtonDown(button) && !_was_down[button]) _was_down[button] = true;
  return _was_down[button];
}

void repeatedly_execute_always() {
  _was_down[eMouseLeft] = mouse.IsButtonDown(eMouseLeft);
  _was_down[eMouseRight] = mouse.IsButtonDown(eMouseRight);
}


Usage: check UpDown.MouseUp/Down in rep_ex

You need the static keyword so you can use the structs name ("UpDown") directly instead of having to declare an instance of it. The functions need to be static, too. Their names have to begin with "[struct name]::" and they have to be imported inside the struct.

Wonkyth

#2
Hmm.
I understand most of this, but:
Code: ags

static bool UpDown::MouseUp(MouseButton button)

I'm not sure what's going on here, is this similar to an extender function?
I assume it's creating a function-like thing that belongs to the UpDown struct, and the MouseUp bit is the actual product.

Thanks KhrisMUC, even if I don't quite understand what's going on here, I'll replicate the same style in the keyboard version.
I'll probably be back with more questions/problems about this soon, but we'll see...


"But with a ninja on your face, you live longer!"

Khris

The syntax of a function declaration is this:
[static] return-type name([parameters]) { [body] }
return-type is usually "function", but that's just a substitute for "int".
I want to return a bool (true or false) though.

Wonkyth

Ok, I've had a little play around with this, and using this
Code: ags
 
if (UpDown.MouseDown(eMouseLeft)){
    player.Say("hi");
    }
  if (UpDown.MouseUp(eMouseLeft)){
    player.Say("bye");
    }

in the repeatedly_execute function, I get some undesired results.
What I get is the player saying "bye" all the time unless the left mouse button is down.
That shouldn't happen, it should say nothing unless the mouse has just been pressed, or the mouse has just been released.
Unless I'm not inputting correctly, then it isn't achieving the results that my original code did.

So, am I doing something wrong, or is it the code?
"But with a ninja on your face, you live longer!"

SMF spam blocked by CleanTalk