Skip to content
Snippets Groups Projects
Commit 7c2c4c60 authored by Fodor Ágoston's avatar Fodor Ágoston
Browse files

menü kialakitása, egy nehézség betöltése

parent f9ca900a
No related branches found
No related tags found
No related merge requests found
#include "application.hpp" #include "application.hpp"
#include "graphics.hpp" #include "graphics.hpp"
#include "widget.hpp" #include "widget.hpp"
#include <iostream> #include "menu.hpp"
#include "spinbox.hpp"
using namespace genv; using namespace genv;
using namespace std; using namespace std;
...@@ -12,10 +14,7 @@ Application::Application() ...@@ -12,10 +14,7 @@ Application::Application()
} }
void ellenoriz(vector<Widget> v)
{
}
void Application::registerWidget(Widget* w) void Application::registerWidget(Widget* w)
{ {
...@@ -23,13 +22,81 @@ void Application::registerWidget(Widget* w) ...@@ -23,13 +22,81 @@ void Application::registerWidget(Widget* w)
widgets.push_back(w); widgets.push_back(w);
} }
void Application :: board(int diffi)
{
vector<vector<int>> board;
if(diffi == 1)
{
board = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
}
for (int i = 0; i < 9; ++i)
{
vector<SpinBox*> rowWidgets;
for (int j = 0; j < 9; ++j)
{
SpinBox* spin;
if (board[i][j] != 0)
{
spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, board[i][j], board[i][j], board[i][j], 0,100,255);
}
else
{
spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, 0, 9,0,255,255,255);
}
rowWidgets.push_back(spin);
}
widgets2.push_back(rowWidgets);
}
}
void Application::event_loop() void Application::event_loop()
{ {
Menu m;
int wrongi = -1; int wrongi = -1;
event ev; event ev;
int focus = -1; int focus = -1;
while (m.diffret() == 0 and gin >> ev)
{
m.draw();
m.handle(ev);
gout << refresh;
if(ev.type == ev_key and ev.keycode == key_escape)
{
break;
}
}
board(m.diffret());
while(gin >> ev) while(gin >> ev)
{ {
if (ev.type == ev_mouse && ev.button==btn_left) if (ev.type == ev_mouse && ev.button==btn_left)
{ {
for (size_t i=0; i<widgets.size(); i++) for (size_t i=0; i<widgets.size(); i++)
...@@ -42,11 +109,6 @@ void Application::event_loop() ...@@ -42,11 +109,6 @@ void Application::event_loop()
} }
wrongi = -1; wrongi = -1;
if (focus != -1) { if (focus != -1) {
widgets[focus]->handle(ev); widgets[focus]->handle(ev);
for (int k = 0; k < 81; k += 9) { for (int k = 0; k < 81; k += 9) {
...@@ -70,7 +132,7 @@ void Application::event_loop() ...@@ -70,7 +132,7 @@ void Application::event_loop()
for (int i = k; i < 81; i += 9) { for (int i = k; i < 81; i += 9) {
for (int j = i + 9; j < 81; j += 9) { for (int j = i + 9; j < 81; j += 9) {
if (widgets[i]->getter() != 0 && widgets[i]->getter() == widgets[j]->getter()) { if (widgets[i]->getter() != 0 && widgets[i]->getter() == widgets[j]->getter()) {
wrongi = i; wrongi = j;
break; break;
} }
} }
...@@ -83,6 +145,7 @@ void Application::event_loop() ...@@ -83,6 +145,7 @@ void Application::event_loop()
} }
} }
for (int row_offset = 0; row_offset < 9; row_offset += 3) { for (int row_offset = 0; row_offset < 9; row_offset += 3) {
for (int col_offset = 0; col_offset < 9; col_offset += 3) { for (int col_offset = 0; col_offset < 9; col_offset += 3) {
std::vector<bool> seen(10, false); std::vector<bool> seen(10, false);
...@@ -113,6 +176,7 @@ void Application::event_loop() ...@@ -113,6 +176,7 @@ void Application::event_loop()
} }
} }
for (int i = 0; i < widgets.size(); ++i) { for (int i = 0; i < widgets.size(); ++i) {
if( i == wrongi){ if( i == wrongi){
widgets[i]->draw(); widgets[i]->draw();
...@@ -121,8 +185,16 @@ void Application::event_loop() ...@@ -121,8 +185,16 @@ void Application::event_loop()
widgets[i]->draw2(); widgets[i]->draw2();
} }
} }
if(ev.type == ev_key and ev.keycode == key_escape)
{
break;
}
gout << refresh; gout << refresh;
} }
} }
...@@ -2,11 +2,15 @@ ...@@ -2,11 +2,15 @@
#define APPLICATION_HPP #define APPLICATION_HPP
#include <vector> #include <vector>
#include "vector" #include "vector"
#include <iostream> #include "menu.hpp"
class Widget; class Widget;
class SpinBox;
class Application class Application
{ {
public: public:
...@@ -16,9 +20,16 @@ public: ...@@ -16,9 +20,16 @@ public:
virtual void registerWidget(Widget*); virtual void registerWidget(Widget*);
void ellenoriz(std::vector<Widget> v); void ellenoriz(std::vector<Widget> v);
void board(int );
protected: protected:
std::vector<Widget*> widgets; std::vector<Widget*> widgets;
std::vector<std::vector<SpinBox*>> widgets2;
}; };
#endif #endif
#include "application.hpp" #include "application.hpp"
#include "spinbox.hpp"
#include <vector>
using namespace genv;
using namespace std; using namespace std;
class MyApp : public Application
{
public:
MyApp()
{
}
void palya()
{
vector<vector<int>> board = {
{5, 3, 0, 0, 7, 0, 0, 0, 0},
{6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 9, 8, 0, 0, 0, 0, 6, 0},
{8, 0, 0, 0, 6, 0, 0, 0, 3},
{4, 0, 0, 8, 0, 3, 0, 0, 1},
{7, 0, 0, 0, 2, 0, 0, 0, 6},
{0, 6, 0, 0, 0, 0, 2, 8, 0},
{0, 0, 0, 4, 1, 9, 0, 0, 5},
{0, 0, 0, 0, 8, 0, 0, 7, 9}
};
for (int i = 0; i < 9; ++i)
{
vector<SpinBox*> rowWidgets;
for (int j = 0; j < 9; ++j)
{
SpinBox* spin;
if (board[i][j] != 0)
{
spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, board[i][j], board[i][j], board[i][j]);
}
else
{
spin = new SpinBox(this, 5 + j * 100, 5 + i * 100, 100, 0, 9,0);
}
rowWidgets.push_back(spin);
}
widgets.push_back(rowWidgets);
}
}
protected:
vector<vector<SpinBox*>> widgets;
};
int main() int main()
{ {
MyApp app;
app.palya();
app.event_loop();
Application app;
app.event_loop();
return 0; return 0;
} }
menu.cpp 0 → 100644
#include "menu.hpp"
#include "graphics.hpp"
using namespace genv;
Menu::Menu() : diffnum(0) {}
void Menu::draw() {
gout.load_font("LiberationSans-Regular.ttf", 30);
gout << move_to(20, 20) << color(255, 255, 255) << text("Sudoku, please select a difficulty");
gout << move_to(50, 50) << color(255, 255, 255) << box(200, 50);
gout << move_to(120, 80) << color(0, 0, 0) << text("Easy (e)");
gout << move_to(50, 120) << color(255, 255, 255) << box(200, 50);
gout << move_to(110, 150) << color(0, 0, 0) << text("Medium (m)");
gout << move_to(50, 190) << color(255, 255, 255) << box(200, 50);
gout << move_to(120, 220) << color(0, 0, 0) << text("Hard (h)");
gout << refresh;
}
void Menu::handle(genv::event ev) {
if (ev.type == ev_key)
{
if (ev.keycode == 'e')
{
diffnum = 1;
}
else if (ev.keycode == 'm')
{
diffnum = 2;
}
else if (ev.keycode == 'h')
{
diffnum = 3;
}
}
}
int Menu::diffret() {
return diffnum;
}
menu.hpp 0 → 100644
#ifndef MENU_HPP
#define MENU_HPP
#include "graphics.hpp"
class Menu
{
protected:
int diffnum;
public:
Menu();
void draw();
void handle(genv::event ev);
int diffret();
};
#endif // MENU_HPP
...@@ -2,20 +2,17 @@ ...@@ -2,20 +2,17 @@
#include "graphics.hpp" #include "graphics.hpp"
#include "widget.hpp" #include "widget.hpp"
using namespace genv; using namespace genv;
using namespace std; using namespace std;
int mouse_x = 0; SpinBox::SpinBox(Application* parent,int x, int y, int sizey, int min, int max, int start, int r,int g, int b) : Widget(parent,x,y,sizey*3,sizey), _min(min), _max(max), _num(start),_r(r),
int mouse_y = 0; _g(g), _b(b), _isselected(0)
{}
SpinBox::SpinBox(Application* parent,int x, int y, int sizey, int min, int max, int start) : Widget(parent,x,y,sizey*3,sizey), _min(min), _max(max), _num(start), _isselected(0)
{
}
void SpinBox::draw() void SpinBox::draw()
{ {
gout.load_font("LiberationSans-Regular.ttf", 40);
_sizex = _sizey; _sizex = _sizey;
gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey); gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey);
...@@ -29,12 +26,19 @@ void SpinBox::draw() ...@@ -29,12 +26,19 @@ void SpinBox::draw()
int text_height = gout.cascent() + gout.cdescent(); int text_height = gout.cascent() + gout.cdescent();
if(_num != 0)
{
gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent())
<< color(255, 0, 0) << color(255, 0, 0)
<< text(to_string(_num)); << text(to_string(_num));
} }
}
void SpinBox::draw2() void SpinBox::draw2()
{ {
gout.load_font("LiberationSans-Regular.ttf", 40);
_sizex = _sizey; _sizex = _sizey;
gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey); gout << move_to (_x,_y) << color(50,50,50) << box(_sizex,_sizey);
...@@ -48,10 +52,13 @@ void SpinBox::draw2() ...@@ -48,10 +52,13 @@ void SpinBox::draw2()
int text_height = gout.cascent() + gout.cdescent(); int text_height = gout.cascent() + gout.cdescent();
if(_num != 0)
{
gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent()) gout << move_to(_x + (_sizex - text_width) / 2, _y + (_sizey - text_height) / 2 + gout.cascent())
<< color(0, 255, 0) << color(_r, _g, _b)
<< text(to_string(_num)); << text(to_string(_num));
} }
}
bool SpinBox::isover(int ms_x, int ms_y) bool SpinBox::isover(int ms_x, int ms_y)
{ {
......
...@@ -6,11 +6,11 @@ ...@@ -6,11 +6,11 @@
class SpinBox : public Widget class SpinBox : public Widget
{ {
protected: protected:
int _min, _max, _num; int _min, _max, _num, _r, _g, _b;
bool _isselected; bool _isselected;
public: public:
SpinBox(Application* ,int, int, int,int, int,int); SpinBox(Application* ,int, int, int,int, int,int, int, int, int);
void draw() override; void draw() override;
void handle(genv::event) override; void handle(genv::event) override;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment