Go Down

Topic: My GUI library for a touchscreen (Read 94 times) previous topic - next topic

fenyesb

Aug 15, 2017, 07:29 pm Last Edit: Aug 15, 2017, 07:38 pm by fenyesb
Hello  :) , I made a library for displaying windows and in them buttons and texts. This was done as a homework project recently for a c++ course at my university, but maybe someone else may also find it useful. I used a Mega with a 320x240 TFT, but it should be easily adjustable.

You can add new windows and controls, by inheriting from the classes and overriding the appropriate functions. For example the code for the button class is like this, so you usually have to override the Touch and Draw functions if you want to create a more advanced button:
Code: [Select]
#include "Button.h"
#include "Config.h"
#include <string>

Button::Button(int x, int y) : Label(x, y), callback(NULL)
{
}

Button::~Button()
{
}

void Button::Draw(Graphics graphics) const
{
 graphics.Color(0, 70, 255);
 graphics.Fill();
 graphics.Shrink(BUTTON_BORDER);
 graphics.Color(255, 255, 255);
 graphics.Text(text.c_str());
}

void Button::SetCallback(void (*f)(Button*))
{
 callback = f;
}

void Button::SetText(std::string s)
{
 text = s;
}

void Button::Touch()
{
 if(callback != NULL)
 callback(this);
}


This is how you add controls to a window:
Code: [Select]

MainWindow::MainWindow(int x, int y, int w, int h) : Window(x, y, w, h)
{
 SetTitle("Control test");

 Label* mylabel = new Label(10, 10);
 mylabel->SetText("Hello Label");
 AddChild(mylabel);

 NumericInput* numinput = new NumericInput(10, 30);
 numinput->SetRange(-100, 100);
 AddChild(numinput);
 
 Button* mybutton = new Button(10, 60);
 mybutton->SetText("Open dialog");
 mybutton->SetCallback(open_dialog);
 AddChild(mybutton);

 Button* filesbutton = new Button(10, 90);
 filesbutton->SetText("Open SD card");
 filesbutton->SetCallback(open_sd);
 AddChild(filesbutton);
 
 Checkbox* checkbox = new Checkbox(10, 120);
 checkbox->SetText("Checkbox");
 AddChild(checkbox);
 
 Button* resetbutton = new Button(10, 150);
 resetbutton->SetText("Reset");
 resetbutton->SetCallback(reset);
 AddChild(resetbutton);
}


link to the github page

If you have any feedback, feel free to post it here.  :)


Go Up