From 15b5d042edb3b545a8267ecc2ae9f14e9e88aca5 Mon Sep 17 00:00:00 2001 From: edgar Date: Thu, 14 Nov 2019 15:09:43 +0100 Subject: [PATCH] =?UTF-8?q?Aufger=C3=A4umter=20GUI-Code.=20Klassen=20wurde?= =?UTF-8?q?n=20in=20eigene=20cpp-Dateien=20ausgelagert.=20GUI.hpp=20wurde?= =?UTF-8?q?=20implementiert.=20Klasseninitialisierung=20wurde=20in=20init.?= =?UTF-8?q?cpp=20ausgelagert.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 2 +- main/main.cpp | 201 +-------------------------- src/GUI.hpp | 39 ++++++ src/GUI_Window.cpp | 63 +++++++++ src/Image.cpp | 66 +++++++++ src/Statusbar.cpp | 35 +++++ src/demo.cpp | 5 - src/demo.h | 8 -- src/init.cpp | 35 +++++ test/{test_demo.cpp => test_GUI.cpp} | 4 +- 10 files changed, 247 insertions(+), 211 deletions(-) create mode 100644 src/GUI.hpp create mode 100644 src/GUI_Window.cpp create mode 100644 src/Image.cpp create mode 100644 src/Statusbar.cpp delete mode 100644 src/demo.cpp delete mode 100644 src/demo.h create mode 100644 src/init.cpp rename test/{test_demo.cpp => test_GUI.cpp} (52%) diff --git a/CMakeLists.txt b/CMakeLists.txt index bf43ed9..cd60b08 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ file(GLOB_RECURSE TESTS test/*.cpp) set(LIB_PATH "${CMAKE_SOURCE_DIR}/../library") include_directories(${LIB_PATH}/include) -add_executable(GUI ${SOURCES} ${HEADERS} main/main.cpp) +add_executable(GUI ${SOURCES} ${HEADERS} main/main.cpp src/Statusbar.cpp src/GUI_Window.cpp src/Image.cpp src/GUI.hpp src/init.cpp) target_link_libraries(GUI ${LIB_PATH}/lib/liblibrary.a) diff --git a/main/main.cpp b/main/main.cpp index aca54ba..15c92c4 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,199 +1,10 @@ +#include "GUI.hpp" -#include -#include -#include -#include -#include - -#include "vkvm.hpp" - - -#define window_height 600 -#define window_width 800 - -//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: - /** The constructor of Statusbar has to get additional parameters. - * @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. - */ - Statusbar (int x, int y, int w, int h, char *text) : - Fl_Box(x, y, w, h,text) { - this->text = text; - }; - void set_text(char *text) { - this->text = text; - } - //An exampel to show, how the content of the text can be changed. - void change_text(){ - if(text=="status0") - set_text("status1"); - else - set_text("status0"); - } - //This function refreshes the statusbar. - void refresh_label(){ - this->label(text); - } -}; - - -//My_Window*************************************/ -/** The My_Windows-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. - * @parame x The mouse-position on the x-axis. - * @parame y The mouse-position on the y-axis. - * @parame button The button that was pushed last. - */ - -class My_Window : public Fl_Window { - int x, y, button; - - /*Function to handle the input*/ - int handle(int e) { - switch (e) { - /*Mousebutton*/ - case FL_PUSH: - if (Fl::event_button() == FL_LEFT_MOUSE) { - std::cout << "Mouse:left" << std::endl; - } else if (Fl::event_button() == FL_RIGHT_MOUSE) { - std::cout << "Mouse:right" << std::endl; - } else { - std::cout << "Mouse:middle" << std::endl; - } - return 1; - /*Mousebutton and movement*/ - case FL_DRAG: - x = Fl::event_x(); - y = Fl::event_y(); - std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl; - if (Fl::event_button() == FL_LEFT_MOUSE) { - std::cout << "Mouse:left" << std::endl; - } else if (Fl::event_button() == FL_RIGHT_MOUSE) { - std::cout << "Mouse:right" << std::endl; - } else { - std::cout << "Mouse:middle" << std::endl; - } - return 1; - /*Mousemovement*/ - case FL_MOVE: - x = Fl::event_x(); - y = Fl::event_y(); - std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl; - return 1; - /*keyboardbutton*/ - case FL_KEYBOARD: - button = Fl::event_button(); - std::cout << "Keyboard:" << (unsigned short) button << " down"<redraw(); - Fl::repeat_timeout(0.5, refresh_image, pointer);//nach 0.5 sec refresh_image(pointer) anrufen. Here pointer ist wie ein Parameter -} - -void change_color(void *pointer) { - ((Image *) pointer)->change_color(); - Fl::repeat_timeout(1, change_color, pointer); -} - -void refresh_statusbar(void *pointer) { - ((Statusbar **) pointer)[0]->refresh_label(); - Fl::repeat_timeout(0.5, refresh_statusbar, pointer); -} - -void change_status(void *pointer) { - ((Statusbar **) pointer)[0]->change_text(); - Fl::repeat_timeout(1, change_status, pointer); -} -//main*************************************/ -/** - * The main function initializes all needed classes and executes the functions. +/** main + * The main function executes the functions of the GUI. */ + int main(int argc, char **argv) { - vkvm::initialize(0); - - My_Window *window = new My_Window(window_width, window_height, "example"); - Statusbar *status[5]; - window->begin(); - Image *image = new Image(0, 30, window_width, window_height - 30); - status[0] = new Statusbar(0, 0, 60, 30, "status0"); - status[1] = new Statusbar(60, 0, 60, 30, "status1"); - status[2] = new Statusbar(120, 0, 60, 30, "status2"); - status[3] = new Statusbar(180, 0, 60, 30, "status3"); - status[4] = new Statusbar(240, 0, 60, 30, "status4"); - Fl::repeat_timeout(0.5, refresh_image, image); - Fl::repeat_timeout(1, change_color, image); - Fl::repeat_timeout(0.5, refresh_statusbar, status); - Fl::repeat_timeout(1, change_status, status); - window->end(); - window->show(argc, argv); - return Fl::run(); + GUI_init(); + GUI_run(); } \ No newline at end of file diff --git a/src/GUI.hpp b/src/GUI.hpp new file mode 100644 index 0000000..74b9fd9 --- /dev/null +++ b/src/GUI.hpp @@ -0,0 +1,39 @@ +#include +#include +#include +#include +#include +#include "vkvm.hpp" + +#ifndef GUI_GUI_HPP +#define GUI_GUI_HPP +class GUI_Window : public Fl_Window { + int x, y, button; + int handle(int e); +public: + GUI_Window(int x, int y, const char *l) : Fl_Window(x, y, l) +} + +class Image : public Fl_Widget { + uchar *buf; +public: + Image(int x, int y, int w, int h); + void refresh_image(void *pointer); + void change_color(void *pointer); + void refresh_statusbar(void *pointer); + void change_status(void *pointer); +} + +class Statusbar : public Fl_Box { + char *text; +public: + Statusbar (int x, int y, int w, int h, char *text); + void set_text(char *text); + void change_text(); + void refresh_label(); +} + +void GUI_init(); +void GUI_run(); + +#endif //GUI_GUI_HPP diff --git a/src/GUI_Window.cpp b/src/GUI_Window.cpp new file mode 100644 index 0000000..73fb96e --- /dev/null +++ b/src/GUI_Window.cpp @@ -0,0 +1,63 @@ +#include "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; + + /*Function to handle the input*/ + int handle(int e) { + switch (e) { + /*Mousebutton*/ + case FL_PUSH: + if (Fl::event_button() == FL_LEFT_MOUSE) { + std::cout << "Mouse:left" << std::endl; + } else if (Fl::event_button() == FL_RIGHT_MOUSE) { + std::cout << "Mouse:right" << std::endl; + } else { + std::cout << "Mouse:middle" << std::endl; + } + return 1; + /*Mousebutton and movement*/ + case FL_DRAG: + x = Fl::event_x(); + y = Fl::event_y(); + std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl; + if (Fl::event_button() == FL_LEFT_MOUSE) { + std::cout << "Mouse:left" << std::endl; + } else if (Fl::event_button() == FL_RIGHT_MOUSE) { + std::cout << "Mouse:right" << std::endl; + } else { + std::cout << "Mouse:middle" << std::endl; + } + return 1; + /*Mousemovement*/ + case FL_MOVE: + x = Fl::event_x(); + y = Fl::event_y(); + std::cout << "Postion X:" << x << " Postion Y:" << y << std::endl; + return 1; + /*keyboardbutton*/ + case FL_KEYBOARD: + button = Fl::event_button(); + std::cout << "Keyboard:" << (unsigned short) button << " down"<redraw(); + Fl::repeat_timeout(0.5, refresh_image, pointer);//nach 0.5 sec refresh_image(pointer) anrufen. Here pointer ist wie ein Parameter +} + +void change_color(void *pointer) { + ((Image *) pointer)->change_color(); + Fl::repeat_timeout(1, change_color, pointer); +} + +void refresh_statusbar(void *pointer) { + ((Statusbar **) pointer)[0]->refresh_label(); + Fl::repeat_timeout(0.5, refresh_statusbar, pointer); +} + +void change_status(void *pointer) { + ((Statusbar **) pointer)[0]->change_text(); + Fl::repeat_timeout(1, change_status, pointer); +} diff --git a/src/Statusbar.cpp b/src/Statusbar.cpp new file mode 100644 index 0000000..4bdfc2b --- /dev/null +++ b/src/Statusbar.cpp @@ -0,0 +1,35 @@ +#include "GUI.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 { + char *text; +public: + /** The constructor of Statusbar has to get additional parameters. + * @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. + */ + Statusbar (int x, int y, int w, int h, char *text) : + Fl_Box(x, y, w, h,text) { + this->text = text; + }; + void set_text(char *text) { + this->text = text; + } + //An exampel to show, how the content of the text can be changed. + void change_text(){ + if(text=="status0") + set_text("status1"); + else + set_text("status0"); + } + //This function refreshes the statusbar. + void refresh_label(){ + this->label(text); + } +}; diff --git a/src/demo.cpp b/src/demo.cpp deleted file mode 100644 index 1cc01fc..0000000 --- a/src/demo.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "demo.h" - -int test() { - return 42; -} diff --git a/src/demo.h b/src/demo.h deleted file mode 100644 index 9b77cb9..0000000 --- a/src/demo.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SHARED_MEMORY_DEMO_H -#define SHARED_MEMORY_DEMO_H - - -int test(); - - -#endif //SHARED_MEMORY_DEMO_H diff --git a/src/init.cpp b/src/init.cpp new file mode 100644 index 0000000..d42a800 --- /dev/null +++ b/src/init.cpp @@ -0,0 +1,35 @@ +#include "GUI.hpp" +#define window_height 600 +#define window_width 800 +/** GUI_init + * The GUI_init-function initializes all needed classes for the GUI. + */ +void GUI_init() { + vkvm::initialize(0); + My_Window *window = new My_Window(window_width, window_height, "example"); + Statusbar *status[5]; + Image *image = new Image(0, 30, window_width, window_height - 30); + + //Dummy-Values TBD + status[0] = new Statusbar(0, 0, 60, 30, "status0"); + status[1] = new Statusbar(60, 0, 60, 30, "status1"); + status[2] = new Statusbar(120, 0, 60, 30, "status2"); + status[3] = new Statusbar(180, 0, 60, 30, "status3"); + status[4] = new Statusbar(240, 0, 60, 30, "status4"); + +} + +/** GUI_run + * The GUI_run-function starts all functions needed for the GUI. + */ + +void GUI_run() { + window->begin(); + Fl::repeat_timeout(0.5, refresh_image, image); + Fl::repeat_timeout(1, change_color, image); + Fl::repeat_timeout(0.5, refresh_statusbar, status); + Fl::repeat_timeout(1, change_status, status); + window->end(); + window->show(argc, argv); + return Fl::run(); +} diff --git a/test/test_demo.cpp b/test/test_GUI.cpp similarity index 52% rename from test/test_demo.cpp rename to test/test_GUI.cpp index 7cb240e..f2244bb 100644 --- a/test/test_demo.cpp +++ b/test/test_GUI.cpp @@ -1,6 +1,6 @@ #include -#include "../src/demo.h" +#include "../src/GUI.hpp" -TEST_CASE("Demo test") { +TEST_CASE("GUI Test") { REQUIRE(test() == 42); } \ No newline at end of file