Libraryless. Click here for Pure Java version (29916L/189K).
1 | srecord noeq G22ChallengePanel(G22Challenge challenge) extends HasChangeListeners is Swingable { |
2 | settableWithChange long randomSeed = 1; //randomUnsignedInt(); |
3 | |
4 | //transient JPanel imagesPanel = jline(); |
5 | transient DynamicHStack imagesPanel; |
6 | transient L<Entry> entries = syncL(); |
7 | transient JComponent imagesSection; |
8 | transient GazelleV_LeftArrowScript.Script parsedScript; |
9 | settableWithChange Set<Class> solutionTypes = syncSet(); |
10 | |
11 | transient settable new G22Utils g22utils; |
12 | settable double timeoutPerImage = 10.0; |
13 | settable int imagesToPaint = 3; |
14 | settable int interval = 100; |
15 | settable int imageDisplaySize = 100; |
16 | settable Color imageBorderColor1 = Color.green; |
17 | settable Color imageBorderColor2 = Color.red; |
18 | settable int imageBorderSize = 1; |
19 | |
20 | visualize { |
21 | imagesPanel = (DynamicHStack) dynamicHStack().spacing(20); |
22 | awtCalcEvery(imagesPanel, interval, r _paintAnImage); |
23 | |
24 | ret withCenteredBoldTitle(str(challenge), |
25 | withTopMargin(jvsplit( |
26 | northAndCenterWithMargin( |
27 | centerAndEastWithMargin( |
28 | centeredLiveValueLabel( |
29 | liveValue_hasChangeListeners(this, |
30 | -> "Looking for: " + solutionTypeDesc())), |
31 | swing(-> JPopDownButton(menu -> |
32 | menu.add(jMenuItem("Random seed: " + n2(randomSeed))))) |
33 | ), |
34 | imagesSection = jCenteredRaisedSection("Images", jBorderlessHigherScrollPane(jfullcenter(imagesPanel))), |
35 | /*jRightAlignedLine( |
36 | withLabel("Random seed:", |
37 | liveValueLabel(liveValue_hasChangeListeners(this, -> randomSeed)))),*/ |
38 | ), |
39 | jCenteredSection("Analysis", |
40 | jTodo() |
41 | ) |
42 | ))); |
43 | } |
44 | |
45 | void _paintAnImage { |
46 | if (l(entries) >= imagesToPaint) ret; |
47 | |
48 | int index = l(entries)+1; |
49 | Entry entry = new(index, (int) (randomSeed+index-1)); |
50 | entries.add(entry); |
51 | |
52 | try { |
53 | if (parsedScript == null) { |
54 | var parser = g22utils.leftArrowParser(); |
55 | parsedScript = parser.parse(challenge.code); |
56 | } |
57 | |
58 | new VarContext ctx; |
59 | var result = evalWithTimeout(timeoutPerImage, -> { |
60 | temp tempSetRandomSeed((int) entry.seed); |
61 | ret parsedScript.get(ctx); |
62 | }); |
63 | |
64 | if (result.isA()) { |
65 | var returnedObject = result.a(); |
66 | if (returnedObject instanceof MakesBufferedImage |
67 | || returnedObject instanceof Image) |
68 | returnedObject = toBufferedImage(returnedObject); |
69 | if (returnedObject cast BufferedImage) { |
70 | entry.solution(ctx.get("solution")); |
71 | updateSolutionType(entry.solution); |
72 | addImage(entry, returnedObject); |
73 | } else { |
74 | addImage(entry, null, jcenteredlabel("Script did not return an image but instead " + aOrAn(className(returnedObject)))); |
75 | } |
76 | } else { |
77 | addImage(entry, null, jcenteredlabel("Script timed out after " + nSeconds(iceil(timeoutPerImage)))); |
78 | } |
79 | } catch print e { |
80 | entry.error(e); |
81 | addImage(entry, null, jError(e)); |
82 | } finally { |
83 | entry.calculating(false); |
84 | } |
85 | } |
86 | |
87 | void addImage(Entry entry, BufferedImage image, JComponent component) { |
88 | entry.image(image).component(component); |
89 | addAndRevalidate(imagesPanel, component); |
90 | setSectionTitle(imagesSection, firstToUpper(nImages(entries)) + " calculated"); |
91 | } |
92 | |
93 | void addImage(Entry entry, BufferedImage image) { |
94 | entry.image(image); |
95 | addImage(entry, image, imageToComponent(entry)); |
96 | } |
97 | |
98 | JComponent imageToComponent(Entry entry) { |
99 | var image = entry.image; |
100 | |
101 | // var is = g22utils.stdImageSurface(image); |
102 | // jFixedSize(image.getWidth(), image.getHeight(), is); |
103 | // ret is; |
104 | |
105 | double scale = calcFitScale(image, imageDisplaySize); |
106 | var image2 = scaleImageWithOp(image, scale, |
107 | AffineTransformOp.TYPE_NEAREST_NEIGHBOR); |
108 | for (color : ll(imageBorderColor1, imageBorderColor2)) |
109 | image2 = addBorderToImage(image2, color, imageBorderSize); |
110 | S toolTip = "Image " + n2(entry.index) + " of " + challenge + " (seed " + n2(entry.seed) + ", " + image.getWidth() + "*" + image.getHeight() + " px)"; |
111 | JButton b = withToolTip(toolTip, jimageButton(image2)); |
112 | setMargin(10, b); |
113 | ret b; |
114 | } |
115 | |
116 | class Entry { |
117 | int index; // index in sequence starting with 1 |
118 | long seed; // seed for randomizer |
119 | settable BufferedImage image; |
120 | settable Throwable error; |
121 | settable O solution; |
122 | settable bool calculating = true; |
123 | settable JComponent component; |
124 | |
125 | *(int *index, long *seed) {} |
126 | } |
127 | |
128 | void updateSolutionType(O solution) { |
129 | Class c = _getClass(solution); |
130 | if (solutionTypes.add(c)) change(); |
131 | } |
132 | |
133 | S solutionTypeDesc() { |
134 | LS types = map solutionTypeDesc(solutionTypes); |
135 | ret textOut_or(types, "Calculating..."); |
136 | } |
137 | |
138 | S solutionTypeDesc(Class c) { |
139 | if (c == null) |
140 | ret "no solution given"; |
141 | if (c == Rect) |
142 | ret "a rectangular area in the image"; |
143 | ret aOrAn(shortClassName(c)); |
144 | } |
145 | } |
Began life as a copy of #1034214
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034225 |
Snippet name: | G22ChallengePanel backup |
Eternal ID of this version: | #1034225/1 |
Text MD5: | 86f7350cb275763c2dea654dd24f47e0 |
Transpilation MD5: | d65fc958b631185ee7a1e83a56764163 |
Author: | stefan |
Category: | javax / gazelle v |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-01-29 03:56:48 |
Source code size: | 5104 bytes / 145 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 141 / 197 |
Referenced in: | [show references] |