1 ! Copyright (C) 2010 Slava Pestov.
2 USING: arrays accessors colors euler.b-rep fry gml
3 gml.runtime gml.viewer gml.printer io.directories
4 io.encodings.utf8 io.files io.pathnames io.streams.string kernel
5 locals models namespaces sequences ui ui.gadgets
6 ui.gadgets.buttons ui.gadgets.editors ui.gadgets.frames
7 ui.gadgets.grids ui.gadgets.labels ui.gadgets.packs
8 ui.gadgets.scrollers ui.gadgets.worlds ui.gadgets.tables
9 ui.gadgets.labeled unicode ;
13 SINGLETON: stack-entry-renderer
15 M: stack-entry-renderer row-columns
16 drop [ write-gml ] with-string-writer 1array ;
18 M: stack-entry-renderer row-value
21 : <stack-table> ( model -- table )
22 stack-entry-renderer <table>
28 : <stack-display> ( model -- gadget )
29 <stack-table> <scroller> "Operand stack"
30 COLOR: dark-gray <colored-labeled-gadget> ;
32 TUPLE: gml-editor < frame editor gml stack-model b-rep b-rep-model ;
34 : update-models ( gml-editor -- )
35 [ [ b-rep>> dup finish-b-rep ] [ b-rep-model>> ] bi set-model ]
36 [ [ gml>> operand-stack>> ] [ stack-model>> ] bi set-model ]
39 : with-gml-editor ( gml-editor quot -- )
41 [ [ gml>> gml set ] [ b-rep>> b-rep set ] bi @ ]
46 : find-gml-editor ( gadget -- gml-editor )
47 [ gml-editor? ] find-parent ;
49 : load-input ( file gml-editor -- )
50 [ utf8 file-contents ] dip editor>> set-editor-string ;
52 : update-viewer ( gml-editor -- )
53 dup [ editor>> editor-string run-gml-string ] with-gml-editor ;
55 : new-viewer ( gml-editor -- )
57 [ [ b-rep-model>> ] [ stack-model>> ] bi gml-viewer ]
60 : reset-viewer ( gml-editor -- )
63 gml get operand-stack>> delete-all
66 : <new-button> ( -- button )
67 "New viewer" [ find-gml-editor new-viewer ] <border-button> ;
69 : <update-button> ( -- button )
70 "Update viewer" [ find-gml-editor update-viewer ] <border-button> ;
72 : <reset-button> ( -- button )
73 "Reset viewer" [ find-gml-editor reset-viewer ] <border-button> ;
75 : <control-buttons> ( -- gadget )
77 <new-button> add-gadget
78 <update-button> add-gadget
79 <reset-button> add-gadget ;
81 CONSTANT: example-dir "vocab:gml/examples/"
83 : gml-files ( -- seq )
84 example-dir directory-files
85 [ file-extension >lower "gml" = ] filter ;
87 : <example-button> ( file -- button )
88 dup '[ example-dir _ append-path swap find-gml-editor load-input ]
91 : <example-buttons> ( -- gadget )
94 "Examples:" <label> add-gadget
95 [ <example-button> add-gadget ] reduce ;
97 : <editor-panel> ( editor -- gadget )
102 <scroller> "Editor" COLOR: dark-gray <colored-labeled-gadget> ;
104 : <gml-editor> ( -- gadget )
105 2 3 gml-editor new-frame
108 dup b-rep>> <model> >>b-rep-model
109 dup gml>> operand-stack>> <model> >>stack-model
111 { 0 0 } >>filled-cell
112 <source-editor> >>editor
113 dup editor>> <editor-panel> { 0 0 } grid-add
114 dup stack-model>> <stack-display> { 0 1 } grid-add
115 <control-buttons> { 0 2 } grid-add
116 <example-buttons> { 1 0 } grid-add ;
118 M: gml-editor focusable-child* editor>> ;
120 : gml-editor-window ( -- )
121 <gml-editor> "Generative Modeling Language" open-window ;
123 MAIN: gml-editor-window