Hello everyone,
Im trying to make a custom window class as subclass of PGButton but there is something wrong in my code about moving part (Positioning / Coords).
I’m not very familiar with the 2DAspect coordinate system and cant see the problem.
I just wanted to ask, do you have any idea?
GUIWindow.cpp;
#include "GUIWindow.h"
void OnMouseDown(const Event* pTheEvent, void* pData)
{
auto self = static_cast<GUIWindow*>(pData);
if (!self) { return; }
const LPoint2 mpos = self->mwatcher->get_mouse();
self->mouse_x_delta = abs(mpos.get_x() - self->window_np.get_pos().get_x());
self->mouse_y_delta = abs(mpos.get_y() - self->window_np.get_pos().get_y());
self->is_moving = true;
std::cout << "OnMouseDown" << std::endl;
}
void OnMouseUp(const Event* pTheEvent, void* pData)
{
static_cast<GUIWindow*>(pData)->is_moving = false;
std::cout << "OnMouseUp" << std::endl;
}
GUIWindow::~GUIWindow()
{
}
void GUIWindow::initEvents()
{
Engine* engine = &Engine::instance();
engine->framework.get_task_mgr().add(make_task([=](AsyncTask* task) -> AsyncTask::DoneStatus {
if (is_moving) { OnMove(); }
return AsyncTask::DS_cont;
}, "OnMove"));
engine->framework.define_key(this->get_press_event(MouseButton::one()), "button down", OnMouseDown, this);
engine->framework.define_key(this->get_release_event(MouseButton::one()), "button up", OnMouseUp, this);
}
void GUIWindow::OnMove()
{
if (!mwatcher->has_mouse()) { return; }
const LPoint2 mpos = mwatcher->get_mouse();
window_np.set_pos(mpos.get_x() - mouse_x_delta, 0, mpos.get_y() - mouse_y_delta);
std::cout << "OnMove.." << std::endl;
}
GUIWindow.h;
#pragma once
#include <stdio.h>
#include <asyncTaskManager.h>
#include <genericAsyncTask.h>
#include <texturePool.h>
#include "mouseWatcher.h"
#include "pgButton.h"
#include "Engine.h"
#include <mouseButton.h>
template<class Callable>
AsyncTask* make_task(Callable callable, const std::string& name, int sort = 0, int priority = 0) {
class InlineTask final : public AsyncTask {
public:
InlineTask(Callable callable, const std::string& name, int sort, int priority) :
AsyncTask(name),
_callable(std::move(callable)) {
_sort = sort;
_priority = priority;
}
ALLOC_DELETED_CHAIN(InlineTask);
private:
virtual DoneStatus do_task() override final {
return _callable(this);
}
Callable _callable;
};
return new InlineTask(std::move(callable), name, sort, priority);
}
class GUIWindow : public PGButton
{
public:
GUIWindow(const std::string tex_path, float _scale_factor=1.0):PGButton("GUIWindow"){
Engine* engine = &Engine::instance();
tex = TexturePool::load_texture(tex_path);
tex->set_anisotropic_degree(0);
width = tex->get_orig_file_x_size();
height = tex->get_orig_file_y_size();
scale_factor = _scale_factor;
PGFrameStyle _style = this->get_frame_style(0);
_style.set_color(1.0f, 1.0f, 1.0f, 0.95f);
_style.set_type(PGFrameStyle::T_flat);
_style.set_texture(tex);
this->clear_effects();
this->setup("", .0);
this->set_notify(NULL);
this->set_frame_style(0, _style);
this->set_frame_style(1, _style);
this->set_frame_style(2, _style);
this->set_frame_style(3, _style);
this->set_frame(((float)width / (float)height) * scale_factor, 0, 0, 1 * scale_factor);
window_np = engine->window->get_aspect_2d().attach_new_node(this);
mwatcher = DCAST(MouseWatcher, engine->window->get_mouse().node());
initEvents();
}
~GUIWindow();
void initEvents();
void OnMove();
bool is_show;
bool is_moving;
int width;
int height;
float scale_factor;
float mouse_x_delta;
float mouse_y_delta;
Texture* tex;
NodePath window_np;
MouseWatcher* mwatcher;
};
GUIWindow* test_window = new GUIWindow("bin/neuera/ui/window_bg_small.png");
Video;