Skip to content

Commit

Permalink
MXP to MSP Tags (Mudlet#4015)
Browse files Browse the repository at this point in the history
* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* MXP to MSP Tags

* test/CMakeLists.txt update

* test/CMakeLists.txt update backout

* test/CMakeLists.txt update
  • Loading branch information
mpconley authored Aug 18, 2020
1 parent c0e2f9b commit 1fe5100
Show file tree
Hide file tree
Showing 13 changed files with 319 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,12 @@ set(mudlet_SRCS
TMxpEntityTagHandler.cpp
TMxpFontTagHandler.cpp
TMxpLinkTagHandler.cpp
TMxpMusicTagHandler.cpp
TMxpNodeBuilder.cpp
TMxpMudlet.cpp
TMxpProcessor.cpp
TMxpSendTagHandler.cpp
TMxpSoundTagHandler.cpp
TMxpSupportTagHandler.cpp
MxpTag.cpp
TMxpTagHandler.cpp
Expand Down Expand Up @@ -230,6 +232,8 @@ set(mudlet_HDRS
TMxpEntityTagHandler.h
TMxpFontTagHandler.h
TMxpLinkTagHandler.h
TMxpMusicTagHandler.h
TMxpSoundTagHandler.h
TMxpElementDefinitionHandler.h
TMxpElementRegistry.h
TMxpContext.h
Expand Down
5 changes: 5 additions & 0 deletions src/TMxpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "TMxpContext.h"
#include "TMxpTagHandlerResult.h"

class TMediaData;

class TMxpClient
{
protected:
Expand Down Expand Up @@ -59,6 +61,9 @@ class TMxpClient
virtual int setLink(const QStringList& hrefs, const QStringList& hints) = 0;
virtual bool getLink(int id, QStringList** hrefs, QStringList** hints) = 0;

virtual void playMedia(TMediaData& mediaData) = 0;
virtual void stopMedia(TMediaData& mediaData) = 0;

virtual bool tagReceived(MxpTag* tag) { return tag->isStartTag() ? startTagReceived(tag->asStartTag()) : endTagReceived(tag->asEndTag()); }

virtual bool startTagReceived(MxpStartTag* startTag) { return true; }
Expand Down
11 changes: 11 additions & 0 deletions src/TMxpMudlet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "TMxpMudlet.h"
#include "Host.h"
#include "TMedia.h"
#include "TConsole.h"
#include "TLinkStore.h"

Expand Down Expand Up @@ -77,6 +78,16 @@ bool TMxpMudlet::getLink(int id, QStringList** links, QStringList** hints)
return true;
}

void TMxpMudlet::playMedia(TMediaData& mediaData)
{
mpHost->mpMedia->playMedia(mediaData);
}

void TMxpMudlet::stopMedia(TMediaData& mediaData)
{
mpHost->mpMedia->stopMedia(mediaData);
}

TMxpTagHandlerResult TMxpMudlet::tagHandled(MxpTag* tag, TMxpTagHandlerResult result)
{
if (tag->isStartTag()) {
Expand Down
4 changes: 4 additions & 0 deletions src/TMxpMudlet.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "post_guard.h"

class Host;
class TMediaData;

class TMxpMudlet : public TMxpClient
{
Expand Down Expand Up @@ -77,6 +78,9 @@ class TMxpMudlet : public TMxpClient

bool getLink(int id, QStringList** links, QStringList** hints) override;

void playMedia(TMediaData& mediaData) override;
void stopMedia(TMediaData& mediaData) override;

bool isBold, isItalic, isUnderline;

void setBold(bool bold) override { isBold = bold; }
Expand Down
109 changes: 109 additions & 0 deletions src/TMxpMusicTagHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/***************************************************************************
* Copyright (C) 2020 by Mike Conley - sousesider[at]gmail.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include "TMedia.h"
#include "TMxpMusicTagHandler.h"
#include "TMxpClient.h"

TMxpTagHandlerResult TMxpMusicTagHandler::handleStartTag(TMxpContext& ctx, TMxpClient& client, MxpStartTag* tag)
{
TMediaData mediaData {};

mediaData.setMediaProtocol(TMediaData::MediaProtocolMSP);
mediaData.setMediaType(TMediaData::MediaTypeMusic);
mediaData.setMediaFileName(tag->getAttributeValue("FName"));

if (tag->hasAttribute("V")) {
QString volume = tag->getAttributeValue("V");

if (!volume.isEmpty()) {
mediaData.setMediaVolume(volume.toInt());

if (mediaData.getMediaVolume() == TMediaData::MediaVolumePreload) {
// Support preloading
} else if (mediaData.getMediaVolume() > TMediaData::MediaVolumeMax) {
mediaData.setMediaVolume(TMediaData::MediaVolumeMax);
} else if (mediaData.getMediaVolume() < TMediaData::MediaVolumeMin) {
mediaData.setMediaVolume(TMediaData::MediaVolumeMin);
}
}
}

if (tag->hasAttribute("L")) {
QString loops = tag->getAttributeValue("L");

if (!loops.isEmpty()) {
mediaData.setMediaLoops(loops.toInt());

if (mediaData.getMediaLoops() < TMediaData::MediaLoopsRepeat || mediaData.getMediaLoops() == 0) {
mediaData.setMediaLoops(TMediaData::MediaLoopsDefault);
}
}
}

if (tag->hasAttribute("P")) {
QString priority = tag->getAttributeValue("P");

if (!priority.isEmpty()) {
mediaData.setMediaPriority(priority.toInt());

if (mediaData.getMediaPriority() > TMediaData::MediaPriorityMax) {
mediaData.setMediaPriority(TMediaData::MediaPriorityMax);
} else if (mediaData.getMediaPriority() < TMediaData::MediaPriorityMin) {
mediaData.setMediaPriority(TMediaData::MediaPriorityMin);
}
}
}

if (tag->hasAttribute("C")) {
QString musicContinue = tag->getAttributeValue("C");

if (!musicContinue.isEmpty()) {
if (musicContinue.toInt() == 0) {
mediaData.setMediaContinue(TMediaData::MediaContinueRestart);
} else {
mediaData.setMediaContinue(TMediaData::MediaContinueDefault);
}
}
}

if (tag->hasAttribute("T")) {
QString type = tag->getAttributeValue("T");

if (!type.isEmpty()) {
mediaData.setMediaTag(type.toLower());
}
}

if (tag->hasAttribute("U")) {
QString url = tag->getAttributeValue("U");

if (!url.isEmpty()) {
mediaData.setMediaUrl(url);
}
}

if (mediaData.getMediaFileName() == "Off" && mediaData.getMediaUrl().isEmpty()) {
client.stopMedia(mediaData);
} else {
client.playMedia(mediaData);
}

return MXP_TAG_HANDLED;
}
33 changes: 33 additions & 0 deletions src/TMxpMusicTagHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************************************
* Copyright (C) 2020 by Mike Conley - sousesider[at]gmail.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#ifndef MUDLET_TMXPMUSICTAGHANDLER_H
#define MUDLET_TMXPMUSICTAGHANDLER_H

#include "TMxpTagHandler.h"

// <music FName="never_gonna_give_you_up.mp3" [V=100] [L=1] [C=1] [T="misc"] [U="https://www.example.com/sounds/"]>
class TMxpMusicTagHandler : public TMxpSingleTagHandler
{
public:
TMxpMusicTagHandler() : TMxpSingleTagHandler("MUSIC") {}

TMxpTagHandlerResult handleStartTag(TMxpContext& ctx, TMxpClient& client, MxpStartTag* tag) override;
};
#endif //MUDLET_TMXPMUSICTAGHANDLER_H
97 changes: 97 additions & 0 deletions src/TMxpSoundTagHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/***************************************************************************
* Copyright (C) 2020 by Mike Conley - sousesider[at]gmail.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#include "TMedia.h"
#include "TMxpSoundTagHandler.h"
#include "TMxpClient.h"

TMxpTagHandlerResult TMxpSoundTagHandler::handleStartTag(TMxpContext& ctx, TMxpClient& client, MxpStartTag* tag)
{
TMediaData mediaData {};

mediaData.setMediaProtocol(TMediaData::MediaProtocolMSP);
mediaData.setMediaType(TMediaData::MediaTypeSound);
mediaData.setMediaFileName(tag->getAttributeValue("FName"));

if (tag->hasAttribute("V")) {
QString volume = tag->getAttributeValue("V");

if (!volume.isEmpty()) {
mediaData.setMediaVolume(volume.toInt());

if (mediaData.getMediaVolume() == TMediaData::MediaVolumePreload) {
// Support preloading
} else if (mediaData.getMediaVolume() > TMediaData::MediaVolumeMax) {
mediaData.setMediaVolume(TMediaData::MediaVolumeMax);
} else if (mediaData.getMediaVolume() < TMediaData::MediaVolumeMin) {
mediaData.setMediaVolume(TMediaData::MediaVolumeMin);
}
}
}

if (tag->hasAttribute("L")) {
QString loops = tag->getAttributeValue("L");

if (!loops.isEmpty()) {
mediaData.setMediaLoops(loops.toInt());

if (mediaData.getMediaLoops() < TMediaData::MediaLoopsRepeat || mediaData.getMediaLoops() == 0) {
mediaData.setMediaLoops(TMediaData::MediaLoopsDefault);
}
}
}

if (tag->hasAttribute("P")) {
QString priority = tag->getAttributeValue("P");

if (!priority.isEmpty()) {
mediaData.setMediaPriority(priority.toInt());

if (mediaData.getMediaPriority() > TMediaData::MediaPriorityMax) {
mediaData.setMediaPriority(TMediaData::MediaPriorityMax);
} else if (mediaData.getMediaPriority() < TMediaData::MediaPriorityMin) {
mediaData.setMediaPriority(TMediaData::MediaPriorityMin);
}
}
}

if (tag->hasAttribute("T")) {
QString type = tag->getAttributeValue("T");

if (!type.isEmpty()) {
mediaData.setMediaTag(type.toLower());
}
}

if (tag->hasAttribute("U")) {
QString url = tag->getAttributeValue("U");

if (!url.isEmpty()) {
mediaData.setMediaUrl(url);
}
}

if (mediaData.getMediaFileName() == "Off" && mediaData.getMediaUrl().isEmpty()) {
client.stopMedia(mediaData);
} else {
client.playMedia(mediaData);
}

return MXP_TAG_HANDLED;
}
33 changes: 33 additions & 0 deletions src/TMxpSoundTagHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/***************************************************************************
* Copyright (C) 2020 by Mike Conley - sousesider[at]gmail.com *
* *
* This program 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. *
* *
* This program 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 this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/

#ifndef MUDLET_TMXPSOUNDTAGHANDLER_H
#define MUDLET_TMXPSOUNDTAGHANDLER_H

#include "TMxpTagHandler.h"

// <sound FName="mm_door_close1.*" [V=100] [L=1] [P=50] [T="misc"] [U="https://www.example.com/sounds/"]>
class TMxpSoundTagHandler : public TMxpSingleTagHandler
{
public:
TMxpSoundTagHandler() : TMxpSingleTagHandler("SOUND") {}

TMxpTagHandlerResult handleStartTag(TMxpContext& ctx, TMxpClient& client, MxpStartTag* tag) override;
};
#endif //MUDLET_TMXPSOUNDTAGHANDLER_H
4 changes: 4 additions & 0 deletions src/TMxpTagProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
#include "TMxpFontTagHandler.h"
#include "TMxpFormattingTagsHandler.h"
#include "TMxpLinkTagHandler.h"
#include "TMxpMusicTagHandler.h"
#include "TMxpSendTagHandler.h"
#include "TMxpSoundTagHandler.h"
#include "TMxpSupportTagHandler.h"
#include "TMxpTagHandlerResult.h"
#include "TMxpTagParser.h"
Expand Down Expand Up @@ -83,6 +85,8 @@ TMxpTagProcessor::TMxpTagProcessor()
registerHandler(TMxpFeatureOptions({"a", {"href", "hint"}}), new TMxpLinkTagHandler());
registerHandler(TMxpFeatureOptions({"color", {"fore", "back"}}), new TMxpColorTagHandler());
registerHandler(TMxpFeatureOptions({"font", {"color", "back"}}), new TMxpFontTagHandler());
registerHandler(TMxpFeatureOptions({"sound", {"fname", "v", "l", "p", "t", "u"}}), new TMxpSoundTagHandler());
registerHandler(TMxpFeatureOptions({"music", {"fname", "v", "l", "p", "c", "t", "u"}}), new TMxpMusicTagHandler());

mSupportedMxpElements["b"] = QVector<QString>();
mSupportedMxpElements["i"] = QVector<QString>();
Expand Down
4 changes: 4 additions & 0 deletions src/mudlet.pro
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,8 @@ SOURCES += \
TMxpCustomElementTagHandler.cpp \
TMxpFontTagHandler.cpp \
TMxpLinkTagHandler.cpp \
TMxpMusicTagHandler.cpp \
TMxpSoundTagHandler.cpp \
TMxpMudlet.cpp \
TMxpNodeBuilder.cpp \
TMxpProcessor.cpp \
Expand Down Expand Up @@ -638,6 +640,8 @@ HEADERS += \
TMxpCustomElementTagHandler.h \
TMxpFontTagHandler.h \
TMxpLinkTagHandler.h \
TMxpMusicTagHandler.h \
TMxpSoundTagHandler.h \
TMxpElementDefinitionHandler.h \
TMxpElementRegistry.h \
TMxpEntityTagHandler.h \
Expand Down
Loading

0 comments on commit 1fe5100

Please sign in to comment.