From b6c91e837372f824b76e7f3d0ee9b803c5456780 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Wed, 8 Jan 2020 10:58:03 +0100 Subject: [PATCH] ~ staring refactoring --- src/GUI.cpp | 98 ++++++++++++++++++++++++++-------------------- src/GUI.hpp | 73 +--------------------------------- src/GUI_Window.cpp | 2 +- src/GUI_Window.hpp | 35 +++++++++++++++++ src/Image.cpp | 15 ++----- src/Image.hpp | 26 ++++++++++++ src/Statusbar.cpp | 21 +++++++--- src/Statusbar.hpp | 21 ++++++++++ test/test_GUI.cpp | 2 +- 9 files changed, 160 insertions(+), 133 deletions(-) create mode 100644 src/GUI_Window.hpp create mode 100644 src/Image.hpp create mode 100644 src/Statusbar.hpp diff --git a/src/GUI.cpp b/src/GUI.cpp index 2cde0d1..c19d254 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -1,3 +1,6 @@ +#include "GUI_Window.hpp" +#include "Image.hpp" +#include "Statusbar.hpp" #include "GUI.hpp" @@ -5,60 +8,69 @@ * The GUI_run-function starts all functions needed for the GUI. */ -auto GUI_run(int argc, char **argv) -> int { - vkvm::initialize(0); - int window_height, window_width; - window_height = vkvm::getHeight(); - window_width = vkvm::getWidth(); - char *resolusion = get_resolusion(window_height, window_width); - std::cout << resolusion << std::endl; - auto *window = new GUI_Window(window_width, window_height + 30, "example"); - Statusbar *status[5]; - //Dummy-Values TBD - window->begin(); - auto *image = new Image(0, 30, window_width, window_height); +int redrawInterval; +int width; +int height; - status[0] = new Statusbar(0, 0, 300, 30, resolusion); +static void redrawCallback(void * pointer); +static void refreshCallback(void * pointer); + +int GUI_run(int argc, char **argv) { + vkvm::initialize(0); + width = vkvm::getWidth(); + height = vkvm::getHeight(); + redrawInterval = vkvm::getRedrawInterval(); + char *resolution = get_resolution(width, height); + std::cout << resolution << std::endl; + auto *window = new GUI_Window(width, height + 30, "vKVM GUI"); + + Statusbar *status[5]; + + window->begin(); + auto *image = new Image(0, 30, width, height); + + status[0] = new Statusbar(0, 0, 300, 30, resolution); status[1] = new Statusbar(300, 0, 170, 30, "Event:"); status[2] = new Statusbar(470, 0, 200, 30, "Mouse Position:"); vkvm::registerEvent(vkvm::EventType::Redraw, [image]() { - redraw(image); + image->getPixels(); + image->redraw(); }); - vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image]() { - image->setDelay(vkvm::getRedrawInterval()); + vkvm::registerEvent(vkvm::EventType::UpdateControlRegisters, [image, window, status]() { + int newRedrawInterval = vkvm::getRedrawInterval(); + if(newRedrawInterval != redrawInterval) { + redrawInterval = newRedrawInterval; + } + + int newWidth = vkvm::getWidth(); + int newHeight = vkvm::getHeight(); + + if(newWidth != width || newHeight != height) { + width = newWidth; + height = newHeight; + window->size(width, height + 30); + image->size(width, height); + status[0]->label(get_resolution(width, height)); + + window->redraw(); + } }); - Fl::repeat_timeout(image->getDelay(), get_new_image, image); + + Fl::repeat_timeout((double) redrawInterval / 1000, refreshCallback, image); window->end(); window->show(argc, argv); return Fl::run(); } -char *get_resolusion(int window_height, int window_width) { - char *resolusion = new char[25]; - std::string str_temp; - strcpy(resolusion, "Resolution Height:"); - str_temp = std::to_string(window_height); - strcat(resolusion, str_temp.c_str()); - strcat(resolusion, " Width:"); - str_temp = std::to_string(window_width); - strcat(resolusion, str_temp.c_str()); - return resolusion; +static void refreshCallback(void * pointer) { + auto *image = static_cast(pointer); + image->getPixels(); + + Fl::repeat_timeout((double) redrawInterval / 1000, redrawCallback, image); } -void redraw(Image *image) { - image->getPixels(); +static void redrawCallback(void * pointer) { + auto *image = static_cast(pointer); image->redraw(); -} - -void get_new_image(void *pointer) { - Image *image = ((Image *) pointer); - image->getPixels(); - Fl::repeat_timeout(image->getDelay(), refresh_image, pointer); -} - -void refresh_image(void *pointer) { - ((Image *) pointer)->redraw(); - get_new_image(pointer); -} - - + refreshCallback(image); +} \ No newline at end of file diff --git a/src/GUI.hpp b/src/GUI.hpp index 7943393..1658551 100644 --- a/src/GUI.hpp +++ b/src/GUI.hpp @@ -1,79 +1,8 @@ -#include -#include -#include -#include -#include -#include -#include -#include "Mouse_Status.hpp" -#include "vkvm.hpp" -#include "internal.hpp" - #ifndef GUI_GUI_HPP #define GUI_GUI_HPP -/** GUI_Window - * The GUI_Window-class generates a window, within the window it recognizes the curretn mouse-position. - * It also recognizes if a button is pushed on the keyboard or the mouse. Furthermore the class depict the - * content of the Image-class and provides functions to refresh it. - * @param x The mouse-position on the x-axis. - * @param y The mouse-position on the y-axis. - * @param button The button that was pushed last. - */ -class GUI_Window : public Fl_Window { - int x, y, button,lastX,lastY,mouse_status; - vkvm::KeyCode keyCode; - - int handle(int e) override; - - static char *position_to_string(int x, int y); - -public: - GUI_Window(int x, int y, const char *l); -}; - -/** The constructor of the image class, get additional - * @param x: The mouse-position on the x-axis. - * @param y: The mouse-position on the y-axis: - * @param w: The width of a single pixel, - * @param h: The height of a single pixel. - */ - -class Image : public Fl_Widget { - uchar *buf; - double delay; - -public: - Image(int x, int y, int w, int h); - - void draw() override; - - void getPixels(); - - void setDelay(double delay); - - double getDelay(); -}; - -/** Statusbar - * The class inherits a char *text from the Fl_Box. It also shows the window-resolution and the mouse-position. - * @param text: A pointer to a char-array, that can contain a text like a status or whatever you want. - */ -class Statusbar : public Fl_Box { - char* text; -public: - Statusbar(int x, int y, int w, int h, char *text); -}; - -char *get_resolusion(int window_height, int window_width); - -void refresh_image(void *pointer); - -void get_new_image(void *pointer); - -void redraw(Image *image); int GUI_run(int argc, char **argv); -#endif //GUI_GUI_HPP +#endif diff --git a/src/GUI_Window.cpp b/src/GUI_Window.cpp index 13e1d75..073bf17 100644 --- a/src/GUI_Window.cpp +++ b/src/GUI_Window.cpp @@ -1,4 +1,4 @@ -#include "GUI.hpp" +#include "GUI_Window.hpp" /*Function to handle the input*/ diff --git a/src/GUI_Window.hpp b/src/GUI_Window.hpp new file mode 100644 index 0000000..0cf2ab3 --- /dev/null +++ b/src/GUI_Window.hpp @@ -0,0 +1,35 @@ +#include +#include +#include +#include +#include +#include +#include +#include "Mouse_Status.hpp" +#include "vkvm.hpp" +#include "internal.hpp" +#include "Image.hpp" + +#ifndef GUI_GUI_HPP +#define GUI_GUI_HPP + +/** GUI_Window + * The GUI_Window-class generates a window, within the window it recognizes the curretn mouse-position. + * It also recognizes if a button is pushed on the keyboard or the mouse. Furthermore the class depict the + * content of the Image-class and provides functions to refresh it. + * @param x The mouse-position on the x-axis. + * @param y The mouse-position on the y-axis. + * @param button The button that was pushed last. + */ +class GUI_Window : public Fl_Window { + int x, y, button,lastX,lastY,mouse_status; + vkvm::KeyCode keyCode; + + int handle(int e) override; + + static char *position_to_string(int x, int y); + +public: + GUI_Window(int x, int y, const char *l); +}; +#endif //GUI_GUI_HPP diff --git a/src/Image.cpp b/src/Image.cpp index 9ebee9f..629efca 100644 --- a/src/Image.cpp +++ b/src/Image.cpp @@ -1,4 +1,6 @@ -#include "GUI.hpp" +#include +#include +#include "Image.hpp" /** Image * The Image-class draws the bitmap that it get from the Shared Memory. @@ -13,7 +15,6 @@ auto Image::draw() -> void { Image::Image(int x, int y, int w, int h) : Fl_Widget(x, y, w, h, nullptr) { buf = new uchar[w * h * 3]; - delay = ((double)vkvm::getRedrawInterval())/1000; } /*A function to change the colors of the image-class. It reads the colors from the Shared Memory-Class*/ @@ -26,12 +27,4 @@ auto Image::getPixels() -> void { buf[(i * w() + j) * 3 + 2] = c.getBlue(); } } -} - -auto Image::getDelay() -> double { - return delay; -} - -auto Image::setDelay(double delay) -> void { - this->delay = delay; -} +} \ No newline at end of file diff --git a/src/Image.hpp b/src/Image.hpp new file mode 100644 index 0000000..17a8f96 --- /dev/null +++ b/src/Image.hpp @@ -0,0 +1,26 @@ +#ifndef GUI_IMAGE_HPP +#define GUI_IMAGE_HPP + + +#include +#include + +/** The constructor of the image class, get additional + * @param x: The mouse-position on the x-axis. + * @param y: The mouse-position on the y-axis: + * @param w: The width of a single pixel, + * @param h: The height of a single pixel. + */ + +class Image : public Fl_Widget { + uchar *buf; + +public: + Image(int x, int y, int w, int h); + + void draw() override; + + void getPixels(); +}; + +#endif diff --git a/src/Statusbar.cpp b/src/Statusbar.cpp index 694d84a..cbaf44e 100644 --- a/src/Statusbar.cpp +++ b/src/Statusbar.cpp @@ -1,4 +1,5 @@ -#include "GUI.hpp" +#include "GUI_Window.hpp" +#include "Statusbar.hpp" /** The constructor of Statusbar has to get additional parameters. @@ -7,8 +8,18 @@ * @param w: The width of a single pixel, * @param h: The height of a single pixel. */ -Statusbar::Statusbar(int x, int y, int w, int h, char *text) : - Fl_Box(x, y, w, h, text) { - this->text = text; -}; +Statusbar::Statusbar(int x, int y, int w, int h, char *text) : Fl_Box(x, y, w, h, text) { +} + +char *get_resolution(int window_width, int window_height) { + char *resolution = new char[25]; + std::string str_temp; + strcpy(resolution, "Resolution:"); + str_temp = std::to_string(window_width); + strcat(resolution, str_temp.c_str()); + strcat(resolution, " x "); + str_temp = std::to_string(window_height); + strcat(resolution, str_temp.c_str()); + return resolution; +} \ No newline at end of file diff --git a/src/Statusbar.hpp b/src/Statusbar.hpp new file mode 100644 index 0000000..fb52e31 --- /dev/null +++ b/src/Statusbar.hpp @@ -0,0 +1,21 @@ +// +// Created by joethei on 08.01.20. +// + +#ifndef GUI_STATUSBAR_HPP +#define GUI_STATUSBAR_HPP + +/** Statusbar + * The class inherits a char *text from the Fl_Box. It also shows the window-resolution and the mouse-position. + * @param text: A pointer to a char-array, that can contain a text like a status or whatever you want. + */ +class Statusbar : public Fl_Box { +public: + Statusbar(int x, int y, int w, int h, char *text); +}; + +char *get_resolution(int window_width, int window_height); + + + +#endif diff --git a/test/test_GUI.cpp b/test/test_GUI.cpp index 2010995..4ef4364 100644 --- a/test/test_GUI.cpp +++ b/test/test_GUI.cpp @@ -1,5 +1,5 @@ #include -#include "../src/GUI.hpp" +#include "../src/GUI_Window.hpp" TEST_CASE("GUI Test") { REQUIRE(42 == 42);