Ejercicio tercero completado y funcionando

parent 5493a422
// /**
// Created by pete on 18/09/25. * @file BackgroundWindow.cpp
// * @author dpp00022
*
* @date 2025/08/18
*
* @brief Implementación de la clase BackgroundWindow
*/
#include "BackgroundWindow.h" #include "BackgroundWindow.h"
#include <imgui.h> #include <imgui.h>
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
/**
* Constructor de la clase
*/
BackgroundWindow::BackgroundWindow() { BackgroundWindow::BackgroundWindow() {
color_fondo = glm::vec3(0.6f, 0.6f, 0.6f); color_fondo = glm::vec3(0.6f, 0.6f, 0.6f);
} }
/**
* @brief Mét0do de aviso a los listeners de este objeto
*
* Esta función pasa el color actual de fondo a los listeners.
*/
void BackgroundWindow::warnListeners() { void BackgroundWindow::warnListeners() {
glm::vec3 aux = color_fondo; glm::vec3 aux = color_fondo;
...@@ -19,6 +32,9 @@ void BackgroundWindow::warnListeners() { ...@@ -19,6 +32,9 @@ void BackgroundWindow::warnListeners() {
} }
} }
/**
* Función de renderizado de la ventana a través de ImGui
*/
void BackgroundWindow::render() { void BackgroundWindow::render() {
ImGui::SetNextWindowPos( ImVec2(10, 10), ImGuiCond_Once ); ImGui::SetNextWindowPos( ImVec2(10, 10), ImGuiCond_Once );
......
// /**
// Created by pete on 18/09/25. * @file BackgroundWindow.h
// * @author dpp00022
*
* @date 2025/09/18
*
* @brief Declaración de la clase BackgroundWindow
*/
#ifndef BACKGROUNDWINDOW_H #ifndef BACKGROUNDWINDOW_H
#define BACKGROUNDWINDOW_H #define BACKGROUNDWINDOW_H
...@@ -8,10 +13,13 @@ ...@@ -8,10 +13,13 @@
#include <glm/vec3.hpp> #include <glm/vec3.hpp>
#include "GuiElement.h" #include "GuiElement.h"
/**
* Clase que implementa un elemento de la interfaz con un
* selector de color para el color de fondo de la ventana.
*/
class BackgroundWindow: public GuiElement { class BackgroundWindow: public GuiElement {
glm::vec3 color_fondo; glm::vec3 color_fondo; ///< String que almacena el color de fondo actual
public: public:
BackgroundWindow(); BackgroundWindow();
virtual ~BackgroundWindow() = default; virtual ~BackgroundWindow() = default;
...@@ -19,6 +27,4 @@ public: ...@@ -19,6 +27,4 @@ public:
void render() override; void render() override;
}; };
#endif //BACKGROUNDWINDOW_H #endif //BACKGROUNDWINDOW_H
...@@ -17,6 +17,9 @@ PAG::GUI* PAG::GUI::instancia = nullptr; ...@@ -17,6 +17,9 @@ PAG::GUI* PAG::GUI::instancia = nullptr;
BackgroundWindow PAG::GUI::background = BackgroundWindow(); BackgroundWindow PAG::GUI::background = BackgroundWindow();
LogWindow PAG::GUI::log = LogWindow(); LogWindow PAG::GUI::log = LogWindow();
/**
* Constructor de la clase
*/
PAG::GUI::GUI() { PAG::GUI::GUI() {
background.addListener(&PAG::Renderer::getInstancia()); background.addListener(&PAG::Renderer::getInstancia());
} }
...@@ -100,4 +103,13 @@ void PAG::GUI::callback_raton(int button, bool pulsado) { ...@@ -100,4 +103,13 @@ void PAG::GUI::callback_raton(int button, bool pulsado) {
io.AddMouseButtonEvent(button, pulsado); io.AddMouseButtonEvent(button, pulsado);
} }
/**
* @brief Función para agregar un mensaje al registro
*
* Se pasa la responsabilidad a la clase LogWindow.
*
* @param texto Mensaje que se desea registrar
*/
void PAG::GUI::registrarMensaje(std::string texto) {
log.registrarMensaje(texto);
}
\ No newline at end of file
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#define PAG_P1_GUI_H #define PAG_P1_GUI_H
#include <imgui_impl_glfw.h> #include <imgui_impl_glfw.h>
#include <string>
#include "BackgroundWindow.h" #include "BackgroundWindow.h"
#include "LogWindow.h" #include "LogWindow.h"
...@@ -40,6 +40,7 @@ namespace PAG { ...@@ -40,6 +40,7 @@ namespace PAG {
void destruirGUI(); void destruirGUI();
void callback_raton(int button, bool pulsado); void callback_raton(int button, bool pulsado);
void registrarMensaje(std::string texto);
}; };
} }
......
// /**
// Created by pete on 18/09/25. * @file GuiElement.cpp
// * @author dpp00022
*
* @date 2025/08/18
*
* @brief Implementación de la clase abstracta GuiElement
*/
#include "GuiElement.h" #include "GuiElement.h"
/**
* Constructor de la clase
*/
GuiElement::GuiElement() { GuiElement::GuiElement() {
listeners = std::vector<Listener*>(); listeners = std::vector<Listener*>();
} }
/**
* Función para agregar un listener a la lista
* @param listener Puntero al objeto listener que se desea registrar
*/
void GuiElement::addListener(Listener *listener) { void GuiElement::addListener(Listener *listener) {
listeners.push_back(listener); listeners.push_back(listener);
} }
// /**
// Created by pete on 18/09/25. * @file GuiElement.h
// * @author dpp00022
*
* @date 2025/09/18
*
* @brief Declaración de la clase abstracta GuiElement
*/
#ifndef GUIELEMENT_H #ifndef GUIELEMENT_H
#define GUIELEMENT_H #define GUIELEMENT_H
#include <vector> #include <vector>
#include "Listener.h" #include "Listener.h"
/**
* @brief Clase abstracta que implementa el comportamiento de
* un elemento de la interfaz de usuario.
*
* La clase implementa el comportamiento de agregación de objetos
* listener a los que avisar cuando haya actualizaciones. En cambio,
* no implementa cómo avisar a estos Listeners ni cómo se debe
* renderizar el elemento.
*/
class GuiElement { class GuiElement {
protected: protected:
std::vector<Listener*> listeners; std::vector<Listener*> listeners;
......
// /**
// Created by pete on 18/09/25. * @file Listener.h
// * @author dpp00022
*
* @date 2025/09/18
*
* @brief Declaración de la interfaz Listener
*/
#ifndef LISTENER_H #ifndef LISTENER_H
#define LISTENER_H #define LISTENER_H
// TODO: Cuando el profesor añada el showAxis, cambiar Background a 2 // TODO: Cuando el profesor añada el showAxis, cambiar Background a 2
/**
* @brief Tipo enumerado para discernir los diferentes tipos de ventanas de la GUI.
*
* El valor es igual al número de argumentos que se pasan en la lista variable.
*/
enum WindowType { enum WindowType {
Background = 1 Background = 1
}; };
/**
* Interfaz que especifica el comportamiento de una clase que escucha
* las actualizaciones de otra clase.
*/
class Listener { class Listener {
public: public:
Listener() = default; Listener() = default;
......
// /**
// Created by pete on 19/09/25. * @file LogWindow.cpp
// * @author dpp00022
*
* @date 2025/08/19
*
* @brief Implementación de la clase LogWindow
*/
#include "LogWindow.h" #include "LogWindow.h"
#include <imgui.h> #include <imgui.h>
/**
* Constructor de la clase
*/
LogWindow::LogWindow() { LogWindow::LogWindow() {
log = std::string(); log = std::string();
} }
/**
* @brief Mét0do de aviso a los listeners de este objeto
*
* Esta clase no avisa a listeners.
*/
void LogWindow::warnListeners() { } void LogWindow::warnListeners() { }
/**
* Función de renderizado de la ventana a través de ImGui
*/
void LogWindow::render() { void LogWindow::render() {
ImGui::SetNextWindowPos(ImVec2(200, 10), ImGuiCond_Once); ImGui::SetNextWindowPos(ImVec2(200, 10), ImGuiCond_Once);
ImGui::SetNextWindowSize(ImVec2(300, 200), ImGuiCond_Once); ImGui::SetNextWindowSize(ImVec2(300, 200), ImGuiCond_Once);
...@@ -23,3 +38,12 @@ void LogWindow::render() { ...@@ -23,3 +38,12 @@ void LogWindow::render() {
} }
ImGui::End(); ImGui::End();
} }
/**
* Función para agregar un mensaje al registro
*
* @param texto Mensaje que se desea registrar
*/
void LogWindow::registrarMensaje(std::string texto) {
log.append(texto).append("\n");
}
// /**
// Created by pete on 19/09/25. * @file LogWindow.h
// * @author dpp00022
*
* @date 2025/09/19
*
* @brief Declaración de la clase LogWindow
*/
#ifndef LOGWINDOW_H #ifndef LOGWINDOW_H
#define LOGWINDOW_H #define LOGWINDOW_H
...@@ -8,14 +13,21 @@ ...@@ -8,14 +13,21 @@
#include <string> #include <string>
#include "GuiElement.h" #include "GuiElement.h"
/**
* Clase que implementa un elemento de la interfaz con un
* registro de los mensajes generados durante la ejecución
* del programa.
*/
class LogWindow: public GuiElement{ class LogWindow: public GuiElement{
std::string log; std::string log; ///< String que almacena el registro de mensajes
public: public:
LogWindow(); LogWindow();
virtual ~LogWindow() = default; virtual ~LogWindow() = default;
void warnListeners() override; void warnListeners() override;
void render() override; void render() override;
void registrarMensaje(std::string texto);
}; };
#endif //LOGWINDOW_H #endif //LOGWINDOW_H
...@@ -64,6 +64,12 @@ void PAG::Renderer::refrescar_tamanio_framebuffer(int width, int height) { ...@@ -64,6 +64,12 @@ void PAG::Renderer::refrescar_tamanio_framebuffer(int width, int height) {
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
} }
/**
* Función que implementa el mét0do de actualización del Renderer.
*
* @param t Clase de ventana que está avisando a este Listener
* @param ... Lista de argumentos propios de la ventana
*/
void PAG::Renderer::wakeUp(WindowType t, ...) { void PAG::Renderer::wakeUp(WindowType t, ...) {
switch (t) { switch (t) {
case Background: { case Background: {
......
#include <iostream> #include <iostream>
#include <glad/glad.h> #include <glad/glad.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "GUI.h" #include "GUI.h"
#include "Renderer.h" #include "Renderer.h"
// - Esta función callback será llamada cuando GLFW produzca algún error // - Esta función callback será llamada cuando GLFW produzca algún error
void error_callback(int errno, const char* desc) { void error_callback(int errno, const char* desc) {
std::string aux(desc); std::string aux = std::string();
std::cout << "Error de GLFW número " << errno << ": " << aux << std::endl; aux.append("Error de GLFW número ").append(std::to_string(errno)).append(desc);
std::cout << aux << std::endl;
PAG::GUI::getInstancia().registrarMensaje(aux);
} }
// - Esta función callback será llamada cada vez que el área de dibujo // - Esta función callback será llamada cada vez que el área de dibujo
// OpenGL deba ser redibujada. // OpenGL deba ser redibujada.
void refresh_window_callback(GLFWwindow *window) { void refresh_window_callback(GLFWwindow *window) {
// Llamada al método refrescar del Renderer // Llamada al mét0do refrescar del Renderer
PAG::Renderer::getInstancia().refrescar(); PAG::Renderer::getInstancia().refrescar();
std::cout << "Refresh callback called" << std::endl; std::cout << "Refresh callback called" << std::endl;
PAG::GUI::getInstancia().registrarMensaje("Refresh callback called");
} }
// - Esta función callback será llamada cada vez que se cambie el tamaño // - Esta función callback será llamada cada vez que se cambie el tamaño
...@@ -25,6 +27,7 @@ void refresh_window_callback(GLFWwindow *window) { ...@@ -25,6 +27,7 @@ void refresh_window_callback(GLFWwindow *window) {
void framebuffer_size_callback(GLFWwindow *window, int width, int height) { void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
PAG::Renderer::getInstancia().refrescar_tamanio_framebuffer(width, height); PAG::Renderer::getInstancia().refrescar_tamanio_framebuffer(width, height);
std::cout << "Resize callback called" << std::endl; std::cout << "Resize callback called" << std::endl;
PAG::GUI::getInstancia().registrarMensaje("Resize callback called");
} }
// - Esta función callback será llamada cada vez que se pulse una tecla // - Esta función callback será llamada cada vez que se pulse una tecla
...@@ -34,17 +37,24 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int mod ...@@ -34,17 +37,24 @@ void key_callback(GLFWwindow *window, int key, int scancode, int action, int mod
glfwSetWindowShouldClose(window, GLFW_TRUE); glfwSetWindowShouldClose(window, GLFW_TRUE);
} }
std::cout << "Key callback called" << std::endl; std::cout << "Key callback called" << std::endl;
PAG::GUI::getInstancia().registrarMensaje("Key callback called");
} }
// - Esta función callback será llamada cada vez que se pulse algún botón // - Esta función callback será llamada cada vez que se pulse algún botón
// del ratón sobre el área de dibujo OpenGL. // del ratón sobre el área de dibujo OpenGL.
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) {
if (action == GLFW_PRESS) { if (action == GLFW_PRESS) {
std::cout << "Pulsado el botón " << button << std::endl; std::string aux = std::string();
aux.append("Pulsado el botón ").append(std::to_string(button));
std::cout << aux << std::endl;
PAG::GUI::getInstancia().registrarMensaje(aux);
PAG::GUI::getInstancia().callback_raton(button, true); PAG::GUI::getInstancia().callback_raton(button, true);
} }
else if (action == GLFW_RELEASE) { else if (action == GLFW_RELEASE) {
std::cout << "Soltado el botón " << button << std::endl; std::string aux = std::string();
aux.append("Soltado el botón ").append(std::to_string(button));
std::cout << aux << std::endl;
PAG::GUI::getInstancia().registrarMensaje(aux);
PAG::GUI::getInstancia().callback_raton(button, false); PAG::GUI::getInstancia().callback_raton(button, false);
} }
refresh_window_callback(window); refresh_window_callback(window);
...@@ -55,9 +65,13 @@ void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) ...@@ -55,9 +65,13 @@ void mouse_button_callback(GLFWwindow *window, int button, int action, int mods)
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) { void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
refresh_window_callback(window); refresh_window_callback(window);
std::cout << "Movida la rueda del ratón " << xoffset std::string aux = std::string();
<< " unidades en horizontal y " << yoffset aux.append("Movida la rueda del ratón ").append(std::to_string(xoffset))
<< " unidades en vertical" << std::endl; .append(" unidades en horizontal y ").append(std::to_string(yoffset))
.append(" unidades en vertical.");
std::cout << aux << std::endl;
PAG::GUI::getInstancia().registrarMensaje(aux);
} }
int main() { int main() {
...@@ -125,6 +139,12 @@ int main() { ...@@ -125,6 +139,12 @@ int main() {
// Inicializamos la GUI a través de ImGui // Inicializamos la GUI a través de ImGui
PAG::GUI::getInstancia().inicializarGUI(window); PAG::GUI::getInstancia().inicializarGUI(window);
PAG::GUI::getInstancia().registrarMensaje(std::string((const char*)glGetString ( GL_RENDERER )));
PAG::GUI::getInstancia().registrarMensaje(std::string((const char*)glGetString ( GL_VENDOR )));
PAG::GUI::getInstancia().registrarMensaje(std::string((const char*)glGetString ( GL_VERSION )));
PAG::GUI::getInstancia().registrarMensaje(std::string((const char*)glGetString ( GL_SHADING_LANGUAGE_VERSION )));
// - Ciclo de eventos de la aplicación. La condición de parada es que la // - Ciclo de eventos de la aplicación. La condición de parada es que la
// ventana principal deba cerrarse. Por ejemplo, si el usuario pulsa el // ventana principal deba cerrarse. Por ejemplo, si el usuario pulsa el
// botón de cerrar la ventana (la X). // botón de cerrar la ventana (la X).
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment