|
1 | 1 | import Phaser from 'phaser'; |
2 | 2 | import { render } from 'phaser-jsx'; |
3 | 3 |
|
| 4 | +import {baseballGroundTruth, kidneyGroundTruth} from '../../const' |
| 5 | + |
4 | 6 | import { Typewriter } from '../components'; |
5 | 7 | import { |
6 | 8 | key, |
@@ -79,6 +81,181 @@ export class Level2 extends ParentScene { |
79 | 81 | private selectedText?: Phaser.GameObjects.Text; |
80 | 82 | private selectedDataset: string = "none"; |
81 | 83 |
|
| 84 | +private attachInfoIcon( |
| 85 | + target: Phaser.GameObjects.Image, |
| 86 | + imageKey: string, |
| 87 | + offsetX = 35, |
| 88 | + offsetY = -20 |
| 89 | +) { |
| 90 | + const icon = this.add.text( |
| 91 | + target.x + offsetX, |
| 92 | + target.y + offsetY, |
| 93 | + '🛈', |
| 94 | + { |
| 95 | + fontSize: '25px', |
| 96 | + color: '#ffffff', |
| 97 | + // backgroundColor: '#0000ff', |
| 98 | + fontStyle: 'normal', |
| 99 | + padding: { x: 8, y: 6 }, |
| 100 | + fontFamily: 'Verdana', |
| 101 | + } |
| 102 | + ) |
| 103 | + .setScrollFactor(0) |
| 104 | + .setDepth(9999) |
| 105 | + .setOrigin(0.5) |
| 106 | + .setResolution(2) |
| 107 | + .setInteractive(); |
| 108 | + |
| 109 | + // console.log( |
| 110 | + // `🧷 Info icon attached to ${target.texture.key} at (${icon.x}, ${icon.y})` |
| 111 | + // ); |
| 112 | + |
| 113 | +icon.on('pointerover', (pointer: Phaser.Input.Pointer) => { |
| 114 | + const hoverWindowX = pointer.x + 230; |
| 115 | + const hoverWindowY = pointer.y - 20; |
| 116 | + |
| 117 | + if (!this.hoverWindow) { |
| 118 | + const hoverText = this.getInfoText(imageKey); |
| 119 | + |
| 120 | + // Step 1: Create the text and hide it first |
| 121 | + this.hoverWindowText = this.add.text( |
| 122 | + hoverWindowX, |
| 123 | + hoverWindowY, |
| 124 | + hoverText, |
| 125 | + { |
| 126 | + fontFamily: 'Verdana', |
| 127 | + fontSize: '16px', |
| 128 | + color: '#ffffff', |
| 129 | + wordWrap: { width: 500 }, |
| 130 | + } |
| 131 | + ) |
| 132 | + .setScrollFactor(0) |
| 133 | + .setDepth(1012) |
| 134 | + .setAlpha(1) |
| 135 | + .setVisible(false) |
| 136 | + .setOrigin(0.5, 0.5) |
| 137 | + .setResolution(2); |
| 138 | + |
| 139 | + |
| 140 | + // Step 2: Getting the text size |
| 141 | + const bounds = this.hoverWindowText.getBounds(); |
| 142 | + const padding = 20; |
| 143 | + |
| 144 | + // Step 3: Adding a Background |
| 145 | + this.hoverWindow = this.add.rectangle( |
| 146 | + hoverWindowX, |
| 147 | + hoverWindowY, |
| 148 | + bounds.width + padding, |
| 149 | + bounds.height + padding, |
| 150 | + 0x000000 |
| 151 | + ) |
| 152 | + .setScrollFactor(0) |
| 153 | + .setDepth(1011) |
| 154 | + .setAlpha(0.9) |
| 155 | + .setStrokeStyle(2, 0xffffff); |
| 156 | + |
| 157 | + // Step 4: Displaying Text |
| 158 | + this.hoverWindowText.setVisible(true); |
| 159 | + } |
| 160 | +}); |
| 161 | + |
| 162 | + icon.on('pointerout', () => { |
| 163 | + this.hoverWindow?.destroy(); |
| 164 | + this.hoverWindowText?.destroy(); |
| 165 | + this.hoverWindow = undefined; |
| 166 | + this.hoverWindowText = undefined; |
| 167 | + }); |
| 168 | +} |
| 169 | + |
| 170 | + |
| 171 | + |
| 172 | +// helper method: returns the corresponding message text based on the imageKey |
| 173 | +private getInfoText(imageKey: string): string { |
| 174 | + switch(imageKey) { |
| 175 | + case 'baseball_groundtruth': |
| 176 | + return baseballGroundTruth; |
| 177 | + case 'kidney_groundtruth': |
| 178 | + return kidneyGroundTruth; |
| 179 | + default: |
| 180 | + return 'Dataset Info'; |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +// private attachInfoIcon( |
| 185 | +// target: Phaser.GameObjects.Image, |
| 186 | +// imageKey: string, |
| 187 | +// offsetX = 35, |
| 188 | +// offsetY = -35 |
| 189 | +// ) { |
| 190 | +// const x = target.x + offsetX; |
| 191 | +// const y = target.y + offsetY; |
| 192 | + |
| 193 | +// const circle = this.add.circle(0, 0, 16, 0xcccccc) |
| 194 | +// .setScrollFactor(0); |
| 195 | + |
| 196 | +// const text = this.add.text(0, 0, 'i', { |
| 197 | +// fontSize: '18px', |
| 198 | +// color: '#000000', |
| 199 | +// fontFamily: 'Verdana', |
| 200 | +// }) |
| 201 | +// .setOrigin(0.5) |
| 202 | +// .setScrollFactor(0); |
| 203 | + |
| 204 | +// const icon = this.add.container(x, y, [circle, text]) |
| 205 | +// .setSize(32, 32) |
| 206 | +// .setInteractive( |
| 207 | +// new Phaser.Geom.Rectangle(-16, -16, 32, 32), |
| 208 | +// Phaser.Geom.Rectangle.Contains |
| 209 | +// ) |
| 210 | +// .setScrollFactor(0) |
| 211 | +// .setDepth(10000); |
| 212 | + |
| 213 | +// console.log(`Info icon attached to ${target.texture.key} at (${icon.x}, ${icon.y})`); |
| 214 | +// console.log(`Target button depth: ${target.depth}, Icon depth: ${icon.depth}`); |
| 215 | + |
| 216 | +// icon.on('pointerover', (pointer: Phaser.Input.Pointer) => { |
| 217 | +// const hoverWindowX = pointer.x + 20; |
| 218 | +// const hoverWindowY = pointer.y - 20; |
| 219 | + |
| 220 | +// if (!this.hoverWindow) { |
| 221 | +// this.hoverWindow = this.add.rectangle( |
| 222 | +// hoverWindowX, |
| 223 | +// hoverWindowY, |
| 224 | +// 135, |
| 225 | +// 50, |
| 226 | +// 0x000000 |
| 227 | +// ) |
| 228 | +// .setScrollFactor(0) |
| 229 | +// .setDepth(10001) |
| 230 | +// .setAlpha(0.9) |
| 231 | +// .setStrokeStyle(2, 0xffffff); |
| 232 | + |
| 233 | +// this.hoverWindowText = this.add.text( |
| 234 | +// hoverWindowX, |
| 235 | +// hoverWindowY, |
| 236 | +// this.getInfoText(imageKey) |
| 237 | +// ) |
| 238 | +// .setScrollFactor(0) |
| 239 | +// .setDepth(10002) |
| 240 | +// .setAlpha(1) |
| 241 | +// .setOrigin(0.5, 0.5) |
| 242 | +// .setStyle({ |
| 243 | +// fontFamily: 'Verdana', |
| 244 | +// fontSize: '12px', |
| 245 | +// color: '#ffffff', |
| 246 | +// }); |
| 247 | +// } |
| 248 | +// }); |
| 249 | + |
| 250 | +// icon.on('pointerout', () => { |
| 251 | +// this.hoverWindow?.destroy(); |
| 252 | +// this.hoverWindowText?.destroy(); |
| 253 | +// this.hoverWindow = undefined; |
| 254 | +// this.hoverWindowText = undefined; |
| 255 | +// }); |
| 256 | + |
| 257 | +// return icon; |
| 258 | +// } |
82 | 259 |
|
83 | 260 |
|
84 | 261 | constructor() { |
@@ -568,15 +745,19 @@ return result; |
568 | 745 | .setScale(1.5) |
569 | 746 | .on("pointerover", (pointer:any)=>{ |
570 | 747 | console.log("pointer over"); |
| 748 | + |
| 749 | + const hoverWindowX = pointer.x + 50 |
| 750 | + const hoverWindowY = pointer.y + 50 |
| 751 | + |
571 | 752 | if(!this.hoverWindow){ |
572 | 753 | this.hoverWindow = this |
573 | 754 | .add |
574 | | - .rectangle(pointer.x, pointer.y, 135, 50, 0x000000) |
| 755 | + .rectangle(hoverWindowX, hoverWindowY, 135, 50, 0x000000) |
575 | 756 | .setScrollFactor(0) |
576 | 757 | .setDepth(1011) |
577 | 758 | .setAlpha(1) |
578 | 759 | .setStrokeStyle(2, 0xffffff); |
579 | | - this.hoverWindowText = this.add.text(pointer.x, pointer.y, "Baseball Player\nDataset") |
| 760 | + this.hoverWindowText = this.add.text(hoverWindowX, hoverWindowY, "Baseball Player\nDataset") |
580 | 761 | .setScrollFactor(0) |
581 | 762 | .setDepth(1012) |
582 | 763 | .setAlpha(1) |
@@ -616,14 +797,16 @@ return result; |
616 | 797 | this.baseBallBtn.setDepth(998); |
617 | 798 | this.registry.set('currentDataset', 'baseball'); |
618 | 799 |
|
619 | | - |
620 | 800 | } else { |
621 | 801 | this.selectedDataset = "none"; |
622 | 802 | this.selectedText?.destroy(); |
623 | 803 | this.baseBallBtn.setDepth(1010); |
624 | 804 |
|
625 | 805 | } |
626 | 806 | }); |
| 807 | + console.log("ready to attach info icon for baseball"); |
| 808 | + this.attachInfoIcon(this.baseBallBtn, 'baseball_groundtruth'); |
| 809 | + |
627 | 810 |
|
628 | 811 | this.add.text(0, 280, 'Start\nSimulation') |
629 | 812 | .setScrollFactor(0) |
@@ -656,15 +839,18 @@ return result; |
656 | 839 | .setScale(1.5) |
657 | 840 | .on("pointerover", (pointer:any)=>{ |
658 | 841 | console.log("pointer over"); |
| 842 | + const hoverWindowX = pointer.x + 50 |
| 843 | + const hoverWindowY = pointer.y + 50 |
| 844 | + |
659 | 845 | if(!this.hoverWindow){ |
660 | 846 | this.hoverWindow = this |
661 | 847 | .add |
662 | | - .rectangle(pointer.x, pointer.y, 135, 50, 0x000000) |
| 848 | + .rectangle(hoverWindowX, hoverWindowY, 135, 50, 0x000000) |
663 | 849 | .setScrollFactor(0) |
664 | 850 | .setDepth(1011) |
665 | 851 | .setAlpha(1) |
666 | 852 | .setStrokeStyle(2, 0xffffff); |
667 | | - this.hoverWindowText = this.add.text(pointer.x, pointer.y, "Kidney Treatments\nDataset") |
| 853 | + this.hoverWindowText = this.add.text(hoverWindowX, hoverWindowY, "Kidney Treatments\nDataset") |
668 | 854 | .setScrollFactor(0) |
669 | 855 | .setDepth(1012) |
670 | 856 | .setAlpha(1) |
@@ -705,14 +891,17 @@ return result; |
705 | 891 | .disableInteractive(); |
706 | 892 | this.kidneyBtn.setDepth(998); |
707 | 893 | this.registry.set('currentDataset', 'kidney'); |
708 | | - |
| 894 | + |
709 | 895 | } else { |
710 | 896 | this.selectedDataset = "none"; |
711 | 897 | this.selectedText?.destroy(); |
712 | 898 | this.kidneyBtn.setDepth(1010); |
713 | 899 |
|
714 | 900 | } |
715 | 901 | }); |
| 902 | + console.log("ready to attach info icon for kidney"); |
| 903 | + |
| 904 | + this.attachInfoIcon(this.kidneyBtn, 'kidney_groundtruth'); |
716 | 905 |
|
717 | 906 | this.debateStartBtn.on('pointerdown', async () => { |
718 | 907 | this.registry.set('isWorkflowRunning', true); |
|
0 commit comments