Skip to content

Commit

Permalink
More UI updates, added exit option to alignment visualization. Added …
Browse files Browse the repository at this point in the history
…a messagebox feature to the engine.
  • Loading branch information
dolavn committed Mar 8, 2018
1 parent 5530ecb commit 07d101b
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 17 deletions.
2 changes: 1 addition & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using namespace glm;
using namespace std;

Button::Button(vec3 color, float x, float y, float width, float height, string str, Mesh* mesh, Shader& shader, Shader& textShader, Engine& engine,function<void(Engine& engine)> action):DrawableObject(shader,color),textShader(textShader),engine(engine),mesh(mesh),text(nullptr),action(action) {
Button::Button(vec3 color, float x, float y, float width, float height, string str, Mesh* mesh, Engine& engine,function<void(Engine& engine)> action):DrawableObject(engine.getShader(),color),textShader(engine.getTextShader()),engine(engine),mesh(mesh),text(nullptr),action(action) {
setupText(str, x, y, width, height);
setLocation(vec3(x, y, 6.1));
setScale(vec3(width, height, 1));
Expand Down
2 changes: 1 addition & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/Button.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class Button :public DrawableObject {
public:
Button(glm::vec3 color, float x,float y, float width, float height,std::string str,Mesh* mesh, Shader& shader,Shader& textShader, Engine& engine,std::function<void(Engine& engine)> action);
Button(glm::vec3 color, float x,float y, float width, float height,std::string str,Mesh* mesh,Engine& engine,std::function<void(Engine& engine)> action);
Button(const Button& other);
virtual ~Button();
void onClick();
Expand Down
20 changes: 19 additions & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/Engine.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <Windows.h>
#include "Engine.h"
#include "UserInterface.h"
#include "MessageBox.h"

using namespace glm;
using namespace std;

Expand Down Expand Up @@ -38,11 +40,17 @@ void Engine::run() {
glReadPixels(mouse.xPos, mouse.yPos, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, data);
int ind = getId(vec3(data[0], data[1], data[2]));
DrawableObject* selObj = currScene->getSelectedObj();
if (selObj != nullptr) { selObj->onRelease(); }
if (ind >= 0) {
if (selObj != nullptr && selObj->getId()!=ind) {
printf("ind:%d\nselObj:%d\n", ind, selObj->getId());
selObj->onRelease();
}
currScene->getObject(ind).onClick();
}
else {
if (selObj != nullptr) {
selObj->onRelease();
}
currScene->onClickBackground();
}
}
Expand All @@ -55,6 +63,15 @@ void Engine::run() {
}
}

void Engine::showAlert(string title, string text) {
if (currScene == nullptr) { return; }
UI::Messagebox m(vec3(0.8f, 0.2f, 0.2f), vec3(0.0f, 0.0f, 0.0f), 6.0f, 8.0f, 1.0f, 1.0f, title, text, *this);
m.setOnDismiss([](Engine& e) {e.cont(); });
int ind = currScene->addObject(&m);
currScene->setSelectedObj(&(currScene->getObject(ind)));
halt();
}

void Engine::clearScene() {
if (currScene != nullptr) {
delete(currScene);
Expand All @@ -81,6 +98,7 @@ void Engine::clearAdditionalFunctions() {
}

void Engine::callAdditionalFunctions() {
if (!runAdditionalFunctions) { return; }
for (unsigned int i = 0; i < additionalFunctions.size(); ++i) {
bool del = !additionalFunctions[i]();
if (del) {
Expand Down
4 changes: 4 additions & 0 deletions SequenceAlignmentVis/SequenceAlignmentVis/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class Engine {
virtual ~Engine();
void run();
void changeScene(Scene* newScene);
void showAlert(std::string title, std::string text);

inline void halt() { runAdditionalFunctions = false; }
inline void cont() { runAdditionalFunctions = true; }
inline void addAdditionalFunction(std::function<bool()> func) { additionalFunctions.push_back(func); }
inline Display& getDisplay() { return d; }
inline Shader& getShader() { return *shader; }
Expand All @@ -25,6 +28,7 @@ class Engine {
mutable std::mutex mtx;
std::thread::id drawingThread;
std::vector<std::function<bool()>> additionalFunctions;
bool runAdditionalFunctions = true;
bool isRunning = false;
Scene* currScene;
Shader* shader;
Expand Down
135 changes: 135 additions & 0 deletions SequenceAlignmentVis/SequenceAlignmentVis/MessageBox.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include "Messagebox.h"
#include "Engine.h"
#include <iostream>

const float Z_COORDINATE = 4.0f;
const float TITLE_SIZE_FACTOR = 1.6f;
const float TEXT_SIZE_FACTOR = 0.8f;
const int LETTERS_PER_LINE = 10;
const float Z_OFFSET = -0.1f;

using namespace glm;
using namespace std;

namespace UI {
Messagebox::Messagebox(vec3 color, vec3 textColor, float width, float height, float x, float y, string title, string text, Engine& e) : DrawableObject(e.getShader(), color), textShader(e.getTextShader()), textColor(textColor), width(width), height(height), x(x), y(y),e(e){
setupText(title, text);
createMesh();
}

Messagebox::Messagebox(const Messagebox& other) : DrawableObject(other), textShader(other.textShader), textColor(other.textColor), width(other.width), height(other.height), x(other.x), y(other.y),onDismiss(other.onDismiss),e(other.e){
copyText(other);
createMesh();
}

Messagebox::~Messagebox() {
clear();
}

void Messagebox::draw(Shader* shader, mat4 VP) {
if (titleInd == -1) {
titleInd = scn->addObject(title);
}
for (unsigned int i = 0; i < text.size(); ++i) {
if (textInd[i] == -1) {
textInd[i] = scn->addObject(text[i]);
}
}
Shader& s = shader != nullptr ? *shader : defaultShader;
glm::mat4 MVP = VP*modelMatrix;
glm::vec3 id = getIdVec();
s.Bind();
s.Update(MVP, modelMatrix);
s.setColor(color, id);
if (mesh != nullptr) {
mesh->Draw();
}

}

DrawableObject* Messagebox::clone() {
return new Messagebox(*this);
}

void Messagebox::onClick() {
scn->setSelectedObj(this);
}

void Messagebox::onRelease() {
onDismiss(e);
scn->removeDrawable(getId());
}

void Messagebox::onKeyClick(int key) {

}

void Messagebox::setupText(string title, string text) {
float titleSize = TITLE_SIZE_FACTOR*(width / title.size());
float textSize = 2*(width / LETTERS_PER_LINE);
vec3 titleLocation(x+width/2-titleSize, y + height / 2- titleSize /2, Z_COORDINATE);
this->title = new Text(titleLocation, textColor, titleSize, title, textShader);
this->title->setZOffset(Z_OFFSET);
titleInd = -1;
float currY = y + height / 2 - 2 * titleSize;
while (text.size() > 0) {
string prefix = text.substr(0, LETTERS_PER_LINE);
vec3 textLocation(x + width / 2 - textSize, currY, Z_COORDINATE);
this->text.push_back(new Text(textLocation, textColor, textSize, prefix, textShader));
this->text[this->text.size() - 1]->setZOffset(Z_OFFSET);
this->textInd.push_back(-1);
if (LETTERS_PER_LINE >= text.size()) {
text = "";
}else {
text = text.substr(LETTERS_PER_LINE, text.size());
}
currY = currY - textSize;
}
}

void Messagebox::createMesh() {
Vertex vertices[] =
{
Vertex(glm::vec3(x + width / 2.0f, y - height / 2, Z_COORDINATE), vec2(0,0), glm::vec3(0, 0, -1),color),
Vertex(glm::vec3(x - width / 2.0f, y - height / 2, Z_COORDINATE), vec2(1,0), glm::vec3(0, 0, -1),color),
Vertex(glm::vec3(x - width / 2.0f, y + height / 2, Z_COORDINATE), vec2(1,1), glm::vec3(0, 0, -1),color),
Vertex(glm::vec3(x + width / 2.0f, y + height / 2, Z_COORDINATE), vec2(0,1), glm::vec3(0, 0, -1),color),
};

unsigned int indices[] = { 3, 0, 1,
3, 1, 2
};
mesh = new Mesh(vertices, sizeof(vertices) / sizeof(vertices[0]), indices, sizeof(indices) / sizeof(indices[0]));
}

void Messagebox::copyText(const Messagebox& other) {
this->title = static_cast<Text*>(other.title->clone());
titleInd = -1;
for (auto it = other.text.begin(); it != other.text.end(); ++it) {
text.push_back(static_cast<Text*>((*it)->clone()));
textInd.push_back(-1);
}
}

void Messagebox::clear() {
if (mesh != nullptr) {
delete(mesh);
mesh = nullptr;
}
if (title != nullptr) {
delete(title);
if (titleInd != -1) {
scn->removeDrawable(titleInd);
}
}
for (unsigned int i = 0; i < text.size(); ++i) {
Text* curr = text[i];
if (curr != nullptr) {
delete(curr);
if (textInd[i] != -1) {
scn->removeDrawable(textInd[i]);
}
}
}
}
}
42 changes: 42 additions & 0 deletions SequenceAlignmentVis/SequenceAlignmentVis/MessageBox.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef MESSAGE_BOX_H_
#define MESSAGE_BOX_H_

#include "Object.h"
#include <functional>

class Text;
class Engine;

namespace UI {
class Messagebox :public DrawableObject {
public:
Messagebox(glm::vec3 color, glm::vec3 textColor, float width, float height, float x, float y, std::string title, std::string text, Engine& e);
Messagebox(const Messagebox& other);
virtual ~Messagebox();

void draw(Shader* shader, glm::mat4 vp);
DrawableObject* clone();
void onClick();
void onRelease();
void onKeyClick(int key);

void setOnDismiss(std::function<void(Engine&)> callback) { onDismiss = callback; }
private:
void setupText(std::string title, std::string text);
void createMesh();
void copyText(const Messagebox& other);
void clear();
float width, height, x, y;
glm::vec3 textColor;
Engine& e;
Shader& textShader;
Mesh* mesh;
Text* title;
int titleInd;
std::vector<Text*> text;
std::vector<int> textInd;
std::function<void(Engine&)> onDismiss = [](Engine&e) {};
};
}

#endif
3 changes: 2 additions & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/Object.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#include "Object.h"
#include "Scene.h"

DrawableObject::DrawableObject(Shader& defaultShader, glm::vec3 color) :id(-1), defaultShader(defaultShader), color(color){

}

glm::vec3 DrawableObject::getIdVec() {
int tempId = getId();
int tempId = getId()+1;
int ri = tempId % 256;
tempId = tempId / 256;
int gi = tempId % 256;
Expand Down
6 changes: 5 additions & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ class DrawableObject {
DrawableObject(Shader& defaultShader, glm::vec3 color);
DrawableObject(const DrawableObject& other) :defaultShader(other.defaultShader), color(other.color), location(other.location), rotate(other.rotate),scale(other.scale), modelMatrix(other.modelMatrix) { }

virtual ~DrawableObject(){}

virtual void draw(Shader* shader, glm::mat4 vp) = 0;
virtual DrawableObject* clone() = 0;
virtual void onClick() = 0;
virtual void onRelease() = 0;
virtual void onKeyClick(int key) = 0;



inline void setId(int id) { this->id = id; }
inline void setLocation(glm::vec3 location) { this->location = location; updateModelMatrix(); }
inline void setRotate(glm::vec3 axis, float deg) { this->rotate = glm::rotate(rotate, deg, axis); updateModelMatrix(); }
Expand Down Expand Up @@ -50,7 +54,7 @@ class Object3D:public DrawableObject {
public:
Object3D(glm::vec3 color,Mesh* mesh,Shader& shader);
Object3D(const Object3D& other);
~Object3D();
virtual ~Object3D();

inline void setTexture(Texture* tex) { this->tex = tex; }
inline void resetTexture() { this->tex = nullptr; }
Expand Down
2 changes: 1 addition & 1 deletion SequenceAlignmentVis/SequenceAlignmentVis/RadioButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class RadioButton :public DrawableObject {
public:
RadioButton(float x, float y, std::string str, Shader& shader, T value);
RadioButton(const RadioButton& other);
~RadioButton();
virtual ~RadioButton();
void draw(Shader* shader, glm::mat4 vp);
DrawableObject* clone();
void onClick();
Expand Down
11 changes: 3 additions & 8 deletions SequenceAlignmentVis/SequenceAlignmentVis/Scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ void Scene::onClickBackground() {
}

int Scene::addObject(DrawableObject* object) {
bool shouldLock = lockingThread != std::this_thread::get_id();
if (shouldLock) { mtx.lock(); }
DrawableObject* objectToAdd = object->clone();
objectToAdd->setScene(this);
bool text = &object->getDefaultShader() == textShader;
Expand All @@ -149,9 +147,8 @@ int Scene::addObject(DrawableObject* object) {
else {
list.push_back(objectToAdd);
}
objectToAdd->setId(!text ? (ind+1) : -1);
objectToAdd->setId(!text ? (ind) : -1);
int ans = text ? -1 - ind : ind;
if (shouldLock) { mtx.unlock(); }
return ans;
}

Expand All @@ -167,18 +164,16 @@ void Scene::clearFontTexture() {
}

void Scene::removeDrawable(int ind) {
bool shouldLock = lockingThread != std::this_thread::get_id();
if (shouldLock) { mtx.lock(); }
vector<DrawableObject*>& list = ind >= 0 ? objects : textObjects;
int corrInd = ind >= 0 ? ind : -ind - 1;
vector<int>& freeIndices = ind >= 0 ? freeIndicesObjects : freeIndicesTextObjects;
if (corrInd >= (int)list.size()) { return; }
if (list[corrInd] != nullptr) {
printf("deleting\n");
delete(list[corrInd]);
list[corrInd] = nullptr;
freeIndices.push_back(corrInd);
}
if (shouldLock) { mtx.unlock(); }
}

DrawableObject& Scene::getObject(int ind) {
Expand Down Expand Up @@ -346,6 +341,6 @@ void Menu::addButton(float x, float y, float width, float height, string text, f
}

void Menu::addButton(vec3 color,float x, float y, float width, float height, string text, function<void(Engine& engine)> action) {
Button button(color, x, y, width, height, text,cubeMesh,engine.getShader(), engine.getTextShader(), engine, action);
Button button(color, x, y, width, height, text,cubeMesh,engine, action);
addObject(&button);
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<ClInclude Include="Aligner.h" />
<ClInclude Include="Button.h" />
<ClInclude Include="Engine.h" />
<ClInclude Include="MessageBox.h" />
<ClInclude Include="Object.h" />
<ClInclude Include="Display.h" />
<ClInclude Include="DPTable.h" />
Expand All @@ -143,6 +144,7 @@
<ClCompile Include="Aligner.cpp" />
<ClCompile Include="Button.cpp" />
<ClCompile Include="Engine.cpp" />
<ClCompile Include="MessageBox.cpp" />
<ClCompile Include="Object.cpp" />
<ClCompile Include="Display.cpp" />
<ClCompile Include="DPTable.cpp" />
Expand Down
Loading

0 comments on commit 07d101b

Please sign in to comment.