2023-11-20 23:25:36 -06:00
# pragma region License
2023-11-14 18:11:32 -06:00
/*
License ( OLC - 3 )
~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2023-11-14 18:12:54 -06:00
Copyright 2018 - 2023 OneLoneCoder . com
2023-11-14 18:11:32 -06:00
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 .
2023-11-29 00:50:00 -06:00
Portions of this software are copyright © 2023 The FreeType
Project ( www . freetype . org ) . Please see LICENSE_FT . txt for more information .
All rights reserved .
2023-11-14 18:11:32 -06:00
*/
2023-11-20 23:25:36 -06:00
# pragma endregion
2023-10-01 01:48:27 -05:00
# pragma once
2023-10-07 15:47:26 -05:00
# include "Menu.h"
2023-11-30 23:33:40 -06:00
# include "BitwiseEnum.h"
2023-10-01 01:48:27 -05:00
2023-11-06 01:00:17 -06:00
enum class ButtonAttr {
2023-12-19 17:10:04 -06:00
NONE = 0 b000 ,
UNSELECTABLE = 0 b001 , //Makes the component unselectable.
UNSELECTABLE_VIA_KEYBOARD = 0 b010 , //Makes the component unselectable via keyboard.
FIT_TO_LABEL = 0 b100 , //Scales the text horizontally to fit the label if the text takes up more space rather than wrapping.
2023-11-06 01:00:17 -06:00
} ;
enum class ComponentAttr {
2023-12-21 10:26:18 -06:00
NONE = 0 b000000 ,
LEFT_ALIGN = 0 b000001 , //Labels are centered by default.
SHADOW = 0 b000010 , //Adds shadows to the label text.
OUTLINE = 0 b000100 , //Adds an outline around the component.
BACKGROUND = 0 b001000 , //Renders the background of the menu theme for this component.
FIT_TO_LABEL = 0 b010000 , //Scales the text horizontally to fit the label if the text takes up more space rather than wrapping.
2023-11-06 01:00:17 -06:00
} ;
2023-12-10 23:02:16 -06:00
enum class SelectionType {
CROSSHAIR ,
HIGHLIGHT ,
INNER_BOX ,
2023-12-16 22:17:49 -06:00
NONE ,
2023-12-10 23:02:16 -06:00
} ;
2023-12-16 22:17:49 -06:00
using SelectionType : : CROSSHAIR ;
using SelectionType : : HIGHLIGHT ;
using SelectionType : : INNER_BOX ;
2023-12-10 23:02:16 -06:00
2023-12-08 16:36:51 -06:00
class MenuComponent : public IAttributable {
2023-10-01 01:48:27 -05:00
friend class Menu ;
2023-10-07 19:06:56 -05:00
friend class MenuItemButton ;
2023-10-12 18:35:45 -05:00
friend class ScrollableWindowComponent ;
2023-10-18 18:07:10 +00:00
friend class InventoryScrollableWindowComponent ;
2023-11-14 23:20:13 -06:00
friend class MenuItemItemButton ;
2023-12-16 21:28:41 -06:00
friend class RowItemDisplay ;
2023-10-01 01:48:27 -05:00
MenuType menuDest ;
2023-12-16 21:28:41 -06:00
MenuFunc onHover = [ ] ( MenuFuncData dat ) { return true ; } ;
MenuFunc onMouseOut = [ ] ( MenuFuncData dat ) { return true ; } ;
bool hoverState = false ;
bool runHoverFunctions = false ;
2023-10-01 01:48:27 -05:00
private :
2023-10-12 19:35:16 -05:00
virtual bool GetHoverState ( Crawler * game ) ;
2023-10-23 00:05:30 -05:00
std : : pair < MenuType , std : : string > memoryLeakInfo ; //Used to identify memory leak hints for this component.
2023-12-16 21:28:41 -06:00
void _BeforeUpdate ( Crawler * game ) ;
2023-12-08 16:36:51 -06:00
virtual void BeforeUpdate ( Crawler * game ) ;
void _Update ( Crawler * game ) ;
2023-12-14 04:18:05 -06:00
void _DrawDecal ( ViewPort & window , bool focused ) ;
2023-12-16 21:28:41 -06:00
void _OnMouseOut ( ) ;
void _OnHover ( ) ;
2023-12-10 23:02:16 -06:00
SelectionType selectionType = CROSSHAIR ;
2023-10-01 01:48:27 -05:00
protected :
2023-12-09 01:58:46 -06:00
int depth = 0 ;
2023-10-24 04:52:24 -05:00
float hoverEffect = 0 ;
2023-10-15 14:59:35 -05:00
std : : string name = " " ;
2023-10-01 01:48:27 -05:00
geom2d : : rect < float > rect ;
2023-12-15 23:57:09 -06:00
vf2d originalPos ;
2023-10-03 02:34:26 -05:00
std : : string label ;
bool border = true ;
2023-10-07 16:26:03 -05:00
bool draggable = false ;
2023-10-22 10:35:53 -05:00
bool background = true ;
bool showDefaultLabel = true ;
2023-10-07 18:28:19 -05:00
MenuFunc onClick ;
2023-10-07 19:06:56 -05:00
bool hovered = false ;
2023-10-12 18:35:45 -05:00
bool selectable = true ;
2023-10-17 00:31:56 -05:00
bool selectableViaKeyboard = true ;
2023-12-16 22:17:49 -06:00
bool selected = false ;
2023-10-12 19:35:16 -05:00
bool disabled = false ; //If set to true, this component will not be rendered or updated.
2023-10-12 18:35:45 -05:00
bool renderInMain = true ; //If set to false, this component is the responsibility of some other windowing system and won't be rendered or updated via the main window loop.
2023-10-17 05:35:19 -05:00
bool valid = true ; //If set to false, this would cause the component to be removed.
2023-12-19 17:10:04 -06:00
bool fitToLabel = false ; //Will shrink text horizontally to fit the size of the label if the display text is too large.
2023-12-19 18:46:59 -06:00
bool grayedOut = false ; //If turned on, a button will appear grayed out and unresponsive.
2023-12-22 06:14:37 -06:00
int inventoryIndex = 0 ;
2023-12-10 20:14:32 -06:00
vf2d labelScaling = { 1 , 1 } ;
2023-10-01 01:48:27 -05:00
virtual void Update ( Crawler * game ) ;
2023-12-15 23:57:09 -06:00
virtual void DrawDecal ( ViewPort & window , bool focused ) ;
2023-10-12 19:35:16 -05:00
virtual bool GetHoverState ( Crawler * game , MenuComponent * child ) ;
2023-10-15 12:58:39 -05:00
virtual void AfterCreate ( ) ; //Called after the creation of all menus finish.
2023-11-14 23:20:13 -06:00
//CALL THIS FOR A PARENT to check a child's DrawDecal validity!
2023-12-14 01:43:42 -06:00
virtual bool PointWithinParent ( MenuComponent * child , vi2d drawPos ) ;
//CALL THIS FOR A PARENT to check a child's DrawDecal validity!
virtual bool PointWithinParent ( MenuComponent * child , geom2d : : rect < float > drawRect ) ;
2023-12-16 21:28:41 -06:00
virtual void OnMouseOut ( ) ;
virtual void OnHover ( ) ;
2023-10-12 19:35:16 -05:00
public :
2023-12-08 16:36:51 -06:00
MenuType parentMenu = MenuType : : ENUM_END ;
MenuComponent * parentComponent = nullptr ;
2023-12-10 19:14:37 -06:00
MenuComponent ( geom2d : : rect < float > rect , std : : string label , MenuFunc onClick , ButtonAttr attributes = ButtonAttr : : NONE ) ;
MenuComponent ( geom2d : : rect < float > rect , std : : string label , MenuType menuDest , MenuFunc onClick , ButtonAttr attributes = ButtonAttr : : NONE ) ;
2023-12-10 20:14:32 -06:00
MenuComponent ( geom2d : : rect < float > rect , std : : string label , MenuType menuDest , MenuFunc onClick , vf2d labelScaling , ButtonAttr attributes = ButtonAttr : : NONE ) ;
2023-10-22 23:19:47 -05:00
virtual ~ MenuComponent ( ) ;
2023-10-17 05:35:19 -05:00
vf2d GetPos ( ) ;
2023-10-07 18:28:19 -05:00
//We picked up a draggable component, we should make a copy and return it here. If a nullptr is returned here, the pickup is not allowed.
2023-10-07 19:06:56 -05:00
//WARNING!!! This allocates a brand new component when successful!!! Be prepared to clear it!
2023-10-07 18:28:19 -05:00
virtual MenuComponent * PickUpDraggableItem ( ) ;
//We are attempting to drop draggable onto this item. If it's not allowed, return false.
virtual bool DropDraggableItem ( MenuComponent * draggable ) ;
2023-10-17 00:50:58 -05:00
//A notification that a button outside the region has been selected. Return false if it's not allowed.
virtual bool HandleOutsideDisabledButtonSelection ( MenuComponent * disabledButton ) ;
2023-10-18 16:06:08 +00:00
//Called whenever an inventory slot gets updated, whether it's adding or removing an item.
virtual void OnInventorySlotsUpdate ( ITCategory cat ) ;
2023-12-06 22:47:09 -06:00
//Called whenever equipment and base stats are updated, notifying a component that numbers that may be displayed have changed.
virtual void OnEquipStatsUpdate ( ) ;
2023-10-24 03:03:34 -05:00
std : : string GetLabel ( ) ;
2023-11-12 23:51:49 -06:00
std : : string GetName ( ) ;
2023-12-10 23:02:16 -06:00
virtual void SetSelected ( bool selected ) final ;
virtual void SetSelectionType ( SelectionType selectionType ) final ;
2023-12-01 22:55:33 -06:00
virtual void Enable ( bool enabled ) ;
2023-11-11 04:03:48 -06:00
virtual void Cleanup ( ) ;
2023-12-16 21:28:41 -06:00
virtual void SetHoverFunc ( std : : function < bool ( MenuFuncData ) > func ) ;
virtual void SetMouseOutFunc ( std : : function < bool ( MenuFuncData ) > func ) ;
2023-12-19 18:46:59 -06:00
void SetGrayedOut ( bool grayedOut ) ;
virtual void OnPlayerMoneyUpdate ( uint32_t newMoney ) ;
2023-11-30 23:33:40 -06:00
} ;