Skip to content
Snippets Groups Projects
Commit 5c5d92de authored by Joakim Borge Hunskår's avatar Joakim Borge Hunskår
Browse files

Merge branch 'more-button-widgets' into 'main'

More button widgets

See merge request !10
parents 4a061b0e 4db8244d
No related branches found
No related tags found
1 merge request!10More button widgets
Showing
with 218 additions and 17 deletions
project('testproject', 'cpp',
version : '0.1',
default_options : ['warning_level=0',
'cpp_std=c++17',
'cpp_std=c++20',
'c_std=c17',
'default_library=static'])
......
......@@ -4,6 +4,7 @@
#include "Point.h"
#include <string>
#include "Color.h"
namespace TDT4102 {
class Button : public TDT4102::Widget {
private:
......@@ -19,7 +20,7 @@ namespace TDT4102 {
void update(nk_context* context) override;
public:
explicit Button(TDT4102::Point location, unsigned int width, unsigned int height, std::string label);
void setLabel(std::string newlabel);
void setLabel(std::string newLabel);
void setLabelColor(Color newColor) {labelColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};};
void setButtonColor(Color newColor) {buttonColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setButtonColorHover(Color newColor) {buttonColorHover = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
......
#pragma once
#include "Widget.h"
#include "Point.h"
#include <string>
#include "Color.h"
namespace TDT4102 {
class CheckBox : public TDT4102::Widget {
private:
std::string label;
nk_bool isSelected_ = false;
bool lastRightMouseButtonState = false;
bool lastLeftMouseButtonState = false;
nk_color labelColor = nk_rgba(100, 100, 100, 255);
nk_color checkBoxColor = nk_rgba(150, 150, 150, 255);
nk_color checkBoxColorHover = nk_rgba(150, 150, 150, 255);
nk_color checkBoxColorActive = nk_rgba(150, 150, 150, 255);
protected:
void update(nk_context* context) override;
public:
explicit CheckBox(TDT4102::Point location, unsigned int width, unsigned int height, std::string label);
bool isSelected() const;
void setLabel(std::string newLabel);
void setLabelColor(Color newColor) {labelColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};};
void setCheckBoxColor(Color newColor) {checkBoxColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setCheckBoxColorHover(Color newColor) {checkBoxColorHover = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setCheckBoxColorActive(Color newColor) {checkBoxColorActive = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
};
}
\ No newline at end of file
#pragma once
#include "Widget.h"
#include "Point.h"
#include <string>
#include "Color.h"
namespace TDT4102 {
class RadioButton : public TDT4102::Widget {
private:
std::string label;
nk_bool isSelected_ = false;
bool lastRightMouseButtonState = false;
bool lastLeftMouseButtonState = false;
nk_color labelColor = nk_rgba(100, 100, 100, 255);
nk_color radioColor = nk_rgba(150, 150, 150, 255);
nk_color radioColorHover = nk_rgba(150, 150, 150, 255);
nk_color radioColorActive = nk_rgba(150, 150, 150, 255);
protected:
void update(nk_context* context) override;
public:
explicit RadioButton(TDT4102::Point location, unsigned int width, unsigned int height, std::string label);
bool isSelected() const;
void setLabel(std::string newLabel);
void setLabelColor(Color newColor) {labelColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};};
void setRadioColor(Color newColor) {radioColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setRadioColorHover(Color newColor) {radioColorHover = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setRadioColorActive(Color newColor) {radioColorActive = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
};
}
\ No newline at end of file
......@@ -12,7 +12,7 @@ namespace TDT4102 {
class TextBox : public TDT4102::Widget {
private:
std::string text;
std::string contents;
nk_color textColor = nk_rgba(175, 175, 175, 255);
nk_color boxColor = nk_rgba(50 , 50, 50, 255);
nk_color borderColor = nk_rgba(50, 50, 50, 255);
......@@ -20,6 +20,7 @@ namespace TDT4102 {
void update(nk_context* context) override;
public:
explicit TextBox(TDT4102::Point location, unsigned int width, unsigned int height, std::string initialText = "");
std::string getText() const;
void setText(std::string updatedText);
void setTextColor(TDT4102::Color newColor) {textColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setBoxColor(TDT4102::Color newColor) {boxColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
......
project('animationwindow', ['c', 'cpp'], version: '0.01', default_options: ['cpp_std=c++20', 'default_library=static', 'buildtype=debugoptimized'])
project('animationwindow', ['c', 'cpp'], version: '0.01', default_options: ['cpp_std=c++20', 'c_std=c17', 'default_library=static', 'buildtype=debugoptimized'])
if host_machine.system() == 'windows'
sdl2_dep = subproject('sdl2_windows').get_variable('sdl2_windows_dep')
......@@ -14,6 +14,8 @@ build_files = [
'src/internal/KeyboardKeyConverter.cpp',
'src/internal/nuklear_implementation.cpp',
'src/widgets/Button.cpp',
'src/widgets/RadioButton.cpp',
'src/widgets/CheckBox.cpp',
'src/widgets/TextInput.cpp',
'src/widgets/TextBox.cpp',
'src/widgets/DropdownList.cpp',
......
......@@ -32,6 +32,6 @@ void TDT4102::Button::update(nk_context *context) {
nk_style_pop_style_item(context);
}
void TDT4102::Button::setLabel(std::string newlabel) {
label = newlabel;
void TDT4102::Button::setLabel(std::string newLabel) {
label = newLabel;
}
#include "widgets/CheckBox.h"
TDT4102::CheckBox::CheckBox(TDT4102::Point location, unsigned int width, unsigned int height, std::string label) :
TDT4102::Widget(location, width, height),
label{std::move(label)} {}
void TDT4102::CheckBox::update(nk_context *context) {
if (nk_input_has_mouse_click_down_in_rect(&context->input, NK_BUTTON_RIGHT, context->current->layout->clip, true)) {
isSelected_ = false;
}
if (nk_input_has_mouse_click_down_in_rect(&context->input, NK_BUTTON_LEFT, context->current->layout->clip, true)) {
isSelected_ = true;
}
struct nk_style* s = &context->style;
nk_style_push_color(context, &s->checkbox.text_normal, labelColor);
nk_style_push_color(context, &s->checkbox.text_hover, labelColor);
nk_style_push_color(context, &s->checkbox.text_active, labelColor);
nk_style_push_style_item(context, &s->checkbox.normal, nk_style_item_color(checkBoxColor));
nk_style_push_style_item(context, &s->checkbox.hover, nk_style_item_color(checkBoxColorHover));
nk_style_push_style_item(context, &s->checkbox.active, nk_style_item_color(checkBoxColorActive));
if (nk_checkbox_label(context, label.data(), &isSelected_)) {
isSelected_ = true;
fire();
}
nk_style_pop_color(context);
nk_style_pop_color(context);
nk_style_pop_color(context);
nk_style_pop_style_item(context);
nk_style_pop_style_item(context);
nk_style_pop_style_item(context);
}
void TDT4102::CheckBox::setLabel(std::string newLabel) {
label = newLabel;
}
bool TDT4102::CheckBox::isSelected() const {
return (bool)isSelected_;
}
#include "widgets/RadioButton.h"
TDT4102::RadioButton::RadioButton(TDT4102::Point location, unsigned int width, unsigned int height, std::string label) :
TDT4102::Widget(location, width, height),
label{std::move(label)} {}
void TDT4102::RadioButton::update(nk_context *context) {
if (nk_input_has_mouse_click_down_in_rect(&context->input, NK_BUTTON_RIGHT, context->current->layout->clip, true)) {
isSelected_ = false;
}
if (nk_input_has_mouse_click_down_in_rect(&context->input, NK_BUTTON_LEFT, context->current->layout->clip, true)) {
isSelected_ = true;
}
struct nk_style* s = &context->style;
nk_style_push_color(context, &s->option.text_normal, labelColor);
nk_style_push_color(context, &s->option.text_hover, labelColor);
nk_style_push_color(context, &s->option.text_active, labelColor);
nk_style_push_style_item(context, &s->option.normal, nk_style_item_color(radioColor));
nk_style_push_style_item(context, &s->option.hover, nk_style_item_color(radioColorHover));
nk_style_push_style_item(context, &s->option.active, nk_style_item_color(radioColorActive));
if (nk_radio_label(context, label.data(), &isSelected_)) {
isSelected_ = true;
fire();
}
nk_style_pop_color(context);
nk_style_pop_color(context);
nk_style_pop_color(context);
nk_style_pop_style_item(context);
nk_style_pop_style_item(context);
nk_style_pop_style_item(context);
}
void TDT4102::RadioButton::setLabel(std::string newLabel) {
label = newLabel;
}
bool TDT4102::RadioButton::isSelected() const {
return (bool)isSelected_;
}
......@@ -5,7 +5,7 @@ void TDT4102::TextBox::update(nk_context *context) {
nk_style_push_color(context, &s->edit.text_normal, textColor);
nk_style_push_color(context, &s->edit.border_color, borderColor);
nk_style_push_style_item(context, &s->edit.normal, nk_style_item_color(boxColor));
nk_edit_string_zero_terminated(context, NK_EDIT_READ_ONLY, const_cast<char*>(text.data()), internal::TEXT_BOX_CHARACTER_LIMIT, nk_filter_ascii);
nk_edit_string_zero_terminated(context, NK_EDIT_READ_ONLY, const_cast<char*>(contents.data()), internal::TEXT_BOX_CHARACTER_LIMIT, nk_filter_ascii);
nk_style_pop_color(context);
nk_style_pop_color(context);
nk_style_pop_style_item(context);
......@@ -13,13 +13,17 @@ void TDT4102::TextBox::update(nk_context *context) {
TDT4102::TextBox::TextBox(TDT4102::Point location, unsigned int width, unsigned int height, std::string initialText)
: TDT4102::Widget(location, width, height) {
text = initialText;
text.resize(internal::TEXT_BOX_CHARACTER_LIMIT);
contents = initialText;
contents.resize(internal::TEXT_BOX_CHARACTER_LIMIT);
}
std::string TDT4102::TextBox::getText() const {
return std::string(contents.data());;
}
void TDT4102::TextBox::setText(std::string updatedText) {
text = updatedText;
text.resize(internal::TEXT_BOX_CHARACTER_LIMIT);
contents = updatedText;
contents.resize(internal::TEXT_BOX_CHARACTER_LIMIT);
}
project('std_lib_facilities', ['c', 'cpp'], version: '0.01', default_options: ['cpp_std=c++17', 'default_library=static', 'buildtype=debugoptimized'])
project('std_lib_facilities', ['c', 'cpp'], version: '0.01', default_options: ['cpp_std=c++20', 'c_std=c17', 'default_library=static', 'buildtype=debugoptimized'])
incdir = include_directories('.')
install_subdir('include', install_dir: '.')
......
......@@ -10,7 +10,10 @@
#include "widgets/Button.h"
#include "widgets/TextInput.h"
#include "widgets/RadioButton.h"
#include "widgets/TextBox.h"
#include "widgets/RadioButton.h"
#include "widgets/CheckBox.h"
struct Ball {
float x = 0;
......@@ -64,24 +67,45 @@ int main(int argc, char* argv[]) {
TDT4102::TextBox b{{300, 200}, 108, 35, "TEXTBOX!"};
window.add(b);
b.setBoxColor(TDT4102::Color::dark_orange);
b.setTextColor(TDT4102::Color::green);
input.setCallback(&textChanged);
window.add(input);
TDT4102::RadioButton rb{{200, 200}, 100, 30, "Easy"};
window.add(rb);
rb.setLabelColor(TDT4102::Color::salmon);
rb.setRadioColor(TDT4102::Color::gold);
rb.setRadioColorHover(TDT4102::Color::lime_green);
rb.setRadioColorActive(TDT4102::Color::aquamarine);
TDT4102::CheckBox cb{{200, 300}, 120, 50, "Check"};
window.add(cb);
// cb.setLabelColor(TDT4102::Color::blue);
cb.setCheckBoxColorHover(TDT4102::Color::red);
// cb.setCheckBoxColorActive(TDT4102::Color::green);
cb.setLabel("This is a");
int x = 10;
int y = 10;
// TDT4102::Image image("res/unknown.jpg");
// image.width = 100;
// image.height = 100;
// // TDT4102::Image image("res/unknown.jpg");
// // image.width = 100;
// // image.height = 100;
std::vector<std::string> colors{"red", "gold", "green", "rebecca_purple", "blue"};
std::vector<Ball> circles;
circles.resize(5000);
for (unsigned int i = 0; i < circles.size(); i++) {
circles.at(i).x = randint(0, window.get_width());
circles.at(i).y = randint(0, window.get_height());
circles.at(i).x = randint(0, window.width());
circles.at(i).y = randint(0, window.height());
circles.at(i).radius = randint(5, 10);
circles.at(i).xSpeed = float(randint(-400, 400)) / 100.0f;
circles.at(i).ySpeed = float(randint(-400, 400)) / 100.0f;
......@@ -108,10 +132,26 @@ int main(int argc, char* argv[]) {
// window.draw_text({mousePosition.x, mousePosition.y + 200}, std::to_string(mousePosition.x) + ", " + std::to_string(mousePosition.y), TDT4102::Color::brown, 70, TDT4102::Font::courier_bold_italic);
// window.draw_arc({500, 500}, mousePosition.x, mousePosition.y, 190, 350, TDT4102::Color::orange);
// window.draw_image(mousePosition, image);
// if (rb.isSelected()) {
// std::cout << "Checked\n";
// } else {
// std::cout << "Not checked\n";
// }
// window.draw_text({mousePosition.x, mousePosition.y + 100}, std::to_string(mousePosition.x) + ", " + std::to_string(mousePosition.y));
// window.draw_text({mousePosition.x, mousePosition.y + 200}, std::to_string(mousePosition.x) + ", " + std::to_string(mousePosition.y), TDT4102::Color::brown, 70, TDT4102::Font::courier_bold_italic);
// window.draw_arc({500, 500}, mousePosition.x, mousePosition.y, 190, 350, TDT4102::Color::orange);
// window.draw_image(mousePosition, image);
// if (rb.isSelected()) {
// std::cout << "Checked\n";
// } else {
// std::cout << "Not checked\n";
// }
bool allAtCenter = true;
for (Ball& ball : circles) {
float gravityX = (float(window.get_width()) / 2.0f) - ball.x;
float gravityY = (float(window.get_height()) / 2.0f) - ball.y;
float gravityX = (float(window.width()) / 2.0f) - ball.x;
float gravityY = (float(window.height()) / 2.0f) - ball.y;
float absoluteSpeed = std::max(std::sqrt(gravityX * gravityX + gravityY * gravityY), 3.0f);
const float gravity = 0.07f;
ball.xSpeed += (gravityX / absoluteSpeed) * gravity;
......@@ -130,10 +170,12 @@ int main(int argc, char* argv[]) {
} else if(ball.y + ball.radius < 0) {
ball.y = window.h() + ball.radius;
}*/
if ((std::abs(ball.x - float(window.get_width()) / 2.0f) > 1.0f) && (std::abs(ball.y - float(window.get_height()) / 2.0f) > 1.0f)) {
if ((std::abs(ball.x - float(window.width()) / 2.0f) > 1.0f) && (std::abs(ball.y - float(window.height()) / 2.0f) > 1.0f)) {
allAtCenter = false;
}
// window.draw_circle({int(ball.x), int(ball.y)}, ball.radius, ball.colour, TDT4102::Color::black);
// window.draw_circle({int(ball.x), int(ball.y)}, ball.radius, ball.colour, TDT4102::Color::black);
// window.draw_circle({int(ball.x), int(ball.y)}, ball.radius, ball.colour, TDT4102::Color::black);
}
if (allAtCenter) {
for (int i = 0; i < circles.size(); i++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment