mkxp/binding-mri/input-binding.cpp

201 lines
4.2 KiB
C++
Raw Normal View History

2013-09-01 14:27:21 +00:00
/*
** input-binding.cpp
**
** This file is part of mkxp.
**
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
2013-09-01 14:27:21 +00:00
**
** 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"
#include "sharedstate.h"
2013-09-01 14:27:21 +00:00
#include "exception.h"
#include "binding-util.h"
#include "util.h"
2013-09-01 14:27:21 +00:00
RB_METHOD(inputUpdate)
{
RB_UNUSED_PARAM;
shState->input().update();
2013-09-01 14:27:21 +00:00
return Qnil;
}
static int getButtonArg(int argc, VALUE *argv)
2013-09-01 14:27:21 +00:00
{
int num;
rb_check_argc(argc, 1);
if (FIXNUM_P(argv[0]))
{
num = FIX2INT(argv[0]);
}
else if (SYMBOL_P(argv[0]) && rgssVer >= 3)
{
VALUE symHash = getRbData()->buttoncodeHash;
num = FIX2INT(rb_hash_lookup2(symHash, argv[0], INT2FIX(Input::None)));
}
else
{
// FIXME: RMXP allows only few more types that
// don't make sense (symbols in pre 3, floats)
num = 0;
}
2013-09-01 14:27:21 +00:00
return num;
2013-09-01 14:27:21 +00:00
}
RB_METHOD(inputPress)
2013-09-01 14:27:21 +00:00
{
RB_UNUSED_PARAM;
int num = getButtonArg(argc, argv);
2013-09-01 14:27:21 +00:00
return rb_bool_new(shState->input().isPressed(num));
}
2013-09-01 14:27:21 +00:00
RB_METHOD(inputTrigger)
{
RB_UNUSED_PARAM;
int num = getButtonArg(argc, argv);
2013-09-01 14:27:21 +00:00
return rb_bool_new(shState->input().isTriggered(num));
2013-09-01 14:27:21 +00:00
}
RB_METHOD(inputRepeat)
{
RB_UNUSED_PARAM;
int num = getButtonArg(argc, argv);
2013-09-01 14:27:21 +00:00
return rb_bool_new(shState->input().isRepeated(num));
2013-09-01 14:27:21 +00:00
}
RB_METHOD(inputDir4)
{
RB_UNUSED_PARAM;
return rb_fix_new(shState->input().dir4Value());
2013-09-01 14:27:21 +00:00
}
RB_METHOD(inputDir8)
{
RB_UNUSED_PARAM;
return rb_fix_new(shState->input().dir8Value());
2013-09-01 14:27:21 +00:00
}
/* Non-standard extensions */
RB_METHOD(inputMouseX)
{
RB_UNUSED_PARAM;
return rb_fix_new(shState->input().mouseX());
2013-09-01 14:27:21 +00:00
}
RB_METHOD(inputMouseY)
{
RB_UNUSED_PARAM;
return rb_fix_new(shState->input().mouseY());
2013-09-01 14:27:21 +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);
if (rgssVer >= 3)
{
VALUE symHash = rb_hash_new();
for (size_t i = 0; i < buttonCodesN; ++i)
{
ID sym = rb_intern(buttonCodes[i].str);
VALUE val = INT2FIX(buttonCodes[i].val);
/* 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);
}
rb_iv_set(module, "buttoncodes", symHash);
getRbData()->buttoncodeHash = symHash;
}
else
{
for (size_t i = 0; i < buttonCodesN; ++i)
{
ID sym = rb_intern(buttonCodes[i].str);
VALUE val = INT2FIX(buttonCodes[i].val);
rb_const_set(module, sym, val);
}
}
2013-09-01 14:27:21 +00:00
}