2013-09-01 16:27:21 +02:00
|
|
|
/*
|
|
|
|
** disposable-binding.h
|
|
|
|
**
|
|
|
|
** This file is part of mkxp.
|
|
|
|
**
|
2023-10-04 21:07:34 +02:00
|
|
|
** Copyright (C) 2013 - 2021 Amaryllis Kulla <ancurio@mapleshrine.eu>
|
2013-09-01 16:27:21 +02: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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DISPOSABLEBINDING_H
|
|
|
|
#define DISPOSABLEBINDING_H
|
|
|
|
|
|
|
|
#include "disposable.h"
|
|
|
|
#include "binding-util.h"
|
2021-06-04 14:29:45 -04:00
|
|
|
#include "graphics.h"
|
2013-09-01 16:27:21 +02:00
|
|
|
|
2014-08-09 18:35:01 +02:00
|
|
|
/* 'Children' are disposables that are disposed together
|
|
|
|
* with their parent. Currently this is only used by Viewport
|
2014-10-24 18:55:03 +02:00
|
|
|
* in RGSS1. */
|
2014-08-09 18:35:01 +02:00
|
|
|
inline void
|
|
|
|
disposableAddChild(VALUE disp, VALUE child)
|
|
|
|
{
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_LOCK;
|
2021-02-22 01:22:26 -05:00
|
|
|
if (NIL_P(disp) || NIL_P(child)) {
|
2021-02-10 00:04:34 -05:00
|
|
|
return;
|
2021-02-22 01:22:26 -05:00
|
|
|
}
|
2021-02-10 00:04:34 -05:00
|
|
|
|
2023-05-03 23:00:06 -04:00
|
|
|
VALUE objID = rb_obj_id(child);
|
|
|
|
|
2014-08-09 18:35:01 +02:00
|
|
|
VALUE children = rb_iv_get(disp, "children");
|
2021-02-10 00:04:34 -05:00
|
|
|
|
|
|
|
bool exists = false;
|
2014-08-09 18:35:01 +02:00
|
|
|
|
|
|
|
if (NIL_P(children))
|
|
|
|
{
|
|
|
|
children = rb_ary_new();
|
|
|
|
rb_iv_set(disp, "children", children);
|
|
|
|
}
|
2021-02-10 00:04:34 -05:00
|
|
|
else {
|
2023-05-03 23:00:06 -04:00
|
|
|
exists = RTEST(rb_funcall(children, rb_intern("include?"), 1, objID));
|
2021-02-10 00:04:34 -05:00
|
|
|
}
|
2014-08-09 18:35:01 +02:00
|
|
|
|
2023-05-08 23:38:54 -04:00
|
|
|
if (!exists) {
|
2023-05-03 23:00:06 -04:00
|
|
|
rb_ary_push(children, objID);
|
2023-05-08 23:38:54 -04:00
|
|
|
VALUE objectspace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
|
|
|
|
VALUE method = rb_funcall(disp, rb_intern("method"), 1, rb_id2sym(rb_intern("_sprite_finalizer")));
|
|
|
|
rb_funcall(objectspace, rb_intern("define_finalizer"), 2, child, method);
|
|
|
|
}
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_UNLOCK;
|
2021-02-10 00:04:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void
|
|
|
|
disposableRemoveChild(VALUE disp, VALUE child)
|
|
|
|
{
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_LOCK;
|
2021-02-22 01:22:26 -05:00
|
|
|
if (NIL_P(disp) || NIL_P(child)) {
|
2021-02-10 00:04:34 -05:00
|
|
|
return;
|
2021-02-22 01:22:26 -05:00
|
|
|
}
|
2021-02-10 00:04:34 -05:00
|
|
|
|
2023-05-03 23:00:06 -04:00
|
|
|
VALUE objID = rb_obj_id(child);
|
|
|
|
|
2021-02-10 00:04:34 -05:00
|
|
|
VALUE children = rb_iv_get(disp, "children");
|
|
|
|
if (NIL_P(children))
|
|
|
|
return;
|
|
|
|
|
2023-05-03 23:00:06 -04:00
|
|
|
VALUE index = rb_funcall(children, rb_intern("index"), 1, objID);
|
2021-02-10 00:04:34 -05:00
|
|
|
if (NIL_P(index))
|
|
|
|
return;
|
|
|
|
|
|
|
|
rb_funcall(children, rb_intern("delete_at"), 1, index);
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_UNLOCK;
|
2014-08-09 18:35:01 +02:00
|
|
|
}
|
|
|
|
|
2023-05-08 23:38:54 -04:00
|
|
|
inline void
|
|
|
|
disposableForgetChild(VALUE disp, VALUE child)
|
|
|
|
{
|
|
|
|
VALUE children = rb_iv_get(disp, "children");
|
|
|
|
|
|
|
|
if (NIL_P(children)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE index = rb_funcall(children, rb_intern("index"), 1, child);
|
|
|
|
if (NIL_P(index)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_funcall(children, rb_intern("delete_at"), 1, index);
|
|
|
|
}
|
|
|
|
|
2014-08-09 18:35:01 +02:00
|
|
|
inline void
|
|
|
|
disposableDisposeChildren(VALUE disp)
|
|
|
|
{
|
|
|
|
VALUE children = rb_iv_get(disp, "children");
|
|
|
|
|
|
|
|
if (NIL_P(children))
|
|
|
|
return;
|
|
|
|
|
2023-05-03 23:00:06 -04:00
|
|
|
for (long i = 0; i < RARRAY_LEN(children); ++i) {
|
|
|
|
int state;
|
|
|
|
rb_protect([](VALUE args){
|
|
|
|
VALUE objectspace = rb_const_get(rb_cObject, rb_intern("ObjectSpace"));
|
|
|
|
VALUE ref = rb_funcall(objectspace, rb_intern("_id2ref"), 1, args);
|
|
|
|
rb_funcall(ref, rb_intern("_mkxp_dispose_alias"), 0);
|
|
|
|
return Qnil;
|
|
|
|
}, rb_ary_entry(children, i), &state);
|
|
|
|
}
|
|
|
|
//rb_funcall2(rb_ary_entry(children, i), dispFun, 0, 0);
|
2014-08-09 18:35:01 +02:00
|
|
|
}
|
|
|
|
|
2013-09-01 16:27:21 +02:00
|
|
|
template<class C>
|
|
|
|
RB_METHOD(disposableDispose)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2014-09-23 21:12:58 +02:00
|
|
|
C *d = getPrivateData<C>(self);
|
|
|
|
|
|
|
|
if (!d)
|
|
|
|
return Qnil;
|
2014-08-09 18:35:01 +02:00
|
|
|
|
|
|
|
/* Nothing to do if already disposed */
|
2014-09-23 21:12:58 +02:00
|
|
|
if (d->isDisposed())
|
2014-08-09 18:35:01 +02:00
|
|
|
return Qnil;
|
|
|
|
|
2014-10-24 18:55:03 +02:00
|
|
|
if (rgssVer == 1)
|
|
|
|
disposableDisposeChildren(self);
|
2014-08-09 18:35:01 +02:00
|
|
|
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_LOCK;
|
2014-09-23 21:12:58 +02:00
|
|
|
d->dispose();
|
2021-06-04 14:29:45 -04:00
|
|
|
GFX_UNLOCK;
|
2013-09-01 16:27:21 +02:00
|
|
|
|
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
2014-09-23 21:12:58 +02:00
|
|
|
template<class C>
|
2013-09-01 16:27:21 +02:00
|
|
|
RB_METHOD(disposableIsDisposed)
|
|
|
|
{
|
|
|
|
RB_UNUSED_PARAM;
|
|
|
|
|
2014-09-23 21:12:58 +02:00
|
|
|
C *d = getPrivateData<C>(self);
|
|
|
|
|
|
|
|
if (!d)
|
|
|
|
return Qtrue;
|
|
|
|
|
|
|
|
return rb_bool_new(d->isDisposed());
|
2013-09-01 16:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
static void disposableBindingInit(VALUE klass)
|
|
|
|
{
|
|
|
|
_rb_define_method(klass, "dispose", disposableDispose<C>);
|
2014-09-23 21:12:58 +02:00
|
|
|
_rb_define_method(klass, "disposed?", disposableIsDisposed<C>);
|
2014-10-24 18:29:08 +02:00
|
|
|
|
|
|
|
/* Make sure we always have access to the original method, even
|
|
|
|
* if it is overridden by user scripts */
|
2014-10-24 18:55:03 +02:00
|
|
|
if (rgssVer == 1)
|
|
|
|
rb_define_alias(klass, "_mkxp_dispose_alias", "dispose");
|
2014-09-23 21:12:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class C>
|
|
|
|
inline void
|
|
|
|
checkDisposed(VALUE self)
|
|
|
|
{
|
|
|
|
if (disposableIsDisposed<C>(0, 0, self) == Qtrue)
|
|
|
|
raiseDisposedAccess(self);
|
2013-09-01 16:27:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // DISPOSABLEBINDING_H
|