~ staring refactoring
This commit is contained in:
parent
f91b9cf2f0
commit
b6c91e8373
98
src/GUI.cpp
98
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<Image*>(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<Image*>(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);
|
||||
}
|
73
src/GUI.hpp
73
src/GUI.hpp
|
@ -1,79 +1,8 @@
|
|||
#include <iostream>
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Export.H>
|
||||
#include <FL/Fl_Window.H>
|
||||
#include <FL/fl_draw.H>
|
||||
#include <unistd.h>
|
||||
#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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#include "GUI.hpp"
|
||||
#include "GUI_Window.hpp"
|
||||
|
||||
|
||||
/*Function to handle the input*/
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
#include <iostream>
|
||||
#include <FL/Fl.H>
|
||||
#include <FL/Fl_Box.H>
|
||||
#include <FL/Fl_Export.H>
|
||||
#include <FL/Fl_Window.H>
|
||||
#include <FL/fl_draw.H>
|
||||
#include <unistd.h>
|
||||
#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
|
|
@ -1,4 +1,6 @@
|
|||
#include "GUI.hpp"
|
||||
#include <FL/fl_draw.H>
|
||||
#include <vkvm.hpp>
|
||||
#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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
#ifndef GUI_IMAGE_HPP
|
||||
#define GUI_IMAGE_HPP
|
||||
|
||||
|
||||
#include <FL/fl_types.h>
|
||||
#include <FL/Fl_Widget.H>
|
||||
|
||||
/** 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
|
|
@ -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;
|
||||
}
|
|
@ -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
|
|
@ -1,5 +1,5 @@
|
|||
#include <catch2/catch.hpp>
|
||||
#include "../src/GUI.hpp"
|
||||
#include "../src/GUI_Window.hpp"
|
||||
|
||||
TEST_CASE("GUI Test") {
|
||||
REQUIRE(42 == 42);
|
||||
|
|
Loading…
Reference in New Issue