diff --git a/README.md b/README.md index 0da9e54..9795c9b 100644 --- a/README.md +++ b/README.md @@ -321,12 +321,36 @@ Change character distance model.setDistance(2) ``` +Enable Zoom feature + +```js +model.enableZoom() +``` + +Disable Zoom feature + +```js +model.disableZoom() +``` + Fullscreen ```js model.setFullscreen(false) ``` +Enable Fullscreen feature + +```js +model.enableFullScreen() +``` + +Disable Fullscreen feature + +```js +model.disableFullScreen() +``` + Change Animation ```js model.setAnimation("Run") @@ -439,6 +463,22 @@ const character2 = { window.model2 = await generateModels(1.5, `#model_3d2`, character2, "classic"); ``` +### Disable zoom effect. +To disable zoom effect on mouse wheel, change following vars to: + +```js + window.zoomDisabled = true +``` + This value can be changed by enableZoom and disableZoom function. + +### Disable fullscreen mode. +To disable zoom effect on mouse wheel, change following vars to: + +```js + window.fullScreenDisabled = true +``` +This value can be changed by enableZoom and disableZoom function. + # Updates As this library is based on a minified version of the Wowhead model viewer, regular upgrades of this library may require you to clear your cached data. If you are using a Docker `bypass-cors-policies` container, you can follow these steps to clean up the cache: diff --git a/index.js b/index.js index de1409e..252539a 100644 --- a/index.js +++ b/index.js @@ -17,35 +17,35 @@ import "./setup.js" * @param env {('classic'|'live')}: select game enve * @returns {Promise} */ -async function generateModels(aspect, containerSelector, model, env=`live`) { - let modelOptions - let fullOptions +async function generateModels(aspect, containerSelector, model, env=`live`, options) { + let zamingModelOptions + let zamingFullOptions if (model.id && model.type) { const {id, type} = model - modelOptions = {models: {id, type}} + zamingModelOptions = {models: {id, type}} } else { const {race, gender} = model // CHARACTER OPTIONS // This is how we describe a character properties - fullOptions = await findRaceGenderOptions( + zamingFullOptions = await findRaceGenderOptions( race, gender ) - modelOptions = optionsFromModel(model, fullOptions) + zamingModelOptions = optionsFromModel(model, zamingFullOptions) } if(env === `classic`) { - modelOptions = { + zamingModelOptions = { dataEnv: `classic`, env: `classic`, gameDataEnv: `classic`, hd: false, - ...modelOptions + ...zamingModelOptions } } else { - modelOptions = { + zamingModelOptions = { hd: true, - ...modelOptions + ...zamingModelOptions } } const models = { @@ -54,14 +54,15 @@ async function generateModels(aspect, containerSelector, model, env=`live`) { // eslint-disable-next-line no-undef container: jQuery(containerSelector), aspect: aspect, - ...modelOptions + ...zamingModelOptions } console.log(`Creating viewer with options`, models) // eslint-disable-next-line no-undef - const wowModelViewer = await new WowModelViewer(models) - if(fullOptions) { - wowModelViewer.currentCharacterOptions = fullOptions + const wowModelViewer = await new WowModelViewer(containerSelector, options, models) + + if(zamingFullOptions) { + wowModelViewer.currentCharacterOptions = zamingFullOptions wowModelViewer.characterGender = model.gender wowModelViewer.characterRace = model.race diff --git a/setup.js b/setup.js index fcecf93..702c029 100644 --- a/setup.js +++ b/setup.js @@ -4,6 +4,12 @@ if(!window.CONTENT_PATH){ if(!window.WOTLK_TO_RETAIL_DISPLAY_ID_API){ window.WOTLK_TO_RETAIL_DISPLAY_ID_API=`https://wotlk.murlocvillage.com/api/items` } +if(!window.fullScreenDisabled){ + window.fullScreenDisabled = false +} +if(!window.zoomDisabled){ + window.zoomDisabled = false +} class WebP { getImageExtension() { @@ -59,3 +65,42 @@ const WH = window.WH export { WH, } + +function firstOnApply (opts) { + const elements = opts.elements + const events = opts.events + const isDelegated = opts.isDelegated || false + elements.each(function (i, element) { + const eventsListeners = $._data(element, 'events') + $.each(events, function (i, event) { + const curEventListeners = eventsListeners[event] + const delegatedListeners = curEventListeners.slice(0, curEventListeners.delegateCount) + const vanillaListeners = curEventListeners.slice(curEventListeners.delegateCount) + + if (isDelegated) { + delegatedListeners.unshift(delegatedListeners.pop()) + Array.prototype.splice.apply( + curEventListeners, + [0, curEventListeners.delegateCount].concat(delegatedListeners) + ) + } else { + vanillaListeners.unshift(vanillaListeners.pop()) + Array.prototype.splice.apply( + curEventListeners, + [curEventListeners.delegateCount, vanillaListeners.length].concat(vanillaListeners) + ) + } + }) + }) +} + +$.fn.firstOn = function () { + const args = $.makeArray(arguments) + $.fn.on.apply(this, args) + const eventsString = args[0] + firstOnApply({ + elements: this, + events: eventsString.split(' ') + }) + return this +} diff --git a/types/character_modeling.d.ts b/types/character_modeling.d.ts index cad1336..4a30774 100644 --- a/types/character_modeling.d.ts +++ b/types/character_modeling.d.ts @@ -15,8 +15,8 @@ export function optionsFromModel(model: any, fullOptions: {}): { items: (any | any[]); } | { models: { - id; - type; + id: any; + type: any; }; }; /** @@ -33,7 +33,17 @@ export function findRaceGenderOptions(race: number, gender: number): Promise} */ -export function findItemsInEquipments(equipments: any, env?: ('classic' | 'live')): Promise; +export function findItemsInEquipments(equipments: any[{ + item: { + entry: number; + displayid: number; + }; + transmog: { + entry: number; + displayid: number; + }; + slot: number; +}], env?: ("classic" | "live")): Promise; /** * * @param item{number}: Item id @@ -42,7 +52,7 @@ export function findItemsInEquipments(equipments: any, env?: ('classic' | 'live' * @param env {('classic'|'live')}: select game env * @return {Promise} */ -export function getDisplaySlot(item: number, slot: number, displayId: number, env?: ('classic' | 'live')): Promise; +export function getDisplaySlot(item: number, slot: number, displayId: number, env?: ("classic" | "live")): Promise; /** * * @param {Object} character - The character object. diff --git a/types/index.d.ts b/types/index.d.ts index eb17bc7..61c2321 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -10,7 +10,7 @@ import { findRaceGenderOptions } from "./character_modeling.js"; export function generateModels(aspect: number, containerSelector: string, model: {} | { id: number; type: number; -}, env?: ('classic' | 'live')): Promise; +}, env?: ("classic" | "live")): Promise; import { getDisplaySlot } from "./character_modeling.js"; import { findItemsInEquipments } from "./character_modeling.js"; import { modelingType } from "./character_modeling.js"; diff --git a/types/wow_model_viewer.d.ts b/types/wow_model_viewer.d.ts index 952f226..1e9582f 100644 --- a/types/wow_model_viewer.d.ts +++ b/types/wow_model_viewer.d.ts @@ -4,6 +4,22 @@ export class WowModelViewer { * @returns {Array.} */ getListAnimations(): Array; + /** + * Enable zoom feature + */ + enableZoom(): void; + /** + * Enable zoom feature + */ + disableZoom(): void; + /** + * Enable zoom feature + */ + enableFullScreen(): void; + /** + * Enable zoom feature + */ + disableFullScreen(): void; /** * Change character distance * @param {number} val @@ -51,10 +67,10 @@ export class WowModelViewer { _currentCharacterOptions: number; _characterGender: any; _characterRace: any; - set currentCharacterOptions(arg: any); + set currentCharacterOptions(value: any); get currentCharacterOptions(): any; - set characterGender(arg: any); + set characterGender(value: any); get characterGender(): any; - set characterRace(arg: any); + set characterRace(value: any); get characterRace(): any; } diff --git a/wow_model_viewer.js b/wow_model_viewer.js index 6cf4220..0677437 100644 --- a/wow_model_viewer.js +++ b/wow_model_viewer.js @@ -3,6 +3,23 @@ import {getCharacterOptions} from "./character_modeling.js" // eslint-disable-next-line no-undef class WowModelViewer extends ZamModelViewer { + + zoomDisabled; + fullScreenDisabled; + constructor(containerSelector, options, zamconfig) { + super(zamconfig); + this.zoomDisabled = options.zoomDisabled ?? false; + this.fullScreenDisabled = options.fullScreenDisabled ?? false; + + // override wowhead events listeners + $(containerSelector + ' canvas').firstOn('dblclick', (event) => { + if ( this.fullScreenDisabled === true) event.stopImmediatePropagation(); + }) + $(containerSelector + ' canvas').firstOn('DOMMouseScroll mousewheel', (event) => { + if ( this.zoomDisabled === true) event.stopImmediatePropagation(); + }) + } + /** * Returns the list of animation names * @returns {Array.} @@ -12,6 +29,34 @@ class WowModelViewer extends ZamModelViewer { return [...new Set(this.renderer.models[0].am.map(e => e.l))] } + /** + * Enable zoom feature + */ + enableZoom() { + window.zoomDisabled = false; + } + + /** + * Enable zoom feature + */ + disableZoom() { + window.zoomDisabled = true; + } + + /** + * Enable zoom feature + */ + enableFullScreen() { + window.fullScreenDisabled = false; + } + + /** + * Enable zoom feature + */ + disableFullScreen() { + window.fullScreenDisabled = true; + } + /** * Change character distance * @param {number} val