diff --git a/CMakeLists.txt b/CMakeLists.txt index 420f264c..aadf06dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,8 +53,10 @@ SET(SOURCES # game sources src/main.cpp src/menustate.cpp + src/teststate.cpp # framework sources + src/framework/dispatcher.cpp src/framework/framebuffer.cpp src/framework/font.cpp src/framework/fonts.cpp @@ -68,6 +70,14 @@ SET(SOURCES src/framework/logger.cpp src/framework/util.cpp +# ui + src/framework/ui/uielement.cpp + src/framework/ui/uicontainer.cpp + src/framework/ui/uipanel.cpp + src/framework/ui/uibutton.cpp + src/framework/ui/uilabel.cpp + +# network src/framework/net/connection.cpp src/framework/net/connections.cpp) diff --git a/src/framework/dispatcher.cpp b/src/framework/dispatcher.cpp new file mode 100644 index 00000000..b2364670 --- /dev/null +++ b/src/framework/dispatcher.cpp @@ -0,0 +1,51 @@ +/* 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 "dispatcher.h" +#include "platform.h" + +Dispatcher g_dispatcher; + +void Dispatcher::poll(int ticks) +{ + while(!m_taskList.empty()) { + Task *task = m_taskList.top(); + if(ticks < task->ticks) + break; + + task->callback(); + delete task; + m_taskList.pop(); + } +} + +void Dispatcher::scheduleTask(const Callback& callback, int delay) +{ + m_taskList.push(new Task(Platform::getTicks() + delay, callback)); +} + +void Dispatcher::addTask(const Callback& callback) +{ + m_taskList.push(new Task(callback)); +} diff --git a/src/framework/dispatcher.h b/src/framework/dispatcher.h new file mode 100644 index 00000000..5aa8205b --- /dev/null +++ b/src/framework/dispatcher.h @@ -0,0 +1,64 @@ +/* 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 DISPATCHER_H +#define DISPATCHER_H + +#include "prerequisites.h" + +#include + +typedef std::function Callback; + +class Task { +public: + inline Task(const Callback& _callback) : ticks(0), callback(_callback) { } + inline Task(int _ticks, const Callback& _callback) : ticks(_ticks), callback(_callback) { } + inline bool operator<(const Task& other) const { return ticks > other.ticks; } + int ticks; + Callback callback; +}; + +class lessTask : public std::binary_function { +public: + bool operator()(Task*& t1,Task*& t2) { return (*t1) < (*t2); } +}; + +class Dispatcher +{ +public: + Dispatcher() { } + + void poll(int ticks); + + void addTask(const Callback& callback); + void scheduleTask(const Callback& callback, int delay); + +private: + std::priority_queue, lessTask> m_taskList; +}; + +extern Dispatcher g_dispatcher; + +#endif // DISPATCHER_H diff --git a/src/framework/engine.cpp b/src/framework/engine.cpp index f7b8a1ac..8b4f0355 100644 --- a/src/framework/engine.cpp +++ b/src/framework/engine.cpp @@ -29,7 +29,9 @@ #include "input.h" #include "configs.h" #include "gamestate.h" +#include "dispatcher.h" #include "net/connections.h" +#include "ui/uicontainer.h" #define MINIMUN_UPDATE_DELAY 50 @@ -40,9 +42,6 @@ void Engine::init() // initialize stuff g_graphics.init(); g_fonts.init(); - - // finally show the window - onResize(Platform::getWindowWidth(), Platform::getWindowHeight()); } void Engine::terminate() @@ -72,7 +71,7 @@ void Engine::run() Point fpsPos(10,10); while(!m_stopping) { - // fire platform events + // poll platform events Platform::poll(); // poll network events @@ -80,6 +79,10 @@ void Engine::run() // update before redering ticks = Platform::getTicks(); + + // poll diaptcher tasks + g_dispatcher.poll(ticks); + updateElapsedTicks = ticks - lastUpdateTicks; if(updateElapsedTicks >= MINIMUN_UPDATE_DELAY) { update(ticks, updateElapsedTicks); @@ -133,6 +136,7 @@ void Engine::render() g_graphics.beginRender(); if(m_currentState) m_currentState->render(); + g_gui.render(); g_graphics.endRender(); } @@ -140,6 +144,7 @@ void Engine::update(int ticks, int elapsedTicks) { if(m_currentState) m_currentState->update(ticks, elapsedTicks); + g_gui.update(ticks, elapsedTicks); } void Engine::onClose() @@ -148,13 +153,18 @@ void Engine::onClose() m_currentState->onClose(); } -void Engine::onResize(int width, int height) +void Engine::onResize(const Size& size) { - g_graphics.resize(width, height); + g_graphics.resize(size); + g_gui.resize(size); } void Engine::onInputEvent(InputEvent *event) { - if(m_currentState) - m_currentState->onInputEvent(event); + // inputs goest to gui first + if(!g_gui.onInputEvent(event)) { + // if gui didnt capture the input then goes to the state + if(m_currentState) + m_currentState->onInputEvent(event); + } } diff --git a/src/framework/engine.h b/src/framework/engine.h index 99e9b9a1..7f2063dd 100644 --- a/src/framework/engine.h +++ b/src/framework/engine.h @@ -26,6 +26,7 @@ #define ENGINE_H #include "prerequisites.h" +#include "size.h" struct InputEvent; @@ -57,7 +58,7 @@ public: /// Fired by platform on window close void onClose(); /// Fired by platform on window resize - void onResize(int width, int height); + void onResize(const Size& size); /// Fired by platform on mouse/keyboard input void onInputEvent(InputEvent *event); diff --git a/src/framework/font.cpp b/src/framework/font.cpp index 8c7dddae..fc5149b3 100644 --- a/src/framework/font.cpp +++ b/src/framework/font.cpp @@ -273,7 +273,7 @@ Point* Font::calculateGlyphsPositions(const std::string& text, int align, Size * } -Size Font::calculateTextBoxSize(const std::string& text) +Size Font::calculateTextRectSize(const std::string& text) { Size size; calculateGlyphsPositions(text, ALIGN_TOP_LEFT, &size); diff --git a/src/framework/font.h b/src/framework/font.h index 43b6d680..66cc0e00 100644 --- a/src/framework/font.h +++ b/src/framework/font.h @@ -43,7 +43,7 @@ enum EAlign { ALIGN_BOTTOM_RIGHT = ALIGN_BOTTOM | ALIGN_RIGHT, ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT }; - + class Font { public: @@ -71,12 +71,7 @@ public: Point *calculateGlyphsPositions(const std::string& text, int align = ALIGN_TOP_LEFT, Size *textBoxSize = NULL); /// Simulate render and calculate text size - Size calculateTextBoxSize(const std::string& text); - - /* - /// Render a text inside a rect - void renderText(const Rect& screenCoords, EAlign align, const std::string& text); - */ + Size calculateTextRectSize(const std::string& text); private: int m_lineHeight; diff --git a/src/framework/graphics.cpp b/src/framework/graphics.cpp index dd2013ec..34d3f546 100644 --- a/src/framework/graphics.cpp +++ b/src/framework/graphics.cpp @@ -81,10 +81,9 @@ bool Graphics::isExtensionSupported(const char *extension) return 0; } -void Graphics::resize(int width, int height) +void Graphics::resize(const Size& size) { - m_screenSize.setWidth(width); - m_screenSize.setHeight(height); + m_screenSize = size; restoreViewport(); } diff --git a/src/framework/graphics.h b/src/framework/graphics.h index 2ecc4730..a61ab3e6 100644 --- a/src/framework/graphics.h +++ b/src/framework/graphics.h @@ -44,7 +44,7 @@ public: bool isExtensionSupported(const char *extension); /// Called after every window resize - void resize(int width, int height); + void resize(const Size& size); /// Restore original viewport void restoreViewport(); diff --git a/src/framework/ui/ui.h b/src/framework/ui/ui.h new file mode 100644 index 00000000..f2f05011 --- /dev/null +++ b/src/framework/ui/ui.h @@ -0,0 +1,37 @@ +/* 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 UI_H +#define UI_H + +#include "../prerequisites.h" + +#include "uiconstants.h" +#include "uielement.h" +#include "uicontainer.h" +#include "uipanel.h" +#include "uibutton.h" +#include "uilabel.h" + +#endif // UI_H diff --git a/src/framework/ui/uibutton.cpp b/src/framework/ui/uibutton.cpp new file mode 100644 index 00000000..3c858aea --- /dev/null +++ b/src/framework/ui/uibutton.cpp @@ -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 "uibutton.h" + diff --git a/src/framework/ui/uibutton.h b/src/framework/ui/uibutton.h new file mode 100644 index 00000000..ed57cddb --- /dev/null +++ b/src/framework/ui/uibutton.h @@ -0,0 +1,39 @@ +/* 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 UIBUTTON_H +#define UIBUTTON_H + +#include "../prerequisites.h" +#include "uielement.h" + +class UIButton : public UIElement +{ +public: + UIButton(UIContainerPtr parent) : UIElement(parent) { } + + virtual UI::ControlType getControlType() const { return UI::Button; } +}; + +#endif // UIBUTTON_H diff --git a/src/framework/ui/uiconstants.h b/src/framework/ui/uiconstants.h new file mode 100644 index 00000000..7aafffeb --- /dev/null +++ b/src/framework/ui/uiconstants.h @@ -0,0 +1,67 @@ +/* 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 UICONSTANTS_H +#define UICONSTANTS_H + +namespace UI { + enum ButtonState + { + Up, + Down, + MouseOver + }; + + enum ButtonEvent + { + PressUp, + PressDown, + EnterMouseOver, + LeaveMouseOver + }; + + enum MessageBoxFlags + { + Ok = 1 << 0, + Cancel = 1 << 1, + Yes = 1 << 2, + No = 1 << 3, + OkCancel = Ok | Cancel, + YesNo = Yes | No + }; + + enum ControlType + { + Element, + Container, + Panel, + Window, + Label, + TextBox, + Button, + CheckBox + }; +} + +#endif // UICONSTANTS_H diff --git a/src/framework/ui/uicontainer.cpp b/src/framework/ui/uicontainer.cpp new file mode 100644 index 00000000..f2e8f5de --- /dev/null +++ b/src/framework/ui/uicontainer.cpp @@ -0,0 +1,100 @@ +/* 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 "uicontainer.h" + +UIContainer g_gui; + +void UIContainer::addChild(UIElementPtr child) +{ + m_children.push_back(child); + child->setParent(asUIContainer()); + + // adjust child rect + Rect childRect = child->getRect(); + childRect.translate(m_rect.topLeft()); + child->setRect(childRect); +} + +void UIContainer::removeChild(UIElementPtr child) +{ + if(m_activeElement == child) + setActiveElement(UIElementPtr()); + m_children.remove(child); +} + +UIElementPtr UIContainer::getChildByName(const std::string& name) const +{ + for(auto it = m_children.begin(); it != m_children.end(); ++it) { + if((*it)->getName() == name) { + return (*it); + break; + } + } + return UIElementPtr(); +} + + +void UIContainer::setRect(const Rect& rect) +{ + // update children rect + for(auto it = m_children.begin(); it != m_children.end(); ++it) { + UIElementPtr child = (*it)->asUIElement(); + + // transforme child rect + Rect childRect = child->getRect(); + childRect.translate(-m_rect.topLeft()); + childRect.translate(rect.topLeft()); + child->setRect(childRect); + } + + m_rect = rect; +} + +void UIContainer::resize(const Size& size) +{ + Rect newRect = m_rect; + newRect.setSize(size); + setRect(newRect); +} + +void UIContainer::move(const Point& trans) +{ + Rect newRect = m_rect; + newRect.translate(trans); + setRect(newRect); +} + +void UIContainer::moveTo(const Point& pos) +{ + Rect newRect = m_rect; + newRect.moveTo(pos); + setRect(newRect); + +} + +void UIContainer::setActiveElement(UIElementPtr activeElement) +{ + +} diff --git a/src/framework/ui/uicontainer.h b/src/framework/ui/uicontainer.h new file mode 100644 index 00000000..2d332988 --- /dev/null +++ b/src/framework/ui/uicontainer.h @@ -0,0 +1,68 @@ +/* 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 UICONTAINER_H +#define UICONTAINER_H + +#include "../prerequisites.h" +#include "uielement.h" +#include "../point.h" +#include "../rect.h" + +class UIContainer : public UIElement +{ +public: + UIContainer(UIContainerPtr parent = UIContainerPtr()) : UIElement(parent) { } + virtual ~UIContainer() { } + + virtual void addChild(UIElementPtr child); + virtual void removeChild(UIElementPtr child); + virtual UIElementPtr getChildByName(const std::string& name) const; + + virtual void setRect(const Rect& rect); + virtual void resize(const Size& size); + virtual void move(const Point& trans); + virtual void moveTo(const Point& pos); + + virtual void render() { } + virtual void update(int ticks, int elapsedTicks) { } + virtual bool onInputEvent(InputEvent *event) { return false; } + + virtual void setActiveElement(UIElementPtr activeElement); + UIElementPtr getActiveElement() const { return m_activeElement; } + + virtual UI::ControlType getControlType() const { return UI::Container; } + UIContainerPtr asUIContainer() { return UIContainerPtr(this); } + +protected: + std::list m_children; + UIElementPtr m_activeElement; + +private: + void onMove(const Point& pos); +}; + +extern UIContainer g_gui; + +#endif // UICONTAINER_H diff --git a/src/framework/ui/uielement.cpp b/src/framework/ui/uielement.cpp new file mode 100644 index 00000000..af19f49c --- /dev/null +++ b/src/framework/ui/uielement.cpp @@ -0,0 +1,36 @@ +/* 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 "uielement.h" +#include "uicontainer.h" + +UIElement::UIElement(UIContainerPtr parent) : + m_visible(true), + m_active(true) +{ + if(parent) + parent->addChild(asUIElement()); +} + + diff --git a/src/framework/ui/uielement.h b/src/framework/ui/uielement.h new file mode 100644 index 00000000..3ccc3426 --- /dev/null +++ b/src/framework/ui/uielement.h @@ -0,0 +1,77 @@ +/* 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 UIELEMENT_H +#define UIELEMENT_H + +#include "../prerequisites.h" +#include "../input.h" +#include "../rect.h" +#include "uiconstants.h" + +class UIContainer; +typedef std::shared_ptr UIContainerPtr; + +class UIElement; +typedef std::shared_ptr UIElementPtr; + +class UIElement +{ +public: + UIElement(UIContainerPtr parent); + virtual ~UIElement() { } + + virtual void render() { } + virtual void update(int ticks, int elapsedTicks) { } + virtual bool onInputEvent(InputEvent *event) { return false; } + + virtual void setParent(UIContainerPtr parent) { m_parent = parent; } + UIContainerPtr getParent() const { return m_parent; } + + virtual void setName(const std::string& name) { m_name = name; } + const std::string& getName() const { return m_name; } + + virtual void setRect(const Rect& rect) { m_rect = rect; } + const Rect& getRect() const{ return m_rect; } + + virtual void setActive(bool active) { m_active = active; } + bool isActive() const { return m_active; } + + virtual void setVisible(bool visible) { m_visible = visible; } + bool isVisible() const { return m_visible; } + + virtual UI::ControlType getControlType() const { return UI::Element; } + + UIElementPtr asUIElement() { return UIElementPtr(this); } + +protected: + UI::ControlType m_type; + UIContainerPtr m_parent; + Rect m_rect; + std::string m_name; + bool m_visible; + bool m_active; +}; + +#endif // UIELEMENT_H diff --git a/src/framework/ui/uilabel.cpp b/src/framework/ui/uilabel.cpp new file mode 100644 index 00000000..e1cc5048 --- /dev/null +++ b/src/framework/ui/uilabel.cpp @@ -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 "uilabel.h" + diff --git a/src/framework/ui/uilabel.h b/src/framework/ui/uilabel.h new file mode 100644 index 00000000..214766b4 --- /dev/null +++ b/src/framework/ui/uilabel.h @@ -0,0 +1,39 @@ +/* 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 UILABEL_H +#define UILABEL_H + +#include "../prerequisites.h" +#include "uielement.h" + +class UILabel : public UIElement +{ +public: + UILabel(UIContainerPtr parent) : UIElement(parent) { } + + virtual UI::ControlType getControlType() const { return UI::Label; } +}; + +#endif // UILABEL_H diff --git a/src/framework/ui/uipanel.cpp b/src/framework/ui/uipanel.cpp new file mode 100644 index 00000000..f21ef43d --- /dev/null +++ b/src/framework/ui/uipanel.cpp @@ -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 "uipanel.h" + diff --git a/src/framework/ui/uipanel.h b/src/framework/ui/uipanel.h new file mode 100644 index 00000000..d70be61a --- /dev/null +++ b/src/framework/ui/uipanel.h @@ -0,0 +1,39 @@ +/* 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 UIPANEL_H +#define UIPANEL_H + +#include "../prerequisites.h" +#include "uicontainer.h" + +class UIPanel : public UIContainer +{ +public: + UIPanel(UIContainerPtr parent) : UIContainer(parent) { } + + virtual UI::ControlType getControlType() const { return UI::Panel; } +}; + +#endif // UIPANEL_H diff --git a/src/framework/win32platform.cpp b/src/framework/win32platform.cpp index a9e1dcd1..3606ddcb 100644 --- a/src/framework/win32platform.cpp +++ b/src/framework/win32platform.cpp @@ -23,6 +23,7 @@ #include "platform.h" #include "engine.h" +#include "size.h" #include #include @@ -411,7 +412,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) win32.height = HIWORD(lParam); } - g_engine.onResize(LOWORD(lParam), HIWORD(lParam)); + g_engine.onResize(Size(LOWORD(lParam), HIWORD(lParam))); break; } default: diff --git a/src/framework/x11platform.cpp b/src/framework/x11platform.cpp index 0c3447be..9455b685 100644 --- a/src/framework/x11platform.cpp +++ b/src/framework/x11platform.cpp @@ -26,6 +26,7 @@ #include "engine.h" #include "input.h" #include "logger.h" +#include "size.h" #include #include @@ -300,7 +301,7 @@ void Platform::poll() static int oldWidth = -1; static int oldHeight = -1; if(oldWidth != event.xconfigure.width || oldHeight != event.xconfigure.height) { - g_engine.onResize(event.xconfigure.width, event.xconfigure.height); + g_engine.onResize(Size(event.xconfigure.width, event.xconfigure.height)); oldWidth = event.xconfigure.width; oldHeight = event.xconfigure.height; } diff --git a/src/main.cpp b/src/main.cpp index a16149bd..e94ece8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,6 +27,7 @@ #include "framework/resources.h" #include "framework/platform.h" #include "menustate.h" +#include "teststate.h" /// Catches signals so we can exit nicely void signal_handler(int sig) @@ -108,8 +109,9 @@ int main(int argc, const char *argv[]) // state scope { - std::shared_ptr menuState(new MenuState); - g_engine.changeState(menuState.get()); + std::shared_ptr initialState(new MenuState); + //std::shared_ptr initialState(new TestState); + g_engine.changeState(initialState.get()); Platform::showWindow(); //Platform::hideMouseCursor(); diff --git a/src/menustate.cpp b/src/menustate.cpp index 19e09ea4..eea94691 100644 --- a/src/menustate.cpp +++ b/src/menustate.cpp @@ -31,17 +31,28 @@ #include "framework/rect.h" #include "framework/fonts.h" #include "framework/input.h" +#include "framework/dispatcher.h" +#include "framework/ui/ui.h" #include "framework/net/connections.h" -TexturePtr background; - void MenuState::onEnter() { m_background = g_textures.get("background.png"); m_background->enableBilinearFilter(); - - m_connection = g_connections.createConnection(); - m_connection->connect("www.google.com.br", 80); + + /* + UIPanelPtr panel(new UIPanel); + panel.setAnchorsLeft(g_gui.left()); + panel.setAnchorsBottom(g_gui.right()); + panel.setMarginBottom(10); + panel.setMarginLeft(10); + panel.setSize(Size(100, 100)); + + UIButtonPtr button(new UIButton); + button.setAnchorsHorizontalCenter(panel.horizontalCenter()); + button.setTop + g_gui.addChild(panel); + */ } void MenuState::onLeave() @@ -54,55 +65,27 @@ void MenuState::onClose() g_engine.stop(); } -int x, y; void MenuState::onInputEvent(InputEvent* event) { - static bool moving = false; - static int lastX; - static int lastY; - if(event->type == EV_MOUSE_LDOWN) { - moving = true; - lastX = event->mouse.x; - lastY = event->mouse.y; - } else if(event->type == EV_MOUSE_LUP) { - moving = false; - } else if(event->type == EV_MOUSE_MOVE) { - if(moving) { - x = lastX - event->mouse.x; - y = lastY - event->mouse.y; - } - } + } void MenuState::render() { + // render background static Size minTexCoordsSize(1240, 880); const Size& screenSize = g_graphics.getScreenSize(); const Size& texSize = m_background->getSize(); - Size texCoordsSize = screenSize; if(texCoordsSize < minTexCoordsSize) texCoordsSize.scale(minTexCoordsSize, KEEP_ASPECT_RATIO_BY_EXPANDING); texCoordsSize = texCoordsSize.boundedTo(texSize); - Rect texCoords(0, 0, texCoordsSize); texCoords.moveBottomRight(texSize.toPoint()); g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords); - - static const char *text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\n" - "Nulla pulvinar odio ac arcu tempor consequat.\n" - "Praesent at enim sapien, at vestibulum ligula.\n" - "Aliquam eleifend ante eu sapien vehicula consectetur.\n" - "Nunc id ligula ligula, eget vestibulum magna.\n" - "In mattis nisi non nisl semper ultricies."; - Size textSize = g_defaultFont->calculateTextBoxSize(text); - g_defaultFont->renderText(text, Rect(100, 100, textSize.width()-30, textSize.height()-30), ALIGN_CENTER, Point(x,y), true); } void MenuState::update(int ticks, int elapsedTicks) { - if(m_connection->getLastError()){ - logError("%s", m_connection->getLastError().message().c_str()); - m_connection->resetLastError(); - } + } diff --git a/src/menustate.h b/src/menustate.h index 5b46f035..6c310c00 100644 --- a/src/menustate.h +++ b/src/menustate.h @@ -46,8 +46,6 @@ public: private: TexturePtr m_background; - ConnectionPtr m_connection; - int m_connectionTicks; }; #endif // MENUSTATE_H diff --git a/src/teststate.cpp b/src/teststate.cpp new file mode 100644 index 00000000..4d160dc6 --- /dev/null +++ b/src/teststate.cpp @@ -0,0 +1,60 @@ +/* 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 "teststate.h" +#include "framework/graphics.h" +#include "framework/logger.h" +#include "framework/engine.h" +#include "framework/input.h" + +void TestState::onEnter() +{ + +} + +void TestState::onLeave() +{ + +} + +void TestState::onClose() +{ + g_engine.stop(); +} + +void TestState::onInputEvent(InputEvent* event) +{ + +} + +void TestState::render() +{ + +} + +void TestState::update(int ticks, int elapsedTicks) +{ + +} + diff --git a/src/teststate.h b/src/teststate.h new file mode 100644 index 00000000..ac294069 --- /dev/null +++ b/src/teststate.h @@ -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. + */ + + +#ifndef TESTSTATE_H +#define TESTSTATE_H + +#include "framework/gamestate.h" + +class TestState : public GameState +{ +public: + TestState() { } + + virtual void onEnter(); + virtual void onLeave(); + + virtual void onClose(); + virtual void onInputEvent(InputEvent *event); + + virtual void render(); + virtual void update(int ticks, int elapsedTicks); +}; + +#endif // TESTSTATE_H