Skip to content

Instantly share code, notes, and snippets.

@erkyrath
Created November 30, 2015 04:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erkyrath/1386e832eab296798be8 to your computer and use it in GitHub Desktop.
Save erkyrath/1386e832eab296798be8 to your computer and use it in GitHub Desktop.
Demonstrate displaying an illustration window using Unified Glulx Input.
"Test Case" by Andrew Plotkin.
Include Unified Glulx Input by Andrew Plotkin.
[The overall concept is this: At various points throughout the game, small "thumbnail" graphics will appear in the main window. These are hyperlinked. If the player clicks a thumbnail:
1) The game opens up a graphical window that covers the main window entirely (it splits the main window, but takes up 100% of the window space) and displays a larger illustration.
2) The game also displays a caption for the picture in the status line.
3) If the player clicks on the larger illustration, OR presses any key, the graphical window closes and the player is returned to normal gameplay.
]
Test Chamber is a room. "[Figure - Red Button]Not much to do here, [if Figure - Red Button has been displayed]now that you've pushed[otherwise]other than push[end if] that big, shiny red button in the corner." The big shiny red button is scenery in the Test Chamber. Instead of doing something with the button, say "Just click it with your mouse."
The player carries a red rock.
The player carries a green rock.
Figure - Red Button is the file "red_button.png".
Figure - Gorilla Shark is the file "gorilla_shark.png".
Section - the table of illustrations
[In the full game, this table will match up all the full illustrations with their thumbnails and caption text.]
Table of Illustrations
link-number illustration-name thumb-name display-status caption
102 Figure - Gorilla Shark Figure - Red Button false "Totally Awesome!!"
To decide if (F - a figure-name) has been displayed:
if the display-status corresponding to a thumb-name of F in the Table of Illustrations is false, no;
otherwise yes.
Section - displaying thumbnails
To say (F - a figure-name):
if F has been displayed:
do nothing;
otherwise:
let N be the link-number corresponding to a thumb-name of F in the Table of Illustrations;
say "[hyperlink N]";
display thumb F;
say "(link [N])";
say "[/hyperlink][line break]";
To display thumb (F - a figure-name):
(- glk_image_draw(gg_mainwin, ResourceIDsOfFigures-->{F}, imagealign_MarginLeft, 0); -);
Section - handling input
[We customize input in four ways. First, request hyperlink input (in normal mode) and char input (in window-open mode).]
Setting up input rule:
if the illustration window is not open:
now the story-window is hyperlink-input-request;
else:
now the input-request of the story-window is char-input.
[Second, a set of input-accepting rules. Note that there's no specific rule for accepting character input. UGI knows that char input is acceptable whenever the input-request of the story-window is char-input.]
[Let mouse events pass through to become commands. (We have to do this explicitly because UGI is not yet aware of third-party windows.)]
Accepting input rule when handling mouse-event and the illustration window is open:
accept input event.
[Graphics-window-redraw events redraw the graphics window. But we reject the event so that the input loop keeps waiting.]
Accepting input rule when handling redraw-event and the illustration window is open:
let F be the illustration-name corresponding to a link-number of current illustration link-number in the Table of Illustrations;
redraw the full illustration of F;
reject input event.
[Same goes for window-rearrange events. This does the work of UGI's "standard redraw status line on arrange rule" -- it redraws the status line -- but it also redraws the graphics window.]
Accepting input rule when handling arrange-event and the illustration window is open:
let F be the illustration-name corresponding to a link-number of current illustration link-number in the Table of Illustrations;
redraw the full illustration of F;
redraw the status line;
reject input event.
[Third, rules to convert hyperlink, mouse, and character input into custom actions.]
Handling input rule for a command input-context when handling hyperlink-event:
let N be current input event hyperlink number;
handle the current input event as the action of displaying illustration N;
rule succeeds.
Handling input rule for a command input-context when handling char-event and the illustration window is open:
say line break;
handle the current input event as the action of undisplaying illustration;
rule succeeds.
Handling input rule for a command input-context when handling mouse-event and the illustration window is open:
say line break;
handle the current input event as the action of undisplaying illustration;
rule succeeds.
[Fourth, we customize the prompt for window-open mode. This is only relevant because I've modified the display rules to give the illustration window the top 75% of the screen, instead of 100%.]
Prompt displaying rule when the illustration window is open:
instead say "<Hit a key:>"
Section - status line rules
Rule for constructing the status line:
center "[the player's surroundings]" at row 1;
rule succeeds.
Rule for constructing the status line when the illustration window is open:
let N be the current illustration link-number;
let C be the caption corresponding to a link-number of N in the Table of Illustrations;
center C at row 1;
rule succeeds.
Section - special actions
[We create actions for displaying and removing the full illustration. These actions cannot be typed by the player; they're generated by the parser when a hyperlink, character, or mouse event arrives.]
The current illustration link-number is a number that varies.
Displaying illustration is an action out of world applying to one number.
Check displaying illustration when the illustration window is open:
say "Cannot display illustration [number understood], because illustration [current illustration link-number] is already visible.";
stop the action.
Carry out displaying illustration:
let N be the number understood;
now the current illustration link-number is N;
[say "We will display illustration [N].";]
let F be the illustration-name corresponding to a link-number of N in the Table of Illustrations;
reveal the full illustration of F;
[now the display-status corresponding to a link-number of N in the Table of Illustrations is true.] [Commented out so that I can test repeated window-opening.]
Undisplaying illustration is an action out of world applying to nothing.
Check undisplaying illustration when the illustration window is not open:
say "Cannot undisplay illustration, because no illustration is visible.";
stop the action.
Carry out undisplaying illustration:
close the illustration window;
try looking;
Section - opening the full illustration window
Include (-
Constant GG_ILLUSTRATION_ROCK 210;
Global gg_illustration_window = 0;
-) after "Global Variables" in "Output.i6t".
To reveal the full illustration of (F - a figure-name):
(-
if (gg_illustration_window == 0) {
! I've changed the illustration window to cover only the top 75% of the screen.
gg_illustration_window = glk_window_open(gg_mainwin, (winmethod_Above+winmethod_Proportional), 75, wintype_Graphics, GG_ILLUSTRATION_ROCK );
! Wait for a mouse-click event. (UGI does not yet handle any windows except the main window.)
glk_request_mouse_event(gg_illustration_window);
}
if (gg_illustration_window) { ! testing to see if the window exists
BlackBackground();
DisplayPicture(ResourceIDsOfFigures-->{F});
}
-).
To redraw the full illustration of (F - a figure-name):
(-
BlackBackground();
DisplayPicture(ResourceIDsOfFigures-->{F});
-).
To decide if the illustration window is open:
(- (gg_illustration_window) -).
To decide if the illustration window is not open:
(- (~~gg_illustration_window) -).
Section - blacking out the background
[This is copied nearly verbatim from Emily Short's Simple Graphical Windows extension. It works just fine.]
Include (-
[ BlackBackground color result graph_width graph_height;
if (gg_illustration_window) {
result = glk_window_get_size(gg_illustration_window, gg_arguments, gg_arguments+WORDSIZE);
graph_width = gg_arguments-->0;
graph_height = gg_arguments-->1;
glk_window_fill_rect(gg_illustration_window, 0, 0, 0, graph_width, graph_height);
}
];
-).
Section - centering text
[This is copied from Basic Screen Effects. That extension contains code which is not compatible with UGI, but the text-centering routines are no problem.]
To center (quote - text) at the/-- row (depth - a number):
(- CenterPrint({quote}, {depth}); -).
Include (-
[ CenterPrint str depth i j len;
font off;
i = VM_ScreenWidth();
len = TEXT_TY_CharacterLength(str);
if (len > 63) len = 63;
j = (i-len)/2 - 1;
VM_MoveCursorInStatusLine(depth, j);
print (I7_string) str;
font on;
];
-).
Section - scaling and displaying the picture
Include (-
[ DisplayPicture cur_pic result graph_width graph_height img_width img_height h_total w_total h_offset w_offset;
if (gg_illustration_window) {
result = glk_window_get_size(gg_illustration_window, gg_arguments, gg_arguments+WORDSIZE);
graph_width = gg_arguments-->0;
graph_height = gg_arguments-->1;
result = glk_image_get_info( cur_pic, gg_arguments, gg_arguments+WORDSIZE);
img_width = gg_arguments-->0;
img_height = gg_arguments-->1;
w_total = img_width;
h_total = img_height;
if (graph_height - h_total < 0) { ! if the image won't fit, find the scaling factor
w_total = (graph_height * w_total)/h_total;
h_total = graph_height;
}
if (graph_width - w_total < 0) {
h_total = (graph_width * h_total)/w_total;
w_total = graph_width;
}
w_offset = (graph_width - w_total)/2; if (w_offset < 0) w_offset = 0;
h_offset = (graph_height - h_total)/2; if (h_offset < 0) h_offset = 0;
glk_image_draw_scaled(gg_illustration_window, cur_pic, w_offset, h_offset, w_total, h_total);
}
];
-).
Section - removing the illustration
[Finally, this part just closes the graphics window. As far as I can tell, it's doing its job.]
To close the illustration window:
(-
if (gg_illustration_window) {
glk_window_close(gg_illustration_window, 0);
gg_illustration_window = 0;
}
-).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment