2013-09-01 14:27:21 +00:00
|
|
|
/*
|
|
|
|
** input-binding.cpp
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
2021-09-06 18:50:44 +00:00
|
|
|
** Copyright (C) 2021 Amaryllis Kulla <amaryllis.kulla@protonmail.com>
|
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"
|
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-09 17:15:54 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
#include <mruby/hash.h>
|
|
|
|
#include <string.h>
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2013-09-03 09:06:01 +00:00
|
|
|
MRB_FUNCTION(inputUpdate)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:19:18 +00:00
|
|
|
MRB_FUN_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
shState->input().update();
|
2013-09-01 14:27:21 +00:00
|
|
|
|
|
|
|
return mrb_nil_value();
|
|
|
|
}
|
|
|
|
|
2014-08-09 17:15:54 +00:00
|
|
|
static mrb_int getButtonArg(mrb_state *mrb)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
|
|
|
mrb_int num;
|
2014-12-09 03:21:48 +00:00
|
|
|
mrb_value arg;
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-12-09 03:21:48 +00:00
|
|
|
mrb_get_args(mrb, "o", &arg);
|
2014-08-28 21:11:10 +00:00
|
|
|
|
2014-12-09 03:21:48 +00:00
|
|
|
if (mrb_fixnum_p(arg))
|
|
|
|
{
|
|
|
|
num = mrb_fixnum(arg);
|
|
|
|
}
|
|
|
|
else if (mrb_symbol_p(arg) && rgssVer >= 3)
|
|
|
|
{
|
2014-08-28 21:11:10 +00:00
|
|
|
mrb_value symHash = getMrbData(mrb)->buttoncodeHash;
|
2014-12-09 03:21:48 +00:00
|
|
|
mrb_value numVal = mrb_hash_fetch(mrb, symHash, arg,
|
2014-08-28 21:11:10 +00:00
|
|
|
mrb_fixnum_value(Input::None));
|
|
|
|
num = mrb_fixnum(numVal);
|
|
|
|
}
|
|
|
|
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
|
|
|
MRB_METHOD(inputPress)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-09 17:15:54 +00:00
|
|
|
MRB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
mrb_int num = getButtonArg(mrb);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return mrb_bool_value(shState->input().isPressed(num));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
MRB_METHOD(inputTrigger)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2014-08-09 17:15:54 +00:00
|
|
|
MRB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
mrb_int num = getButtonArg(mrb);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return mrb_bool_value(shState->input().isTriggered(num));
|
|
|
|
}
|
|
|
|
|
|
|
|
MRB_METHOD(inputRepeat)
|
|
|
|
{
|
2014-08-09 17:15:54 +00:00
|
|
|
MRB_UNUSED_PARAM;
|
|
|
|
|
|
|
|
mrb_int num = getButtonArg(mrb);
|
2013-09-01 14:27:21 +00:00
|
|
|
|
2014-02-03 14:04:57 +00:00
|
|
|
return mrb_bool_value(shState->input().isRepeated(num));
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 09:06:01 +00:00
|
|
|
MRB_FUNCTION(inputDir4)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:19:18 +00:00
|
|
|
MRB_FUN_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return mrb_fixnum_value(shState->input().dir4Value());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 09:06:01 +00:00
|
|
|
MRB_FUNCTION(inputDir8)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:19:18 +00:00
|
|
|
MRB_FUN_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return mrb_fixnum_value(shState->input().dir8Value());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Non-standard extensions */
|
2013-09-03 09:06:01 +00:00
|
|
|
MRB_FUNCTION(inputMouseX)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:19:18 +00:00
|
|
|
MRB_FUN_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return mrb_fixnum_value(shState->input().mouseX());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2013-09-03 09:06:01 +00:00
|
|
|
MRB_FUNCTION(inputMouseY)
|
2013-09-01 14:27:21 +00:00
|
|
|
{
|
2013-09-03 13:19:18 +00:00
|
|
|
MRB_FUN_UNUSED_PARAM;
|
|
|
|
|
2013-10-09 10:30:33 +00:00
|
|
|
return mrb_fixnum_value(shState->input().mouseY());
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-09 17:15:54 +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(mrb_state *mrb)
|
|
|
|
{
|
|
|
|
RClass *module = mrb_define_module(mrb, "Input");
|
|
|
|
|
|
|
|
mrb_define_module_function(mrb, module, "update", inputUpdate, MRB_ARGS_NONE());
|
|
|
|
mrb_define_module_function(mrb, module, "press?", inputPress, MRB_ARGS_REQ(1));
|
|
|
|
mrb_define_module_function(mrb, module, "trigger?", inputTrigger, MRB_ARGS_REQ(1));
|
|
|
|
mrb_define_module_function(mrb, module, "repeat?", inputRepeat, MRB_ARGS_REQ(1));
|
|
|
|
mrb_define_module_function(mrb, module, "dir4", inputDir4, MRB_ARGS_NONE());
|
|
|
|
mrb_define_module_function(mrb, module, "dir8", inputDir8, MRB_ARGS_NONE());
|
|
|
|
|
|
|
|
mrb_define_module_function(mrb, module, "mouse_x", inputMouseX, MRB_ARGS_NONE());
|
|
|
|
mrb_define_module_function(mrb, module, "mouse_y", inputMouseY, MRB_ARGS_NONE());
|
|
|
|
|
2014-08-09 17:15:54 +00:00
|
|
|
mrb_value modVal = mrb_obj_value(module);
|
|
|
|
|
2014-08-28 21:11:10 +00:00
|
|
|
if (rgssVer >= 3)
|
2014-08-09 17:15:54 +00:00
|
|
|
{
|
2014-08-28 21:11:10 +00:00
|
|
|
mrb_value symHash = mrb_hash_new_capa(mrb, buttonCodesN);
|
|
|
|
|
|
|
|
for (size_t i = 0; i < buttonCodesN; ++i)
|
|
|
|
{
|
|
|
|
const char *str = buttonCodes[i].str;
|
|
|
|
mrb_sym sym = mrb_intern_static(mrb, str, strlen(str));
|
|
|
|
mrb_value symVal = mrb_symbol_value(sym);
|
|
|
|
mrb_value val = mrb_fixnum_value(buttonCodes[i].val);
|
|
|
|
|
|
|
|
/* In RGSS3 all Input::XYZ constants are equal to :XYZ symbols,
|
|
|
|
* to be compatible with the previous convention */
|
|
|
|
mrb_const_set(mrb, modVal, sym, symVal);
|
|
|
|
mrb_hash_set(mrb, symHash, symVal, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
mrb_iv_set(mrb, modVal, mrb_intern_lit(mrb, "buttoncodes"), symHash);
|
|
|
|
getMrbData(mrb)->buttoncodeHash = symHash;
|
2014-08-09 17:15:54 +00:00
|
|
|
}
|
2014-08-28 21:11:10 +00:00
|
|
|
else
|
2014-08-09 17:15:54 +00:00
|
|
|
{
|
2014-08-28 21:11:10 +00:00
|
|
|
for (size_t i = 0; i < buttonCodesN; ++i)
|
|
|
|
{
|
|
|
|
const char *str = buttonCodes[i].str;
|
|
|
|
mrb_sym sym = mrb_intern_static(mrb, str, strlen(str));
|
|
|
|
mrb_value val = mrb_fixnum_value(buttonCodes[i].val);
|
|
|
|
|
|
|
|
mrb_const_set(mrb, modVal, sym, val);
|
|
|
|
}
|
2014-08-09 17:15:54 +00:00
|
|
|
}
|
2013-09-01 14:27:21 +00:00
|
|
|
}
|