Skip to content

Commit

Permalink
Merge pull request #61 from AliMatias/combine-molecule
Browse files Browse the repository at this point in the history
Combine molecule
  • Loading branch information
ldgobbi authored Sep 6, 2019
2 parents 7897375 + 0a3252b commit 8bdfcb3
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 24 deletions.
20 changes: 10 additions & 10 deletions DB Scripts/INSERT_materiales_mapping_element.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
1,NULL,1,1);
1,0,1,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
2,NULL,31,1);
2,0,31,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
3,NULL,22,1);
3,0,22,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
4,NULL,44,1);
4,0,44,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
5,NULL,27,1);
5,0,27,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
6,NULL,30,1);
6,0,30,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
7,NULL,2,1);
7,0,2,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
8,6,NULL,1);
8,6,0,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
9,6,NULL,1);
9,6,0,2);
INSERT INTO materiales_mapping_element (id_material,id_elemento,id_molecula,cantidad) VALUES (
10,NULL,7,1);
10,0,7,2);
Binary file modified New Unity Project/Assets/SIAMM.db
Binary file not shown.
22 changes: 11 additions & 11 deletions New Unity Project/Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 170076734}
m_IndirectSpecularColor: {r: 0.44657868, g: 0.49641263, b: 0.57481706, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -11231,7 +11231,7 @@ MonoBehaviour:
m_HandleRect: {fileID: 1807429813}
m_Direction: 0
m_Value: 0
m_Size: 0.99999833
m_Size: 0.9999994
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -19993,7 +19993,7 @@ RectTransform:
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
m_AnchorMin: {x: 0.5, y: 1}
m_AnchorMax: {x: 0.5, y: 1}
m_AnchoredPosition: {x: -99.00005, y: 0.00002335011}
m_AnchoredPosition: {x: -98.99998, y: -0.000026358184}
m_SizeDelta: {x: 10, y: 0}
m_Pivot: {x: 0, y: 1}
--- !u!114 &896579102
Expand Down Expand Up @@ -30358,8 +30358,8 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 1488716185}
m_HandleRect: {fileID: 1488716184}
m_Direction: 2
m_Value: 0
m_Size: 1
m_Value: 1
m_Size: 0.9999999
m_NumberOfSteps: 0
m_OnValueChanged:
m_PersistentCalls:
Expand Down Expand Up @@ -44376,7 +44376,7 @@ MonoBehaviour:
m_PersistentCalls:
m_Calls:
- m_Target: {fileID: 57740621}
m_MethodName: CombineAtoms
m_MethodName: Combine
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
Expand Down Expand Up @@ -47065,6 +47065,11 @@ PrefabInstance:
propertyPath: m_Name
value: ToolTip
objectReference: {fileID: 0}
- target: {fileID: 4055092199926187753, guid: c156571454850594ebe75756c089424f,
type: 3}
propertyPath: m_Alpha
value: 0
objectReference: {fileID: 0}
- target: {fileID: 4055092199926187755, guid: c156571454850594ebe75756c089424f,
type: 3}
propertyPath: m_LocalPosition.x
Expand Down Expand Up @@ -47180,11 +47185,6 @@ PrefabInstance:
propertyPath: background
value:
objectReference: {fileID: 557895596}
- target: {fileID: 4055092199926187753, guid: c156571454850594ebe75756c089424f,
type: 3}
propertyPath: m_Alpha
value: 0
objectReference: {fileID: 0}
m_RemovedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: c156571454850594ebe75756c089424f, type: 3}
--- !u!1001 &5417521375962967491
Expand Down
73 changes: 71 additions & 2 deletions New Unity Project/Assets/Scripts/Managers/CombinationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class CombinationManager : MonoBehaviour
private bool combineMode = false;
private AtomManager atomManager;
private QryMoleculas qryMolecule;
private QryMaterials qryMaterial;
private MoleculeManager moleculeManager;
private SelectionManager selectionManager;
public Button combineButton;
Expand All @@ -37,7 +38,9 @@ void Awake()
//instancio en el momento la clase que contiene las querys, seria lo mismo que hacer class algo = new class();
GameObject go = new GameObject();
go.AddComponent<QryMoleculas>();
go.AddComponent<QryMaterials>();
qryMolecule = go.GetComponent<QryMoleculas>();
qryMaterial = go.GetComponent<QryMaterials>();

moleculeManager = FindObjectOfType<MoleculeManager>();
selectionManager = FindObjectOfType<SelectionManager>();
Expand Down Expand Up @@ -73,10 +76,35 @@ public void SwitchCombineMode()
}
}

public void Combine(){
List<int> selectedAtoms = atomManager.GetSelectedAtoms();
List<int> selectedMolecules = moleculeManager.GetSelectedMolecules();
int atomCount = selectedAtoms.Count;
int molCount = selectedMolecules.Count;
if(atomCount > 0 && molCount > 0){
Debug.LogError("CombinationManager :: Se intentó combinar átomos y moléculas.");
popup.MostrarPopUp("Manager Combinación",
"Solo se pueden combinar átomos con átomos, y moléculas con moléculas.");
}else{
if(atomCount>1){
Debug.Log("Combinando átomos.");
CombineAtoms(selectedAtoms);
}else{
if(molCount>1){
Debug.Log("Combinando moléculas.");
CombineMolecules(selectedMolecules);
}else{
Debug.LogError("CombinationManager :: Intento de combinar sin seleccionar nada");
popup.MostrarPopUp("Manager Combinación",
"Debe seleccionar 2 o más átomos o moléculas para poder combinar.");
}
}
}
}

//Acá tiene que ir a la bd a buscar la combinación
public void CombineAtoms()
private void CombineAtoms(List<int> selectedAtoms)
{
List<int> selectedAtoms = atomManager.GetSelectedAtoms();
List<int> elementNumbers = new List<int>();
foreach (int index in selectedAtoms)
{
Expand Down Expand Up @@ -190,6 +218,47 @@ group element by element into elementOccurrences
}
}

private void CombineMolecules(List<int> selectedMolecules){
List<int> moleculeIds = new List<int>();
foreach (int index in selectedMolecules)
{
int molId = moleculeManager.FindMoleculeInList(index).MoleculeId;
if(molId != null && molId != 0)
{
moleculeIds.Add(molId);
}
}
int previous = moleculeIds[0];
//valido que todas las moléculas sean iguales
foreach (int mol in moleculeIds){
if(previous != mol){
Debug.Log("No se pueden combinar moléculas distintas.");
popup.MostrarPopUp("Manager Combinación",
"El programa no soporta combinar distintas moléculas aún.");
return;
}
}
try{
//acá agarro el [0] porque son todos iguales los índices (lo chequee arriba)
MaterialMappingData mapping = qryMaterial.GetMaterialByMoleculeId(moleculeIds[0]);
MoleculeData molecule = qryMolecule.GetMoleculeById(mapping.IdMolecule);
MaterialData material = qryMaterial.GetMaterialById(mapping.IdMaterial);
if(moleculeIds.Count >= mapping.Amount){
popup.MostrarPopUp("Combinación","Creaste " + material.Name);
}else{
Debug.Log("Cantidad insuficiente");
popup.MostrarPopUp("Cantidad insuficiente","Se necesitan al menos " + mapping.Amount
+ " moléculas de " + molecule.TraditionalNomenclature +
" para formar " + material.Name);
}

}catch(Exception e){
Debug.LogError("Hubo un error tratando de obtener el material de la base de datos");
popup.MostrarPopUp("Manager Combinación",
"Hubo un error tratando de obtener el material de la base de datos");
}
}


static List<int> Intersect(List<List<int>> lists)
{
Expand Down
5 changes: 4 additions & 1 deletion New Unity Project/Assets/Scripts/Managers/MoleculeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ public void SpawnMolecule(List<AtomInMolPositionData> atomsPosition, string name
//seteo nombre
newMolecule.SetMoleculeName(name);
List<AtomInMolPositionData> normalizedAtoms = NormalizeAtomPositions(atomsPosition);
//seteo su ID de molécula
newMolecule.MoleculeId = normalizedAtoms[0].MoleculeId;
//spawneo todos sus átomos
foreach (AtomInMolPositionData pos in normalizedAtoms)
{
Expand All @@ -135,6 +137,7 @@ public void SpawnMolecule(List<AtomInMolPositionData> atomsPosition, string name
Material mat = materials[GetMaterialIndexFromDictionary(element.ClasificacionGrupo)];
newMolecule.SpawnAtom(pos, mat);
}

//y despues sus conexiones una vez que esten todos posicionados
foreach (AtomInMolPositionData atom in normalizedAtoms)
{
Expand Down Expand Up @@ -182,7 +185,7 @@ private int GetMaterialIndexFromDictionary(string cat)
return categories[cat];
}

private Molecule FindMoleculeInList(int index)
public Molecule FindMoleculeInList(int index)
{
foreach(Molecule molecule in molecules)
{
Expand Down
14 changes: 14 additions & 0 deletions New Unity Project/Assets/Scripts/Model/MaterialData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class MaterialData{

private int id;
private string name;

public int Id { get => id; set => id = value; }
public string Name { get => name; set => name = value; }

public MaterialData(int id, string name)
{
this.id = id;
this.name = name;
}
}
11 changes: 11 additions & 0 deletions New Unity Project/Assets/Scripts/Model/MaterialData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions New Unity Project/Assets/Scripts/Model/MaterialMappingData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class MaterialMappingData{

private int idMaterial;
private int idElement;
private int idMolecule;
private int amount;

public int IdMaterial { get => idMaterial; set => idMaterial = value; }
public int IdElement { get => idElement; set => idElement = value; }
public int IdMolecule { get => idMolecule; set => idMolecule = value; }
public int Amount { get => amount; set => amount = value; }

public MaterialMappingData(int idMaterial, int idElement, int idMolecule, int amount)
{
this.idMaterial = idMaterial;
this.idElement = idElement;
this.idMolecule = idMolecule;
this.amount = amount;
}
}
11 changes: 11 additions & 0 deletions New Unity Project/Assets/Scripts/Model/MaterialMappingData.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions New Unity Project/Assets/Scripts/Molecule/Molecule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ public class Molecule : MonoBehaviour
public GameObject moleculeLabel;
//indice que indica posición+
private int moleculeIndex;
//indica id en tabla de moléculas
private int moleculeId;

public int MoleculeId { get => moleculeId; set => moleculeId = value; }
public int MoleculeIndex { get => moleculeIndex; set => moleculeIndex = value; }

private void Awake()
Expand Down
79 changes: 79 additions & 0 deletions New Unity Project/Assets/Scripts/Querys/QryMaterials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using Mono.Data.Sqlite;
using System;
using System.Collections.Generic;
using UnityEngine;

public class QryMaterials : MonoBehaviour
{
private DBManager dBManager = null;

private void Awake()
{
dBManager = FindObjectOfType<DBManager>();
}

#region Metodos Exec Querys & Management

public MaterialMappingData GetMaterialByMoleculeId(int moleculeId){
MaterialMappingData result = null;
//dejo un reader local para cada query, no siendo global
SqliteDataReader reader = null;
SqliteConnection dbConnection = null;

try{
string sqlQuery = "SELECT * FROM materiales_mapping_element WHERE id_molecula=" + moleculeId;
//LLAMADA AL METODO DE LA DBMANAGER
dbConnection = dBManager.openCon();
reader = dBManager.ManageExec(dbConnection, sqlQuery);
while (reader.Read())
{
int idMat = reader.GetInt32(0);
int idElem = reader.GetInt32(1);
int idMol = reader.GetInt32(2);
int amount = reader.GetInt32(3);

result = new MaterialMappingData(idMat, idElem, idMol, amount);
}
}
catch (Exception e)
{
throw e;
}
finally
{
dBManager.ManageClosing(dbConnection, reader);
}
return result;
}

public MaterialData GetMaterialById(int materialId){
MaterialData data = null;
//dejo un reader local para cada query, no siendo global
SqliteDataReader reader = null;
SqliteConnection dbConnection = null;
try{
string sqlQuery = "SELECT * FROM materiales_lista WHERE id=" + materialId;
//LLAMADA AL METODO DE LA DBMANAGER
dbConnection = dBManager.openCon();
reader = dBManager.ManageExec(dbConnection, sqlQuery);
while (reader.Read())
{
int idMat = reader.GetInt32(0);
string name = reader.GetString(1);

data = new MaterialData(idMat, name);
}
}
catch (Exception e)
{
throw e;
}
finally
{
dBManager.ManageClosing(dbConnection, reader);
}
return data;
}

#endregion
}
11 changes: 11 additions & 0 deletions New Unity Project/Assets/Scripts/Querys/QryMaterials.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8bdfcb3

Please sign in to comment.