Files
GlModelViewer/includes/GlModelViewer.php
2024-10-23 21:01:32 -07:00

111 lines
4.4 KiB
PHP

<?php
use MediaWiki\MediaWikiServices;
class GlModelViewer extends ImageHandler {
public static function onMimeMagicInit(MimeAnalyzer $mime) {
$mime->addExtraTypes('model/gltf-binary glb gltf');
$mime->addExtraInfo('model/gltf-binary [DRAWING]');
}
public static function onMimeMagicImproveFromExtension( MimeAnalyzer $mimeAnalyzer, $ext, &$mime ) {
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
$mime = 'model/gltf-binary';
}
}
function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
}
public static function onBeforePageDisplay(OutputPage $out) {
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
if ($findGltf[0]) {
$out->addModules('ext.glmv');
}
}
public static function onImageBeforeProduceHTML( DummyLinker &$linker, Title &$title, &$file, array &$frameParams, array &$handlerParams, &$time, &$result, Parser $parser, string &$query, &$widthOption ) {
if ($file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$result = self::buildViewer($file->getDescriptionText(), $file->getFullUrl(), $frameParams);
return false;
}
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
$file = $imagepage->getFile();
if ($file->getMimeType() !== 'model/gltf-binary') {
return;
}
$out->addModules('ext.glmv');
$viewer = self::buildViewer($file->getDescriptionText(), $file->getFullUrl(), ['class' => 'view-default']);
$out->addHtml(Html::rawElement('div',['id' => 'file', 'class' => 'fullModelView'],$viewer));
}
public static function onAlternateEditPreview( EditPage $editor, Content $content, string &$previewHTML, ?ParserOutput &$parserOutput ) {
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile($editor->getTitle());
if (!$file || $file->getMimeType() !== 'model/gltf-binary') {
return true;
}
$out = $editor->getContext()->getOutput();
$out->addModules('ext.glmv');
$previewViewer = self::buildViewer($content->getText(), $file->getFullUrl(), ['class' => 'view-default']);
$addButtonAttr = array(
'class' => 'AddHotspot',
'onclick' => 'readyAddHotspot()'
);
$addHsButton = Html::rawElement('button',$addButtonAttr,'Add a new hotspot');
$previewHTML = Html::rawElement('div',NULL,$previewViewer.$addHsButton);
return false;
}
private static function buildViewer($inText, $srcUrl, $frameParams) {
preg_match('/<pre>([\S\s]*?)<\/pre>/',$inText,$modelDescript);
$metadata = json_decode($modelDescript[1], true);
if (isset($frameParams['class'])) {
preg_match('/view-(\S*)/',$frameParams['class'],$viewClassExtract);
$viewClass = $viewClassExtract[1];
} else {
$viewClass = 'default';
}
if (isset($metadata['annotations'])) {
$hotspots = [];
foreach($metadata['annotations'] as $idx => $an) {
$elAnnot = Html::rawElement('div',['class' => 'HotspotAnnotation HiddenAnnotation'],$an['label']);
$hsDefault = array(
'class' => 'Hotspot',
'slot' => 'hotspot-'.($idx +1),
'onmousedown' => 'event.stopPropagation()',
'ontouchstart' => 'event.stopPropagation()',
'onclick' => 'onAnnotation(event)'
);
$attrHotspot = array_merge($hsDefault, $an);
$elHotspot = Html::rawElement('button',$attrHotspot,$elAnnot.($idx +1));
array_push($hotspots, $elHotspot);
}
}
$attrModelView = $metadata['viewerConfig'][$viewClass];
$attrModelView = array_merge(['src' => $srcUrl, 'class' => 'mv-model', 'interpolation-decay' => '100'], $attrModelView);
$attrModelView['style'] = 'width: 100%; height: 100%; min-height: 400px;';
$elModel = Html::rawElement('model-viewer', $attrModelView, implode($hotspots));
$attrContainer = array(
'style' => "width: 800px; height: 600px;",
'onmousedown' => 'clearAnnotations()',
'ontouchstart' => 'clearAnnotations()'
);
return Html::rawElement('div', $attrContainer, $elModel);
}
public static function console_log($data, $add_script_tags = false) {
#Usage:
#echo '<script>';
#self::console_log('logged string');
#echo '</script>';
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
if ($add_script_tags) {
$command = '<script>'. $command . '</script>';
}
echo $command;
}
}