Input: Implement RGSS3 functionality in bindings

Ie. using symbols instead of Input:: constants to query
button states.
This commit is contained in:
Jonas Kulla 2014-02-03 15:04:57 +01:00
parent 5a6c0c14ed
commit 64f1e32fdc
4 changed files with 70 additions and 37 deletions

View File

@ -237,6 +237,24 @@ rb_get_args(int argc, VALUE *argv, const char *format, ...)
break; break;
} }
case 'n' :
{
if (argI >= argc)
break;
ID *sym = va_arg(ap, ID*);
VALUE symVal = *arg++;
if (!SYMBOL_P(symVal))
rb_raise(rb_eTypeError, "Argument %d: Expected symbol", argI);
*sym = SYM2ID(symVal);
++argI;
break;
}
case '|' : case '|' :
opt = true; opt = true;
break; break;

View File

@ -131,7 +131,7 @@ wrapNilProperty(VALUE self, const char *iv)
rb_iv_set(self, iv, Qnil); rb_iv_set(self, iv, Qnil);
} }
/* Implemented: oSszfib| */ /* Implemented: oSszfibn| */
int int
rb_get_args(int argc, VALUE *argv, const char *format, ...); rb_get_args(int argc, VALUE *argv, const char *format, ...);

View File

@ -33,40 +33,45 @@ RB_METHOD(inputUpdate)
return Qnil; return Qnil;
} }
static int getButtonArg(VALUE self, int argc, VALUE *argv)
{
int num;
#ifdef RGSS3
ID sym;
rb_get_args(argc, argv, "n", &sym RB_ARG_END);
if (rb_const_defined(self, sym))
num = FIX2INT(rb_const_get(self, sym));
else
num = 0;
#else
(void) self;
rb_get_args(argc, argv, "i", &num RB_ARG_END);
#endif
return num;
}
RB_METHOD(inputPress) RB_METHOD(inputPress)
{ {
RB_UNUSED_PARAM; int num = getButtonArg(self, argc, argv);
int num; return rb_bool_new(shState->input().isPressed(num));
rb_get_args(argc, argv, "i", &num RB_ARG_END);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(shState->input().isPressed(bc));
} }
RB_METHOD(inputTrigger) RB_METHOD(inputTrigger)
{ {
RB_UNUSED_PARAM; int num = getButtonArg(self, argc, argv);
int num; return rb_bool_new(shState->input().isTriggered(num));
rb_get_args(argc, argv, "i", &num RB_ARG_END);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(shState->input().isTriggered(bc));
} }
RB_METHOD(inputRepeat) RB_METHOD(inputRepeat)
{ {
RB_UNUSED_PARAM; int num = getButtonArg(self, argc, argv);
int num; return rb_bool_new(shState->input().isRepeated(num));
rb_get_args(argc, argv, "i", &num RB_ARG_END);
Input::ButtonCode bc = (Input::ButtonCode) num;
return rb_bool_new(shState->input().isRepeated(bc));
} }
RB_METHOD(inputDir4) RB_METHOD(inputDir4)

View File

@ -33,34 +33,44 @@ MRB_FUNCTION(inputUpdate)
return mrb_nil_value(); return mrb_nil_value();
} }
MRB_FUNCTION(inputPress) static mrb_int getButtonArg(mrb_state *mrb, mrb_value self)
{ {
mrb_int num; mrb_int num;
#ifdef RGSS3
mrb_sym sym;
mrb_get_args(mrb, "n", &sym);
if (mrb_const_defined(mrb, self, sym))
num = mrb_fixnum(mrb_const_get(mrb, self, sym));
else
num = 0;
#else
mrb_get_args(mrb, "i", &num); mrb_get_args(mrb, "i", &num);
#endif
Input::ButtonCode bc = (Input::ButtonCode) num; return num;
return mrb_bool_value(shState->input().isPressed(bc));
} }
MRB_FUNCTION(inputTrigger) MRB_METHOD(inputPress)
{ {
mrb_int num; mrb_int num = getButtonArg(mrb, self);
mrb_get_args(mrb, "i", &num);
Input::ButtonCode bc = (Input::ButtonCode) num; return mrb_bool_value(shState->input().isPressed(num));
return mrb_bool_value(shState->input().isTriggered(bc));
} }
MRB_FUNCTION(inputRepeat) MRB_METHOD(inputTrigger)
{ {
mrb_int num; mrb_int num = getButtonArg(mrb, self);
mrb_get_args(mrb, "i", &num);
Input::ButtonCode bc = (Input::ButtonCode) num; return mrb_bool_value(shState->input().isTriggered(num));
}
return mrb_bool_value(shState->input().isRepeated(bc)); MRB_METHOD(inputRepeat)
{
mrb_int num = getButtonArg(mrb, self);
return mrb_bool_value(shState->input().isRepeated(num));
} }
MRB_FUNCTION(inputDir4) MRB_FUNCTION(inputDir4)