mkxp/binding-mruby/viewport-binding.cpp
Jonas Kulla e858bbdcf5 Lift 'Disposable' concept from core into bindings
Instead of replicating the RGSS Disposable interface in C++
and merely binding it, redefine the 'disposed' state as the
entire core object being deleted (and the binding object's
private pointer being null).

This makes the behavior more accurate in regard to RMXP.
It is now for example possible to subclass disposable classes
and access their 'dispose'/'disposed?' methods without
initializing the base class first (because the internal pointer
is simply null before initialization). Accessing any other
base methods will still raise an exception.

There are some quirks and irregular behavior in RMXP; eg.
most nullable bitmap attributes of disposable classes
(Sprite, Plane etc.) can still be queried afterwards, but
some cannot (Tilemap#tileset), and disposing certain
attributes crashes RMXP entirely (Tilemap#autotiles[n]).
mkxp tries to behave as close possible, but will be more
lenient some circumstances.

To the core, disposed bitmap attributes will look
identically to null, which slightly diverges from RMXP
(where they're treated as still existing, but aren't drawn).
The Disposable interface has been retained containing a
single signal, for the binding to inform core when
objects are disposed (so active attributes can be set to null).
2014-08-09 21:21:38 +02:00

97 lines
2.5 KiB
C++

/*
** viewport-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 "viewport.h"
#include "disposable-binding.h"
#include "flashable-binding.h"
#include "sceneelement-binding.h"
#include "binding-util.h"
#include "binding-types.h"
DEF_TYPE(Viewport);
MRB_METHOD(viewportInitialize)
{
Viewport *v;
if (mrb->c->ci->argc == 1)
{
/* The rect arg is only used to init the viewport,
* and does NOT replace its 'rect' property */
mrb_value rectObj;
Rect *rect;
mrb_get_args(mrb, "o", &rectObj);
rect = getPrivateDataCheck<Rect>(mrb, rectObj, RectType);
v = new Viewport(rect);
}
else
{
mrb_int x, y, width, height;
mrb_get_args(mrb, "iiii", &x, &y, &width, &height);
v = new Viewport(x, y, width, height);
}
setPrivateData(self, v, ViewportType);
/* Wrap property objects */
v->setRect(new Rect(*v->getRect()));
v->setColor(new Color);
v->setTone(new Tone);
wrapProperty(mrb, self, v->getRect(), CSrect, RectType);
wrapProperty(mrb, self, v->getColor(), CScolor, ColorType);
wrapProperty(mrb, self, v->getTone(), CStone, ToneType);
return self;
}
DEF_PROP_OBJ(Viewport, Rect, Rect, CSrect)
DEF_PROP_OBJ(Viewport, Color, Color, CScolor)
DEF_PROP_OBJ(Viewport, Tone, Tone, CStone)
DEF_PROP_I(Viewport, OX)
DEF_PROP_I(Viewport, OY)
void
viewportBindingInit(mrb_state *mrb)
{
RClass *klass = defineClass(mrb, "Viewport");
disposableBindingInit <Viewport>(mrb, klass);
flashableBindingInit <Viewport>(mrb, klass);
sceneElementBindingInit<Viewport>(mrb, klass);
mrb_define_method(mrb, klass, "initialize", viewportInitialize, MRB_ARGS_REQ(1) | MRB_ARGS_OPT(3));
INIT_PROP_BIND( Viewport, Rect, "rect" );
INIT_PROP_BIND( Viewport, OX, "ox" );
INIT_PROP_BIND( Viewport, OY, "oy" );
INIT_PROP_BIND( Viewport, Color, "color" );
INIT_PROP_BIND( Viewport, Tone, "tone" );
mrb_define_method(mrb, klass, "inspect", inspectObject, MRB_ARGS_NONE());
}