summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/metauml/metauml_state.mp
blob: fb9875e262a2ab07880d28c51e1b48f8c75574e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
% MetaUML, a MetaPost library for typesetting exquisite UML diagrams.
% Copyright (C) 2005 Ovidiu Gheorghies
%
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License
% as published by the Free Software Foundation; either version 2
% of the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

if known _metauml_state_mp:
  expandafter endinput
fi;
_metauml_state_mp:=1;

% Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost.
% The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times, 
% but this macro makes sure that MetaPost won't try to load the file and display a message for that.
def inputonce text libraryFile=
	if not known scantokens ("_" & str libraryFile & "_mp"):
		%includeonce% show "Loading " & str libraryFile;
		scantokens ("input " & str libraryFile);
	else:
		%includeonce% show str libraryFile & " already loaded.";
	fi;
enddef;

inputonce metauml_defaults;
inputonce util_log;

vardef StateInfo@#=
  attributes(@#);
  var(color)   foreColor, borderColor;
  var(numeric) round;

  FontInfo.@#iFont(metauml_defaultFont, defaultscale);
  @#iFont.ignoreNegativeBase := 1;

  PictureInfo.@#iText  (2, 2, 2, 2)(@#iFont);
  PictureStackInfo.@#iStack (2, 2, 2, 2)(9)(@#iText);

  @#iStack.iPict.ignoreNegativeBase := 1;

  Margins.@#(2,2,0,6);
  
  GroupInfo.@#iGroupSubstates(2, 2, 4, 2);
  
  @#round := 5;
  @#drawNameLine := 0;

  ShadeInfo.@#iShade;
  @#foreColor := .9white;
  @#borderColor := black;
enddef;

vardef StateInfoCopy@#(text src)=
  log "StateInfoCopy: Copying state info";
  attributes(@#);
  var(color)   foreColor, borderColor;
  var(numeric) round;

  PictureStackInfoCopy.@#iStack (src.iStack);
  MarginsCopy.@#(src);
  @#round := src.round;
  @#drawNameLine := src.drawNameLine;

  GroupInfoCopy.@#iGroupSubstates(src.iGroupSubstates);
  
  ShadeInfoCopy.@#iShade(src.iShade);
  @#foreColor := src.foreColor;
  @#borderColor := src.borderColor;
enddef;

StateInfo.iState;

vardef State@#(text contents)(text _substates)=
  EState.@#(iState)(contents)(_substates);
enddef;

vardef EState@#(text _info)(text contents)(text _substates)=
  ObjectEquations(@#);
  @#className := "State";

  StateInfoCopy.@#info(_info);

  numeric @#nLines; @#nLines := 0;
  string @#lines[];

  numeric @#nInternalTransitions; @#nInternalTransitions := 0;
  string @#internalTransitions[];

  for l=contents:
    @#lines[@#nLines] := l;
    @#nLines := @#nLines + 1;
  endfor;

  EGroup.@#substates(@#info.iGroupSubstates)(_substates);
  %@#substates.info.boxed := 1;

  EPictureStack.@#stack(@#info.iStack)(scantokens listArray(@#lines)(@#nLines))("vcenterbase");
enddef;

vardef stateTransitions@#(text transitions)=
  State_internalTransitions@#(transitions);
enddef;

vardef State_internalTransitions@#(text transitions)=
  for transition=transitions:
    @#internalTransitions[@#nInternalTransitions] := transition;
    @#nInternalTransitions := @#nInternalTransitions + 1;
  endfor;
enddef;

vardef State_layout@#=
  if @#layedout = 1:
    log "State '" & (str @#) & "' has already been layed out";
  else:
    @#layedout := 1;
    log "State layout: '" & (str @#) & "'";

    EPictureStack.@#internalTransitionsStack(@#info.iStack)
       (scantokens listArray(@#internalTransitions)(@#nInternalTransitions))("vleftbase");

    log "State name layout '" & (str @#) & "'...";
    PictureStack_layout.@#stack;
    log "Internal transitions layout '" & (str @#) & "'...";
    PictureStack_layout.@#internalTransitionsStack;
    log "Substates layout '" & (str @#) & "'...";
    Group_layout.@#substates;

    log "All elements in state '" & (str @#) & "' successfully layed out, integrating...";

%    @#internalTransitionsStack.info.boxed := 1;

    @#contentWidth = lmax(@#stack.width, @#internalTransitionsStack.width, @#substates.width);

    @#width = @#contentWidth + @#info.left + @#info.right + 2 * @#info.round;
    @#height = @#stack.height + @#info.top + @#info.bottom +
               @#internalTransitionsStack.height + @#substates.height;

    @#stack.n = @#n + (0, -@#info.top);
    @#substates.n = @#stack.s;

    @#internalTransitionsStack.sw = @#sw + (@#info.round, @#info.round);

    attributes(@#);
    var(pair) urt, lrt, ulft, llft;
    var(pair) urta, lrta, ulfta, llfta;
    var(pair) urtb, lrtb, ulftb, llftb;

    @#urta := @#ne - (@#info.round, 0);
    @#urtb := @#ne - (0, @#info.round);

    @#lrta := @#se + (0, @#info.round);
    @#lrtb := @#se - (@#info.round, 0);

    @#ulfta := @#nw - (0, @#info.round);
    @#ulftb := @#nw + (@#info.round, 0);

    @#llfta := @#sw + (@#info.round, 0);
    @#llftb := @#sw + (0, @#info.round);

    @#urt := @#ne - (@#info.round, @#info.round) + @#info.round * (sind(45),cosd(45));
    @#lrt := @#se + (-@#info.round, @#info.round) + @#info.round * (sind(135),cosd(135));
    @#llft := @#sw + (@#info.round, @#info.round) + @#info.round * (sind(225),cosd(225));
    @#ulft := @#nw + (@#info.round, -@#info.round) + @#info.round * (sind(315),cosd(315));

    var(pair) nameLineLeft, nameLineRight;
    ypart @#nameLineLeft = ypart @#nameLineRight = @#stack.bottom;
    xpart @#nameLineLeft = xpart @#nw;
    xpart @#nameLineRight = xpart @#ne;

    log "State layout for " & (str @#) & " ended.";
  fi;
enddef;

vardef State_draw@#=
  State_layout.@#;
  objectEnsurePositioning.@#;

  attributes(@#);
  var(path) border;

  @#border := @#urta .. @#urt .. @#urtb -- %
              @#lrta .. @#lrt .. @#lrtb -- %
              @#llfta .. @#llft .. @#llftb -- %
          @#ulfta .. @#ulft .. @#ulftb -- cycle;

  %fill @#border shifted (@#info.shade, -@#info.shade) withcolor .7white;
   drawObjectShade(@#);

   fill @#border withcolor @#info.foreColor;
   draw @#border withcolor @#info.borderColor;


  drawObject(@#internalTransitionsStack);
  drawObject(@#stack);
  drawObject(@#substates);

  if @#info.drawNameLine = 1:
    draw @#nameLineLeft -- @#nameLineRight;
  fi;
enddef;

vardef State_border@#=
  @#border
enddef;