The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'!
https://forums.lestoria.net
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
272 lines
13 KiB
272 lines
13 KiB
1 year ago
|
#pragma region License
|
||
1 year ago
|
/*
|
||
|
License (OLC-3)
|
||
|
~~~~~~~~~~~~~~~
|
||
|
|
||
11 months ago
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
||
1 year ago
|
|
||
|
Redistribution and use in source and binary forms, with or without modification,
|
||
|
are permitted provided that the following conditions are met:
|
||
|
|
||
|
1. Redistributions or derivations of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
|
||
|
2. Redistributions or derivative works in binary form must reproduce the above
|
||
|
copyright notice. This list of conditions and the following disclaimer must be
|
||
|
reproduced in the documentation and/or other materials provided with the distribution.
|
||
|
|
||
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
||
|
be used to endorse or promote products derived from this software without specific
|
||
|
prior written permission.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
||
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
||
|
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
||
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
||
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
|
SUCH DAMAGE.
|
||
1 year ago
|
|
||
|
Portions of this software are copyright © 2023 The FreeType
|
||
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
||
|
All rights reserved.
|
||
1 year ago
|
*/
|
||
1 year ago
|
#pragma endregion
|
||
1 year ago
|
#pragma once
|
||
|
#include "Menu.h"
|
||
|
#include "MenuComponent.h"
|
||
1 year ago
|
#include "MenuItemButton.h"
|
||
11 months ago
|
#include "AdventuresInLestoria.h"
|
||
1 year ago
|
|
||
12 months ago
|
using A=Attribute;
|
||
1 year ago
|
|
||
1 year ago
|
class ScrollableWindowComponent:public MenuComponent{
|
||
|
protected:
|
||
12 months ago
|
ViewPort subWindow;
|
||
1 year ago
|
std::vector<MenuComponent*>components;
|
||
1 year ago
|
MenuComponent*upButton=nullptr;
|
||
|
MenuComponent*downButton=nullptr;
|
||
1 year ago
|
geom2d::rect<float>bounds; //It's for the scrollbar.
|
||
1 year ago
|
float scrollBarHeight=0;
|
||
|
float scrollBarTop=0;
|
||
|
bool scrollBarSelected=false;
|
||
|
float scrollBarHoverTime=0;
|
||
12 months ago
|
vf2d scrollOffset;
|
||
1 year ago
|
protected:
|
||
1 year ago
|
inline bool OnScreen(MenuComponent*component){
|
||
12 months ago
|
return geom2d::overlaps(geom2d::rect<float>{{},rect.size},geom2d::rect<float>{component->rect.pos+vf2d{2,2},component->rect.size-vf2d{2,2}});
|
||
1 year ago
|
}
|
||
1 year ago
|
public:
|
||
12 months ago
|
inline ScrollableWindowComponent(geom2d::rect<float>rect,ComponentAttr attributes=ComponentAttr::BACKGROUND|ComponentAttr::OUTLINE)
|
||
|
:MenuComponent(rect,"",[](MenuFuncData data){return true;},ButtonAttr::UNSELECTABLE|ButtonAttr::UNSELECTABLE_VIA_KEYBOARD){
|
||
1 year ago
|
background=attributes&ComponentAttr::BACKGROUND;
|
||
|
border=attributes&ComponentAttr::OUTLINE;
|
||
1 year ago
|
}
|
||
1 year ago
|
virtual inline void RemoveAllComponents(){
|
||
12 months ago
|
while(components.size()>0){
|
||
|
RemoveButton(components.back());
|
||
1 year ago
|
}
|
||
|
}
|
||
|
virtual inline void RemoveButton(MenuComponent*button){
|
||
12 months ago
|
std::vector<MenuComponent*>&buttonList=Menu::menus[button->parentMenu]->buttons.at(int(button->originalPos.y));
|
||
|
std::vector<MenuComponent*>&keyboardButtonList=Menu::menus[button->parentMenu]->keyboardButtons.at(int(button->originalPos.y));
|
||
1 year ago
|
size_t removedCount=0;
|
||
|
removedCount+=std::erase(buttonList,button);
|
||
|
removedCount+=std::erase(keyboardButtonList,button);
|
||
11 months ago
|
removedCount+=Menu::menus[button->parentMenu]->components.erase(button->GetName());
|
||
|
if(removedCount!=3){
|
||
1 year ago
|
std::cout<<"WARNING! Attempted to remove buttons from button listing, but not found!";
|
||
|
}
|
||
|
if(buttonList.size()==0){
|
||
12 months ago
|
if(!Menu::menus[button->parentMenu]->buttons.erase(int(button->originalPos.y))){
|
||
|
ERR("WARNING! Attempted to erase key "<<button->originalPos.y<<" from button map, but the list still exists!")
|
||
1 year ago
|
}
|
||
|
}
|
||
|
if(keyboardButtonList.size()==0){
|
||
12 months ago
|
if(!Menu::menus[button->parentMenu]->keyboardButtons.erase(int(button->originalPos.y))){
|
||
|
ERR("WARNING! Attempted to erase key "<<button->originalPos.y<<" from button map, but the list still exists!")
|
||
1 year ago
|
}
|
||
|
}
|
||
11 months ago
|
auto componentSearchResults=std::find(components.begin(),components.end(),button);
|
||
|
if(componentSearchResults==components.end())ERR("Could not find Component"<<std::quoted(button->GetName())<<" inside the component list!");
|
||
|
components.erase(componentSearchResults);
|
||
12 months ago
|
Menu::menus[button->parentMenu]->RecalculateComponentCount();
|
||
1 year ago
|
delete button;
|
||
12 months ago
|
CalculateBounds();
|
||
1 year ago
|
}
|
||
12 months ago
|
virtual inline void SetScrollAmount(vf2d scrollOffset){
|
||
|
this->scrollOffset=scrollOffset;
|
||
|
for(MenuComponent*component:components){
|
||
|
component->rect.pos=component->originalPos+scrollOffset;
|
||
|
}
|
||
|
}
|
||
|
virtual inline vf2d GetScrollAmount(){
|
||
|
return scrollOffset;
|
||
|
}
|
||
1 year ago
|
protected:
|
||
12 months ago
|
virtual inline void AfterCreate()override{
|
||
|
//Let's use the internal name of this component to add unique names for sub-components.
|
||
12 months ago
|
upButton=Menu::menus[parentMenu]->ADD(name+vf2d(rect.pos+vf2d{rect.size.x-12,0}).str()+"_"+vf2d(12,12).str(),MenuComponent)({rect.pos+vf2d{rect.size.x-12,0},{12,12}},"^",[&](MenuFuncData dat){SetScrollAmount(GetScrollAmount()+vf2d{0,"ThemeGlobal.MenuButtonScrollSpeed"_F});return true;},ButtonAttr::UNSELECTABLE_VIA_KEYBOARD)DEPTH depth-1 END;
|
||
|
downButton=Menu::menus[parentMenu]->ADD(name+vf2d(rect.pos+rect.size-vf2d{12,12}).str()+"_"+vf2d(12,12).str(),MenuComponent)({rect.pos+rect.size-vf2d{12,12},{12,12}},"v",[&](MenuFuncData dat){SetScrollAmount(GetScrollAmount()-vf2d{0,"ThemeGlobal.MenuButtonScrollSpeed"_F});return true;},ButtonAttr::UNSELECTABLE_VIA_KEYBOARD)DEPTH depth-1 END;
|
||
12 months ago
|
subWindow=ViewPort::rectViewPort({},rect.size,Menu::menus[parentMenu]->pos+rect.pos);
|
||
|
if(upButton){upButton->Enable(!disabled);}
|
||
|
if(downButton){downButton->Enable(!disabled);}
|
||
12 months ago
|
}
|
||
11 months ago
|
virtual inline void BeforeUpdate(AiL*game)override{
|
||
12 months ago
|
MenuComponent::BeforeUpdate(game);
|
||
12 months ago
|
for(MenuComponent*component:components){
|
||
12 months ago
|
component->_BeforeUpdate(game);
|
||
12 months ago
|
}
|
||
|
}
|
||
11 months ago
|
virtual inline void Update(AiL*game)override{
|
||
1 year ago
|
MenuComponent::Update(game);
|
||
1 year ago
|
|
||
1 year ago
|
vf2d windowAbsPos=Menu::menus[parentMenu]->pos+rect.pos;
|
||
|
|
||
|
bool mouseOverScrollbar=geom2d::overlaps(geom2d::rect<float>(windowAbsPos+vf2d{rect.size.x-12,scrollBarTop+12},{12,scrollBarHeight}),game->GetMousePos());
|
||
|
|
||
|
if(mouseOverScrollbar||scrollBarSelected){
|
||
|
scrollBarHoverTime=std::min(scrollBarHoverTime+game->GetElapsedTime(),"ThemeGlobal.HighlightTime"_F);
|
||
11 months ago
|
if(game->GetMouse(0).bPressed&&!geom2d::contains(rect,bounds)){
|
||
1 year ago
|
scrollBarSelected=true;
|
||
|
}
|
||
|
if(game->GetMouse(0).bReleased){
|
||
|
scrollBarSelected=false;
|
||
|
}
|
||
|
if(scrollBarSelected){
|
||
|
float spaceBetweenTopAndBottomArrows=rect.size.y-24;
|
||
|
float viewHeight=rect.size.y;
|
||
|
|
||
|
float totalContentHeight=bounds.size.y;
|
||
1 year ago
|
if(totalContentHeight==0)totalContentHeight=1;
|
||
1 year ago
|
float scrollBarScale=(spaceBetweenTopAndBottomArrows/totalContentHeight);
|
||
|
//The scroll amount moves centered on the position the mouse is at.
|
||
|
float newScrollbarTop=(game->GetMousePos().y-windowAbsPos.y-12)-scrollBarHeight/2;
|
||
12 months ago
|
SetScrollAmount({GetScrollAmount().x,(-newScrollbarTop+1)/scrollBarScale});
|
||
1 year ago
|
}
|
||
|
}else{
|
||
|
scrollBarHoverTime=std::max(scrollBarHoverTime-game->GetElapsedTime(),0.f);
|
||
|
}
|
||
|
|
||
1 year ago
|
if(game->GetMouseWheel()!=0){
|
||
|
if(game->GetMouseWheel()>0){
|
||
12 months ago
|
SetScrollAmount(GetScrollAmount()+vf2d{0,"ThemeGlobal.MenuScrollWheelSpeed"_F});
|
||
1 year ago
|
}else{
|
||
12 months ago
|
SetScrollAmount(GetScrollAmount()-vf2d{0,"ThemeGlobal.MenuScrollWheelSpeed"_F});
|
||
1 year ago
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
if(bounds.size.y-rect.size.y>0){
|
||
12 months ago
|
SetScrollAmount({GetScrollAmount().x,std::clamp(GetScrollAmount().y,-(bounds.size.y-rect.size.y),0.f)});
|
||
1 year ago
|
}else{
|
||
12 months ago
|
SetScrollAmount({GetScrollAmount().x,0});
|
||
1 year ago
|
}
|
||
12 months ago
|
|
||
12 months ago
|
std::sort(components.begin(),components.end(),[](MenuComponent*c1,MenuComponent*c2){return c1->depth>c2->depth;});
|
||
1 year ago
|
for(MenuComponent*component:components){
|
||
1 year ago
|
component->disabled=!OnScreen(component);
|
||
|
component->_Update(game);
|
||
1 year ago
|
}
|
||
1 year ago
|
|
||
|
upButton->disabled=false;
|
||
|
downButton->disabled=false;
|
||
|
if(geom2d::contains(rect,bounds)){//This means we have no reason to show a scrollbar.
|
||
|
upButton->disabled=true;
|
||
|
downButton->disabled=true;
|
||
|
}
|
||
1 year ago
|
}
|
||
12 months ago
|
inline void DrawScrollbar(ViewPort&window,vf2d parentPos,bool focused){
|
||
1 year ago
|
float spaceBetweenTopAndBottomArrows=rect.size.y-24;
|
||
|
float viewHeight=rect.size.y;
|
||
|
float totalContentHeight=bounds.size.y;
|
||
1 year ago
|
if(totalContentHeight==0)totalContentHeight=1;
|
||
1 year ago
|
float scrollBarScale=(spaceBetweenTopAndBottomArrows/totalContentHeight);
|
||
1 year ago
|
scrollBarHeight=std::min(spaceBetweenTopAndBottomArrows,viewHeight*scrollBarScale-1);
|
||
12 months ago
|
scrollBarTop=-GetScrollAmount().y*scrollBarScale+1;
|
||
1 year ago
|
|
||
|
float focusedWindowColorMult=(focused?1:"ThemeGlobal.MenuUnfocusedColorMult"_F);
|
||
|
|
||
12 months ago
|
window.FillRectDecal(rect.pos+parentPos+vf2d{rect.size.x-11.75f,scrollBarTop+12},{12,scrollBarHeight},PixelLerp(Menu::GetCurrentTheme().GetButtonCol(),Menu::GetCurrentTheme().GetHighlightCol(),scrollBarHoverTime/"ThemeGlobal.HighlightTime"_F)*focusedWindowColorMult);
|
||
|
window.DrawRectDecal(rect.pos+parentPos+vf2d{rect.size.x-11.75f,scrollBarTop+12},{12,scrollBarHeight},WHITE*focusedWindowColorMult);
|
||
1 year ago
|
}
|
||
|
|
||
12 months ago
|
virtual inline void DrawDecal(ViewPort&window,bool focused)override{
|
||
|
MenuComponent::DrawDecal(window,focused);
|
||
1 year ago
|
if(border){
|
||
12 months ago
|
window.DrawRectDecal(rect.pos,rect.size);
|
||
1 year ago
|
}
|
||
1 year ago
|
for(MenuComponent*component:components){
|
||
12 months ago
|
component->_DrawDecal(subWindow,focused);
|
||
1 year ago
|
}
|
||
11 months ago
|
if(!geom2d::contains(rect,bounds)){
|
||
|
DrawScrollbar(window,{},focused);
|
||
|
}
|
||
1 year ago
|
}
|
||
11 months ago
|
virtual bool GetHoverState(AiL*game,MenuComponent*child)override{
|
||
11 months ago
|
return geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos,rect.size},game->GetMousePos())&& //Make sure the mouse is inside the parent window component first....
|
||
|
geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos+child->rect.pos,child->rect.size},game->GetMousePos());
|
||
1 year ago
|
}
|
||
1 year ago
|
//Calculates the bounds of all components.
|
||
12 months ago
|
inline void CalculateBounds(){
|
||
|
bounds={};
|
||
1 year ago
|
for(MenuComponent*component:components){
|
||
|
if(component->rect.pos.x<bounds.pos.x){
|
||
|
float sizeIncrease=bounds.pos.x-component->rect.pos.x;
|
||
|
bounds.size.x+=sizeIncrease;
|
||
|
bounds.pos.x=component->rect.pos.x;
|
||
|
}
|
||
|
if(component->rect.right().start.x>bounds.right().start.x){
|
||
|
float sizeIncrease=component->rect.right().start.x-bounds.right().start.x;
|
||
|
bounds.size.x+=sizeIncrease;
|
||
|
}
|
||
|
if(component->rect.pos.y<bounds.pos.y){
|
||
|
float sizeIncrease=bounds.pos.y-component->rect.pos.y;
|
||
|
bounds.size.y+=sizeIncrease;
|
||
|
bounds.pos.y=component->rect.pos.y;
|
||
|
}
|
||
|
if(component->rect.bottom().start.y>bounds.bottom().start.y){
|
||
|
float sizeIncrease=component->rect.bottom().start.y-bounds.bottom().start.y;
|
||
|
bounds.size.y+=sizeIncrease;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
1 year ago
|
public:
|
||
12 months ago
|
template<class T>
|
||
12 months ago
|
T* _AddComponent(std::string key,T*button){
|
||
1 year ago
|
components.push_back(button);
|
||
|
button->renderInMain=false; //Now we are in control!
|
||
1 year ago
|
button->parentComponent=this;
|
||
11 months ago
|
button->disabled=disabled;
|
||
1 year ago
|
|
||
12 months ago
|
CalculateBounds();
|
||
1 year ago
|
|
||
12 months ago
|
Menu::menus[parentMenu]->_AddComponent(key,button);
|
||
12 months ago
|
return button;
|
||
1 year ago
|
}
|
||
1 year ago
|
virtual inline bool PointWithinParent(MenuComponent*child,vi2d drawPos)override{
|
||
|
return geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos,rect.size},drawPos);
|
||
|
}
|
||
12 months ago
|
virtual inline bool PointWithinParent(MenuComponent*child,geom2d::rect<float> drawRect)override{
|
||
|
return geom2d::overlaps(geom2d::rect<float>{Menu::menus[parentMenu]->pos+rect.pos,rect.size},drawRect);
|
||
|
}
|
||
1 year ago
|
virtual inline bool HandleOutsideDisabledButtonSelection(MenuComponent*disabledButton)override{
|
||
|
//Set the offset so the center is highlighted by this button.
|
||
12 months ago
|
SetScrollAmount(vf2d{GetScrollAmount().x,-disabledButton->rect.pos.y+disabledButton->rect.size.y});
|
||
1 year ago
|
return true;
|
||
|
};
|
||
1 year ago
|
virtual void Cleanup()override{}
|
||
1 year ago
|
inline std::vector<MenuComponent*>&GetComponents(){
|
||
|
return components;
|
||
|
}
|
||
1 year ago
|
virtual inline void Enable(bool enabled)override final{
|
||
|
disabled=!enabled;
|
||
11 months ago
|
for(MenuComponent*component:components){
|
||
|
component->Enable(enabled);
|
||
|
}
|
||
12 months ago
|
if(upButton){upButton->Enable(enabled);}
|
||
|
if(downButton){downButton->Enable(enabled);}
|
||
1 year ago
|
};
|
||
1 year ago
|
};
|