From 8e58607fb9b8e9a6e2c85bc2d00c464f7165f76c Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Wed, 10 Jan 2024 16:28:09 -0500 Subject: [PATCH] Slider component implemented. --- .../Adventures in Lestoria.vcxproj | 1 + .../Adventures in Lestoria.vcxproj.filters | 3 + Adventures in Lestoria/Menu.h | 2 +- Adventures in Lestoria/Slider.h | 120 ++++++++++++++++++ 4 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 Adventures in Lestoria/Slider.h diff --git a/Adventures in Lestoria/Adventures in Lestoria.vcxproj b/Adventures in Lestoria/Adventures in Lestoria.vcxproj index 5f27fa3f..3b252359 100644 --- a/Adventures in Lestoria/Adventures in Lestoria.vcxproj +++ b/Adventures in Lestoria/Adventures in Lestoria.vcxproj @@ -458,6 +458,7 @@ + diff --git a/Adventures in Lestoria/Adventures in Lestoria.vcxproj.filters b/Adventures in Lestoria/Adventures in Lestoria.vcxproj.filters index 5a62a907..8a4e4d8a 100644 --- a/Adventures in Lestoria/Adventures in Lestoria.vcxproj.filters +++ b/Adventures in Lestoria/Adventures in Lestoria.vcxproj.filters @@ -420,6 +420,9 @@ Header Files + + Header Files\Interface + diff --git a/Adventures in Lestoria/Menu.h b/Adventures in Lestoria/Menu.h index 2fc80d45..b5fca742 100644 --- a/Adventures in Lestoria/Menu.h +++ b/Adventures in Lestoria/Menu.h @@ -207,7 +207,7 @@ public: safemap>keyboardButtons; //Button ordered storage for keyboard/menu static Theme&GetCurrentTheme(); - bool UsingMouseNavigation(); + static bool UsingMouseNavigation(); void SetMouseNavigation(bool mouseNavigation); static void InventorySlotsUpdated(ITCategory cat); //Called whenever the player's inventory gets modified. static void MerchantInventorySlotsUpdated(ITCategory cat); //Called whenever a traveling merchant's inventory item gets updated. diff --git a/Adventures in Lestoria/Slider.h b/Adventures in Lestoria/Slider.h new file mode 100644 index 00000000..8733b907 --- /dev/null +++ b/Adventures in Lestoria/Slider.h @@ -0,0 +1,120 @@ +#pragma region License +/* +License (OLC-3) +~~~~~~~~~~~~~~~ + +Copyright 2024 Joshua Sigona + +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. + +Portions of this software are copyright © 2023 The FreeType +Project (www.freetype.org). Please see LICENSE_FT.txt for more information. +All rights reserved. +*/ +#pragma endregion +#pragma once + +#include "MenuComponent.h" +#include "AdventuresInLestoria.h" +#include "Error.h" + +INCLUDE_game +INCLUDE_GFX + +class Slider:public MenuComponent{ + bool dragging=false; + float&val; //0.f-1.f + std::pairminMaxDisplayValues; + + //A slider component with a right-aligned label to the left, and a value display on the right-hand side. + //valRef is between 0-1. + //minMaxDisplayValues are visual only. + inline Slider(const geom2d::rect&rect,const std::string&label,float&valRef,const std::pair&minMaxDisplayValues={0,100},const ButtonAttr&attributes=ButtonAttr::NONE) + :MenuComponent(rect,label,DO_NOTHING,attributes),val(valRef),minMaxDisplayValues(minMaxDisplayValues){ + if(minMaxDisplayValues.first>minMaxDisplayValues.second)ERR(std::format("WARNING! The first value provided for component {} is greater than the second! {} > {}",name,minMaxDisplayValues.first,minMaxDisplayValues.second)); + } + + //Gets the absolute X position of the slider. + float GetSliderX(){ + return rect.pos.x+rect.size.x*val; + } + + float GetSliderVal(float x){ + return (x-rect.pos.x)/rect.size.x; + } + + int GetValueDisplay(){ + int diff=minMaxDisplayValues.second-minMaxDisplayValues.first; + return diff*val+minMaxDisplayValues.first; + } + + inline virtual void Update(AiL*game)override final{ + MenuComponent::Update(game); + if(disabled)return; + + if(hovered&&(game->KEY_CONFIRM.Pressed()||game->GetMouse(Mouse::LEFT).bPressed)){ + dragging=true; + } + + if(dragging){ + if(Menu::UsingMouseNavigation()){ + val=GetSliderVal(game->GetMouseX()); + }else{ + if(game->KEY_LEFT.Held()){ + val-=1.f*game->GetElapsedTime(); + } + if(game->KEY_RIGHT.Held()){ + val+=1.f*game->GetElapsedTime(); + } + } + val=std::clamp(val,0.f,1.f); + + if(game->KEY_CONFIRM.Released()||game->GetMouse(Mouse::LEFT).bReleased){ + dragging=false; + } + } + } + + inline virtual void DrawDecal(ViewPort&window,bool focused)override final{ + Pixel backCol=PixelLerp(Menu::themes[Menu::themeSelection].GetButtonCol(),Menu::themes[Menu::themeSelection].GetHighlightCol(),hoverEffect/"ThemeGlobal.HighlightTime"_F); + if(grayedOut){ + backCol=DARK_GREY; + } + + vf2d labelSize=game->GetTextSizeProp(label); + window.DrawShadowStringPropDecal({rect.pos.x-2-labelSize.x,rect.pos.y+rect.size.y/2-labelSize.y/2},label); + + window.FillRectDecal({rect.pos.x,rect.pos.y+rect.size.y/2-1},{rect.size.x,2}); + + window.DrawRotatedDecal({GetSliderX(),rect.pos.y+rect.size.y/2},GFX["circle.png"].Decal(),0.f,vf2d(GFX["circle.png"].Sprite()->Size())/2.f,{2.f,2.f},backCol); + + std::string valDisplayText=std::to_string(GetValueDisplay()); + vf2d valDisplayTextSize=game->GetTextSize(valDisplayText); + vf2d valDisplayOutlinePos={rect.pos.x+rect.size.x+2,rect.pos.y}; + window.DrawRectDecal(valDisplayOutlinePos,valDisplayTextSize+vf2d{4,4}); + window.DrawShadowStringDecal(valDisplayOutlinePos+vf2d{2,2},valDisplayText); + } +}; \ No newline at end of file