%
% Communicatieve interactie
%
% ci_interface.pl 
%
% Written by Anders Bouwer, 15 november 2004
%
% It should not be necessary to modify this file!
%


% process_text_input(InputStr, E):-
%
% processes InputStr and displays the answer in editor E
%
process_text_input(InputStr, E):-
    new(Chain, chain), 
    send(regex('\\w+'), for_all, InputStr,
	 message(Chain, append, ?(@arg1, register_value, @arg2, 0, name))),

    chain_list(Chain, WordList), 

    % call predicate process_text( Input, Output )
    process_text(WordList, Output),	  
    send(E, clear),
    send(E, append, '\n'),
    forall(member(Word, Output),
          (
	   send(E, append, Word),
	   send(E, append, ' ')
          )
    ).


% Start the interface
	
go:-
    % Create a picture for the diagrams: @grapher 
    grapher([ clear ]),
    % send(@grapher, height, 60), 
  
    % Create a picture for the animal character + text balloon
    new(P, picture('SpeechBubble', size(600, 200))), 

    get(@grapher, frame, F), 
    send(F, append, P), 
    send(P, display, bitmap('animal.jpg'), point(60,120)),
    send(P, below, @grapher), 
    % send(@grapher, label, 'The Interactive Zoo'),
    send(F, label, 'The Interactive Zoo'),

    % Dialog, for the text entry
    new(D, dialog), 
    send(D, below, P),	  

    % Editor, for the text balloon
    new(E, editor),
    send(E, width, 35), 
    send(E, height, 5), 
    send(E, wrap, word),
    send(P, display, E, point(200,100)),
    get(E, bounding_box, area(EX, EY, EW, EH)), 

    % Text entry item for textual input
    new(TI, text_item(input, '', 
	message(@prolog, process_text_input, D?input_member?selection, E))),
    send(TI, length, 80),
    send(D, append, TI),
    send(D, layout),
    send(D, open),

    % The text balloon
    %
    new(TB, device),      
    XMargin is 10,
    YMargin is 10,
    % BubblePointer Width and Height, and Thickness (approximately)
    BPW is 12,
    BPH is 12,
    BPT is 5,
    TBX is EX - XMargin, 
    TBY is EY - YMargin, 
    TBW is EW+2*XMargin, 
    TBH is EH+2*YMargin,
    new(B, box(TBW, TBH)),
    send(TB, display, B), 
    send(P, display, TB, point(TBX, TBY)), 
    send(B, radius, 30),
    % send(B, shadow, 2),
    % The thickness of the line for the speech bubble
    send(B, pen, 3),
    % position of LeftBubblePointer 
    % 1/6 of length    
    new(LeftLine, line(TBW/6-BPT, TBH-1, TBW/6-BPW, TBH+BPH, none)), 
    new(RightLine, line(TBW/6+BPT, TBH-1, TBW/6-BPW, TBH+BPH, none)),
    send(LeftLine, pen, 3), 
    send(RightLine, pen, 3), 
    send(TB, display, LeftLine), 
    send(TB, display, RightLine), 
    new(WhiteLine, line(TBW/6-BPT+1, TBH-2, TBW/6+BPT-1, TBH-2, none)), 
    send(WhiteLine, colour, white),
    send(WhiteLine, pen, 3),
    send(TB, display, WhiteLine),
    send(E?text_image_member, elevation, @nil),
    send(E?text_image_member, pen, 0),
    % send(E?text_image_member?elevation, shadow, @nil),

    % display welcome_text 
    send(E, load, 'welcome_text.txt'),

    % make text in editor uneditable
    send(E, editable, @off), 

    % put E on top of TB
    send(E, swap, TB),

    normalise_margin(P, XMargin, YMargin),
    send(D, layout),
    send(F, fit).
    

% make sure picture displays the top left of the figure
%
normalise_margin(P, LeftMargin, TopMargin):-
        get(P, bounding_box, area(X, Y, _W, _H)), 
        send(P, scroll_to, point(X-LeftMargin, Y-TopMargin)).


	  




