basic menu functionallity

master
Eduardo Bart 13 years ago
parent f2bdc89d8d
commit 9d1ddf34bf

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

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

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

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

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

@ -32,7 +32,8 @@
class UIButtonSkin : public UIElementSkin
{
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 draw(UIElement *element);

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

@ -23,4 +23,26 @@
#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;
}

@ -28,10 +28,23 @@
#include "../prerequisites.h"
#include "uielement.h"
class Font;
class UILabel : public UIElement
{
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

@ -28,6 +28,7 @@
#include "uielementskin.h"
#include "uibuttonskin.h"
#include "uiwindowskin.h"
#include "uitexteditskin.h"
UISkins g_uiSkins;
@ -63,7 +64,7 @@ bool UISkins::load(const std::string& file)
std::string name;
it.first() >> name;
UIButtonSkin *skin = new UIButtonSkin(name, UI::Button);
UIElementSkin *skin = new UIButtonSkin(name);
skin->load(it.second());
m_elementSkins.push_back(skin);
}
@ -87,11 +88,37 @@ bool UISkins::load(const std::string& file)
std::string name;
it.first() >> name;
UIWindowSkin *skin = new UIWindowSkin(name, UI::Window);
UIElementSkin *skin = new UIWindowSkin(name);
skin->load(it.second());
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) {
logError("Malformed font file \"%s\"", file.c_str());
return false;

@ -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;
}

@ -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

@ -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"

@ -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

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

@ -82,7 +82,6 @@ void MenuState::render()
void MenuState::createMainMenu()
{
UIButtonPtr button;
int y = 0;
m_menuPanel = UIPanelPtr(new UIPanel);
@ -93,13 +92,14 @@ void MenuState::createMainMenu()
m_menuPanel->setMargin(0, 60, 70, 0);
g_ui->addChild(m_menuPanel);
button = UIButtonPtr(new UIButton("Enter Game"));
button->anchorTop(m_menuPanel->top());
button->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
button->setMargin(y += 16);
m_menuPanel->addChild(button);
// main menu
UIButtonPtr enterGameButton = UIButtonPtr(new UIButton("Enter Game"));
enterGameButton->anchorTop(m_menuPanel->top());
enterGameButton->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
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->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
button->setMargin(y += 30);
@ -122,11 +122,68 @@ void MenuState::createMainMenu()
button->anchorTop(m_menuPanel->top());
button->anchorHorizontalCenter(m_menuPanel->horizontalCenter());
button->setMargin(y += 30);
button->onClick([]{ g_engine.stop(); });
m_menuPanel->addChild(button);
// login window
UIWindowPtr window(new UIWindow("Enter Game"));
window->setSize(Size(236, 178));
window->anchorHorizontalCenter(g_ui->horizontalCenter());
window->anchorVerticalCenter(g_ui->verticalCenter());
window->setVisible(false);
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); });
}

Loading…
Cancel
Save