basic menu functionallity

This commit is contained in:
Eduardo Bart 2011-04-09 21:51:35 -03:00
parent f2bdc89d8d
commit 9d1ddf34bf
16 changed files with 313 additions and 22 deletions

View File

@ -84,6 +84,8 @@ SET(SOURCES
src/framework/ui/uilabel.cpp src/framework/ui/uilabel.cpp
src/framework/ui/uiwindow.cpp src/framework/ui/uiwindow.cpp
src/framework/ui/uiwindowskin.cpp src/framework/ui/uiwindowskin.cpp
src/framework/ui/uitextedit.cpp
src/framework/ui/uitexteditskin.cpp
# network # network
src/framework/net/connection.cpp src/framework/net/connection.cpp

View File

@ -63,7 +63,7 @@ void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
} }
} }
// store glyph size // store glyph size
m_glyphsSize[glyph].setWidth(width); m_glyphsSize[glyph].setSize(width, m_glyphHeight);
} }
delete[] texturePixels; delete[] texturePixels;
@ -102,7 +102,6 @@ bool Font::load(const std::string& file)
// set glyphs height // set glyphs height
for(int glyph = 32; glyph < 256; ++glyph) { for(int glyph = 32; glyph < 256; ++glyph) {
m_glyphsSize[glyph].setHeight(m_glyphHeight);
} }
calculateGlyphsWidthsAutomatically(glyphSize); calculateGlyphsWidthsAutomatically(glyphSize);
@ -124,7 +123,7 @@ bool Font::load(const std::string& file)
m_glyphsTextureCoords[glyph].setRect(((glyph - 32) % numHorizontalGlyphs) * glyphSize.width(), m_glyphsTextureCoords[glyph].setRect(((glyph - 32) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - 32) / numHorizontalGlyphs) * glyphSize.height(), ((glyph - 32) / numHorizontalGlyphs) * glyphSize.height(),
m_glyphsSize[glyph].width(), m_glyphsSize[glyph].width(),
m_glyphsSize[glyph].height()); m_glyphHeight);
} }
} catch (YAML::ParserException& e) { } catch (YAML::ParserException& e) {
@ -253,7 +252,7 @@ Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size *
// protect buffer overflow on glyphsPostions // protect buffer overflow on glyphsPostions
int numGlyphs = text.length(); int numGlyphs = text.length();
if(numGlyphs > 8192) if(numGlyphs > 8192)
logFatal("A text was too long to render!"); logFatal("could not calculate glyphs positions, text length is > 8192!");
// calculate lines width // calculate lines width
if((align & ALIGN_RIGHT || align & ALIGN_HORIZONTAL_CENTER) || textBoxSize) { if((align & ALIGN_RIGHT || align & ALIGN_HORIZONTAL_CENTER) || textBoxSize) {
@ -281,7 +280,7 @@ Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size *
// new line or first glyph // new line or first glyph
if(glyph == (uchar)'\n' || i == 0) { if(glyph == (uchar)'\n' || i == 0) {
if(glyph == (uchar)'\n') { if(glyph == (uchar)'\n') {
virtualPos.y += m_glyphsSize[glyph].height() + m_glyphSpacing.height(); virtualPos.y += m_glyphHeight + m_glyphSpacing.height();
lines++; lines++;
} }

View File

@ -35,5 +35,6 @@
#include "uilabel.h" #include "uilabel.h"
#include "uiskins.h" #include "uiskins.h"
#include "uiwindow.h" #include "uiwindow.h"
#include "uitextedit.h"
#endif // UI_H #endif // UI_H

View File

@ -38,9 +38,12 @@ bool UIButton::onInputEvent(const InputEvent& event)
if(event.type == EV_MOUSE_LDOWN && if(event.type == EV_MOUSE_LDOWN &&
getRect().contains(Point(event.mouse.x, event.mouse.y))) { getRect().contains(Point(event.mouse.x, event.mouse.y))) {
m_state = UI::ButtonDown; m_state = UI::ButtonDown;
} else if(m_state == UI::ButtonDown && } else if(m_state == UI::ButtonDown && event.type == EV_MOUSE_LUP) {
event.type == EV_MOUSE_LUP) {
m_state = UI::ButtonUp; m_state = UI::ButtonUp;
if(getRect().contains(Point(event.mouse.x, event.mouse.y))) {
if(m_buttonClickCallback)
m_buttonClickCallback();
}
} }
return false; return false;
} }

View File

@ -29,6 +29,8 @@
#include "uielement.h" #include "uielement.h"
#include "../borderedimage.h" #include "../borderedimage.h"
typedef std::function<void()> Callback;
class UIButton : public UIElement class UIButton : public UIElement
{ {
public: public:
@ -43,9 +45,12 @@ public:
UI::EButtonState getState() { return m_state; } UI::EButtonState getState() { return m_state; }
void onClick(const Callback& callback) { m_buttonClickCallback = callback; }
private: private:
std::string m_text; std::string m_text;
UI::EButtonState m_state; UI::EButtonState m_state;
Callback m_buttonClickCallback;
}; };
typedef std::shared_ptr<UIButton> UIButtonPtr; typedef std::shared_ptr<UIButton> UIButtonPtr;

View File

@ -32,7 +32,8 @@
class UIButtonSkin : public UIElementSkin class UIButtonSkin : public UIElementSkin
{ {
public: public:
UIButtonSkin(const std::string& name, UI::EElementType elementType) :UIElementSkin(name, elementType) { } UIButtonSkin(const std::string& name) :
UIElementSkin(name, UI::Button) { }
void load(const YAML::Node& node); void load(const YAML::Node& node);
void draw(UIElement *element); void draw(UIElement *element);

View File

@ -58,7 +58,7 @@ namespace UI {
Panel, Panel,
Window, Window,
Label, Label,
TextBox, TextEdit,
Button, Button,
CheckBox CheckBox
}; };

View File

@ -23,4 +23,26 @@
#include "uilabel.h" #include "uilabel.h"
#include "../fonts.h"
UILabel::UILabel(const std::string& text, Font* font) :
UIElement(UI::Label),
m_text(text),
m_font(font)
{
if(!font)
m_font = g_fonts.get("tibia-10px-antialised");
setSize(m_font->calculateTextRectSize(text));
}
void UILabel::render()
{
m_font->renderText(m_text, getRect(), ALIGN_LEFT, Color(0xFFBFBFBF));
}
void UILabel::setText(const std::string& text)
{
setSize(m_font->calculateTextRectSize(text));
m_text = text;
}

View File

@ -28,10 +28,23 @@
#include "../prerequisites.h" #include "../prerequisites.h"
#include "uielement.h" #include "uielement.h"
class Font;
class UILabel : public UIElement class UILabel : public UIElement
{ {
public: public:
UILabel() : UIElement(UI::Label) { } UILabel(const std::string& text, Font *font = NULL);
void render();
void setText(const std::string& text);
const std::string& getText() const { return m_text; }
private:
std::string m_text;
Font *m_font;
}; };
typedef std::shared_ptr<UILabel> UILabelPtr;
#endif // UILABEL_H #endif // UILABEL_H

View File

@ -28,6 +28,7 @@
#include "uielementskin.h" #include "uielementskin.h"
#include "uibuttonskin.h" #include "uibuttonskin.h"
#include "uiwindowskin.h" #include "uiwindowskin.h"
#include "uitexteditskin.h"
UISkins g_uiSkins; UISkins g_uiSkins;
@ -63,7 +64,7 @@ bool UISkins::load(const std::string& file)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIButtonSkin *skin = new UIButtonSkin(name, UI::Button); UIElementSkin *skin = new UIButtonSkin(name);
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
@ -87,11 +88,37 @@ bool UISkins::load(const std::string& file)
std::string name; std::string name;
it.first() >> name; it.first() >> name;
UIWindowSkin *skin = new UIWindowSkin(name, UI::Window); UIElementSkin *skin = new UIWindowSkin(name);
skin->load(it.second()); skin->load(it.second());
m_elementSkins.push_back(skin); m_elementSkins.push_back(skin);
} }
} }
{
const YAML::Node& node = doc["labels"];
for(auto it = node.begin(); it != node.end(); ++it) {
std::string name;
it.first() >> name;
UIElementSkin *skin = new UIElementSkin(name, UI::Label);
skin->load(it.second());
m_elementSkins.push_back(skin);
}
}
{
const YAML::Node& node = doc["text edits"];
for(auto it = node.begin(); it != node.end(); ++it) {
std::string name;
it.first() >> name;
UIElementSkin *skin = new UITextEditSkin(name);
skin->load(it.second());
m_elementSkins.push_back(skin);
}
}
} catch (YAML::ParserException& e) { } catch (YAML::ParserException& e) {
logError("Malformed font file \"%s\"", file.c_str()); logError("Malformed font file \"%s\"", file.c_str());
return false; return false;

View File

@ -0,0 +1,45 @@
/* 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 "uitextedit.h"
#include "../fonts.h"
UITextEdit::UITextEdit(Font* font) :
UIElement(UI::TextEdit),
m_font(font)
{
if(!font)
m_font = g_fonts.get("tibia-10px-antialised");
}
void UITextEdit::render()
{
UIElement::render();
m_font->renderText(m_text, getRect(), ALIGN_LEFT, Color(0xFFBFBFBF));
}
void UITextEdit::setText(const std::string& text)
{
m_text = text;
}

View File

@ -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 UITEXTEDIT_H
#define UITEXTEDIT_H
#include "../prerequisites.h"
#include "uielement.h"
class Font;
class UITextEdit : public UIElement
{
public:
UITextEdit(Font *font = NULL);
void render();
void setText(const std::string& text);
const std::string& getText() const { return m_text; }
private:
std::string m_text;
Font *m_font;
};
typedef std::shared_ptr<UITextEdit> UITextEditPtr;
#endif // UITEXTEDIT_H

View File

@ -0,0 +1,26 @@
/* 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 "uitexteditskin.h"

View File

@ -0,0 +1,40 @@
/* 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 UITEXTEDITSKIN_H
#define UITEXTEDITSKIN_H
#include "../prerequisites.h"
#include "uiconstants.h"
#include "uielementskin.h"
class UITextEditSkin : public UIElementSkin
{
public:
UITextEditSkin(const std::string& name) :
UIElementSkin(name, UI::TextEdit) { }
};
#endif // UITEXTEDITSKIN_H

View File

@ -32,8 +32,8 @@
class UIWindowSkin : public UIElementSkin class UIWindowSkin : public UIElementSkin
{ {
public: public:
UIWindowSkin(const std::string& name, UI::EElementType elementType) : UIWindowSkin(const std::string& name) :
UIElementSkin(name, elementType) { } UIElementSkin(name, UI::Window) { }
void load(const YAML::Node& node); void load(const YAML::Node& node);
void draw(UIElement *element); void draw(UIElement *element);

View File

@ -82,7 +82,6 @@ void MenuState::render()
void MenuState::createMainMenu() void MenuState::createMainMenu()
{ {
UIButtonPtr button;
int y = 0; int y = 0;
m_menuPanel = UIPanelPtr(new UIPanel); m_menuPanel = UIPanelPtr(new UIPanel);
@ -93,13 +92,14 @@ void MenuState::createMainMenu()
m_menuPanel->setMargin(0, 60, 70, 0); m_menuPanel->setMargin(0, 60, 70, 0);
g_ui->addChild(m_menuPanel); g_ui->addChild(m_menuPanel);
button = UIButtonPtr(new UIButton("Enter Game")); // main menu
button->anchorTop(m_menuPanel->top()); UIButtonPtr enterGameButton = UIButtonPtr(new UIButton("Enter Game"));
button->anchorHorizontalCenter(m_menuPanel->horizontalCenter()); enterGameButton->anchorTop(m_menuPanel->top());
button->setMargin(y += 16); enterGameButton->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
m_menuPanel->addChild(button); enterGameButton->setMargin(y += 16);
m_menuPanel->addChild(enterGameButton);
button = UIButtonPtr(new UIButton("Access Account")); UIButtonPtr button = UIButtonPtr(new UIButton("Access Account"));
button->anchorTop(m_menuPanel->top()); button->anchorTop(m_menuPanel->top());
button->anchorHorizontalCenter(m_menuPanel->horizontalCenter()); button->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
button->setMargin(y += 30); button->setMargin(y += 30);
@ -122,11 +122,68 @@ void MenuState::createMainMenu()
button->anchorTop(m_menuPanel->top()); button->anchorTop(m_menuPanel->top());
button->anchorHorizontalCenter(m_menuPanel->horizontalCenter()); button->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
button->setMargin(y += 30); button->setMargin(y += 30);
button->onClick([]{ g_engine.stop(); });
m_menuPanel->addChild(button); m_menuPanel->addChild(button);
// login window
UIWindowPtr window(new UIWindow("Enter Game")); UIWindowPtr window(new UIWindow("Enter Game"));
window->setSize(Size(236, 178)); window->setSize(Size(236, 178));
window->anchorHorizontalCenter(g_ui->horizontalCenter()); window->anchorHorizontalCenter(g_ui->horizontalCenter());
window->anchorVerticalCenter(g_ui->verticalCenter()); window->anchorVerticalCenter(g_ui->verticalCenter());
window->setVisible(false);
g_ui->addChild(window); g_ui->addChild(window);
UILabelPtr label(new UILabel("Account name:"));
label->anchorLeft(window->left());
label->anchorTop(window->top());
label->setMargin(18, 33);
window->addChild(label);
label = UILabelPtr(new UILabel("Password:"));
label->anchorLeft(window->left());
label->anchorTop(window->top());
label->setMargin(18, 62);
window->addChild(label);
label = UILabelPtr(new UILabel("If you don't have\nan account yet:"));
label->anchorLeft(window->left());
label->anchorTop(window->top());
label->setMargin(18, 87);
window->addChild(label);
button = UIButtonPtr(new UIButton("Create Account"));
button->anchorLeft(window->left());
button->anchorTop(window->top());
button->setMargin(132, 94);
window->addChild(button);
button = UIButtonPtr(new UIButton("Ok"));
button->setSize(Size(43, 20));
button->anchorRight(window->right());
button->anchorBottom(window->bottom());
button->setMargin(0, 0, 10, 66);
button->onClick([window]{ window->setVisible(false); });
window->addChild(button);
button = UIButtonPtr(new UIButton("Cancel"));
button->setSize(Size(43, 20));
button->anchorRight(window->right());
button->anchorBottom(window->bottom());
button->setMargin(0, 0, 10, 13);
button->onClick([window]{ window->setVisible(false); });
window->addChild(button);
UITextEditPtr textEdit(new UITextEdit);
textEdit->anchorRight(window->right());
textEdit->anchorTop(window->top());
textEdit->setMargin(32, 0, 0, 18);
window->addChild(textEdit);
textEdit = UITextEditPtr(new UITextEdit);
textEdit->anchorRight(window->right());
textEdit->anchorTop(window->top());
textEdit->setMargin(61, 0, 0, 18);
window->addChild(textEdit);
enterGameButton->onClick([window] { window->setVisible(true); });
} }