From f54ef6401d17c19e1e6047909098f4455c1134c4 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Fri, 8 Apr 2011 15:29:59 -0300 Subject: [PATCH] image class --- CMakeLists.txt | 1 + src/framework/borderedimage.cpp | 27 ++++++++++++++++-- src/framework/borderedimage.h | 29 +++++++++++++++---- src/framework/image.cpp | 50 +++++++++++++++++++++++++++++++++ src/framework/image.h | 50 +++++++++++++++++++++++++++++++++ src/framework/ui/uibutton.cpp | 25 +++++++++-------- src/framework/ui/uibutton.h | 8 ++++-- src/framework/ui/uiconstants.h | 34 +++++++++++----------- src/framework/ui/uicontainer.h | 2 +- src/framework/ui/uielement.h | 4 +-- src/framework/ui/uilabel.h | 2 +- src/framework/ui/uipanel.cpp | 20 ++++++------- src/framework/ui/uipanel.h | 2 +- 13 files changed, 200 insertions(+), 54 deletions(-) create mode 100644 src/framework/image.cpp create mode 100644 src/framework/image.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 02cd0ee4..7eb1d45b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,7 @@ SET(SOURCES src/teststate.cpp # framework sources + src/framework/image.cpp src/framework/borderedimage.cpp src/framework/dispatcher.cpp src/framework/framebuffer.cpp diff --git a/src/framework/borderedimage.cpp b/src/framework/borderedimage.cpp index 2632587e..c43b272e 100644 --- a/src/framework/borderedimage.cpp +++ b/src/framework/borderedimage.cpp @@ -28,9 +28,32 @@ #include -BorderedImage::BorderedImage(const std::string& textureName) +BorderedImage::BorderedImage(TexturePtr texture, + const Rect& left, + const Rect& right, + const Rect& top, + const Rect& bottom, + const Rect& topLeft, + const Rect& topRight, + const Rect& bottomLeft, + const Rect& bottomRight, + const Rect& center) : Image(texture) { - m_texture = g_textures.get(textureName); + setTexCoords(left, right, top, bottom, topLeft, topRight, bottomLeft, bottomRight, center); +} + +BorderedImage::BorderedImage(const std::string& texture, + const Rect& left, + const Rect& right, + const Rect& top, + const Rect& bottom, + const Rect& topLeft, + const Rect& topRight, + const Rect& bottomLeft, + const Rect& bottomRight, + const Rect& center) : Image(texture) +{ + setTexCoords(left, right, top, bottom, topLeft, topRight, bottomLeft, bottomRight, center); } void BorderedImage::setTexCoords(const Rect& left, diff --git a/src/framework/borderedimage.h b/src/framework/borderedimage.h index 859c93cd..d186db17 100644 --- a/src/framework/borderedimage.h +++ b/src/framework/borderedimage.h @@ -26,15 +26,34 @@ #define BORDEREDIMAGE_H #include "prerequisites.h" - +#include "image.h" #include "rect.h" #include "texture.h" -class BorderedImage +class BorderedImage : public Image { public: - BorderedImage(TexturePtr texture) : m_texture(texture) { } - BorderedImage(const std::string& textureName); + BorderedImage(TexturePtr texture, + const Rect& left, + const Rect& right, + const Rect& top, + const Rect& bottom, + const Rect& topLeft, + const Rect& topRight, + const Rect& bottomLeft, + const Rect& bottomRight, + const Rect& center); + + BorderedImage(const std::string& texture, + const Rect& left, + const Rect& right, + const Rect& top, + const Rect& bottom, + const Rect& topLeft, + const Rect& topRight, + const Rect& bottomLeft, + const Rect& bottomRight, + const Rect& center); void setTexCoords(const Rect& left, const Rect& right, @@ -49,8 +68,6 @@ public: void draw(const Rect& screenCoords); private: - TexturePtr m_texture; - Rect m_leftBorderTexCoords; Rect m_rightBorderTexCoords; Rect m_topBorderTexCoords; diff --git a/src/framework/image.cpp b/src/framework/image.cpp new file mode 100644 index 00000000..d41198ce --- /dev/null +++ b/src/framework/image.cpp @@ -0,0 +1,50 @@ +/* The MIT License + * + * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#include "image.h" +#include "graphics.h" +#include "textures.h" + +Image::Image(const std::string& texture) +{ + m_texture = g_textures.get(texture); +} + +Image::Image(const std::string& texture, Rect textureCoords) : + m_textureCoords(textureCoords) +{ + m_texture = g_textures.get(texture); +} + +void Image::enableBilinearFilter() +{ + m_texture->enableBilinearFilter(); +} + +void Image::draw(const Rect& screenCoords) +{ + g_graphics.drawTexturedRect(screenCoords, m_texture.get(), m_textureCoords); +} + + diff --git a/src/framework/image.h b/src/framework/image.h new file mode 100644 index 00000000..b63875d7 --- /dev/null +++ b/src/framework/image.h @@ -0,0 +1,50 @@ +/* The MIT License + * + * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + + +#ifndef IMAGE_H +#define IMAGE_H + +#include "prerequisites.h" +#include "texture.h" +#include "rect.h" + +class Image +{ +public: + Image(TexturePtr texture) : m_texture(texture) { } + Image(TexturePtr texture, Rect textureCoords) : m_texture(texture), m_textureCoords(textureCoords) { } + Image(const std::string& texture); + Image(const std::string& texture, Rect textureCoords); + + void enableBilinearFilter(); + virtual void draw(const Rect& screenCoords); + +protected: + TexturePtr m_texture; + Rect m_textureCoords; +}; + +typedef std::shared_ptr ImagePtr; + +#endif // IMAGE_H diff --git a/src/framework/ui/uibutton.cpp b/src/framework/ui/uibutton.cpp index cf84a620..032ef315 100644 --- a/src/framework/ui/uibutton.cpp +++ b/src/framework/ui/uibutton.cpp @@ -27,23 +27,24 @@ #include "../font.h" UIButton::UIButton(const std::string& text) : - m_text(text) + m_text(text), + m_state(UI::ButtonUp) { - m_boderedImage = BorderedImagePtr(new BorderedImage("ui.png")); - m_boderedImage->setTexCoords(Rect(45,139,1,18), - Rect(130,139,1,18), - Rect(46,138,84,1), - Rect(46,157,84,1), - Rect(45,138,1,1), - Rect(130,138,1,1), - Rect(45,157,1,1), - Rect(130,157,1,1), - Rect(46,139,84,18)); + m_imageButtonUp = ImagePtr(new BorderedImage("ui.png", + Rect(45,139,1,18), + Rect(130,139,1,18), + Rect(46,138,84,1), + Rect(46,157,84,1), + Rect(45,138,1,1), + Rect(130,138,1,1), + Rect(45,157,1,1), + Rect(130,157,1,1), + Rect(46,139,84,18))); } void UIButton::render() { - m_boderedImage->draw(m_rect); + m_imageButtonUp->draw(m_rect); g_fonts.get("tibia-8px-antialised")->renderText(m_text, m_rect, ALIGN_CENTER); } diff --git a/src/framework/ui/uibutton.h b/src/framework/ui/uibutton.h index 758340e9..6b3a4750 100644 --- a/src/framework/ui/uibutton.h +++ b/src/framework/ui/uibutton.h @@ -36,11 +36,15 @@ public: void render(); - virtual UI::ControlType getControlType() const { return UI::Button; } + virtual UI::EControlType getControlType() const { return UI::Button; } private: std::string m_text; - BorderedImagePtr m_boderedImage; + UI::EButtonState m_state; + + ImagePtr m_imageButtonUp; + ImagePtr m_imageButtonDown; + ImagePtr m_imageButtonOver; }; typedef std::shared_ptr UIButtonPtr; diff --git a/src/framework/ui/uiconstants.h b/src/framework/ui/uiconstants.h index 7aafffeb..b5bf2fd2 100644 --- a/src/framework/ui/uiconstants.h +++ b/src/framework/ui/uiconstants.h @@ -26,32 +26,32 @@ #define UICONSTANTS_H namespace UI { - enum ButtonState + enum EButtonState { - Up, - Down, - MouseOver + ButtonUp, + ButtonDown, + ButtonMouseOver }; - enum ButtonEvent + enum EButtonEvent { - PressUp, - PressDown, - EnterMouseOver, - LeaveMouseOver + ButtonPressUp, + ButtonPressDown, + ButtonEnterMouseOver, + ButtonLeaveMouseOver }; - enum MessageBoxFlags + enum EMessageBoxFlags { - Ok = 1 << 0, - Cancel = 1 << 1, - Yes = 1 << 2, - No = 1 << 3, - OkCancel = Ok | Cancel, - YesNo = Yes | No + MessageBoxOk = 1 << 0, + MessageBoxCancel = 1 << 1, + MessageBoxYes = 1 << 2, + MessageBoxNo = 1 << 3, + MessageBoxOkCancel = MessageBoxOk | MessageBoxCancel, + MessageBoxYesNo = MessageBoxYes | MessageBoxNo }; - enum ControlType + enum EControlType { Element, Container, diff --git a/src/framework/ui/uicontainer.h b/src/framework/ui/uicontainer.h index 01c191bd..5c7c3d60 100644 --- a/src/framework/ui/uicontainer.h +++ b/src/framework/ui/uicontainer.h @@ -52,7 +52,7 @@ public: virtual void setActiveElement(UIElementPtr activeElement); UIElementPtr getActiveElement() const { return m_activeElement; } - virtual UI::ControlType getControlType() const { return UI::Container; } + virtual UI::EControlType getControlType() const { return UI::Container; } UIContainerPtr asUIContainer() { return std::static_pointer_cast(shared_from_this()); } diff --git a/src/framework/ui/uielement.h b/src/framework/ui/uielement.h index cae845ff..7b9b1ed0 100644 --- a/src/framework/ui/uielement.h +++ b/src/framework/ui/uielement.h @@ -61,12 +61,12 @@ public: virtual void setVisible(bool visible) { m_visible = visible; } bool isVisible() const { return m_visible; } - virtual UI::ControlType getControlType() const { return UI::Element; } + virtual UI::EControlType getControlType() const { return UI::Element; } UIElementPtr asUIElement() { return shared_from_this(); } protected: - UI::ControlType m_type; + UI::EControlType m_type; UIContainerPtr m_parent; Rect m_rect; std::string m_name; diff --git a/src/framework/ui/uilabel.h b/src/framework/ui/uilabel.h index c193d01f..24856adc 100644 --- a/src/framework/ui/uilabel.h +++ b/src/framework/ui/uilabel.h @@ -33,7 +33,7 @@ class UILabel : public UIElement public: UILabel() { } - virtual UI::ControlType getControlType() const { return UI::Label; } + virtual UI::EControlType getControlType() const { return UI::Label; } }; #endif // UILABEL_H diff --git a/src/framework/ui/uipanel.cpp b/src/framework/ui/uipanel.cpp index 7339c517..ab6d24d6 100644 --- a/src/framework/ui/uipanel.cpp +++ b/src/framework/ui/uipanel.cpp @@ -27,16 +27,16 @@ UIPanel::UIPanel() { - m_boderedImage = BorderedImagePtr(new BorderedImage("ui.png")); - m_boderedImage->setTexCoords(Rect(0,214,5,32), - Rect(6,214,5,32), - Rect(43,214,32,5), - Rect(43,220,32,5), - Rect(43,225,5,5), - Rect(49,225,5,5), - Rect(43,230,5,5), - Rect(48,231,5,5), - Rect(11,214,32,32)); + m_boderedImage = BorderedImagePtr(new BorderedImage("ui.png", + Rect(0,214,5,32), + Rect(6,214,5,32), + Rect(43,214,32,5), + Rect(43,220,32,5), + Rect(43,225,5,5), + Rect(49,225,5,5), + Rect(43,230,5,5), + Rect(48,231,5,5), + Rect(11,214,32,32))); } void UIPanel::render() diff --git a/src/framework/ui/uipanel.h b/src/framework/ui/uipanel.h index 1535b14b..3297be47 100644 --- a/src/framework/ui/uipanel.h +++ b/src/framework/ui/uipanel.h @@ -36,7 +36,7 @@ public: void render(); - virtual UI::ControlType getControlType() const { return UI::Panel; } + virtual UI::EControlType getControlType() const { return UI::Panel; } private: BorderedImagePtr m_boderedImage;