UnityMol  0.9.6-875
UnityMol viewer / In developement
ColorPicker.cs
Go to the documentation of this file.
1 
66 using UnityEngine;
67 using System;
68 using System.Collections;
69 using System.Collections.Generic;
70 using Molecule.Model;
71 
72 
73 
74 public class ColorPicker{
75 
76  private Texture2D m_aTexture;
77  private Rect m_text_area = new Rect(5, 45, 228, 228);
78  private Rect m_close_area = new Rect(5, 20, 50, 20);
79  private Rect m_r_area = new Rect(60, 20, 50, 20);
80  private Rect m_g_area = new Rect(115, 20, 50, 20);
81  private Rect m_b_area = new Rect(170, 20, 50, 20);
82  private int m_r_value = 255;
83  private int m_g_value = 255;
84  private int m_b_value = 255;
85  private Rect m_activeArea;
86  private Rect m_rect;
87  private string m_title;
88  private bool m_enabled;
89  private int callId;
90  public bool changed = false;
91 
92  private List<string> m_atoms = new List<string>();
93  private string m_residue = "All";
94  private string m_chain = "All";
95  private Camera mainCamera = null;
96 
97  public bool enabled
98  {
99  get
100  {
101  return m_enabled;
102  }
103  set
104  {
105  m_enabled = value;
106  }
107  }
108  //We use a ColorObject to reference a variable from outside
109  //Impossible to do with UnityEngine.Color as it is a Struct
111  public ColorObject color
112  {
113  get
114  {
115  return m_colorObj;
116  }
117  set
118  {
119  m_colorObj = value;
120  }
121  }
122 
123  public ColorPicker(int xstart,int ystart,int width, int height,
124  ColorObject colorObj, List<string> atoms, string residue = "All", string chain = "All",
125  string title="Color Picker", int caller=-1)
126  {
127  m_colorObj = colorObj;
128  m_atoms = atoms;
129  m_residue = residue;
130  m_chain = chain;
131  m_r_value = (int)(colorObj.color.r * 255f);
132  m_g_value = (int)(colorObj.color.g * 255f);
133  m_b_value = (int)(colorObj.color.b * 255f);
134  m_rect = new Rect(xstart,ystart,width,height);
135  m_aTexture = (Texture2D)Resources.Load("ImprovedColorPicker");
136  m_title=title;
137  this.enabled = true;
138  m_activeArea = new Rect(m_rect.x + m_text_area.x, m_rect.y + m_text_area.y, m_text_area.width, m_text_area.height);
139  callId = caller;
140  }
141 
142  public ColorPicker(Rect r,
143  ColorObject colorObj, List<string> atoms, string residue = "All", string chain = "All",
144  string title="Color Picker", int caller=-1)
145  {
146  m_colorObj = colorObj;
147  m_atoms = atoms;
148  m_residue = residue;
149  m_chain = chain;
150  m_r_value = (int)(colorObj.color.r * 255f);
151  m_g_value = (int)(colorObj.color.g * 255f);
152  m_b_value = (int)(colorObj.color.b * 255f);
153  m_rect = r;
154  m_aTexture = (Texture2D)Resources.Load("ImprovedColorPicker");
155  m_title=title;
156  this.enabled = true;
157  m_activeArea = new Rect(m_rect.x + m_text_area.x, m_rect.y + m_text_area.y, m_text_area.width, m_text_area.height);
158  callId = caller;
159  }
160 
161  public void SwitchEnable(int caller)
162  {
163  if(caller == callId)
164  enabled = !enabled;
165  }
166 
167  public void OnGUI()
168  {
169  if(enabled)
170  {
171 
172  if(mainCamera == null)
173  mainCamera = Camera.main;
174  GUI.Window(60, m_rect, loadColor, m_title);
175 
176  Vector3 mousePos = Input.mousePosition;
177  mousePos.y = Screen.height - mousePos.y;
178  if(m_activeArea.Contains(mousePos) && Input.GetMouseButton(0) && GUIUtility.hotControl == 0)
179  {
180  int X = (int)((mousePos.x - m_activeArea.x) / m_activeArea.width * m_aTexture.width);
181  int Y = (int)((mousePos.y - m_activeArea.y) / m_activeArea.height * m_aTexture.height);
182 // Debug.Log(X + " " + Y + " " + mousePos.x + " " + mousePos.y);
183  m_r_value = (int)(m_aTexture.GetPixel(X,-Y).r * 255);
184  m_g_value = (int)(m_aTexture.GetPixel(X,-Y).g * 255);
185  m_b_value = (int)(m_aTexture.GetPixel(X,-Y).b * 255);
186 // Debug.Log("Color: "+m_color);
187  changed = true;
188  }
189  else
190  changed = false;
191  if(m_r_value < 0)
192  m_r_value = 0;
193  if(m_r_value > 255)
194  m_r_value = 255;
195  if(m_g_value < 0)
196  m_g_value = 0;
197  if(m_g_value > 255)
198  m_g_value = 255;
199  if(m_b_value < 0)
200  m_b_value = 0;
201  if(m_b_value > 255)
202  m_b_value = 255;
203  m_colorObj.color = new Color((float)m_r_value/255f, (float)m_g_value/255f, (float)m_b_value/255f, 1f);
204 
205  if(m_atoms != null){
208  manager.SetColor(m_colorObj.color, m_atoms, m_residue, m_chain);
209  }
210  else{ //Atom selection
211 
212  //TODO : Do something smarter like a list of atom numbers
213  if(UI.UIData.atomtype == UI.UIData.AtomType.optihb){
214  foreach(GameObject obj in mainCamera.GetComponent<ClickAtom>().objList){
215  string toparse = obj.name.Split(new string[] {"Collider_Atom_"}, StringSplitOptions.None)[1];
216  int atomnumber = int.Parse(toparse);
217 
218  manager.SetColor(m_colorObj.color, atomnumber);
219 
220  }
221  }
222  else{
223  foreach(GameObject obj in mainCamera.GetComponent<ClickAtom>().objList)
224  manager.SetColor(m_colorObj.color, (int)obj.GetComponent<BallUpdate>().number);
225  }
226 
227  }
228  }
229 
230  }
231  }
232 
233  public void loadColor(int a){
234 
235  if(GUI.Button(m_close_area, "Close"))
237  string temp;
238  temp = GUI.TextField(m_r_area, m_r_value.ToString());
239  int.TryParse(temp, out m_r_value);
240  temp = GUI.TextField(m_g_area, m_g_value.ToString());
241  int.TryParse(temp, out m_g_value);
242  temp = GUI.TextField(m_b_area, m_b_value.ToString());
243  int.TryParse(temp, out m_b_value);
244  GUI.DrawTexture(m_text_area, m_aTexture, ScaleMode.ScaleToFit, true, 0F);
245  if (Event.current.type == EventType.Repaint)
246  MoleculeModel.newtooltip = GUI.tooltip;
247  GUI.DragWindow();
248 
249  }
250 
251 }
Rect m_b_area
Definition: ColorPicker.cs:81
bool enabled
Definition: ColorPicker.cs:98
Rect m_close_area
Definition: ColorPicker.cs:78
Rect m_text_area
Definition: ColorPicker.cs:77
void loadColor(int a)
Definition: ColorPicker.cs:233
ColorPicker(Rect r, ColorObject colorObj, List< string > atoms, string residue="All", string chain="All", string title="Color Picker", int caller=-1)
Definition: ColorPicker.cs:142
ColorObject color
Definition: ColorPicker.cs:112
abstract void SetColor(Color col, List< string > atoms, string residue="All", string chain="All")
bool changed
Definition: ColorPicker.cs:90
bool m_enabled
Definition: ColorPicker.cs:88
string m_title
Definition: ColorPicker.cs:87
static ColorPicker m_colorPicker
Rect m_activeArea
Definition: ColorPicker.cs:85
void OnGUI()
Definition: ColorPicker.cs:167
Color color
Definition: ColorObject.cs:72
Texture2D m_aTexture
Definition: ColorPicker.cs:76
string m_chain
Definition: ColorPicker.cs:94
Rect m_g_area
Definition: ColorPicker.cs:80
long number
Definition: BallUpdate.cs:84
ColorPicker(int xstart, int ystart, int width, int height, ColorObject colorObj, List< string > atoms, string residue="All", string chain="All", string title="Color Picker", int caller=-1)
Definition: ColorPicker.cs:123
ColorObject m_colorObj
Definition: ColorPicker.cs:110
!WiP Includes FLAGS of GUI.
Definition: UIData.cs:78
List< string > m_atoms
Definition: ColorPicker.cs:92
Camera mainCamera
Definition: ColorPicker.cs:95
string m_residue
Definition: ColorPicker.cs:93
static AtomType atomtype
Definition: UIData.cs:139
Rect m_r_area
Definition: ColorPicker.cs:79
void SwitchEnable(int caller)
Definition: ColorPicker.cs:161
List< GameObject > objList
Definition: ClickAtom.cs:76
Definition: GUIDisplay.cs:66
static GenericManager getCurrentAtomManager()
Definition: UnityMolMain.cs:50