ShoyuVanilla / FoundryVTT-Chat-Portrait

Other
14 stars 24 forks source link

Feature Request: Ability for modules to add custom message-portrait images #73

Closed Mejari closed 3 years ago

Mejari commented 3 years ago

Not sure if this is covered by the multisystem TODO in the README, but it would be great if modules were able to code in support for chat portrait message-portrait images themselves. For example, I'm currently working on a module that adds different stats that can be rolled, and it would be nice to provide chat-portrait images to inject into the roll chat messages instead of the unknown '?' image.

p4535992 commented 3 years ago

Add some Hooks on version 0.5.0, any feed back is welcome, a example on the details on the README.md file

Mejari commented 3 years ago

That's great! One thing is that I'd need the chat message information to do some logic on whether or not to do the replacing. I'm not sure I have enough of a grasp of the code to know if these changes would break other things, but I tried out this change to ChatPortrait.js and it seemed to function the way I wanted with adding the chat message data and the setting of the image replacer:

index 5e617ec..933ae0d 100644
--- a/dist/module/ChatPortrait.js
+++ b/dist/module/ChatPortrait.js
@@ -158,10 +158,16 @@ export class ChatPortrait {
             iconMainCustomImage: imgPath,
             iconMainCustomReplacer: "",
         };
-        Hooks.call('ChatPortraitReplaceData', chatPortraitCustomData);
+        Hooks.call('ChatPortraitReplaceData', chatPortraitCustomData, chatMessage);
         if (chatPortraitCustomData.iconMainCustomImage) {
             imgPath = chatPortraitCustomData.iconMainCustomImage;
         }
+       
+       let imageReplacerToUse = imageReplacer;
+       if(!!chatPortraitCustomData.iconMainCustomReplacer
+           && typeof chatPortraitCustomData.iconMainCustomReplacer == 'object') {
+           imageReplacerToUse = chatPortraitCustomData.iconMainCustomReplacer;
+       }
         return ChatPortrait.generatePortraitImageElement(imgPath).then((imgElement) => {
             const messageData = messageDataBase.message ? messageDataBase.message : messageDataBase.document.data;
             // Very very rare use case ????
@@ -269,7 +275,7 @@ export class ChatPortrait {
                         let value = "";
                         let images = { iconMainReplacer: "", iconsDamageType: [] };
                         if (ChatPortrait.useImageReplacer(html)) {
-                            images = ChatPortrait.getImagesReplacerAsset(imageReplacer, elementItemName.innerText, elementItemContentList[i]);
+                            images = ChatPortrait.getImagesReplacerAsset(imageReplacerToUse, elementItemName.innerText, elementItemContentList[i]);
                             if (images && images.iconMainReplacer) {
                                 value = images.iconMainReplacer;
                             }
@@ -456,7 +462,7 @@ export class ChatPortrait {
                     let value = "";
                     let images = { iconMainReplacer: "", iconsDamageType: [] };
                     if (ChatPortrait.useImageReplacer(html)) {
-                        images = ChatPortrait.getImagesReplacerAsset(imageReplacer, elementItemText.innerText, elementItemContentList[i]);
+                        images = ChatPortrait.getImagesReplacerAsset(imageReplacerToUse, elementItemText.innerText, elementItemContentList[i]);
                         if (images && images.iconMainReplacer) {
                             value = images.iconMainReplacer;
                         }

I was then able to use the new hook to do this with my module:

Hooks.on('ChatPortraitReplaceData', (chatPortraitCustomData, chatMessage) => {
    const speaker = ChatMessage.getSpeakerActor(chatMessage.data.speaker);
    if('KW_WarfareUnitSheet' === speaker?.sheet?.constructor.name) {
        chatPortraitCustomData.iconMainCustomReplacer = {
            "KW_WARFARE.Power": "systems/dnd5e/icons/skills/yellow_08.jpg",
            "KW_WARFARE.Attack": "systems/dnd5e/icons/skills/red_31.jpg",
            "KW_WARFARE.Morale": "systems/dnd5e/icons/skills/yellow_17.jpg",
            "KW_WARFARE.Command": "systems/dnd5e/icons/skills/ice_16.jpg"
        };
    }
    return chatPortraitCustomData;
});

and it resulted in exactly what I'd hoped!

image

p4535992 commented 3 years ago

Ty for the suggestion ! Your idea make more sense than mine... Integrated on version 0.5.1 (note i changed the name of the fields , checkout the README.md update). Let me know

Mejari commented 3 years ago

Thank you so much, it works great! I appreciate the help.