Tidy up PHP code
Signed-off-by: Justin Georgi <justin.georgi@gmail.com>
This commit is contained in:
@@ -5,19 +5,19 @@ use ImageHandler;
|
|||||||
use Html;
|
use Html;
|
||||||
|
|
||||||
class GlModelHandler extends ImageHandler {
|
class GlModelHandler extends ImageHandler {
|
||||||
/**
|
/**
|
||||||
* Model cannot be displayed directly in a browser but can be rendered.
|
* Model cannot be displayed directly in a browser but can be rendered.
|
||||||
*
|
*
|
||||||
* @param File $file
|
* @param File $file
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function mustRender( $file ) {
|
public function mustRender( $file ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model can be rendered.
|
* Model can be rendered.
|
||||||
*
|
*
|
||||||
* @param File $file
|
* @param File $file
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
@@ -25,53 +25,53 @@ class GlModelHandler extends ImageHandler {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a MediaTransformOutput object representing the transformed output.
|
* Get a MediaTransformOutput object representing the transformed output.
|
||||||
*
|
*
|
||||||
* @param File $image
|
* @param File $image
|
||||||
* @param string $dstPath Filesystem destination path
|
* @param string $dstPath Filesystem destination path
|
||||||
* @param string $dstUrl Destination URL to use in output HTML
|
* @param string $dstUrl Destination URL to use in output HTML
|
||||||
* @param array $params Arbitrary set of parameters validated by $this->validateParam()
|
* @param array $params Arbitrary set of parameters validated by $this->validateParam()
|
||||||
* @param int $flags A bitfield, may contain self::TRANSFORM_LATER
|
* @param int $flags A bitfield, may contain self::TRANSFORM_LATER
|
||||||
* @return MediaTransformOutput
|
* @return MediaTransformOutput
|
||||||
*/
|
*/
|
||||||
public function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
|
public function doTransform($image, $dstPath, $dstUrl, $params, $flags = 0) {
|
||||||
return new GlModelTransformOutput($image, $params);
|
return new GlModelTransformOutput($image, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check the incoming media parameters
|
* Check the incoming media parameters
|
||||||
*
|
*
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string $value
|
* @param string $value
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public function validateParam( $name, $value ) {
|
public function validateParam( $name, $value ) {
|
||||||
if (in_array($name, ['width', 'height'])) {
|
if (in_array($name, ['width', 'height'])) {
|
||||||
return $value > 0;
|
return $value > 0;
|
||||||
} else if (in_array($name, ['view', 'hsset'])) {
|
} else if (in_array($name, ['view', 'hsset'])) {
|
||||||
return true;
|
return true;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Small helper function to display information on the browser console
|
* Small helper function to display information on the browser console
|
||||||
*
|
*
|
||||||
* Usage:
|
* Usage:
|
||||||
* echo '<script>';
|
* echo '<script>';
|
||||||
* self::console_log('logged string');
|
* self::console_log('logged string');
|
||||||
* echo '</script>';
|
* echo '</script>';
|
||||||
*
|
*
|
||||||
* @param $data information to display
|
* @param $data information to display
|
||||||
* @param bool $add_script_tags true to put information is inside complete script tag
|
* @param bool $add_script_tags true to put information is inside complete script tag
|
||||||
*/
|
*/
|
||||||
public static function console_log($data, $add_script_tags = false) {
|
public static function console_log($data, $add_script_tags = false) {
|
||||||
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
||||||
if ($add_script_tags) {
|
if ($add_script_tags) {
|
||||||
$command = '<script>'. $command . '</script>';
|
$command = '<script>'. $command . '</script>';
|
||||||
|
}
|
||||||
|
echo $command;
|
||||||
}
|
}
|
||||||
echo $command;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -6,117 +6,119 @@ use Html;
|
|||||||
use ParserOutput;
|
use ParserOutput;
|
||||||
|
|
||||||
class GlModelHooks {
|
class GlModelHooks {
|
||||||
/**
|
/**
|
||||||
* MWHook: Add gltf mime types to MimeMagic
|
* MWHook: Add gltf mime types to MimeMagic
|
||||||
*
|
*
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicInit
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicInit
|
||||||
*
|
*
|
||||||
* @param MimeAnalyzer $mime Instance of MW MimeAnalyzer object
|
* @param MimeAnalyzer $mime Instance of MW MimeAnalyzer object
|
||||||
*/
|
*/
|
||||||
public static function onMimeMagicInit($mime) {
|
public static function onMimeMagicInit($mime) {
|
||||||
$mime->addExtraTypes('model/gltf-binary glb gltf');
|
$mime->addExtraTypes('model/gltf-binary glb gltf');
|
||||||
$mime->addExtraInfo('model/gltf-binary [MULTIMEDIA]');
|
$mime->addExtraInfo('model/gltf-binary [MULTIMEDIA]');
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MWHook: Make sure that gltf files get the proper mime assignement on upload
|
|
||||||
*
|
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicImproveFromExtension
|
|
||||||
*
|
|
||||||
* @param MimeAnalyzer $mimeAnalyzer Instance of MW MimeAnalyzer object
|
|
||||||
* @param string $ext extention of upload file
|
|
||||||
* @param string &$mime current assigned mime type
|
|
||||||
*/
|
|
||||||
public static function onMimeMagicImproveFromExtension( $mimeAnalyzer, $ext, &$mime ) {
|
|
||||||
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
|
|
||||||
$mime = 'model/gltf-binary';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MWHook: Load the js and css modules if model-viewer element is found in the html output
|
|
||||||
*
|
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
|
||||||
*
|
|
||||||
* @param OutputPage $out compiled page html and manipulation methods
|
|
||||||
*/
|
|
||||||
public static function onBeforePageDisplay($out) {
|
|
||||||
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
|
|
||||||
if ($findGltf[0]) {
|
|
||||||
$mvScriptAttr = array(
|
|
||||||
'src' => 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js',
|
|
||||||
'type' => 'module'
|
|
||||||
);
|
|
||||||
$out->addHeadItems(Html::rawElement('script',$mvScriptAttr));
|
|
||||||
$out->addModules('ext.glmv');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MWHook: Display model on file page
|
|
||||||
*
|
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageOpenShowImageInlineBefore
|
|
||||||
*
|
|
||||||
* @param ImagePage $imagepage information regarding the page for the image file
|
|
||||||
* @param OutputPage $out compiled page html and manipulation methods
|
|
||||||
*/
|
|
||||||
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
|
|
||||||
$file = $imagepage->getFile();
|
|
||||||
if ($file->getMimeType() == 'model/gltf-binary') {
|
|
||||||
$out->clearHTML();
|
|
||||||
$out->addWikiTextAsContent('[[' . $file->getTitle()->getFullText() . '|800x600px]]');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* MWHook: Display model when model file page edits are previewed
|
|
||||||
*
|
|
||||||
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEditPreview
|
|
||||||
*
|
|
||||||
* @param EditPage $editor access to information about the edit page itself
|
|
||||||
* @param Content $content access to the current content of the editor
|
|
||||||
* @param string &$previewHTML by reference access to the resultant compiled preview html
|
|
||||||
* @param ?ParserOutput &$parserOutput
|
|
||||||
* @return bool|void True to continue default processing or false to abort for custom processing
|
|
||||||
*/
|
|
||||||
public static function onAlternateEditPreview( $editor, $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');
|
* MWHook: Make sure that gltf files get the proper mime assignement on upload
|
||||||
|
*
|
||||||
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600']);
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/MimeMagicImproveFromExtension
|
||||||
$previewViewer = $mvTransform->toHtml([ 'preview' => true]);
|
*
|
||||||
|
* @param MimeAnalyzer $mimeAnalyzer Instance of MW MimeAnalyzer object
|
||||||
$addButtonAttr = array(
|
* @param string $ext extention of upload file
|
||||||
'class' => 'AddHotspot',
|
* @param string &$mime current assigned mime type
|
||||||
'onclick' => 'readyAddHotspot()'
|
*/
|
||||||
);
|
public static function onMimeMagicImproveFromExtension( $mimeAnalyzer, $ext, &$mime ) {
|
||||||
$addHsButton = Html::rawElement('button',$addButtonAttr,'Add a new hotspot');
|
if ( $mime !== 'model/gltf-binary' && in_array( $ext, ['glb', 'gltf'] ) ) {
|
||||||
|
$mime = 'model/gltf-binary';
|
||||||
$previewHTML = Html::rawElement('div',NULL,$previewViewer.$addHsButton);
|
}
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* MWHook: Load the js and css modules if model-viewer element is found in the html output
|
||||||
* Small helper function to display information on the browser console
|
*
|
||||||
*
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
|
||||||
* Usage:
|
*
|
||||||
* echo '<script>';
|
* @param OutputPage $out compiled page html and manipulation methods
|
||||||
* self::console_log('logged string');
|
*/
|
||||||
* echo '</script>';
|
public static function onBeforePageDisplay($out) {
|
||||||
*
|
preg_match('/(<model-viewer src="\S*?\.(glb|gltf"))/',$out->getHTML(),$findGltf);
|
||||||
* @param $data information to display
|
if ($findGltf[0]) {
|
||||||
* @param bool $add_script_tags true to put information is inside complete script tag
|
$mvScriptAttr = array(
|
||||||
*/
|
'src' => 'https://ajax.googleapis.com/ajax/libs/model-viewer/3.5.0/model-viewer.min.js',
|
||||||
public static function console_log($data, $add_script_tags = false) {
|
'type' => 'module'
|
||||||
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
);
|
||||||
if ($add_script_tags) {
|
$out->addHeadItems(Html::rawElement('script',$mvScriptAttr));
|
||||||
$command = '<script>'. $command . '</script>';
|
$out->addModules('ext.glmv');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MWHook: Display model on file page
|
||||||
|
*
|
||||||
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/ImageOpenShowImageInlineBefore
|
||||||
|
*
|
||||||
|
* @param ImagePage $imagepage information regarding the page for the image file
|
||||||
|
* @param OutputPage $out compiled page html and manipulation methods
|
||||||
|
*/
|
||||||
|
public static function onImageOpenShowImageInlineBefore( $imagepage, $out ){
|
||||||
|
$file = $imagepage->getFile();
|
||||||
|
if ($file->getMimeType() == 'model/gltf-binary') {
|
||||||
|
$out->clearHTML();
|
||||||
|
$out->addWikiTextAsContent('[[' . $file->getTitle()->getFullText() . '|800x600px]]');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MWHook: Display model when model file page edits are previewed
|
||||||
|
*
|
||||||
|
* @see https://www.mediawiki.org/wiki/Manual:Hooks/AlternateEditPreview
|
||||||
|
*
|
||||||
|
* @param EditPage $editor access to information about the edit page itself
|
||||||
|
* @param Content $content access to the current content of the editor
|
||||||
|
* @param string &$previewHTML by reference access to the resultant compiled preview html
|
||||||
|
* @param ?ParserOutput &$parserOutput
|
||||||
|
* @return bool|void True to continue default processing or false to abort for custom processing
|
||||||
|
*/
|
||||||
|
public static function onAlternateEditPreview( $editor, $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');
|
||||||
|
|
||||||
|
$mvTransform = $file->transform([ 'width' => '800', 'hight' => '600']);
|
||||||
|
$previewViewer = $mvTransform->toHtml([ 'preview' => true]);
|
||||||
|
|
||||||
|
$addButtonAttr = array(
|
||||||
|
'class' => 'AddHotspot',
|
||||||
|
'onclick' => 'readyAddHotspot()'
|
||||||
|
);
|
||||||
|
$addHsButton = array(
|
||||||
|
Html::rawElement('button',$addButtonAttr,'Add a new hotspot')
|
||||||
|
);
|
||||||
|
|
||||||
|
$previewHTML = Html::rawElement('div',NULL,$previewViewer.implode($addHsButton));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small helper function to display information on the browser console
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* echo '<script>';
|
||||||
|
* self::console_log('logged string');
|
||||||
|
* echo '</script>';
|
||||||
|
*
|
||||||
|
* @param $data information to display
|
||||||
|
* @param bool $add_script_tags true to put information is inside complete script tag
|
||||||
|
*/
|
||||||
|
public static function console_log($data, $add_script_tags = false) {
|
||||||
|
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
||||||
|
if ($add_script_tags) {
|
||||||
|
$command = '<script>'. $command . '</script>';
|
||||||
|
}
|
||||||
|
echo $command;
|
||||||
}
|
}
|
||||||
echo $command;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -45,189 +45,192 @@ class GlModelTransformOutput extends MediaTransformOutput {
|
|||||||
if ($this->thumb) {
|
if ($this->thumb) {
|
||||||
return Html::rawElement('span',['class' => 'fake-thumbnail'],'Gltf model thumbnail');
|
return Html::rawElement('span',['class' => 'fake-thumbnail'],'Gltf model thumbnail');
|
||||||
}
|
}
|
||||||
//return Html::rawElement( 'div',[ 'class' => 'my-model-class'], $this->url . ' is a ' . $this->parameters['type'] . ' 3D model!' );
|
|
||||||
if (isset($options['img-class'])) {
|
if (isset($options['img-class'])) {
|
||||||
$this->parameters['class'] = $options['img-class'];
|
$this->parameters['class'] = $options['img-class'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['preview']) && $options['preview']) {
|
if (isset($options['preview']) && $options['preview']) {
|
||||||
$this->parameters['preview'] = $options['preview'];
|
$this->parameters['preview'] = $options['preview'];
|
||||||
}
|
}
|
||||||
|
|
||||||
return self::buildViewer($this->file->getDescriptionText(),$this->url,$this->parameters);
|
return self::buildViewer($this->file->getDescriptionText(),$this->url,$this->parameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Build model-viewer and child elements
|
* Build model-viewer and child elements
|
||||||
*
|
*
|
||||||
* This takes in the metadata text from the model page (or the current editor)
|
* This takes in the metadata text from the model page (or the current editor)
|
||||||
* and produces the html string for the model-viewer and all relevant child
|
* and produces the html string for the model-viewer and all relevant child
|
||||||
* elements.
|
* elements.
|
||||||
*
|
*
|
||||||
* @param string $inText The metadata text which must include a json formatted string inside a pre tag
|
* @param string $inText The metadata text which must include a json formatted string inside a pre tag
|
||||||
* @param string $srcUrl The full url pointing to the model file
|
* @param string $srcUrl The full url pointing to the model file
|
||||||
* @param array $frameParams The additional user defined parameters for the viewer such as hotspot and view classes
|
* @param array $frameParams The additional user defined parameters for the viewer such as hotspot and view classes
|
||||||
* @return string Html string of the complete model-viewer element inside a div container
|
* @return string Html string of the complete model-viewer element inside a div container
|
||||||
*/
|
*/
|
||||||
private function buildViewer($inText, $srcUrl, $viewParams) {
|
private function buildViewer($inText, $srcUrl, $viewParams) {
|
||||||
//Gather basic data
|
//Gather basic data
|
||||||
preg_match('/<pre>([\S\s]*?)<\/pre>/',$inText,$modelDescript);
|
preg_match('/<pre>([\S\s]*?)<\/pre>/',$inText,$modelDescript);
|
||||||
$metadata = json_decode($modelDescript[1], true);
|
$metadata = json_decode($modelDescript[1], true);
|
||||||
$viewClass = self::extractClassParams('view',$viewParams['class']);
|
$viewClass = self::extractClassParams('view',$viewParams['class']);
|
||||||
$hsSetClass = self::extractClassParams('hsset',$viewParams['class']);
|
$hsSetClass = self::extractClassParams('hsset',$viewParams['class']);
|
||||||
|
|
||||||
//Handle annotations and annotation sets
|
//Handle annotations and annotation sets
|
||||||
if (isset($metadata['annotations'])) {
|
if (isset($metadata['annotations'])) {
|
||||||
$hotspots = [];
|
$hotspots = [];
|
||||||
$annotations = [];
|
$annotations = [];
|
||||||
if ($hsSetClass != 'default' && isset($metadata['annotationSets'])) {
|
if ($hsSetClass != 'default' && isset($metadata['annotationSets'])) {
|
||||||
foreach($metadata['annotationSets'][$hsSetClass] as $includeAn) {
|
foreach($metadata['annotationSets'][$hsSetClass] as $includeAn) {
|
||||||
if (isset($metadata['annotations'][$includeAn])) {
|
if (isset($metadata['annotations'][$includeAn])) {
|
||||||
$annotations[$includeAn] = $metadata['annotations'][$includeAn];
|
$annotations[$includeAn] = $metadata['annotations'][$includeAn];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$annotations = $metadata['annotations'];
|
||||||
|
}
|
||||||
|
foreach($annotations as $label => $an) {
|
||||||
|
$elAnnot = Html::rawElement('div',['class' => 'HotspotAnnotation HiddenAnnotation'],$label);
|
||||||
|
$hsDefault = array(
|
||||||
|
'class' => 'Hotspot',
|
||||||
|
'slot' => 'hotspot-'.(count($hotspots) +1),
|
||||||
|
'ontouchstart' => 'event.stopPropagation()',
|
||||||
|
'onclick' => 'onAnnotation(event)'
|
||||||
|
);
|
||||||
|
if (isset($viewParams['preview'])) {
|
||||||
|
$hsDefault['onmousedown'] = 'grabAnnotation(event)';
|
||||||
|
$hsDefault['onmouseup'] = 'releaseAnnotation(event)';
|
||||||
|
}
|
||||||
|
$attrHotspot = array_merge($hsDefault, $an);
|
||||||
|
$elHotspot = Html::rawElement('button',$attrHotspot,$elAnnot.(count($hotspots) +1));
|
||||||
|
array_push($hotspots, $elHotspot);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
$annotations = $metadata['annotations'];
|
//Set viewer configurations or use basic default if none defined
|
||||||
}
|
if (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$viewClass])) {
|
||||||
foreach($annotations as $label => $an) {
|
$attrModelView = $metadata['viewerConfig'][$viewClass];
|
||||||
$elAnnot = Html::rawElement('div',['class' => 'HotspotAnnotation HiddenAnnotation'],$label);
|
} else {
|
||||||
$hsDefault = array(
|
$attrModelView = array('camera-controls' => true);
|
||||||
'class' => 'Hotspot',
|
|
||||||
'slot' => 'hotspot-'.(count($hotspots) +1),
|
|
||||||
'ontouchstart' => 'event.stopPropagation()',
|
|
||||||
'onclick' => 'onAnnotation(event)'
|
|
||||||
);
|
|
||||||
if (isset($viewParams['preview'])) {
|
|
||||||
$hsDefault['onmousedown'] = 'grabAnnotation(event)';
|
|
||||||
$hsDefault['onmouseup'] = 'releaseAnnotation(event)';
|
|
||||||
}
|
}
|
||||||
$attrHotspot = array_merge($hsDefault, $an);
|
|
||||||
$elHotspot = Html::rawElement('button',$attrHotspot,$elAnnot.(count($hotspots) +1));
|
|
||||||
array_push($hotspots, $elHotspot);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//Set viewer configurations or use basic default if none defined
|
//Add important additional attributes and render model-viewer with hotspots
|
||||||
if (isset($metadata['viewerConfig']) && isset($metadata['viewerConfig'][$viewClass])) {
|
$attrModelView = array_merge(['src' => $srcUrl, 'class' => 'mv-model', 'interpolation-decay' => '100', 'interaction-prompt' => 'none'], $attrModelView);
|
||||||
$attrModelView = $metadata['viewerConfig'][$viewClass];
|
$attrModelView['style'] = 'width: 100%; height: 100%;';
|
||||||
} else {
|
$hotspotHtml = (isset($hotspots)) ? implode($hotspots) : '';
|
||||||
$attrModelView = array('camera-controls' => true);
|
|
||||||
}
|
|
||||||
|
|
||||||
//Add important additional attributes and render model-viewer with hotspots
|
|
||||||
$attrModelView = array_merge(['src' => $srcUrl, 'class' => 'mv-model', 'interpolation-decay' => '100', 'interaction-prompt' => 'none'], $attrModelView);
|
|
||||||
$attrModelView['style'] = 'width: 100%; height: 100%;';
|
|
||||||
$hotspotHtml = (isset($hotspots)) ? implode($hotspots) : '';
|
|
||||||
|
|
||||||
$elModel = Html::rawElement('model-viewer', $attrModelView, $hotspotHtml);
|
|
||||||
$elMenu = self::buildViewMenu();
|
|
||||||
|
|
||||||
//Render and return container element with model-viewer
|
|
||||||
$attrContainer = array(
|
|
||||||
'class' => 'glmv-container',
|
|
||||||
'style' => 'width: ' . $this->width . 'px; height: ' . $this->height . 'px;',
|
|
||||||
'onmousedown' => 'clearAnnotations()',
|
|
||||||
'ontouchstart' => 'clearAnnotations()'
|
|
||||||
);
|
|
||||||
return Html::rawElement('div', $attrContainer, $elModel . $elMenu);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Detect and return model user params from link classes
|
|
||||||
*
|
|
||||||
* The best way to send information in a file link is by adding classes to the
|
|
||||||
* link. This function parses the array of those classes and returns the information
|
|
||||||
* or 'default'
|
|
||||||
*
|
|
||||||
* @param string $paramType the type of parameter to be extracted 'view'|'hsset'
|
|
||||||
* @param string $classList a string of class names
|
|
||||||
* @return string parameter name or 'default'
|
|
||||||
*/
|
|
||||||
private static function extractClassParams(string $paramType, ? string $classList) {
|
|
||||||
if (!in_array($paramType, ['view', 'hsset'])) {
|
|
||||||
return 'default';
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isset($classList)) {
|
$elModel = Html::rawElement('model-viewer', $attrModelView, $hotspotHtml);
|
||||||
return 'default';
|
$elMenu = self::buildViewMenu();
|
||||||
|
|
||||||
|
//Render and return container element with model-viewer
|
||||||
|
$attrContainer = array(
|
||||||
|
'class' => 'glmv-container',
|
||||||
|
'style' => 'width: ' . $this->width . 'px; height: ' . $this->height . 'px;',
|
||||||
|
'onmousedown' => 'clearAnnotations()',
|
||||||
|
'ontouchstart' => 'clearAnnotations()'
|
||||||
|
);
|
||||||
|
|
||||||
|
return Html::rawElement('div', $attrContainer, $elModel . $elMenu);
|
||||||
}
|
}
|
||||||
|
|
||||||
$searchString = '/' . $paramType . '-(\S*)/';
|
/**
|
||||||
preg_match($searchString,$classList,$extractInfo);
|
* Detect and return model user params from link classes
|
||||||
|
*
|
||||||
|
* The best way to send information in a file link is by adding classes to the
|
||||||
|
* link. This function parses the array of those classes and returns the information
|
||||||
|
* or 'default'
|
||||||
|
*
|
||||||
|
* @param string $paramType the type of parameter to be extracted 'view'|'hsset'
|
||||||
|
* @param string $classList a string of class names
|
||||||
|
* @return string parameter value or 'default'
|
||||||
|
*/
|
||||||
|
private static function extractClassParams(string $paramType, ? string $classList) {
|
||||||
|
if (!in_array($paramType, ['view', 'hsset'])) {
|
||||||
|
return 'default';
|
||||||
|
}
|
||||||
|
|
||||||
return ($extractInfo[1]) ? $extractInfo[1] : 'default';
|
if (!isset($classList)) {
|
||||||
}
|
return 'default';
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$searchString = '/' . $paramType . '-(\S*)/';
|
||||||
* Build the button menu used for viewer actions
|
preg_match($searchString,$classList,$extractInfo);
|
||||||
*
|
|
||||||
* @return string
|
|
||||||
*/
|
|
||||||
private static function buildViewMenu() {
|
|
||||||
$attrMenu = array(
|
|
||||||
'class' => 'glmv-menu'
|
|
||||||
);
|
|
||||||
$mainConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
|
|
||||||
$menuUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/menu_arrow.svg';
|
|
||||||
$attrMenuImg = array (
|
|
||||||
'class' => 'glmv-menu-image',
|
|
||||||
'src' => $menuUrl
|
|
||||||
);
|
|
||||||
$menuImg = Html::rawElement('img', $attrMenuImg, '');
|
|
||||||
|
|
||||||
$gotoUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/goto_hs.svg';
|
return ($extractInfo[1]) ? $extractInfo[1] : 'default';
|
||||||
$attrMenuButtonPrev = array (
|
}
|
||||||
'class' => 'glmv-menu-button prev-hs',
|
|
||||||
'onclick' => 'prevAnnotation(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
/**
|
||||||
'onmousedown' => 'event.stopPropagation()',
|
* Build the button menu used for viewer actions
|
||||||
'ontouchstart' => 'event.stopPropagation()'
|
*
|
||||||
);
|
* @return string
|
||||||
$attrMenuButtonNext = array (
|
*/
|
||||||
'class' => 'glmv-menu-button next-hs',
|
private static function buildViewMenu() {
|
||||||
'onclick' => 'nextAnnotation(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
$attrMenu = array(
|
||||||
'onmousedown' => 'event.stopPropagation()',
|
'class' => 'glmv-menu'
|
||||||
'ontouchstart' => 'event.stopPropagation()'
|
);
|
||||||
);
|
$mainConfig = ConfigFactory::getDefaultInstance()->makeConfig( 'main' );
|
||||||
|
$menuUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/menu_arrow.svg';
|
||||||
$slideUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/hs_slideshow.svg';
|
$attrMenuImg = array (
|
||||||
$attrMenuButtonSlides = array (
|
'class' => 'glmv-menu-image',
|
||||||
'class' => 'glmv-menu-button',
|
'src' => $menuUrl
|
||||||
'onclick' => 'event.target.toggleAttribute("toggled"); slideshowAnnotations(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
);
|
||||||
'onmousedown' => 'event.stopPropagation()',
|
$menuImg = Html::rawElement('img', $attrMenuImg, '');
|
||||||
'ontouchstart' => 'event.stopPropagation()'
|
|
||||||
);
|
$gotoUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/goto_hs.svg';
|
||||||
|
$attrMenuButtonPrev = array (
|
||||||
$hideUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/hs_hide.svg';
|
'class' => 'glmv-menu-button prev-hs',
|
||||||
$attrMenuButtonHide = array (
|
'onclick' => 'prevAnnotation(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
||||||
'class' => 'glmv-menu-button',
|
'onmousedown' => 'event.stopPropagation()',
|
||||||
'onclick' => 'event.target.toggleAttribute("toggled"); toggleAnnotations(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
'ontouchstart' => 'event.stopPropagation()'
|
||||||
'onmousedown' => 'event.stopPropagation()',
|
);
|
||||||
'ontouchstart' => 'event.stopPropagation()'
|
$attrMenuButtonNext = array (
|
||||||
);
|
'class' => 'glmv-menu-button next-hs',
|
||||||
$menuButtons = array(
|
'onclick' => 'nextAnnotation(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
||||||
Html::rawElement('div', $attrMenuButtonPrev, '<img src="' . $gotoUrl . '"></image>'),
|
'onmousedown' => 'event.stopPropagation()',
|
||||||
Html::rawElement('div', $attrMenuButtonSlides, '<img src="' . $slideUrl . '"></image>'),
|
'ontouchstart' => 'event.stopPropagation()'
|
||||||
Html::rawElement('div', $attrMenuButtonNext, '<img src="' . $gotoUrl . '"></image>'),
|
);
|
||||||
Html::rawElement('div', $attrMenuButtonHide, '<img src="' . $hideUrl . '"></image>')
|
|
||||||
);
|
$slideUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/hs_slideshow.svg';
|
||||||
|
$attrMenuButtonSlides = array (
|
||||||
return Html::rawElement('div', $attrMenu, $menuImg . implode($menuButtons));
|
'class' => 'glmv-menu-button',
|
||||||
}
|
'onclick' => 'event.target.toggleAttribute("toggled"); slideshowAnnotations(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
||||||
|
'onmousedown' => 'event.stopPropagation()',
|
||||||
/**
|
'ontouchstart' => 'event.stopPropagation()'
|
||||||
* Small helper function to display information on the browser console
|
);
|
||||||
*
|
|
||||||
* Usage:
|
$hideUrl = $mainConfig->get( 'ExtensionAssetsPath' ) . '/GlModelViewer/resources/hs_hide.svg';
|
||||||
* echo '<script>';
|
$attrMenuButtonHide = array (
|
||||||
* self::console_log('logged string');
|
'class' => 'glmv-menu-button',
|
||||||
* echo '</script>';
|
'onclick' => 'event.target.toggleAttribute("toggled"); toggleAnnotations(event.target.closest(".glmv-container").querySelector("model-viewer"))',
|
||||||
*
|
'onmousedown' => 'event.stopPropagation()',
|
||||||
* @param $data information to display
|
'ontouchstart' => 'event.stopPropagation()'
|
||||||
* @param bool $add_script_tags true to put information is inside complete script tag
|
);
|
||||||
*/
|
$menuButtons = array(
|
||||||
public static function console_log($data, $add_script_tags = false) {
|
Html::rawElement('div', $attrMenuButtonPrev, '<img src="' . $gotoUrl . '"></image>'),
|
||||||
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
Html::rawElement('div', $attrMenuButtonSlides, '<img src="' . $slideUrl . '"></image>'),
|
||||||
if ($add_script_tags) {
|
Html::rawElement('div', $attrMenuButtonNext, '<img src="' . $gotoUrl . '"></image>'),
|
||||||
$command = '<script>'. $command . '</script>';
|
Html::rawElement('div', $attrMenuButtonHide, '<img src="' . $hideUrl . '"></image>')
|
||||||
|
);
|
||||||
|
|
||||||
|
return Html::rawElement('div', $attrMenu, $menuImg . implode($menuButtons));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Small helper function to display information on the browser console
|
||||||
|
*
|
||||||
|
* Usage:
|
||||||
|
* echo '<script>';
|
||||||
|
* self::console_log('logged string');
|
||||||
|
* echo '</script>';
|
||||||
|
*
|
||||||
|
* @param $data information to display
|
||||||
|
* @param bool $add_script_tags true to put information is inside complete script tag
|
||||||
|
*/
|
||||||
|
public static function console_log($data, $add_script_tags = false) {
|
||||||
|
$command = 'console.log('. json_encode($data, JSON_HEX_TAG).');';
|
||||||
|
if ($add_script_tags) {
|
||||||
|
$command = '<script>'. $command . '</script>';
|
||||||
|
}
|
||||||
|
echo $command;
|
||||||
}
|
}
|
||||||
echo $command;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user