UnityMol  0.9.6-875
UnityMol viewer / In developement
SphereManager.cs
Go to the documentation of this file.
1 using UnityEngine;
2 using UnityEngine.Rendering;
3 
4 using System.Collections;
5 using System.Collections.Generic;
6 using UI;
7 using Molecule.Model;
8 
11  static public GameObject[] sphereGameObjects;
12 
16  public override void Init () {
17  balls = GameObject.FindObjectsOfType(typeof(BallUpdateSphere)) as BallUpdateSphere[];
18  BallUpdate.resetColors = true;
19  BallUpdate.resetRadii = true;
20  enabled = true;
21  isInit = true;
22  }
23 
24  public override void DestroyAll() {
25  Debug.Log("Destroying Spheres");
26 // Debug.Log(balls.Length.ToString());
27 
28  balls = GameObject.FindObjectsOfType(typeof(BallUpdateSphere)) as BallUpdateSphere[];
29  foreach(BallUpdateSphere sb in balls) {
30  //sb.renderer.enabled = false;
31  //DestroyImmediate(sb);
32  GameObject.Destroy(sb.gameObject);
33  }
34  isInit = false;
35  }
36 
37  public override void ToggleDistanceCueing(bool enabling) {
38 
39  }
40 
41 
42  public override void EnableRenderers() {
44  {
46  }
48  balls = GameObject.FindObjectsOfType(typeof(BallUpdateSphere)) as BallUpdateSphere[];
49  foreach(BallUpdateSphere sp in balls){
50  if(UIData.guided){
51  sp.gameObject.transform.position = new Vector3(MoleculeModel.atomsLocationlist[(int)sp.number][0], MoleculeModel.atomsLocationlist[(int)sp.number][1], MoleculeModel.atomsLocationlist[(int)sp.number][2]);
52  }
53  sp.GetComponent<Renderer>().enabled = true;
54  sp.GetComponent<Collider>().enabled = true;
55  }
56  enabled = true;
57  }
58 
59  public override void DisableRenderers() {
61  {
63  }
64  balls = GameObject.FindObjectsOfType(typeof(BallUpdateSphere)) as BallUpdateSphere[];
65  foreach(BallUpdateSphere sp in balls){
66  sp.GetComponent<Renderer>().enabled = false;
67  if(UIData.atomtype != UIData.AtomType.particleball) // Particles don't have their own collider so we must keep it
68  sp.GetComponent<Collider>().enabled = false; // Disable the collider at the same time to avoid ghost-clicking
69  }
70  enabled = false;
71  }
72 
73  public override void EnableShadows(){
74  foreach(BallUpdateSphere sp in balls){
75  sp.GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.On;
76  sp.GetComponent<Renderer>().receiveShadows = true;
77  }
78  }
79  public override void DisableShadows(){
80  foreach(BallUpdateSphere sp in balls){
81  sp.GetComponent<Renderer>().shadowCastingMode = ShadowCastingMode.Off;
82  sp.GetComponent<Renderer>().receiveShadows = false;
83  }
84  }
85 
86 
90  private void ResetColors() {
91  if(UIData.atomtype == UIData.AtomType.sphere){
92  // balls = GameObject.FindObjectsOfType(typeof(BallUpdateSphere)) as BallUpdateSphere[];
93  foreach(BallUpdateSphere sp in balls) {
94  sp.GetComponent<Renderer>().material.SetColor("_Color", Molecule.Model.MoleculeModel.atomsColorList[(int)sp.number]);
95  // sp.oldatomcolor = sp.atomcolor;
96  }
97  BallUpdate.resetColors = false;
99  }
100  }
101 
117  public override void SetColor(Color col, List<string> atom, string residue = "All", string chain = "All") {
118 // Debug.Log("SetColor spall");
119  if(!atom.Contains ("All")){
120  if(residue == "All"){
121  if(chain == "All"){
122  // ---------- ATOM
123 // Debug.Log("ATOM");
125  foreach(BallUpdateSphere sp in balls){
126  if(atom.Contains(sp.tag))
128  }
129  }else{
130  foreach(BallUpdateSphere sp in balls){
131  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number]))
133  }
134  }
135  }
136  else{
137  // ---------- ATOM + CHAIN
138 // Debug.Log("ATOM+CHAIN");
140  foreach(BallUpdateSphere sp in balls){
141  if(atom.Contains(sp.tag)
142  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
144  }
145  }else{
146  foreach(BallUpdateSphere sp in balls){
147  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
148  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
150  }
151  }
152  }
153  }
154  else{
155  if(chain == "All"){
156  // ---------- ATOM + RESIDUE
157 // Debug.Log("ATOM+RESIDUE");
159  foreach(BallUpdateSphere sp in balls){
160  if(atom.Contains(sp.tag)
161  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue)
163  }
164  }else{
165  foreach(BallUpdateSphere sp in balls){
166  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
167  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue)
169  }
170  }
171  }
172  else{
173  // ---------- ATOM + RESIDUE + CHAIN
174 // Debug.Log("ATOM+RESIDUE+CHAIN");
176  foreach(BallUpdateSphere sp in balls){
177  if(atom.Contains(sp.tag)
178  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
179  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
181  }
182  }else{
183  foreach(BallUpdateSphere sp in balls){
184  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
185  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
186  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
188  }
189  }
190  }
191  }
192  }
193  else{
194  if(residue == "All"){
195  if(chain == "All"){
196  // ---------- EVERYTHING
197 // Debug.Log("EVERYTHING");
198  foreach(BallUpdateSphere sp in balls)
200  }
201  else{
202  // ---------- CHAIN
203 // Debug.Log("CHAIN");
204  foreach(BallUpdateSphere sp in balls){
205  if(Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
207  }
208  }
209  }
210  else{
211  if(chain == "All"){
212  // ---------- RESIDUE
213 // Debug.Log("RESIDUE");
214  foreach(BallUpdateSphere sp in balls){
215  if(Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue){
217  }
218  }
219  }
220  else{
221  // ---------- CHAIN + RESIDUE
222 // Debug.Log("RESIDUE+CHAIN");
223  foreach(BallUpdateSphere sp in balls){
224  if(Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
225  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
227  }
228  }
229  }
230  }
231 
232  BallUpdate.resetColors = true;
233 // BallUpdate.bondsReadyToBeReset = true;
234  }
235 
245  public override void SetColor(Color col, int atomNum) {
246 // Debug.Log("SphereManager object: SetColor(int, Color) : " + atomNum + " - " + col.ToString());
247  foreach(BallUpdateSphere sp in balls){
248  if(sp.GetComponent<BallUpdate>().number == atomNum)
250  }
251  BallUpdate.resetColors = true;
252  }
253 
266  public override void SetRadii(List<string> atom, string residue = "All", string chain = "All") {
267  if(!atom.Contains ("All")){
268  if(residue == "All"){
269  if(chain == "All"){
270  // ---------- ATOM
272  foreach(BallUpdateSphere sp in balls) {
273  if(atom.Contains(sp.tag))
275  }
276  }else{
277  foreach(BallUpdateSphere sp in balls) {
278  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number]))
280  }
281  }
282  }
283  else{
284  // ---------- ATOM + CHAIN
286  foreach(BallUpdateSphere sp in balls) {
287  if(atom.Contains(sp.tag)
288  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
290  }
291  }else{
292  foreach(BallUpdateSphere sp in balls) {
293  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
294  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
296  }
297  }
298  }
299  }
300  else{
301  if(chain == "All"){
302  // ---------- ATOM + RESIDUE
304  foreach(BallUpdateSphere sp in balls) {
305  if(atom.Contains(sp.tag)
306  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue)
308  }
309  }else{
310  foreach(BallUpdateSphere sp in balls) {
311  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
312  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue)
314  }
315  }
316  }
317  else{
318  // ---------- ATOM + CHAIN + RESIDUE
320  foreach(BallUpdateSphere sp in balls) {
321  if(atom.Contains(sp.tag)
322  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
323  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
325  }
326  }else{
327  foreach(BallUpdateSphere sp in balls) {
328  if(atom.Contains(Molecule.Model.MoleculeModel.atomsNamelist[(int)sp.number])
329  && Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
330  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
332  }
333  }
334  }
335  }
336  }
337  else{
338  if(residue == "All"){
339  if(chain == "All"){
340  // ---------- EVERYTHING
341  foreach(BallUpdateSphere sp in balls) {
343  }
344  }
345  else{
346  // ---------- CHAIN
347  foreach(BallUpdateSphere sp in balls) {
348  if(Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
350  }
351  }
352  }
353  else{
354  if(chain == "All"){
355  // ---------- RESIDUE
356  foreach(BallUpdateSphere sp in balls) {
357  if(Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue)
359  }
360  }
361  else{
362  // ---------- CHAIN + RESIDUE
363  foreach(BallUpdateSphere sp in balls) {
364  if(Molecule.Model.MoleculeModel.atomsResnamelist[(int)sp.number] == residue
365  && Molecule.Model.MoleculeModel.atomsChainList[(int)sp.number] == chain)
367  }
368  }
369  }
370  }
371  BallUpdate.resetRadii = true;
373  }
374 
381  public override void SetRadii(int id) {
382  foreach(BallUpdateSphere sp in balls) {
383  if((int)sp.number == id)
385  }
386  BallUpdate.resetRadii = true;
388  }
389 
399  public Color GetColor(Vector3 pos){
400  foreach(BallUpdateSphere sp in balls)
401  if(sp.transform.position == pos)
402  return sp.GetComponent<Renderer>().material.GetColor("_Color");
403  return Color.white;
404  }
405 
415  public override GameObject GetBall(int id){
416  return balls[id].gameObject;
417  }
418 
419  public override void ResetRadii() {
420  if(UIData.atomtype == UIData.AtomType.sphere){
421  foreach(BallUpdateSphere sp in balls) {
422  float buffer = Molecule.Model.MoleculeModel.atomsTypelist[(int)sp.number].radius
425  sp.transform.localScale = new Vector3( buffer, buffer, buffer);
426  sp.oldrayonFactor = sp.rayonFactor;
427  }
429  BallUpdate.resetRadii = false;
430  }
431  }
432 
437  for (int i=0; i<sphereGameObjects.Length; i++) {
438  MouseOverMoleculeIMDSimulation mover = (MouseOverMoleculeIMDSimulation) sphereGameObjects[i].AddComponent<MouseOverMoleculeIMDSimulation>();
439  mover.setAtomId(i);
440  }
441  }
442 
447  for (int i=0; i<sphereGameObjects.Length; i++) {
448  Destroy(sphereGameObjects[i].GetComponent<MouseOverMoleculeIMDSimulation>());
449  }
450  }
451 
452  // Update is called once per frame
453  void Update () {
454  if(UIData.atomtype != UIData.AtomType.sphere) {
455  if(GameObject.FindObjectsOfType(typeof(BallUpdateSphere)).Length >0) // Sometimes they're already destroyed at this point.
457  return;
458  }
459 
461  ResetColors();
463  ResetRadii();
464  }
465 
466  public override void ResetPositions(){
467  for (int j=0; j<balls.Length; j++){
468  int i = (int)balls[j].number;
469  balls[j].transform.localPosition = new Vector3(MoleculeModel.atomsLocationlist[i][0],
472  }
473  }
474 
475  public override void ResetIMDSimulationPositions(){
476  for (int j=0; j<balls.Length; j++){
477  int i = (int)balls[j].number;
478  balls[j].transform.localPosition = MoleculeModel.atomsIMDSimulationLocationlist[i];
479  }
480  }
481 
482  // Returns the closest atom game object in space from a given position during an IMD simulation.
484  BallUpdateSphere closestAtom = balls[0];
485  BallUpdateSphere tempAtom;
486  float minDist = Vector3.Distance(closestAtom.gameObject.transform.position, position);
487  float tempDist;
488 
489  SingleAtomSelection selection = new SingleAtomSelection();
490 
491  for (int i = 1; i < balls.Length; i++) {
492  tempAtom = balls[i];
493  tempDist = Vector3.Distance(tempAtom.gameObject.transform.position, position);
494  if (tempDist < minDist) {
495  minDist = tempDist;
496  closestAtom = tempAtom;
497  }
498  }
499 
500  selection.go = closestAtom.gameObject;
501  selection.pdbIndex = (int) closestAtom.number;
502 
503  return selection;
504  }
505  public override void showHydrogens(bool hide) {
506  for (int j=0; j<balls.Length; j++){
507  if (balls[j].tag=="H")
508  balls[j].GetComponent<Renderer>().enabled=!hide;
509  }
510  }
511 
512 
513 }
void DestroyMouseOversIMDSimulation()
Destroys the mouse overs
static float newScale
Definition: GUIDisplay.cs:144
override void SetColor(Color col, List< string > atom, string residue="All", string chain="All")
Sets the color of atoms.
override void ResetPositions()
override void DisableShadows()
static bool isFileLoaded
Definition: UIData.cs:115
static bool guided
Definition: UIData.cs:201
void CreateMouseOversIMDSimulation()
Creates the mouse overs, needed when an IMD simulation and mouse interaction are required ...
override void DestroyAll()
override void SetRadii(int id)
Changes the scale for Atom selection.
static List< string > atomsNamelist
The name of each atom.
static List< string > atomsChainList
The chain of each atom.
SingleAtomSelection getClosestAtomGameObject(Vector3 position)
static List< AtomModel > atomsTypelist
The type of each atom.
static bool resetColors
Definition: BallUpdate.cs:73
static List< Vector3 > atomsIMDSimulationLocationlist
The coordinates of each atom, simulated through an IMD simulation.
override void SetColor(Color col, int atomNum)
Sets the color of a specific atom.
override void ToggleDistanceCueing(bool enabling)
long number
Definition: BallUpdate.cs:84
static void setCurrentAtomManager(GenericManager manager)
Definition: UnityMolMain.cs:63
BallUpdateSphere[] balls
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
override void ResetIMDSimulationPositions()
void ResetColors()
Resets the colors of all spheres and sticks.
override void EnableShadows()
!WiP manage GUI, and provide static strings for the GUI.
Definition: GUIDisplay.cs:94
override void DisableRenderers()
Disables the renderers for all objects managed by the instance of the manager.
static List< Color > atomsColorList
The color of each atom.
static float radiusFactor
Definition: BallUpdate.cs:81
override void showHydrogens(bool hide)
static float oldRadiusFactor
Definition: BallUpdate.cs:82
static bool bondsReadyToBeReset
Definition: BallUpdate.cs:75
override void Init()
Initializes this sphere manager.
float oldrayonFactor
Definition: BallUpdate.cs:80
static AtomType atomtype
Definition: UIData.cs:139
static List< string > atomsResnamelist
The name of the residue to which each atom belongs.
Color GetColor(Vector3 pos)
Gets the color of the atom at the location "pos".
static bool quickSelection
Definition: GUIDisplay.cs:157
override void SetRadii(List< string > atom, string residue="All", string chain="All")
override void ResetRadii()
override GameObject GetBall(int id)
Gets the GameObject from balls at the position "id".
static GameObject[] sphereGameObjects
override void EnableRenderers()
Enables the renderers for all objects managed by the instance of the manager.
float rayonFactor
Definition: BallUpdate.cs:79
Definition: GUIDisplay.cs:66
static List< float > atomsLocalScaleList
static bool resetRadii
Definition: BallUpdate.cs:76
static List< float[]> atomsLocationlist
The coordinates of each atom.