rework on graphics.cpp, implement some GFX with lua

This commit is contained in:
Eduardo Bart 2011-08-14 14:45:25 -03:00
parent 2abe962aa9
commit afc197f2dc
28 changed files with 250 additions and 128 deletions

View File

@ -14,6 +14,7 @@ Module
require 'util' require 'util'
require 'widget' require 'widget'
require 'messagebox' require 'messagebox'
require 'dispatcher'
rootWidget = getRootWidget() rootWidget = getRootWidget()
return true return true

View File

@ -0,0 +1,28 @@
local eventId = 1
local events = { }
local orig = { scheduleEvent = scheduleEvent,
addEvent = addEvent }
-- fix original scheduleEvent
function scheduleEvent(func, delay)
eventId = eventId + 1
local id = eventId + 1
local function proxyFunc()
func()
table[id] = nil
end
table[id] = proxyFunc
orig.scheduleEvent(proxyFunc, delay)
end
-- fix original addEvent
function addEvent(func)
eventId = eventId + 1
local id = eventId + 1
local function proxyFunc()
func()
table[id] = nil
end
table[id] = proxyFunc
orig.addEvent(proxyFunc)
end

View File

@ -1,6 +1,6 @@
Button < UIButton Button < UIButton
font: helvetica-11px-bold font: helvetica-11px-bold
color: 240 173 77 255 font-color: 240 173 77
size: 106 24 size: 106 24
border-image: border-image:
source: /core_ui/images/button.png source: /core_ui/images/button.png

View File

@ -5,7 +5,8 @@ FlatPanel < Panel
source: /core_ui/images/panel_flat.png source: /core_ui/images/panel_flat.png
border: 4 border: 4
RoundedGridPanel < Panel RoundedPanel < Panel
color: 255 255 255 192
border-image: border-image:
source: /core_ui/images/panel_rounded.png source: /core_ui/images/panel_rounded.png
border: 4 border: 4

View File

@ -1,5 +1,5 @@
Window < UIWindow Window < UIWindow
size: 200 100 size: 200 200
head: head:
height: 20 height: 20
border-image: border-image:

26
modules/gfx/gfx.lua Normal file
View File

@ -0,0 +1,26 @@
GFX = { }
function GFX.fadeIn(widget, time, elapsed)
if not elapsed then elapsed = 0 end
if not time then time = 750 end
widget.opacity = math.min((255*elapsed)/time, 255)
if elapsed < time then
scheduleEvent(function()
GFX.fadeIn(widget, time, elapsed + 30)
end, 30)
end
end
function GFX.fadeOut(widget, time, elapsed)
if not elapsed then elapsed = 0 end
if not time then time = 750 end
widget.opacity = (255*(time - elapsed))/time
if elapsed < time then
scheduleEvent(function()
GFX.fadeOut(widget, time, elapsed + 30)
end, 30)
else
widget:destroy()
end
end

15
modules/gfx/gfx.otmod Normal file
View File

@ -0,0 +1,15 @@
Module
name: gfx
description: Contains utilities for generating graphics effects
author: OTClient team
website: https://github.com/edubart/otclient
version: 0.2
autoLoad: true
dependencies:
- core
onLoad: |
require 'gfx'
return true

View File

@ -50,4 +50,4 @@ MainWindow
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
margin.bottom: 16 margin.bottom: 16
margin.right: 16 margin.right: 16
onClick: function(self) self.parent:destroy() end onClick: function(self) GFX.fadeOut(self.parent) end

View File

@ -10,7 +10,7 @@ Panel
smooth: true smooth: true
anchors.fill: parent anchors.fill: parent
RoundedGridPanel RoundedPanel
id: mainMenu id: mainMenu
size: 144 162 size: 144 162
anchors.left: parent.left anchors.left: parent.left
@ -23,7 +23,10 @@ Panel
anchors.top: parent.top anchors.top: parent.top
anchors.horizontalCenter: parent.horizontalCenter anchors.horizontalCenter: parent.horizontalCenter
margin.top: 18 margin.top: 18
onClick: rootWidget:addChild(loadUI("/mainmenu/ui/entergamewindow.otui")) onClick: |
local enterGameWindow = loadUI("/mainmenu/ui/entergamewindow.otui")
rootWidget:addChild(enterGameWindow)
GFX.fadeIn(enterGameWindow)
MenuButton MenuButton
text: Options text: Options

View File

@ -28,8 +28,6 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
AnimatedTexture::~AnimatedTexture() AnimatedTexture::~AnimatedTexture()
{ {
g_graphics.disableDrawing();
glDeleteTextures(m_numFrames, m_framesTextureId); glDeleteTextures(m_numFrames, m_framesTextureId);
delete[] m_framesTextureId; delete[] m_framesTextureId;
delete[] m_framesDelay; delete[] m_framesDelay;
@ -38,8 +36,6 @@ AnimatedTexture::~AnimatedTexture()
void AnimatedTexture::enableBilinearFilter() void AnimatedTexture::enableBilinearFilter()
{ {
g_graphics.disableDrawing();
for(int i=0;i<m_numFrames;++i) { for(int i=0;i<m_numFrames;++i) {
glBindTexture(GL_TEXTURE_2D, m_framesTextureId[i]); glBindTexture(GL_TEXTURE_2D, m_framesTextureId[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

View File

@ -116,6 +116,9 @@ void BorderImage::draw(const Rect& screenCoords)
Rect rectCoords; Rect rectCoords;
Size centerSize = screenCoords.size() - m_bordersSize; Size centerSize = screenCoords.size() - m_bordersSize;
g_graphics.bindTexture(m_texture);
g_graphics.startDrawing();
// first the center // first the center
if(centerSize.area() > 0) { if(centerSize.area() > 0) {
rectCoords = Rect(screenCoords.left() + m_leftBorderTexCoords.width(), rectCoords = Rect(screenCoords.left() + m_leftBorderTexCoords.width(),
@ -175,4 +178,6 @@ void BorderImage::draw(const Rect& screenCoords)
screenCoords.top() + m_topRightCornerTexCoords.height() + centerSize.height(), screenCoords.top() + m_topRightCornerTexCoords.height() + centerSize.height(),
m_bottomRightCornerTexCoords.size()); m_bottomRightCornerTexCoords.size());
g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomRightCornerTexCoords); g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomRightCornerTexCoords);
g_graphics.stopDrawing();
} }

View File

@ -62,6 +62,10 @@ void Font::renderText(const std::string& text,
Size textBoxSize; Size textBoxSize;
const std::vector<Point>& glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize); const std::vector<Point>& glyphsPositions = calculateGlyphsPositions(text, align, &textBoxSize);
g_graphics.bindColor(color);
g_graphics.bindTexture(m_texture);
g_graphics.startDrawing();
for(int i = 0; i < textLenght; ++i) { for(int i = 0; i < textLenght; ++i) {
int glyph = (uchar)text[i]; int glyph = (uchar)text[i];
@ -122,8 +126,12 @@ void Font::renderText(const std::string& text,
} }
// render glyph // render glyph
g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords, color); g_graphics.drawTexturedRect(glyphScreenCoords, m_texture, glyphTextureCoords);
} }
g_graphics.stopDrawing();
g_graphics.bindColor(Color::white);
} }
const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text, const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text,

View File

@ -13,8 +13,6 @@ PFNGLCHECKFRAMEBUFFERSTATUSPROC oglCheckFramebufferStatus = 0;
FrameBuffer::FrameBuffer(int width, int height) FrameBuffer::FrameBuffer(int width, int height)
{ {
g_graphics.disableDrawing();
m_fbo = 0; m_fbo = 0;
m_width = width; m_width = width;
m_height = height; m_height = height;
@ -68,8 +66,6 @@ FrameBuffer::FrameBuffer(int width, int height)
FrameBuffer::~FrameBuffer() FrameBuffer::~FrameBuffer()
{ {
g_graphics.disableDrawing();
glDeleteTextures(1, &m_fboTexture); glDeleteTextures(1, &m_fboTexture);
if(m_fbo) if(m_fbo)
oglDeleteFramebuffers(1, &m_fbo); oglDeleteFramebuffers(1, &m_fbo);
@ -77,8 +73,6 @@ FrameBuffer::~FrameBuffer()
void FrameBuffer::bind() void FrameBuffer::bind()
{ {
g_graphics.disableDrawing();
if(!m_fallbackOldImp) { if(!m_fallbackOldImp) {
// bind framebuffer // bind framebuffer
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo); oglBindFramebuffer(GL_FRAMEBUFFER_EXT, m_fbo);
@ -101,8 +95,6 @@ void FrameBuffer::bind()
void FrameBuffer::unbind() void FrameBuffer::unbind()
{ {
g_graphics.disableDrawing();
if(!m_fallbackOldImp) { if(!m_fallbackOldImp) {
// bind back buffer again // bind back buffer again
oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0); oglBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
@ -127,9 +119,6 @@ void FrameBuffer::unbind()
void FrameBuffer::draw(int x, int y, int width, int height) void FrameBuffer::draw(int x, int y, int width, int height)
{ {
g_graphics.disableDrawing();
glEnable(GL_TEXTURE_2D);
glColor4ubv(Color::white.rgbaPtr()); glColor4ubv(Color::white.rgbaPtr());
glBindTexture(GL_TEXTURE_2D, m_fboTexture); glBindTexture(GL_TEXTURE_2D, m_fboTexture);
glBegin(GL_QUADS); glBegin(GL_QUADS);

View File

@ -10,13 +10,11 @@ Graphics g_graphics;
void Graphics::init() void Graphics::init()
{ {
m_drawMode = DRAW_NONE;
// setup opengl // setup opengl
glEnable(GL_ALPHA_TEST); // enable alpha by default glEnable(GL_ALPHA_TEST); // enable alpha by default
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
//glEnable(GL_TEXTURE_2D); // enable textures by default glEnable(GL_TEXTURE_2D); // enable textures by default
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND); glEnable(GL_BLEND);
@ -25,6 +23,10 @@ void Graphics::init()
logInfo("GPU ", glGetString(GL_RENDERER)); logInfo("GPU ", glGetString(GL_RENDERER));
logInfo("OpenGL ", glGetString(GL_VERSION)); logInfo("OpenGL ", glGetString(GL_VERSION));
m_drawing = false;
bindColor(Color::white);
m_opacity = 255;
} }
void Graphics::terminate() void Graphics::terminate()
@ -69,8 +71,6 @@ void Graphics::resize(const Size& size)
void Graphics::restoreViewport() void Graphics::restoreViewport()
{ {
disableDrawing();
const int& width = m_screenSize.width(); const int& width = m_screenSize.width();
const int& height = m_screenSize.height(); const int& height = m_screenSize.height();
@ -98,35 +98,16 @@ void Graphics::beginRender()
{ {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity(); glLoadIdentity();
// bind white color by default
glColor4ubv(Color::white.rgbaPtr());
m_boundColor = Color::white;
} }
void Graphics::endRender() void Graphics::endRender()
{ {
disableDrawing(); assert(!m_drawing);
// clear any bound texture
m_boundTexture.reset();
}
void Graphics::disableDrawing()
{
if(m_drawMode != DRAW_NONE) {
glEnd();
m_drawMode = DRAW_NONE;
m_boundTexture.reset();
glColor4ubv(Color::white.rgbaPtr());
}
} }
void Graphics::drawTexturedRect(const Rect& screenCoords, void Graphics::drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords, const Rect& textureCoords)
const Color& color)
{ {
if(screenCoords.isEmpty() || textureCoords.isEmpty()) if(screenCoords.isEmpty() || textureCoords.isEmpty())
return; return;
@ -150,21 +131,33 @@ void Graphics::drawTexturedRect(const Rect& screenCoords,
textureLeft = (float)textureCoords.left() / textureSize.width(); textureLeft = (float)textureCoords.left() / textureSize.width();
} }
bindTexture(texture, color); if(!m_drawing) {
bindTexture(texture);
glBegin(GL_QUADS);
}
glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top); glTexCoord2f(textureLeft, textureTop); glVertex2i(left, top);
glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom); glTexCoord2f(textureLeft, textureBottom); glVertex2i(left, bottom);
glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom); glTexCoord2f(textureRight, textureBottom); glVertex2i(right, bottom);
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top); glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
if(!m_drawing)
glEnd();
} }
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords, void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords, const Rect& textureCoords)
const Color& color)
{ {
if(screenCoords.isEmpty() || textureCoords.isEmpty()) if(screenCoords.isEmpty() || textureCoords.isEmpty())
return; return;
if(!m_drawing) {
bindTexture(texture);
startDrawing();
}
// render many repeated texture rects // render many repeated texture rects
Rect virtualScreenCoords(0,0,screenCoords.size()); Rect virtualScreenCoords(0,0,screenCoords.size());
for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) { for(int y = 0; y <= virtualScreenCoords.height(); y += textureCoords.height()) {
@ -185,14 +178,19 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
} }
partialCoords.translate(screenCoords.topLeft()); partialCoords.translate(screenCoords.topLeft());
drawTexturedRect(partialCoords, texture, partialTextureCoords, color); drawTexturedRect(partialCoords, texture, partialTextureCoords);
} }
} }
if(!m_drawing)
stopDrawing();
} }
void Graphics::drawFilledRect(const Rect& screenCoords, void Graphics::drawFilledRect(const Rect& screenCoords,
const Color& color) const Color& color)
{ {
assert(!m_drawing);
if(screenCoords.isEmpty()) if(screenCoords.isEmpty())
return; return;
@ -202,11 +200,18 @@ void Graphics::drawFilledRect(const Rect& screenCoords,
int top = screenCoords.top(); int top = screenCoords.top();
int left = screenCoords.left(); int left = screenCoords.left();
bindColor(color);
glColor4ubv(color.rgbaPtr());
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2i(left, top); glVertex2i(left, top);
glVertex2i(left, bottom); glVertex2i(left, bottom);
glVertex2i(right, bottom); glVertex2i(right, bottom);
glVertex2i(right, top); glVertex2i(right, top);
glEnd();
glEnable(GL_TEXTURE_2D);
} }
@ -214,6 +219,8 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
const Color& color, const Color& color,
int innerLineWidth) int innerLineWidth)
{ {
assert(!m_drawing);
if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty()) if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty())
return; return;
@ -223,7 +230,9 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
int top = screenCoords.top(); int top = screenCoords.top();
int left = screenCoords.left(); int left = screenCoords.left();
bindColor(color); glColor4ubv(color.rgbaPtr());
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
// top line // top line
glVertex2i(left, top); glVertex2i(left, top);
@ -248,40 +257,34 @@ void Graphics::drawBoundingRect(const Rect& screenCoords,
glVertex2i(right , bottom - innerLineWidth); glVertex2i(right , bottom - innerLineWidth);
glVertex2i(right - innerLineWidth, bottom - innerLineWidth); glVertex2i(right - innerLineWidth, bottom - innerLineWidth);
glVertex2i(right - innerLineWidth, top + innerLineWidth); glVertex2i(right - innerLineWidth, top + innerLineWidth);
glEnd();
} }
void Graphics::bindColor(const Color& color) void Graphics::bindColor(const Color& color)
{ {
// switch drawing to colored quads Color tmp = color;
if(m_drawMode != DRAW_COLOR_QUADS || m_boundColor != color) { tmp.setAlpha(std::min((uint8)m_opacity, color.a()));
if(m_drawMode != DRAW_NONE) glColor4ubv(tmp.rgbaPtr());
glEnd(); }
glDisable(GL_TEXTURE_2D);
if(m_boundColor != color) { void Graphics::bindTexture(const TexturePtr& texture)
glColor4ubv(color.rgbaPtr()); {
m_boundColor = color; glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
} }
glBegin(GL_QUADS);
m_drawMode = DRAW_COLOR_QUADS; void Graphics::startDrawing()
{
assert(!m_drawing);
glBegin(GL_QUADS);
m_drawing = true;
}
void Graphics::stopDrawing()
{
if(m_drawing) {
glEnd();
m_drawing = false;
} }
} }
void Graphics::bindTexture(const TexturePtr& texture, const Color& color)
{
// switch drawing to textured quads
if(m_drawMode != DRAW_TEXTURE_QUADS || m_boundTexture != texture || m_boundColor != color) {
if(m_drawMode != DRAW_NONE)
glEnd();
glEnable(GL_TEXTURE_2D);
if(m_boundTexture != texture) {
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
m_boundTexture = texture;
}
if(m_boundColor != color) {
glColor4ubv(color.rgbaPtr());
m_boundColor = color;
}
glBegin(GL_QUADS);
m_drawMode = DRAW_TEXTURE_QUADS;
}
}

View File

@ -5,15 +5,6 @@
class Graphics class Graphics
{ {
enum DrawMode {
DRAW_NONE = 0,
DRAW_QUADS = 1,
DRAW_TEXTURE = 2,
DRAW_COLORED = 4,
DRAW_COLOR_QUADS = DRAW_QUADS | DRAW_COLORED,
DRAW_TEXTURE_QUADS = DRAW_QUADS | DRAW_TEXTURE | DRAW_COLORED
};
public: public:
/// Initialize default OpenGL states /// Initialize default OpenGL states
void init(); void init();
@ -35,18 +26,18 @@ public:
/// Called after every render /// Called after every render
void endRender(); void endRender();
void disableDrawing();
void bindColor(const Color& color);
void bindTexture(const TexturePtr& texture);
// drawing API // drawing API
void drawTexturedRect(const Rect& screenCoords, void drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords = Rect(), const Rect& textureCoords = Rect());
const Color& color = Color::white);
void drawRepeatedTexturedRect(const Rect& screenCoords, void drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture, const TexturePtr& texture,
const Rect& textureCoords, const Rect& textureCoords);
const Color& color = Color::white);
void drawFilledRect(const Rect& screenCoords, void drawFilledRect(const Rect& screenCoords,
const Color& color); const Color& color);
@ -57,14 +48,16 @@ public:
const Size& getScreenSize() const { return m_screenSize; } const Size& getScreenSize() const { return m_screenSize; }
private: void startDrawing();
void bindTexture(const TexturePtr& texture, const Color& color = Color::white); void stopDrawing();
void bindColor(const Color& color);
TexturePtr m_boundTexture; int getOpacity() const { return m_opacity; }
Color m_boundColor; void setOpacity(int opacity) { m_opacity = opacity; }
private:
bool m_drawing;
int m_opacity;
Size m_screenSize; Size m_screenSize;
DrawMode m_drawMode;
}; };
extern Graphics g_graphics; extern Graphics g_graphics;

View File

@ -11,8 +11,6 @@ Texture::Texture(int width, int height, int channels, uchar *pixels)
uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height) uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int height)
{ {
g_graphics.disableDrawing();
// get smax texture size supported by the driver // get smax texture size supported by the driver
static GLint maxTexSize = -1; static GLint maxTexSize = -1;
if(maxTexSize == -1) if(maxTexSize == -1)
@ -95,8 +93,6 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
Texture::~Texture() Texture::~Texture()
{ {
g_graphics.disableDrawing();
// free texture from gl memory // free texture from gl memory
if(m_textureId) if(m_textureId)
glDeleteTextures(1, &m_textureId); glDeleteTextures(1, &m_textureId);
@ -104,8 +100,6 @@ Texture::~Texture()
void Texture::enableBilinearFilter() void Texture::enableBilinearFilter()
{ {
g_graphics.disableDrawing();
// enable smooth texture // enable smooth texture
glBindTexture(GL_TEXTURE_2D, m_textureId); glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@ -114,8 +108,6 @@ void Texture::enableBilinearFilter()
uchar* Texture::getPixels() uchar* Texture::getPixels()
{ {
g_graphics.disableDrawing();
// copy pixels from opengl memory // copy pixels from opengl memory
uchar* pixels = new uchar[m_glSize.area()*4]; uchar* pixels = new uchar[m_glSize.area()*4];
glBindTexture(GL_TEXTURE_2D, m_textureId); glBindTexture(GL_TEXTURE_2D, m_textureId);

View File

@ -2,6 +2,7 @@
#include <graphics/fontmanager.h> #include <graphics/fontmanager.h>
#include <ui/ui.h> #include <ui/ui.h>
#include <net/protocol.h> #include <net/protocol.h>
#include <core/eventdispatcher.h>
void LuaInterface::registerFunctions() void LuaInterface::registerFunctions()
{ {
@ -21,6 +22,8 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberField<UIWidget>("width", &UIWidget::getWidth, &UIWidget::setWidth); g_lua.bindClassMemberField<UIWidget>("width", &UIWidget::getWidth, &UIWidget::setWidth);
g_lua.bindClassMemberField<UIWidget>("height", &UIWidget::getHeight, &UIWidget::setHeight); g_lua.bindClassMemberField<UIWidget>("height", &UIWidget::getHeight, &UIWidget::setHeight);
g_lua.bindClassMemberField<UIWidget>("parent", &UIWidget::getParent, &UIWidget::setParent); g_lua.bindClassMemberField<UIWidget>("parent", &UIWidget::getParent, &UIWidget::setParent);
g_lua.bindClassMemberField<UIWidget>("color", &UIWidget::getColor, &UIWidget::setColor);
g_lua.bindClassMemberField<UIWidget>("opacity", &UIWidget::getOpacity, &UIWidget::setOpacity);
g_lua.bindClassMemberField<UIWidget>("marginTop", &UIWidget::getMarginTop, &UIWidget::setMarginTop); g_lua.bindClassMemberField<UIWidget>("marginTop", &UIWidget::getMarginTop, &UIWidget::setMarginTop);
g_lua.bindClassMemberField<UIWidget>("marginBottom", &UIWidget::getMarginBottom, &UIWidget::setMarginBottom); g_lua.bindClassMemberField<UIWidget>("marginBottom", &UIWidget::getMarginBottom, &UIWidget::setMarginBottom);
g_lua.bindClassMemberField<UIWidget>("marginLeft", &UIWidget::getMarginLeft, &UIWidget::setMarginLeft); g_lua.bindClassMemberField<UIWidget>("marginLeft", &UIWidget::getMarginLeft, &UIWidget::setMarginLeft);
@ -57,8 +60,10 @@ void LuaInterface::registerFunctions()
// global functions // global functions
g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1)); g_lua.bindGlobalFunction("importFont", std::bind(&FontManager::importFont, &g_fonts, _1));
g_lua.bindGlobalFunction("setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1));
g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1)); g_lua.bindGlobalFunction("importStyles", std::bind(&UIManager::importStyles, &g_ui, _1));
g_lua.bindGlobalFunction("setDefaultFont", std::bind(&FontManager::setDefaultFont, &g_fonts, _1));
g_lua.bindGlobalFunction("loadUI", std::bind(&UIManager::loadUI, &g_ui, _1)); g_lua.bindGlobalFunction("loadUI", std::bind(&UIManager::loadUI, &g_ui, _1));
g_lua.bindGlobalFunction("getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui)); g_lua.bindGlobalFunction("getRootWidget", std::bind(&UIManager::getRootWidget, &g_ui));
g_lua.bindGlobalFunction("addEvent", std::bind(&EventDispatcher::addEvent, &g_dispatcher, _1, false));
g_lua.bindGlobalFunction("scheduleEvent", std::bind(&EventDispatcher::scheduleEvent, &g_dispatcher, _1, _2));
} }

View File

@ -192,4 +192,34 @@ luavalue_cast(int index, std::function<Ret(Args...)>& o) {
return false; return false;
} }
// additional casts
inline void push_luavalue(const Color& v) {
g_lua.newTable();
g_lua.pushInteger(v.r());
g_lua.setField("r");
g_lua.pushInteger(v.g());
g_lua.setField("g");
g_lua.pushInteger(v.b());
g_lua.setField("b");
g_lua.pushInteger(v.a());
g_lua.setField("a");
}
inline bool luavalue_cast(int index, Color& o) {
if(!g_lua.isTable(index))
return false;
g_lua.getField("r", index);
o.setRed(g_lua.popInteger());
g_lua.getField("g", index);
o.setGreen(g_lua.popInteger());
g_lua.getField("b", index);
o.setBlue(g_lua.popInteger());
g_lua.getField("a", index);
o.setAlpha(g_lua.popInteger());
return true;
}
#endif #endif

View File

@ -24,8 +24,9 @@ void UIButton::loadStyleFromOTML(const OTMLNodePtr& styleNode)
UIWidget::loadStyleFromOTML(styleNode); UIWidget::loadStyleFromOTML(styleNode);
for(int i=0; i<3; ++i) { for(int i=0; i<3; ++i) {
m_statesStyle[i].image = getImage(); m_statesStyle[i].image = m_image;
m_statesStyle[i].color = getColor(); m_statesStyle[i].color = m_color;
m_statesStyle[i].fontColor = m_fontColor;
m_statesStyle[i].textTranslate = Point(0,0); m_statesStyle[i].textTranslate = Point(0,0);
} }
@ -51,7 +52,8 @@ void UIButton::loadStateStyle(ButtonStateStyle& stateStyle, const OTMLNodePtr& s
if(OTMLNodePtr node = stateStyleNode->get("image")) if(OTMLNodePtr node = stateStyleNode->get("image"))
stateStyle.image = Image::loadFromOTML(node); stateStyle.image = Image::loadFromOTML(node);
stateStyle.textTranslate = stateStyleNode->readAt("text-translate", Point()); stateStyle.textTranslate = stateStyleNode->readAt("text-translate", Point());
stateStyle.color = stateStyleNode->readAt("color", getColor()); stateStyle.color = stateStyleNode->readAt("font-color", m_fontColor);
stateStyle.color = stateStyleNode->readAt("color", m_color);
} }
void UIButton::render() void UIButton::render()
@ -63,7 +65,7 @@ void UIButton::render()
currentStyle.image->draw(textRect); currentStyle.image->draw(textRect);
textRect.translate(currentStyle.textTranslate); textRect.translate(currentStyle.textTranslate);
getFont()->renderText(m_text, textRect, AlignCenter, currentStyle.color); getFont()->renderText(m_text, textRect, AlignCenter, currentStyle.fontColor);
} }
void UIButton::onHoverChange(UIHoverEvent& event) void UIButton::onHoverChange(UIHoverEvent& event)

View File

@ -8,6 +8,7 @@ class UIButton : public UIWidget
struct ButtonStateStyle { struct ButtonStateStyle {
ImagePtr image; ImagePtr image;
Point textTranslate; Point textTranslate;
Color fontColor;
Color color; Color color;
}; };

View File

@ -30,7 +30,7 @@ void UILabel::loadStyleFromOTML(const OTMLNodePtr& styleNode)
void UILabel::render() void UILabel::render()
{ {
getFont()->renderText(m_text, getGeometry(), m_align, getColor()); getFont()->renderText(m_text, getGeometry(), m_align, m_fontColor);
} }
void UILabel::resizeToText() void UILabel::resizeToText()

View File

@ -37,7 +37,7 @@ void UILineEdit::render()
int textLength = m_text.length(); int textLength = m_text.length();
const TexturePtr& texture = m_font->getTexture(); const TexturePtr& texture = m_font->getTexture();
for(int i=0;i<textLength;++i) { for(int i=0;i<textLength;++i) {
g_graphics.drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i], m_color); g_graphics.drawTexturedRect(m_glyphsCoords[i], texture, m_glyphsTexCoords[i]);
} }
// render cursor // render cursor

View File

@ -8,6 +8,7 @@
#include <graphics/borderimage.h> #include <graphics/borderimage.h>
#include <graphics/fontmanager.h> #include <graphics/fontmanager.h>
#include <otml/otmlnode.h> #include <otml/otmlnode.h>
#include <graphics/graphics.h>
UIWidget::UIWidget(UIWidgetType type) UIWidget::UIWidget(UIWidgetType type)
{ {
@ -18,8 +19,10 @@ UIWidget::UIWidget(UIWidgetType type)
m_focusable = false; m_focusable = false;
m_destroyed = false; m_destroyed = false;
m_updateScheduled = false; m_updateScheduled = false;
m_opacity = 255;
m_marginLeft = m_marginRight = m_marginTop = m_marginBottom = 0; m_marginLeft = m_marginRight = m_marginTop = m_marginBottom = 0;
m_color = Color::white; m_color = Color::white;
m_fontColor = Color::white;
// generate an unique id, this is need because anchored layouts find widgets by id // generate an unique id, this is need because anchored layouts find widgets by id
static unsigned long id = 1; static unsigned long id = 1;
@ -116,10 +119,18 @@ void UIWidget::loadStyleFromOTML(const OTMLNodePtr& styleNode)
else if(node->tag() == "font") { else if(node->tag() == "font") {
setFont(g_fonts.getFont(node->value())); setFont(g_fonts.getFont(node->value()));
} }
// font color
else if(node->tag() == "font-color") {
setFontColor(node->read<Color>());
}
// color // color
else if(node->tag() == "color") { else if(node->tag() == "color") {
setColor(node->read<Color>()); setColor(node->read<Color>());
} }
// opacity
else if(node->tag() == "opacity") {
setOpacity(node->read<int>());
}
// size // size
else if(node->tag() == "size") { else if(node->tag() == "size") {
resize(node->read<Size>()); resize(node->read<Size>());
@ -199,8 +210,16 @@ void UIWidget::render()
m_image->draw(getGeometry()); m_image->draw(getGeometry());
for(const UIWidgetPtr& child : m_children) { for(const UIWidgetPtr& child : m_children) {
if(child->isVisible()) if(child->isVisible()) {
int oldOpacity = g_graphics.getOpacity();
if(child->getOpacity() < oldOpacity)
g_graphics.setOpacity(child->getOpacity());
g_graphics.bindColor(child->getColor());
child->render(); child->render();
g_graphics.setOpacity(oldOpacity);
}
} }
} }

View File

@ -49,7 +49,9 @@ public:
void setImage(const ImagePtr& image) { m_image = image; } void setImage(const ImagePtr& image) { m_image = image; }
virtual void setFont(const FontPtr& font) { m_font = font; } virtual void setFont(const FontPtr& font) { m_font = font; }
void setOpacity(int opacity) { m_opacity = opacity; }
void setColor(const Color& color) { m_color = color; } void setColor(const Color& color) { m_color = color; }
void setFontColor(const Color& color) { m_fontColor = color; }
void setMarginLeft(int margin) { m_marginLeft = margin; updateGeometry(); } void setMarginLeft(int margin) { m_marginLeft = margin; updateGeometry(); }
void setMarginRight(int margin) { m_marginRight = margin; updateGeometry(); } void setMarginRight(int margin) { m_marginRight = margin; updateGeometry(); }
void setMarginTop(int margin) { m_marginTop = margin; updateGeometry(); } void setMarginTop(int margin) { m_marginTop = margin; updateGeometry(); }
@ -86,7 +88,9 @@ public:
ImagePtr getImage() const { return m_image; } ImagePtr getImage() const { return m_image; }
FontPtr getFont() const { return m_font; } FontPtr getFont() const { return m_font; }
Color getFontColor() const { return m_fontColor; }
Color getColor() const { return m_color; } Color getColor() const { return m_color; }
int getOpacity() const { return m_opacity; }
int getMarginLeft() const { return m_marginLeft; } int getMarginLeft() const { return m_marginLeft; }
int getMarginRight() const { return m_marginRight; } int getMarginRight() const { return m_marginRight; }
int getMarginTop() const { return m_marginTop; } int getMarginTop() const { return m_marginTop; }
@ -164,7 +168,9 @@ protected:
// basic style components used by all widgets // basic style components used by all widgets
ImagePtr m_image; ImagePtr m_image;
FontPtr m_font; FontPtr m_font;
int m_opacity;
Color m_color; Color m_color;
Color m_fontColor;
int m_marginLeft; int m_marginLeft;
int m_marginRight; int m_marginRight;
int m_marginTop; int m_marginTop;

View File

@ -55,7 +55,7 @@ void UIWindow::render()
headTextRect.addLeft(-m_headMargin); headTextRect.addLeft(-m_headMargin);
else if(m_titleAlign & AlignRight) else if(m_titleAlign & AlignRight)
headTextRect.addRight(-m_headMargin); headTextRect.addRight(-m_headMargin);
getFont()->renderText(m_title, headTextRect, m_titleAlign, getColor()); m_font->renderText(m_title, headTextRect, m_titleAlign, m_fontColor);
} }
// draw window body // draw window body

View File

@ -46,17 +46,16 @@ private:
inline std::ostream& operator<<(std::ostream& out, const Color& color) inline std::ostream& operator<<(std::ostream& out, const Color& color)
{ {
out << "Color(" << (int)color.r() << "," out << (int)color.r() << " "<< (int)color.g() << " "<< (int)color.b() << " " << (int)color.a();
<< (int)color.g() << ","
<< (int)color.b() << ","
<< (int)color.a() << ")";
return out; return out;
} }
inline std::istream& operator>>(std::istream& in, Color& color) inline std::istream& operator>>(std::istream& in, Color& color)
{ {
int r, g, b, a; int r, g, b, a = 255;
in >> r >> g >> b >> a; in >> r >> g >> b;
if(!in.eof())
in >> a;
color.setRGBA(r, g, b, a); color.setRGBA(r, g, b, a);
return in; return in;
} }

0
src/protocolgame.h Executable file → Normal file
View File

0
src/tibiaspr.h Executable file → Normal file
View File