| 1 |
% Copyright 2005 2015 Ovidiu Gheorghies |
| 2 |
% Licensed under the Apache License, Version 2.0. |
| 3 |
|
| 4 |
if known _util_object_mp: |
| 5 |
expandafter endinput |
| 6 |
fi; |
| 7 |
_util_object_mp:=1; |
| 8 |
|
| 9 |
% Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost. |
| 10 |
% The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times, |
| 11 |
% but this macro makes sure that MetaPost won't try to load the file and display a message for that. |
| 12 |
def inputonce text libraryFile= |
| 13 |
if not known scantokens ("_" & str libraryFile & "_mp"): |
| 14 |
%includeonce% show "Loading " & str libraryFile; |
| 15 |
scantokens ("input " & str libraryFile); |
| 16 |
else: |
| 17 |
%includeonce% show str libraryFile & " already loaded."; |
| 18 |
fi; |
| 19 |
enddef; |
| 20 |
|
| 21 |
inputonce util_infrastructure; |
| 22 |
inputonce util_log; |
| 23 |
|
| 24 |
def ObjectEquations(suffix $)= |
| 25 |
attributes($); |
| 26 |
var(pair) n, ne, e, se, s, sw, w, nw, c; |
| 27 |
var(pair) dim; |
| 28 |
var(numeric) width, height; |
| 29 |
|
| 30 |
var(numeric) left, right; |
| 31 |
var(numeric) top, bottom; |
| 32 |
var(numeric) midx, midy; |
| 33 |
|
| 34 |
var(picture) pict; |
| 35 |
var(string) className; |
| 36 |
|
| 37 |
var(numeric) laidout, drawn; |
| 38 |
|
| 39 |
$.laidout := 0; |
| 40 |
$.drawn := 0; |
| 41 |
|
| 42 |
$.left = xpart $.nw = xpart $.sw; |
| 43 |
$.right = xpart $.ne = xpart $.se; |
| 44 |
|
| 45 |
$.top = ypart $.ne = ypart $.nw; |
| 46 |
$.bottom = ypart $.se = ypart $.sw; |
| 47 |
|
| 48 |
$.w = .5[$.nw,$.sw]; |
| 49 |
$.s = .5[$.sw,$.se]; |
| 50 |
$.e = .5[$.ne,$.se]; |
| 51 |
$.n = .5[$.ne,$.nw]; |
| 52 |
|
| 53 |
$.c = .5[$.nw, $.se]; |
| 54 |
|
| 55 |
$.width = $.right - $.left; |
| 56 |
$.height = $.top - $.bottom; |
| 57 |
|
| 58 |
$.midx = xpart($.c); |
| 59 |
$.midy = ypart($.c); |
| 60 |
|
| 61 |
xpart $.dim = $.width; |
| 62 |
ypart $.dim = $.height; |
| 63 |
|
| 64 |
$.className := "Object"; |
| 65 |
enddef; |
| 66 |
|
| 67 |
def objectBox(text obj)= |
| 68 |
obj.nw -- obj.ne -- obj.se -- obj.sw -- cycle |
| 69 |
enddef; |
| 70 |
|
| 71 |
vardef objectBorder(suffix @#)= |
| 72 |
save methodName; |
| 73 |
string methodName; |
| 74 |
methodName := @#className & "_border"; |
| 75 |
log "objectBorder: invoking " & methodName & " arg=" & str @#; |
| 76 |
|
| 77 |
scantokens (methodName).@# |
| 78 |
enddef; |
| 79 |
|
| 80 |
vardef Object_toString(text obj)= |
| 81 |
save ret; string ret; |
| 82 |
ret := "Generic object"; |
| 83 |
ret |
| 84 |
enddef; |
| 85 |
|
| 86 |
vardef toString(suffix @#)= |
| 87 |
scantokens (@#className & "_toString").@# |
| 88 |
enddef; |
| 89 |
|
| 90 |
vardef layoutObject(suffix @#)= |
| 91 |
save methodName; |
| 92 |
string methodName; |
| 93 |
methodName := @#className & "_layout"; |
| 94 |
log "layoutObject: invoking " & methodName & " arg=" & str @#; |
| 95 |
|
| 96 |
scantokens (methodName).@#; |
| 97 |
enddef; |
| 98 |
|
| 99 |
vardef drawObject(suffix @#)= |
| 100 |
save methodName; |
| 101 |
string methodName; |
| 102 |
methodName := @#className & "_draw"; |
| 103 |
log "drawObject: invoking " & methodName & " arg=" & str @#; |
| 104 |
|
| 105 |
scantokens (methodName).@#; |
| 106 |
enddef; |
| 107 |
|
| 108 |
vardef drawObjectShade(suffix @#)= |
| 109 |
fill objectBorder(@#) shifted |
| 110 |
(@#.info.iShade.shift, -@#.info.iShade.shift) withcolor @#.info.iShade.background; |
| 111 |
enddef; |
| 112 |
|
| 113 |
vardef objectEnsurePositioning@#= |
| 114 |
if unknown (xpart @#nw): |
| 115 |
log "*** WARNING: DEFAULTING OBJECT'S (" & (str @#) & ") x TO 0"; |
| 116 |
xpart @#nw = 0; |
| 117 |
fi; |
| 118 |
if unknown (ypart @#nw): |
| 119 |
log "*** WARNING: DEFAULTING OBJECT'S (" & (str @#) & ") y TO 0"; |
| 120 |
ypart @#nw = 0; |
| 121 |
fi; |
| 122 |
enddef; |
| 123 |
|
| 124 |
vardef drawObjectAt(suffix @#)(text eq)= |
| 125 |
eq; |
| 126 |
drawObject(@#); |
| 127 |
enddef; |
| 128 |
|
| 129 |
vardef layoutObjects(text objects)= |
| 130 |
log "layoutObjects: begin"; |
| 131 |
forsuffixes o = objects: |
| 132 |
if (str o) <> "": |
| 133 |
layoutObject(o); |
| 134 |
fi; |
| 135 |
endfor; |
| 136 |
log "layoutObjects: end"; |
| 137 |
enddef; |
| 138 |
|
| 139 |
vardef drawObjects(text objects)= |
| 140 |
% This needs to be done first in order to allow for equations which define |
| 141 |
% relative positioning to be appropiately solved. Otherwise, for example, |
| 142 |
% object0 may be positioned somewhere by default, and object1 also, leading |
| 143 |
% to inconsitent equations. Inconsistency would have been caused by the fact that the |
| 144 |
% positioning equations become meaningful only after object layout is performed |
| 145 |
% --- but that would have been too late, since the positioning would have been |
| 146 |
% done already to inappropriate values. |
| 147 |
% |
| 148 |
layoutObjects(objects); |
| 149 |
|
| 150 |
forsuffixes o = objects: |
| 151 |
if (str o) <> "": |
| 152 |
drawObject(o); |
| 153 |
fi; |
| 154 |
endfor; |
| 155 |
enddef; |
| 156 |
|
| 157 |
%% |
| 158 |
%% Joins the given pictures according to the equation given |
| 159 |
%% by the pictureDoJoin macro. Note: the pictureDoJoin macro |
| 160 |
%% can be conveniently set by calling setPicutureJoin macro. |
| 161 |
%% |
| 162 |
%% Usage example: |
| 163 |
%% joinObjects (p, q, r); |
| 164 |
%% |
| 165 |
def joinObjects(text pictures)= |
| 166 |
log "joinObjects: call started."; |
| 167 |
|
| 168 |
save _index; |
| 169 |
_index := 0; |
| 170 |
forsuffixes p=pictures: |
| 171 |
exitif (str p) = ""; |
| 172 |
if _index=0: |
| 173 |
log " joinObjects: calling join for first object."; |
| 174 |
log _index; |
| 175 |
log p; |
| 176 |
objectDoJoinFirst(p)(_index); |
| 177 |
else: |
| 178 |
log " joinObjects: calling join for next object."; |
| 179 |
log _index; |
| 180 |
log p; |
| 181 |
objectDoJoin(previousPic, p)(_index); |
| 182 |
fi; |
| 183 |
|
| 184 |
_index := _index + 1; |
| 185 |
def previousPic=p enddef; |
| 186 |
endfor; |
| 187 |
|
| 188 |
log "joinObjects: call ended." |
| 189 |
enddef; |
| 190 |
|
| 191 |
%% |
| 192 |
%% Sets the macro pictureDoJoin, which is used by joinObjects to join |
| 193 |
%% one picture into another. |
| 194 |
%% |
| 195 |
%% The pictureDoJoin macro has two arguments: pa and pb. |
| 196 |
%% pa represents the first picture and pb the second picture. |
| 197 |
%% The second picture is positioned relatively to pa as specified |
| 198 |
%% by the given equation. |
| 199 |
%% |
| 200 |
%% Usage examples: |
| 201 |
%% |
| 202 |
%% % place the second picture at the bottom of the first |
| 203 |
%% setPictureJoin(pa.sw = pb.nw); |
| 204 |
%% |
| 205 |
%% % place the second picture at the right of the first |
| 206 |
%% setPictureJoin(pa.ne = pb.nw); |
| 207 |
%% |
| 208 |
def setObjectJoin(text eq)= |
| 209 |
def objectDoJoin(suffix pa, pb)(expr index)=eq; enddef; |
| 210 |
def objectDoJoinFirst(suffix pa)(expr index)= log " objectDoJoinFirst: NOP"; enddef; |
| 211 |
enddef; |
| 212 |
|
| 213 |
def setObjectJoinFirst(text eq)= |
| 214 |
def objectDoJoinFirst(suffix pa)(expr index)=eq; enddef; |
| 215 |
enddef; |
| 216 |
|
| 217 |
%% |
| 218 |
%% A shortcut for setting the join equation and actually performing the join. |
| 219 |
% |
| 220 |
def joinObjectsEq(text objects)(text eq)= |
| 221 |
setObjectJoin(eq); |
| 222 |
joinObjects(objects); |
| 223 |
enddef; |
| 224 |
|
| 225 |
|
| 226 |
%% |
| 227 |
%% Joins the pictures and draws them. A shorthand for |
| 228 |
%% the corresponding two macro invocations. |
| 229 |
%% |
| 230 |
def joinDrawObjects(text pictures)= |
| 231 |
joinObjects(pictures); |
| 232 |
drawObjects(pictures); |
| 233 |
enddef; |