Implement slider GUI component
This commit is contained in:
parent
d3c4e95d83
commit
f19fe48f5f
@ -153,6 +153,7 @@ if(BUILD_CLIENT)
|
|||||||
src/game/gui/layout.cpp
|
src/game/gui/layout.cpp
|
||||||
src/game/gui/line_edit.cpp
|
src/game/gui/line_edit.cpp
|
||||||
src/game/gui/message_box.cpp
|
src/game/gui/message_box.cpp
|
||||||
|
src/game/gui/slider.cpp
|
||||||
src/game/gui/style_change_listener.cpp
|
src/game/gui/style_change_listener.cpp
|
||||||
src/game/gui/style.cpp
|
src/game/gui/style.cpp
|
||||||
src/game/gui/text_edit.cpp
|
src/game/gui/text_edit.cpp
|
||||||
|
@ -141,6 +141,7 @@ ifeq ($(BUILD_CLIENT),1)
|
|||||||
src/game/gui/layout.cpp \
|
src/game/gui/layout.cpp \
|
||||||
src/game/gui/line_edit.cpp \
|
src/game/gui/line_edit.cpp \
|
||||||
src/game/gui/message_box.cpp \
|
src/game/gui/message_box.cpp \
|
||||||
|
src/game/gui/slider.cpp \
|
||||||
src/game/gui/style_change_listener.cpp \
|
src/game/gui/style_change_listener.cpp \
|
||||||
src/game/gui/style.cpp \
|
src/game/gui/style.cpp \
|
||||||
src/game/gui/text_edit.cpp \
|
src/game/gui/text_edit.cpp \
|
||||||
|
@ -24,19 +24,20 @@ SOFTWARE.
|
|||||||
|
|
||||||
#include "game/gui/container.hpp"
|
#include "game/gui/container.hpp"
|
||||||
|
|
||||||
|
#include "common/xml/parser.hpp"
|
||||||
|
#include "common/xml/xml_node.hpp"
|
||||||
|
#include "common/xml/reader.hpp"
|
||||||
#include "game/engine/engine.hpp"
|
#include "game/engine/engine.hpp"
|
||||||
#include "game/gui/layout.hpp"
|
|
||||||
#include "game/gui/box.hpp"
|
#include "game/gui/box.hpp"
|
||||||
#include "game/gui/button.hpp"
|
#include "game/gui/button.hpp"
|
||||||
#include "game/gui/chat.hpp"
|
#include "game/gui/chat.hpp"
|
||||||
#include "game/gui/combo_box.hpp"
|
#include "game/gui/combo_box.hpp"
|
||||||
#include "game/gui/label.hpp"
|
|
||||||
#include "game/gui/text_edit.hpp"
|
|
||||||
#include "game/gui/image.hpp"
|
#include "game/gui/image.hpp"
|
||||||
#include "game/gui/item_gallery.hpp"
|
#include "game/gui/item_gallery.hpp"
|
||||||
#include "common/xml/parser.hpp"
|
#include "game/gui/label.hpp"
|
||||||
#include "common/xml/xml_node.hpp"
|
#include "game/gui/layout.hpp"
|
||||||
#include "common/xml/reader.hpp"
|
#include "game/gui/text_edit.hpp"
|
||||||
|
#include "game/gui/slider.hpp"
|
||||||
|
|
||||||
using namespace polygun::gui;
|
using namespace polygun::gui;
|
||||||
|
|
||||||
@ -181,6 +182,8 @@ void Container::load_container(xml::XMLNode* node, engine::Engine* engine) {
|
|||||||
c = new TextEdit(engine, n);
|
c = new TextEdit(engine, n);
|
||||||
else if(n->get_name()=="chat")
|
else if(n->get_name()=="chat")
|
||||||
c = new Chat(engine, n);
|
c = new Chat(engine, n);
|
||||||
|
else if(n->get_name()=="slider")
|
||||||
|
c = new Slider(engine, n);
|
||||||
|
|
||||||
if(c) {
|
if(c) {
|
||||||
if(!parent_set)
|
if(!parent_set)
|
||||||
|
121
src/game/gui/slider.cpp
Normal file
121
src/game/gui/slider.cpp
Normal file
@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
PolyGun
|
||||||
|
|
||||||
|
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
|
||||||
|
|
||||||
|
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 "game/gui/slider.hpp"
|
||||||
|
|
||||||
|
#include "common/xml/reader.hpp"
|
||||||
|
#include "game/engine/engine.hpp"
|
||||||
|
#include "game/gui/container.hpp"
|
||||||
|
#include "game/gui/style.hpp"
|
||||||
|
#include "game/renderer/gui_renderer.hpp"
|
||||||
|
#include "game/renderer/texture.hpp"
|
||||||
|
#include "game/window/window.hpp"
|
||||||
|
|
||||||
|
using namespace polygun::gui;
|
||||||
|
|
||||||
|
Slider::Slider(engine::Engine* engine, Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, const SliderProp& prop) :
|
||||||
|
Component(x, y, width, height, id),
|
||||||
|
m_engine(engine),
|
||||||
|
m_properties(prop),
|
||||||
|
m_value(prop.m_min),
|
||||||
|
m_pressed(false),
|
||||||
|
m_texture_value(engine->get_master_renderer()->create_texture())
|
||||||
|
{}
|
||||||
|
|
||||||
|
Slider::Slider(engine::Engine* engine, xml::XMLNode* node) :
|
||||||
|
Component(node),
|
||||||
|
m_engine(engine),
|
||||||
|
m_properties(),
|
||||||
|
m_value(0),
|
||||||
|
m_pressed(false),
|
||||||
|
m_texture_value(engine->get_master_renderer()->create_texture())
|
||||||
|
{
|
||||||
|
xml::XMLReader reader(node);
|
||||||
|
if(!reader.get_attribute("min", m_properties.m_min))
|
||||||
|
m_properties.m_min = 0;
|
||||||
|
if(!reader.get_attribute("max", m_properties.m_max))
|
||||||
|
m_properties.m_max = 100;
|
||||||
|
m_value = m_properties.m_min;
|
||||||
|
}
|
||||||
|
|
||||||
|
Slider::~Slider() {
|
||||||
|
delete m_texture_value;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Slider::render(renderer::GUIRenderer* renderer) {
|
||||||
|
const float width = std::min(m_parent->get_right(), get_right())-get_x();
|
||||||
|
const float height = std::min(m_parent->get_bottom(), get_bottom())-get_y();
|
||||||
|
const float bar_height = height*0.3f;
|
||||||
|
const float bar_y = get_y()+(height-bar_height)/2;
|
||||||
|
const float picker_float_value = (static_cast<float>(m_value)-m_properties.m_min)/(m_properties.m_max-m_properties.m_min);
|
||||||
|
const float picker_width = get_height()/2;
|
||||||
|
const float picker_x = get_x()+picker_float_value*get_width()-picker_width/2;
|
||||||
|
const unsigned extrusion = Style::get().m_extrusion_size;
|
||||||
|
|
||||||
|
renderer->fill_rect(math::Vector2f{get_x(), bar_y}, math::Vector2f{width-extrusion, bar_height-extrusion}, subtract_from_color(Style::get().m_lower_background_color, math::RGBAColor{30, 30, 30, 0}));
|
||||||
|
renderer->fill_rect(math::Vector2f{get_x()+extrusion, bar_y+extrusion}, math::Vector2f{width-extrusion, bar_height-extrusion}, add_to_color(Style::get().m_lower_background_color, math::RGBAColor{100, 100, 100, 0}));
|
||||||
|
renderer->fill_rect(math::Vector2f{get_x()+extrusion, bar_y+extrusion}, math::Vector2f{width-extrusion*2, bar_height-extrusion*2}, Style::get().m_lower_background_color);
|
||||||
|
|
||||||
|
renderer->fill_rect(math::Vector2f{picker_x, get_y()}, math::Vector2f{picker_width-extrusion, get_height()-extrusion}, add_to_color(Style::get().m_background_color, math::RGBAColor{100, 100, 100, 0}));
|
||||||
|
renderer->fill_rect(math::Vector2f{picker_x+extrusion, get_y()+extrusion}, math::Vector2f{picker_width-extrusion, get_height()-extrusion}, subtract_from_color(Style::get().m_background_color, math::RGBAColor{30, 30, 30, 0}));
|
||||||
|
renderer->fill_rect(math::Vector2f{picker_x+extrusion, get_y()+extrusion}, math::Vector2f{picker_width-extrusion*2, get_height()-extrusion*2}, Style::get().m_background_color);
|
||||||
|
|
||||||
|
if(m_pressed)
|
||||||
|
renderer->render_texture(math::Vector2f{picker_x+picker_width/2-m_texture_value->get_width()/2, get_y()-m_texture_value->get_height()}, m_texture_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Slider::update(const window::Event& event) {
|
||||||
|
const float width = std::min(m_parent->get_right(), get_right())-get_x();
|
||||||
|
const float height = std::min(m_parent->get_bottom(), get_bottom())-get_y();
|
||||||
|
switch(event.m_type) {
|
||||||
|
case window::EventType::EVENT_TYPE_MOUSE_BUTTON_UP:
|
||||||
|
m_pressed = false;
|
||||||
|
break;
|
||||||
|
case window::EventType::EVENT_TYPE_MOUSE_BUTTON_DOWN:
|
||||||
|
if(m_engine->get_window()->is_mouse_inside_area(math::Vector2f{get_x(), get_y()}, math::Vector2f{width, height}) &&
|
||||||
|
event.m_data.m_mouse_event.m_button==window::MouseButton::MOUSE_BUTTON_LEFT && !m_inactive) {
|
||||||
|
m_engine->get_gui_master().request_highlight(this);
|
||||||
|
m_pressed = true;
|
||||||
|
calculate_value();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case window::EventType::EVENT_TYPE_MOUSE_MOVE:
|
||||||
|
if(m_pressed) {
|
||||||
|
if(m_engine->get_window()->get_mouse_x()<get_x() || m_engine->get_window()->get_mouse_x()>get_x()+width)
|
||||||
|
return true;
|
||||||
|
calculate_value();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Slider::calculate_value() {
|
||||||
|
m_value = m_properties.m_min+(m_engine->get_window()->get_mouse_x()-get_x())/get_width()*(m_properties.m_max-m_properties.m_min);
|
||||||
|
std::unique_ptr<renderer::Surface> surf = Style::get().m_font.render_text(std::to_string(m_value), Style::get().m_text_color);
|
||||||
|
m_texture_value->load_from_surface(*surf);
|
||||||
|
}
|
68
src/game/gui/slider.hpp
Normal file
68
src/game/gui/slider.hpp
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
/*
|
||||||
|
PolyGun
|
||||||
|
|
||||||
|
Copyright (c) 2024 mrkubax10 <mrkubax10@onet.pl>
|
||||||
|
|
||||||
|
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 POLYGUN_GUI_SLIDER_HPP
|
||||||
|
#define POLYGUN_GUI_SLIDER_HPP
|
||||||
|
|
||||||
|
#include "game/gui/component.hpp"
|
||||||
|
|
||||||
|
namespace polygun::engine {
|
||||||
|
class Engine;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace polygun::renderer {
|
||||||
|
class Texture;
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace polygun::gui {
|
||||||
|
struct SliderProp {
|
||||||
|
int m_min;
|
||||||
|
int m_max;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Slider final : public Component {
|
||||||
|
public:
|
||||||
|
Slider(engine::Engine* engine, Constraint* x, Constraint* y, Constraint* width, Constraint* height, const std::string& id, const SliderProp& prop);
|
||||||
|
Slider(engine::Engine* engine, xml::XMLNode* node);
|
||||||
|
virtual ~Slider() override;
|
||||||
|
|
||||||
|
void set_value(int value) { m_value = value; }
|
||||||
|
int get_value() const { return m_value; }
|
||||||
|
|
||||||
|
virtual void render(renderer::GUIRenderer* renderer) override;
|
||||||
|
virtual bool update(const window::Event& event) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
engine::Engine* m_engine;
|
||||||
|
SliderProp m_properties;
|
||||||
|
int m_value;
|
||||||
|
bool m_pressed;
|
||||||
|
renderer::Texture* m_texture_value;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void calculate_value();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // POLYGUN_GUI_SLIDER_HPP
|
Loading…
x
Reference in New Issue
Block a user