16.04.2016, 23:49
Code:
Gui <- {};
class Gui.Window
{
constructor(_x, _y, _width, _height, _texture)
{
createId = null;
window = { x = _x, y = _y, width = _width, height = _height, texture = _texture };
};
function show()
{
if(window.texture)
setTextureVisible(createId = createTexture(window.x, window.y, window.width, window.height, window.texture), true);
};
function hide()
{
if(window.texture)
setTextureVisible(createId, false);
};
createId = null;
window = null;
};
class Gui.Button
{
constructor(_text, _x, _y, _width, _height)
{
text = _text;
font = "Font_Old_10_White_Hi.TGA";
color = { r = 255, g = 255, b = 255 };
texture = "DLG_CONVERSATION.TGA";
focusTexture = "MENU_INGAME.TGA";
surface = { x = _x, y = _y, width = _width, height = _height };
showIds = { draw = null, texture = null };
reaction = [];
focus = false;
};
function show()
{
local resolution = getResolution();
local x = surface.x + surface.width / 2 - (8192.0 / resolution.width) * (getTextWidth(font, text) / 2);
local y = surface.y + surface.height / 2 - (8192.0 / resolution.height) * (getFontHeight(font) / 2);
setDrawVisible(showIds.draw = createDraw(text, font, x, y, color.r, color.g, color.b, true), true);
setTextureVisible(showIds.texture = createTexture(surface.x, surface.y, surface.width, surface.height, texture), true);
device.push(this);
};
function hide()
{
destroyDraw(showIds.draw);
destroyTexture(showIds.texture);
device.remove(device.find(this));
};
function connect(_action, _func)
{
reaction.push({ action = _action, func = _func})
};
text = null;
font = null;
color = null;
texture = null;
focusTexture = null;
surface = null;
showIds = null;
reaction = null;
focus = false;
device = [];
};
addEvent("onRender", function()
{
local mousePos = getCursorPosition();
foreach(key in Gui.Button.device) {
if((key.surface.x <= mousePos.x && (key.surface.x + key.surface.width) >= mousePos.x)
&& (key.surface.y <= mousePos.y && (key.surface.y + key.surface.height) >= mousePos.y)) {
if(!key.focus) {
key.focus = true; setTexture(key.showIds.texture, key.focusTexture);
}
} else {
if(key.focus)
setTexture(key.showIds.texture, key.texture); key.focus = false;
}
}
});
addEvent("onClick", function(button, x, y, wheel)
{
foreach(key in Gui.Button.device) {
if((key.surface.x <= x && (key.surface.x + key.surface.width) >= x)
&& (key.surface.y <= y && (key.surface.y + key.surface.height) >= y)) {
foreach(i, funce in key.reaction) {
if(funce.action == button)
funce.func();
}
}
}
});
Kod działa, ale chciałbym go napisać lepiej. Wszelaka konstruktywna krytyka i porady mile widziane. Wyznawców zasady "nie znam się, a wypowiem się", serdecznie proszę o zignorowanie tego tematu.