2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** input-binding.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
|
|
|
** Copyright (C) 2013 Jonas Kulla <Nyocurio@gmail.com>
|
|
|
|
**
|
|
|
|
** mkxp is free software: you can redistribute it and/or modify
|
|
|
|
** it under the terms of the GNU General Public License as published by
|
|
|
|
** the Free Software Foundation, either version 2 of the License, or
|
|
|
|
** (at your option) any later version.
|
|
|
|
**
|
|
|
|
** mkxp is distributed in the hope that it will be useful,
|
|
|
|
** but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
** GNU General Public License for more details.
|
|
|
|
**
|
|
|
|
** You should have received a copy of the GNU General Public License
|
|
|
|
** along with mkxp. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "input.h"
|
2013-10-09 10:30:33 +00:00
|
|
|
#include "sharedstate.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
#include "exception.h"
|
|
|
|
#include "binding-util.h"
|
2014-08-07 15:13:10 +00:00
|
|
|
#include "util.h"
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
RB_METHOD(inputUpdate)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->input().update();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-08-07 15:13:10 +00:00
|
|
|
static int getButtonArg(int argc, VALUE *argv)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
int num;
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer >= 3)
|
|
|
|
{
|
|
|
|
ID sym;
|
|
|
|
rb_get_args(argc, argv, "n", &sym RB_ARG_END);
|
2014-02-03 14:04:57 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
VALUE symHash = getRbData()->buttoncodeHash;
|
|
|
|
num = FIX2INT(rb_hash_lookup2(symHash, ID2SYM(sym), INT2FIX(Input::None)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rb_get_args(argc, argv, "i", &num RB_ARG_END);
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return num;
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
RB_METHOD(inputPress)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-07 15:13:10 +00:00
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
int num = getButtonArg(argc, argv);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return rb_bool_new(shState->input().isPressed(num));
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
RB_METHOD(inputTrigger)
|
|
|
|
{
|
2014-08-07 15:13:10 +00:00
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
int num = getButtonArg(argc, argv);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return rb_bool_new(shState->input().isTriggered(num));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(inputRepeat)
|
|
|
|
{
|
2014-08-07 15:13:10 +00:00
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
int num = getButtonArg(argc, argv);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return rb_bool_new(shState->input().isRepeated(num));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(inputDir4)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return rb_fix_new(shState->input().dir4Value());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(inputDir8)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return rb_fix_new(shState->input().dir8Value());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-standard extensions */
|
|
|
|
RB_METHOD(inputMouseX)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return rb_fix_new(shState->input().mouseX());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RB_METHOD(inputMouseY)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return rb_fix_new(shState->input().mouseY());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-07 15:13:10 +00:00
|
|
|
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
Input::ButtonCode val;
|
|
|
|
}
|
|
|
|
static buttonCodes[] =
|
|
|
|
{
|
|
|
|
{ "DOWN", Input::Down },
|
|
|
|
{ "LEFT", Input::Left },
|
|
|
|
{ "RIGHT", Input::Right },
|
|
|
|
{ "UP", Input::Up },
|
|
|
|
|
|
|
|
{ "A", Input::A },
|
|
|
|
{ "B", Input::B },
|
|
|
|
{ "C", Input::C },
|
|
|
|
{ "X", Input::X },
|
|
|
|
{ "Y", Input::Y },
|
|
|
|
{ "Z", Input::Z },
|
|
|
|
{ "L", Input::L },
|
|
|
|
{ "R", Input::R },
|
|
|
|
|
|
|
|
{ "SHIFT", Input::Shift },
|
|
|
|
{ "CTRL", Input::Ctrl },
|
|
|
|
{ "ALT", Input::Alt },
|
|
|
|
|
|
|
|
{ "F5", Input::F5 },
|
|
|
|
{ "F6", Input::F6 },
|
|
|
|
{ "F7", Input::F7 },
|
|
|
|
{ "F8", Input::F8 },
|
|
|
|
{ "F9", Input::F9 },
|
|
|
|
|
|
|
|
{ "MOUSELEFT", Input::MouseLeft },
|
|
|
|
{ "MOUSEMIDDLE", Input::MouseMiddle },
|
|
|
|
{ "MOUSERIGHT", Input::MouseRight }
|
|
|
|
};
|
|
|
|
|
|
|
|
static elementsN(buttonCodes);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
inputBindingInit()
|
|
|
|
{
|
|
|
|
VALUE module = rb_define_module("Input");
|
|
|
|
|
|
|
|
_rb_define_module_function(module, "update", inputUpdate);
|
|
|
|
_rb_define_module_function(module, "press?", inputPress);
|
|
|
|
_rb_define_module_function(module, "trigger?", inputTrigger);
|
|
|
|
_rb_define_module_function(module, "repeat?", inputRepeat);
|
|
|
|
_rb_define_module_function(module, "dir4", inputDir4);
|
|
|
|
_rb_define_module_function(module, "dir8", inputDir8);
|
|
|
|
|
|
|
|
_rb_define_module_function(module, "mouse_x", inputMouseX);
|
|
|
|
_rb_define_module_function(module, "mouse_y", inputMouseY);
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer >= 3)
|
2014-08-07 15:13:10 +00:00
|
|
|
{
|
2014-08-28 21:11:10 +00:00
|
|
|
VALUE symHash = rb_hash_new();
|
2014-08-07 15:13:10 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
for (size_t i = 0; i < buttonCodesN; ++i)
|
|
|
|
{
|
|
|
|
ID sym = rb_intern(buttonCodes[i].str);
|
|
|
|
VALUE val = INT2FIX(buttonCodes[i].val);
|
2014-08-07 15:13:10 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
/* In RGSS3 all Input::XYZ constants are equal to :XYZ symbols,
|
|
|
|
* to be compatible with the previous convention */
|
|
|
|
rb_const_set(module, sym, ID2SYM(sym));
|
|
|
|
rb_hash_aset(symHash, ID2SYM(sym), val);
|
|
|
|
}
|
2014-08-07 15:13:10 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
rb_iv_set(module, "buttoncodes", symHash);
|
|
|
|
getRbData()->buttoncodeHash = symHash;
|
2014-08-07 15:13:10 +00:00
|
|
|
}
|
2014-08-28 21:11:10 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < buttonCodesN; ++i)
|
|
|
|
{
|
|
|
|
ID sym = rb_intern(buttonCodes[i].str);
|
|
|
|
VALUE val = INT2FIX(buttonCodes[i].val);
|
2014-08-07 15:13:10 +00:00
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
rb_const_set(module, sym, val);
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|