Utilizing the ability to "inject" html in VORPLE

I’m trying to inject some custom html code into the Iform7-VORPLE adventure.
I can see how to do it when it’s very simple, but something like the following is a bit confusing to inject.
In the following, I can see how to set up the “form”, but not how to do the remainder.
The contained double quotes make the issue that much more difficult to figure out how to pass it.

I realize that VORPLE can already generate an “alert”, but I’m just using it as an example that I can extend to other situations. Once I have this I can figure out how to get other events to happen, which depend on some code in the html portion and some in an external javascript file.

Can anyone help with this?

HTML embedding in the basic extension isn’t meant for complex HTML, it’s mainly for making simple containers that can be filled with content by other means. So you’d create the form element and name it:

place a "form" element called "deed";

and then include a JavaScript file that has a function that fills in the contents of the form.

I’ve finally managed to get a button with ‘dummy’ graphics to work, but can only position it in the ‘body’ of the interpreter webpage. I’m the first to admit that I’m new to jQuery/JavaScript and have a lot to learn. Sure hope someone can point me in the correct direction.

I’ve included my javascript code below. I’d like to be able to the embed the button (and later, other elements) into other portions of the webpage instead, but am having trouble understanding how to correctly reference the Vorple tags to move the button to various portions of the webpage. I also suspect that there are only certain segments into which one can/should put user elements and that could have a bearing on the issue.

[code]$(document).ready(function ()
{
$(‘body’).prepend(’javascript button’);
});

upImage = new Image();
upImage.src = “buyit15u.jpg”;
downImage = new Image();
downImage.src = “buyit15d.jpg”
normalImage = new Image();
normalImage.src = “buyit15.jpg”;

function changeImage()
{
document.images[“jsbutton”].src= upImage.src;
return true;
}

function changeImageBack()
{
document.images[“jsbutton”].src = normalImage.src;
return true;
}

function handleMDown()
{
document.images[“jsbutton”].src = downImage.src;
return true;
}

function handleMUp()
{
changeImage();
return true;
}[/code]

I’ve finally got the methodology for placing buttons and the like in the current turn area figured out.
I’ve also got the technique for erasing them when the turn is over, as well as getting info to and from them.
I still have a bit of work to do to smooth things out, but I “believe” that I’m over the hump.
It sure makes the adventure more engaging, when one can inject these embellishments.

The technique is actually very simple. One forms a named “div” from within Inform7+Vorple,
Then one calls a javascript/jQuery to append/prepend the statements required for the active element, such as a button.
The trick is that the ready event in jQuery is triggered well before the segment in which I’m interested exists.
That is why the forgoing wouldn’t work, even when I changed things and had the correct tags mentioned.
If one uses Inform7+Vorple to form the “div”, then immediately populates it using the execute javascript call, the timing is implicit and the code ends up being located at the correct place in the HTML.

I’d recommend creating your HTML in a more jQuery-y way - there’s definitely no need for inline javascript! See this example from Parchment for attaching multiple event handlers and other properties.

Thought I more-or-less did that, but in not a quite so elegant way.
(Please correct me if I’m wrong, because I’d really like learn to to do this properly.)

What I’d really like to get working, eventually, is a way to trigger an Inform7 event based on a change of button state.
Now that is something that I expect is rather difficult.

Here is the script code:

[code]upImage = new Image();
upImage.src = “buyit15u.jpg”;
downImage = new Image();
downImage.src = “buyit15d.jpg”
normalImage = new Image();
normalImage.src = “buyit15.jpg”;

function insertButtonCode()
{
$(’.buttonCode’).append(’javascript button’);
};

function removeButtonCode()
{
$(’.buttonCode’).remove();
};

function changeImage()
{
document.images[“jsbutton”].src= upImage.src;
return true;
}

function changeImageBack()
{
document.images[“jsbutton”].src = normalImage.src;
return true;
}

function handleMDown()
{
document.images[“jsbutton”].src = downImage.src;
return true;
}

function handleMUp()
{
changeImage();
return true;
}
[/code]

Here is the Inform7 code:

[code]“Buttons2” by Gary

Include Vorple by Juhana Leinonen.

Release along with the “Vorple” interpreter.

Release along with JavaScript “Button.js”.

Release along with a file of “buyit15u” called “buyit15u.jpg”.
Release along with a file of “buyit15d” called “buyit15d.jpg”.
Release along with a file of “buyit15” called “buyit15.jpg”.

Placing a button is an action out of world.

Understand “place” as Placing a button.

Carry out placing a button (this is the carry out placing a button rule):
if Vorple is available:
execute JavaScript command “removeButtonCode()”; [Erase any previous button(s)]
open HTML tag “div” called “buttonCode”; [Start the tag to contain the button]
say "This is a button: "; [‘Introduce the button’]
close HTML tag; [End the tag to contain the button]
execute JavaScript command “insertButtonCode()”; [Place the button]
mark the current action “normal”; [Make sure it appears in the turn area]
otherwise:
say “[bold type]Buttons are not possible without VORPLE[roman type]”;

Erasing a button is an action out of world.

Understand “erase” as erasing a button.

Carry out erasing a button (this is the carry out erasing a button rule):
if Vorple is available:
execute JavaScript command “removeButtonCode()”; [Erase any previous button(s)]
otherwise:
say “[bold type]Buttons are not possible without VORPLE[roman type]”.

Example Location is a room.
[/code]

Dannii;

I looked at the example you noted in the following quote and can now see the difference between your code and mine.

However; I was not able to find, in the jQuery sites API documentation, an explanation of the jQuery syntax you used to define the text input.
Obviously, it must work, but I’m not sure how to parallel it in order to extend it to my situation with buttons.
Where can I read up on this?

Well…I’ve figured out that many of the websites that address jQuery programming seem blissfully unaware of the changes in jQuery. I finally found some references to object-oriented jQuery and can see a definite parallel to object oriented PERL, with which I am very familiar. The method of instantiation, the constructors and destructors, and other similar concepts make perfect sense to me. I’ve used them quite a lot.

However; there are still enough language-specific differences to make this a challenge and not a walk-in-the-park. The webpages I’ve found are often very outdated and the approaches are not usually entirely consistent with that used in the Parchment example that Dannii provided. So; while I’ll eventually figure it out, I could really use a pointer to a good book on the subject that specifically reflects the jQuery programming style used in Parchment.

Dannii or Juhana, if one of you could make some suggestions on books or online resources, it would be greatly appreciated. A few bucks to learn how to use jQuery properly would be a small price to pay.

Sorry, been a bit busy. The official API docs do explain how it works, although it’s complicated because the main jQuery() function is so overloaded: api.jquery.com/jQuery/#jQuery2

It’s really a fantastic API in my opinion. Pass the plain tag as the first parameter and then an object as the second. In the object you can specify basically every HTML attribute, CSS styles through a css object, as well as all the event handlers. This API was added in jQuery 1.4 if that helps for searching for tutorials.
You can then call .append() on it to create a second node if needed, without even needing to save the reference. Chaining is quite cool.

No need to apologize. I’m happy that you’re helping me as much as you can.

I can get the jQuery to work to some extent, but some of the formatting is still giving me a problem.
I’m constructing a clickable image “button”. The correct format of the html is <a … > … .
One can’t put the event handlers between and , so one has to embed them within the initial .
I’ve tried a few things, but with no real success.
Think I have to do this in two steps, <a … >, then embed the … between that and
.
Once that’s done, I can .appendTo the target location within the body.
The issue is ti find the correct “selector” to do the first step.

When you have a minute, do you have any thoughts on this approach?
It just sounds more complex than it should be.

Can you show the code you’ve got now?

If you have complex HTML, it might be easier to write it to a .html file and use jQuery’s .load() to inject it to the page and then use .on() to attach event handlers. The downside is that AJAX calls (which is what .load() does) don’t generally work from the local filesystem so you’d have to set up a server to test it (which is easier than it sounds, though).

Dannii and Juhana;

The code I’m writing is not that complex. I had it more-or-less working in a hacked-together fashion, but wanted to try pure jQuery as suggested earlier.

The following is definitely incorrect, but JQuery apparently works completely or it doesn’t, so debugging seems to be a bit hit-and-miss. That said; it’s what I have at the moment. I’m still on the steep side of the learning curve, so please bear with me and let me know what I’m doing wrong.

[code]//New_Button.js

upImage = new Image();
upImage.src = “buyit15u.jpg”;
downImage = new Image();
downImage.src = “buyit15d.jpg”
normalImage = new Image();
normalImage.src = “buyit15.jpg”;

function insertButtonCode()
{
// Trying to place the following code in a random container, such as the current turn area of the Parchment screen.
// <a href="#" onMouseOver=“return changeImage()” onMouseOut= "return changeImageBack()"onMouseDown=“return handleMDown()”
// onMouseUp=“return handleMUp()”><img name=“jsbutton” src=“buyit15.jpg” width=“110” height=“28” border=“0” alt=“javascript button”
// The event handler code in the above is what I’ve shown embedded in the following jQuery code.
// Had it working using a somewhat patched together mixture of jQuery and JavaScript. (earlier post).
// Then I tried modelling the jQuery after the text input portion of the input.js example, as Dannii suggested.
// I KNOW I’ve got it wrong. The structure that results from the following is definitely not correctly nested.
// Just cannot figure out how to get jQuery to construct the correct structure.

$( "<a/>", {
	'href': 'http://www.geocaching.com',
	mouseover: function( event )
	{
		document.images["jsbutton"].src= upImage.src;
		return true;
	},
	mouseout: function( event )
	{
	   document.images["jsbutton"].src = normalImage.src;
	   return true;
	},
	mousedown: function( event )
	{
		document.images["jsbutton"].src = downImage.src;
		return true;
	},
	mouseup: function( event )
	{
		changeImage();
		return true;
	},
	name: "jsbutton",
	src: "buyit15.jpg",
	alt: "javascript button",
	border: "0",
	height: "28",
	width: "110"
}).appendTo( ".buttonCode" )

};

function removeButtonCode()
{
$(".buttonCode").remove();
};
[/code]

I seem to be making some progress by trying to do things a bit differently.

Here is a “straw man” to try out the event handling and buttons with images. It seems to work correctly. Now, if I transfer it into an external file, modify it to something more “useful” and can get it to inject text into the input handler to fake user keyboard input, I should have what I wanted. Throwing an Inform7 “event” that could be caught within the Inform7 language would have been more elegant, but I understand that it is hard to do.

[code]

This is a heading

This is a paragraph.

This is another paragraph.

[/code]

I’d recommend using CSS more. You don’t actually need any of those event handlers if you use the :hover and :active psuedo classes. Something like this:

[code]// JS
function insertButtonCode()
{
$( “”, {
href: ‘
http://www.geocaching.com’,
‘class’: ‘mybutton’
}).appendTo( “.buttonCode” )
};

// CSS
a.mybutton {
background: url(buyit15.jpg);
height: 28;
width: 110;
}

a.mybutton:hover {
background: url(buyit15u.jpg);
}

a.mybutton:active {
background: url(buyit15d.jpg);
}
[/code]

Dannii;

I tried this and the graphics don’t seem to load, even though the image tag is being created correctly and with the correct class reference.
In Inform7/Vorple, were does the CSS file expect to find the noted graphics?
Do I need to do some relative addressing, if I’ve used Release along with…?

They are relative to the location of the style sheet. Or you can put a full url there too.

Understood. I have them in the same directory, so it should work as you presented it in your post.
The header definitely contains instructions to load the Button.css and Button.js, so that’s not it.
When I look at the code with the Inspector in Firefox, the Button.css is not associated with the html code for the button.
I bought some good books on the subject today, so I’ll do a bit of reading and see why it’s not picking up the style sheets.

Can you show the header? It might be a simple mistake.

This is what I have:

[code]

Buttons2 - Parchment [/code]