- Codi de UI mes clar. Variables van amunt o avall segons botó del ratolí

This commit is contained in:
2023-06-06 14:03:03 +02:00
parent 6e166c5e77
commit 845772ed16
3 changed files with 28 additions and 42 deletions

View File

@@ -5,11 +5,11 @@
namespace ui namespace ui
{ {
const bool button(const char *label, const int x, const int y, const int w, const int h) const int button(const char *label, const int x, const int y, const int w, const int h)
{ {
const int mx = input::mouseX(); const int mx = input::mouseX();
const int my = input::mouseY(); const int my = input::mouseY();
const bool btnDown = input::mouseBtn(1); const bool btnDown = input::mouseBtn(1) || input::mouseBtn(3);
const bool inside = (mx>=x) && (mx<x+w) && (my>=y) && (my<y+h); const bool inside = (mx>=x) && (mx<x+w) && (my>=y) && (my<y+h);
const int txt_size = strlen(label)*4; const int txt_size = strlen(label)*4;
const int txt_x = x+(w-txt_size)/2; const int txt_x = x+(w-txt_size)/2;
@@ -18,6 +18,11 @@ namespace ui
draw::fillrect(x, y, w, h); draw::fillrect(x, y, w, h);
draw::print(label, 1+txt_x, y+3, 15, 8); draw::print(label, 1+txt_x, y+3, 15, 8);
return inside && input::mouseClk(1); if (inside)
{
if (input::mouseClk(1)) return 1;
if (input::mouseClk(3)) return 3;
}
return 0;
} }
} }

View File

@@ -2,5 +2,5 @@
namespace ui namespace ui
{ {
const bool button(const char *label, const int x, const int y, const int w, const int h); const int button(const char *label, const int x, const int y, const int w, const int h);
} }

View File

@@ -71,6 +71,19 @@ int sx=1, sy=0;
if (sign) draw::draw(x,y,5,7,50,120); if (sign) draw::draw(x,y,5,7,50,120);
} }
void btn(const char* label, const int x, const int y, int &var, int min, int max)
{
char buffer[100];
int result=0;
draw::print("ROOM WIDTH:", x, y+3, 15, 0);
result=ui::button(SDL_itoa(var, buffer, 10), x+50, y, 28, 11);
if (result)
{
var=SDL_max(min, SDL_min(max, var-(result-2)));
restart();
}
}
bool game::loop() bool game::loop()
{ {
actor::update(actor::getFirst()); actor::update(actor::getFirst());
@@ -88,45 +101,13 @@ bool game::loop()
print(0,40,input::mouseBtn(2)?1:0); print(0,40,input::mouseBtn(2)?1:0);
print(0,50,input::mouseBtn(3)?1:0); print(0,50,input::mouseBtn(3)?1:0);
char buffer[100]; btn("ROOM WIDTH:", 330, 10, room_w, 0, 3);
draw::print("ROOM WIDTH:", 330, 13, 15, 0); btn("ROOM HEIGHT:", 330, 25, room_h, 0, 3);
if (ui::button(SDL_itoa(room_w, buffer, 10), 380, 10, 28, 11))
{
room_w++;if(room_w>3)room_w=0;
restart();
}
draw::print("ROOM HEIGHT:", 330, 28, 15, 0); btn("DOOR XP:", 330, 50, room_xp, -1, 5);
if (ui::button(SDL_itoa(room_h, buffer, 10), 380, 25, 28, 11)) btn("DOOR XN:", 330, 65, room_xn, -1, 5);
{ btn("DOOR YP:", 330, 80, room_yp, -1, 5);
room_h++;if(room_h>3)room_h=0; btn("DOOR YN:", 330, 95, room_yn, -1, 5);
restart();
}
draw::print("DOOR XP:", 330, 53, 15, 0);
if (ui::button(SDL_itoa(room_xp, buffer, 10), 380, 50, 28, 11))
{
room_xp++;if(room_xp>5)room_xp=-1;
restart();
}
draw::print("DOOR XN:", 330, 68, 15, 0);
if (ui::button(SDL_itoa(room_xn, buffer, 10), 380, 65, 28, 11))
{
room_xn++;if(room_xn>5)room_xn=-1;
restart();
}
draw::print("DOOR YP:", 330, 83, 15, 0);
if (ui::button(SDL_itoa(room_yp, buffer, 10), 380, 80, 28, 11))
{
room_yp++;if(room_yp>5)room_yp=-1;
restart();
}
draw::print("DOOR YN:", 330, 98, 15, 0);
if (ui::button(SDL_itoa(room_yn, buffer, 10), 380, 95, 28, 11))
{
room_yn++;if(room_yn>5)room_yn=-1;
restart();
}
draw::render(); draw::render();