gpt4 book ai didi

java - 深度系统 - 先前的 'top' 项目在更改时闪烁

转载 作者:行者123 更新时间:2023-11-30 01:54:09 25 4
gpt4 key购买 nike

我正在尝试在处理中建立某种深度系统。目标是使其功能类似于 (Windows) 窗口。

我有一个名为“Window”的类,它可以接受一些参数,并且它将成功绘制一个可以拖动的窗口。

深度系统按目前的方式工作。我无法单击当前窗口“下方”的窗口,如果单击另一个窗口,窗口的顺序会正确切换。

问题是,每当我在窗口之间切换时,先前选择的窗口就会闪烁(未绘制)一帧,然后再次出现。

我根本不明白为什么会发生这种情况。这是我的代码,如果您需要任何进一步的信息,请告诉我。

Windows.pde:

Window[] wins;
int win_count = 0;
boolean win_drag = false;
int win_selected = 2;

void setup()
{
size(800, 600);
wins = new Window[3];
wins[0] = new Window("Test", 20, 20, 300, 200);
wins[1] = new Window("Test 2", 20, 260, 350, 225);
wins[2] = new Window("Test 3", 400, 20, 250, 150);
}

void draw()
{
background(10);

for (int i = 0; i < wins.length; i ++)
{
wins[i].draw_window();
}
}


void bringToTop(Window winID)
{
Window[] new_wins;
new_wins = new Window[wins.length];

int win_pos = -1;

for (int i = 0; i < wins.length; i ++)
{
if (wins[i] == winID)
{
win_pos = i;
break;
}
}

arrayCopy(wins, 0, new_wins, 0, win_pos);
arrayCopy(wins, win_pos + 1, new_wins, win_pos, wins.length - win_pos - 1);
new_wins[wins.length - 1] = winID;

arrayCopy(new_wins, wins);
}


boolean isOnTop(Window winID)
{

int win_pos = -1;

for (int i = 0; i < wins.length; i ++)
{
if (wins[i] == winID)
{
win_pos = i;
break;
}
}

Window[] top_wins;
top_wins = new Window[wins.length];
int winTopCount = 0;

for (int i = 0; i < wins.length; i ++)
{
if (mouse_in_rect(wins[i].winX, wins[i].winY, wins[i].winW, wins[i].winH + 24))
{
top_wins[winTopCount] = wins[i];
winTopCount ++;
}
}

int last_real_win = -1;

for (int i = 0; i < top_wins.length; i ++)
{

if (top_wins[i] != null)
{
last_real_win = i;
}
}

return (wins[win_pos] == top_wins[last_real_win]);
}

WindowObj.pde:

class Window
{
String winT;
int winX;
int winY;
int winW;
int winH;

boolean dragging;
int winXOff;
int winYOff;
int winTH;
int my_id;

Window(String ttl, int WX, int WY, int WW, int WH)
{
winT = ttl;
winX = WX;
winY = WY;
winW = WW;
winH = WH;

dragging = false;
winXOff = 0;
winYOff = 0;
winTH = 24;
my_id = win_count ++;
}

void draw_window()
{
if (win_selected == my_id)
{
fill(60);
}
else
{
fill(40);
}
rect(winX, winY, winW, winTH);

fill(25);
rect(winX, winY + 24, winW, winH);


if (dragging == true)
{
winX = mouseX + winXOff;
winY = mouseY + winYOff;


if (winX < 0)
{
winX = 0;
}

if (winX > width - winW - 1)
{
winX = width - winW - 1;
}


if (winY < 0)
{
winY = 0;
}

if (winY > height - winH - winTH - 1)
{
winY = height - winH - winTH - 1;
}

}


Window win_pos = wins[0];

for (int i = 0; i < wins.length; i ++)
{
if (wins[i].my_id == my_id)
{
win_pos = wins[i];
}
}

if (mouse_in_rect(winX, winY, winW, 24) && mousePressed && mouseButton == LEFT && dragging == false && isOnTop(win_pos) && win_drag == false)
{
dragging = true;
winXOff = winX - mouseX;
winYOff = winY - mouseY;
win_drag = true;
win_selected = my_id;
bringToTop(win_pos);
}


if (mouse_in_rect(winX, winY + 24, winW, winH) && mousePressed && mouseButton == LEFT && dragging == false && isOnTop(win_pos) && win_drag == false)
{
win_selected = my_id;
bringToTop(win_pos);
}


if (dragging == true)
{
if (mouseButton != LEFT)
{
win_drag = false;
dragging = false;
winXOff = 0;
winYOff = 0;
}
}

}



}

mouseFunctions.pde:

boolean mouse_in_rect(int mX, int mY, int mW, int mH)
{
int but_x = mX;
int but_y = mY;
int but_w = mW;
int but_h = mH;

if (mouseX > but_x && mouseY > but_y && mouseX < but_x + but_w && mouseY < but_y + but_h)
{
return true;
}
else
{
return false;
}
}

最佳答案

导致此问题的原因是您在单个循环中计算了窗口顺序和绘图。
如果某个窗口的位置发生了变化,则可以省略某个窗口的绘制,而对另一个窗口进行两次绘制。请注意,数组 wins 中窗口的索引已更改。

将窗口的绘制和更新分为两种单独的方法:

class Window
{
// ...

void draw_window()
{
if (win_selected == my_id)
{
fill(60);
}
else
{
fill(40);
}
rect(winX, winY, winW, winTH);

fill(25);
rect(winX, winY + 24, winW, winH);
}

void update_window()
{
if (dragging == true)
{
// ...
}

// ...
}

首先更新窗口的顺序并计算其新位置。之后在一个单独的循环中绘制所有窗口:

void draw()
{
background(10);

for (int i = 0; i < wins.length; i ++) {
wins[i].update_window();
}

for (int i = 0; i < wins.length; i ++) {
wins[i].draw_window();
}
}

关于java - 深度系统 - 先前的 'top' 项目在更改时闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55076672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com