diff --git a/dependencies/subprojects/animationwindow/include/widgets/TextBox.h b/dependencies/subprojects/animationwindow/include/widgets/TextBox.h new file mode 100644 index 0000000000000000000000000000000000000000..4d6d9a21d23c716591559e966f50c02b8a4e9654 --- /dev/null +++ b/dependencies/subprojects/animationwindow/include/widgets/TextBox.h @@ -0,0 +1,28 @@ +#pragma once + +#include "Widget.h" +#include "Point.h" +#include "Color.h" +#include <string> + +namespace TDT4102 { + namespace internal { + const static unsigned int TEXT_BOX_CHARACTER_LIMIT = 1000; + } + + class TextBox : public TDT4102::Widget { + private: + std::string text; + 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); + protected: + void update(nk_context* context) override; + public: + explicit TextBox(TDT4102::Point location, unsigned int width, unsigned int height, std::string initialText = ""); + 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};} + void setBorderColor(TDT4102::Color newColor) {borderColor = nk_color{(nk_byte)newColor.redChannel, (nk_byte)newColor.greenChannel, (nk_byte)newColor.blueChannel, (nk_byte)newColor.alphaChannel};} + }; +} \ No newline at end of file diff --git a/dependencies/subprojects/animationwindow/meson.build b/dependencies/subprojects/animationwindow/meson.build index cb568b7c8dd3eb8b69bdd8f423af0256337ee13d..ae86abd0a08710f8fb365b15ef14637ce79bd6a2 100644 --- a/dependencies/subprojects/animationwindow/meson.build +++ b/dependencies/subprojects/animationwindow/meson.build @@ -15,6 +15,7 @@ build_files = [ 'src/internal/nuklear_implementation.cpp', 'src/widgets/Button.cpp', 'src/widgets/TextInput.cpp', + 'src/widgets/TextBox.cpp', 'src/widgets/DropdownList.cpp', 'src/AnimationWindow.cpp', 'src/Color.cpp', diff --git a/dependencies/subprojects/animationwindow/src/widgets/TextBox.cpp b/dependencies/subprojects/animationwindow/src/widgets/TextBox.cpp new file mode 100644 index 0000000000000000000000000000000000000000..3f4d3c2d1b877e4431304dd1bfed65de727ec8c6 --- /dev/null +++ b/dependencies/subprojects/animationwindow/src/widgets/TextBox.cpp @@ -0,0 +1,25 @@ +#include "widgets/TextBox.h" + +void TDT4102::TextBox::update(nk_context *context) { + struct nk_style* s = &context->style; + 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_style_pop_color(context); + nk_style_pop_color(context); + nk_style_pop_style_item(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); + +} + +void TDT4102::TextBox::setText(std::string updatedText) { + text = updatedText; + text.resize(internal::TEXT_BOX_CHARACTER_LIMIT); +} + diff --git a/dependencies/testproject_featuredemo.cpp b/dependencies/testproject_featuredemo.cpp index 8cba7a85152b697f6c7db6d37d836cb9be867917..183eb88bbd200ecd0e8b799a9745b3399e46a91b 100644 --- a/dependencies/testproject_featuredemo.cpp +++ b/dependencies/testproject_featuredemo.cpp @@ -10,6 +10,7 @@ #include "widgets/Button.h" #include "widgets/TextInput.h" +#include "widgets/TextBox.h" struct Ball { float x = 0; @@ -61,15 +62,18 @@ int main(int argc, char* argv[]) { alert_btn.setCallback([&window]() { window.show_info_dialog("DONT CLICK ME!"); }); window.add(alert_btn); + TDT4102::TextBox b{{300, 200}, 108, 35, "TEXTBOX!"}; + window.add(b); + input.setCallback(&textChanged); window.add(input); 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"}; @@ -97,13 +101,13 @@ int main(int argc, char* argv[]) { if (window.is_key_down(KeyboardKey::S)) { y++; } - window.draw_rectangle({x, y}, 15, 15); + // window.draw_rectangle({x, y}, 15, 15); TDT4102::Point mousePosition = window.get_mouse_coordinates(); - 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); + // 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); bool allAtCenter = true; for (Ball& ball : circles) { float gravityX = (float(window.get_width()) / 2.0f) - ball.x; @@ -129,7 +133,7 @@ int main(int argc, char* argv[]) { 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)) { 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); } if (allAtCenter) { for (int i = 0; i < circles.size(); i++) {