Skip to content

Commit

Permalink
Per-object opacity
Browse files Browse the repository at this point in the history
initial push
  • Loading branch information
jcbk101 committed Aug 15, 2024
1 parent e42fb8b commit 24b42fb
Show file tree
Hide file tree
Showing 19 changed files with 145 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/libtiled/mapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ QVariant MapObject::mapObjectProperty(Property property) const
case PositionProperty: return mPos;
case SizeProperty: return mSize;
case RotationProperty: return mRotation;
// BONGO
case OpacityProperty: return mOpacity;
case CellProperty: Q_ASSERT(false); break;
case ShapeProperty: return mShape;
case TemplateProperty: Q_ASSERT(false); break;
Expand All @@ -334,6 +336,7 @@ void MapObject::setMapObjectProperty(Property property, const QVariant &value)
case PositionProperty: setPosition(value.toPointF()); break;
case SizeProperty: setSize(value.toSizeF()); break;
case RotationProperty: setRotation(value.toReal()); break;
case OpacityProperty: setOpacity(value.toReal()); break;
case CellProperty: Q_ASSERT(false); break;
case ShapeProperty: setShape(value.value<Shape>()); break;
case TemplateProperty: Q_ASSERT(false); break;
Expand Down Expand Up @@ -374,6 +377,8 @@ MapObject *MapObject::clone() const
o->setShape(mShape);
o->setCell(mCell);
o->setRotation(mRotation);
// BONGO
o->setOpacity(mOpacity);
o->setVisible(mVisible);
o->setChangedProperties(mChangedProperties);
o->setObjectTemplate(mObjectTemplate);
Expand All @@ -389,6 +394,8 @@ void MapObject::copyPropertiesFrom(const MapObject *object)
setShape(object->shape());
setCell(object->cell());
setRotation(object->rotation());
// BONGO
setOpacity(object->opacity());
setVisible(object->isVisible());
setProperties(object->properties());
setChangedProperties(object->changedProperties());
Expand Down Expand Up @@ -428,6 +435,10 @@ void MapObject::syncWithTemplate()
if (!propertyChanged(MapObject::RotationProperty))
setRotation(base->rotation());

// BONGO
if (!propertyChanged(MapObject::OpacityProperty))
setOpacity(base->opacity());

if (!propertyChanged(MapObject::VisibleProperty))
setVisible(base->isVisible());
}
Expand Down
22 changes: 22 additions & 0 deletions src/libtiled/mapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class TILEDSHARED_EXPORT MapObject : public Object
ShapeProperty = 1 << 11,
TemplateProperty = 1 << 12,
CustomProperties = 1 << 13,
// BONGO
OpacityProperty = 1 << 14,
AllProperties = 0xFF
};

Expand Down Expand Up @@ -200,6 +202,10 @@ class TILEDSHARED_EXPORT MapObject : public Object
qreal rotation() const;
void setRotation(qreal rotation);

// BONGO
qreal opacity() const;
void setOpacity(qreal opacity);

Alignment alignment(const Map *map = nullptr) const;

bool isVisible() const;
Expand Down Expand Up @@ -247,6 +253,8 @@ class TILEDSHARED_EXPORT MapObject : public Object
const ObjectTemplate *mObjectTemplate = nullptr;
ObjectGroup *mObjectGroup = nullptr;
qreal mRotation = 0.0;
// BONGO
qreal mOpacity = 1.0;
bool mVisible = true;
bool mTemplateBase = false;
ChangedProperties mChangedProperties;
Expand Down Expand Up @@ -479,6 +487,20 @@ inline qreal MapObject::rotation() const
inline void MapObject::setRotation(qreal rotation)
{ mRotation = rotation; }

/**
* BONGO: Add Opacity for objects
* returns current opacity
*/
inline qreal MapObject::opacity() const
{ return mOpacity; }

/**
* BONGO: Add Opacity for objects
* sets new opacity
*/
inline void MapObject::setOpacity(qreal opacity)
{ mOpacity = opacity; }

inline bool MapObject::isVisible() const
{ return mVisible; }

Expand Down
7 changes: 7 additions & 0 deletions src/libtiled/mapreader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,13 @@ std::unique_ptr<MapObject> MapReaderPrivate::readObject()
object->setPropertyChanged(MapObject::RotationProperty);
}

// BONGO
const qreal opacity = atts.value(QLatin1String("opacity")).toDouble(&ok);
if (ok) {
object->setOpacity(opacity);
object->setPropertyChanged(MapObject::OpacityProperty);
}

if (gid) {
object->setCell(cellForGid(gid));
object->setPropertyChanged(MapObject::CellProperty);
Expand Down
4 changes: 3 additions & 1 deletion src/libtiled/maprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,9 @@ void CellRenderer::render(const Cell &cell, const QPointF &screenPos, const QSiz
fragment.scaleX = size.width() / imageRect.width();
fragment.scaleY = size.height() / imageRect.height();
fragment.rotation = 0;
fragment.opacity = 1;
// fragment.opacity = 1;
// BONGO
fragment.opacity = cell.getOpacity();

const auto fillMode = tile->tileset()->fillMode();
if (fillMode == Tileset::PreserveAspectFit) {
Expand Down
4 changes: 4 additions & 0 deletions src/libtiled/maptovariantconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,10 @@ QVariant MapToVariantConverter::toVariant(const MapObject &object) const
if (notTemplateInstance || object.propertyChanged(MapObject::RotationProperty))
objectVariant[QStringLiteral("rotation")] = object.rotation();

// BONGO
if (notTemplateInstance || object.propertyChanged(MapObject::OpacityProperty))
objectVariant[QStringLiteral("opacity")] = object.opacity();

if (notTemplateInstance || object.propertyChanged(MapObject::VisibleProperty))
objectVariant[QStringLiteral("visible")] = object.isVisible();

Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/mapwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,11 @@ void MapWriterPrivate::writeObject(QXmlStreamWriter &w,
if (shouldWrite(rotation != 0.0, isTemplateInstance, mapObject.propertyChanged(MapObject::RotationProperty)))
w.writeAttribute(QStringLiteral("rotation"), QString::number(rotation));

// BONGO
const qreal opacity = mapObject.opacity();
if (shouldWrite(opacity != 1.0, isTemplateInstance, mapObject.propertyChanged(MapObject::OpacityProperty)))
w.writeAttribute(QStringLiteral("opacity"), QString::number(opacity));

if (shouldWrite(!mapObject.isVisible(), isTemplateInstance, mapObject.propertyChanged(MapObject::VisibleProperty)))
w.writeAttribute(QStringLiteral("visible"), QLatin1String(mapObject.isVisible() ? "1" : "0"));

Expand Down
5 changes: 5 additions & 0 deletions src/libtiled/tilelayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ class TILEDSHARED_EXPORT Cell
void setRotatedHexagonal120(bool v) { v ? _flags |= RotatedHexagonal120 : _flags &= ~RotatedHexagonal120; }

void rotate(RotateDirection direction);
// BONGO
float getOpacity() const { return _opacity; }
void setOpacity(float mOpacity) { _opacity = mOpacity; }

bool checked() const { return _flags & Checked; }
void setChecked(bool checked) { checked ? _flags |= Checked : _flags &= ~Checked; }
Expand All @@ -138,6 +141,8 @@ class TILEDSHARED_EXPORT Cell
Tileset *_tileset = nullptr;
int _tileId = -1;
int _flags = 0;
//BONGO
float _opacity = 1.0;
};

inline Tile *Cell::tile() const
Expand Down
8 changes: 8 additions & 0 deletions src/libtiled/varianttomapconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,8 @@ std::unique_ptr<MapObject> VariantToMapConverter::toMapObject(const QVariantMap
const qreal width = variantMap[QStringLiteral("width")].toReal();
const qreal height = variantMap[QStringLiteral("height")].toReal();
const qreal rotation = variantMap[QStringLiteral("rotation")].toReal();
// BONGO
const qreal opacity = variantMap[QStringLiteral("opacity")].toReal();

QString className = variantMap[QStringLiteral("class")].toString();
if (className.isEmpty()) // fallback for compatibility
Expand All @@ -738,6 +740,12 @@ std::unique_ptr<MapObject> VariantToMapConverter::toMapObject(const QVariantMap
object->setPropertyChanged(MapObject::RotationProperty);
}

// BONGO
if (variantMap.contains(QLatin1String("opacity"))) {
object->setOpacity(opacity);
object->setPropertyChanged(MapObject::OpacityProperty);
}

if (!templateVariant.isNull()) { // This object is a template instance
QString templateFileName = resolvePath(mDir, templateVariant);
auto objectTemplate = TemplateManager::instance()->loadObjectTemplate(templateFileName);
Expand Down
2 changes: 2 additions & 0 deletions src/plugins/lua/luaplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,8 @@ void LuaWriter::writeMapObject(const Tiled::MapObject *mapObject)
mWriter.writeKeyAndValue("width", mapObject->width());
mWriter.writeKeyAndValue("height", mapObject->height());
mWriter.writeKeyAndValue("rotation", mapObject->rotation());
// BONGO
mWriter.writeKeyAndValue("opacity", mapObject->opacity());

if (!mapObject->cell().isEmpty())
mWriter.writeKeyAndValue("gid", mGidMapper.cellToGid(mapObject->cell()));
Expand Down
31 changes: 31 additions & 0 deletions src/plugins/python/pythonbind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6996,6 +6996,17 @@ _wrap_PyTiledMapObject_rotation(PyTiledMapObject *self, PyObject *PYBINDGEN_UNUS
return py_retval;
}

/* BONGO */
PyObject *
_wrap_PyTiledMapObject_opacity(PyTiledMapObject *self, PyObject *PYBINDGEN_UNUSED(_args), PyObject *PYBINDGEN_UNUSED(_kwargs))
{
PyObject *py_retval;
double retval;

retval = self->obj->opacity();
py_retval = Py_BuildValue((char *) "d", retval);
return py_retval;
}

PyObject *
_wrap_PyTiledMapObject_setCell(PyTiledMapObject *self, PyObject *args, PyObject *kwargs)
Expand Down Expand Up @@ -7082,6 +7093,22 @@ _wrap_PyTiledMapObject_setRotation(PyTiledMapObject *self, PyObject *args, PyObj
return py_retval;
}

/* BONGO */
PyObject *
_wrap_PyTiledMapObject_setOpacity(PyTiledMapObject *self, PyObject *args, PyObject *kwargs)
{
PyObject *py_retval;
double r;
const char *keywords[] = {"r", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwargs, (char *) "d", (char **) keywords, &r)) {
return NULL;
}
self->obj->setOpacity(r);
Py_INCREF(Py_None);
py_retval = Py_None;
return py_retval;
}

PyObject *
_wrap_PyTiledMapObject_setShape(PyTiledMapObject *self, PyObject *args, PyObject *kwargs)
Expand Down Expand Up @@ -7272,11 +7299,15 @@ static PyMethodDef PyTiledMapObject_methods[] = {
{(char *) "name", (PyCFunction) _wrap_PyTiledMapObject_name, METH_NOARGS, "name()\n\n" },
{(char *) "objectGroup", (PyCFunction) _wrap_PyTiledMapObject_objectGroup, METH_NOARGS, "objectGroup()\n\n" },
{(char *) "rotation", (PyCFunction) _wrap_PyTiledMapObject_rotation, METH_NOARGS, "rotation()\n\n" },
/* BONGO */
{(char *) "opacity", (PyCFunction) _wrap_PyTiledMapObject_opacity, METH_NOARGS, "opacity()\n\n" },
{(char *) "setCell", (PyCFunction) _wrap_PyTiledMapObject_setCell, METH_KEYWORDS|METH_VARARGS, "setCell(c)\n\ntype: c: Tiled::Cell const" },
{(char *) "setHeight", (PyCFunction) _wrap_PyTiledMapObject_setHeight, METH_KEYWORDS|METH_VARARGS, "setHeight(h)\n\ntype: h: double" },
{(char *) "setName", (PyCFunction) _wrap_PyTiledMapObject_setName, METH_KEYWORDS|METH_VARARGS, "setName(n)\n\ntype: n: QString" },
{(char *) "setPosition", (PyCFunction) _wrap_PyTiledMapObject_setPosition, METH_KEYWORDS|METH_VARARGS, "setPosition(pos)\n\ntype: pos: QPointF" },
{(char *) "setRotation", (PyCFunction) _wrap_PyTiledMapObject_setRotation, METH_KEYWORDS|METH_VARARGS, "setRotation(r)\n\ntype: r: double" },
/* BONGO */
{(char *) "setOpacity", (PyCFunction) _wrap_PyTiledMapObject_setOpacity, METH_KEYWORDS|METH_VARARGS, "setOpacity(r)\n\ntype: r: double" },
{(char *) "setShape", (PyCFunction) _wrap_PyTiledMapObject_setShape, METH_KEYWORDS|METH_VARARGS, "setShape(s)\n\ntype: s: Tiled::MapObject::Shape" },
{(char *) "setSize", (PyCFunction) _wrap_PyTiledMapObject_setSize, METH_KEYWORDS|METH_VARARGS, "setSize(size)\n\ntype: size: QSizeF" },
{(char *) "setType", (PyCFunction) _wrap_PyTiledMapObject_setType, METH_KEYWORDS|METH_VARARGS, "setType(n)\n\ntype: n: QString" },
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/python/tiledbinding.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ def _decorate(obj, *args, **kwargs):
cls_mapobject.add_method('objectGroup', retval('Tiled::ObjectGroup*',reference_existing_object=True), [])
cls_mapobject.add_method('rotation', 'double', [])
cls_mapobject.add_method('setRotation', None, [('double','r')])
# BONGO
cls_mapobject.add_method('opacity', 'double', [])
cls_mapobject.add_method('setOpacity', None, [('double','opacity')])
cls_mapobject.add_method('isVisible', 'bool', [])
cls_mapobject.add_method('setVisible', None, [('bool','v')])
cls_mapobject.add_method('name', 'QString', [])
Expand Down
6 changes: 6 additions & 0 deletions src/tiled/editablemapobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ void EditableMapObject::setRotation(qreal rotation)
setMapObjectProperty(MapObject::RotationProperty, rotation);
}

// BONGO
void EditableMapObject::setOpacity(qreal opacity)
{
setMapObjectProperty(MapObject::OpacityProperty, opacity);
}

void EditableMapObject::setVisible(bool visible)
{
setMapObjectProperty(MapObject::VisibleProperty, visible);
Expand Down
12 changes: 12 additions & 0 deletions src/tiled/editablemapobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ class EditableMapObject : public EditableObject
Q_PROPERTY(qreal height READ height WRITE setHeight)
Q_PROPERTY(QSizeF size READ size WRITE setSize)
Q_PROPERTY(qreal rotation READ rotation WRITE setRotation)
// BONGO
Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity)
Q_PROPERTY(bool visible READ isVisible WRITE setVisible)
Q_PROPERTY(QJSValue polygon READ polygon WRITE setPolygon)
Q_PROPERTY(QString text READ text WRITE setText)
Expand Down Expand Up @@ -114,6 +116,8 @@ class EditableMapObject : public EditableObject
qreal height() const;
QSizeF size() const;
qreal rotation() const;
// BONGO
qreal opacity() const;
bool isVisible() const;
QJSValue polygon() const;
QString text() const;
Expand Down Expand Up @@ -149,6 +153,8 @@ public slots:
void setHeight(qreal height);
void setSize(QSizeF size);
void setRotation(qreal rotation);
// BONGO
void setOpacity(qreal opacity);
void setVisible(bool visible);
void setPolygon(QJSValue polygon);
void setPolygon(const QPolygonF &polygon);
Expand Down Expand Up @@ -219,6 +225,12 @@ inline qreal EditableMapObject::rotation() const
return mapObject()->rotation();
}

// BONGO
inline qreal EditableMapObject::opacity() const
{
return mapObject()->opacity();
}

inline bool EditableMapObject::isVisible() const
{
return mapObject()->isVisible();
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/mapobjectitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ void MapObjectItem::syncWithMapObject()

setPos(pixelPos);
setRotation(mObject->rotation());
// BONGO
setOpacity(mObject->opacity());

if (mBoundingRect != bounds) {
// Notify the graphics scene about the geometry change in advance
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/objectselectionitem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void MapObjectOutline::syncWithMapObject(const MapRenderer &renderer)

setPos(pixelPos);
setRotation(mObject->rotation());
// BONGO
setOpacity(mObject->opacity());
setFlag(QGraphicsItem::ItemIgnoresTransformations,
mObject->shape() == MapObject::Point);

Expand Down
4 changes: 3 additions & 1 deletion src/tiled/objectselectiontool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1657,7 +1657,9 @@ void ObjectSelectionTool::saveSelectionState()
mapObject->position(),
mapObject->size(),
mapObject->polygon(),
mapObject->rotation()
mapObject->rotation(),
// BONGO
mapObject->opacity()
};
mMovingObjects.append(object);
}
Expand Down
2 changes: 2 additions & 0 deletions src/tiled/objectselectiontool.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ class ObjectSelectionTool : public AbstractObjectTool
QSizeF oldSize;
QPolygonF oldPolygon;
qreal oldRotation;
// BONGO
qreal oldOpacity;
};

QVector<MovingObject> mMovingObjects;
Expand Down
15 changes: 15 additions & 0 deletions src/tiled/propertybrowser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,12 @@ void PropertyBrowser::addMapObjectProperties()
tr("Flipping"), groupProperty);

flippingProperty->setAttribute(QLatin1String("flagNames"), mFlippingFlagNames);

// BONGO
QtVariantProperty *opacityProperty = addProperty(OpacityProperty, QMetaType::Double, tr("Opacity"), groupProperty);
opacityProperty->setAttribute(QLatin1String("minimum"), 0.0);
opacityProperty->setAttribute(QLatin1String("maximum"), 1.0);
opacityProperty->setAttribute(QLatin1String("singleStep"), 0.1);
}

if (mMapObjectFlags & ObjectIsText) {
Expand Down Expand Up @@ -1296,6 +1302,13 @@ QUndoCommand *PropertyBrowser::applyMapObjectValueTo(PropertyId id, const QVaria
val.toDouble());
}
break;
// BONGO
case OpacityProperty:{
command = new ChangeMapObject(mDocument, mapObject,
MapObject::OpacityProperty,
val.toReal());
break;
}
case FlippingProperty: {
const int flippingFlags = val.toInt();

Expand Down Expand Up @@ -1913,6 +1926,8 @@ void PropertyBrowser::updateProperties()
if (mapObject->cell().flippedVertically())
flippingFlags |= 2;
mIdToProperty[FlippingProperty]->setValue(flippingFlags);
// BONGO
mIdToProperty[OpacityProperty]->setValue(mapObject->opacity());
}

if (flags & ObjectIsText) {
Expand Down
2 changes: 2 additions & 0 deletions src/tmxviewer/tmxviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class MapObjectItem : public QGraphicsItem

setPos(pixelPos);
setRotation(mapObject->rotation());
// BONGO
setOpacity(mapObject->opacity());
}

QRectF boundingRect() const override
Expand Down

0 comments on commit 24b42fb

Please sign in to comment.