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

Merge branch 'new-widget-slider' into 'main'

New widget slider

See merge request !11
parents 5c5d92de 9feebf6f
No related branches found
No related tags found
1 merge request!11New widget slider
#pragma once
#include "Widget.h"
#include "Point.h"
#include "Color.h"
namespace TDT4102 {
class Slider : public TDT4102::Widget {
private:
int value;
int minValue;
int maxValue;
int stepValue;
nk_color sliderBarColor = nk_rgba(100, 100, 100, 255);
nk_color sliderBarColorActive = nk_rgba(100, 100, 100, 255);
nk_color sliderBarColorHover = nk_rgba(100, 100, 100, 255);
nk_color sliderBarColorFilled = nk_rgba(100, 100, 100, 255);
nk_color sliderCursorColor = nk_rgba(180, 180, 180, 255);
nk_color sliderCursorColorActive = nk_rgba(180, 180, 180, 255);
nk_color sliderCursorColorHover = nk_rgba(180, 180, 180, 255);
protected:
void update(nk_context* context) override;
public:
explicit Slider(TDT4102::Point location, unsigned int width, unsigned int height, int min = 0, int max = 100, int initialValue = 0, int step = 1);
int getValue() const;
void setSliderBarColor(Color newColor) {sliderBarColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};};
void setSliderBarColorActive(Color newColor) {sliderBarColorActive = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setSliderBarColorHover(Color newColor) {sliderBarColorHover = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setSliderBarColorFilled(Color newColor) {sliderBarColorFilled = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setSliderCursorColor(Color newColor) {sliderCursorColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setSliderCursorColorHover(Color newColor) {sliderCursorColorHover = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
void setSliderCurserColorActive(Color newColor) {sliderCursorColorActive = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};}
};
}
\ No newline at end of file
...@@ -19,6 +19,7 @@ build_files = [ ...@@ -19,6 +19,7 @@ build_files = [
'src/widgets/TextInput.cpp', 'src/widgets/TextInput.cpp',
'src/widgets/TextBox.cpp', 'src/widgets/TextBox.cpp',
'src/widgets/DropdownList.cpp', 'src/widgets/DropdownList.cpp',
'src/widgets/Slider.cpp',
'src/AnimationWindow.cpp', 'src/AnimationWindow.cpp',
'src/Color.cpp', 'src/Color.cpp',
'src/Image.cpp', 'src/Image.cpp',
......
#include "widgets/Slider.h"
TDT4102::Slider::Slider(TDT4102::Point location, unsigned int width, unsigned int height, int min, int max, int initialValue, int step)
: TDT4102::Widget(location, width, height) {
minValue = min;
maxValue = max;
value = initialValue;
stepValue = step;
}
int TDT4102::Slider::getValue() const {
return value;
}
void TDT4102::Slider::update(nk_context *context) {
struct nk_style* s = &context->style;
nk_style_push_color(context, &s->slider.bar_normal, sliderBarColor);
nk_style_push_color(context, &s->slider.bar_hover, sliderBarColorHover);
nk_style_push_color(context, &s->slider.bar_active, sliderBarColorActive);
nk_style_push_color(context, &s->slider.bar_filled, sliderBarColorFilled);
nk_style_push_style_item(context, &s->slider.cursor_normal, nk_style_item_color(sliderCursorColor));
nk_style_push_style_item(context, &s->slider.cursor_hover, nk_style_item_color(sliderCursorColorHover));
nk_style_push_style_item(context, &s->slider.cursor_active, nk_style_item_color(sliderCursorColorActive));
if (nk_slider_int(context, minValue, &value, maxValue, stepValue)) {
fire();
}
nk_style_pop_color(context);
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);
}
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "widgets/TextInput.h" #include "widgets/TextInput.h"
#include "widgets/RadioButton.h" #include "widgets/RadioButton.h"
#include "widgets/TextBox.h" #include "widgets/TextBox.h"
#include "widgets/Slider.h"
#include "widgets/RadioButton.h" #include "widgets/RadioButton.h"
#include "widgets/CheckBox.h" #include "widgets/CheckBox.h"
...@@ -50,6 +51,14 @@ void textChanged() { ...@@ -50,6 +51,14 @@ void textChanged() {
std::cout << "The input text is now: " << input.getText() << std::endl; std::cout << "The input text is now: " << input.getText() << std::endl;
} }
TDT4102::Slider rv{{200, 300}, 300, 80};
TDT4102::Slider gv{{200, 420}, 300, 80, 0, 255, 7};
TDT4102::Slider bv{{200, 540}, 300, 80, 0, 255, 240};
// void sliderValue() {
// std::cout << "Value is: " << s.getValue() << std::endl;
// }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
TDT4102::AnimationWindow window(100, 100, 1000, 800, "Test"); TDT4102::AnimationWindow window(100, 100, 1000, 800, "Test");
// window.keep_previous_frame(true); // window.keep_previous_frame(true);
...@@ -73,6 +82,20 @@ int main(int argc, char* argv[]) { ...@@ -73,6 +82,20 @@ int main(int argc, char* argv[]) {
input.setCallback(&textChanged); input.setCallback(&textChanged);
window.add(input); window.add(input);
rv.setCallback([&window] {window.setBackgroundColor(TDT4102::Color(rv.getValue(), gv.getValue(), bv.getValue()));});
gv.setCallback([&window] {window.setBackgroundColor(TDT4102::Color(rv.getValue(), gv.getValue(), bv.getValue()));});
bv.setCallback([&window] {window.setBackgroundColor(TDT4102::Color(rv.getValue(), gv.getValue(), bv.getValue()));});
// s.setSliderCursorColor(TDT4102::Color::lime_green);
// s.setSliderCursorColorHover(TDT4102::Color::lime_green);
// s.setSliderBarColorFilled(TDT4102::Color::pink);
// s.setCallback(&sliderValue);
window.add(rv);
window.add(bv);
window.add(gv);
TDT4102::RadioButton rb{{200, 200}, 100, 30, "Easy"}; TDT4102::RadioButton rb{{200, 200}, 100, 30, "Easy"};
window.add(rb); window.add(rb);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment