import Vue from 'vue' ; import Vuex , { ActionContext } from 'vuex' ; import { remote } from 'electron' ; import { InspectorLogEntry , LogEntryType } from '@/scripts/renderer/InspectorLogEntry' ; import { ShaderType , LanguageVersion } from '@/scripts/renderer/_constants' ; import { UniformEditor } from '@/scripts/renderer/UniformEditor' ; import { ShaderLog } from '@/scripts/editor/ShaderView' ; import { UniformState } from './scripts/renderer/UniformsCache' ; export interface State { platform : string ; vertexSource : string ; fragmentSource : string ; languageVersion : LanguageVersion ; activeShader : ShaderType ; editorClean : boolean ; model : string ; animation : boolean ; wireframe : boolean ; vertexLog : ShaderLog ; fragmentLog : ShaderLog ; compilationSucceeded : boolean ; uniforms : UniformState [ ] ; uniformsEditors : UniformEditor [ ] ; availableTextures : string [ ] ; availableTextureUnitsCount : number ; texturesAssignedToTextureUnits : string [ ] ; textureUnitToUpdate : { unit : number ; texture : string ; } ; texturesLoaded : boolean ; texturesToRestore ? : string [ ] ; windowReady : boolean ; } export interface StateSaveInfo { vertexSource : string ; fragmentSource : string ; model : string ; uniforms : UniformState [ ] ; textureUnits ? : string [ ] ; } Vue . use ( Vuex ) ; const state = { platform : remote . process . platform , vertexSource : '' , fragmentSource : '' , languageVersion : LanguageVersion . GLSL_ES300 , activeShader : ShaderType . Vertex , editorClean : true , model : '' , animation : true , wireframe : false , vertexLog : { errors : [ ] , warnings : [ ] } as ShaderLog , fragmentLog : { errors : [ ] , warnings : [ ] } as ShaderLog , compilationSucceeded : true , uniforms : [ ] , uniformsEditors : [ ] as UniformEditor [ ] , availableTextures : [ ] as string [ ] , availableTextureUnitsCount : 0 , texturesAssignedToTextureUnits : [ ] as string [ ] , textureUnitToUpdate : { unit : 0 , texture : '' } , texturesLoaded : false , windowReady : false } ; const mutations = { SET_VERTEX_SOURCE ( state , value ) { state . vertexSource = value ; } , SET_FRAGMENT_SOURCE ( state , value ) { state . fragmentSource = value ; } , SET_LANGUAGE_VERSION ( state , value ) { state . languageVersion = value ; } , SET_ACTIVE_SHADER ( state , value ) { state . activeShader = value ; } , SET_EDITOR_CLEAN ( state , value ) { state . editorClean = value ; } , SET_ANIMATION ( state , value ) { state . animation = value ; } , SET_MODEL ( state , value ) { state . model = value ; } , SET_WIREFRAME ( state , value ) { state . wireframe = value ; } , SET_LOGS ( state , log ) { const vertexLog = { errors : [ ] , warnings : [ ] } ; const fragmentLog = { errors : [ ] , warnings : [ ] } ; for ( let entry of log ) { const log = ( entry . shader === ShaderType . Vertex ) ? vertexLog : fragmentLog ; const entries = ( entry . type === LogEntryType . Error ) ? log . errors : log . warnings ; const lineNumber = entry . line - 1 ; const lineEntries = entries . find ( ( [ line ] ) => line === lineNumber ) ; if ( lineEntries === undefined ) { entries . push ( [ lineNumber , [ entry . description ] ] ) ; } else { lineEntries [ 1 ] . push ( entry . description ) ; } } state . vertexLog = vertexLog ; state . fragmentLog = fragmentLog ; } , CLEAR_LOG_LINE ( state , { shader , line } ) { const log = ( shader === ShaderType . Vertex ) ? state . vertexLog : state . fragmentLog ; const errors = log . errors ; const warnings = log . warnings ; for ( let index = errors . length - 1 ; index >= 0 ; index -- ) { if ( errors [ index ] [ 0 ] === line ) { errors . splice ( index , 1 ) ; } } for ( let index = warnings . length - 1 ; index >= 0 ; index -- ) { if ( warnings [ index ] [ 0 ] === line ) { warnings . splice ( index , 1 ) ; } } if ( shader === ShaderType . Vertex ) { state . vertexLog = { errors , warnings } ; } else { state . fragmentLog = { errors , warnings } ; } } , SET_COMPILATION_SUCCEEDED ( state , value ) { state . compilationSucceeded = value ; } , SET_UNIFORMS ( state , uniforms ) { state . uniforms = uniforms ; } , SET_UNIFORMS_EDITORS ( state , editors ) { state . uniformsEditors = editors ; } , SET_AVAILABLE_TEXTURES ( state , textures ) { state . availableTextures = textures ; } , SET_AVAILABLE_TEXTURE_UNITS_COUNT ( state , value ) { state . availableTextureUnitsCount = value ; } , SET_TEXTURES_ASSIGNED_TO_TEXTURE_UNITS ( state , textures ) { state . texturesAssignedToTextureUnits = textures ; } , SET_TEXTURE_ASSIGNED_TO_TEXTURE_UNIT ( state , value ) { Vue . set ( state . texturesAssignedToTextureUnits , value . unit , value . texture ) ; } , SET_TEXTURE_UNIT_TO_UPDATE ( state , value ) { state . textureUnitToUpdate = value ; } , SET_TEXTURES_LOADED ( state , value ) { state . texturesLoaded = value ; } , SET_TEXTURES_TO_RESTORE ( state , textures ) { state . texturesToRestore = textures ; } , SET_WINDOW_READY ( state , value ) { state . windowReady = value ; } } ; const getters = { errorsCount ( state ) { const vertexErrors = state . vertexLog . errors . reduce ( ( previous , current ) => previous + current [ 1 ] . length , 0 ) ; const fragmentErrors = state . fragmentLog . errors . reduce ( ( previous , current ) => previous + current [ 1 ] . length , 0 ) ; return vertexErrors + fragmentErrors ; } , warningsCount ( state ) { const vertexWarnings = state . vertexLog . warnings . reduce ( ( previous , current ) => previous + current [ 1 ] . length , 0 ) ; const fragmentWarnings = state . fragmentLog . warnings . reduce ( ( previous , current ) => previous + current [ 1 ] . length , 0 ) ; return vertexWarnings + fragmentWarnings ; } , documentHasUnsavedChanges ( state ) { return ! state . editorClean ; } , saveInfo ( state ) { const saveInfo = { vertexSource : state . vertexSource , fragmentSource : state . fragmentSource , model : state . model , uniforms : state . uniforms , textureUnits : state . texturesAssignedToTextureUnits } ; return saveInfo ; } } ; const actions = { restoreState ( context , state ) { context . commit ( 'SET_VERTEX_SOURCE' , state . vertexSource ) ; context . commit ( 'SET_FRAGMENT_SOURCE' , state . fragmentSource ) ; context . commit ( 'SET_MODEL' , state . model ) ; context . commit ( 'SET_UNIFORMS' , state . uniforms ) ; if ( state . textureUnits ) { context . commit ( 'SET_TEXTURES_TO_RESTORE' , state . textureUnits ) ; } } } ; export default new Vuex . Store ( { strict : process . env . NODE_ENV !== 'production' , state , mutations , getters , actions } ) ;