@@ -76,6 +76,39 @@ $(function() {
7676
7777 var view = {
7878 init : function ( ) {
79+
80+ // mouse event
81+
82+ var canvas = document . getElementById ( "gl-canvas" ) ;
83+ canvas . addEventListener ( "mousedown" , function ( evt ) {
84+ mouseFlag = 1 ;
85+ } , false ) ;
86+ canvas . addEventListener ( "mousemove" , function ( evt ) {
87+ if ( mouseFlag === 1 ) {
88+ setMousePos ( canvas , evt , 0 ) ; //add default light event;
89+ }
90+ } , false ) ;
91+ canvas . addEventListener ( "mouseup" , function ( ) {
92+ mouseFlag = 0 ;
93+ //mouseFlag = (mouseFlag ==0)?1:0;
94+ } , false ) ;
95+
96+
97+ // show light Position event
98+
99+ $ ( '#lightsPositionSelect:checkbox' ) . click ( function ( ) {
100+ var $this = $ ( this ) ;
101+ // $this will contain a reference to the checkbox
102+ if ( $this . is ( ':checked' ) ) {
103+ $ ( '#lightPosition_container' ) . css ( "display" , "block" ) ;
104+ } else {
105+ $ ( '#lightPosition_container' ) . css ( "display" , "none" ) ;
106+ }
107+ } ) ;
108+
109+
110+
111+ // "Add light" button event
79112 var addLightBtn = $ ( '#btn_addLight' ) ;
80113 addLightBtn . click ( function ( ) {
81114 octopus . addLight ( ) ;
@@ -85,7 +118,7 @@ $(function() {
85118 this . $lightList = $ ( 'ul#accordion_Lights' ) ;
86119 this . defaultLightTemplate = $ ( 'script[data-template="defaultLight"]' ) . html ( ) ;
87120 this . lightTemplate = $ ( 'script[data-template="light"]' ) . html ( ) ;
88-
121+ this . $lightMarkList = $ ( '#lightPosition_container' ) ;
89122
90123 // Delegated event to listen for removal clicks
91124 this . $lightList . on ( 'click' , '.myLightsTitle .destroy' , function ( e ) {
@@ -100,24 +133,29 @@ $(function() {
100133 render : function ( ) {
101134 // Cache vars for use in forEach() callback (performance)
102135 var $lightList = this . $lightList ,
136+ $lightMarkList = this . $lightMarkList ,
103137 lightTemplate = this . lightTemplate ;
104138
105139 var defaultLightTemplate = lightTemplate . replace ( / { { id} } / g, 0 ) . replace ( "LIGHT0" , "DEFAULT LIGHT" ) ;
106140
107141 // Clear and render
108142 $lightList . html ( '' ) ;
143+ $lightMarkList . html ( '' ) ;
109144 $lightList . append ( defaultLightTemplate ) ;
110145
111- $ ( "#lightPanel0 .destroy" ) . remove ( ) ;
146+ $ ( "#lightPanel0 .destroy" ) . remove ( ) ; //can not be deleted
112147
113- setupLightFunctions ( 0 ) ; //for default light
148+ //for default light
149+ setupLightFunctions ( 0 ) ; //sliderbar checkbox etc, function in sliders.js
150+ drawLightMarkPosition ( 0 ) ;
114151
115152 octopus . getExistLights ( ) . forEach ( function ( light ) {
116153
117154 // Replace template markers with data
118155 var thisTemplate = lightTemplate . replace ( / { { id} } / g, light . id ) ;
119156 $lightList . append ( thisTemplate ) ;
120157 setupLightFunctions ( light . id ) ;
158+ drawLightMarkPosition ( light . id ) ;
121159 } ) ;
122160
123161
@@ -130,6 +168,13 @@ $(function() {
130168 $ ( lightContentName ) . addClass ( 'in' ) ;
131169
132170 }
171+
172+ //for show lights position
173+ //init default point
174+
175+
176+
177+
133178 }
134179 } ;
135180
@@ -141,18 +186,78 @@ $(function() {
141186function addLightParameters ( index ) {
142187 //init parameter
143188 mouseXY [ index ] = [ Math . random ( ) - 0.5 , Math . random ( ) - 0.5 ] ;
144- lightColor [ index ] = [ Math . random ( ) / 2 + 0.5 , Math . random ( ) / 2 + 0.5 , Math . random ( ) / 2 + 0.5 ] ;
189+ lightColor [ index ] = [ Math . random ( ) , Math . random ( ) , Math . random ( ) ] ;
145190 lightIntensity [ index ] = 0.5 ;
146191 pointLightDis [ index ] = 0.5 ;
147192 showDiffuse [ index ] = 1 ;
148193 showSpec [ index ] = 1 ;
149194
150- //mouse stuff
195+ //mouse
151196 var canvas = document . getElementById ( "gl-canvas" ) ;
152197 canvas . addEventListener ( "mousemove" , function ( evt ) {
153198 if ( mouseFlag === 1 ) {
154- setMousePos ( canvas , evt , index ) ; //this function in "cube.js"
199+ setMousePos ( canvas , evt , index ) ;
155200 }
156201 } , false ) ;
157202
158203}
204+
205+
206+ //for show lights position
207+
208+ function setLightMarkFill ( index )
209+ {
210+ var colorString = color2hex ( lightColor [ index ] ) ;
211+ var lightMarkName = '#lightMark' + index ;
212+ $ ( lightMarkName ) . attr ( 'fill' , colorString ) ;
213+ }
214+
215+ function setLightMarkPosition ( index )
216+ {
217+ var lightPx = ( mouseXY [ index ] [ 0 ] + 0.5 ) * 100 + "%" ;
218+ var lightPy = ( mouseXY [ index ] [ 1 ] + 0.5 ) * 100 + "%" ;
219+
220+ var lightMarkName = '#lightMark' + index ;
221+ $ ( lightMarkName ) . attr ( 'cx' , lightPx ) . attr ( 'cy' , lightPy ) ;
222+
223+ console . log ( "inMark" ) ;
224+ }
225+
226+ function drawLightMarkPosition ( index ) {
227+ var lightPx = ( mouseXY [ index ] [ 0 ] + 0.5 ) * 100 + "%" ;
228+ var lightPy = ( mouseXY [ index ] [ 1 ] + 0.5 ) * 100 + "%" ;
229+ var colorString = color2hex ( lightColor [ index ] ) ;
230+ var lightMark = "lightMark" + index ;
231+ var circle = makeSVG ( 'circle' , { id :lightMark , cx : lightPx , cy : lightPy , fill : colorString , r :8 , stroke : 'white' , 'stroke-width' : 1 , } ) ;
232+ document . getElementById ( 'lightPosition_container' ) . appendChild ( circle ) ;
233+ }
234+
235+ function makeSVG ( tag , attrs ) {
236+ var el = document . createElementNS ( 'http://www.w3.org/2000/svg' , tag ) ;
237+ for ( var k in attrs )
238+ el . setAttribute ( k , attrs [ k ] ) ;
239+ return el ;
240+ }
241+
242+ //mouse functions
243+
244+ function setMousePos ( canvas , evt , i ) {
245+
246+ if ( currentLight == i )
247+ {
248+ mouseXY [ i ] [ 0 ] = getMousePos ( canvas , evt ) . x ;
249+ mouseXY [ i ] [ 1 ] = getMousePos ( canvas , evt ) . y ;
250+ setLightMarkPosition ( i ) ;
251+ console . log ( i + ": " + mouseXY [ i ] [ 0 ] + " " + mouseXY [ i ] [ 1 ] ) ;
252+ }
253+
254+ }
255+
256+ function getMousePos ( canvas , evt ) {
257+ var rect = canvas . getBoundingClientRect ( ) ;
258+ return {
259+ x : ( evt . clientX - rect . left ) / ( rect . right - rect . left ) - 0.5 ,
260+ y : ( evt . clientY - rect . top ) / ( rect . bottom - rect . top ) - 0.5
261+ } ;
262+ }
263+
0 commit comments