Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: rongel on Sat 19/11/2022 14:04:30

Title: [SOLVED] Inserting image into text / String
Post by: rongel on Sat 19/11/2022 14:04:30
Hello!

Is there a way to insert an image into text (more specifically into TextWindow)?
I have a tutorial messages in my game that simply use the Display command. It works well, but I would like to insert an image of a keyboard button into it (rather than use a letter).

I don't want to use a custom GUI for the tutorial messages, because the amount of text changes a lot in them. Display and the TextWindow works well adjusting to the right size. I tried playing around with the Dynamicsprite, but couldn't create the sprite on top of the TextWindow.

So is this possible at all, without using custom GUIs for the messages? Thanks!
Title: Re: Inserting image into text / String
Post by: Khris on Sat 19/11/2022 17:32:26
You can do this by editing one of the fonts. You can't use more than one font in a Display() message so you would have to use a bunch of special characters for this.

Download FontEdit (https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/tool-radiant-fontedit/) to edit AGS fonts.

To add a font, open your game and create a new font. Save the game and close AGS. Now open the .WFN file of the font you want to use in FontEdit, draw the keyboard key characters, then save it over the newly created WFN file (if your game had three fonts previously, save the font over agsfont3.wfn, that's the fourth font you just created in AGS).
Now use that font for your Display messages.
Title: Re: Inserting image into text / String
Post by: rongel on Sat 19/11/2022 18:14:56
Thanks Khris!

That's a clever approach, I would have never thought of it. I'll try it soon and let you know if it works, or if run into problems.
Title: Re: Inserting image into text / String
Post by: rongel on Sat 19/11/2022 20:33:09
Ok, tested the font edit tool, it's pretty neat and simple to use. But it seems that you can only draw white pixels (or erase them). My keyboard button image that I want to use is small, but made in Photoshop and has several colors and shading. Is it somehow possible to add that image to a font? I'm not very familiar with font editing unfortunately.
Title: Re: Inserting image into text / String
Post by: Khris on Sat 19/11/2022 21:04:39
Ah, no, unfortunately bitmap fonts only support a single color.

You can look into the SpriteFont plugin though, that should work: https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/another-plugin-spritefont-renderer-native-bitmap-fonts/
Github: https://github.com/CalinLeafshade/ags-spritefont

It allows you to use arbitrary sprites for characters and can be used with native commands like Display().
Title: Re: Inserting image into text / String
Post by: rongel on Sat 19/11/2022 21:12:26
Thanks for the tip! Downloaded it, I'll test it tomorrow!
Title: Re: Inserting image into text / String
Post by: Crimson Wizard on Sat 19/11/2022 23:25:11
Besides the above methods with the fonts, using DrawingSurface and DynamicSprite, which you paste onto a custom GUI's background (or set as a GUI button's graphic), is a most flexible method, as that would allow you to draw virtually anything, both text, geometric primitives and images combined. With certain amount of extra scripting you could do even embedded animation (for example: make some parts of the message glow to bring player's attention, and so forth).

In order to replace Display calls, for instance, you may write your own custom function that
* generates the dynamic sprite;
* shows a GUI with that sprite on it;
* waits until player's input (WaitMouseKey, or WaitForInput in 3.6.0);
* hides GUI and disposes the sprite.
Title: Re: Inserting image into text / String
Post by: Snarky on Sun 20/11/2022 06:49:59
And to add to Crimson Wizard's suggestion...

It sounded like the main reason you didn't want to use a GUI was that TextWindow resizes itself to fit the text. Does that mean you're using a decorated TextWindow with a frame and tiled background?

If so, there are a number of modules and code samples that draw a background and border frame for an arbitrarily sized window. CustomDialogGUI (https://www.adventuregamestudio.co.uk/forums/modules-plugins-tools/module-customdialoggui-1-9/) is one, SpeechBubble 0.8.10 (https://www.adventuregamestudio.co.uk/forums/index.php?msg=636585653) another. (You may have to just borrow the relevant code, since both modules have other logic that doesn't quite fit your use-case.)

It's not super-difficult to do yourself, in any case. It basically amounts to:

Title: Re: Inserting image into text / String
Post by: rongel on Sun 20/11/2022 07:42:49
Thanks for the answers!

Because my game is almost completed and I'm just doing final touches, I'm looking for the simplest / safest a way that doesn't introduce any new problems. So I'm still figuring which approach to take.

QuoteIt sounded like the main reason you didn't want to use a GUI was that TextWindow resizes itself to fit the text. Does that mean you're using a decorated TextWindow with a frame and tiled background?

Yes, that's the reason! And yes, I use a TextWindow with custom graphics (8 different corner sprites). Works well and looks nice enough. Little hesitant to use a new module at this point, but I might take a look.

QuoteBesides the above methods with the fonts, using DrawingSurface and DynamicSprite, which you paste onto a custom GUI's background (or set as a GUI button's graphic), is a most flexible method

I didn't know I could do this! What command should I use to paste the DynamicSprite to the GUI's background? I only managed to do it in room's background. Would it be possible to to paste it into my TextWindow GUI, it has a background image sprite in the editor. That would be the perfect solution!
Title: Re: Inserting image into text / String
Post by: Khris on Sun 20/11/2022 09:36:43
Here's how to draw stuff on a GUI's background:

DynamicSprite *gbg; // we need a persistent pointer

function game_start() {
  gbg = DynamicSprite.Create(200, 200);
  gSomeGUI.BackgroundGraphic = gbg.Graphic;
}

function DisplayStuff() {
  DrawingSurface* ds = gbg.getDrawingSurface();
  // draw stuff
  ds.Release();
}
Title: Re: Inserting image into text / String
Post by: rongel on Sun 20/11/2022 10:24:49
Quote from: Khris on Sun 20/11/2022 09:36:43Here's how to draw stuff on a GUI's background:
Thanks, I managed to change the TextWindow background with this! Is there a way to place the DynamicSprite on to the background, like with the Room.GetDrawingSurfaceForBackground command, instead of replacing it?

I'll continue fiddling with these!
Title: Re: Inserting image into text / String
Post by: Khris on Sun 20/11/2022 10:47:23
Once you have the DrawingSurface of the background, you can do the same things you can do with any DrawingSurface.
If you want to place a sprite, just draw that (2nd) sprite to the surface at any position.

Title: Re: Inserting image into text / String
Post by: rongel on Sun 20/11/2022 11:27:46
Looks like I got it working, this is just what I was looking for! Now I have my keyboard button image inserted into my TextWindow GUI, and everything works with the Display command.

Here's my code, does it seem alright ?

dynamic_test = DynamicSprite.CreateFromExistingSprite(6472, true);
   
gMessage_Box.BackgroundGraphic = dynamic_test.Graphic;
   
DrawingSurface* ds = dynamic_test.GetDrawingSurface();
   
// draw stuff
ds.DrawImage(5, 57, 13525);
ds.Release();

Thanks for all the help everyone!

Title: Re: Inserting image into text / String
Post by: rongel on Sun 20/11/2022 11:37:11
Here's what it looks now in the game:

(https://i.imgur.com/MZQm7jl.png)
Title: Re: Inserting image into text / String
Post by: Crimson Wizard on Sun 20/11/2022 12:54:14
Hah, I actually did not know that you can do that directly to TextWindow :). I was thinking more about having fully custom gui where you'd also have to draw the borders yourself, but if this works with TextWindow, that's even better.
Title: Re: Inserting image into text / String
Post by: rongel on Sun 20/11/2022 13:21:33
Yes, this seems to work well! I just need to remove the dynamic sprite / restore the GUI after displaying the message, otherwise the symbol will stay there in the later messages.
I'll mark this as solved!