|
|
|
#pragma once
|
|
|
|
#include "Menu.h"
|
|
|
|
#include "MenuComponent.h"
|
|
|
|
#include "Crawler.h"
|
|
|
|
|
|
|
|
typedef Attribute A;
|
|
|
|
|
|
|
|
class ScrollableWindowComponent:public MenuComponent{
|
|
|
|
protected:
|
|
|
|
Renderable r;
|
|
|
|
std::vector<MenuComponent*>components;
|
|
|
|
MenuComponent*upButton=nullptr;
|
|
|
|
MenuComponent*downButton=nullptr;
|
|
|
|
geom2d::rect<float>bounds; //It's for the scrollbar.
|
|
|
|
private:
|
|
|
|
inline bool OnScreen(MenuComponent*component){
|
|
|
|
return geom2d::overlaps(rect,geom2d::rect<float>{component->rect.pos+V(A::SCROLL_OFFSET),component->rect.size});
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
inline ScrollableWindowComponent(MenuType parent,geom2d::rect<float>rect,Decal*icon,MenuFunc onClick)
|
|
|
|
:MenuComponent(parent,rect,"",onClick,false){
|
|
|
|
r.Create(rect.size.x,rect.size.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual inline ~ScrollableWindowComponent(){
|
|
|
|
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
virtual inline void AfterCreate()override{
|
|
|
|
upButton=new MenuComponent(parentMenu,{vf2d{rect.size.x-12,0},{12,12}},"^",[](MenuFuncData dat){std::cout<<"Clicked"<<std::endl;});
|
|
|
|
downButton=new MenuComponent(parentMenu,{rect.size-vf2d{12,12},{12,12}},"v",[](MenuFuncData dat){});
|
|
|
|
Menu::menus[parentMenu]->AddComponent("scrollableWindowButton"+upButton->rect.pos.str()+"_"+upButton->rect.size.str(),upButton);
|
|
|
|
Menu::menus[parentMenu]->AddComponent("scrollableWindowButton"+downButton->rect.pos.str()+"_"+downButton->rect.size.str(),downButton);
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
virtual inline void Update(Crawler*game)override{
|
|
|
|
MenuComponent::Update(game);
|
|
|
|
|
|
|
|
if(game->GetMouseWheel()!=0){
|
|
|
|
if(game->GetMouseWheel()>0){
|
|
|
|
V(A::SCROLL_OFFSET).y+="ThemeGlobal.MenuScrollWheelSpeed"_I;
|
|
|
|
}else{
|
|
|
|
V(A::SCROLL_OFFSET).y-="ThemeGlobal.MenuScrollWheelSpeed"_I;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(MenuComponent*component:components){
|
|
|
|
component->disabled=!OnScreen(component);
|
|
|
|
component->_Update(game);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virtual inline void Draw(Crawler*game,vf2d parentPos,bool focused)override{
|
|
|
|
MenuComponent::Draw(game,parentPos,focused);
|
|
|
|
Sprite*prevDrawTarget=game->GetDrawTarget();
|
|
|
|
game->SetDrawTarget(r.Sprite());
|
|
|
|
game->Clear(BLANK);
|
|
|
|
for(MenuComponent*component:components){
|
|
|
|
component->_Draw(game,rect.pos+V(A::SCROLL_OFFSET),focused);
|
|
|
|
}
|
|
|
|
game->SetDrawTarget(prevDrawTarget);
|
|
|
|
game->DrawSprite(parentPos,r.Sprite());
|
|
|
|
}
|
|
|
|
virtual inline void DrawDecal(Crawler*game,vf2d parentPos,bool focused)override{
|
|
|
|
MenuComponent::DrawDecal(game,parentPos,focused);
|
|
|
|
game->DrawRectDecal(rect.pos+parentPos,rect.size);
|
|
|
|
for(MenuComponent*component:components){
|
|
|
|
component->_DrawDecal(game,rect.pos+parentPos+V(A::SCROLL_OFFSET),focused);
|
|
|
|
}
|
|
|
|
game->DrawRectDecal(rect.pos+parentPos+vf2d{rect.size.x-12,0},{12,12});
|
|
|
|
game->DrawRectDecal(rect.pos+parentPos+vf2d{rect.size.x-12,12},{12,rect.size.y-24});
|
|
|
|
game->DrawRectDecal(rect.pos+parentPos+rect.size-vf2d{12,12},{12,12});
|
|
|
|
}
|
|
|
|
virtual bool GetHoverState(Crawler*game,MenuComponent*child)override{
|
|
|
|
return geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos+child->rect.pos+V(A::SCROLL_OFFSET),child->rect.size},game->GetMousePos());
|
|
|
|
}
|
|
|
|
public:
|
|
|
|
void inline AddComponent(Menu*parentMenu,std::string key,MenuComponent*button){
|
|
|
|
components.push_back(button);
|
|
|
|
button->renderInMain=false; //Now we are in control!
|
|
|
|
button->parentComponent=this;
|
|
|
|
|
|
|
|
if(button->rect.pos.x<bounds.pos.x){
|
|
|
|
float sizeIncrease=bounds.pos.x-button->rect.pos.x;
|
|
|
|
bounds.size.x+=sizeIncrease;
|
|
|
|
bounds.pos.x=button->rect.pos.x;
|
|
|
|
}
|
|
|
|
if(button->rect.right().start.x>bounds.right().start.x){
|
|
|
|
float sizeIncrease=button->rect.right().start.x-bounds.right().start.x;
|
|
|
|
bounds.size.x+=sizeIncrease;
|
|
|
|
}
|
|
|
|
if(button->rect.pos.y<bounds.pos.y){
|
|
|
|
float sizeIncrease=bounds.pos.y-button->rect.pos.y;
|
|
|
|
bounds.size.y+=sizeIncrease;
|
|
|
|
bounds.pos.y=button->rect.pos.y;
|
|
|
|
}
|
|
|
|
if(button->rect.bottom().start.y>bounds.bottom().start.y){
|
|
|
|
float sizeIncrease=button->rect.bottom().start.y-bounds.bottom().start.y;
|
|
|
|
bounds.size.y+=sizeIncrease;
|
|
|
|
}
|
|
|
|
|
|
|
|
parentMenu->AddComponent(key,button);
|
|
|
|
}
|
|
|
|
virtual inline bool PointWithinParent(MenuComponent*child,vi2d drawPos)override{
|
|
|
|
return geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos,rect.size},drawPos);
|
|
|
|
}
|
|
|
|
};
|