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-12-09 03:21:48 +00:00
|
|
|
rb_check_argc(argc, 1);
|
2014-02-03 14:04:57 +00:00
|
|
|
|
2014-12-09 03:21:48 +00:00
|
|
|
if (FIXNUM_P(argv[0]))
|
|
|
|
{
|
|
|
|
num = FIX2INT(argv[0]);
|
|
|
|
}
|
|
|
|
else if (SYMBOL_P(argv[0]) && rgssVer >= 3)
|
|
|
|
{
|
2014-08-28 21:11:10 +00:00
|
|
|
VALUE symHash = getRbData()->buttoncodeHash;
|
2014-12-09 03:21:48 +00:00
|
|
|
num = FIX2INT(rb_hash_lookup2(symHash, argv[0], INT2FIX(Input::None)));
|
2014-08-28 21:11:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-12-09 03:21:48 +00:00
|
|
|
// FIXME: RMXP allows only few more types that
|
|
|
|
// don't make sense (symbols in pre 3, floats)
|
|
|
|
num = 0;
|
2014-08-28 21:11:10 +00:00
|
|
|
}
|
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[] =
|
|
|
|
{
|
2015-09-04 02:47:43 +00:00
|
|
|
{ "DOWN", Input::Down },
|
|
|
|
{ "LEFT", Input::Left },
|
|
|
|
{ "RIGHT", Input::Right },
|
|
|
|
{ "UP", Input::Up },
|
|
|
|
|
|
|
|
{ "ACTION", Input::Action },
|
|
|
|
{ "CANCEL", Input::Cancel },
|
|
|
|
{ "MENU", Input::Menu },
|
|
|
|
{ "ITEMS", Input::Items },
|
|
|
|
{ "RUN", Input::Run },
|
|
|
|
{ "DEACTIVATE", Input::Deactivate },
|
|
|
|
|
|
|
|
{ "L", Input::L },
|
|
|
|
{ "R", Input::R },
|
|
|
|
|
|
|
|
{ "F5", Input::F5 },
|
|
|
|
{ "F6", Input::F6 },
|
|
|
|
{ "F7", Input::F7 },
|
|
|
|
{ "F8", Input::F8 },
|
|
|
|
{ "F9", Input::F9 },
|
2014-08-07 15:13:10 +00:00
|
|
|
|
|
|
|
{ "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
|
|
|
}
|