From 558e553612a6d733a433368102fef19e72071a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Horv=C3=A1th=20Kata?= <horka23@cortex.itk.ppke.hu> Date: Sun, 18 May 2025 21:54:42 +0200 Subject: [PATCH] feliratok hozzaadva --- main.cpp | 95 +++++++++++++++++++++++++++++++------------------- statictext.cpp | 50 ++++++++++++++++++++++++++ statictext.hpp | 19 ++++++++++ widgets.cpp | 2 +- 4 files changed, 129 insertions(+), 37 deletions(-) create mode 100644 statictext.cpp create mode 100644 statictext.hpp diff --git a/main.cpp b/main.cpp index be4093b..78b19d6 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "cell.hpp" #include "button.hpp" #include "textbox.hpp" +#include "statictext.hpp" #include <vector> #include <iostream> @@ -21,6 +22,7 @@ private: Textbox* name2_input; Button* start_button; std::vector<Widget*> menu_widgets; + StaticText* jatekszabaly, *nev1, *nev2; std::vector<std::vector<Cell*>> _cells; @@ -28,6 +30,7 @@ private: GameLogic _game; bool game_started; Button* restart_button; + StaticText* win; @@ -35,18 +38,34 @@ private: void createMenu() { name1_input = new Textbox(this, 190, 300, 200, 40); menu_widgets.push_back(name1_input); - registerWidget(name1_input); + name2_input = new Textbox(this, 410, 300, 200, 40); menu_widgets.push_back(name2_input); - registerWidget(name2_input); + start_button = new Button(this, 350, 360, 100, 40, "START"); menu_widgets.push_back(start_button); - registerWidget(start_button); - // gout << move_to(270, 280) << text("J�t�kos 1"); - // gout << move_to(490, 280) << text("J�t�kos 2"); + + + jatekszabaly= new StaticText(this, 250, 50, 300, 200, "Játékszabály:\n A potyogtatós amőba egy logikai játék, amelyet egy rácsos táblán játszanak, ahol a játékosok felváltva ejtenek le korongokat egy oszlopba. A cél négy azonos színű korongot egymás mellé helyezni vízszintesen, függőlegesen vagy átlósan. Az első játékos, akinek ez sikerül, megnyeri a játékot. A kezdéshez kérlek adjátok meg a neveteket:)"); + menu_widgets.push_back(jatekszabaly); + + + nev1= new StaticText(this, 250, 250, 100, 50, "1. játékos"); + menu_widgets.push_back(nev1); + + nev2= new StaticText(this, 450, 250, 100, 50, "2. játékos"); + menu_widgets.push_back(nev2); + + for (Widget* w : menu_widgets) { + registerWidget(w); + } + + + + } @@ -58,7 +77,12 @@ private: current_mode=GAME; start_button->setSelected(false); game_started=true; + + } + + + else{ start_button->setSelected(false); } @@ -127,6 +151,11 @@ private: _cells.push_back(rowcells); } + + win = new StaticText(this, 300, 700, 200, 40, ""); + game_widgets.push_back(win); + + } @@ -158,15 +187,11 @@ private: void checkButtons(){ - /*if(start_button && start_button->isSelected()){ - game_started=true; - start_button->setSelected(false); - }*/ - if(restart_button && restart_button->isSelected()){ _game.reset(); game_started=true; + if (win) win->set_text(""); //generateCells(); updateBoard(); restart_button->setSelected(false); @@ -189,23 +214,18 @@ private: } - void showGameStatus(){ - if(_game.getWinner()==GameLogic::EMPTY){ - std::cout << "dontetlen :("; - } - else{ - //std::cout << _game.getWinner(); - if (_game.getWinner()==1){ - std::cout << player1_name << " nyert :)"; - } - else if (_game.getWinner()==2){ - std::cout << player2_name << "nyert :)"; - } - - - + void showGameStatus(){ + if (_game.getWinner() == GameLogic::EMPTY) { + win->set_text("Döntetlen!"); + } else { + if (_game.getWinner() == 1) { + win->set_text(player1_name + " nyert!"); + } else if (_game.getWinner() == 2) { + win->set_text(player2_name + " nyert!"); } } +} + public: @@ -257,7 +277,7 @@ public: checkButtons(); } - // checkButtons(); + } else if(ev.type==ev_key){ if(_focused_widget){ @@ -271,19 +291,22 @@ public: } - void drawAllWidgets() { - if(current_mode==MENU){ - for(Widget* widget:menu_widgets){ - widget->draw(); - } + void drawAllWidgets(){ + gout << move_to(0,0) << color(0,0,0) << box(_width, _height); + if(current_mode == MENU){ + + for(Widget* widget : menu_widgets){ + widget->draw(); } - else if(current_mode==GAME){ - for(Widget* widget : game_widgets){ - widget->draw(); - } + } + else if(current_mode == GAME){ + + for(Widget* widget : game_widgets){ + widget->draw(); } - gout << genv::refresh; } + gout << genv::refresh; +} }; diff --git a/statictext.cpp b/statictext.cpp new file mode 100644 index 0000000..0eb5d16 --- /dev/null +++ b/statictext.cpp @@ -0,0 +1,50 @@ +#include "statictext.hpp" +#include "graphics.hpp" +#include<sstream> +using namespace genv; + +StaticText::StaticText(App* parent, int x, int y, int sx, int sy, const std::string& text) + : Widget(parent, x, y, sx, sy), _text(text) {} + +void StaticText::draw() { + gout << move_to(_x, _y) + << color(0,0,0) + << box(_size_x, _size_y); + + gout << color(255, 255, 255); + + int line_height = gout.cascent() + gout.cdescent(); + int max_lines = _size_y / line_height; + int line = 0; + std::string word; + std::string line_text; + std::stringstream iss(_text); + + while (iss >> word) { + std::string test_line = line_text.empty() ? word : line_text + " " + word; + if (gout.twidth(test_line) > _size_x - 10) { + // ki�rjuk az aktu�lis sort + gout << move_to(_x + 5, _y + line * line_height + gout.cascent()) + << text(line_text); + line_text = word; + line++; + if (line >= max_lines) break; + } else { + line_text = test_line; + } + } + + // utols� sor (ha m�g van hely) + if (line < max_lines && !line_text.empty()) { + gout << move_to(_x + 5, _y + line * line_height + gout.cascent()) + << text(line_text); + } +} + +void StaticText::set_text(const std::string& text) { + _text = text; +} + +std::string StaticText::get_text() const { + return _text; +} diff --git a/statictext.hpp b/statictext.hpp new file mode 100644 index 0000000..3a21832 --- /dev/null +++ b/statictext.hpp @@ -0,0 +1,19 @@ +#ifndef STATICTEXT_HPP_INCLUDED +#define STATICTEXT_HPP_INCLUDED + +//#include "graphics.hpp" +#include "widgets.hpp" +#include <string> + +class StaticText : public Widget{ +protected: + std::string _text; +public: + StaticText(App* parent, int x, int y, int sx, int sy, const std::string& text); + void draw() override; + void set_text(const std::string& text); + std::string get_text() const; +}; + + +#endif // STATICTEXT_HPP_INCLUDED diff --git a/widgets.cpp b/widgets.cpp index 38ff8c3..af3ac04 100644 --- a/widgets.cpp +++ b/widgets.cpp @@ -13,7 +13,7 @@ bool Widget::is_selected(const int mouse_x, const int mouse_y) { mouse_y > _y && mouse_y < _y + _size_y; } -//Widget::~Widget()= default; + -- GitLab